[ncl-talk] Question on Arguments to be passed for a procedure

Karin Meier-Fleischer meier-fleischer at dkrz.de
Thu Jan 26 05:58:15 MST 2017


Hi Francesco,

I think you are looking for something like the attached script.

Bye,
Karin


Am 26.01.17 um 11:09 schrieb Francesco Trotta:
> Dear Ncl-Team
>    I'm using NCL - version 6.3.0
>    I need to write 6 netcdf files for 6 atmospheric variables 
> (u10m,v10m,t2m,q2m,precip,snow) with the same structure.
>    I would like to define just one procedure for this jobs instead 6 
> procedure , passing as argument the corrispettive variable.
>    But to write the variable in the file I need to specify the name of 
> variable (for example fout->snow = (/atmField/), see code below)
>    Is there a way to pass a name of variable ad argument in order to 
> define just one procedure for all the variable?
>
> thanks
> Francesco
> procedure 
> write_atm_snow(path_extrapdataAtm,file_extrapdataAtm,glAttDescr, \
>  nx,ny,nt,lon1d,lat1d,time,tmask,atmField,maskFieldName,atmFieldName)
> begin
>
>    pathfile_extrapdataAtm = path_extrapdataAtm+"/"+file_extrapdataAtm
>    system("rm -f "+pathfile_extrapdataAtm)
>    fout = addfile(pathfile_extrapdataAtm, "c")
>    ;print(fout)
>
>  ;===================================================================
>    ; Explicitly declare file definition mode. Improve efficiency.
>  ;===================================================================
>    setfileoption(fout,"DefineMode",True)
>
>  ;===================================================================
>    ; Defines global attributes associated with the file.
>  ;===================================================================
>    fAtt              = True
>    fAtt at description  = glAttDescr
>    fAtt at creation_date= systemfunc ("date")
>    fileattdef(fout, fAtt)
>
>  ;===================================================================
>    ; Defines dimension names, dimension sizes, unlimited dimensions on 
> the file.
>    ; (Note: to get an UNLIMITED record dimension, we set the 
> dimensionality
>    ;  to -1 (or the actual size) and set the dimension name to True.)
>  ;===================================================================
>    dimNames = (/ "lon",  "lat",   "time"/)
>    dimSizes = (/ nx,     ny,      nt/)
>    dimUnlim = (/ False, False,    True /)
>    filedimdef(fout,dimNames,dimSizes,dimUnlim)
>
>  ;===================================================================
>    ; Defines a list of variable names, variable types, and
>    ; variable dimension names for the file.
>  ;===================================================================
>    filevardef(fout, "lon",        typeof(lon1d),   "lon")
>    filevardef(fout, "lat",        typeof(lat1d),   "lat")
>    filevardef(fout, "time",       typeof(time),    "time")
>    filevardef(fout, maskFieldName,typeof(tmask), (/"lat","lon"/))
>    filevardef(fout, atmFieldName, typeof(atmField),(/"time","lat","lon"/))
>
>  ;===================================================================
>    ; Copies attributes from an input variable to the variables on the 
> file.
>  ;====================================================================
>    filevarattdef(fout, "lon",        lon1d)
>    filevarattdef(fout, "lat",        lat1d)
>    filevarattdef(fout, "time",       time)
>    filevarattdef(fout, maskFieldName,tmask)
>    filevarattdef(fout, atmFieldName, atmField)
>
>  ;===================================================================
>    ; Explicitly exit file definition mode. **NOT REQUIRED**
>  ;===================================================================
>    setfileoption(fout,"DefineMode",False)
>
>  ;===================================================================
>    ; output only the data values since the dimensionality and such have
>    ; been predefined. The "(/", "/)" syntax tells NCL to only output the
>    ; data values to the predefined locations on the file.
>  ;====================================================================
>    fout->lat  = (/lat1d/)
>    fout->lon  = (/lon1d/)
>    fout->time = (/time/)
>    fout->LSM  = (/tmask/)
>    fout->snow = (/atmField/)
>
> end
>
>>
>
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

-- 
Dipl. Geophys. Karin Meier-Fleischer
Visualization, NCL
Application Support

Deutsches Klimarechenzentrum GmbH (DKRZ)
Bundesstrasse 45a - D20146 Hamburg - Germany

Phone:    +49 (0)40 460094 126
Fax:      +49 (0)40 460094 270
E-Mail:   meier-fleischer at dkrz.de
URL:      www.dkrz.de

Geschäftsführer: Prof. Dr. Thomas Ludwig
Sitz der Gesellschaft: Hamburg
Amtsgericht Hamburg HRB 39784

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170126/7730859c/attachment.html 
-------------- next part --------------
;---------------------------------------------------------------
; DKRZ NCL Example:   write_netCDF_for_each_variable_detailed.ncl
; 
; Description:        Read variables and write them to single 
;                     netCDF file.
;
; Input data:         netCDF, 6 variables, dimensions (time,lat,lon)
;
; 01.11.16  meier-fleischer(at)dkrz.de
;---------------------------------------------------------------
;********************************************************
; procedure write_variable_to_netCDF(fin, vname, outfile)
;
;       Write variable to netCDF file.
;********************************************************
undef("write_variable_to_netCDF")
procedure write_variable_to_netCDF(fin:file, vname:string, outfile:string)
local var, time, lat, lon, ntime, nlat, nlon, \
      fout, fAtt, dimNames, dimSizes, dumUnlim
begin

  if (isfilepresent(outfile)) then
    system("rm -rf "+outfile)                   ;-- make sure that file does not exist
  end if

;-- get variable t and its dimensions and dimension sizes
  var   =  fin->$vname$                        ;-- get variable
  time  =  fin->time                           ;-- get dimension time
  lat   =  fin->lat                            ;-- get dimension lat
  lon   =  fin->lon                            ;-- get dimension lon
          
  ntim  =  dimsizes(time)                       ;-- get dimension sizes of time
  nlat  =  dimsizes(lat)                        ;-- get dimension sizes of lat
  nlon  =  dimsizes(lon)                        ;-- get dimension sizes of lon

;-- create new netCDF file
  fout = addfile(outfile,"c")

;-- begin output file settings
  setfileoption(fout,"DefineMode",True)         ;-- explicitly declare file definition mode

;-- create global attributes of the file
  fAtt                  =  True
  fAtt at title            = "NCL Efficient Approach to netCDF Creation"  
  fAtt at source_file      =  vname at info
  fAtt at Conventions      = "CF"   
  fAtt at creation_date    =  systemfunc ("date")        
  fAtt at history          = "NCL script: write_netCDF_for_each_variable_detailed.ncl" 
  fAtt at comment          = "Copy variable to single netCDF file"       
  fileattdef(fout,fAtt) 

;-- predefine the coordinate variables and their dimensionality
  dimNames = (/"time", "lat", "lon"/)  
  dimSizes = (/ -1   ,  nlat,  nlon /) 
  dimUnlim = (/ True , False, False/)   
  filedimdef(fout,dimNames,dimSizes,dimUnlim)

;-- predefine the the dimensionality of the variables to be written out
  filevardef(fout, "time" ,typeof(time),getvardims(time)) 
  filevardef(fout, "lat"  ,typeof(lat), getvardims(lat))                          
  filevardef(fout, "lon"  ,typeof(lon), getvardims(lon))                          
  filevardef(fout,  vname ,typeof(var), getvardims(var))

;-- copy attributes associated with each variable to the file
  filevarattdef(fout,"time" ,time)              ;-- copy time attributes
  filevarattdef(fout,"lat"  ,lat)               ;-- copy lat attributes
  filevarattdef(fout,"lon"  ,lon)               ;-- copy lon attributes
  filevarattdef(fout, vname ,var)               ;-- copy var attributes

;-- exit file definition mode 
  setfileoption(fout,"DefineMode",False)

;-- write data to file
  fout->time    =  (/time/)                     ;-- write time to new netCDF file
  fout->lat     =  (/lat/)                      ;-- write lat to new netCDF file
  fout->lon     =  (/lon/)                      ;-- write lon to new netCDF file
  fout->$vname$ =  (/var/)                      ;-- write variable to new netCDF file

  print("--> Procedure write_variable_to_netCDF:  wrote file "+outfile)

end  ;-- end procedure write_variable_to_netCDF


;*********************************
;*******       MAIN       ********
;*********************************
begin

  diri = "$HOME/NCL/Workshops/DKRZ_2016/data"  ;-- input file directory
  diro = "./tmp"                               ;-- output file directory
  system("mkdir -p "+diro)                      ;-- create directory if it doesn't exist

;-- open input data file
  fin  = addfile(diri+"/rectilinear_grid_2D.nc","r")    ;-- open data file
  path = getfilepath(fin)                               ;-- get full path file name

;-- get variable names
  varnames  = getfilevarnames(fin)                      ;-- retrieve the file variables with dimensions
  varnames at _FillValue = default_fillvalue("string")     ;-- define missing value

  varnames  = where((varnames .ne. "time") .and. \
                    (varnames .ne. "lat")  .and. \
                    (varnames .ne. "lon"), \
                     varnames, getFillValue(varnames))  ;-- we do not want the dimension variables
  vind      = ind(.not. ismissing(varnames))
  varnames := varnames(vind)                            ;-- variables without dimension variables
  dimvars   = dimsizes(varnames)                        ;-- # of variables

;-- loop over variables
  do i=0,dimvars-1
     
     outfile = diro + "/" + varnames(i) + "_new.nc"     ;-- full path output file name
     vname   = varnames(i)                              ;-- variable
     vname at info = path                                  ;-- attach attribute info with full path file name to variable

     write_variable_to_netCDF(fin, vname, outfile)      ;-- call procedure to write variable to new single file
  
  end do
  
end


More information about the ncl-talk mailing list