comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Loop Multiple Files and Rename
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Loop Multiple Files and Rename [message #67751] Sat, 22 August 2009 09:16
Jean H. is currently offline  Jean H.
Messages: 472
Registered: July 2006
Senior Member
For your loop, start with FILE_SEARCH .This will find all your files and
return an array with every file name. Loop through this array, and use
David solution from there to change the file name

Jean


anniebryant@gmail.com wrote:
> Hi All,
>
> I have a pretty basic question.
>
> I have a list of MODIS images in a folder, all beginning with the
> prefix "MOD09GA" followed by a string of other characters, i.e.
> '.A2009138.h09v05.005.2009140163311_Grid_2D_reprojected.img'
>
> I have written a very basic little program to reorder the bands in the
> MODIS image, but I've only gotten it to work one image at a time.
>
> Ideally, I'd have a loop that takes each image from the folder,
> reorders the bands, then writes each file with a unique name.
>
> _________________________________
>
> pro MOD09GA_bandreorder
>
> ns=4578
> nl=2367
> nb=7
>
> image = fltarr(ns,nl,nb)
> morder=fltarr(ns,nl,nb)
> ;outfile=fltarr(ns,nl,nb)
>
> for i=1,11 do begin ; is this opening multiple files, or the same
> over and over?? figure it out AB!
>
> openr, i, '/users/u0609216/documents/modis/2009/Geo_2009/MOD09GA.*'
> readu, i, image
> close, i
>
> b1 = image(*,*,0)
> b2 = image(*,*,1)
> b3 = image(*,*,2)
> b4 = image(*,*,3)
> b5 = image(*,*,4)
> b6 = image(*,*,5)
> b7 = image(*,*,6)
> ; band order should be (3,4,1,2,5,6,7)
> morder[*,*,0]=b3
> morder[*,*,1]=b4
> morder[*,*,2]=b1
> morder[*,*,3]=b2
> morder[*,*,4]=b5
> morder[*,*,5]=b6
> morder[*,*,6]=b7
>
> print, "Now processing:" ,'file', i
>
> get_lun,unit
>
> openw,unit,'/users/u0609216/documents/modis/2009/reorder_200 9/
> reorder.bip'
> writeu,unit,morder
> close,unit
>
> endfor
>
> print, 'Done!'
>
> end
Re: Loop Multiple Files and Rename [message #67752 is a reply to message #67751] Fri, 21 August 2009 16:40 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
pp writes:

> Open only opens one file, which gets associated to a unit number. I do
> not know what it will do if the name has a * in it, it probably will
> not work.
>
> I suggest putting the code to do the reorder of a file in a separate
> routine:

I typically, just modify the name:

inputFile = '/data/MOD09/MOD09GAh12v01_Grid2d.img'
root = FSC_Base_Filename(inputFile, DIRECTORY=dir, EXTENSION=ext)
outputFile = Filepath(ROOT_DIR=dir, root + '_reorderd' + '.' + ext)

This is easy to do in a loop.

FSC_Base_Filename is a Coyote Library routine.

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Loop Multiple Files and Rename [message #67753 is a reply to message #67752] Fri, 21 August 2009 16:28 Go to previous message
penteado is currently offline  penteado
Messages: 866
Registered: February 2018
Senior Member
Administrator
On Aug 21, 7:13 pm, "anniebry...@gmail.com" <anniebry...@gmail.com>
wrote:
> The question is:
>
> 1.  How do I give each of the files a unique name when I use the
> writeu (or if there is a better command), i.e. something similar to
> their initial MODIS input name.

You need to decide some rule to generate a new name from the input
file name. From what you wrote, I am guessing it may be to make a file
with the same name, in another directory.

> 2.  Using openr and a * after the initial few letters, is it finding
> each individual file in the folder?

Open only opens one file, which gets associated to a unit number. I do
not know what it will do if the name has a * in it, it probably will
not work.

I suggest putting the code to do the reorder of a file in a separate
routine:

pro MOD09GA_file_bandreorder,filein,fileout
;reads a (ns,nl,nb) float array from filein, and writes it reordered
in fileout
;contants
ns=4578
nl=2367
nb=7
b_order[2,3,0,1,4,5,6] ; band order should be (3,4,1,2,5,6,7)
;read the input file
image = fltarr(ns,nl,nb)
openr,unit,filein,/get_lun
readu,unit,image
free_lun,unit
;make the reordered array
morder=image[*,*,b_order]
;write the reodered array
print, "Now processing:" ,fileout
openw,unit,fileout,/get_lun
writeu,unit,morder
free_lun,unit
print, 'Done!'
end

Then a separate program makes a list of files to read, and from it
makes a list of files to write, and calls the one above:

pro MOD09GA_bandreorder
;make a list of files to read
filesin=file_search('/users/u0609216/documents/modis/2009/Ge o_2009/
MOD09GA.*')
;make the names of the output files
filesout='/users/u0609216/documents/modis/2009/
reorder_2009/'+file_basename(filesin)
;reorder each file
for i=0,n_elements(filesin)-1 do MOD09GA_file_bandreorder,filesin
[i],filesout[i]
end
Re: Loop Multiple Files and Rename [message #67754 is a reply to message #67753] Fri, 21 August 2009 15:13 Go to previous message
anniebryant@gmail.com is currently offline  anniebryant@gmail.com
Messages: 16
Registered: March 2009
Junior Member
On Aug 21, 4:06 pm, pp <pp.pente...@gmail.com> wrote:
> You described what you want to do. But what is the question?
>
> Also, regarding this section of the code:
>
>
>
>>   morder=fltarr(ns,nl,nb)
>>   b1 = image(*,*,0)
>>   b2 = image(*,*,1)
>>   b3 = image(*,*,2)
>>   b4 = image(*,*,3)
>>   b5 = image(*,*,4)
>>   b6 = image(*,*,5)
>>   b7 = image(*,*,6)
>>                     ; band order should be (3,4,1,2,5,6,7)
>>   morder[*,*,0]=b3
>>   morder[*,*,1]=b4
>>   morder[*,*,2]=b1
>>   morder[*,*,3]=b2
>>   morder[*,*,4]=b5
>>   morder[*,*,5]=b6
>>   morder[*,*,6]=b7
>
> No need to write so many lines to do this. You could use just:
>
> border=[3,4,1,2,5,6,7]-1
> morder=image[*,*,border]

The question is:

1. How do I give each of the files a unique name when I use the
writeu (or if there is a better command), i.e. something similar to
their initial MODIS input name.

2. Using openr and a * after the initial few letters, is it finding
each individual file in the folder?

Sorry for being a bit vague. Does that clear it up?
Re: Loop Multiple Files and Rename [message #67755 is a reply to message #67754] Fri, 21 August 2009 15:06 Go to previous message
penteado is currently offline  penteado
Messages: 866
Registered: February 2018
Senior Member
Administrator
You described what you want to do. But what is the question?

Also, regarding this section of the code:

>   morder=fltarr(ns,nl,nb)
>   b1 = image(*,*,0)
>   b2 = image(*,*,1)
>   b3 = image(*,*,2)
>   b4 = image(*,*,3)
>   b5 = image(*,*,4)
>   b6 = image(*,*,5)
>   b7 = image(*,*,6)
>                     ; band order should be (3,4,1,2,5,6,7)
>   morder[*,*,0]=b3
>   morder[*,*,1]=b4
>   morder[*,*,2]=b1
>   morder[*,*,3]=b2
>   morder[*,*,4]=b5
>   morder[*,*,5]=b6
>   morder[*,*,6]=b7


No need to write so many lines to do this. You could use just:

border=[3,4,1,2,5,6,7]-1
morder=image[*,*,border]
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Loop Multiple Files and Rename
Next Topic: Re: CHISQR_CVF question.

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 13:51:41 PDT 2025

Total time taken to generate the page: 0.00734 seconds