[ncl-talk] Output file naming using systemfunc
Dave Allured - NOAA Affiliate
dave.allured at noaa.gov
Wed Aug 10 18:46:02 MDT 2016
Cary,
begin
diri = "/Volumes/tas/"
fili = systemfunc ("ls " + diri + "tas_Amon_*")
out_template = "/Volumes/out/tas_FMA_MODEL_EXPERIMENT.nc"
do k = 0, dimsizes(fili)-1
infile = fili(k)
; Remove directories, isolate the base file name.
nitems = str_fields_count (infile, "/")
name2 = str_get_field (infile, nitems, "/")
; Isolate selected parts of the file name. Pattern:
; tas_Amon_[model]_[experiment]_[realization]_[years].nc
model = str_get_field (name2, 3, "_")
expt = str_get_field (name2, 4, "_")
; Make output file name using template.
outfile = str_sub_str (out_template, "MODEL", model)
outfile = str_sub_str (outfile, "EXPERIMENT", expt)
print ("")
print (infile+"")
print (outfile+"")
end do
end
--Dave
On Wed, Aug 10, 2016 at 1:14 PM, Lynch, Cary D <cary.lynch at pnnl.gov> wrote:
> NCL-TALK:
>
> I am trying to rename and move output files using the systemfunc and/or
> system command within a NCL script. Currently I hard code output names;
> use a separate shell script; and/or use CDO commands.
>
> Input file name(s) look something like this: tas_ Amon_ [model]_
> [experiment]_ [realization, etc]_ [years].nc
> I want them to look like this: tas_ FMA_ [model]_ [experiment].nc
> And I want them to be located in a new directory.
>
> The below script works, but requires me to use a shell script to move and
> rename output files using Unix and sed commands.
>
> ;**************************************
> ; systemfunc to rename files
> ;**************************************
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
> ;**************************************
> begin
> ;****************************************
>
> diri = "/Volumes/tas/"
> diro = "/Volumes/out/"
>
> fili = systemfunc("ls " + diri + "tas_Amon_*")
> filo = fili + "_x.nc"
>
> ; read in all data files
>
> do k = 0,dimsizes(fili)-1
> in1 = addfile(fili(k),"r")
> tsi = in1->tas
>
> ; do seasonal average then average over time dim
>
> atsi = month_to_season(tsi,"FMA")
> climi = dim_avg_n_Wrap(atsi,0)
>
> ; write out data files
>
> system("/bin/rm -f " + filo(k))
> ; out = addfile(diro+filo(k),"c")
> out = addfile(filo(k),"c")
> out->climate = climi
> delete([/tsi,atsi,climi/])
>
> end do
>
> End
> ~
>
> Then I use the following shell:
>
> mv /Volumes/tas/*_x.nc /Volumes/out/.
> sed -i -- 's/Amon/FMA/g' /Volumes/out/_x.nc
> sed -i -- 's/r1i1p1//g' /Volumes/out/_x.nc
> sed -i -- 's/.nc_x.nc/.nc/g' /Volumes/out/_x.nc
>
> Thank you.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20160810/dd10914b/attachment.html
More information about the ncl-talk
mailing list