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

Ignore x0 y0 in dfs2 #544

Merged
merged 22 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b2df9ae
only read x0, y0 if file is spectral
jsmariegaard Jan 30, 2023
a72bf76
dont read x0, y0, z0
jsmariegaard Jan 30, 2023
88a784d
temporary disable tests with deprecated functionality
jsmariegaard Jan 30, 2023
0b5cf00
_cart property and improved isel
jsmariegaard Feb 14, 2023
42bb270
Create BW_Ronne_Layout1998_rotated_crop.dfs2
jsmariegaard Feb 14, 2023
ed2e890
test_select_area_rotated_UTM_2, improved behavior of cropped rotated …
jsmariegaard Feb 14, 2023
a455d7b
comment
jsmariegaard Feb 14, 2023
cf20709
only use self._cart.Xy2Proj on rotated grids
jsmariegaard Feb 14, 2023
e2c84cf
test_MIKE_SHE_output with x0, y0 not zero
jsmariegaard Feb 14, 2023
bdc6909
refactor dfs2.__init__
jsmariegaard Feb 14, 2023
dc2ce16
rename self._source
jsmariegaard Feb 14, 2023
0f61f5b
minor
jsmariegaard Feb 14, 2023
8e6fca1
move _validate_no_orientation_in_geo and _origin_and_orientation_in_C…
jsmariegaard Feb 14, 2023
6eb1c23
dfs3: handle origo and orientation in same way as dfs2
jsmariegaard Feb 14, 2023
cfbb76b
__eq__ for all grids; Grid3D: x, y, improved isel and_index_to_Grid3D…
jsmariegaard Feb 14, 2023
963b22d
new test and updated test_read_rotated_grid
jsmariegaard Feb 14, 2023
ceee6c7
fix dfs3 origin shift for non-rotated
jsmariegaard Feb 14, 2023
5c975fc
Merge branch 'main' into ignore-x0-y0-in-grid2d
ecomodeller Feb 14, 2023
ae8357d
Type hints and docstrings
ecomodeller Feb 14, 2023
e52b9c9
Northing values needs to be rounded with 6 decimals...
jsmariegaard Feb 15, 2023
60efd10
remove __eq__ but convert origin to tuple (and check that it has leng…
jsmariegaard Feb 15, 2023
573147c
Update Dfs2 - Various types.ipynb
jsmariegaard Feb 15, 2023
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
27 changes: 25 additions & 2 deletions mikeio/dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from abc import abstractmethod
from datetime import datetime, timedelta
from typing import Iterable, List, Optional, Tuple, Union

import numpy as np
import pandas as pd
from tqdm import tqdm, trange

from copy import deepcopy
from mikecore.DfsFactory import DfsFactory
from mikecore.DfsFile import (
Expand All @@ -16,7 +17,7 @@
)
from mikecore.DfsFileFactory import DfsFileFactory
from mikecore.eum import eumQuantity
from tqdm import tqdm, trange
from mikecore.Projections import Cartography

from .base import TimeSeries
from .dataset import Dataset
Expand Down Expand Up @@ -751,3 +752,25 @@ def orientation(self):
def dx(self):
"""Step size in x direction"""
pass

def _validate_no_orientation_in_geo(self):
if self.is_geo and abs(self._orientation) > 1e-6:
raise ValueError("Orientation is not supported for LONG/LAT coordinates")

def _origin_and_orientation_in_CRS(self):
"""Project origin and orientation to projected CRS (if not LONG/LAT)"""
if self.is_geo:
origin = self._longitude, self._latitude
orientation = 0.0
else:
lon, lat = self._longitude, self._latitude
cart = Cartography.CreateGeoOrigin(
projectionString=self._projstr,
lonOrigin=lon,
latOrigin=lat,
orientation=self._orientation,
)
# convert origin and orientation to projected CRS
origin = tuple(np.round(cart.Geo2Proj(lon, lat), 6))
orientation = cart.OrientationProj
return origin, orientation
84 changes: 29 additions & 55 deletions mikeio/dfs2.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os
from copy import deepcopy

import numpy as np
import pandas as pd
from tqdm import tqdm

from mikecore.DfsFactory import DfsBuilder, DfsFactory
from mikecore.DfsFile import DfsFile, DfsSimpleType
from mikecore.DfsFileFactory import DfsFileFactory
from mikecore.eum import eumQuantity, eumUnit
from mikecore.Projections import Cartography
from tqdm import tqdm

from . import __dfs_version__
from .dataset import Dataset
Expand Down Expand Up @@ -44,14 +44,14 @@ def _write_dfs2_header(filename, ds: Dataset, title="") -> DfsFile:

factory = DfsFactory()
_write_dfs2_spatial_axis(builder, factory, geometry)
proj = geometry.projection_string
proj_str = geometry.projection_string
origin = geometry.origin
orient = geometry.orientation

if geometry.is_geo:
proj = factory.CreateProjectionGeoOrigin(proj, *origin, orient)
proj = factory.CreateProjectionGeoOrigin(proj_str, *origin, orient)
else:
cart: Cartography = Cartography.CreateProjOrigin(proj, *origin, orient)
cart: Cartography = Cartography.CreateProjOrigin(proj_str, *origin, orient)
proj = factory.CreateProjectionGeoOrigin(
wktProjectionString=geometry.projection,
lon0=cart.LonOrigin,
Expand Down Expand Up @@ -105,62 +105,35 @@ class Dfs2(_Dfs123):

_ndim = 2

def __init__(self, filename=None, type="horizontal"):
def __init__(self, filename=None, type: str = "horizontal"):
super().__init__(filename)

self._dx = None
self._dy = None
self._nx = None
self._ny = None
self._x0 = 0
self._y0 = 0
self._x0 = 0.0
self._y0 = 0.0
self.geometry = None

if filename:
self._read_dfs2_header()
is_spectral = type == "spectral"

if self._projstr == "LONG/LAT":
if np.abs(self._orientation) < 1e-6:
origin = self._longitude, self._latitude
self.geometry = Grid2D(
dx=self._dx,
dy=self._dy,
nx=self._nx,
ny=self._ny,
x0=self._x0,
y0=self._y0,
origin=origin,
projection=self._projstr,
is_spectral=is_spectral,
)
else:
raise ValueError(
"LONG/LAT with non-zero orientation is not supported"
)
else:
lon, lat = self._longitude, self._latitude
cart = Cartography.CreateGeoOrigin(
projectionString=self._projstr,
lonOrigin=self._longitude,
latOrigin=self._latitude,
orientation=self._orientation,
)
origin_projected = np.round(cart.Geo2Proj(lon, lat), 7)
orientation_projected = cart.OrientationProj

self.geometry = Grid2D(
dx=self._dx,
dy=self._dy,
nx=self._nx,
ny=self._ny,
x0=self._x0,
y0=self._y0,
orientation=orientation_projected,
origin=origin_projected,
projection=self._projstr,
is_spectral=is_spectral,
)
is_spectral = type.lower() in ["spectral", "spectra", "spectrum"]
self._read_dfs2_header(read_x0y0=is_spectral)
self._validate_no_orientation_in_geo()
origin, orientation = self._origin_and_orientation_in_CRS()

self.geometry = Grid2D(
dx=self._dx,
dy=self._dy,
nx=self._nx,
ny=self._ny,
x0=self._x0,
y0=self._y0,
orientation=orientation,
origin=origin,
projection=self._projstr,
is_spectral=is_spectral,
)

def __repr__(self):
out = ["<mikeio.Dfs2>"]
Expand All @@ -185,14 +158,15 @@ def __repr__(self):

return str.join("\n", out)

def _read_dfs2_header(self):
def _read_dfs2_header(self, read_x0y0: bool = False):
if not os.path.isfile(self._filename):
raise Exception(f"file {self._filename} does not exist!")

self._dfs = DfsFileFactory.Dfs2FileOpen(self._filename)
self._source = self._dfs
self._x0 = self._dfs.SpatialAxis.X0
self._y0 = self._dfs.SpatialAxis.Y0
if read_x0y0:
self._x0 = self._dfs.SpatialAxis.X0
self._y0 = self._dfs.SpatialAxis.Y0
self._dx = self._dfs.SpatialAxis.Dx
self._dy = self._dfs.SpatialAxis.Dy
self._nx = self._dfs.SpatialAxis.XCount
Expand Down
64 changes: 38 additions & 26 deletions mikeio/dfs3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import numpy as np
import pandas as pd

from mikecore.DfsBuilder import DfsBuilder
from mikecore.DfsFactory import DfsFactory
from mikecore.DfsFile import DfsFile, DfsSimpleType
from mikecore.DfsFileFactory import DfsFileFactory
from mikecore.eum import eumQuantity, eumUnit
from mikecore.Projections import Cartography

from . import __dfs_version__
from .dataset import Dataset
Expand Down Expand Up @@ -34,23 +36,23 @@ def _write_dfs3_header(filename, ds: Dataset, title="") -> DfsFile:

factory = DfsFactory()
_write_dfs3_spatial_axis(builder, factory, geometry)
origin = geometry._origin # Origin in geographical coordinates
orient = geometry._orientation
origin = geometry.origin # Origin in geographical coordinates
orient = geometry.orientation

# if geometry.is_geo:
proj = factory.CreateProjectionGeoOrigin(
geometry.projection_string, *origin, orient
)
# else:
# cart: Cartography = Cartography.CreateProjOrigin(
# geometry.projection_string, *origin, orient
# )
# proj = factory.CreateProjectionGeoOrigin(
# wktProjectionString=geometry.projection,
# lon0=cart.LonOrigin,
# lat0=cart.LatOrigin,
# orientation=cart.Orientation,
# )
if geometry.is_geo:
proj = factory.CreateProjectionGeoOrigin(
geometry.projection_string, *origin, orient
)
else:
cart: Cartography = Cartography.CreateProjOrigin(
geometry.projection_string, *origin, orient
)
proj = factory.CreateProjectionGeoOrigin(
wktProjectionString=geometry.projection,
lon0=cart.LonOrigin,
lat0=cart.LatOrigin,
orientation=cart.Orientation,
)

builder.SetGeographicalProjection(proj)

Expand Down Expand Up @@ -110,13 +112,16 @@ def __init__(self, filename=None):
self._nx = None
self._ny = None
self._nz = None
self._x0 = 0
self._y0 = 0
self._z0 = 0
self._x0 = 0.0
self._y0 = 0.0
self._z0 = 0.0
self.geometry = None

if filename:
self._read_dfs3_header()
self._validate_no_orientation_in_geo()
origin, orientation = self._origin_and_orientation_in_CRS()

self.geometry = Grid3D(
x0=self._x0,
dx=self._dx,
Expand All @@ -127,9 +132,9 @@ def __init__(self, filename=None):
z0=self._z0,
dz=self._dz,
nz=self._nz,
origin=(self._longitude, self._latitude),
origin=origin,
projection=self._projstr,
orientation=self._orientation,
orientation=orientation,
)

def __repr__(self):
Expand All @@ -154,17 +159,19 @@ def __repr__(self):

return str.join("\n", out)

def _read_dfs3_header(self):
def _read_dfs3_header(self, read_x0y0z0: bool = False):
if not os.path.isfile(self._filename):
raise Exception(f"file {self._filename} does not exist!")

self._dfs = DfsFileFactory.Dfs3FileOpen(self._filename)

self.source = self._dfs
self._source = self._dfs

if read_x0y0z0:
self._x0 = self._dfs.SpatialAxis.X0
self._y0 = self._dfs.SpatialAxis.Y0
self._z0 = self._dfs.SpatialAxis.Z0

self._x0 = self._dfs.SpatialAxis.X0
self._y0 = self._dfs.SpatialAxis.Y0
self._z0 = self._dfs.SpatialAxis.Z0
self._dx = self._dfs.SpatialAxis.Dx
self._dy = self._dfs.SpatialAxis.Dy
self._dz = self._dfs.SpatialAxis.Dz
Expand Down Expand Up @@ -406,3 +413,8 @@ def dz(self):
@property
def shape(self):
return (self._n_timesteps, self._nz, self._ny, self._nx)

@property
def is_geo(self):
"""Are coordinates geographical (LONG/LAT)?"""
return self._projstr == "LONG/LAT"
Loading