Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Write the following code into a new Macro (you can exclude the comments):

Code Block
languagepy
# read the netCDF file and print its list of variables
nc = read("fc_12.nc")
vars = variables(nc)
print(vars)   #  we could also do:  print(variables(nc))

# set the current variable to be t2m and print its attributes
setcurrent(nc, 't2m')
atts = attributes(nc)
print(atts)

...

The variables() function returns a list of all the variable names from in the whole netCDF file.

Most of the netCDF functions work on the current variable, set in the setcurrent() function. The attributes() function returns a definition - a set of named members, similar to a Python dictionary, relevant to the current variable. A definition's elements can be accessed either using the 'dot' operator, e.g. atts.units,  or using indexing notation, e.g. atts["units"].

...

  • drop your NetCDF Visualiser icon into the Macro Editor,
  • also add drop the Contouring icon
  • create a Text Plotting icon and enter a random title string, then drop this into the Macro Editor
  • replace the value to the right of text_line_1 with the value of the long_name attribute
  • add a plot() command which contains the NetCDF Visualiser variable, the Contouring variable and the Text Plotting variable
  • feel free to add the units attribute to the title as well, using the ampersand operator & to concatenate parts of the string

...

Note that in these netCDF files, the data values are scaled in the netCDF file. The actual values for the t2m variable are encoded as 16-bit integers, but they have scale_factor and add_offset attributes which Metview applies by default.

Image Added

We can see this by extracting the values. Try the following macro, which will print the 'real world' values from t2m:

Code Block
languagepy
# read the netCDF file
nc = read("fc_12.nc")

# set the current variable to be t2m and print its values
setcurrent(nc, 't2m')
vals = values(nc)
print(vals)
print('max: ', maxvalue(vals))
print('min: ', minvalue(vals))

Now add the following line before the call to values() :

Code Block
languagepy
netcdf_auto_scale_values(0) # 1 means 'on', 0 means 'off'

Now the results should look different and will reflect the values as they are packed in the file.

Try something similar with the time variable:

Code Block
languagepy
# select time as the current variable and print its values
setcurrent(nc, 'time')
times = values(nc)
print(times)

The result is a list of date variables. These will be explained in more detail in the session Handling Time in Metview.

To get the 'packed' values for this variable, put this line before the call to values() :

Code Block
netcdf_auto_translate_times(0)  # 1 means 'on', 0 means 'off'

Other ASCII Data

ASCII Table Data

...

See if you can write a macro which extracts lat, lon and value columns into vectors and creates a new geopoints variable from the data.

NetCDF

...

Modify your first netCDF macro which plots the t2m variable and make it compute the temperature in degrees Celcius by subtracting 273.15 from it before plotting.

 Have a look at the macro netcdf_info in the solutions folder to see how to extract meta-data from the netCDF file.