X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 12 Aug 2015 03:03 PM by  anon
Layer Stacking based on filenames
 2 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
12 Aug 2015 03:03 PM
    I have in one folder 80 Landsat scenes, each with 6 bands for a total of 480 .tif files. I want to stack bands 3 and 4 for each scene to create an NDVI, but I am not sure how to make IDL recognize to only stack the appropriate .tif files. The naming nomenclature is like this: LT50290282011268PAC01_sr_band3.tif LT50290282011268PAC01_sr_band4.tif LT50300281984185XXX15_sr_band3.tif LT50300281984185XXX15_sr_band4.tif so I want the first two files stacked together and the second two files stacked together. The code I am using which will stack all images in a folder is: pro NDVI_stack ;first restore all the base save files and initialize batch. envi, /restore_base_save_files envi_batch_init, log_file='batch.txt' ;set up path to input and output files input_path = 'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\NDVI' ;change to output directory where I want the processed files to be placed cd,'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\NDVI\stacked' files=file_search(input_path,'*tif',count=count,/fold_case) ;open and gather file information in arrays in_fid=lonarr(count) in_nb=lonarr(count) in_dims=lonarr(5, count) in_dt=lonarr(count) for i=0L, count-1 do begin envi_open_file, files[i], r_fid=r_fid if (r_fid eq -1) then begin envi_batch_exit return endif envi_file_query, r_fid, ns=ns, nl=nl, nb=nb, dims=dims, data_type=dt in_fid[i]=r_fid in_nb[i]=nb in_dims[*,i]=dims in_dt[i]=dt endfor ;set up output fid, pos, and dims arrays out_fid = replicate(in_fid[0], in_nb[0]) for i=1, count-1 do out_fid = [out_fid, replicate(in_fid[i], in_nb[i])] out_pos = lindgen(in_nb[0]) for i = 1, count-1 do out_pos = [out_pos, lindgen(in_nb[i])] rep_dims = (intarr(in_nb[0])+1) # in_dims[*,0] for i = 1, count-1 do $ rep_dims = [rep_dims, (intarr(in_nb[i]) + 1) # in_dims[*,i]] out_dims = transpose(rep_dims) ;set the output projection and pixel size from the first file. ;save the result to disk and use max data type out_proj = envi_get_projection(fid=in_fid[0], pixel_size=out_ps) out_dt = max(in_dt) out_name = 'file_basename(filename, '.dat') + '_ndvi.tif' ;call the layer stacking routine. Do not set the exclusive keyword allow for an ;inclusive result. Use nearest neighbor for the interpolation method. envi_doit, 'envi_layer_stacking_doit', fid=out_fid, pos=out_pos, dims=out_dims, $ out_dt=out_dt, out_name=out_name, interp=0, out_ps=out_ps, $ out_proj=out_proj, r_fid=r_fid ;exit ENVI envi_batch_exit end

    Zachary Norman



    Basic Member


    Posts:173
    Basic Member


    --
    14 Aug 2015 09:12 AM
    Hi Stefano, If all of your files have similar naming schemes (for the bands), you will likely need to do some string processing to find the individual base file names. By that I mean you will need to isolate all of the files with names like 'LT50290282011268PAC01' and then select the appropriate bands. Here is a small example to illustrate that: IDL> temp = 'LT50290282011268PAC01_sr_band3.tif' IDL> basename = strmid(temp, 0, strpos(temp,'_')) IDL> print, temp, basename LT50290282011268PAC01_sr_band3.tifLT50290282011268PAC01 IDL> print, temp, basename, /implied LT50290282011268PAC01_sr_band3.tif LT50290282011268PAC01 IDL> first_image = basename + '_sr_band3.tif' IDL> second_image = basename + '_sr_band4.tif' IDL> print, first_image, second_image, /implied LT50290282011268PAC01_sr_band3.tif LT50290282011268PAC01_sr_band4.tif

    Deleted User



    New Member


    Posts:6
    New Member


    --
    14 Sep 2015 02:09 AM
    This is my code for creating a meta-file for landsat names (with a .hdr which sometimes doesn't work out), you can use it to create your own layer stacking or just make meta-files (which saves some space). But warning, if you create a meta-file don't move your original files, the meta-file only points to their locations. In my code you have to change the variables image_ext and name_convention at the top to align with how you want your files searched/sorted. Input your source directory in source_dir.You can disable a .hdr using create_hdr_opt I usually use ENVI classic, which you can start at IDL by just typing ENVI in IDL> prompt. ----------------------------------------------------------------------------------------------------------------------- ;Program to use IDL features to create a meta file for matching file names automatically ; ;Band order is derived from a standard descending sort, so filenames decide band order in the meta file ; ;Created by Kenneth Lynn Dudley, Okinawa Institute of Science and Technology (OIST) 2015 function create_meta_hdr, output_file_name, matching_bands hdr_fwhm = [] hdr_wavelength = [] foreach band, matching_bands, index do begin ;matching_bands list has been sorted A-Z ;First band will be default for remaining bands for some header items if index EQ 0 then begin ENVI_OPEN_FILE, band, /INVISIBLE, /NO_REALIZE, R_FID=b_fid, /NO_INTERACTIVE_QUERY ENVI_FILE_QUERY, b_fid, Acquisition_time=b_aqc_time, Cloud_cover=b_cloud, DATA_IGNORE_VALUE=b_ignore, $ Data_type=b_data_type, DIMS=b_dims, fwhm=b_fwhm, interleave=b_interleave, NL=bNL, NS=bNS, sensor_type=b_sensor, $ sun_azimuth=b_azimuth, sun_elevation=b_elev, wavelength_units=b_wave_units, WL=bWL ENVI_FILE_MNG, id=b_fid, /remove endif else begin ENVI_OPEN_FILE, band, /INVISIBLE, /NO_REALIZE, R_FID=b_fid, /NO_INTERACTIVE_QUERY ENVI_FILE_QUERY, b_fid, fwhm=b_fwhm, WL=bWL ENVI_FILE_MNG, id=b_fid, /remove endelse ;store band-by-band different headers hdr_fwhm = [hdr_fwhm, b_fwhm] hdr_wavelength = [hdr_wavelength, bWL] endforeach ;Write composite header for ENVI Meta File, Keep in mind, this file doesnt play well with ENVI Classic, so it's better not to have it ;ENVI 5.# handles Meta .hdr files correctly. ENVI_SETUP_HEAD, Acquisition_time=b_aqc_time, Cloud_cover=b_cloud, DATA_IGNORE_VALUE=b_ignore, $ Data_type=b_data_type, fwhm=hdr_fwhm, interleave=b_interleave, NL=bNL, NS=bNS, sensor_type=b_sensor, $ sun_azimuth=b_azimuth, sun_elevation=b_elev, wavelength_units=b_wave_units, WL=hdr_wavelength, /WRITE, $ NB = (n_elements(matching_bands)), FNAME = output_file_name + '.hdr', File_type = 1 end pro CreateMetaFile create_hdr_opt = 1 ; YES=1 or NO=0 on creating a .hdr file for the META file, some attributes are carried over in the meta (wavelength) and others not (acquisition date) and must be put into a .hdr for the META file. source_dir = 'D:\00_Full_SR_LT_COPY\SOki_NN\LT4-5' ;'Z:\EconomoU\Kenneth\Okinawa Datasets\Remote Sensing Imagery\Landsat Scenes\OperationalScenes\sr_warped' image_ext = ['*.img'];['*.img','*.tif'] ; image file extension, can use multiple types name_convention = ['_band1','_band2','_band3','_band4','_band5','_band7'] ; Put in order of default display [0] is blue [1] is green [2] is red [blue, green, red] output_name_mod = '_Full_META' ; change this to change the output file name "LANDSATNAME+output_name_mod" name_convention = '*'+name_convention+'*' ;Do not change find_index_drive = [] foreach ext, image_ext do find_index_drive = [find_index_drive, File_Search(source_dir, ext)] ; create a total index of files and directories find_index_drive = find_index_drive[where(find_index_drive NE '', /NULL)] ; Get rid of any extra blank spaces uniq_file_list = list() foreach file, find_index_drive do uniq_file_list.add, (File_Basename(file)).Substring(0,20) ; This assumes landsat naming convention uniq_file_list = uniq_file_list.ToArray() uniq_file_list = uniq_file_list[uniq(uniq_file_list, sort(uniq_file_list))] foreach file, uniq_file_list do begin band_matches = find_index_drive[where(strmatch(find_index_drive, '*'+file+'*') EQ 1, /null)] matching_bands = [] foreach name, name_convention do begin insert_band = band_matches[where(strmatch(band_matches, name) EQ 1, /NULL)] matching_bands = [matching_bands, insert_band] endforeach check_null = where(matching_bands EQ !NULL, count_null) if count_null NE 0 then begin print, 'Error, found ', strtrim(total(check_null),2), ' for file: ', file endif else begin matching_bands = matching_bands[(sort(matching_bands))] ;Open first file to pull info from metadata ENVI_OPEN_FILE, matching_bands[0], /INVISIBLE, /NO_REALIZE, R_FID=match_fid, /NO_INTERACTIVE_QUERY ENVI_FILE_QUERY, match_fid, DIMS=match_img_dims ENVI_FILE_MNG, id=match_fid, /remove ;Create output variables output_file_name = FILE_DIRNAME(matching_bands[0], /MARK_DIRECTORY)+file + output_name_mod Header_out = 'ENVI META FILE' Band_out = 'Bands: 1' Dims_out = 'Dims : 1-'+strtrim(match_img_dims[2],2)+',1-'+strtrim(match_img_dims[4],2) GET_LUN, out_meta_file openw, out_meta_file, output_file_name printf, out_meta_file, "ENVI META FILE" foreach input_f, matching_bands do begin printf, out_meta_file, "File : "+input_f printf, out_meta_file, Band_out printf, out_meta_file, Dims_out printf, out_meta_file, "" endforeach close, out_meta_file FREE_LUN, out_meta_file IF create_hdr_opt then result = create_meta_hdr(output_file_name, matching_bands) endelse endforeach print, 'Done!' end
    You are not authorized to post a reply.