I have downloaded numerous hdf files from an ftp site. Let’s say I already have 500 hdf files, 250 of them were level 1 hdf files and the rest were level 2. The level 1 format is: A2001031505000.L1A_LAC.sample.hdf and the level 2 format is: A2001017054000.L2_LAC.sample.hdf
As what you will observe they differ in this part L1A_LAC, L2_LAC. Now, how will I do a batch process in projecting all the level 2 hdf files only? How will I filter these files and create a new directory for the filtered files?
Any help will be appreciated.
Thanks!
Here’s what I have so far:
def inputData(self, event):
def findSDS (path, filter):
for root, dirs, files in os.walk(path):
for file in fnmatch.filter(files, filter):
yield file
for sds in findSDS('/path/to/file', '*.hdf'):
inSDS = 'HDF4_SDS:' + '/path/to/file' + '/' + sds + ':01'
path = 'D:/new/dir'
outTIFF = path + sds + '.tif'
cmd = 'gdalwarp -geoloc -t_srs "+proj=longlat +datum=WGS84" -te 113.205 1.120 157.105 2.005 %s %s' % (inSDS, outTIFF)
os.system(cmd)
os.mkdir(path)
UPDATE:(tried calling .bat from the python script and run the script. And it just created an empty/new directory(out_path). Tried calling the .bat from the cmd line and here’s the output:
C:UserDesktopsample>projected.bat
C:UserDesktopsample>set in_path=D:pathtofile
C:UserDesktopsample>set out_path=D:newfolder
C:UserDesktopsample>md D:newfolder
A subdirectory or file D:newfolder already exists.
C:UserDesktopsample>cd D:pathtofile
C:UserDesktopsample>FORFILES /m *L2_LAC*.hdf /C "cmd /c gdalwarp -geoloc -t_srs "+proj=longlat +datum=WGS84" -te 113.205 1.120 157.105 2.005 @path D:newfolder@fname.tif"
ERROR: Invalid argument/option - '+datum=WGS84 -te 113.205 1.120 157.105 2.005 @path D:newfolder@fname.tif'.
Type "FORFILES /?" for usage.
UPDATE 2:
C:UserDesktopsample>projected.bat
C:UserDesktopsample>set in_path=D:pathtofile
C:UserDesktopsample>set out_path=D:newfolder
C:UserDesktopsample>md D:newfolder
C:UserDesktopsample>cd D:pathtofile
C:UserDesktopsample>FORFILES /m *L2_LAC*.hdf /C "cmd /c gdalwarp -geoloc -t_srs "+proj=longlat +datum=WGS84" -te 113.205 1.120 157.105 2.005 @path D:newfolder@fname.tif"
ERROR: Files of type "*L2_LAC*.hdf" not found.
UPDATE 3:
C:UserDesktopsample>projected.bat
C:UserDesktopsample>set in_path=D:pathtofile
C:UserDesktopsample>set out_path=D:newfolder
C:UserDesktopsample>md D:newfolder
C:UserDesktopsample>cd /d D:pathtofile
C:UserDesktopsample>FORFILES /m *L2_LAC*.hdf /C "cmd /c gdalwarp -geoloc -t_srs "+proj=longlat +datum=WGS84" -te 113.205 1.120 157.105 2.005 @path D:newfolder/@fname.tif"
ERROR 1: Translating source or target SRS failed:
'+proj=longlat
Tried running the gdalwarp in the cmd line and it was successful:
C:UserDesktopsample>gdalwarp -geoloc -t_srs "+proj=longlat +datum=WGS84" -te 113.205 1.120 157.105 2.005 HDF4_SDS:hdf:"A2001017054000.L2_LAC.sample.hdf":01 sample.tif
Creating output file that is 1941P x 1670L.
Processing input file HDF4_SDS:hdf:"A2001017054000.L2_LAC.sample.hdf":01.
0...10...20...30...40...50...60...70...80...90...100 - done.