Re: 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 - 17:51:06 MDT

Hi Wei Huang

Thank you!
That is a very nice and simple solution,
really what I was looking for.
I may not have searched very hard, but I didn't find
these features documented, certainly not as clearly as in your email.

So, the 'file pointer' 'f' returned by 'addfile' holds all
the needed information!
The trick is to retrieve the global attributes with getvaratts,
and the dimension names with getfiledimsizes, both applied to 'f'.
Then apply 'isunlimited' to each dimension name just retrieved.

Now that you told me the solution,
I could find a few bits and pieces [not all]
of what you said in the 'addfile' documentation examples.
However, your email synthesizes everything much better!

Many thanks,
Gus Correa

On 06/07/2012 07:10 PM, Wei Huang wrote:
> Gus,
>
> There are already functions to do these.
>
> See example below:
>
> ncl 0> f = addfile("uvt.nc", "r")
> ncl 1> atts = getvaratts(f)
> ncl 2> print(atts)
>
>
> Variable: atts
> Type: string
> Total Size: 32 bytes
> 4 values
> Number of Dimensions: 1
> Dimensions and sizes: [4]
> Coordinates:
> (0) creation_date
> (1) Conventions
> (2) source_file
> (3) title
>
>
> ncl 15> title = f@$atts(3)$
> ncl 16> print(title)
>
>
> Variable: title
> Type: string
> Total Size: 8 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes: [1]
> Coordinates:
> (0) NCL generated netCDF file
>
>
>
>
> ncl 3> ds = getfiledimsizes(f)
> ncl 4> print(ds)
>
>
> Variable: ds
> Type: integer
> Total Size: 16 bytes
> 4 values
> Number of Dimensions: 1
> Dimensions and sizes: [4]
> Coordinates:
> (0) 1
> (1) 14
> (2) 64
> (3) 128
>
>
> ncl 6> dn = getvardims(f)
> ncl 7> print(dn)
>
>
> Variable: dn
> Type: string
> Total Size: 32 bytes
> 4 values
> Number of Dimensions: 1
> Dimensions and sizes: [4]
> Coordinates:
> Number Of Attributes: 1
> _FillValue : missing
> (0) time
> (1) lev
> (2) lat
> (3) lon
>
> ncl 9> unlmt = isunlimited(f, "time")
> ncl 10> print(unlmt)
>
>
> Variable: unlmt
> Type: logical
> Total Size: 4 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes: [1]
> Coordinates:
> (0) True
> ncl 11> unlmt = isunlimited(f, "level")
> ncl 12> print(unlmt)
>
>
> Variable: unlmt
> Type: logical
> Total Size: 4 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes: [1]
> Coordinates:
> (0) False
>
>
>
>
>
> huangwei@ucar.edu
> VETS/CISL
> National Center for Atmospheric Research
> P.O. Box 3000 (1850 Table Mesa Dr.)
> Boulder, CO 80307-3000 USA
> (303) 497-8924
>
>
>
>
>
>
> On Jun 7, 2012, at 4:44 PM, Gus Correa wrote:
>
>> 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
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Jun 7 17:51:21 2012

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