On Monday, January 27, 2014 4:22:26 PM UTC+8, Brent Fallarcuna wrote:
> Hello everyone!
>
>
>
> I'm extracting values on a specific pixel from hundreds of hdf files and used for loop.
>
>
>
> This is my code:
>
>
>
> ;Daily NPP/GPP Plotting
>
> cd, ('C:\Users\Brent\Documents\NPP Annual Files\MODIS17A2v5')
>
> filelist = file_search('*.*.h29v07.005.*.hdf')
>
> i = 0
>
> arr = strarr(1,637)
>
>
>
> FOR i = 0, 636 DO BEGIN
>
> ; Open HDF and Import SDS
>
> filename = filelist[i]
>
>
>
> ;access hdf file
>
> hdf_id = hdf_sd_start(filename);this is where my program stops
>
>
>
> ;return the index of 'Npp_1km' variable
>
> index = hdf_sd_nametoindex(hdf_id, 'PsnNet_1km')
>
>
>
> ;access the dataset
>
> varid = hdf_sd_select(hdf_id, index)
>
>
>
> ;read the contents of the variable
>
> hdf_sd_getdata, varid, data
>
>
>
> ;endaccess
>
> hdf_sd_endaccess, varid
>
> hdf_sd_end, hdf_id
>
>
>
> ;rotate hdf
>
> data = rotate(data, 7)
>
> ;scale the data
>
> kaliwa_tile = data * (0.0001)
>
> point = kaliwa_tile[880,566]
>
>
>
> print, point
>
> arr[0,i] = point
>
> ENDFOR
>
>
>
> cd, ('C:\Users\Brent\Documents\MOD17A3UTM')
>
> openw, lun, 'outputfilefor8dayPsnNet1km.csv', /get_lun
>
> printf, lun, arr, format= '(a)'
>
> close, lun
>
> free_lun, lun
>
>
>
> END
>
>
>
>
>
> After the values from several hdfs are printed, there's an error on the console saying "HDF_SD_START: Unable to start the HDF-SD interface."
>
> As I checked the variables tab, the i stopped at the 24th hdf file. As I checked the file using hdf browser, it didn't open, thus the file seems to be corrupted.
>
>
>
> My question is, can I continue my for loop and proceed reading the other uncorrupted hdf files every time I got the said error?
>
>
>
> I've tried catch statement but I can't figure it out on how to use and implement it correctly.
>
>
>
>
>
> Brent
Hello.
I've run this code, wherein I inserted the for loop. The output on the command line says "MOD17A2.A2000065.h29v07.005.2010162162935.hdf
Attempt to subscript FILENAME with K is out of range." and it only prints a single value.
Perhaps IDL is confused with my code structure. Is it ok to use for loop within the for loop?
What I needed is to identify those corrupted files (to re-download them again) and put the values in my csv output file.
;Daily NPP/GPP Plotting
cd, ('C:\Users\Brent\Documents\NPP Annual Files\MODIS17A2v5')
filelist = file_search('*.*.h29v07.005.*.hdf')
k = 0
arr = strarr(1,637)
FOR k = 0, 636 DO BEGIN
; Open HDF and Import SDS
filename = filelist[k]
for i=0, 636 do begin ;<====This is where I inserted the for loop
;establish error handler
catch, error
if error ne 0 then begin
print, filename[i]
print, !error_state.msg
;continue with next item in loop if error was encountered
catch, /cancel
continue
endif
;access hdf file
hdf_id = hdf_sd_start(filename[k])
catch, /cancel
;Do other stuff with file here if no error was encountered.
;return the index of 'Npp_1km' variable
index = hdf_sd_nametoindex(hdf_id, 'PsnNet_1km')
;access the dataset
varid = hdf_sd_select(hdf_id, index)
;read the contents of the variable
hdf_sd_getdata, varid, data
;endaccess
hdf_sd_endaccess, varid
hdf_sd_end, hdf_id
;rotate hdf
data = rotate(data, 7)
;scale the data
kaliwa_tile = data * (0.0001)
point = kaliwa_tile[880,566]
print, point
arr[0,k] = point
endfor
ENDFOR
cd, ('C:\Users\Brent\Documents\MOD17A3UTM')
openw, lun, 'outputfilefor8dayPsnNet1km.csv', /get_lun
printf, lun, arr, format= '(a)'
close, lun
free_lun, lun
END
Brent
|