HDF to tiff : Extract in folders based on HDF file names

4441
5
11-17-2015 08:54 AM
ZiaAhmed
New Contributor III

Hi,

I am processing  190s HDF files  from a Folder (J:\RapidEyr_Atcor\HDF).  Here is the example of file name:

AtmosCorrected_4549227_2014-12-23_RE1_3A_278182.hdf

AtmosCorrected_4549227_2015-02-15_RE1_3A_278163.hdf

AtmosCorrected_4549227_2015-01-13_RE1_3A_278182.hdf

AtmosCorrected_4549227_2015-03-23_RE1_3A_278102.hdf

Each HDF  file contains 13 bands. But I want to extract the first  5 bands from each of  HDF file and   save as TIFF files in a folder created with [16:46] characters of  corresponding HDF file.

Folder and files arrangement as like as below:

J:\RapidEyr_Atcor\TIF \4549227_2015-02-15_RE1_3A_278163

          4549227_2015-02-15_RE1_3A_278163_b1. tif,

          4549227_2015-02-15_RE1_3A_278163_b2.tif,

          4549227_2015-02-15_RE1_3A_278163_b3.tif,

          4549227_2015-02-15_RE1_3A_278163_b4.tif,

          4549227_2015-02-15_RE1_3A_278163_b5.tif

J:\RapidEyr_Atcor\TIF \4549227_2015-01-13_RE1_3A_278182

          4549227_2015-01-13_RE1_3A_278182_b1.tif

          4549227_2015-01-13_RE1_3A_278182_b2.tif

           4549227_2015-01-13_RE1_3A_278182_b3.tif

          4549227_2015-01-13_RE1_3A_278182_b4.tif

          4549227_2015-01-13_RE1_3A_278182_b4.tif

I think, first I have to create a output folder and name it with [16:46] characters of  one  HDF file, then apply arcpy.ExtractSubDataset for selecting the first five bands (0:4) from the corresponding HDF file.  And I have to run this process in a loop for all HDF files.

If anyone help me to modify following codes to process all HDF files with batch mode. 

Thanks

Zia

  1. import arcpy 
  2. try: 
  3. arcpy.env.workspace = r"J:\RapidEyr_Atcor\HDF" 
  4. rasterListA = arcpy.ListRasters() 
  5. for raster in rasterListA: 
  6. tifOutA="J:\\RapidEyr_Atcor\\TIFF"+raster[16:46] 
  7. arcpy.ExtractSubDataset_management(raster,tifOutA+"_B1"+".tif", "0"
  8. arcpy.ExtractSubDataset_management(raster,tifOutA+"_B2"+".tif", "1"
  9. arcpy.ExtractSubDataset_management(raster,tifOutA+"_B3"+".tif", "2"
  10. arcpy.ExtractSubDataset_management(raster,tifOutA+"_B4"+".tif", "3"
  11. arcpy.ExtractSubDataset_management(raster,tifOutA+"_B5"+".tif", "4"
  12. except: 
  13. print "Extract Subdataset example failed." 
  14. print arcpy.GetMessages() 
Tags (1)
0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

could you format your code using one of the methods here Code Formatting... the basics++

your indentation is either incorrect or it didn't get carried over during the copy-paste

try:
    blah blah
    for bl in blah:
        blah blah
except
    blah blah
0 Kudos
ZiaAhmed
New Contributor III

Thank. I do not understand want you want to say, i asking help for how to use file names create folders in a loop.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

True...but your code won't work since it isn't formatted properly because of indentation issues, which, if run should have generated error messages, and if they did, they would be useful to see.  Once, that is fixed, then it is a matter of dissecting the code. tifoutA  that should be indented as well, and I presume it is printing out a name but you are using a slice of the input name.  Through a print statement in after you have the name to make sure the name is correct.  So if you can provide documentation via print statements and error messages that would be great.

0 Kudos
ZiaAhmed
New Contributor III

Hi Dan,

I am able to extract bands  from HDF, but I have to run three python scripts: (1) First one create folders based on the name of HDF files and import HDF files in respective folders (2) Then 2nd Script extract  first 5 bands in folder and (3) 3rd one create a multi-tif file. This there anyway combine three scripts to make one?

I am going post all codes in another threads, Thanks again

Zia

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

Hi Zia, since you put all the info in a different thread and are getting responses, you may want to delete this one so it doesn't remain an unanswered thread.  You won't be able to delete it from within your geonet inbox, but should be able to if you open it up Ina new tab (click on the title if going thru the inbox).  Thanks.

0 Kudos