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

Flip MeteoBase data #185

Merged
merged 5 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 15 additions & 10 deletions nlmod/read/meteobase.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import numpy as np
from pandas import Timestamp
import rioxarray # noqa # pylint: disable=unused-import
from xarray import DataArray


Expand Down Expand Up @@ -150,7 +149,6 @@ def get_xy_from_ascii_meta(
Tuple[np.ndarray, np.ndarray]
Tuple with the the x and y coordinates as numpy array
"""

if "xllcorner" in meta.keys():
xstart = meta["xllcorner"] + meta["cellsize"] / 2
elif "xllcenter" in meta.keys():
Expand All @@ -168,17 +166,19 @@ def get_xy_from_ascii_meta(
elif "yllcenter" in meta.keys():
ystart = meta["yllcenter"]

y = np.linspace(
ystart,
ystart + meta["cellsize"] * meta["nrows"],
meta["nrows"],
endpoint=False,
y = np.flip(
np.linspace(
ystart,
ystart + meta["cellsize"] * meta["nrows"],
meta["nrows"],
endpoint=True,
)
)
return x, y


def read_meteobase_ascii(
zfile: ZipFile, foldername: str, meta: Dict[str, str], replace_na: bool = False
zfile: ZipFile, foldername: str, meta: Dict[str, str], replace_na: bool = True
) -> DataArray:
"""Read list of .asc files in a meteobase zipfile.

Expand All @@ -204,10 +204,11 @@ def read_meteobase_ascii(
]
if meta["Bestandsformaat"] == ".ASC (Arc/Info-raster)":
times = []
data_array = None
for i, fname in enumerate(fnames):
data_array = None
with zfile.open(fname) as fo:
data, ascii_meta = read_ascii(fo)

if data_array is None:
meta.update(ascii_meta)
data_array = np.zeros(
Expand All @@ -223,6 +224,7 @@ def read_meteobase_ascii(

if "nodata_value" in meta.keys() and replace_na:
data_array[data_array == meta["nodata_value"]] = np.nan
meta["nodata_value"] = str(np.nan)

x, y = get_xy_from_ascii_meta(ascii_meta)

Expand All @@ -247,7 +249,7 @@ def read_meteobase_ascii(
def read_meteobase(
path: Union[Path, str],
meteobase_type: Optional[str] = None,
replace_na: bool = False,
replace_na: bool = True,
) -> List[DataArray]:
"""Read Meteobase zipfile with ASCII data.

Expand Down Expand Up @@ -284,7 +286,10 @@ def read_meteobase(
meta[mb_type.upper()]["Projectie"]
== "RD new (Amersfoort, rijksdriehoekstelsel)"
):
import rioxarray # noqa # pylint: disable=unused-import

da.rio.write_crs("EPSG:28992", inplace=True)

da_list.append(da)

return da_list
3 changes: 2 additions & 1 deletion tests/test_017_metbase.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import nlmod
from pathlib import Path

import nlmod

data_path = Path(__file__).parent / "data"


Expand Down
1 change: 0 additions & 1 deletion tests/test_018_knmi_data_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from nlmod.read import knmi_data_platform


data_path = Path(__file__).parent / "data"


Expand Down