1
|
# renames MODIS files from a year and day to year.month.day
|
2
|
# ---------------------------------------------------------------------------
|
3
|
# Developed by John Donoghue II
|
4
|
# 620 October, 2010
|
5
|
|
6
|
# Convert MODIS Day of Year to Date File Name.py
|
7
|
|
8
|
import sys
|
9
|
import string
|
10
|
import os
|
11
|
import shutil
|
12
|
|
13
|
sourceDir = "/data/project/organisms/MODIS_LST_Oregon/ClearDayGDAL/ClearDay_Original_IMG_Extracts"
|
14
|
outputDir = "/data/project/organisms/MODIS_LST_Oregon/ClearDayGDAL/ClearDay_Original_IMG_Extracts/ByDate"
|
15
|
|
16
|
# Loop through the folder
|
17
|
for dirname, dirnames, filenames in os.walk(sourceDir):
|
18
|
for filename in filenames:
|
19
|
ext = os.path.splitext(filename)[1]
|
20
|
if ext == ".img":
|
21
|
print "processing " + filename
|
22
|
|
23
|
# process the file
|
24
|
inFile = sourceDir + "/" + filename
|
25
|
|
26
|
# rename the file to the month and day
|
27
|
theYear = filename[9:13]
|
28
|
theDay = filename[13:16]
|
29
|
prefix = filename[0:9]
|
30
|
tile = filename[17:27]
|
31
|
|
32
|
# convert day to month and year
|
33
|
theYear = int(theYear)
|
34
|
theDay = int(theDay)
|
35
|
from datetime import date
|
36
|
myDate = date.fromordinal(date(theYear, 1, 1).toordinal() + theDay - 1)
|
37
|
|
38
|
myYear = myDate.year
|
39
|
myMonth = myDate.month
|
40
|
myDay = myDate.day
|
41
|
|
42
|
outFile = prefix + "." + str(myYear) + "." + str(myMonth) + "." + str(myDay) + "." + tile + ".ClearDay.img"
|
43
|
print ".. Writing " + outFile
|
44
|
|
45
|
#print theYear + ' ' + theDay
|
46
|
outFile = outputDir + "/" + outFile
|
47
|
|
48
|
# remove outfile if present
|
49
|
if not os.path.exists(outFile) == True:
|
50
|
#os.remove(outFile)
|
51
|
shutil.copy2(inFile,outFile)
|
52
|
|
53
|
# Process: Extract Subdataset
|
54
|
#print " saving " + filename[0:27] + "_ClearDay.img"
|
55
|
#myCommand = "gdal_translate -of HFA HDF4_EOS:EOS_GRID:" + filename + ":MODIS_Grid_Daily_1km_LST:Clear_day_cov " + outFile
|
56
|
#output = os.popen(myCommand).read() # shows the ouput data of the command
|
57
|
|
58
|
print "Finished"
|