Versions Compared

Key

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

...

Metview provides a set of NetCDF handling macro functions, allowing users to apply a number of functions and operators to NetCDF data - see NetCDF Functions.

 

How operators and functions work on NetCDF

...

Functions and operators working on netcdf netCDF files , apply only to the current variable. When the netcdf netCDF file contains several variables, you need to address each variable separately and explicitly, and apply the function /operator to each in turn. For this purpose, Metview Macro provides functions to query the contents of a netcdf netCDF file and to set one of its variables to be the current variable :.

Users can list the variables held in a netcdf variable by means of the function variables() :

...

     var_list = variables(netcdf)

this This returns a list of strings, each holding a variable name. Counting the number of elements in the output list gives you the number of variables.

To set one of the existing variables to be the current variable, use function setcurrent(), which takes the index (starting at 1) of the desired variable:


     setcurrent(netcdf, n)

where n is the number of the variable to be set as current.

The two functions above provide the basic framework to operate on multi-variable netcdf files :.

Example 1 : To operate on a netcdf file which you want to overwrite with the new results


...

 

Code Block
languagebash
# 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
languagebash
# 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