Re: VarAttName Becoming a Variable Attribute itself

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Tue Sep 17 2013 - 14:31:44 MDT

Hi Greg,
There are two documented ways to assign values to file variable attributes. You are using them both.
>From the PyNIO documentation:

Create an attribute of a variable in the file

The following 2 statements are equivalent;

f.variables['varName'].varAttName = varAttVal
setattr(f.variables['varName'],'varAttName',varAttVal)

What you are doing is sequentially assigning different values to the attribute 'varAttName' 6 times, each time overwriting the previous assignment.
The values you are giving it are what you intend to be the attribute names.
Then you use the 'setattr' routine to create and assign (correctly) all the attributes with their values.

If you want to use the first method it should like like this:

f_out.variables['Tsfc'].standard_name = 'surface_temperature_where_sea_ice'
f_out.variables['Tsfc'].units = 'DegC'

etc.

Otherwise just get rid of the lines with varAttName. You don't need them.
Hope this helps.
 -dave

On Sep 17, 2013, at 1:08 PM, Gregory Deemer <gjdeemer@alaska.edu> wrote:

> Hello,
>
> The following snippet of code shows my setup for creating a new variable, variable attributes, and attribute values.
>
> []
> f_out.create_variable('Tsfc', 'f', ('time', 'lat', 'lon'))
> # Create variable attributes
> f_out.variables['Tsfc'].varAttName = 'standard_name'
> f_out.variables['Tsfc'].varAttName = 'units'
> f_out.variables['Tsfc'].varAttName = '_FillValue'
> f_out.variables['Tsfc'].varAttName = 'missing_value'
> f_out.variables['Tsfc'].varAttName = 'scale_factor'
> f_out.variables['Tsfc'].varAttName = 'add_offset'
> setattr(f_out.variables['Tsfc'], 'standard_name', 'surface_temperature_where_sea_ice')
> setattr(f_out.variables['Tsfc'], 'units', 'DegC')
> setattr(f_out.variables['Tsfc'], '_FillValue', _FillandMiss)
> setattr(f_out.variables['Tsfc'], 'missing_Value', _FillandMiss)
> setattr(f_out.variables['Tsfc'], 'scale_factor', np.array(0.002, dtype=np.float32))
> setattr(f_out.variables['Tsfc'], 'add_offset', offset)
> # Write value to file
> f_out.variables['Tsfc'].assign_value(Tsfc)
> []
>
> Where _FillandMiss, offset, and Tsfc have values previously assigned to them. My script functions without error and I have been able to use the resulting NetCDF file in GrADS without error as well. My question comes in here: after executing an 'ncdump -h *.nc' command for the output file (below), I get the following snippet where the last f_out.variables['var'].varrAttName = 'varAttName' becomes an actual attribute of the variable created. Why is this occurring? I would like to clean this up.
>
> ncdump -h *.nc
> []
> float Tsfc(time, lat, lon) ;
> Tsfc:varAttName = "add_offset" ;
> Tsfc:standard_name = "surface_temperature_where_sea_ice" ;
> Tsfc:units = "DegC" ;
> Tsfc:_FillValue = -30000.f ;
> Tsfc:missing_Value = -30000.f ;
> Tsfc:scale_factor = 0.002f ;
> Tsfc:add_offset = 0.f ;
> []
>
> Thank you kindly,
> Greg
>
> _______________________________________________
> pyngl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/pyngl-talk

_______________________________________________
pyngl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/pyngl-talk
Received on Tue Sep 17 14:31:50 2013

This archive was generated by hypermail 2.1.8 : Fri Nov 22 2013 - 09:37:03 MST