Skip to content

Commit

Permalink
test, refact: Conforming normalizing dataset
Browse files Browse the repository at this point in the history
sup3r expects data in a certain way and implicitly apply some
transformations, so it is required to conform with that to be able to
compare answers.
  • Loading branch information
castelao committed Jun 24, 2024
1 parent e08ea76 commit d916ea9
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions tests/bias/test_presrat_bias_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,31 @@ def fut_cc(fp_fut_cc):
axis=-1).astype('float32')
"""
ds = xr.open_dataset(fp_fut_cc)
# This compute here is required.

# Operating with numpy arrays impose a fixed dimensions order
# This compute is required here.
da = ds['rsds'].compute().transpose('lat', 'lon', 'time')
# Unfortunatelly, _get_factors() assume latitude as descending

# The _get_factors() assume latitude as descending and it will
# silently return wrong values otherwise.
da = da.sortby('lat', ascending=False)
# data = da.data
# time = pd.to_datetime(da.time)

latlon = np.stack(
xr.broadcast(da['lat'], da['lon'] - 360), axis=-1
).astype('float32')
# latlon = np.stack(np.meshgrid(da['lon'] - 360, da['lat']), axis=-1)[
# :, :, ::-1
# ].astype('float32')
for ii in range(4):
for jj in range(4):
)
# Confirm that dataset order is consistent
# Somewhere in pipeline latlon are downgraded to f32
assert np.allclose(latlon.astype('float32'), FP_CC_LAT_LON)

# Verify data alignment in comparison with expected for FP_CC
for ii in range(ds.lat.size):
for jj in range(ds.lon.size):
assert np.allclose(
da.sel(lat=latlon[ii, jj, 0], method='nearest').sel(
lon=latlon[ii, jj, 1] + 360, method='nearest'
)[0],
da.data[ii, jj, 0],
da.sel(lat=latlon[ii, jj, 0]).sel(
lon=latlon[ii, jj, 1] + 360
),
da.data[ii, jj],
)
assert np.allclose(latlon, FP_CC_LAT_LON)

return da.compute()

Expand Down Expand Up @@ -139,7 +143,7 @@ def fut_cc_notrend(fp_fut_cc_notrend):
ds['lon'] = ds['lon'] - 360

# Operating with numpy arrays impose a fixed dimensions order
# Somehow this compute is required here.
# This compute is required here.
da = ds['rsds'].compute().transpose('lat', 'lon', 'time')

# The _get_factors() assume latitude as descending and it will
Expand Down

0 comments on commit d916ea9

Please sign in to comment.