X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 23 Oct 2015 05:18 PM by  anon
Implementing a loop within a loop
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
23 Oct 2015 05:18 PM
    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.

    Zachary Norman



    Basic Member


    Posts:173
    Basic Member


    --
    26 Oct 2015 11:43 AM
    <p>Hi Stefano,</p> <p><br /> </p> <p>You may want to be careful with your naming convention for your foreach loops. I mention this because it could cause you problems and it would cause you problems if you had a nested for loop. Here is an example:</p> <p>for i=0,9 do begin<br /> &nbsp; &nbsp; print,i<br /> &nbsp; &nbsp; for i=0,9 do temp = i<br /> endfor</p> <p>That loop will only print out 0 because the nested loop counts to 9, which is the max of the main loop. After the nested loop is executed the counter is at 9 which causes the main loop to exit. I would suggest doing something like <strong>foreach datfile, datfiles</strong> and <strong>foreach roi,roifiles</strong> to help distinguish. Also, if your images are all of the exact same spatial area, you would only need to do the nested foreach loop for the ROI extents one time, which may save you some processing time while your code</p>
    You are not authorized to post a reply.