Developer Documentation
TemplateVar
For most users, it is sufficient to be able to create a OutputVar
by using SimDir
or by reading it directly from a NetCDF file (e.g. read_var(nc_data_file)
).
The main struct in ClimaAnalysis
is OutputVar
. To manually construct a OutputVar
from scratch involves either making your own NetCDF file or manually defining the attributes, dimensions, dimension attributes, and data. This process is prone to errors (e.g., mismatches between dimension size and data size or mismatch between variable names) and is repetitive when initializing multiple OutputVar
s for testing.
To solve this issue, we introduce TemplateVar
. A TemplateVar
can be thought of as an uninitialized OutputVar
which is not materialized until it is called with initialize
. See the example below of constructing a TemplateVar
and initializing it into an OutputVar
.
var =
Template.TemplateVar() |>
Template.add_attribs(long_name = "Test", short_name = "test") |>
Template.add_time_dim(name = "t", dim = collect(0.0:2.0)) |>
Template.add_lon_dim(units = "degrees") |>
Template.add_lat_dim(axis = "Y") |>
Template.one_to_n_data(collected = true) |>
Template.initialize
Attributes:
short_name => test
long_name => Test
Dimension attributes:
t:
units => seconds
longitude:
units => degrees
latitude:
units => degrees_east
axis => Y
Data defined over:
t with 3 elements (0.0 to 2.0)
longitude with 361 elements (-180.0 to 180.0)
latitude with 181 elements (-90.0 to 90.0)
The convenience functions for adding a dimension are add_time_dim
, add_lon_dim
, add_lat_dim
, add_pfull_dim
, and add_z_dim
which accept the keyword arguments dim_name
, dim_array
, and units
. Any other keyword arguments will be used as attributes for the dimension. These functions are designed to be used with the pipe operator (|>
). Additionally, there are also functions of the form add_x_dim!
which are designed to be used with function composition. The x
can be time
, lon
, lat
, pfull
, or z
.
The order in which dimensions are added to the TemplateVar
determines their order in the OutputVar
. If the same dimension is added again, then the existing dimension is overwritten with the new dimension. As a result, when defining a dimension, one must add all the dimension attributes, name of the dimension, and the dimension array at once.
When adding multiple attributes using add_attribs
or add_attribs!
, existing attributes will be overwritten. There is currently no functionality to remove attributes.
If multiple functions are provided for specifying the data, only the last one will be used.
There is also the convenience function make_template_var
which is shown below.
# Order of arguments determine the order of the dimensions in OutputVar
# Names passed to `make_template_var` is not used for the names of the dimensions of the
# OutputVar
var =
Template.make_template_var("t", "long", "lat"; abc = "def") |>
Template.add_lon_dim(name = "lon", dim = [1.0, 2.0]) |>
Template.add_time_dim(name = "t", dim = [3.0, 4.0]) |>
Template.initialize
Attributes:
abc => def
Dimension attributes:
t:
units => seconds
lon:
units => degrees_north
lat:
units => degrees_east
Data defined over:
t with 2 elements (3.0 to 4.0)
lon with 2 elements (1.0 to 2.0)
lat with 181 elements (-90.0 to 90.0)
API
Template
ClimaAnalysis.Template.TemplateVar
— MethodTemplateVar()
Intialize a TemplateVar
.
A TemplateVar
is an uninitialized OutputVar
.
Examples
This example shows how to add attributes, dimensions, and data to a TemplateVar
and initialize it.
using ClimaAnalysis.Template
var =
Template.TemplateVar() |>
Template.add_attribs(long_name = "Test", short_name = "test") |>
Template.add_time_dim(name = "t", dim = collect(0.0:2.0)) |>
Template.add_lon_dim(units = "degrees") |>
Template.add_lat_dim(axis = "Y") |>
Template.one_to_n_data(collected = true) |>
Template.initialize
ClimaAnalysis.Template.initialize
— Functioninitialize(var::TemplateVar)
Initialize a TemplateVar
into an OutputVar
.
ClimaAnalysis.Template.add_attribs
— Functionadd_attribs(; attribs...)
Return the function add_attribs
with the keyword arguments attribs
.
Designed to be used with the pipe operator (|>
).
ClimaAnalysis.Template.add_attribs!
— Functionadd_attribs!(var::TemplateVar; attribs...)
Add attributes and return var
.
Designed to be used with function composition.
ClimaAnalysis.Template.add_dim
— Functionadd_dim(dim_name, dim; dim_attribs...)
Add dim_name
dimension with array dim
and attributes dim_attribs
.
There are the convenience functions add_time_dim
, add_lon_dim
, add_lat_dim
, add_pfull_dim
, and add_z_dim
which uses default values for dim_name
, dim
, and units
.
ClimaAnalysis.Template.add_dim!
— Functionadd_dim!(var::TemplateVar, dim_name, dim; dim_attribs...)
Add dim_name
dimension with array dim
and attributes dim_attribs
.
There are the convenience functions add_time_dim!
, add_lon_dim!
, add_lat_dim!
, add_pfull_dim!
, and add_z_dim!
which uses default values for dim_name
, dim
, and units
.
ClimaAnalysis.Template.ones_data
— Functionones_data(; data_type = Float64)
Add ones data to TemplateVar
.
Designed to be used with the pipe operator (|>
).
ClimaAnalysis.Template.ones_data!
— Functionones_data!(var::TemplateVar; data_type = Float64)
Add ones data to TemplateVar
.
Designed to be used with function composition.
ClimaAnalysis.Template.zeros_data
— Functionzeros_data(; data_type = Float64)
Add zeros data to TemplateVar
.
Designed to be used with the pipe operator (|>
).
ClimaAnalysis.Template.zeros_data!
— Functionzeros_data!(var::TemplateVar; data_type = Float64)
Add zeros data to TemplateVar
.
Designed to be used with function composition.
ClimaAnalysis.Template.one_to_n_data
— Functionone_to_n_data(; data_type = Float64, collected = false)
Add data of 1:n
to var
where n
is the product of the sizes of the dimensions.
If collected = false
, then collect
is not called on the data and if collected = true
, then collect
is called on the data.
Designed to be used with the pipe operator (|>
).
ClimaAnalysis.Template.one_to_n_data!
— Functionone_to_n_data!(var::TemplateVar; data_type = Float64, collected = false)
Add data of 1:n
to var
where n
is the product of the sizes of the dimensions.
If collected = true
, then collect
is not called on the data and if collected = false
, then collect
is called on the data.
Designed to be used with function composition.
ClimaAnalysis.Template.make_template_var
— Functionmake_template_var(dims::String...; attribs...)
Make a TemplateVar
with the specified dimensions in dims
and additional attribs
.
The order of dims
dictates the order of the dimensions in the OutputVar
after initialization. The names of dims
is not passed to the TemplateVar
.