[ncl-talk] Output file naming using systemfunc

Lynch, Cary D cary.lynch at pnnl.gov
Wed Aug 10 13:14:06 MDT 2016


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/ec48169a/attachment.html 


More information about the ncl-talk mailing list