File: Collapse.py

package info (click to toggle)
cf-python 1.3.2+dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 7,996 kB
  • sloc: python: 51,733; ansic: 2,736; makefile: 78; sh: 2
file content (483 lines) | stat: -rw-r--r-- 28,343 bytes parent folder | download
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import cf
import numpy
import os
import time

def test(chunk_sizes=(17, 34, 60, 300, 100000)):
    start_time = time.time()

    print '----------------------------------------------------------'
    print 'cf.collapse'
    print '----------------------------------------------------------'
    # Save original chunksize
    original_chunksize = cf.CHUNKSIZE()

    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                            "test_file.nc")

    for squeeze in (True, False):
        for missing_data in (True, False):
            for chunk in chunk_sizes:
            
                cf.CHUNKSIZE(chunk)
                
                f = cf.read(filename, squeeze=squeeze)[0]
                
                parameters = 'CHUNKSIZE=%s, missing_data=%s, squeeze=%s' % \
                    (cf.CHUNKSIZE(), missing_data, squeeze)            

                if missing_data:
                    for i in range(9):
                        f.subspace[..., [i, 9-i], i] = cf.masked
        
                print f
                print f.array                    
                print f.Data.dumpd()
                
                w = f.cm('area')
                w /= 10000.
                w = w.copy()
                w.transpose()
                w = w.array
                w = w.reshape(f.array.shape)
                if numpy.ma.is_masked(f.array):
                    w = numpy.ma.array(w, mask=f.array.mask)
                assert(w.shape == f.array.shape)
                print repr(f)
                print '--------------------------------------------------------------------'
                print 'Unweighted collapse, %s' % parameters
                print '--------------------------------------------------------------------'
                    
                method='mean'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights=None)
                    expected = numpy.ma.average(f.array, axis=axis)
                    assert numpy.ma.allclose(c.array.flatten(), expected), '%s %s %s\n%s\n%s' % (method, axes, axis, c.array, expected)
                    print "Unweighted %s over %s passed" % (method, repr(axes))
                        
                method='maximum'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights=None)
                    expected = numpy.ma.amax(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Unweighted %s over %s passed" % (method, repr(axes))
                        
                method='minimum'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights=None)
                    expected = numpy.ma.amin(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Unweighted %s over %s passed" % (method, repr(axes))
                  
                method='mid_range'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights=None)
                    expected = numpy.ma.ptp(f.array, axis=axis) / 2.0
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Unweighted %s over %s passed" % (method, repr(axes))
                
                method='sum'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights=None)
                    expected = numpy.ma.sum(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Unweighted %s over %s passed" % (method, repr(axes))
                
                method='variance'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights=None)
                    expected = numpy.ma.var(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Unweighted biased %s over %s passed" % (method, repr(axes))
                
                method='variance'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights=None, biased=False)
                    expected = numpy.ma.var(f.array, axis=axis, ddof=1)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Unweighted unbiased %s over %s passed" % (method, repr(axes))
                
                method='standard_deviation'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights=None)
                    expected = numpy.ma.std(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Unweighted biased %s over %s passed" % (method, repr(axes))
                
                method='standard_deviation'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights=None, biased=False)
                    expected = numpy.ma.std(f.array, axis=axis, ddof=1)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Unweighted unbiased %s over %s passed" % (method, repr(axes))
                
                print '--------------------------------------------------------------------'
                print 'Weighted collapse, %s' % parameters
                print '--------------------------------------------------------------------'
                    
                method='maximum'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY')
                    expected = numpy.ma.amax(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted %s over %s passed" % (method, repr(axes))
                        
                method='minimum'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY')
                    expected = numpy.ma.amin(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted %s over %s passed" % (method, repr(axes))
                  
                method='mid_range'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY')
                    expected = numpy.ma.ptp(f.array, axis=axis) / 2.0
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted %s over %s passed" % (method, repr(axes))
                
                method='sum'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY')
                    expected = numpy.ma.sum(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted %s over %s passed" % (method, repr(axes))
                
                method='mean'
                #for axes, axis in zip(('grid_latitude', 'grid_longitude',
                #                       ['grid_longitude', 'grid_latitude'], None),
                #                      (-2, -1, None, None)):
                #    print f
                #    print 'AXES=', axes
                #    print  f.Data._axes
                #    print 'f.shape=', f.shape
                #    print 'f.array=', f.array
                #    print 'w.shape=', w.shape
                #    print 'w=', w
                #    print f.cm().array
                #    c = cf.collapse(f, method, axes=axes, weights='XY')
                #    expected = numpy.ma.average(f.array, weights=w, axis=axis)
                #    print 'c.array=', c.array
                #    print 'n=', expected
                #    assert numpy.ma.allclose(c.array.flatten(), expected), 'weighted %s %s %s\n%s\n%s' % (method, axes, axis, c.array, expected)
                #    print "Weighted %s over %s passed" % (method, repr(axes))
        
#                method='mean'
#                for axes, axis, y in zip(('grid_latitude', 'grid_longitude',
#                                          ['grid_longitude', 'grid_latitude'], None),
#                                         (-2, -1, None, None),
#                                         ('grid_lat', 'grid_longitude',
#                                          ('grid_long', 'grid_latitude'),
#                                          ('X', 'grid_latitude'))):
#                    c = cf.collapse(f, method, axes=axes, weights=y)
#                    expected = numpy.ma.average(f.array, weights=w, axis=axis)
#                    assert numpy.ma.allclose(c.array.flatten(), expected), 'weighted %s %s %s\n%s\n%s' % (method, axes, axis, c.array, expected)
#                    print "Weighted %s over %s passed" % (method, repr(axes))
#        
#                method='variance'
#                for axes, axis in zip(('grid_latitude', 'grid_longitude',
#                                       ['grid_longitude', 'grid_latitude'], None),
#                                      (-2, -1, None, None)):
#                    c = cf.collapse(f, method, axes=axes, weights='XY') 
#                    mean, sw = numpy.ma.average(f.array, weights=w, returned=True, axis=axis)
#                    if axis is not None:
#                        mean = numpy.expand_dims(mean, axis)
#                    expected = numpy.ma.average((f.array-mean)**2, weights=w, axis=axis)
#                    assert(numpy.ma.allclose(c.array.flatten(), expected))
#                    print "Weighted %s over %s passed" % (method, repr(axes))
#        
#                method='variance'
#                for axes, axis in zip(('grid_latitude', 'grid_longitude',
#                                       ['grid_longitude', 'grid_latitude'], None),
#                                      (-2, -1, None, None)):
#                    c = cf.collapse(f, method, axes=axes, weights='XY', biased=False) 
#                    mean, sw = numpy.ma.average(f.array, weights=w, returned=True, axis=axis)
##                    print 'mean=', mean
##                    print 'sw=', sw
#                    if axis is not None:
#                        mean = numpy.expand_dims(mean, axis)
#                    variance = numpy.ma.average((f.array-mean)**2, weights=w, axis=axis)
#                    sw2      = numpy.ma.sum(w**2, axis=axis)
#                    expected = (sw**2/((sw**2)-sw2))*variance 
##                    print 'sw2=', sw2
##                    print 'variance=', variance
##                    print  c.array
##                    print expected
#                    assert(numpy.ma.allclose(c.array.flatten(), expected))
#                    print "Weighted unbiased %s over %s passed" % (method, repr(axes))
#        
#                method='standard_deviation'
#                for axes, axis in zip(('grid_latitude', 'grid_longitude',
#                                       ['grid_longitude', 'grid_latitude'], None),
#                                      (-2, -1, None, None)):
#                    c = cf.collapse(f, method, axes=axes, weights='XY') 
#                    mean, sw = numpy.ma.average(f.array, weights=w, returned=True, axis=axis)
#                    if axis is not None:
#                        mean = numpy.expand_dims(mean, axis)
#                    variance = numpy.ma.average((f.array-mean)**2, weights=w, axis=axis)
#                    expected = variance**0.5
#                    assert(numpy.ma.allclose(c.array.flatten(), expected))
#                    print "Weighted %s over %s passed" % (method, repr(axes))
#        
#                method='standard_deviation'
#                for axes, axis in zip(('grid_latitude', 'grid_longitude',
#                                       ['grid_longitude', 'grid_latitude'], None),
#                                      (-2, -1, None, None)):
#                    c = cf.collapse(f, method, axes=axes, weights='XY', biased=False) 
#                    mean, sw = numpy.ma.average(f.array, weights=w, returned=True, axis=axis)
#                    sw2      = numpy.ma.sum(w**2, axis=axis)
#                    if axis is not None:
#                        mean = numpy.expand_dims(mean, axis)
#                    variance = numpy.ma.average((f.array-mean)**2, weights=w, axis=axis)
#                    variance *= sw**2/(sw**2-sw2)
#                    expected = variance**0.5
#                    assert(numpy.ma.allclose(c.array.flatten(), expected))
#                    print "Weighted unbiased %s over %s passed" % (method, repr(axes))
        
                print '--------------------------------------------------------------------'
                print 'Weighted collapse, no cell measure, %s' % parameters
                print '--------------------------------------------------------------------'
        
                cm = f.remove_item('area')
        
                wlat = cf.tools.collapse.calc_weights(f.item('grid_latitude'),
                                                      infer_bounds=True).array
                wlon = cf.tools.collapse.calc_weights(f.item('grid_longitude'),
                                                      infer_bounds=True).array
        
                w0 = wlat.reshape(10, 1)
                w1 = wlon.reshape( 1, 9)
                w = w0*w1
                w = w.reshape(f.array.shape)
                assert(w.shape == f.array.shape)
              
                if numpy.ma.is_masked(f.array):
                    w = numpy.ma.array(w, mask=f.array.mask)
        
                method='maximum'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY')
                    expected = numpy.ma.amax(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, %s over %s passed" % (method, repr(axes))
                        
                method='minimum'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY')
                    expected = numpy.ma.amin(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, %s over %s passed" % (method, repr(axes))
                  
                method='mid_range'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY')
                    expected = numpy.ma.ptp(f.array, axis=axis) / 2.0
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, %s over %s passed" % (method, repr(axes))
                
                method='sum'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY')
                    expected = numpy.ma.sum(f.array, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, %s over %s passed" % (method, repr(axes))
                
                method='mean'
                for axes, axis, x in zip(('grid_latitude', 'grid_longitude',
                                          ['grid_longitude', 'grid_latitude'], None),
                                         (-2, -1, None, None),
                                         (wlat, wlon, w, w)):
                    c = cf.collapse(f, method, axes=axes, weights='XY')
                    expected = numpy.ma.average(f.array, weights=x, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, %s over %s passed" % (method, repr(axes))
        
                method='mean'
                for axes, axis, x, y in zip(('grid_latitude', 'grid_longitude',
                                             ['grid_longitude', 'grid_latitude'], None),
                                            (-2, -1, None, None),
                                            (wlat, wlon, w, w),
                                            ('Y', 'X', 'XY', 'YX')):
                    c = cf.collapse(f, method, axes=axes, weights=y)
                    expected = numpy.ma.average(f.array, weights=x, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, %s over %s passed" % (method, repr(axes))
        
                method='mean'
                for axes, axis, x, y in zip(('grid_latitude', 'grid_longitude',
                                             ['grid_longitude', 'grid_latitude'], None),
                                            (-2, -1, None, None),
                                            (wlat, wlon, w, w),
                                            ('grid_latitude', 'grid_longitude',
                                             ('grid_longitude', 'grid_latitude'),
                                             ('X', 'grid_latitude'))):
                    c = cf.collapse(f, method, axes=axes, weights=y)
                    expected = numpy.ma.average(f.array, weights=x, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, %s over %s passed" % (method, repr(axes))
        
                method='variance'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY') 
                    mean, sw = numpy.ma.average(f.array, weights=w, returned=True, axis=axis)
                    if axis is not None:
                        mean = numpy.expand_dims(mean, axis)
                    expected = numpy.ma.average((f.array-mean)**2, weights=w, axis=axis)
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, %s over %s passed" % (method, repr(axes))
        
                method='variance'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY', biased=False) 
                    mean, sw = numpy.ma.average(f.array, weights=w, returned=True, axis=axis)
#                    print 'mean=', mean
#                    print 'sw=', sw
                    if axis is not None:
                        mean = numpy.expand_dims(mean, axis)
                    variance = numpy.ma.average((f.array-mean)**2, weights=w, axis=axis)
                    sw2      = numpy.ma.sum(w**2, axis=axis)
                    expected = (sw**2/((sw**2)-sw2))*variance 
#                    print 'sw2=', sw2
#                    print 'variance=', variance
#                    print  c.array
#                    print expected
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, unbiased %s over %s passed" % (method, repr(axes))
        
                method='standard_deviation'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY') 
                    mean, sw = numpy.ma.average(f.array, weights=w, returned=True, axis=axis)
                    if axis is not None:
                        mean = numpy.expand_dims(mean, axis)
                    variance = numpy.ma.average((f.array-mean)**2, weights=w, axis=axis)
                    expected = variance**0.5
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, %s over %s passed" % (method, repr(axes))
        
                method='standard_deviation'
                for axes, axis in zip(('grid_latitude', 'grid_longitude',
                                       ['grid_longitude', 'grid_latitude'], None),
                                      (-2, -1, None, None)):
                    c = cf.collapse(f, method, axes=axes, weights='XY', biased=False) 
                    mean, sw = numpy.ma.average(f.array, weights=w, returned=True, axis=axis)
                    sw2      = numpy.ma.sum(w**2, axis=axis)
                    if axis is not None:
                        mean = numpy.expand_dims(mean, axis)
                    variance = numpy.ma.average((f.array-mean)**2, weights=w, axis=axis)
                    variance *= sw**2/(sw**2-sw2)
                    expected = variance**0.5
                    assert(numpy.ma.allclose(c.array.flatten(), expected))
                    print "Weighted, no cell measure, unbiased %s over %s passed" % (method, repr(axes))
        
                #axes=['grid_longitude', 'grid_latitude']
                #c = cf.collapse(f, 'mean', axes=axes, weights='XY')
                #
                #expected = numpy.ma.average(f.array, weights=w)
                #assert(numpy.ma.allclose(c.array.flatten(), expected))
                #print "Weighted mean over %s (no cell measure) passed" % repr(axes)
                #
                #cell_methods='grid_longitude: mean grid_latitude: max'
                #c = cf.collapse(f, cell_methods, weights='XY')
                #d = cf.collapse(f, 'mean', axes='grid_longitude', weights='XY')
                #expected = cf.collapse(d, 'max', axes='grid_latitude', weights='XY').array
                #assert(numpy.ma.allclose(c.array.flatten(), expected))
                #print "Weighted mean, max over %s (no cell measure) passed" % repr(cell_methods)
                #
                #f.domain.insert_cm(cm, axes=['dim1', 'dim0'])
                #
                #axes=['grid_longitude', 'grid_latitude']
                #c = cf.collapse(f, 'mean', axes=axes, weights='XY')
                #expected = numpy.ma.average(f.array, weights=w)
                #assert(numpy.ma.allclose(c.array.flatten(), expected))
                #print "Weighted mean over %s (with cell measure) passed" % repr(axes)
                #
                #axes=None
                #c = cf.collapse(f, 'variance', weights='XY')
                #mean, sw = numpy.ma.average(f.array, weights=w, returned=True)          
                #expected = numpy.ma.sum(w*(f.array-mean)**2)/sw #numpy.ma.sum(w)
                #assert(numpy.ma.allclose(c.array.flatten(), expected))
                #print "Weighted biased variance over %s passed" % repr(axes)
                #
                #axes=None
                #c = cf.collapse(f, 'variance', weights='XY', biased=False)
                #mean, sw = numpy.ma.average(f.array, weights=w, returned=True)
                #sw2 = numpy.sum(w**2)
                #expected = (sw/(sw**2-sw2))*numpy.sum(w*((f.array-mean)**2))
                #expected = (sw**2/(sw**2-sw2))*numpy.ma.average((f.array-mean)**2, weights=w)
                #assert(numpy.ma.allclose(c.array.flatten(), expected))
                #print "Weighted unbiased variance over %s passed" % repr(axes)
                #
                #print '--------------------------------------------------------------------'
                #print 'Cell measures weighted collapse'
                #print '--------------------------------------------------------------------'
                #
                #axes=['grid_longitude', 'grid_latitude']
                #c = cf.collapse(f, 'mean', axes=axes, weights='XY')
                #w = f.cm('area').copy()
                #w.transpose()
                #w = w.array
                #expected = numpy.ma.average(f.array, weights=w)
                #assert(numpy.ma.allclose(c.array.flatten(), expected))
                #print "Cell measures weighted mean over %s passed" % repr(axes)
                
                f.cell_methods = cf.CellMethods('grid_longitude: mean grid_latitude: maximum')
            #--- End: for
        #--- End: for
    #--- End: for
      
    # Reset chunk size
    cf.CHUNKSIZE(original_chunksize)

    time_elapsed = (time.time() - start_time)/60.0
    print
    print '---------------------------------------------------------------------------'
    print 'All cf.collapse tests passed for cf version', cf.__version__
    print 'Running from', os.path.abspath(cf.__file__)
    print 'Time elapsed: %f minutes' % time_elapsed
    print '---------------------------------------------------------------------------' 
    print 
#--- End: def

if __name__ == "__main__":
    test()