]> Wikimedia Canada | Git repositories - eccc_to_commons.git/blob - eccc_merger_almanach.xslt
Rewrite almanach merge logic
[eccc_to_commons.git] / eccc_merger_almanach.xslt
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!-- eccc_merger.xslt - Merge XML historical data as provided by
4 Environment and Climate Change Canada
5 Copyright (C) 2020 Pierre Choffet
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 Rounding is always done one digit after decimal point.
22
23 Output data is calculated as such:
24 temperature.extremeMax : Greatest of all values
25 temperature.extremeMin : Lowest of all values
26 temperature.normalMax : Greatest of all values
27 temperature.normalMin : Lowest of all values
28 temperature.normalMean : Weighted mean of all values
29 precipitation.extremeRainfall : Greatest of all values
30 precipitation.extremeSnowfall : Greatest of all values
31 precipitation.extremePrecipitation: Greatest of all values
32 precipitation.extremeSnowOnGround : Greatest of all values
33 pop : Weighted mean of all values
34 -->
35
36 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
37 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
38 exclude-result-prefixes="xsi">
39
40 <xsl:output method="xml" encoding="utf-8" />
41
42 <xsl:variable name="input-root" select="/" />
43 <xsl:variable name="merge-doc" select="document($merge-path)" />
44
45 <!-- Idempotence -->
46 <xsl:template match="@*|node()">
47 <xsl:copy>
48 <xsl:apply-templates select="@*|node()" />
49 </xsl:copy>
50 </xsl:template>
51
52 <xsl:template match="/climatedata">
53 <xsl:copy>
54 <!-- Upon first merge, import missing metadata elements -->
55 <xsl:if test="not(lang)">
56 <xsl:apply-templates select="$merge-doc/climatedata/lang" />
57 </xsl:if>
58 <xsl:if test="not(stationinformation)">
59 <xsl:apply-templates select="$merge-doc/climatedata/stationinformation" />
60 </xsl:if>
61
62 <xsl:apply-templates select="@*|node()" />
63 </xsl:copy>
64 </xsl:template>
65
66 <xsl:template match="month">
67 <xsl:copy>
68 <xsl:apply-templates select="@index" />
69
70 <!-- ECCC data may not contain all days element, so we're counting by ourserves -->
71 <xsl:call-template name="loop-day">
72 <xsl:with-param name="day" select="1" />
73 </xsl:call-template>
74 </xsl:copy>
75 </xsl:template>
76
77 <xsl:template match="day">
78 <xsl:copy>
79 <xsl:attribute name="index">
80 <xsl:value-of select="@index" />
81 </xsl:attribute>
82
83 <xsl:apply-templates match="node()" />
84 </xsl:copy>
85 </xsl:template>
86
87 <xsl:template match="temperature[@class = 'extremeMax']">
88 <xsl:variable name="month" select="../../@index" />
89 <xsl:variable name="day" select="../@index" />
90 <xsl:variable name="input-extreme-max" select="$input-root/climatedata/month[@index = $month]/day[@index = $day]/temperature[@class = 'extremeMax']" />
91 <xsl:variable name="merge-extreme-max" select="$merge-doc/climatedata/month[@index = $month]/day[@index = $day]/temperature[@class = 'extremeMax']" />
92
93 <xsl:choose>
94 <!-- Both nodes values are numeric and input value is greater or equal than merge
95 or Input exists but merge doesn't or is not numeric
96 -->
97 <xsl:when test="(string(number($input-extreme-max)) != 'NaN' and string(number($merge-extreme-max) != 'NaN') and $input-extreme-max &gt;= $merge-extreme-max) or ($input-extreme-max and (string(number($merge-extreme-max)) = 'NaN'))">
98 <xsl:copy>
99 <xsl:apply-templates select="$input-extreme-max/@*" />
100 <xsl:apply-templates select="$input-extreme-max/node()" />
101 </xsl:copy>
102 </xsl:when>
103 <!-- Both nodes values are numeric and input value is lesser than merge
104 or Input doesn't exist or is not numeric but merge does
105 -->
106 <xsl:when test="(string(number($input-extreme-max)) != 'NaN' and string(number($merge-extreme-max) != 'NaN') and $input-extreme-max &lt; $merge-extreme-max) or ((string(number($input-extreme-max)) = 'NaN') and $merge-extreme-max)">
107 <xsl:copy>
108 <xsl:apply-templates select="$merge-extreme-max/@*" />
109 <xsl:apply-templates select="$merge-extreme-max/node()" />
110 </xsl:copy>
111 </xsl:when>
112 <xsl:otherwise>
113 <xsl:message terminate="no">
114 <xsl:value-of select="concat($month, '-', $day, ' - extremeMax ', number($input-extreme-max), ' ', number($merge-extreme-max))" />
115 </xsl:message>
116 <xsl:message terminate="yes">Trapping case not supposed to happen.</xsl:message>
117 </xsl:otherwise>
118 </xsl:choose>
119 </xsl:template>
120
121 <xsl:template match="temperature[@class = 'extremeMin']">
122 <xsl:variable name="month" select="../../@index" />
123 <xsl:variable name="day" select="../@index" />
124 <xsl:variable name="input-extreme-min" select="$input-root/climatedata/month[@index = $month]/day[@index = $day]/temperature[@class = 'extremeMin']" />
125 <xsl:variable name="merge-extreme-min" select="$merge-doc/climatedata/month[@index = $month]/day[@index = $day]/temperature[@class = 'extremeMin']" />
126
127 <xsl:choose>
128 <!-- Both nodes values are numeric and input value is lesser or equal than merge
129 or Input exists but merge doesn't or is not numeric
130 -->
131 <xsl:when test="(string(number($input-extreme-min)) != 'NaN' and string(number($merge-extreme-min) != 'NaN') and $input-extreme-min &lt;= $merge-extreme-min) or ($input-extreme-min and (string(number($merge-extreme-min)) = 'NaN'))">
132 <xsl:copy>
133 <xsl:apply-templates select="$input-extreme-min/@*" />
134 <xsl:apply-templates select="$input-extreme-min/node()" />
135 </xsl:copy>
136 </xsl:when>
137 <!-- Both nodes values are numeric and input value is greater than merge
138 or Input doesn't exist or is not numeric but merge does
139 -->
140 <xsl:when test="(string(number($input-extreme-min)) != 'NaN' and string(number($merge-extreme-min) != 'NaN') and $input-extreme-min &gt; $merge-extreme-min) or ((string(number($input-extreme-min)) = 'NaN') and $merge-extreme-min)">
141 <xsl:copy>
142 <xsl:apply-templates select="$merge-extreme-min/@*" />
143 <xsl:apply-templates select="$merge-extreme-min/node()" />
144 </xsl:copy>
145 </xsl:when>
146 <xsl:otherwise>
147 <xsl:message terminate="no">
148 <xsl:value-of select="concat($month, '-', $day, ' - extremeMin ', number($input-extreme-min), ' ', number($merge-extreme-min))" />
149 </xsl:message>
150 <xsl:message terminate="yes">Trapping case not supposed to happen.</xsl:message>
151 </xsl:otherwise>
152 </xsl:choose>
153 </xsl:template>
154
155 <xsl:template match="temperature[@class = 'normalMax']">
156 <xsl:variable name="month" select="../../@index" />
157 <xsl:variable name="day" select="../@index" />
158 <xsl:variable name="input-normal-max" select="$input-root/climatedata/month[@index = $month]/day[@index = $day]/temperature[@class = 'normalMax']" />
159 <xsl:variable name="merge-normal-max" select="$merge-doc/climatedata/month[@index = $month]/day[@index = $day]/temperature[@class = 'normalMax']" />
160
161 <xsl:choose>
162 <!-- Both nodes values are numeric and input value is greater or equal than merge
163 or Input exists but merge doesn't or is not numeric
164 -->
165 <xsl:when test="(string(number($input-normal-max)) != 'NaN' and string(number($merge-normal-max) != 'NaN') and $input-normal-max &gt;= $merge-normal-max) or ($input-normal-max and (string(number($merge-normal-max)) = 'NaN'))">
166 <xsl:copy>
167 <xsl:apply-templates select="$input-normal-max/@*" />
168 <xsl:apply-templates select="$input-normal-max/node()" />
169 </xsl:copy>
170 </xsl:when>
171 <!-- Both nodes values are numeric and input value is lesser than merge
172 or Input doesn't exist or is not numeric but merge does
173 -->
174 <xsl:when test="(string(number($input-normal-max)) != 'NaN' and string(number($merge-normal-max) != 'NaN') and $input-normal-max &lt; $merge-normal-max) or ((string(number($input-normal-max)) = 'NaN') and $merge-normal-max)">
175 <xsl:copy>
176 <xsl:apply-templates select="$merge-normal-max/@*" />
177 <xsl:apply-templates select="$merge-normal-max/node()" />
178 </xsl:copy>
179 </xsl:when>
180 <xsl:otherwise>
181 <xsl:message terminate="no">
182 <xsl:value-of select="concat($month, '-', $day, ' - normalMax ', number($input-normal-max), ' ', number($merge-normal-max))" />
183 </xsl:message>
184 <xsl:message terminate="yes">Trapping case not supposed to happen.</xsl:message>
185 </xsl:otherwise>
186 </xsl:choose>
187 </xsl:template>
188
189 <xsl:template match="temperature[@class = 'normalMin']">
190 <xsl:variable name="month" select="../../@index" />
191 <xsl:variable name="day" select="../@index" />
192 <xsl:variable name="input-normal-min" select="$input-root/climatedata/month[@index = $month]/day[@index = $day]/temperature[@class = 'normalMin']" />
193 <xsl:variable name="merge-normal-min" select="$merge-doc/climatedata/month[@index = $month]/day[@index = $day]/temperature[@class = 'normalMin']" />
194
195 <xsl:choose>
196 <!-- Both nodes values are numeric and input value is lesser or equal than merge
197 or Input exists but merge doesn't or is not numeric
198 -->
199 <xsl:when test="(string(number($input-normal-min)) != 'NaN' and string(number($merge-normal-min) != 'NaN') and $input-normal-min &lt;= $merge-normal-min) or ($input-normal-min and (string(number($merge-normal-min)) = 'NaN'))">
200 <xsl:copy>
201 <xsl:apply-templates select="$input-normal-min/@*" />
202 <xsl:apply-templates select="$input-normal-min/node()" />
203 </xsl:copy>
204 </xsl:when>
205 <!-- Both nodes values are numeric and input value is greater than merge
206 or Input doesn't exist or is not numeric but merge does
207 -->
208 <xsl:when test="(string(number($input-normal-min)) != 'NaN' and string(number($merge-normal-min) != 'NaN') and $input-normal-min &gt; $merge-normal-min) or ((string(number($input-normal-min)) = 'NaN') and $merge-normal-min)">
209 <xsl:copy>
210 <xsl:apply-templates select="$merge-normal-min/@*" />
211 <xsl:apply-templates select="$merge-normal-min/node()" />
212 </xsl:copy>
213 </xsl:when>
214 <xsl:otherwise>
215 <xsl:message terminate="no">
216 <xsl:value-of select="concat($month, '-', $day, ' - normalMin ', number($input-normal-min), ' ', number($merge-normal-min))" />
217 </xsl:message>
218 <xsl:message terminate="yes">Trapping case not supposed to happen.</xsl:message>
219 </xsl:otherwise>
220 </xsl:choose>
221 </xsl:template>
222
223 <xsl:template match="temperature[@class = 'normalMean']">
224 <xsl:variable name="month" select="../../@index" />
225 <xsl:variable name="day" select="../@index" />
226 <xsl:variable name="input-normal-mean" select="$input-root/climatedata/month[@index = $month]/day[@index = $day]/temperature[@class = 'normalMean']" />
227 <xsl:variable name="merge-normal-mean" select="$merge-doc/climatedata/month[@index = $month]/day[@index = $day]/temperature[@class = 'normalMean']" />
228
229 <xsl:choose>
230 <!-- Node exists and are not empty on both sides -->
231 <xsl:when test="$input-normal-mean and $input-normal-mean != '' and $merge-normal-mean and $merge-normal-mean/text() != ''">
232 <xsl:copy>
233 <xsl:apply-templates select="$input-normal-mean/@*" />
234 <xsl:attribute name="values-count"><xsl:value-of select="$input-normal-mean/@values-count + 1" /></xsl:attribute>
235 <xsl:value-of select="format-number(number($input-normal-mean/text() * $input-normal-mean/@values-count + $merge-normal-mean/text()) div number($input-normal-mean/@values-count + 1), '0.0')" />
236 </xsl:copy>
237 </xsl:when>
238 <!-- Node exists but are empty on both sides -->
239 <xsl:when test="$input-normal-mean = '' and $merge-normal-mean = ''">
240 <xsl:copy>
241 <xsl:apply-templates select="$input-normal-mean/@*" />
242 <xsl:apply-templates select="$input-normal-mean/node()" />
243 </xsl:copy>
244 </xsl:when>
245 <!-- Node exists in input but not-or-empty in merge -->
246 <xsl:when test="$input-normal-mean and (not($merge-normal-mean) or $merge-normal-mean = '')">
247 <xsl:copy>
248 <xsl:apply-templates select="$input-normal-mean/@*" />
249 <xsl:apply-templates select="$input-normal-mean/node()" />
250 </xsl:copy>
251 </xsl:when>
252 <!-- Node doesn't exist or is empty in input and exists in merge -->
253 <xsl:when test="(not($input-normal-mean) or $input-normal-mean = '') and $merge-normal-mean">
254 <xsl:copy>
255 <xsl:apply-templates select="$merge-normal-mean/@*" />
256 <xsl:if test="$merge-normal-mean != ''">
257 <xsl:attribute name="values-count">1</xsl:attribute>
258 <xsl:apply-templates select="$merge-normal-mean/node()" />
259 </xsl:if>
260 </xsl:copy>
261 </xsl:when>
262 <xsl:otherwise>
263 <xsl:message terminate="no">
264 <xsl:value-of select="concat($month, '-', $day, ' - normalMean: ', $input-normal-mean, ' ', $merge-normal-mean)" />
265 </xsl:message>
266 <xsl:message terminate="yes">Trapping case not supposed to happen.</xsl:message>
267 </xsl:otherwise>
268 </xsl:choose>
269 </xsl:template>
270
271 <xsl:template match="precipitation[@class = 'extremeRainfall']">
272 <xsl:variable name="month" select="../../@index" />
273 <xsl:variable name="day" select="../@index" />
274 <xsl:variable name="input-precipitation-rainfall" select="$input-root/climatedata/month[@index = $month]/day[@index = $day]/precipitation[@class = 'extremeRainfall']" />
275 <xsl:variable name="merge-precipitation-rainfall" select="$merge-doc/climatedata/month[@index = $month]/day[@index = $day]/precipitation[@class = 'extremeRainfall']" />
276
277 <xsl:choose>
278 <!-- Both nodes values are numeric and input value is greater or equal than merge
279 or Input exists but merge doesn't or is not numeric
280 -->
281 <xsl:when test="(string(number($input-precipitation-rainfall)) != 'NaN' and string(number($merge-precipitation-rainfall) != 'NaN') and $input-precipitation-rainfall &gt;= $merge-precipitation-rainfall) or ($input-precipitation-rainfall and (string(number($merge-precipitation-rainfall)) = 'NaN'))">
282 <xsl:copy>
283 <xsl:apply-templates select="$input-precipitation-rainfall/@*" />
284 <xsl:apply-templates select="$input-precipitation-rainfall/node()" />
285 </xsl:copy>
286 </xsl:when>
287 <!-- Both nodes values are numeric and input value is lesser than merge
288 or Input doesn't exist or is not numeric but merge does
289 -->
290 <xsl:when test="(string(number($input-precipitation-rainfall)) != 'NaN' and string(number($merge-precipitation-rainfall) != 'NaN') and $input-precipitation-rainfall &lt; $merge-precipitation-rainfall) or ((string(number($input-precipitation-rainfall)) = 'NaN') and $merge-precipitation-rainfall)">
291 <xsl:copy>
292 <xsl:apply-templates select="$merge-precipitation-rainfall/@*" />
293 <xsl:apply-templates select="$merge-precipitation-rainfall/node()" />
294 </xsl:copy>
295 </xsl:when>
296 <xsl:otherwise>
297 <xsl:message terminate="no">
298 <xsl:value-of select="concat($month, '-', $day, ' - extremeRainfall ', number($input-precipitation-rainfall), ' ', number($merge-precipitation-rainfall))" />
299 </xsl:message>
300 <xsl:message terminate="yes">Trapping case not supposed to happen.</xsl:message>
301 </xsl:otherwise>
302 </xsl:choose>
303 </xsl:template>
304
305 <xsl:template match="precipitation[@class = 'extremeSnowfall']">
306 <xsl:variable name="month" select="../../@index" />
307 <xsl:variable name="day" select="../@index" />
308 <xsl:variable name="input-precipitation-snowfall" select="$input-root/climatedata/month[@index = $month]/day[@index = $day]/precipitation[@class = 'extremeSnowfall']" />
309 <xsl:variable name="merge-precipitation-snowfall" select="$merge-doc/climatedata/month[@index = $month]/day[@index = $day]/precipitation[@class = 'extremeSnowfall']" />
310
311 <xsl:choose>
312 <!-- Both nodes values are numeric and input value is greater or equal than merge
313 or Input exists but merge doesn't or is not numeric
314 -->
315 <xsl:when test="(string(number($input-precipitation-snowfall)) != 'NaN' and string(number($merge-precipitation-snowfall) != 'NaN') and $input-precipitation-snowfall &gt;= $merge-precipitation-snowfall) or ($input-precipitation-snowfall and (string(number($merge-precipitation-snowfall)) = 'NaN'))">
316 <xsl:copy>
317 <xsl:apply-templates select="$input-precipitation-snowfall/@*" />
318 <xsl:apply-templates select="$input-precipitation-snowfall/node()" />
319 </xsl:copy>
320 </xsl:when>
321 <!-- Both nodes values are numeric and input value is lesser than merge
322 or Input doesn't exist or is not numeric but merge does
323 -->
324 <xsl:when test="(string(number($input-precipitation-snowfall)) != 'NaN' and string(number($merge-precipitation-snowfall) != 'NaN') and $input-precipitation-snowfall &lt; $merge-precipitation-snowfall) or ((string(number($input-precipitation-snowfall)) = 'NaN') and $merge-precipitation-snowfall)">
325 <xsl:copy>
326 <xsl:apply-templates select="$merge-precipitation-snowfall/@*" />
327 <xsl:apply-templates select="$merge-precipitation-snowfall/node()" />
328 </xsl:copy>
329 </xsl:when>
330 <xsl:otherwise>
331 <xsl:message terminate="no">
332 <xsl:value-of select="concat($month, '-', $day, ' - extremeSnowfall ', number($input-precipitation-snowfall), ' ', number($merge-precipitation-snowfall))" />
333 </xsl:message>
334 <xsl:message terminate="yes">Trapping case not supposed to happen.</xsl:message>
335 </xsl:otherwise>
336 </xsl:choose>
337 </xsl:template>
338
339 <xsl:template match="precipitation[@class = 'extremePrecipitation']">
340 <xsl:variable name="month" select="../../@index" />
341 <xsl:variable name="day" select="../@index" />
342 <xsl:variable name="input-precipitation-extreme" select="$input-root/climatedata/month[@index = $month]/day[@index = $day]/precipitation[@class = 'extremePrecipitation']" />
343 <xsl:variable name="merge-precipitation-extreme" select="$merge-doc/climatedata/month[@index = $month]/day[@index = $day]/precipitation[@class = 'extremePrecipitation']" />
344
345 <xsl:choose>
346 <!-- Both nodes values are numeric and input value is greater or equal than merge
347 or Input exists but merge doesn't or is not numeric
348 -->
349 <xsl:when test="(string(number($input-precipitation-extreme)) != 'NaN' and string(number($merge-precipitation-extreme) != 'NaN') and $input-precipitation-extreme &gt;= $merge-precipitation-extreme) or ($input-precipitation-extreme and (string(number($merge-precipitation-extreme)) = 'NaN'))">
350 <xsl:copy>
351 <xsl:apply-templates select="$input-precipitation-extreme/@*" />
352 <xsl:apply-templates select="$input-precipitation-extreme/node()" />
353 </xsl:copy>
354 </xsl:when>
355 <!-- Both nodes values are numeric and input value is lesser than merge
356 or Input doesn't exist or is not numeric but merge does
357 -->
358 <xsl:when test="(string(number($input-precipitation-extreme)) != 'NaN' and string(number($merge-precipitation-extreme) != 'NaN') and $input-precipitation-extreme &lt; $merge-precipitation-extreme) or ((string(number($input-precipitation-extreme)) = 'NaN') and $merge-precipitation-extreme)">
359 <xsl:copy>
360 <xsl:apply-templates select="$merge-precipitation-extreme/@*" />
361 <xsl:apply-templates select="$merge-precipitation-extreme/node()" />
362 </xsl:copy>
363 </xsl:when>
364 <xsl:otherwise>
365 <xsl:message terminate="no">
366 <xsl:value-of select="concat($month, '-', $day, ' - extremePrecipitation ', number($input-precipitation-extreme), ' ', number($merge-precipitation-extreme))" />
367 </xsl:message>
368 <xsl:message terminate="yes">Trapping case not supposed to happen.</xsl:message>
369 </xsl:otherwise>
370 </xsl:choose>
371 </xsl:template>
372
373 <xsl:template match="precipitation[@class = 'extremeSnowOnGround']">
374 <xsl:variable name="month" select="../../@index" />
375 <xsl:variable name="day" select="../@index" />
376 <xsl:variable name="input-precipitation-snow-ground" select="$input-root/climatedata/month[@index = $month]/day[@index = $day]/precipitation[@class = 'extremeSnowOnGround']" />
377 <xsl:variable name="merge-precipitation-snow-ground" select="$merge-doc/climatedata/month[@index = $month]/day[@index = $day]/precipitation[@class = 'extremeSnowOnGround']" />
378
379 <xsl:choose>
380 <!-- Both nodes values are numeric and input value is greater or equal than merge
381 or Input exists but merge doesn't or is not numeric
382 -->
383 <xsl:when test="(string(number($input-precipitation-snow-ground)) != 'NaN' and string(number($merge-precipitation-snow-ground) != 'NaN') and $input-precipitation-snow-ground &gt;= $merge-precipitation-snow-ground) or ($input-precipitation-snow-ground and (string(number($merge-precipitation-snow-ground)) = 'NaN'))">
384 <xsl:copy>
385 <xsl:apply-templates select="$input-precipitation-snow-ground/@*" />
386 <xsl:apply-templates select="$input-precipitation-snow-ground/node()" />
387 </xsl:copy>
388 </xsl:when>
389 <!-- Both nodes values are numeric and input value is lesser than merge
390 or Input doesn't exist or is not numeric but merge does
391 -->
392 <xsl:when test="(string(number($input-precipitation-snow-ground)) != 'NaN' and string(number($merge-precipitation-snow-ground) != 'NaN') and $input-precipitation-snow-ground &lt; $merge-precipitation-snow-ground) or ((string(number($input-precipitation-snow-ground)) = 'NaN') and $merge-precipitation-snow-ground)">
393 <xsl:copy>
394 <xsl:apply-templates select="$merge-precipitation-snow-ground/@*" />
395 <xsl:apply-templates select="$merge-precipitation-snow-ground/node()" />
396 </xsl:copy>
397 </xsl:when>
398 <xsl:otherwise>
399 <xsl:message terminate="no">
400 <xsl:value-of select="concat($month, '-', $day, ' - extremeSnowOnGround ', number($input-precipitation-snow-ground), ' ', number($merge-precipitation-snow-ground))" />
401 </xsl:message>
402 <xsl:message terminate="yes">Trapping case not supposed to happen.</xsl:message>
403 </xsl:otherwise>
404 </xsl:choose>
405 </xsl:template>
406
407 <xsl:template match="pop">
408 <xsl:variable name="month" select="../../@index" />
409 <xsl:variable name="day" select="../@index" />
410 <xsl:variable name="input-pop" select="$input-root/climatedata/month[@index = $month]/day[@index = $day]/pop" />
411 <xsl:variable name="merge-pop" select="$merge-doc/climatedata/month[@index = $month]/day[@index = $day]/pop" />
412
413 <xsl:choose>
414 <!-- Node exists and are not empty on both sides -->
415 <xsl:when test="$input-pop and $input-pop != '' and $merge-pop and $merge-pop/text() != ''">
416 <xsl:copy>
417 <xsl:apply-templates select="$input-pop/@*" />
418 <xsl:attribute name="values-count"><xsl:value-of select="$input-pop/@values-count + 1" /></xsl:attribute>
419 <xsl:value-of select="format-number(number($input-pop/text() * $input-pop/@values-count + $merge-pop/text()) div number($input-pop/@values-count + 1), '0.0')" />
420 </xsl:copy>
421 </xsl:when>
422 <!-- Node exists but are empty on both sides -->
423 <xsl:when test="$input-pop = '' and $merge-pop = ''">
424 <xsl:copy>
425 <xsl:apply-templates select="$input-pop/@*" />
426 <xsl:apply-templates select="$input-pop/node()" />
427 </xsl:copy>
428 </xsl:when>
429 <!-- Node exists in input but not-or-empty in merge -->
430 <xsl:when test="$input-pop and (not($merge-pop) or $merge-pop = '')">
431 <xsl:copy>
432 <xsl:apply-templates select="$input-pop/@*" />
433 <xsl:apply-templates select="$input-pop/node()" />
434 </xsl:copy>
435 </xsl:when>
436 <!-- Node doesn't exist or is empty in input and exists in merge -->
437 <xsl:when test="(not($input-pop) or $input-pop = '') and $merge-pop">
438 <xsl:copy>
439 <xsl:apply-templates select="$merge-pop/@*" />
440 <xsl:if test="$merge-pop != ''">
441 <xsl:attribute name="values-count">1</xsl:attribute>
442 <xsl:apply-templates select="$merge-pop/node()" />
443 </xsl:if>
444 </xsl:copy>
445 </xsl:when>
446 <xsl:otherwise>
447 <xsl:message terminate="no">
448 <xsl:value-of select="concat($month, '-', $day, ' - pop: ', $input-pop, ' ', $merge-pop)" />
449 </xsl:message>
450 <xsl:message terminate="yes">Trapping case not supposed to happen.</xsl:message>
451 </xsl:otherwise>
452 </xsl:choose>
453 </xsl:template>
454
455 <xsl:template name="loop-day">
456 <xsl:param name="day" />
457
458 <xsl:variable name="input-day" select="$input-root/climatedata/month[@index = current()/@index]/day[@index = $day]" />
459 <xsl:variable name="merge-day" select="$merge-doc/climatedata/month[@index = current()/@index]/day[@index = $day]" />
460
461 <xsl:choose>
462 <xsl:when test="$input-day">
463 <xsl:apply-templates select="$input-day" />
464 </xsl:when>
465 <xsl:when test="$merge-day">
466 <xsl:apply-templates select="$merge-day" />
467 </xsl:when>
468 </xsl:choose>
469
470 <xsl:if test="$day &lt; 31">
471 <xsl:call-template name="loop-day">
472 <xsl:with-param name="day" select="$day + 1" />
473 </xsl:call-template>
474 </xsl:if>
475 </xsl:template>
476
477 <!-- Almanach files contain no flags -->
478 <xsl:template match="legend" />
479
480 <!-- Remove quality indicator -->
481 <xsl:template match="@quality" />
482 </xsl:stylesheet>