...
In general, users set the current variable (which by default is the first variable in the file) to one of those contained in the file and can then apply a number of functions and operators to them :
...
Code Block | ||
---|---|---|
| ||
setcurrent(netcdf1, 6) |
...
# use the 6th variable in this netCDF file |
...
setcurrent(netcdf1, 2) |
...
# use the 2nd variable in this netCDF file |
...
netcdf3 = netcdf1 op netcdf2 |
...
out = function(netcdf1, ...) |
...
However, because functions and operators work only on the current variable, when the netCDF contains more than one variable (a multi-variable netcdf), special care must be taken when you need the operator/function to apply to all variables - this is detailed later in this page.
...
Example 1 : To operate on a netcdf file which you want to overwrite with the new results
Code Block | ||
---|---|---|
| ||
# Derive a cross section of temperature data in a netcdf variable
(...)
# derive the list of netcdf variables
var_list = variables(temp_xs)
# loop over variables and subtract scalar
for i = 1 to count(var_list) do
setcurrent(temp_xs, i)
temp_xs = temp_xs - 273.15 # acts on current variable only
end for |
Example
...
2
...
:
...
To
...
operate
...
on
...
two
...
netcdf
...
files,
...
assigning
...
the
...
result
...
to
...
a
...
third
...
netcdf,
...
you
...
should
...
create
...
the
...
output
...
netcdf
...
first
...
by
...
a
...
simple
...
copying
...
of
...
one
...
of
...
the
...
input
...
netcdfs
...
:
...
Code Block | ||
---|---|---|
| ||
# Derive cross sections of temperature forecast and analysis
# in two netcdf variables, tfc_xs and tan_xs....
(...)
# derive the list of netcdf variables
var_list = variables(tfc_xs)
# create output netcdf
diff_xs = tfc_xs
# loop over variables and create fc-an difference cross-section
for i = 1 to count(var_list) do
setcurrent(tan_xs, i)
setcurrent(tfc_xs, i)
setcurrent(diff_xs, i)
diff_xs = tfc_xs - tan_xs
end for |
Extracting NetCDF values
If you need to have access to the data values of a netcdf current variable, you can use function values() :
val_list = values(netcdf)
which returns a vector with all the values for the current variable.You can then use the Vector functions to manipulate the data. This technique could even be used to create a new Geopoints variable with the netCDF data, or to insert the values into a GRIB field.
An alternative method for accessing individual values is to the use the function value() :
val = value(netcdf, n)
which returns the nth value from the current netcdf variable.