This is similar to another question recently asked, but the details are different. To start with I have 80 .dat which represent the NDII vegetative index and I want to subset each of these with 53 roi's. To do this I am pretty sure I need two loops, the first which opens the 80 .dat files, and the second which opens and applies the 53 roi's. The code I have now will do this for the first .dat file and apply all roi's like I want but after that it fails to loop through again for the rest of the files. My code I am using is this:
pro allot_roi
e=ENVI()
cd, current = first_dir
;location where all 80 NDII VI's are located
cd, 'H:\Sheyenne\Atmospherically Corrected Landsat\Indices\Main\no_trees\NDII'
files = FILE_SEARCH('*.dat', count=count)
cd, first_dir
;output location to save roi subsets to
thisdir = 'H:\Sheyenne\ROI\subset_roi'
;initiate loop on VI files
foreach file, files do begin
;input raster
infile = file
infile_path = FILEPATH(infile,ROOT_DIR='H:\Sheyenne\Atmospherically Corrected Landsat\Indices\Main\no_trees\NDII')
;save the date located within filename for later use
date = strmid(infile_path,15,7, /REVERSE_OFFSET)
;Load the input raster
input_raster = e.OpenRaster(infile_path)
;set new pathway where roi files are located
cd, 'H:\Sheyenne\ROI\Allotments'
files = FILE_SEARCH('*.roi', count=count)
foreach file, files do begin
;input roi
infile2=file
infile_path2=FILEPATH(infile2, ROOT_DIR='H:\Sheyenne\ROI\Allotments')
;load input roi
rois=e.OpenRoi(infile_path2)
;set name and location for output file which is the subseted data
;get the name of the roi without extension for later use
allotment=strmid(infile2, 0, strpos(infile2,'.'))
outfile = 'NDII_' + date +'_' + allotment + '_'+ '.dat'
outfile_path = thisdir + path_sep() + outfile
;we cannot write to the output file if it exists, so we need to delete it
if (file_which(thisdir, outfile) ne '') then FILE_DELETE, outfile_path
;get pixel locations on the raster for each roi
roiPixels=rois.PixelAddresses(input_raster)
Xmin=min(roiPixels[0,*])
Xmax=max(roiPixels[0,*])
Ymin=min(roiPixels[1,*])
Ymax=max(roiPixels[1,*])
;subset the raster based on these pixels
rastersmall=ENVIRaster(URI='outfile_path', INHERITS_FROM=input_raster)
rastersmall=input_raster.subset(SUB_RECT=[Xmin,Ymin,Xmax,Ymax])
;save the file
rastersmall.Export,outfile_path, 'ENVI'
endforeach
endforeach
print, 'Done Processing'
end
What am I missing to complete the loop for the rest of the files?
EDIT:
I figured it out, I need to change this:
cd, 'H:\Sheyenne\ROI\Allotments'
files = FILE_SEARCH('*.roi', count=count)
foreach file, files do begin
to this:
cd, 'H:\Sheyenne\ROI\Allotments'
files2 = FILE_SEARCH('*.roi', count=count)
foreach file, files2 do begin
I was reusing variables so It caused a problem.
|