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

WIP Support leakage sqlite input #126

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
Empty file.
68 changes: 68 additions & 0 deletions threedi_modelchecker/simulation_templates/leakage/netcdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from osgeo import gdal
import h5py
import numpy as np


HUNDERD_YEARS_IN_SECONDS = 100 * 365 * 24 * 60 * 60


def get_epsg(sr):
"""Return EPSG:<code> from a SpatialReference
"""
key = "GEOGCS" if sr.IsGeographic() else "PROJCS"
name = sr.GetAuthorityName(key)
if name != "EPSG":
return
code = sr.GetAuthorityCode(key)
return f"EPSG:{code}"


def geotiff_to_netcdf(path, out):
"""Convert a leakage GeoTIFF to a NetCDF (according to FEWS 2017.02)

This function writes the minimum amount of fields to be used with
the 3Di API
"""
src = gdal.Open(path)
width = src.RasterXSize
height = src.RasterYSize
xoff, dx, _, yoff, _, dy = src.GetGeoTransform()
assert xoff > 0
if dy < 0:
flip_y = True
dy = -dy
yoff -= (height * dy)
else:
flip_y = False
band = src.GetRasterBand(1)

with h5py.File(out, "w") as f:
f.attrs["title"] = b"leakage"
f.attrs["institution"] = b"Nelen & Schuurmans"
f.attrs["source"] = b"Converted from leakage GeoTIFF file"
f.attrs["fews_implementation_version"] = b"2017.02"

crs = f.create_dataset("crs", shape=(), dtype=np.int32)
crs.attrs["crs_wkt"] = src.GetProjection().encode()
crs.attrs["epsg_code"] = get_epsg(src.GetSpatialRef()).encode()

x = f.create_dataset("x", data=np.arange(xoff, xoff + dx * width, dx))
y = f.create_dataset("y", data=np.arange(yoff, yoff + dy * height, dy))
t = f.create_dataset("time", data=np.array([HUNDERD_YEARS_IN_SECONDS], dtype=float))
t.attrs["units"] = b"seconds since 1970-01-01 00:00:00.0 +0000'"

data = band.ReadAsArray()
data /= 24.0 # leakage geotiff is in mm/day, convert to mm/hr
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to mask the fillvalue here

if flip_y:
data = data[::-1]
values = f.create_dataset("values", shape=(2, height, width), fillvalue=0.0, dtype=data.dtype)
values[0] = data
values.attrs["units"] = b"mm/hr"
values.attrs["_FillValue"] = np.array([band.GetNoDataValue()], dtype=data.dtype)

x.make_scale("x")
y.make_scale("y")
t.make_scale("t")
values.dims[0].attach_scale(t)
values.dims[1].attach_scale(y)
values.dims[2].attach_scale(x)