11import datetime
22import faulthandler
3- import inspect
43import os
54import unittest
65
@@ -25,9 +24,6 @@ def setUp(self):
2524 # self.test_only = ['test_Field_collapse_GROUPS']
2625
2726 def test_Field_collapse_CLIMATOLOGICAL_TIME (self ):
28- if self .test_only and inspect .stack ()[0 ][3 ] not in self .test_only :
29- return
30-
3127 verbose = False
3228
3329 f = cf .example_field (2 )
@@ -258,9 +254,6 @@ def test_Field_collapse_CLIMATOLOGICAL_TIME(self):
258254 self .assertEqual (list (g .shape ), expected_shape )
259255
260256 def test_Field_collapse (self ):
261- if self .test_only and inspect .stack ()[0 ][3 ] not in self .test_only :
262- return
263-
264257 verbose = False
265258
266259 f = cf .read (self .filename2 )[0 ]
@@ -393,9 +386,6 @@ def test_Field_collapse(self):
393386 # )
394387
395388 def test_Field_collapse_WEIGHTS (self ):
396- if self .test_only and inspect .stack ()[0 ][3 ] not in self .test_only :
397- return
398-
399389 verbose = False
400390
401391 f = cf .example_field (2 )
@@ -414,10 +404,40 @@ def test_Field_collapse_WEIGHTS(self):
414404 i = cf .example_field (4 )
415405 i .collapse ("area: maximum" )
416406
407+ # Zero weights
408+ g = cf .example_field (0 )
409+ w = g .weights ('area' )
410+ w .hardmask = False
411+
412+ for method in ('variance' , 'sum_of_weights' ):
413+ f = g .copy ()
414+ weights = w .copy ()
415+
416+ weights [0 , 0 ] = 0
417+ m0 = f .collapse (axes = 'area' , method = method , weights = weights )
418+ weights [0 , 0 ] = cf .masked
419+ m1 = f .collapse (axes = 'area' , method = method , weights = weights )
420+ self .assertTrue (m0 .equals (m1 ))
421+
422+ f [0 , 0 ] = cf .masked
423+ m = f .collapse (axes = 'area' , method = method , weights = weights )
424+ self .assertTrue (m .equals (m0 ))
425+
426+ weights [0 , 0 ] = 0
427+ m0 = f .collapse (axes = 'area' , method = method , weights = weights )
428+ self .assertTrue (m0 .equals (m ))
429+
430+ weights [0 , 0 ] = cf .masked
431+ m0 = f .collapse (axes = 'area' , method = method , weights = weights )
432+ self .assertTrue (m0 .equals (m ))
433+
434+ # Negative weights
435+ weights [0 , 0 ] = - 3.14
436+ g .collapse ('area: mean' , weights = weights , allow_negative_weights = True )
437+ with self .assertRaises (ValueError ):
438+ g .collapse ('area: mean' , weights = weights )
439+
417440 def test_Field_collapse_GROUPS (self ):
418- if self .test_only and inspect .stack ()[0 ][3 ] not in self .test_only :
419- return
420-
421441 verbose = False
422442
423443 f = cf .example_field (2 )
@@ -695,17 +715,6 @@ def test_Field_collapse_GROUPS(self):
695715 print (g .constructs )
696716 self .assertEqual (list (g .shape ), expected_shape , g .shape )
697717
698-
699- # g = f[::2].collapse('T: mean', group=cf.M(5, month=12),
700- # group_span=cf.M(5),group_contiguous=1)
701- # g = f.collapse('T: mean', group=cf.M(5, month= 3),
702- # group_contiguous=1)
703- # g = f.collapse('T: mean', group=cf.M(5, month=12),
704- # group_contiguous=2)
705- # g = f.collapse('T: mean', group=cf.M(5, month= 3),
706- # group_contiguous=2)
707-
708-
709718if __name__ == "__main__" :
710719 print ("Run date:" , datetime .datetime .now ())
711720 cf .environment ()
0 commit comments