Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix wrong reprojction to wrf domain #60

Merged
merged 41 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9bb8fb2
fix wrong reprojction to wrf domain
b-kode Jun 2, 2022
4e017c2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 2, 2022
2692986
address pre-commit checks
b-kode Jun 2, 2022
f011213
address more pre-commit checks
matthiasdemuzere Jun 2, 2022
c984319
fix too many indices error
matthiasdemuzere Jun 2, 2022
1143a70
fix values in test_ucp_resampler_output_values_per_paramater
matthiasdemuzere Jun 2, 2022
4846a4a
fix remaining tests
matthiasdemuzere Jun 2, 2022
d58691d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 2, 2022
3af355a
add test for new _get_wrf_grid_info()
matthiasdemuzere Jun 2, 2022
00558b8
Merge remote-tracking branch 'origin/fix_wrf_reproject' into fix_wrf_…
matthiasdemuzere Jun 2, 2022
507233e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 2, 2022
34284bd
update files
matthiasdemuzere Jun 2, 2022
8e54acf
Merge remote-tracking branch 'origin/fix_wrf_reproject' into fix_wrf_…
matthiasdemuzere Jun 2, 2022
a5a9743
update files
matthiasdemuzere Jun 2, 2022
dc2eb3b
update results of sample files
matthiasdemuzere Jun 2, 2022
e53e1d3
add different proj to _get_wrf_grid_info()
matthiasdemuzere Jun 3, 2022
19d3f20
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2022
1e1910b
add Latlon - cylindrical equidistant
matthiasdemuzere Jun 3, 2022
f0678d3
clean
matthiasdemuzere Jun 3, 2022
81c1a15
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2022
5ecb61c
clean up further
matthiasdemuzere Jun 3, 2022
dd03dc9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2022
b9ce0b7
add test file MAP_PROJ>6
matthiasdemuzere Jun 3, 2022
2d7feb6
update sample data
matthiasdemuzere Jun 3, 2022
d4bfd1a
try fix proj4 definitions?
matthiasdemuzere Jun 3, 2022
6cfeb74
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2022
c4e12dc
test nx ny definition?
matthiasdemuzere Jun 3, 2022
7accec0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2022
98edc02
test nx ny definition?
matthiasdemuzere Jun 3, 2022
8b0ad96
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2022
8227550
try with definition wrf-python
matthiasdemuzere Jun 3, 2022
2f12c09
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2022
3ac3553
add wrf-python to cfg
matthiasdemuzere Jun 3, 2022
1dd0307
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2022
433671a
wrf_proj.to_proj4()
matthiasdemuzere Jun 3, 2022
677e9f5
wrf_proj.proj4()
matthiasdemuzere Jun 3, 2022
991d611
revert back, no wrf-python
matthiasdemuzere Jun 3, 2022
273febd
fix all map_proj re-projections.
matthiasdemuzere Jun 8, 2022
b5b821c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 8, 2022
8d661f3
fix minor issues
matthiasdemuzere Jun 9, 2022
fd1659b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added sample_data/geo_em.d04_LCZ_extent.nc
Binary file not shown.
Binary file added sample_data/geo_em.d04_LCZ_params.nc
Binary file not shown.
Binary file added sample_data/geo_em.d04_NoUrban.nc
Binary file not shown.
Binary file modified testing/geo_em.d04_LCZ_extent.nc
Binary file not shown.
Binary file modified testing/geo_em.d04_LCZ_params.nc
Binary file not shown.
Binary file modified testing/geo_em.d04_LCZ_params_no_urb_param.nc
Binary file not shown.
Binary file removed testing/geo_em.d04_gridinfo.tif
Binary file not shown.
95 changes: 44 additions & 51 deletions tests/w2w_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from w2w.w2w import _get_lcz_band
from w2w.w2w import _get_SW_BW
from w2w.w2w import _get_truncated_normal_sample
from w2w.w2w import _get_wrf_grid_info
from w2w.w2w import _hgt_resampler
from w2w.w2w import _hi_resampler
from w2w.w2w import _initialize_urb_param
Expand All @@ -34,7 +35,6 @@
from w2w.w2w import checks_and_cleaning
from w2w.w2w import create_lcz_extent_file
from w2w.w2w import create_lcz_params_file
from w2w.w2w import create_wrf_gridinfo
from w2w.w2w import expand_land_cat_parents
from w2w.w2w import Info
from w2w.w2w import main
Expand All @@ -50,7 +50,6 @@ def _create_info(info_override):
'src_file_clean': '',
'dst_file': '',
'dst_nu_file': '',
'dst_gridinfo': '',
'dst_lcz_extent_file': '',
'dst_lcz_params_file': '',
'BUILT_LCZ': [],
Expand All @@ -74,11 +73,10 @@ def test_create_info_dict():
built_lcz=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
)
info = Info.from_argparse(args)
# info is Dict, with 9 keys
assert len(info) == 9
# info is Dict, with 8 keys
assert len(info) == 8

# Three files are tifs
assert info.dst_gridinfo == 'input/directory/wrf_file_gridinfo.tif'
# Two files are tifs
assert info.src_file == 'input/directory/lcz_file.tif'
assert info.src_file_clean == 'input/directory/lcz_file_clean.tif'

Expand Down Expand Up @@ -111,7 +109,7 @@ def test_get_lcz_band_lcz_generator(info_mock, capsys):
pytest.param(['--lcz-band', '5'], 5, id='custom band'),
),
)
def test_main_lcz_band_arg(arg, exp, info_mock, capsys, tmpdir):
def test_main_lcz_band_arg(arg, exp, capsys, tmpdir):
input_dir = tmpdir.mkdir('input')
input_files = (
'geo_em.d01.nc',
Expand Down Expand Up @@ -254,7 +252,6 @@ def test_check_lcz_wrf_extent_lcz_too_small(capsys, info_mock):

out, _ = capsys.readouterr()
assert ('ERROR: LCZ domain should be larger than WRF domain') in out
# TODO maybe add the actual values to check they are correct
assert (
'ERROR: LCZ domain should be larger than WRF domain '
'in all directions.\nLCZ bounds (xmin, ymin, xmax, ymax): '
Expand Down Expand Up @@ -357,24 +354,25 @@ def test_wrf_remove_urban_output_already_exists_is_overwritten(tmpdir, info_mock
assert m_time_old != os.path.getmtime(info.dst_nu_file)


def test_create_wrf_gridinfo(tmpdir, info_mock):
def test_get_wrf_grid_info(info_mock):
info = info_mock(
{
'dst_nu_file': 'testing/5by5.nc',
'dst_gridinfo': os.path.join(tmpdir, 'dst_gridinfo.tif'),
'dst_file': 'sample_data/geo_em.d04.nc',
}
)
create_wrf_gridinfo(info)
assert os.listdir(tmpdir) == ['dst_gridinfo.tif']
tif = rxr.open_rasterio(info.dst_gridinfo)
assert tif.rio.crs.to_proj4() == '+init=epsg:4326'
assert tif.rio.transform() == Affine(
0.01000213623046875,
wrf_grid_info = _get_wrf_grid_info(info)
crs_string = (
'+proj=eqc +lat_ts=0 +lat_0=0 +lon_0=5.84999990463257 '
'+x_0=0 +y_0=0 +R=6370000 +units=m +no_defs'
)
assert wrf_grid_info['crs'] == crs_string
assert wrf_grid_info['transform'] == Affine(
1111.7747802734375,
0.0,
-1.4050254821777344,
-838835.115342933,
0.0,
0.010000228881835938,
41.480000495910645,
1111.7747802734375,
4577176.609447704,
)


Expand Down Expand Up @@ -437,11 +435,11 @@ def test_get_lcz_arr(info_mock):
@pytest.mark.parametrize(
('ucp_key', 'data_sum', 'data_mean'),
(
('MH_URB2D', 2, 1.1429937),
('STDH_URB2D', 2, 0.29812142),
('LB_URB2D', 1, 0.15104416),
('LF_URB2D', 1, 0.07032267),
('LP_URB2D', 1, 0.08072148),
('MH_URB2D', 2, 1.14196181),
('STDH_URB2D', 2, 0.29785209),
('LB_URB2D', 1, 0.15090767),
('LF_URB2D', 1, 0.070259198),
('LP_URB2D', 1, 0.080648496),
matthiasdemuzere marked this conversation as resolved.
Show resolved Hide resolved
),
)
def test_ucp_resampler_output_values_per_paramater(
Expand All @@ -451,7 +449,8 @@ def test_ucp_resampler_output_values_per_paramater(
{
'src_file': 'sample_data/lcz_zaragoza.tif',
'src_file_clean': 'testing/lcz_zaragoza_clean.tif',
'dst_gridinfo': 'testing/geo_em.d04_gridinfo.tif',
'dst_file': 'sample_data/geo_em.d04.nc',
'dst_nu_file': 'testing/geo_em.d04_NoUrban.nc',
'BUILT_LCZ': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
)
Expand All @@ -476,7 +475,8 @@ def test_ucp_resampler_output_values_per_paramater_frc_threshold(info_mock):
{
'src_file': 'sample_data/lcz_zaragoza.tif',
'src_file_clean': 'testing/lcz_zaragoza_clean.tif',
'dst_gridinfo': 'testing/geo_em.d04_gridinfo.tif',
'dst_file': 'sample_data/geo_em.d04.nc',
'dst_nu_file': 'testing/geo_em.d04_NoUrban.nc',
'BUILT_LCZ': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
)
Expand All @@ -492,7 +492,7 @@ def test_ucp_resampler_output_values_per_paramater_frc_threshold(info_mock):
assert ucp_res.shape == (1, 102, 162)
# Per parameter, check # max values and non-0 domain average
assert np.sum(ucp_res.data == ucp_res.data.max()) == 1
assert approx(np.mean(ucp_res.data[ucp_res.data > 0])) == 0.428921
assert approx(np.mean(ucp_res.data[ucp_res.data > 0])) == 0.42892075
# Make sure no nans are present.
np.sum(np.isnan(ucp_res.data)) == 0

Expand All @@ -503,7 +503,8 @@ def test_hgt_resampler_output_values(info_mock):
{
'src_file': 'sample_data/lcz_zaragoza.tif',
'src_file_clean': 'testing/lcz_zaragoza_clean.tif',
'dst_gridinfo': 'testing/geo_em.d04_gridinfo.tif',
'dst_file': 'sample_data/geo_em.d04.nc',
'dst_nu_file': 'testing/geo_em.d04_NoUrban.nc',
'BUILT_LCZ': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
)
Expand All @@ -520,8 +521,8 @@ def test_hgt_resampler_output_values(info_mock):
assert ucp_res.shape == (1, 102, 162)

# Check # max values and non-0 domain average
assert np.sum(ucp_res.data == ucp_res.data.max()) == 9
assert approx(np.mean(ucp_res.data[ucp_res.data > 0])) == 6.7111716
assert np.sum(ucp_res.data == ucp_res.data.max()) == 10
assert approx(np.mean(ucp_res.data[ucp_res.data > 0])) == 6.7109852

# Make sure no nans are present.
assert np.sum(np.isnan(ucp_res.data)) == 0
Expand Down Expand Up @@ -651,7 +652,8 @@ def test_hi_resampler(info_mock):
info = info_mock(
{
'src_file_clean': 'testing/lcz_zaragoza_clean.tif',
'dst_gridinfo': 'testing/geo_em.d04_gridinfo.tif',
'dst_file': 'sample_data/geo_em.d04.nc',
'dst_nu_file': 'testing/geo_em.d04_NoUrban.nc',
'BUILT_LCZ': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
)
Expand Down Expand Up @@ -684,15 +686,13 @@ def test_hi_resampler(info_mock):
(
(
False,
np.array(
[32.0, 33.0, 35.0, 36.0, 38.0, 39.0, 41.0, 42.0, 43.0, 44.0, 46.0]
),
np.array([17, 3, 1, 77, 95, 2, 7, 5, 2, 150, 10]),
np.array([32.0, 35.0, 36.0, 38.0, 41.0, 42.0, 43.0, 44.0, 46.0]),
np.array([14, 1, 23, 35, 9, 6, 5, 140, 22]),
),
(
True,
np.array([32.0, 33.0, 35.0, 36.0, 38.0, 39.0, 41.0]),
np.array([18, 30, 1, 171, 136, 4, 9]),
np.array([32.0, 33.0, 35.0, 36.0, 38.0, 41.0, np.nan]),
np.array([14, 1, 1, 60, 44, 19, 116]),
),
),
)
Expand All @@ -706,7 +706,8 @@ def test_lcz_resampler_lcz_nat_mask_on_off_with_lcz15(
info = info_mock(
{
'src_file_clean': 'testing/lcz_zaragoza_clean.tif',
'dst_gridinfo': 'testing/geo_em.d04_gridinfo.tif',
'dst_file': 'sample_data/geo_em.d04.nc',
'dst_nu_file': 'testing/geo_em.d04_NoUrban.nc',
'BUILT_LCZ': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15],
}
)
Expand All @@ -722,7 +723,9 @@ def test_lcz_resampler_lcz_nat_mask_on_off_with_lcz15(
# With natural masking off, majority filtering also includes
# Natural classes
lcz_values_def, lcz_counts_def = np.unique(lcz_resampled, return_counts=True)
assert (lcz_values_def == lcz_values).all()
assert (
lcz_values_def[~np.isnan(lcz_values_def)] == lcz_values[~np.isnan(lcz_values)]
).all()
assert (lcz_counts_def == lcz_counts).all()


Expand All @@ -733,7 +736,6 @@ def test_add_frc_lu_index_2_wrf(info_mock):
'src_file_clean': 'testing/lcz_zaragoza_clean.tif',
'dst_file': 'sample_data/geo_em.d04.nc',
'dst_nu_file': 'testing/geo_em.d04_NoUrban.nc',
'dst_gridinfo': 'testing/geo_em.d04_gridinfo.tif',
'BUILT_LCZ': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
)
Expand Down Expand Up @@ -817,15 +819,13 @@ def test_create_lcz_params_file_attrs_type(
input_dir = tmpdir.mkdir('input')
shutil.copy(os.path.join('sample_data', 'geo_em.d04.nc'), input_dir)
shutil.copy(os.path.join('testing', 'lcz_zaragoza_clean.tif'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_gridinfo.tif'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_NoUrban.nc'), input_dir)

info = info_mock(
{
'src_file_clean': os.path.join(input_dir, 'lcz_zaragoza_clean.tif'),
'dst_file': os.path.join(input_dir, 'geo_em.d04.nc'),
'dst_nu_file': os.path.join(input_dir, 'geo_em.d04_NoUrban.nc'),
'dst_gridinfo': os.path.join(input_dir, 'geo_em.d04_gridinfo.tif'),
'dst_lcz_params_file': os.path.join(input_dir, 'geo_em.d04_LCZ_params.tif'),
'BUILT_LCZ': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
Expand Down Expand Up @@ -864,7 +864,6 @@ def test_create_lcz_extent_file(tmpdir, info_mock):
'dst_lcz_extent_file': os.path.join(tmpdir, 'geo_em.d04_LCZ_extent.nc'),
'dst_file': 'sample_data/geo_em.d04.nc',
'dst_nu_file': 'testing/geo_em.d04_NoUrban.nc',
'dst_gridinfo': 'testing/geo_em.d04_gridinfo.tif',
'BUILT_LCZ': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
)
Expand Down Expand Up @@ -897,7 +896,7 @@ def test_create_lcz_extent_file(tmpdir, info_mock):
assert 'URB_PARAM' not in list(dst_extent.data_vars)

# Number of urban pixels within LANDUSEF[12]
assert np.sum(dst_extent['LANDUSEF'].data[0, 12, :, :] == 1) == 369
assert np.sum(dst_extent['LANDUSEF'].data[0, 12, :, :] == 1) == 255


@pytest.mark.parametrize(
Expand Down Expand Up @@ -1009,7 +1008,6 @@ def test_checks_and_cleaning_sample_data_all_ok(capsys, tmpdir, info_mock):
input_dir = tmpdir.mkdir('input')
shutil.copy(os.path.join('sample_data', 'geo_em.d04.nc'), input_dir)
shutil.copy(os.path.join('testing', 'lcz_zaragoza_clean.tif'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_gridinfo.tif'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_NoUrban.nc'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_LCZ_extent.nc'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_LCZ_params.nc'), input_dir)
Expand All @@ -1019,7 +1017,6 @@ def test_checks_and_cleaning_sample_data_all_ok(capsys, tmpdir, info_mock):
'src_file_clean': os.path.join(input_dir, 'lcz_zaragoza_clean.tif'),
'dst_file': os.path.join(input_dir, 'geo_em.d04.nc'),
'dst_nu_file': os.path.join(input_dir, 'geo_em.d04_NoUrban.nc'),
'dst_gridinfo': os.path.join(input_dir, 'geo_em.d04_gridinfo.tif'),
'dst_lcz_extent_file': os.path.join(input_dir, 'geo_em.d04_LCZ_extent.nc'),
'dst_lcz_params_file': os.path.join(input_dir, 'geo_em.d04_LCZ_params.nc'),
'BUILT_LCZ': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
Expand Down Expand Up @@ -1114,7 +1111,6 @@ def test_checks_and_cleaning_sample_data_check1to5_wrong(
input_dir = tmpdir.mkdir('input')
shutil.copy(os.path.join('sample_data', 'geo_em.d04.nc'), input_dir)
shutil.copy(os.path.join('testing', 'lcz_zaragoza_clean.tif'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_gridinfo.tif'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_NoUrban_dummy.nc'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_LCZ_extent_dummy.nc'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_LCZ_params_dummy.nc'), input_dir)
Expand All @@ -1124,7 +1120,6 @@ def test_checks_and_cleaning_sample_data_check1to5_wrong(
'src_file_clean': os.path.join(input_dir, 'lcz_zaragoza_clean.tif'),
'dst_file': os.path.join(input_dir, 'geo_em.d04.nc'),
'dst_nu_file': os.path.join(input_dir, 'geo_em.d04_NoUrban_dummy.nc'),
'dst_gridinfo': os.path.join(input_dir, 'geo_em.d04_gridinfo.tif'),
'dst_lcz_extent_file': os.path.join(
input_dir, 'geo_em.d04_LCZ_extent_dummy.nc'
),
Expand Down Expand Up @@ -1167,7 +1162,6 @@ def test_checks_and_cleaning_sample_data_check6to9_wrong(
input_dir = tmpdir.mkdir('input')
shutil.copy(os.path.join('sample_data', 'geo_em.d04.nc'), input_dir)
shutil.copy(os.path.join('testing', 'lcz_zaragoza_clean.tif'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_gridinfo.tif'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_NoUrban_dummy.nc'), input_dir)
shutil.copy(os.path.join('testing', 'geo_em.d04_LCZ_extent_dummy.nc'), input_dir)
shutil.copy(
Expand All @@ -1179,7 +1173,6 @@ def test_checks_and_cleaning_sample_data_check6to9_wrong(
'src_file_clean': os.path.join(input_dir, 'lcz_zaragoza_clean.tif'),
'dst_file': os.path.join(input_dir, 'geo_em.d04.nc'),
'dst_nu_file': os.path.join(input_dir, 'geo_em.d04_NoUrban_dummy.nc'),
'dst_gridinfo': os.path.join(input_dir, 'geo_em.d04_gridinfo.tif'),
'dst_lcz_extent_file': os.path.join(
input_dir, 'geo_em.d04_LCZ_extent_dummy.nc'
),
Expand Down
Loading