[ncl-talk] loop over a unique filename

Alexander Schaefer aschae11 at uncc.edu
Tue Jul 12 07:54:31 MDT 2016


Just use an If statement.

do t = 1,1

if (t .lt. 10) then 
  input="/dir/data.000”+t +”.nc”
else if (t .lt. 100) then
  input="/dir/data.00”+t +”.nc"
else if (t .lt. 1000) then
   input="/dir/data.0"+t +”.nc”
else 
  input="/dir/data.” + t +”.nc"
end if
end if
end if

file = addfile(input,”r”)

end do

I would set the filename in the if statements and load the file after them; all done within the loop

Other option is 

ifiles = systemfunc( “ls /dir/data.*.nc”) ; This gets all of your data file names

nfiles = dimsizes(ifiles)

do t = 0, nfiles-1

  ifile = addfile(ifiles(t), “r”)
  
 ; Now do your work

end do


the latter option is smoother but either should work for the purpose.

-Alex




> On Jul 12, 2016, at 9:44 AM, Kacie Shourd <Kacie.Shourd at dri.edu> wrote:
> 
> Hi, Chang
> 
> I have yet to figure out how to loop through files in such a way using purely NCL. There is the "addfiles" function, but I don't believe that loops through files -- it just pulls in the variables from all files and I'm not sure how to keep things separate. 
> 
> I have handled this problem in the past by using a wrapper in an external language (Python, for example), and running things directly from the shell. Looping in other languages is much more straight forward (in my opinion), and shouldn't be too difficult to accomplish.  
> 
> If you need assistance doing this, I would be happy to lend a hand/provide an example. 
> 
> Best,
> Kacie
> 
> ---
> Kacie N. Shourd
> 드림
>  
> Graduate Research Assistant
> Desert Research Institute
> Kacie.Shourd at dri.edu <mailto:Kacie.Shourd at dri.edu>
> Work: (775) 674-7049
> 
> From: ncl-talk-bounces at ucar.edu <mailto:ncl-talk-bounces at ucar.edu> [ncl-talk-bounces at ucar.edu <mailto:ncl-talk-bounces at ucar.edu>] on behalf of Xi Chang [xi.chang01 at gmail.com <mailto:xi.chang01 at gmail.com>]
> Sent: Tuesday, July 12, 2016 6:34 AM
> To: ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
> Subject: [ncl-talk] loop over a unique filename
> 
> Dear NCL,
> 
> I have a unique *nc file like this (with increment 1):
> 
> data.0001.nc <http://data.0001.nc/>
> data.0002.nc <http://data.0002.nc/>
> data.0003.nc <http://data.0003.nc/>
> .
> .
> data.0010.nc <http://data.0010.nc/>
> data.0011.nc <http://data.0011.nc/>
> data.0012.nc <http://data.0012.nc/>
> .
> .
> .
> data.0101.nc <http://data.0101.nc/>
> data.0102.nc <http://data.0102.nc/>
> 
> I want to call this data using a loop, something like this:
> 
> t=1,1
> 
> input="/dir/data.000+"t".nc
> 
> but it doesnt work when then zero is reduced infront of the filename.
> anys suggestion how to call this files on a loop?
> 
> Thanks
> 
> Chang
> 
> 
> PUBLIC RECORDS NOTICE: In accordance with NRS Chapter 239, this email and responses, unless otherwise made confidential by law, may be subject to the Nevada Public Records laws and may be disclosed to the public upon request. _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20160712/e1755807/attachment.html 


More information about the ncl-talk mailing list