2

I have MOD13Q1 NDVI images. Data are named in this format:

MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif  

(YYYYDDD) Here 2007 is a year and 049 is day in year.

I have to rename it in this format: (YYYY.MM.DD) 2007.02.18

I am renaming it manually for every single file. How to perform a batch process for renaming all the files?

3

Suppose the following files:

MOD13Q1.A2007049.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif
MOD13Q1.A2007051.h26v06.006.2015161224938_MODIS_Grid_16DAY_250m_500m_VI.tif

and that they are stored in directory C:/modis_files

Run:

setwd('C:\\modis_files') #path to folder where MODIS files are.

file.rename(list.files(), paste(as.Date(substr(list.files(),10,16),"%Y%j"),".tif", sep=""))

It will result in:

2007-02-18.tif
2007-02-20.tif

If you want to keep the entire file name, while only replacing the [year + julian days], then Bharadwaj A K's answer is the one.

  • 2
    Nice use of the - for the renamed file. Often Windows machines do not handle . characters when used other than the file extension. – Aaron Jan 1 at 19:10
2

If you were to use R language (open source), this will work.

setwd("C:/modis") #path of modis files
li<-as.data.frame(list.files(pattern = ".tif|.TIF"))
li$nn<-paste0(substr(li[,1],1,9),format(as.Date(substr(li[,1],10,16), "%Y%j"),"%Y.%m.%d"),substr(li[,1],17,75))
for(i in 1:nrow(li)){
  file.rename(as.character(li[i,1]),li[i,2])
}
2

For anyone that wants a Python approach, try the following:

import datetime, os, glob

inws = '/path/to/tiff/workspace'

tiffs = glob.glob(os.path.join(inws, "*.tif"))

for tiff in tiffs:
    julien_date = os.path.basename(tiff)[11:16] # Extract the julien date string
    date = datetime.datetime.strptime(julien_date, '%y%j').date() # Format date
    outname = os.path.join(inws, date.strftime('%y-%m-%d') + ".tif") # Create outfile name and path
    os.rename(tiff, outname) # Rename the files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.