[Thread Prev][Thread Next][Index]

Re: [ferret_users] Looping over multiple input files to plot file variables and save to png



Hi Paul,

You can strip the .nc suffix by passing through "sed". Alternatively using basename to strip suffixes works too. You can muck around with the Ferret string functions but they are awkward. Since you're using pyferret it would probably be rather simple to do this in python too.

Try something like

let filenames = SPAWN("/bin/ls -1 basinmask/*.nc | sed -e s/\.nc//")
repeat/range=1:`filenames,ret=iend`/name=ifile go process_file filenames[i=`ifile`]

where process_file takes a filename as an argument.

e.g.

\can mode verify
! Usage:
! go process_file filename
!
def sym filename $1
use ($filename)
! Could use ($filename).nc
set win 1
sha basinmask4
frame/file="($outpath)($filename).png"
can data ($filename)
can sym filename
can win 1
set mode verify/last


Russ

On 02/05/17 13:06, Durack, Paul J. wrote:

Thanks Russ, so how do I then loop over the entries:

 

let filenames = SPAWN("/bin/ls -1 basinmask/*.nc")

! How do I loop over filenames?

!REPEAT/RANGE=1: ($filenames)

!

! Then plot

use ($filename)

set win 1

sha basinmask4

frame/file="($outpath)($filename).png" ! How to I replace *.nc in the filename with *.png?

cancel win 1

 

Cheers,

 

P

 

From: <owner-ferret_users@xxxxxxxx> on behalf of Russ Fiedler <russell.fiedler@xxxxxxxx>
Date: Monday, May 1, 2017 at 5:36 PM
To: "ferret_users@xxxxxxxx" <ferret_users@xxxxxxxx>
Subject: Re: [ferret_users] Looping over multiple input files to plot file variables and save to png

 


Hi Paul,

Rather than writing your filenames to a file you can create a variable using the SPAWN function directly.

let filenames = SPAWN("/bin/ls -1 basinmask/*.nc")

Another way would be to create the file as you are at the moment and read it in via a spawn and 'cat' command

let filenames = SPAWN("cat filenames.txt")

I'd prefer the first method.

Cheers,
Russ

On 02/05/17 08:20, Durack, Paul J. wrote:

I have 51 netcdf files in a directory, and want to loop over each of these and for each plot the contents of a gridded variable and save this to a png file, using pyferret 7.1

 

I’ve been struggling with syntax, and am getting “**TMAP ERR: non-existent or not on line  filenames.txt”, with the following partial code:

 

spawn "ls basinmask/*.nc > filenames.txt" ! This is generating a file, but if I could create the filelist in memory this step could be removed

FILE/FORMAT=DELIMITED/var="filenames" filenames.txt ! < This generates the error

say "processing: ",($file) ! don’t get to here

 

 

The plot and save command will be a simple:

 

> sha basinmask4 ! Each file contains the same named variable

> frame/file=”($mypath)($myfile).png”

 

Can someone help me out to fill in the missing logic/syntax here?

 

Thanks,

 

P

 



[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement