@@ -1091,3 +1091,54 @@ def test_stack(obj):
10911091
10921092 actual = obj .cf .stack ({"latlon" : ["latitude" , "longitude" ]})
10931093 assert_identical (expected , actual )
1094+
1095+
1096+ da = xr .DataArray (
1097+ np .arange (10 )[::- 1 ], # like ocean temperature
1098+ dims = "z" ,
1099+ coords = {"z" : ("z" , np .arange (10 ))},
1100+ name = "test" ,
1101+ )
1102+
1103+
1104+ @pytest .mark .parametrize ("obj" , [da , da .to_dataset ()])
1105+ def test_differentiate_positive_upward (obj ):
1106+ obj .z .attrs ["positive" ] = "down"
1107+ expected = obj .differentiate ("z" , 2 )
1108+ actual = obj .cf .differentiate ("z" , 2 )
1109+ assert_identical (expected , actual )
1110+
1111+ obj .z .attrs ["positive" ] = "up"
1112+ expected = obj .differentiate ("z" , 2 )
1113+ actual = obj .cf .differentiate ("z" , 2 , positive_upward = True )
1114+ assert_identical (expected , actual )
1115+
1116+ obj .z .attrs ["positive" ] = "down"
1117+ expected = - 1 * obj .differentiate ("z" , 2 )
1118+ actual = obj .cf .differentiate ("z" , 2 , positive_upward = True )
1119+ assert_identical (expected , actual )
1120+
1121+ obj = obj .isel (z = slice (None , None , - 1 ))
1122+ expected = - 1 * obj .differentiate ("z" , 2 )
1123+ actual = obj .cf .differentiate ("z" , 2 , positive_upward = True )
1124+ assert_identical (expected , actual )
1125+ obj = obj .isel (z = slice (None , None , - 1 ))
1126+
1127+ with xr .set_options (keep_attrs = True ):
1128+ da ["z" ] = obj .z * - 1
1129+ expected = - 1 * obj .differentiate ("z" , 2 )
1130+ actual = obj .cf .differentiate ("z" , 2 , positive_upward = True )
1131+ assert_identical (expected , actual )
1132+
1133+ obj = obj .isel (z = slice (None , None , - 1 ))
1134+ expected = - 1 * obj .differentiate ("z" , 2 )
1135+ actual = obj .cf .differentiate ("z" , 2 , positive_upward = True )
1136+ assert_identical (expected , actual )
1137+
1138+ del obj .z .attrs ["positive" ]
1139+ with pytest .raises (ValueError ):
1140+ obj .cf .differentiate ("z" , positive_upward = True )
1141+
1142+ obj .z .attrs ["positive" ] = "zzz"
1143+ with pytest .raises (ValueError ):
1144+ obj .cf .differentiate ("z" , positive_upward = True )
0 commit comments