|
7 | 7 | There are various options to do this, the recommended option is to use `cf native functions <https://ncas-cms.github.io/cf-python/class/cf.Field.html#mathematical-operations>`_, as they preserve units and metadata associated with fields. Sometimes, however, the function you need is not implemented in cf, so there are alternative methods. |
8 | 8 | """ |
9 | 9 |
|
10 | | -#%% [markdown] |
| 10 | +# %% [markdown] |
11 | 11 | # |
12 | 12 | # .. figure:: ../images/data-operations-flowchart.png |
13 | 13 | # :scale: 50 % |
|
16 | 16 | # It is recommended to use the highest possible implementation of a given function as shown by the chart. |
17 | 17 | # |
18 | 18 |
|
19 | | -#%% |
| 19 | +# %% |
20 | 20 | # 1. Import cf-python: |
21 | 21 |
|
22 | 22 | import cf |
23 | 23 |
|
24 | | -#%% |
| 24 | +# %% |
25 | 25 | # 2. Read the template field constructs from the example: |
26 | 26 |
|
27 | 27 | f = cf.example_field(1) |
28 | 28 | print(f) |
29 | 29 |
|
30 | | -#%% [markdown] |
| 30 | +# %% [markdown] |
31 | 31 | # |
32 | 32 | # 1: Native cf |
33 | 33 | # ------------ |
|
37 | 37 | # Additionally, where a function or operation has a specific domain, cf will mask any erroneous elements that were not processed properly. |
38 | 38 | # |
39 | 39 |
|
40 | | -#%% |
| 40 | +# %% |
41 | 41 | # 1. Create an instance of the template field to work with: |
42 | 42 |
|
43 | 43 | field1 = f.copy() |
44 | 44 |
|
45 | | -#%% |
| 45 | +# %% |
46 | 46 | # 2. Calculate the sine of the elements in the data array: |
47 | 47 |
|
48 | 48 | new_field = field1.sin() |
49 | 49 |
|
50 | 50 | print(new_field.data) |
51 | 51 |
|
52 | | -#%% |
| 52 | +# %% |
53 | 53 | # Alternatively, we can update the original field in place using the ``inplace`` parameter: |
54 | 54 |
|
55 | 55 | field1.sin(inplace=True) |
|
59 | 59 | # cf will automatically update the units of our field depending on the operation. |
60 | 60 | # Here, since the sine is a dimensionless value, we get the units "1". |
61 | 61 |
|
62 | | -print(f.units) # Original |
63 | | -print(field1.units) # After operation |
| 62 | +print(f.units) # Original |
| 63 | +print(field1.units) # After operation |
64 | 64 |
|
65 | | -#%% [markdown] |
| 65 | +# %% [markdown] |
66 | 66 | # |
67 | 67 | # 2: Dask |
68 | 68 | # ------- |
|
83 | 83 | # from outside of cf. |
84 | 84 | # |
85 | 85 |
|
86 | | -#%% |
| 86 | +# %% |
87 | 87 | # 1. Import the necessary Dask module: |
88 | 88 |
|
89 | 89 | import dask as da |
90 | 90 |
|
91 | | -#%% |
| 91 | +# %% |
92 | 92 | # 2. Create an instance of the template field to work with: |
93 | 93 |
|
94 | 94 | field2 = f.copy() |
95 | 95 |
|
96 | | -#%% |
| 96 | +# %% |
97 | 97 | # 3. Load the data from the field as a Dask array: |
98 | 98 |
|
99 | 99 | data = field2.data |
100 | 100 |
|
101 | 101 | dask_array = data.to_dask_array() |
102 | 102 |
|
103 | | -#%% |
| 103 | +# %% |
104 | 104 | # 4. Create a new field, calculate the sine of the elements, |
105 | 105 | # and write the array to the new field: |
106 | 106 |
|
|
112 | 112 |
|
113 | 113 | print(new_field.data) |
114 | 114 |
|
115 | | -#%% |
| 115 | +# %% |
116 | 116 | # 5. Manually update the units: |
117 | 117 |
|
118 | | -new_field.override_units('1', inplace=True) |
| 118 | +new_field.override_units("1", inplace=True) |
119 | 119 |
|
120 | 120 | print(new_field.units) |
121 | 121 |
|
122 | | -#%% |
| 122 | +# %% |
123 | 123 | # To instead update the original field in place, as before: |
124 | 124 |
|
125 | 125 | calculated_array = da.array.sin(dask_array) |
126 | 126 |
|
127 | 127 | field2.set_data(calculated_array) |
128 | 128 |
|
129 | | -field2.override_units('1', inplace=True) |
| 129 | +field2.override_units("1", inplace=True) |
130 | 130 |
|
131 | 131 | print(field2.data) |
132 | 132 | print(field2.units) |
133 | 133 |
|
134 | | -#%% [markdown] |
| 134 | +# %% [markdown] |
135 | 135 | # |
136 | 136 | # 3: NumPy Universal Functions |
137 | 137 | # ---------------------------- |
|
145 | 145 | # As above, take care to manually update any metadata for the new field. |
146 | 146 | # |
147 | 147 |
|
148 | | -#%% |
| 148 | +# %% |
149 | 149 | # 1. Import NumPy: |
150 | 150 |
|
151 | 151 | import numpy as np |
152 | 152 |
|
153 | | -#%% |
| 153 | +# %% |
154 | 154 | # 2. Create an instance of the template field to work with: |
155 | 155 |
|
156 | 156 | field3 = f.copy() |
157 | 157 |
|
158 | | -#%% |
| 158 | +# %% |
159 | 159 | # 3. Create a new field, compute the sine of the elements, |
160 | 160 | # and write the array to the new field: |
161 | 161 |
|
|
167 | 167 |
|
168 | 168 | print(new_field.data) |
169 | 169 |
|
170 | | -#%% |
| 170 | +# %% |
171 | 171 | # 4. Manually update the units: |
172 | 172 |
|
173 | | -new_field.override_units('1', inplace=True) |
| 173 | +new_field.override_units("1", inplace=True) |
174 | 174 |
|
175 | 175 | print(new_field.units) |
176 | 176 |
|
177 | | -#%% [markdown] |
| 177 | +# %% [markdown] |
178 | 178 | # |
179 | 179 | # 4: NumPy Vectorization |
180 | 180 | # ---------------------- |
|
187 | 187 | # array and applying the function. |
188 | 188 | # |
189 | 189 |
|
190 | | -#%% |
| 190 | +# %% |
191 | 191 | # 1. Import our third-party function; here, from the ``math`` module: |
192 | 192 |
|
193 | 193 | import math |
194 | 194 |
|
195 | | -#%% |
| 195 | +# %% |
196 | 196 | # 2. Create an instance of the template field to work with: |
197 | 197 |
|
198 | 198 | field4 = f.copy() |
199 | 199 |
|
200 | | -#%% |
| 200 | +# %% |
201 | 201 | # 3. Vectorize the function with NumPy: |
202 | 202 |
|
203 | 203 | vectorized_function = np.vectorize(math.sin) |
204 | 204 |
|
205 | | -#%% |
| 205 | +# %% |
206 | 206 | # 4. Create a new field, calculate the sine of the elements, |
207 | 207 | # and write the array to the new field: |
208 | 208 |
|
|
214 | 214 |
|
215 | 215 | print(new_field.data) |
216 | 216 |
|
217 | | -#%% |
| 217 | +# %% |
218 | 218 | # 5. Manually update the units: |
219 | 219 |
|
220 | | -new_field.override_units('1', inplace=True) |
| 220 | +new_field.override_units("1", inplace=True) |
221 | 221 |
|
222 | 222 | print(new_field.units) |
223 | 223 |
|
224 | | -#%% [markdown] |
| 224 | +# %% [markdown] |
225 | 225 | # |
226 | 226 | # Performance |
227 | 227 | # ----------- |
|
0 commit comments