IODA
03-VariablesIntro.py
Go to the documentation of this file.
1 #
2 # (C) Copyright 2020 UCAR
3 #
4 # This software is licensed under the terms of the Apache Licence Version 2.0
5 # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 #
7 
8 # Variables store data. They are generally treated as an extension of
9 # Attributes; everything an Attribute can do, a Variable can do better.
10 # Variables are resizable, chunkable and compressible. They fully support
11 # multidimensional data. They may have attached
12 # dimension scales*, which gives their dimensions meaning. Variables
13 # may be read and written to using Eigen objects, xTensors, and other
14 # user-friendly classes for data manipulation (i.e. replacement ObsDataVectors).
15 
16 import os
17 import sys
18 
19 if os.environ.get('LIBDIR') is not None:
20  sys.path.append(os.environ['LIBDIR'])
21 
22 import ioda
23 # TODO: Uncomment once we are sure that numpy is a required dependency!
24 #import numpy # numpy is very useful when handling multidimensional data.
25 
27  name = "Example-03-python.hdf5",
28  mode = ioda.Engines.BackendCreateModes.Truncate_If_Exists)
29 
30 # You can access all the variables in a group using the ".vars" member object.
31 # i.e. g.vars;
32 
33 # ".vars" is an instance of the "Has_Variables" class. This class is defined
34 # in "include/ioda/Variables/Has_Variables.h". Like with Has_Attributes, the
35 # definition is slightly convoluted, but the key point to remember is that
36 # everything in Has_Varables_Base is available to Has_Variables.
37 # Also remember to check the Doxygen documentation if you get confused.
38 
39 # Let's make some Variables.
40 
41 # Basic variable creation and writing
42 
43 # The create function is the same as for Attributes.
44 intvar1 = g.vars.create(name='var-1', dtype=ioda.Types.int32, dimsCur=[2,3])
45 # The writeVector functions are likewise identical.
46 intvar1.writeVector.int32([1,2,3,4,5,6])
47 # Note: we seldom have variables that have only one element. So,
48 # variables have no readDatum and writeDatum functions.
49 
50 var2 = g.vars.create(name='var-2', dtype=ioda.Types.float, dimsCur=[2,3,4])
51 var2.writeVector.float([1.1,2.2,3.14159,4,5,6,7,8,9,10,11.5,12.6,13,14,15,16,17,18,19,20,21,22,23,24])
52 
53 # Creating and writing multidimensional objects:
54 # TODO: needs numpy in the example environment.
55 # The writeNPArray functions can write any type of numpy array.
56 #var_md = g.vars.create(name='var-md', dtype=ioda.Types.float, dimsCur=[2,3])
57 #var_md_data = np.fromfunction(lambda i, j: i + (j / 2), (2,3), dtype=float)
58 #var_md.writeNPArray.float(var_md_data)
59 
60 # Advanced:
61 # Making a chunked, resizable, compressed variable with default element values.
62 # Chunking expresses how contiguous subsets of data are stored on-disk
63 # and in-memory. It is needed to enable internal data compression.
64 # Usually, ioda sets sensible defaults for chunk sizes
65 # when you create a variable using the ObsGroup interface (Example 5).
66 # But, you might want to override it.
67 # Likewise, ioda usually sets a sensible default for the "fill value"
68 # of a variable (the value used to express missing or unwritten data).
69 # All of these variable creation properties can be controlled by passing
70 # a customized VariableCreationParameters object on function creation.
71 
73 p1.chunk = True
74 p1.chunks = [200, 3]
75 p1.setFillValue.int32(-999)
76 p1.compressWithGZIP()
77 g.vars.create(name='var-3', dtype=ioda.Types.int32, dimsCur=[200,3], dimsMax=[2000,3], params=p1)
78 
79 # Listing variables and checking existence
80 # Same as with attributes.
81 
82 g.vars.list() # Returns a list
83 
84 if len(g.vars.list()) > 5:
85  raise Exception("Way too many variables were created.")
86 
87 if g.vars.exists('var-2') == False:
88  raise Exception("Missing var-2")
89 
90 # Create and remove a variable
91 g.vars.create('removable-int', ioda.Types.int32, [1])
92 g.vars.remove('removable-int')
93 
94 # Opening a variable
95 reopened_v3 = g.vars.open('var-3')
96 
97 # Querying type and dimensions
98 
99 reopened_v3.dims # Trivial. Returns dimensions.
100 
101 if reopened_v3.isA2(dtype=ioda.Types.float) == True:
102  raise Exception("var-3 should be a set of int32s, not single-precision floats!")
103 
104 # Reading data... almost the same as with an Attribute.
105 # You can use readVector or readNPArray.
106 # The data type is used when creating the resultant Python / numPy object.
107 vals2 = var2.readVector.float()
108 
IODA_DL Group createFile(const std::string &filename, BackendCreateModes mode, HDF5_Version_Range compat=defaultVersionRange())
Create a ioda::Group backed by an HDF5 file.
Definition: HH.cpp:113
Used to specify Variable creation-time properties.
Definition: Has_Variables.h:57