1+ import collections
12import os
23import struct
34from pathlib import Path
5+ from typing import Any
46
57import dask
68import numpy as np
@@ -74,9 +76,8 @@ def _open_hds_dis(
7476 )
7577 filesize = os .path .getsize (path )
7678 ntime = filesize // (nlayer * (52 + (nrow * ncol * 8 )))
77- times = read_times (path , ntime , nlayer , nrow , ncol )
78- coords = grid ["coords" ] # TODO: Fix this bit about times
79- coords ["time" ] = times
79+ coords = get_coords (grid )
80+ coords ["time" ] = read_times (path , ntime , nlayer , nrow , ncol )
8081
8182 dask_list = []
8283 # loop over times and add delayed arrays
@@ -102,6 +103,34 @@ def _open_hds_dis(
102103 return data_array
103104
104105
106+ def get_coords (grid : StructuredGrid ) -> dict [str , Any ]:
107+ # unpack tuples
108+ xmin , xmax , ymin , ymax = grid .extent
109+ dx , dy = (grid .delr , - grid .delc )
110+ coords : collections .OrderedDict [str , Any ] = collections .OrderedDict ()
111+ # from cell size to x and y coordinates
112+ if isinstance (dx , (int , float , np .int_ )): # equidistant
113+ coords ["x" ] = np .arange (xmin + dx / 2.0 , xmax , dx )
114+ coords ["y" ] = np .arange (ymax + dy / 2.0 , ymin , dy )
115+ coords ["dx" ] = np .array (float (dx ))
116+ coords ["dy" ] = np .array (float (dy ))
117+ else : # nonequidistant
118+ # even though IDF may store them as float32,
119+ # we always convert them to float64
120+ dx = dx .astype (np .float64 )
121+ dy = dy .astype (np .float64 )
122+ coords ["x" ] = xmin + np .cumsum (dx ) - 0.5 * dx
123+ coords ["y" ] = ymax + np .cumsum (dy ) - 0.5 * dy
124+ if np .allclose (dx , dx [0 ]) and np .allclose (dy , dy [0 ]):
125+ coords ["dx" ] = np .array (float (dx [0 ]))
126+ coords ["dy" ] = np .array (float (dy [0 ]))
127+ else :
128+ coords ["dx" ] = ("x" , dx )
129+ coords ["dy" ] = ("y" , dy )
130+ coords ["layer" ] = np .arange (1 , grid .nlay + 1 )
131+ return coords
132+
133+
105134def read_times (
106135 path : Path , ntime : int , nlayer : int , nrow : int , ncol : int
107136) -> np .ndarray :
0 commit comments