Versions Compared

Key

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

...

Save the changes, and visualise this new icon. See how the settings in the visualiser icon correspond to the variable names in the data. Now visualise another field from the same file. Use the supplied shading_20_levels icon on the plots.

...

Extracting netCDF meta-data

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)

Run the macro - this is what it should print:

Code Block
[longitude,latitude,time,t2m,d2m]
ATTRIBUTES(scale_factor:0.001611,add_offset:254.569370,missing_value:-32767,units:K,long_name:2 metre temperature)

The variables() function returns a list of all the variable names in the 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"].

Image Added

Adapt this macro so that it plots the t2m field and gives it a title based on its long_name attribute. Here are some hints:

  • drop your NetCDF Visualiser icon into the Macro Editor
  • also 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

Computations on netCDF data

Simple computations between netCDFs can be performed in a similar way to fieldsets, but they are only performed on the current variable for each netCDF. The folder contains two netCDF files: fc_12.nc and fc_36.nc, representing a 12-hour and a 36-hour forecast valid at the same time. Write a small macro which reads both files, sets their current variable to t2m and computes the difference. The last line should look something like:

Code Block
diff = fc_36 - fc_12

The variable diff is also a netCDF variable - confirm with this line: "print(type(diff))". Its contents should be identical to the first netCDF in the computation (fc_36), but with the values of its t2m variable updated to be the differences between the two fields. Adapt some of the code from the previous exercise to plot the difference field (and use the supplied Contouring icons pos_shade and neg_shade).

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'

ASCII Data

ASCII Table Data

Metview incorporates functionality to read, process and visualise data stored in ASCII table files, including the commonly-used CSV (comma-separated value) format.

...

Although Metview has some functionality for handling this type of data in Macro, it can do much more with the geopoints format. Therefore, if the data points are in geographic coordinates, one useful exercise is to read one of these files and convert it to geopoints.

...

First, use the values() function to extract arrays of lats, lons and T2m from the CSV data. These will be returned in variables of type vector - this is an in-memory array of double-precision numbers.

Panel
vector or list values( table, number )
vector or list values( table, string )

Returns the given column specified either by an index (starting at 1) or a name (only valid if the table has a header row). If the column type is number, a vector is returned; if it is string, then a list of strings is returned. If the column cannot be found, an error message is generated.

...

There are many more string functions available.

 

Now do the reverse: write this list of parameters into another text file. The new file should look exactly like the original. Here are some hints:

...

There is a dedicated tutorial for handling ODB data in Metview on the Tutorials page.

Extra Work

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.

Optimisations to file writing

...

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.

...

data

...

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