-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from nsidc/support-ae-si12
Refactor AMSR-related fetch code into `amsr` subpackage
- Loading branch information
Showing
10 changed files
with
93 additions
and
87 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import re | ||
from typing import Literal | ||
|
||
import xarray as xr | ||
|
||
from pm_tb_data._types import Hemisphere | ||
|
||
AMSR_RESOLUTIONS = Literal["25", "12"] | ||
|
||
|
||
def normalize_amsr_tbs( | ||
data_fields: xr.Dataset, | ||
resolution: AMSR_RESOLUTIONS, | ||
hemisphere: Hemisphere, | ||
) -> xr.Dataset: | ||
"""Normalize the given Tbs from AU_SI* and AE_SI* products. | ||
Currently only returns daily average channels. | ||
Filters out variables that are not Tbs and renames Tbs to the 'standard' | ||
{channel}{polarization} name. E.g., `SI_25km_NH_06H_DAY` becomes `h06` | ||
""" | ||
var_pattern = re.compile( | ||
f"SI_{resolution}km_{hemisphere[0].upper()}H_" | ||
r"(?P<channel>\d{2})(?P<polarization>H|V)_DAY" | ||
) | ||
|
||
tb_data_mapping = {} | ||
for var in data_fields.keys(): | ||
if match := var_pattern.match(str(var)): | ||
# Preserve variable attrs, but rename the variable and it's dims for | ||
# consistency. | ||
tb_data_mapping[ | ||
f"{match.group('polarization').lower()}{match.group('channel')}" | ||
] = xr.DataArray( | ||
data_fields[var].data, | ||
dims=("fake_y", "fake_x"), | ||
attrs=data_fields[var].attrs, | ||
) | ||
|
||
normalized = xr.Dataset( | ||
tb_data_mapping, | ||
) | ||
|
||
return normalized |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import numpy as np | ||
import xarray as xr | ||
from xarray.testing import assert_equal | ||
|
||
from pm_tb_data._types import NORTH | ||
from pm_tb_data.fetch.amsr.util import normalize_amsr_tbs | ||
|
||
|
||
def test_normalize_amsr_tbs(): | ||
mock_au_si_data_fields = xr.Dataset( | ||
data_vars={ | ||
"SI_25km_NH_06H_DAY": (("Y", "X"), np.arange(0, 6).reshape(2, 3)), | ||
"SI_25km_NH_89V_DAY": (("Y", "X"), np.arange(5, 11).reshape(2, 3)), | ||
}, | ||
) | ||
|
||
expected = xr.Dataset( | ||
data_vars={ | ||
"h06": (("fake_y", "fake_x"), np.arange(0, 6).reshape(2, 3)), | ||
"v89": (("fake_y", "fake_x"), np.arange(5, 11).reshape(2, 3)), | ||
}, | ||
) | ||
actual = normalize_amsr_tbs( | ||
data_fields=mock_au_si_data_fields, | ||
resolution="25", | ||
hemisphere=NORTH, | ||
) | ||
|
||
assert_equal(actual, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters