Feature request: Functions to get *file* global attributes, dimension names, and dimension "Ulim" property

From: Gus Correa <gus_at_nyahnyahspammersnyahnyah>
Date: Thu Jun 07 2012 - 16:44:29 MDT

Hi NCL list and experts

I often use NCL to copy over a NetCDF file structure and data
[or part of the structure and data],
and then modify some of the variables for various purposes.
I enclose a script template below.
Nothing really new here,
there are similar examples in the NCL documentation.
The script copies the
global attributes, dimensions, variables and their attributes,
from the input to the output file,
then modifies what is needed, and writes to the output file.
[In the example one variable is
zeroed out, which could be easily done in NCO, I know, but this
is just an example].
Variants of this script can be built to interpolate variables to
a different grid, etc.

However, I miss a few functions that would make this
work easier and more generic,
which I would like to request as new NCL features.
Maybe they already exist, and in this case please point them
to me.

1) A function to retrieve all global attributes from a file.
Or, if more appropriate, such a function could be split into three:
one to get the number of global attributes,
another to get the global attribute names,
and yet another to get each global attribute value.

Currently I have to use ncdump to list those attributes,
and hardwire them into the script.
With the function/functions requested this could be done
in a general loop, and nothing would have to be hardwired.

2) Two other functions, the first to retrieve the names
of all dimensions in a file [note, in a *file*,
not in a file variable, as the latter function already exists],
and the second function to retrieve the "Ulim" property of
all dimensions in a file.

Here too I have to use ncdump to list all dimension names in a
NetCDF file, along with the information of which one is unlimited,
and then hardwire this information in the script.
With the functions requested this could be done in a loop,
obviating the need for hardwired information,
and making the script more generic.

Thank you,
Gus Correa

*********************************************
script prototype
*********************************************

begin

; input/output file names
   finname="input.nc"
   founame="output.nc"

; read input file, create output file
   fin = addfile(finname, "r")
   fou = addfile(founame, "c")

; create output file structure based on input file structure
   print("Creating output file structure ...")

; set output file in define mode
   setfileoption(fou,"DefineMode",True)

; define output file global attributes
;
; NOTE THE HARDWIRED GLOBAL ATTRIBUTES, COPIED FROM NCDUMP
; I CANNOT USE A NICE GENERAL LOOP HERE :(
;
   nl = integertochar(10) ; newline character
   fouGlAtt = True
   fouGlAtt@history = fin@history + nl + "Modifications: " + nl \
        "myvar zeroed out on " + systemfunc("date")
   fouGlAtt@Conventions = fin@Conventions
   fouGlAtt@author = fin@author
   fouGlAtt@date = fin@date
   fouGlAtt@reference = fin@reference
   fouGlAtt@grid = fin@grid

   fileattdef(fou,fouGlAtt)

; define output file dimensions
;
; NOTE THE HARDWIRED DIMENSION NAMES AND 'ULIM'
; PROPERTY, COPIED FROM NCDUMP
; I CANNOT USE A NICE LOOP HERE :(
;
   oudimNames = (/ "lon", "lat", "time" /)
   indimSizes = getfiledimsizes(fin)
; print(indimSizes)
   oudimSizes = indimSizes
   oudimUlim = (/ False, False, True /)
   filedimdef(fou, oudimNames, oudimSizes, oudimUlim)

; define output file variables
;
; HERE NOTHING NEEDS TO BE HARDWIRED,
; TWO GENERAL NESTED LOOPS,
; FOR THE VARIABLES AND FOR THEIR ATTRIBUTES,
; DO THE WHOLE JOB, THANKS TO THE FUNCTIONS
; getfilevarnames, getfilevartypes, AND getfilevaratts :)
;

   finvarNames = getfilevarnames(fin)
   nfinvarNames = dimsizes(finvarNames)
   finvarTypes = getfilevartypes(fin,finvarNames)

   do n=0,nfinvarNames-1

     onevarName = finvarNames(n)
     onevarType = finvarTypes(n)
     onevarDimNames = getfilevardims(fin,onevarName)

; insert each variable with type and dims
     filevardef(fou, onevarName, onevarType, onevarDimNames)

     onevarAttNames = getfilevaratts(fin,onevarName)
     nonevarAttNames = dimsizes(onevarAttNames)

; define each variable attributes, if there are any
     if (nonevarAttNames .gt. 0)

       do m=0,nonevarAttNames-1

         oneAttName = onevarAttNames(m)
         oneAttValue = fin->$onevarName$@$oneAttName$
         onevarAttValues@$oneAttName$=oneAttValue
         delete(oneAttName)
         delete(oneAttValue)

       end do

       filevarattdef(fou, onevarName, onevarAttValues)
       delete(onevarAttValues)

     end if

     delete(onevarName)
     delete(onevarType)
     delete(onevarDimNames)
     delete(onevarAttNames)

   end do

; exit define mode
   setfileoption(fou,"DefineMode",False)
   print("Output file structure is ready")

; copy over the data in the variables
   print("Copying over the data")

   do n=0,nfinvarNames-1

     onevarName = finvarNames(n)
     fou->$onevarName$ = (/ fin->$onevarName$ /)

   end do

; modify specific variables, e.g. zero-out, but customize as needed
   print("Modifying some specific variables ...")

   fou->myvar = conform_dims(dimsizes(fou->myvar), 0.0, -1)

end
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Jun 7 16:44:43 2012

This archive was generated by hypermail 2.1.8 : Tue Jun 12 2012 - 13:58:38 MDT