convert data modis EVI to date yy-mm-dd in r

277 Views Asked by At

I work with MODIS EVI rasters in 2000. I have 6 raster by years, one raster by month :

"D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000209_aid0001.tif"
"D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000225_aid0001.tif"
"D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000241_aid0001.tif"
"D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000257_aid0001.tif"
"D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000273_aid0001.tif"
"D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000289_aid0001.tif"
"D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000305_aid0001.tif"
"D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000321_aid0001.tif"
"D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000337_aid0001.tif"
"D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000353_aid0001.tif"

I would like convert the month like that 2000-02-09 but I don't know how to do it.

1

There are 1 best solutions below

0
On

Looks like your datetime string format follows year(4digits)day_of_year(3digit) format, e.g. 2000209 means 2000 year & 209 days (27th July 2000). If it's true then the problem isn't difficult:

  • Extract those seven digit numbers. (str_extract)
  • Parse 'datetime' from it. (you need to know that j is used for parsing date from the day_of_year.)
  • [:Graph:] will drop anything other than numbers and punctuation marks.

Code

dt <- data.frame(string =
     c("D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000209_aid0001.tif",
       "D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000225_aid0001.tif",
       "D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000241_aid0001.tif",
       "D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000257_aid0001.tif",
       "D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000273_aid0001.tif",
       "D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000289_aid0001.tif",
       "D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000305_aid0001.tif",
       "D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000321_aid0001.tif",
       "D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000337_aid0001.tif",
       "D:/Rteledetection/Pivots/MODIS/MOD13Q1.006__250m_16_days_EVI_doy2000353_aid0001.tif"))

dt$string %>% str_extract('\\d{7}') %>% str_replace('2000', '2000-') %>% 
   parse_date_time('y-j') %>% str_subset('[:graph:]')

Output

[1] "2000-07-27" "2000-08-12" "2000-08-28" "2000-09-13" "2000-09-29" "2000-10-15"
[7] "2000-10-31" "2000-11-16" "2000-12-02" "2000-12-18"