forked from pygeode/pygeode.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvar.arith.html
More file actions
326 lines (315 loc) · 19.8 KB
/
var.arith.html
File metadata and controls
326 lines (315 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Arithmetic Operations on Variables — PyGeode 1.0 alpha documentation</title>
<link rel="stylesheet" href="_static/pygtheme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.0 alpha',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="top" title="PyGeode 1.0 alpha documentation" href="index.html" />
<link rel="up" title="Var class overview" href="var.html" />
<link rel="next" title="Element-wise math" href="ufunc.html" />
<link rel="prev" title="Retrieving values from a variable" href="var.get.html" />
<link href="http://fonts.googleapis.com/css?family=Ubuntu:300,300italic,regular,italic,500,500italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="ufunc.html" title="Element-wise math"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="var.get.html" title="Retrieving values from a variable"
accesskey="P">previous</a> |</li>
<li><a href="index.html">PyGeode 1.0 alpha documentation</a> »</li>
<li><a href="reference.html" >Reference</a> »</li>
<li><a href="var.html" accesskey="U">Var class overview</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="arithmetic-operations-on-variables">
<h1>Arithmetic Operations on Variables<a class="headerlink" href="#arithmetic-operations-on-variables" title="Permalink to this headline">¶</a></h1>
<p>These methods are called implicitly when any arithmetic operation is applied to a <a class="reference internal" href="var.html#pygeode.Var" title="pygeode.Var"><tt class="xref py py-class docutils literal"><span class="pre">Var</span></tt></a>.
The result is a new Var object, which will apply the operation to any incoming data requests.
The following is a table of all built-in Python arithmetic operations that are supported:</p>
<table border="1" class="docutils">
<colgroup>
<col width="56%" />
<col width="44%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Method:</th>
<th class="head">Example of use:</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__add__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre">+</span> <span class="pre">y</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__sub__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">y</span></tt></td>
</tr>
<tr class="row-even"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__mul__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre">*</span> <span class="pre">y</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__div__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></tt></td>
</tr>
<tr class="row-even"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__pow__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre">**</span> <span class="pre">y</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__abs__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">abs(x)</span></tt></td>
</tr>
<tr class="row-even"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__neg__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">-x</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__pos__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">+x</span></tt></td>
</tr>
<tr class="row-even"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__mod__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre">%</span> <span class="pre">t</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__lt__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre"><</span> <span class="pre">y</span></tt></td>
</tr>
<tr class="row-even"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__le__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre"><=</span> <span class="pre">y</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__gt__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre">></span> <span class="pre">y</span></tt></td>
</tr>
<tr class="row-even"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__ge__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre">>=</span> <span class="pre">y</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__eq__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y</span></tt></td>
</tr>
<tr class="row-even"><td><tt class="xref py py-func docutils literal"><span class="pre">Var.__ne__()</span></tt></td>
<td><tt class="docutils literal"><span class="pre">x</span> <span class="pre">!=</span> <span class="pre">y</span></tt></td>
</tr>
</tbody>
</table>
<p><strong>Axis rules for arithmetic:</strong></p>
<blockquote>
<div><p>When you use one of the 2-argument operations above, PyGeode tries to match
the two sets of input axes, independant of their respective orders. The
output axes are determined from the inputs using following algorithm:</p>
<ol class="arabic simple">
<li>If one of the Vars contains all the input axes, then the output axes are
exactly those axes, in that order. No further checking is done.</li>
<li>Otherwise, the axes from the first Var are prepended to the list of
output axes.</li>
<li>The axes of the second Var are checked, in order. If the output from
step 2 does not already contain an identical axis, then it is
appended to the end of the output axes.</li>
</ol>
<p>In the above algorithm, two axes are considered to be identical if 1) they
are the same type of axis (e.g. <tt class="docutils literal"><span class="pre">Lat</span></tt>), and 2) they contain the same
values. If either of these criteria fail, then the axes will be considered
distinct, and the output variable will contain both axes.</p>
<p>A special case is the <tt class="docutils literal"><span class="pre">Time</span></tt> axis. The criteria for matching are loosened
to allow for time axes with the same range, but different internal
paritioning. For example, a standard time axis with daily values from
<em>January 1, 2000</em> to <em>December 31, 2005</em> can be matched with a monthly time
axis from <em>January 2000</em> to <em>December 2005</em>. Similarly, both can be matched
to a monthly climatological time axis from <em>January</em> to <em>December</em>. For this
case, the axis with the most values (i.e., the finer level of partitioning)
will be used in the output.</p>
</div></blockquote>
<p>Arithmetic operations between Vars and scalar values are allowed. The scalar
would be treated as an array of repeated values, with the same shape and axes
as the Var argument.</p>
<p>However, operations between Vars and numpy arrays are not well defined, since
numpy arrays are missing crucial information about their axes (e.g. coordinate
values).</p>
<p><strong>See Also:</strong></p>
<blockquote>
<div><p><a class="reference internal" href="var.html"><em>Var class overview</em></a></p>
<p><a class="reference internal" href="ufunc.html"><em>Element-wise math</em></a> (higher-level math on PyGeode variables)</p>
</div></blockquote>
<div class="section" id="example">
<h2>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<dl class="docutils">
<dt>Start with some 2D data:</dt>
<dd><div class="first last highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pygeode.tutorial</span> <span class="kn">import</span> <span class="n">t1</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">t1</span><span class="o">.</span><span class="n">Temp</span>
<span class="go"><Var 'Temp'>:</span>
<span class="go"> Shape: (lat,lon) (32,64)</span>
<span class="go"> Axes:</span>
<span class="go"> lat <Lat> : 85 S to 85 N (32 values)</span>
<span class="go"> lon <Lon> : 0 E to 354 E (64 values)</span>
<span class="go"> Attributes:</span>
<span class="go"> {'units': 'K'}</span>
<span class="go"> Type: Var (dtype="float64")</span>
</pre></div>
</div>
</dd>
<dt>Generate a time axis (say, every 6 hours starting at January 1, 2000)</dt>
<dd><div class="first last highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="gp">>>> </span><span class="n">hour_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">124</span><span class="p">)</span> <span class="o">*</span> <span class="mi">6</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">hour_values</span>
<span class="go">[ 0 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102</span>
<span class="go"> 108 114 120 126 132 138 144 150 156 162 168 174 180 186 192 198 204 210</span>
<span class="go"> 216 222 228 234 240 246 252 258 264 270 276 282 288 294 300 306 312 318</span>
<span class="go"> 324 330 336 342 348 354 360 366 372 378 384 390 396 402 408 414 420 426</span>
<span class="go"> 432 438 444 450 456 462 468 474 480 486 492 498 504 510 516 522 528 534</span>
<span class="go"> 540 546 552 558 564 570 576 582 588 594 600 606 612 618 624 630 636 642</span>
<span class="go"> 648 654 660 666 672 678 684 690 696 702 708 714 720 726 732 738]</span>
<span class="gp">>>> </span><span class="n">start_date</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="mi">2000</span><span class="p">,</span><span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">hour</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pygeode</span> <span class="kn">import</span> <span class="n">StandardTime</span>
<span class="gp">>>> </span><span class="n">taxis</span> <span class="o">=</span> <span class="n">StandardTime</span><span class="p">(</span><span class="n">values</span><span class="o">=</span><span class="n">hour_values</span><span class="p">,</span> <span class="n">units</span><span class="o">=</span><span class="s">'hours'</span><span class="p">,</span> <span class="n">startdate</span><span class="o">=</span><span class="n">start_date</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">taxis</span>
<span class="go">time <StandardTime>: Jan 1, 2000 00:00:00 to Jan 31, 2000 18:00:00 (124 values)</span>
</pre></div>
</div>
</dd>
</dl>
<p><strong>1) Operation between a Var and a scalar</strong></p>
<dl class="docutils">
<dt>Create a small linear temperature trend along the time axis. Since our time axis is stored in units of ‘hours’, let’s make a trend of 0.01 degrees per hour.</dt>
<dd><div class="first last highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">t_trend</span> <span class="o">=</span> <span class="n">taxis</span> <span class="o">*</span> <span class="mf">0.01</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">t_trend</span>
<span class="go"><Var '(time*0.01)'>:</span>
<span class="go"> Shape: (time) (124)</span>
<span class="go"> Axes:</span>
<span class="go"> time <StandardTime>: Jan 1, 2000 00:00:00 to Jan 31, 2000 18:00:00 (124 values)</span>
<span class="go"> Attributes:</span>
<span class="go"> {}</span>
<span class="go"> Type: Mul_Var (dtype="float64")</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">t_trend</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="go">[ 0. 0.06 0.12 0.18 0.24 0.3 0.36 0.42 0.48 0.54 0.6 0.66</span>
<span class="go"> 0.72 0.78 0.84 0.9 0.96 1.02 1.08 1.14 1.2 1.26 1.32 1.38</span>
<span class="go"> 1.44 1.5 1.56 1.62 1.68 1.74 1.8 1.86 1.92 1.98 2.04 2.1</span>
<span class="go"> 2.16 2.22 2.28 2.34 2.4 2.46 2.52 2.58 2.64 2.7 2.76 2.82</span>
<span class="go"> 2.88 2.94 3. 3.06 3.12 3.18 3.24 3.3 3.36 3.42 3.48 3.54</span>
<span class="go"> 3.6 3.66 3.72 3.78 3.84 3.9 3.96 4.02 4.08 4.14 4.2 4.26</span>
<span class="go"> 4.32 4.38 4.44 4.5 4.56 4.62 4.68 4.74 4.8 4.86 4.92 4.98</span>
<span class="go"> 5.04 5.1 5.16 5.22 5.28 5.34 5.4 5.46 5.52 5.58 5.64 5.7</span>
<span class="go"> 5.76 5.82 5.88 5.94 6. 6.06 6.12 6.18 6.24 6.3 6.36 6.42</span>
<span class="go"> 6.48 6.54 6.6 6.66 6.72 6.78 6.84 6.9 6.96 7.02 7.08 7.14</span>
<span class="go"> 7.2 7.26 7.32 7.38]</span>
</pre></div>
</div>
</dd>
</dl>
<p><strong>2) Operation between two Vars</strong></p>
<dl class="docutils">
<dt>Add this to our 2D data, to get a 3D field. Use the time-dependant data as the left argument of the addition, so that the time axis is our first (leftmost) axis for the output.</dt>
<dd><div class="first last highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mydata</span> <span class="o">=</span> <span class="n">t_trend</span> <span class="o">+</span> <span class="n">t1</span><span class="o">.</span><span class="n">Temp</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">mydata</span>
<span class="go"><Var '((time*0.01)+Temp)'>:</span>
<span class="go"> Shape: (time,lat,lon) (124,32,64)</span>
<span class="go"> Axes:</span>
<span class="go"> time <StandardTime>: Jan 1, 2000 00:00:00 to Jan 31, 2000 18:00:00 (124 values)</span>
<span class="go"> lat <Lat> : 85 S to 85 N (32 values)</span>
<span class="go"> lon <Lon> : 0 E to 354 E (64 values)</span>
<span class="go"> Attributes:</span>
<span class="go"> {}</span>
<span class="go"> Type: Add_Var (dtype="float64")</span>
</pre></div>
</div>
</dd>
<dt>Give this variable a better name (so it’s more easily identifiable if we save it to a file):</dt>
<dd><div class="first last highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mydata</span> <span class="o">=</span> <span class="n">mydata</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="s">'Temp'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">mydata</span>
<span class="go"><Var 'Temp'>:</span>
<span class="go"> Shape: (time,lat,lon) (124,32,64)</span>
<span class="go"> Axes:</span>
<span class="go"> time <StandardTime>: Jan 1, 2000 00:00:00 to Jan 31, 2000 18:00:00 (124 values)</span>
<span class="go"> lat <Lat> : 85 S to 85 N (32 values)</span>
<span class="go"> lon <Lon> : 0 E to 354 E (64 values)</span>
<span class="go"> Attributes:</span>
<span class="go"> {'units': 'K'}</span>
<span class="go"> Type: RenamedVar (dtype="float64")</span>
</pre></div>
</div>
</dd>
</dl>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Arithmetic Operations on Variables</a><ul>
<li><a class="reference internal" href="#example">Example</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="var.get.html"
title="previous chapter">Retrieving values from a variable</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="ufunc.html"
title="next chapter">Element-wise math</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/var.arith.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="ufunc.html" title="Element-wise math"
>next</a> |</li>
<li class="right" >
<a href="var.get.html" title="Retrieving values from a variable"
>previous</a> |</li>
<li><a href="index.html">PyGeode 1.0 alpha documentation</a> »</li>
<li><a href="reference.html" >Reference</a> »</li>
<li><a href="var.html" >Var class overview</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2013, Mike Neish, Peter Hitchcock.
Last updated on Sep 13, 2015.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
</div>
</body>
</html>