1

I have 165 hdf files(monthly data) of EVI(Enhanced Vegetation Index) , I need to convert them to a geotiff format. In ArcMap 10.1 I can only extract the data one file at a time.

I have this python code for extracting the data. The problem is it only extracts 11 tiles of one month that covers the study area. Say for example January only. How do I change it so that it accounts for all 165 files at one go.

import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = 1
arcpy.CheckOutExtension("Spatial")

inPath='D:/Input/'
outPath='D:/OutPut/'
env.workspace = inPath
arcpy.env.scratchWorkspace = inPath
hdfList = arcpy.ListRasters('*','HDF')

for hdf in hdfList:
    eviName=hdf[8:16] + ".tif"
    print "Subsetting EVI band from ....."+str(hdf)
    data1=arcpy.ExtractSubDataset_management(hdf,outPath + hdf[8:16] + ".tif", "1")
print "Done"
  • How is your data laid out and named? It seems to me you could be overwriting previous iteration. Have a look at os.path.join to concatenate your outPath + hdf[8:16] + ".tif" (like os.path.join(outPath, hdf[8:16] + ".tif")) it's more pythonic and portable so it's a good habit to get into. – Michael Stimson Jan 6 '16 at 5:40
  • All my hdf files are in the folder 'D:/Input/' and each file has the following naming system "MYD13A3.A2013335.h20v05.006.2015273105718.hdf" – Peter M Macharia Jan 6 '16 at 6:06
  • does your loop go through all files ? I see that you print the names – radouxju Jan 6 '16 at 14:16
  • it goes through all the files but the results are not all files – Peter M Macharia Jan 6 '16 at 16:43
  • there are several dots in your file names, maybe this could be an issue when you try to create the output file name. (see suggestion in my answer) – radouxju Jan 7 '16 at 6:58
2

you can right click on the extract subdataset tool and select "batch". Then you can open all files (right click on input, browse option) and launch the extraction for them (clicking "validate" will automatically create valid output names).

enter image description here

As for your Python script, it looks OK but I would use a more robust code for the output file name (os.path.join as mentioned by @Michael Miles Stimson and split to avoid counting the character with the risk of something like "..tif"

os.path.join(outPath, hdf.split(".")[1] + ".tif"))
  • The command works but I have to add each raster one by one. Is there a way to add all of them at a go? – Peter M Macharia Jan 6 '16 at 16:47
  • I've added more details on the use of "batch", hope this helps. – radouxju Jan 7 '16 at 7:02
1

I have been facing problems with these HDF file format of MODIS. Right now i have written script in arcpy which does same thing as MRT tool does. If your problem still persists please download code from this link of mine. Its my github. An modify the code as you wish to. Presently it can handle 5GB of HDF in one run. I am working on another project where more then 16 GB of data has to be processed.

https://github.com/HGIS4YOU/MODIS_DATA_NDVI_Arcpy-code.git

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.