I made a code to calculate daily data from MODIS 8-day data, as it is known the last data of the year is always divided by 5 or 6 and not by 8 depending on whether it is a leap year or not so I added it, the problem is the result because I have repeated dates and other missing ones, for example September 20 is repeated twice but I don't have 21. How could I solve this?
So that the code is well understood, the first thing I did was change the crs of my rasters one by one because I had errors applying it directly to the stack at the end, this worked fine, and then with that list with my reprojected rasters I worked with the loop I have doubts
Here is my code
library(raster)
library(terra)
library(lubridate)
archivos_ET <- list.files(path = "/", pattern = "ET_\\d{4}_\\d{3}", full.names = TRUE)
#Save all raster et files in a list called et_proy
# Create a list to store the daily rasters
et_dia <- list()
for (i in 1:length(et_proy)) {
# # Load the raster
raster_actual <- et_proy[[i]]
# Get the year and day of the current file name
partes_nombre <- strsplit(basename(archivos_ET[i]), "_")[[1]]
ano <- as.integer(partes_nombre[2])
dia <- as.integer(gsub(".tif", "", partes_nombre[3]))
# Check if the year is a leap year
es_bisiesto <- ifelse(ano %% 4 == 0 & (ano %% 100 != 0 | ano %% 400 == 0), TRUE, FALSE)
# Determine the divisor according to whether it is a leap year and the day is 361 (it is the last day of the modis products in a year)
divisor <- ifelse(dia == 361, ifelse(es_bisiesto, 6, 5), 8)
# Loop to split the current raster into daily rasters and store them
for (j in 1:(divisor-1)) {
# Calcular la fecha del raster diario
fecha <- as.Date(paste(ano, "-01-01", sep = "")) + dia + j - 1
# Calculate date from daily raster
raster_diario <- raster_actual / divisor
# Assign date to daily raster
names(raster_diario) <- format(fecha, "%Y-%m-%d")
# Add the daily raster to the list
et_dia[[length(et_dia) + 1]] <- raster_diario
}
# Calculate the date of the last raster
fecha_ultimo <- as.Date(paste(ano, "-01-01", sep = "")) + dia + divisor - 2
# Calculate the last raster
raster_ultimo <- raster_actual / divisor
# Assign date to last raster
names(raster_ultimo) <- format(fecha_ultimo, "%Y-%m-%d")
# Add the last raster to the list
et_dia[[length(et_dia) + 1]] <- raster_ultimo
}
And here is an example of my result
[[993]]
class : RasterLayer
dimensions : 33, 56, 1848 (nrow, ncol, ncell)
resolution : 346, 463 (x, y)
extent : 602219.9, 621595.9, 5353158, 5368437 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs
source : memory
names : X2016.09.20
values : 0.6063787, 2.016294 (min, max)
[[994]]
class : RasterLayer
dimensions : 33, 56, 1848 (nrow, ncol, ncell)
resolution : 346, 463 (x, y)
extent : 602219.9, 621595.9, 5353158, 5368437 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs
source : memory
names : X2016.09.20
values : 0.6063787, 2.016294 (min, max)
[[995]]
class : RasterLayer
dimensions : 33, 56, 1848 (nrow, ncol, ncell)
resolution : 346, 463 (x, y)
extent : 602219.9, 621595.9, 5353158, 5368437 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs
source : memory
names : X2016.09.22
values : 1.3125, 2.177313 (min, max)
[[996]]
class : RasterLayer
dimensions : 33, 56, 1848 (nrow, ncol, ncell)
resolution : 346, 463 (x, y)
extent : 602219.9, 621595.9, 5353158, 5368437 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs
source : memory
names : X2016.09.23
values : 1.3125, 2.177313 (min, max)
I also realized that the first data of my series of years, that is, 2014-01-01, is not there either.