Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/SnowEx/spicy-snow into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrencher committed Sep 5, 2023
2 parents b7612f9 + b39ab67 commit a66728b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 16 additions & 1 deletion spicy_snow/processing/wet_snow.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def id_newly_wet_snow(dataset: xr.Dataset, wet_thresh: int = -2, inplace: bool =
# identify possible newly wet snow in regions FCF > 0.5 with deltaVV
dataset['wet_flag'] = dataset['wet_flag'].where(((dataset['fcf'] < 0.5) | (dataset['deltaVV'] > wet_thresh)), 1)

# mask nans from Sentinel-1 data
dataset['wet_flag'] = dataset['wet_flag'].where(~dataset['deltaVV'].isnull())

if not inplace:
return dataset

Expand Down Expand Up @@ -73,6 +76,9 @@ def id_newly_frozen_snow(dataset: xr.Dataset, freeze_thresh: int = 2, inplace: b
# identify possible re-freezing by increases of deltaGammaNaught of 2dB
dataset['freeze_flag'] = dataset['freeze_flag'].where(dataset['deltaGamma'] < freeze_thresh, 1)

# mask nans from Sentinel-1 data
dataset['freeze_flag'] = dataset['freeze_flag'].where(~dataset['deltaGamma'].isnull())

if not inplace:
return dataset

Expand Down Expand Up @@ -105,6 +111,9 @@ def id_wet_negative_si(dataset: xr.Dataset, wet_SI_thresh = 0, inplace: bool = F
# identify wetting of snow by negative snow index with snow present
dataset['alt_wet_flag'] = dataset['alt_wet_flag'].where(((dataset['ims'] != 4) | (dataset['snow_index'] > wet_SI_thresh)), 1)

# mask nans from Sentinel-1 data
dataset['alt_wet_flag'] = dataset['alt_wet_flag'].where(~dataset['deltaVV'].isnull())

if not inplace:
return dataset

Expand Down Expand Up @@ -160,11 +169,17 @@ def flag_wet_snow(dataset: xr.Dataset, inplace: bool = False) -> Union[None, xr.
dataset['wet_snow'].loc[dict(time = ts)]= dataset.sel(time = ts)['wet_snow'] + dataset.sel(time = ts)['wet_flag']
dataset['wet_snow'].loc[dict(time = ts)] = dataset.sel(time = ts)['wet_snow'] + dataset.sel(time = ts)['alt_wet_flag']
dataset['wet_snow'].loc[dict(time = ts)] = dataset.sel(time = ts)['wet_snow'].where(dataset.sel(time = ts)['wet_snow'] < 1, 1)

# add newly frozen snow flags to old wet snow and then bound at 0 to avoid negatives
dataset['wet_snow'].loc[dict(time = ts)] = dataset.sel(time = ts)['wet_snow'] - dataset.sel(time = ts)['freeze_flag']
dataset['wet_snow'].loc[dict(time = ts)] = dataset.sel(time = ts)['wet_snow'].where(dataset.sel(time = ts)['wet_snow'] > 0, 0)

# make nans at areas without S1 data
dataset['wet_snow'].loc[dict(time = ts)] = dataset.sel(time = ts)['wet_snow'].where(~dataset['s1'].sel(time = ts, band = 'VV').isnull(), np.nan)

# make
dataset['wet_snow'].loc[dict(time = ts)] = dataset.sel(time = ts)['wet_snow'].where(dataset.sel(time = ts)['ims'] == 4, 0)

prev_time = ts

# if >50% wet of last 4 cycles after feb 1 then set remainer till
Expand Down Expand Up @@ -219,4 +234,4 @@ def flag_wet_snow(dataset: xr.Dataset, inplace: bool = False) -> Union[None, xr.
# if less than 50% are wet then keep the save value for wet_snow otherwise set to 1
dataset['wet_snow'] = dataset['wet_snow'].where(dataset['perma_wet'] < 0.5, 1)

return dataset
return dataset
4 changes: 3 additions & 1 deletion tests/test_wetsnow.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ def test_perma_wet_seasons(self):
alt_wet_flag = np.full((10, 10, n), 0.0)
freeze_flag = np.full((10, 10, n), 0.0)
s1 = np.random.randn(10, 10, n, 3)
ims = np.full((10, 10, n), 4, dtype = int)

x = np.linspace(0, 9, 10)
y = np.linspace(10, 19, 10)
Expand All @@ -515,7 +516,8 @@ def test_perma_wet_seasons(self):
wet_flag = (["x", "y", "time"], wet_flag),
alt_wet_flag = (["x", "y", "time"], alt_wet_flag),
freeze_flag = (["x", "y", "time"], freeze_flag),
s1 = (["x", "y", "time", "band"], s1)
s1 = (["x", "y", "time", "band"], s1),
ims = (["x", "y", "time"], ims)
),
coords = dict(
lon = (["x", "y"], lon),
Expand Down

0 comments on commit a66728b

Please sign in to comment.