windmapper 2.2.3__tar.gz → 2.2.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. windmapper-2.2.5/.gitignore +21 -0
  2. windmapper-2.2.5/MANIFEST.in +10 -0
  3. {windmapper-2.2.3 → windmapper-2.2.5}/PKG-INFO +17 -3
  4. {windmapper-2.2.3 → windmapper-2.2.5}/README.md +15 -1
  5. windmapper-2.2.5/implementation/implimentation.ipynb +308 -0
  6. windmapper-2.2.5/implementation/spack.yaml +20 -0
  7. windmapper-2.2.5/implementation/windmapper_config.py +22 -0
  8. {windmapper-2.2.3 → windmapper-2.2.5}/publish.sh +5 -5
  9. {windmapper-2.2.3 → windmapper-2.2.5}/pyproject.toml +2 -2
  10. windmapper-2.2.5/spack-deploy.yaml +6 -0
  11. windmapper-2.2.5/src/windmapper/dem.py +115 -0
  12. {windmapper-2.2.3 → windmapper-2.2.5}/src/windmapper/windmapper_cli.py +63 -187
  13. {windmapper-2.2.3 → windmapper-2.2.5}/src/windmapper/windmapper_utls/MPI_call_WN_1dir.py +1 -1
  14. windmapper-2.2.5/tests/test_dem.py +106 -0
  15. windmapper-2.2.3/.gitignore +0 -26
  16. windmapper-2.2.3/proj.ipynb +0 -49
  17. windmapper-2.2.3/spack-deploy.yaml +0 -6
  18. windmapper-2.2.3/spack.yaml +0 -3
  19. windmapper-2.2.3/write_WN_config.py +0 -55
  20. {windmapper-2.2.3 → windmapper-2.2.5}/.github/workflows/main.yml +0 -0
  21. {windmapper-2.2.3 → windmapper-2.2.5}/.readthedocs.yml +0 -0
  22. {windmapper-2.2.3 → windmapper-2.2.5}/CMakeLists.txt +0 -0
  23. {windmapper-2.2.3 → windmapper-2.2.5}/LICENSE +0 -0
  24. {windmapper-2.2.3 → windmapper-2.2.5}/build.rst +0 -0
  25. {windmapper-2.2.3 → windmapper-2.2.5}/cfg/cli_massSolver.cfg +0 -0
  26. {windmapper-2.2.3 → windmapper-2.2.5}/src/windmapper/__init__.py +0 -0
  27. {windmapper-2.2.3 → windmapper-2.2.5}/src/windmapper/windmapper_utls/__init__.py +0 -0
  28. {windmapper-2.2.3 → windmapper-2.2.5}/src/windmapper/windmapper_utls/rio_merge.py +0 -0
@@ -0,0 +1,21 @@
1
+ __pycache__
2
+ *.tif
3
+ *.vrt
4
+ *.asc
5
+ *.prj
6
+ WindNinja_cli
7
+ .python-version
8
+ .idea/
9
+ docs/_build/
10
+ build/
11
+ MANIFEST
12
+ dist/
13
+ *.egg-info
14
+ _skbuild/
15
+ /spack-deploy/*
16
+ /dist/*
17
+ /implementation/._view
18
+ /implementation/.spack-env
19
+ /implementation/view
20
+ /implementation/windmapper_config
21
+ /implementation/spack.lock
@@ -0,0 +1,10 @@
1
+ graft pysrc
2
+ graft scripts
3
+
4
+ include .gitignore
5
+ include CMakeLists.txt
6
+ include LICENSE
7
+ include README.md
8
+ include windmapper.py
9
+ include pyproject.toml
10
+ include setup.py
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: windmapper
3
- Version: 2.2.3
3
+ Version: 2.2.5
4
4
  Summary: Windfield library generation
5
5
  Author: Chris Marsh
6
6
  Project-URL: Homepage, https://github.com/Chrismarsh/Windmapper
@@ -20,10 +20,24 @@ Description-Content-Type: text/markdown
20
20
 
21
21
  Windmapper is a python tool and a set of algorithms for producing and using pre-computed libraries of wind field used for wind downscaling.
22
22
 
23
+ ## Paper
24
+ Marsh, C. B., Vionnet, V. & Pomeroy, J. W. Windmapper: An Efficient Wind Downscaling Method for Hydrological Models. [Water Resour Res 59, (2023).](https://agupubs.onlinelibrary.wiley.com/doi/10.1029/2022WR032683)
25
+
26
+
23
27
  ![](docs/images/WM-main.png)
24
28
 
25
29
  # Installation
26
30
 
31
+ Because Windmapper depends on WindNinja, it is easiest to use [Spack](https://spack.readthedocs.io/) to install Windmapper:
32
+
33
+ ```
34
+ spack repo add https://github.com/Chrismarsh/spack-repo.git
35
+ spack install py-windmapper
36
+ ```
37
+
38
+ ## Pip
39
+ This is a harder way to get Windmapper.
40
+
27
41
  The default ``pip install windmapper`` will build windmapper without Windninja. However Windninja is required to run.
28
42
 
29
43
  To build Windninja, ensure gdal, boost, and mpi system libraries are installed and then
@@ -38,4 +52,4 @@ Documentation and full instructions on use can be found [here](https://windmappe
38
52
  # Implimentation
39
53
 
40
54
  The directory `implementation/` contains a jupyter notebook with an example implimentation of the code. It is not
41
- vectorized to aid in readability. However, this makes it very very slow!
55
+ vectorized to aid in readability. However, this makes it very, very slow!
@@ -2,10 +2,24 @@
2
2
 
3
3
  Windmapper is a python tool and a set of algorithms for producing and using pre-computed libraries of wind field used for wind downscaling.
4
4
 
5
+ ## Paper
6
+ Marsh, C. B., Vionnet, V. & Pomeroy, J. W. Windmapper: An Efficient Wind Downscaling Method for Hydrological Models. [Water Resour Res 59, (2023).](https://agupubs.onlinelibrary.wiley.com/doi/10.1029/2022WR032683)
7
+
8
+
5
9
  ![](docs/images/WM-main.png)
6
10
 
7
11
  # Installation
8
12
 
13
+ Because Windmapper depends on WindNinja, it is easiest to use [Spack](https://spack.readthedocs.io/) to install Windmapper:
14
+
15
+ ```
16
+ spack repo add https://github.com/Chrismarsh/spack-repo.git
17
+ spack install py-windmapper
18
+ ```
19
+
20
+ ## Pip
21
+ This is a harder way to get Windmapper.
22
+
9
23
  The default ``pip install windmapper`` will build windmapper without Windninja. However Windninja is required to run.
10
24
 
11
25
  To build Windninja, ensure gdal, boost, and mpi system libraries are installed and then
@@ -20,4 +34,4 @@ Documentation and full instructions on use can be found [here](https://windmappe
20
34
  # Implimentation
21
35
 
22
36
  The directory `implementation/` contains a jupyter notebook with an example implimentation of the code. It is not
23
- vectorized to aid in readability. However, this makes it very very slow!
37
+ vectorized to aid in readability. However, this makes it very, very slow!
@@ -0,0 +1,308 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import xarray as xr\n",
10
+ "import rioxarray as rio\n",
11
+ "import glob\n",
12
+ "from pathlib import Path\n",
13
+ "import natsort\n",
14
+ "import subprocess\n",
15
+ "import numpy as np\n"
16
+ ]
17
+ },
18
+ {
19
+ "cell_type": "code",
20
+ "execution_count": null,
21
+ "metadata": {},
22
+ "outputs": [],
23
+ "source": [
24
+ "# get all the speedup \n",
25
+ "files = natsort.natsorted(glob.glob('windmapper_config/*.tif'))\n",
26
+ "\n",
27
+ "# the shortest name is the the base DEM. Remove this basename from the variable name used in the dataset\n",
28
+ "basename = Path(min(files, key=len)).stem\n",
29
+ "\n",
30
+ "variables = []\n",
31
+ "dem = None\n",
32
+ "for tif_file in files:\n",
33
+ " # Use the file name (without extension) as the variable name\n",
34
+ " variable_name = Path(tif_file).stem\n",
35
+ "\n",
36
+ " if variable_name == basename:\n",
37
+ "\n",
38
+ " # The WM area is smaller than the input DEM due to the strict alignment requirements\n",
39
+ " # cut down the input DEM to match the output WM extent\n",
40
+ " variable_name = variable_name.replace(basename,'DEM')\n",
41
+ " shp = Path(tif_file).parent / 'shp' / 'user_bbox.shp'\n",
42
+ " new_dem = 'DEM-WM-clip.tif'\n",
43
+ "\n",
44
+ " command = [\n",
45
+ " \"gdalwarp\", # Replace with actual path to gdalwarp\n",
46
+ " \"-cutline\", shp,\n",
47
+ " \"-crop_to_cutline\",\n",
48
+ " \"-dstalpha\",\n",
49
+ " \"-overwrite\",\n",
50
+ " 'FABDEM-clip.tif',\n",
51
+ " new_dem\n",
52
+ " ]\n",
53
+ " result = subprocess.run(command, capture_output=True, text=True)\n",
54
+ " dem = xr.open_mfdataset(new_dem).isel(band=0).drop_vars('band')\n",
55
+ " dem = dem.rename({'band_data': 'DEM','x':'lon','y':'lat'})\n",
56
+ " continue\n",
57
+ " \n",
58
+ " else:\n",
59
+ " variable_name = variable_name.replace(basename,'dir')\n",
60
+ " \n",
61
+ " ds = xr.open_mfdataset(tif_file).isel(band=0).drop_vars('band')\n",
62
+ " ds = ds.rename({'band_data': variable_name,'x':'lon','y':'lat'})\n",
63
+ " variables.append(ds)\n",
64
+ "\n",
65
+ "# Combine all variables into a single xarray.Dataset\n",
66
+ "# Enforce them to all /exactly/ match the same x,y and compensate for any tiny floating point differences in the lat lon coords \n",
67
+ "ds = xr.merge( xr.align(*variables, join=\"override\"), compat='no_conflicts', join='exact')\n",
68
+ "ds\n"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "execution_count": 3,
74
+ "metadata": {},
75
+ "outputs": [],
76
+ "source": [
77
+ "# Create a new dataset with the same coordinates as the existing dem\n",
78
+ "wind = xr.Dataset(coords=dem.coords)\n",
79
+ "\n",
80
+ "# Add a new variable to the new dataset (optional)\n",
81
+ "wind[\"speed\"] = ((\"lon\", \"lat\"), np.zeros((557,406))+10) # a constant 10m/s wind\n",
82
+ "wind[\"dir\"] = ((\"lon\", \"lat\"), np.zeros((557,406)) + 45) # a 45 degree wind\n",
83
+ "\n",
84
+ "# where the output will go\n",
85
+ "wind[\"u\"] = ((\"lon\", \"lat\"), np.zeros((557,406))) \n",
86
+ "wind[\"v\"] = ((\"lon\", \"lat\"), np.zeros((557,406))) "
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": null,
92
+ "metadata": {},
93
+ "outputs": [],
94
+ "source": [
95
+ "# example usage. The tight loop + lookup is very very slow\n",
96
+ "\n",
97
+ "# this would be run for each timestep of the using model\n",
98
+ "N_windfield = len(ds.data_vars) // 3\n",
99
+ "delta_angle = 360. / N_windfield\n",
100
+ "\n",
101
+ "# Iterate over intput wind field\n",
102
+ "for i in range(wind.sizes['lon']): \n",
103
+ " print(i, wind.sizes['lon'])\n",
104
+ " lon = wind['lon'][i]\n",
105
+ " for j in range(wind.sizes['lat']):\n",
106
+ " \n",
107
+ " lat = wind['lat'][j]\n",
108
+ "\n",
109
+ " # find the nearest point in the windmapper output because the using DEM and WM output don't have to be 1:1 exactly\n",
110
+ " nearest_point = ds.sel(lat=lat, \n",
111
+ " lon=lon, \n",
112
+ " method=\"nearest\")\n",
113
+ " nearest_lat = nearest_point[\"lat\"].values\n",
114
+ " nearest_lon = nearest_point[\"lon\"].values\n",
115
+ "\n",
116
+ " lon_i, lat_j = np.where((ds.lat == nearest_point.lat) & (ds.lon == nearest_point.lon))\n",
117
+ "\n",
118
+ " # get the input wind dir\n",
119
+ " theta = wind.dir[i,j].values.flatten()[0]\n",
120
+ "\n",
121
+ " # compute the two two nearest wind maps to this input direction\n",
122
+ " d1 = int(theta / delta_angle)\n",
123
+ " theta1 = d1 * delta_angle \n",
124
+ " if d1 == 0:\n",
125
+ " d1 = N_windfield\n",
126
+ "\n",
127
+ " d2 = int((theta + delta_angle) / delta_angle)\n",
128
+ " theta2 = d2 * delta_angle \n",
129
+ " if d2 == 0:\n",
130
+ " d2 = N_windfield\n",
131
+ "\n",
132
+ " d1 = int(d1 * delta_angle)\n",
133
+ " d2 = int(d2 * delta_angle)\n",
134
+ "\n",
135
+ " if d1 == 360:\n",
136
+ " d1 = 0\n",
137
+ " if d2 == 360:\n",
138
+ " d2 = 0\n",
139
+ "\n",
140
+ " U_d1 = ds[f'dir_{d1}_U'][lon_i,lat_j].values.flatten()[0]\n",
141
+ " U_d2 = ds[f'dir_{d2}_U'][lon_i,lat_j].values.flatten()[0]\n",
142
+ "\n",
143
+ " V_d1 = ds[f'dir_{d1}_V'][lon_i,lat_j].values.flatten()[0]\n",
144
+ " V_d2 = ds[f'dir_{d2}_V'][lon_i,lat_j].values.flatten()[0]\n",
145
+ "\n",
146
+ " W_d1 = ds[f'dir_{d1}_spd_up_1000'][lon_i,lat_j].values.flatten()[0]\n",
147
+ " W_d2 = ds[f'dir_{d2}_spd_up_1000'][lon_i,lat_j].values.flatten()[0]\n",
148
+ "\n",
149
+ " # interpolate between them\n",
150
+ " U = U_d1*(theta2-theta)/(theta2-theta1)+U_d2*(theta-theta1)/(theta2-theta1)\n",
151
+ " V = V_d1*(theta2-theta)/(theta2-theta1)+V_d2*(theta-theta1)/(theta2-theta1)\n",
152
+ " W_transf = W_d1*(theta2-theta)/(theta2-theta1)+W_d2*(theta-theta1)/(theta2-theta1)\n",
153
+ "\n",
154
+ " # update the direction\n",
155
+ " wind.u[i,j] = U\n",
156
+ " wind.v[i,j] = V\n",
157
+ "\n",
158
+ " # update the speed\n",
159
+ " wind.speed[i,j] = wind.speed[i,j] * W_transf\n",
160
+ "\n",
161
+ " # TODO: fix degree / radian \n",
162
+ " # sign convention \n",
163
+ " # http://mst.nerc.ac.uk/wind_vect_convs.html\n",
164
+ " # new_dir = np.arctan2(-U, -V)\n",
165
+ "\n",
166
+ " # if new_dir < 0:\n",
167
+ " # new_dir = new_dir + 360.\n",
168
+ "\n",
169
+ " # wind.dir[i,j] = new_dir\n"
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": null,
175
+ "metadata": {},
176
+ "outputs": [],
177
+ "source": [
178
+ "# same as previous cell but a bit more pre computed & vectorized\n",
179
+ "# still comically slow\n",
180
+ "\n",
181
+ "# precompute static fields\n",
182
+ "N_windfield = len(ds.data_vars) // 3\n",
183
+ "delta_angle = 360. / N_windfield\n",
184
+ "\n",
185
+ "lon_grid, lat_grid = np.meshgrid(wind['lon'], wind['lat'], indexing='ij')\n",
186
+ "nearest_points = ds.sel(lat=xr.DataArray(lat_grid, dims=[\"lon\", \"lat\"]),\n",
187
+ " lon=xr.DataArray(lon_grid, dims=[\"lon\", \"lat\"]),\n",
188
+ " method=\"nearest\")\n",
189
+ "\n",
190
+ "theta_grid = wind['dir']\n",
191
+ "# Compute angle bins (d1, d2) and their corresponding angles (theta1, theta2)\n",
192
+ "d1 = np.floor(theta_grid / delta_angle).astype(int)\n",
193
+ "d2 = (d1 + 1) % N_windfield\n",
194
+ "theta1_grid = d1 * delta_angle\n",
195
+ "theta2_grid = d2 * delta_angle\n",
196
+ "\n",
197
+ "# Handle edge cases where d1 or d2 wrap around to 0 or 360 degrees\n",
198
+ "d1 = xr.where(d1 == 0, N_windfield, d1)\n",
199
+ "d2 = xr.where(d2 == 0, N_windfield, d2)\n",
200
+ "\n",
201
+ "# Convert angle bins to dataset variable names\n",
202
+ "d1_angles = (d1 * delta_angle).astype(int)\n",
203
+ "d2_angles = (d2 * delta_angle).astype(int)\n",
204
+ "\n",
205
+ "d1_angles = xr.where(d1_angles == 360, 0, d1_angles)\n",
206
+ "d2_angles = xr.where(d2_angles == 0, 0, d2_angles)\n",
207
+ "\n",
208
+ "\n",
209
+ "for i in range(wind.sizes['lon']): # Iterate over the first dimension (x)\n",
210
+ " print(i, wind.sizes['lon'])\n",
211
+ " for j in range(wind.sizes['lat']):\n",
212
+ " \n",
213
+ " U_d1 = nearest_points[f'dir_{d1_angles[i,j].values.flatten()[0]}_U'][i,j].values.flatten()[0]\n",
214
+ " U_d2 = nearest_points[f'dir_{d2_angles[i,j].values.flatten()[0]}_U'][i,j].values.flatten()[0]\n",
215
+ "\n",
216
+ " V_d1 = nearest_points[f'dir_{d1_angles[i,j].values.flatten()[0]}_V'][i,j].values.flatten()[0]\n",
217
+ " V_d2 = nearest_points[f'dir_{d2_angles[i,j].values.flatten()[0]}_V'][i,j].values.flatten()[0]\n",
218
+ "\n",
219
+ " W_d1 = nearest_points[f'dir_{d1_angles[i,j].values.flatten()[0]}_spd_up_1000'][i,j].values.flatten()[0]\n",
220
+ " W_d2 = nearest_points[f'dir_{d2_angles[i,j].values.flatten()[0]}_spd_up_1000'][i,j].values.flatten()[0]\n",
221
+ "\n",
222
+ " theta = theta_grid[i,j].values.flatten()[0]\n",
223
+ " theta1 = theta1_grid[i,j].values.flatten()[0]\n",
224
+ " theta2 = theta2_grid[i,j].values.flatten()[0]\n",
225
+ "\n",
226
+ " U = U_d1*(theta2-theta)/(theta2-theta1)+U_d2*(theta-theta1)/(theta2-theta1)\n",
227
+ " V = V_d1*(theta2-theta)/(theta2-theta1)+V_d2*(theta-theta1)/(theta2-theta1)\n",
228
+ " W_transf = W_d1*(theta2-theta)/(theta2-theta1)+W_d2*(theta-theta1)/(theta2-theta1)\n",
229
+ "\n",
230
+ " wind.u[i,j] = U\n",
231
+ " wind.v[i,j] = V\n",
232
+ "\n",
233
+ " wind.speed[i,j] = wind.speed[i,j] * W_transf\n"
234
+ ]
235
+ },
236
+ {
237
+ "cell_type": "code",
238
+ "execution_count": null,
239
+ "metadata": {},
240
+ "outputs": [],
241
+ "source": [
242
+ "# WIP vectorized approach, ignore\n",
243
+ "# Precompute constants\n",
244
+ "N_windfield = len(ds.data_vars) // 3\n",
245
+ "delta_angle = 360.0 / N_windfield\n",
246
+ "\n",
247
+ "# Precompute nearest neighbors for all lat/lon combinations\n",
248
+ "lon_grid, lat_grid = np.meshgrid(wind['lon'], wind['lat'], indexing='xy')\n",
249
+ "nearest_points = ds.sel(lat=xr.DataArray(lat_grid, dims=[\"lon\", \"lat\"]),\n",
250
+ " lon=xr.DataArray(lon_grid, dims=[\"lon\", \"lat\"]),\n",
251
+ " method=\"nearest\")\n",
252
+ "\n",
253
+ "nearest_lats = nearest_points[\"lat\"].values\n",
254
+ "nearest_lons = nearest_points[\"lon\"].values\n",
255
+ "\n",
256
+ "# Precompute indices for nearest neighbors\n",
257
+ "lon_indices = np.searchsorted(ds['lon'], nearest_lons)\n",
258
+ "lat_indices = np.searchsorted(ds['lat'], nearest_lats)\n",
259
+ "\n",
260
+ "# Vectorized computation over the entire grid\n",
261
+ "theta = wind['dir'].values # Shape (lon, lat)\n",
262
+ "\n",
263
+ "\n",
264
+ "\n",
265
+ "# Extract U, V, and W components for d1 and d2 using precomputed indices\n",
266
+ "U_d1 = ds[f'dir_{d1_angles}_U'].values[lon_indices, lat_indices]\n",
267
+ "U_d2 = ds[f'dir_{d2_angles}_U'].values[lon_indices, lat_indices]\n",
268
+ "\n",
269
+ "V_d1 = ds[f'dir_{d1_angles}_V'].values[lon_indices, lat_indices]\n",
270
+ "V_d2 = ds[f'dir_{d2_angles}_V'].values[lon_indices, lat_indices]\n",
271
+ "\n",
272
+ "W_d1 = ds[f'dir_{d1_angles}_spd_up_1000'].values[lon_indices, lat_indices]\n",
273
+ "W_d2 = ds[f'dir_{d2_angles}_spd_up_1000'].values[lon_indices, lat_indices]\n",
274
+ "\n",
275
+ "# Interpolate U, V, and W components based on theta\n",
276
+ "U = U_d1 * (theta2 - theta) / (theta2 - theta1) + U_d2 * (theta - theta1) / (theta2 - theta1)\n",
277
+ "V = V_d1 * (theta2 - theta) / (theta2 - theta1) + V_d2 * (theta - theta1) / (theta2 - theta1)\n",
278
+ "W_transf = W_d1 * (theta2 - theta) / (theta2 - theta1) + W_d2 * (theta - theta1) / (theta2 - theta1)\n",
279
+ "\n",
280
+ "# Update wind dataset with computed values\n",
281
+ "wind['u'].values[:, :] = U\n",
282
+ "wind['v'].values[:, :] = V\n",
283
+ "wind['speed'].values[:, :] *= W_transf"
284
+ ]
285
+ }
286
+ ],
287
+ "metadata": {
288
+ "kernelspec": {
289
+ "display_name": "Python 3 (ipykernel)",
290
+ "language": "python",
291
+ "name": "python3"
292
+ },
293
+ "language_info": {
294
+ "codemirror_mode": {
295
+ "name": "ipython",
296
+ "version": 3
297
+ },
298
+ "file_extension": ".py",
299
+ "mimetype": "text/x-python",
300
+ "name": "python",
301
+ "nbconvert_exporter": "python",
302
+ "pygments_lexer": "ipython3",
303
+ "version": "3.11.9"
304
+ }
305
+ },
306
+ "nbformat": 4,
307
+ "nbformat_minor": 2
308
+ }
@@ -0,0 +1,20 @@
1
+ spack:
2
+ specs:
3
+ - "py-numpy"
4
+ - "py-importlib-metadata"
5
+ - 'py-xarray +io +viz +parallel'
6
+ - "py-netcdf4"
7
+ - "py-pandas"
8
+ - "gdal@3.5: +python +hdf5 +netcdf"
9
+ - "py-dask"
10
+ - "py-pyproj"
11
+ - "py-matplotlib"
12
+ - "py-jupyterlab"
13
+ - "py-natsort"
14
+ - "py-rioxarray"
15
+ - "py-rasterio"
16
+ - "py-tdqm"
17
+ - "py-windmapper"
18
+ - "windninja"
19
+ view: ./view
20
+
@@ -0,0 +1,22 @@
1
+ # Configuration file for windmapper when a DEM is downloaded from the SRTM 30m archive
2
+
3
+ # Resolution of WindNinja simulations (in m)
4
+ res_wind = 50
5
+
6
+ # Number of wind speed categories (every 360/ncat degrees)
7
+ ncat = 8
8
+
9
+ # Flag to use existing DEM (DEM must be in UTM for WindNinja)
10
+ use_existing_dem = True
11
+ dem_filename = 'FABDEM-clip.tif'
12
+
13
+ # Averaging method to compute the transfer function in the downscaling method
14
+ # "mean_tile": the wind speed is average over the whole domain for each WN simulation (as in marsh et al, 2020)
15
+ # "grid": the wind speed is averaged over a squared area of size targer_res (as in Vionnet et al., 2020)
16
+ wind_average = 'grid'
17
+
18
+ # Target resolution for avegaging (in m)
19
+ targ_res = 1000
20
+
21
+
22
+ MPI_workers=8
@@ -1,9 +1,10 @@
1
1
  set -e
2
- . ~/Documents/science/code/spack/share/spack/setup-env.sh
2
+ # . ~/Documents/science/code/spack/share/spack/setup-env.sh
3
3
  #setup build env
4
- #spack env create -d spack-deploy spack-deploy.yaml
4
+ rm -rf spack-deploy dist
5
+ spack env create -d spack-deploy spack-deploy.yaml
5
6
  spack env activate spack-deploy
6
- #spack install
7
+ spack install
7
8
 
8
9
 
9
10
  #build current version
@@ -14,5 +15,4 @@ twine upload dist/* --skip-existing
14
15
  #clean up
15
16
  spack env deactivate
16
17
  rm -rf spack-deploy
17
- rm -rf dist
18
-
18
+ rm -rf dist
@@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"
4
4
 
5
5
  [project]
6
6
  name = "windmapper"
7
- version = "2.2.3"
7
+ version = "2.2.5"
8
8
  authors = [
9
9
  {name="Chris Marsh"}
10
10
  ]
@@ -27,7 +27,7 @@ dependencies = [
27
27
  Homepage = "https://github.com/Chrismarsh/Windmapper"
28
28
 
29
29
  [tool.scikit-build]
30
- sdist.exclude = ["examples", "docs", "_config.yml", "dist", "spack-deploy", "venv", "implementation"]
30
+ sdist.exclude = ["examples", "docs", "_config.yml", "dist"]
31
31
 
32
32
  [project.scripts]
33
33
  windmapper = "windmapper:main"
@@ -0,0 +1,6 @@
1
+ spack:
2
+ specs:
3
+ - py-pip
4
+ - py-build
5
+ - py-twine@6
6
+ view: ./venv-deploy
@@ -0,0 +1,115 @@
1
+ """Prepare a rectangular, fully populated elevation grid for WindNinja."""
2
+
3
+ import math
4
+
5
+ import numpy as np
6
+ from osgeo import gdal
7
+ from pyproj import Transformer
8
+
9
+
10
+ def largest_valid_window(ds, anchor=None):
11
+ """Find the maximum-area valid pixel window, optionally containing (col, row)."""
12
+ band = ds.GetRasterBand(1)
13
+ mask = band.GetMaskBand()
14
+ nodata = band.GetNoDataValue()
15
+ if nodata is None:
16
+ nodata = -9999 # WindNinja's fallback for untagged elevation rasters.
17
+ alpha = None
18
+ for i in range(2, ds.RasterCount + 1):
19
+ other = ds.GetRasterBand(i)
20
+ if other.GetColorInterpretation() == gdal.GCI_AlphaBand:
21
+ alpha = other
22
+ break
23
+
24
+ width = ds.RasterXSize
25
+ heights = np.zeros(width + 1, dtype=np.int32) # trailing zero closes the last run
26
+ best = None
27
+ best_area = 0
28
+ for row in range(ds.RasterYSize):
29
+ z = band.ReadAsArray(0, row, width, 1)[0]
30
+ valid = np.isfinite(z) & (z != nodata)
31
+ valid &= mask.ReadAsArray(0, row, width, 1)[0] != 0
32
+ if alpha is not None:
33
+ valid &= alpha.ReadAsArray(0, row, width, 1)[0] > 0
34
+ heights[:-1] = np.where(valid, heights[:-1] + 1, 0)
35
+
36
+ # Consecutive valid heights form a histogram. A falling edge closes
37
+ # every taller rectangle on the stack at this column (exclusive).
38
+ stack = []
39
+ for right in np.flatnonzero(np.diff(heights, prepend=0)).tolist():
40
+ height = int(heights[right])
41
+ left = right
42
+ while stack and stack[-1][1] > height:
43
+ left, h = stack.pop()
44
+ top = row + 1 - h
45
+ area = (right - left) * h
46
+ if area > best_area and (
47
+ anchor is None or (left <= anchor[0] < right and top <= anchor[1] <= row)
48
+ ):
49
+ best = (left, top, right - left, h)
50
+ best_area = area
51
+ if height and (not stack or stack[-1][1] < height):
52
+ stack.append((left, height))
53
+
54
+ if best is None:
55
+ if anchor is not None:
56
+ raise ValueError(f"DEM anchor at pixel {anchor} has no valid elevation")
57
+ raise ValueError("The DEM contains no valid elevations")
58
+ return best
59
+
60
+
61
+ def prepare_existing_dem(filename, output, skip_mercator_proj=False, anchor_lonlat=None):
62
+ """Select the rectangle on the projected grid from which all tiles are copied."""
63
+ src = gdal.Open(filename)
64
+ srs = src.GetSpatialRef()
65
+ if srs is None:
66
+ raise ValueError(f"DEM has no coordinate system: {filename}")
67
+
68
+ gt = src.GetGeoTransform()
69
+ if skip_mercator_proj:
70
+ if not srs.IsProjected() or not math.isclose(srs.GetLinearUnits(), 1.0):
71
+ raise ValueError("skip_mercator_proj requires a projected DEM in metres")
72
+ target = src.GetProjection()
73
+ else:
74
+ x, y = gdal.ApplyGeoTransform(gt, src.RasterXSize / 2, src.RasterYSize / 2)
75
+ _, lat = Transformer.from_crs(src.GetProjection(), 'EPSG:4326', always_xy=True).transform(x, y)
76
+ target = f"+proj=merc +lat_ts={lat} +datum=WGS84 +units=m"
77
+
78
+ grid = src
79
+ north_up_square = gt[1] > 0 and gt[2] == gt[4] == 0 and math.isclose(gt[1], -gt[5], rel_tol=1e-12)
80
+ if not skip_mercator_proj or not north_up_square:
81
+ # GDAL can suggest non-square cells when the input already uses the
82
+ # chosen CRS. Keep the suggested pixel area while enforcing dx == dy.
83
+ suggested = gdal.AutoCreateWarpedVRT(src, None, target).GetGeoTransform()
84
+ resolution = math.sqrt(abs(suggested[1] * suggested[5] - suggested[2] * suggested[4]))
85
+ nodata = src.GetRasterBand(1).GetNoDataValue()
86
+ grid = gdal.Warp(
87
+ '', src, format='VRT', dstSRS=target, resampleAlg='bilinear',
88
+ xRes=resolution, yRes=resolution, outputType=gdal.GDT_Float32,
89
+ srcNodata=nodata if nodata is not None else -9999, dstNodata=-9999,
90
+ )
91
+
92
+ anchor = None
93
+ if anchor_lonlat is not None:
94
+ transform = Transformer.from_crs('EPSG:4326', grid.GetProjection(), always_xy=True)
95
+ x, y = transform.transform(*anchor_lonlat, errcheck=True)
96
+ col, row = gdal.ApplyGeoTransform(gdal.InvGeoTransform(grid.GetGeoTransform()), x, y)
97
+ anchor = (math.floor(col), math.floor(row))
98
+ if not (0 <= anchor[0] < grid.RasterXSize and 0 <= anchor[1] < grid.RasterYSize):
99
+ raise ValueError(f"DEM anchor {anchor_lonlat} lies outside the projected grid")
100
+
101
+ window = largest_valid_window(grid, anchor)
102
+ x, y, width, height = window
103
+ print(f"Selected valid DEM window at ({x}, {y}): {width} x {height} "
104
+ f"of {grid.RasterXSize} x {grid.RasterYSize} pixels")
105
+ return gdal.Translate(output, grid, format='GTiff', srcWin=window, bandList=[1],
106
+ maskBand='none', creationOptions=['TILED=YES', 'BIGTIFF=IF_SAFER'])
107
+
108
+
109
+ def tile_window(width, height, i, j, count_x, count_y, overlap):
110
+ """Return an overlapping pixel window. Tile (0, 0) is the southwest tile."""
111
+ left = max(0, i * width // count_x - overlap)
112
+ right = min(width, (i + 1) * width // count_x + overlap)
113
+ top = max(0, height - (j + 1) * height // count_y - overlap)
114
+ bottom = min(height, height - j * height // count_y + overlap)
115
+ return left, top, right - left, bottom - top
@@ -17,6 +17,8 @@
17
17
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
 
20
+ import math
21
+
20
22
  import elevation
21
23
  import pdb, os, shutil
22
24
  import subprocess
@@ -30,6 +32,7 @@ from tqdm import tqdm
30
32
  import cloudpickle
31
33
  from mpi4py import MPI
32
34
  from . import windmapper_utls as wm
35
+ from .dem import prepare_existing_dem, tile_window
33
36
 
34
37
 
35
38
 
@@ -60,20 +63,24 @@ def main():
60
63
 
61
64
  # path to Wind Ninja executable
62
65
 
63
- # default path assumes that WN is on $PATH
64
- wn_exe = shutil.which('WindNinja_cli')
66
+ # Priority: explicit configuration, environment, then PATH.
67
+ requested = (
68
+ getattr(X, "wn_exe", None)
69
+ or os.environ.get("WINDNINJA_EXE")
70
+ or "WindNinja_cli"
71
+ )
65
72
 
66
- if hasattr(X, 'wn_exe'):
67
- wn_exe = X.wn_exe
73
+ wn_exe = shutil.which(os.path.expanduser(os.fspath(requested)))
68
74
 
69
- if not os.path.exists(wn_exe):
70
- if hasattr(X, 'wn_exe'):
71
- print('ERROR: Path to WindNinja_cli not found. Confirm `wn_exe` config option is correct.')
72
- print(f'Path = {wn_exe}')
73
- else:
74
- print(
75
- 'ERROR: WindNinja_cli not found on $PATH. Ensure it is is or Consider specifying a `wn_exe` config option')
76
- exit(-1)
75
+ if wn_exe is None:
76
+ raise FileNotFoundError(
77
+ f"WindNinja executable not found or not executable: {requested!r}. "
78
+ "Set wn_exe in the configuration, set WINDNINJA_EXE, "
79
+ "or put WindNinja_cli on PATH."
80
+ )
81
+
82
+ # Resolve Spack view symlinks before deriving the data directory.
83
+ wn_exe = os.path.realpath(wn_exe)
77
84
 
78
85
  MPI_exec_str = None
79
86
  if hasattr(X, 'MPI_exec_str'):
@@ -237,126 +244,15 @@ write_farsite_atm = false """
237
244
  name_utm = 'ref-DEM-proj'
238
245
  fic_lcc = user_output_dir + '/' + name_utm + '.tif'
239
246
 
240
- # LCC_proj = '+proj=lcc +lon_0=-90 +lat_1=33 +lat_2=45'
241
- LCC_proj = None
242
247
  if use_existing_dem:
243
-
244
- # if we are using a user-provided dem, ensure there are no NoData values that border the
245
- # DEM which will cause issues and ensure it is rectangular
246
- print('Preparing input DEM')
247
-
248
- # mask data values
249
- print('...',end='')
250
- exec_str = """gdal_calc.py -A %s --outfile %s --NoDataValue 0 --calc="1*(A>-100)" """ % (
251
- dem_filename, user_output_dir + 'out.tif')
252
- subprocess.check_call([exec_str], shell=True)
253
- print('25...', end='')
254
-
255
- # convert to shp file
256
- exec_str = """gdal_polygonize.py -8 -b 1 -f "ESRI Shapefile" %s %s/pols """ % (
257
- user_output_dir + 'out.tif', user_output_dir)
258
- subprocess.check_call([exec_str], shell=True)
259
- print('50...', end='')
260
-
261
- # #
262
- # # driver = ogr.GetDriverByName('ESRI Shapefile')
263
- # # dataSource = driver.Open('windmapper_config/pols/out.shp', 0)
264
- # # layer = dataSource.GetLayer()
265
- # # feat = layer.GetFeature(0)
266
- # # geom = feat.GetGeomFieldRef(0)
267
- # #
268
- # # (minX, maxX, minY, maxY) = geom.GetEnvelope()
269
- #
270
- # with open('%s/pols' % user_output_dir, 'r') as file:
271
- # poly = json.load(file)
272
- #
273
- # coords = poly['features'][0]['geometry']['coordinates'][0]
274
- # c = np.array(coords)
275
- # # [0] = lon, [1] = lat
276
- # bbox = [min(coords, key=lambda x: x[0]),
277
- # max(coords, key=lambda x: x[0]),
278
- # min(coords, key=lambda x: x[1]),
279
- # max(coords, key=lambda x: x[1])]
280
- # bbox = np.array(bbox)
281
- #
282
- # # pick the middle
283
- # lons = np.sort(bbox[:, 0])[1:3]
284
- # lats = np.sort(bbox[:, 1])[1:3]
285
- #
286
- # lat_min = X.lat_min = min(lats)
287
- # lat_max = X.lat_max = max(lats)
288
- # lon_min = X.lon_min = min(lons)
289
- # lon_max = X.lon_max = max(lons)
290
- #
291
- # srs_out = osr.SpatialReference()
292
- # srs_out.ImportFromEPSG(4326)
293
- #
294
- # pts_to_shp([[lat_max, lon_min],
295
- # [lat_max, lon_max],
296
- # [lat_min, lon_max],
297
- # [lat_min, lon_min]],
298
- # os.path.join(user_output_dir, 'shp', 'test.shp'),
299
- # srs_out.ExportToProj4(),
300
- # )
301
- #
302
- #
303
-
304
-
305
- # clip original with the shpfile to get the no data only zone
306
- exec_str = """%sgdalwarp -of GTiff -cutline %s/pols/out.shp -crop_to_cutline -dstalpha %s %s """ % (gdal_prefix,
307
- user_output_dir, dem_filename, fic_lcc)
308
- subprocess.check_call([exec_str], shell=True)
309
- print('75...', end='')
310
- shutil.rmtree("%s/pols" % user_output_dir)
311
- os.remove("%s/out.tif" % user_output_dir)
312
- print('100', end='')
313
- print(' - done')
314
-
315
- # Get the bounding box so we can write out shp file later
316
- # also serves to test that we have a geo referenced input
317
- try:
318
- info = gdal.Info(fic_lcc, format='json')
319
- lon = [info['wgs84Extent']['coordinates'][0][x][0] for x in range(0,4)]
320
- lat = [info['wgs84Extent']['coordinates'][0][x][1] for x in range(0,4)]
321
-
322
- lat_min = X.lat_min = min(lat)
323
- lat_max = X.lat_max = max(lat)
324
- lon_min = X.lon_min = min(lon)
325
- lon_max = X.lon_max = max(lon)
326
-
327
- fac = 0.1 # Expansion factor to make sure that the downloaded SRTM tile is large enough
328
-
329
- # Properties of the bounding box
330
- delta_lat = lat_max - lat_min
331
- delta_lon = lon_max - lon_min
332
-
333
- # This is a small extent than what we have so we ensure perfect coverage
334
- lon_min = lon_min + delta_lon * fac
335
- lat_min = lat_min + delta_lat * fac
336
- lon_max = lon_max - delta_lon * fac
337
- lat_max = lat_max - delta_lat * fac
338
-
339
- except:
340
- print('There is no coordinate defined for this input tif.')
341
- exit(-1)
342
-
343
- # we may wish to skip this for specific inputs
344
- # a tight square UTM domain will be slightly offset in merc causing WN issues
345
- if not skip_mercator_proj:
346
- #ensure we have a rectangular domain
347
- LCC_proj = '+proj=merc +lat_ts=%.30f' % ((lat_min + lat_max) / 2.0)
348
-
349
- # convert this to our custom mercator projection
350
- exec_str = '%sgdalwarp %s %s -overwrite -dstnodata -9999 -t_srs "%s" -r bilinear '
351
- com_string = exec_str % (gdal_prefix, fic_lcc, fic_lcc+'.tmp.tif', LCC_proj)
352
- subprocess.check_call([com_string], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
353
-
354
- os.remove("%s" % fic_lcc)
355
- #ensure we have a float32 dataset
356
- exec_str = '%sgdal_translate -ot Float32 %s %s' % (gdal_prefix, fic_lcc+'.tmp.tif', fic_lcc)
357
- subprocess.check_call([exec_str], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
358
- os.remove(fic_lcc+'.tmp.tif')
359
-
248
+ ds = prepare_existing_dem(dem_filename, fic_lcc, skip_mercator_proj,
249
+ getattr(X, 'dem_anchor_lonlat', None))
250
+ gt = ds.GetGeoTransform()
251
+ xmin, ymax = gt[0], gt[3]
252
+ xmax = xmin + ds.RasterXSize * gt[1]
253
+ ymin = ymax + ds.RasterYSize * gt[5]
254
+ bbox = [[ymax, xmin], [ymax, xmax], [ymin, xmax], [ymin, xmin]]
255
+ bbox_srs = ds.GetProjection()
360
256
  else:
361
257
 
362
258
  # need to ensure that we request a square domain in LCC projection
@@ -420,47 +316,20 @@ write_farsite_atm = false """
420
316
 
421
317
  os.remove(fic_lcc+'.tmp.tif')
422
318
 
423
-
424
- srs_out = osr.SpatialReference()
425
- srs_out.ImportFromEPSG(4326)
426
-
427
- pts_to_shp([[lat_max, lon_min],
428
- [lat_max, lon_max],
429
- [lat_min, lon_max],
430
- [lat_min, lon_min]],
431
- os.path.join(user_output_dir,'shp','user_bbox.shp'),
432
- srs_out.ExportToProj4(),
433
- )
434
-
435
- ds = gdal.Open(fic_lcc)
436
- wkt = ds.GetProjection()
437
- srs = osr.SpatialReference()
438
- srs.ImportFromWkt(wkt)
439
-
440
- if LCC_proj is None:
441
- LCC_proj = srs.ExportToProj4()
442
-
443
- band = ds.GetRasterBand(1)
444
- gt = ds.GetGeoTransform()
445
-
446
- t_4326_to_merc = Transformer.from_crs("epsg:4326", LCC_proj, always_xy=True)
447
- x_merc, y_merc = t_4326_to_merc.transform(
448
- [lon_min, lon_max, lon_min, lon_max],
449
- [lat_min, lat_min, lat_max, lat_max],
450
- )
451
-
452
- xmin = min(x_merc)
453
- xmax = max(x_merc)
454
- ymin = min(y_merc)
455
- ymax = max(y_merc)
456
-
457
-
458
- is_geographic = srs.IsGeographic()
459
- if is_geographic:
460
- raise Exception('Input DEM must be projected ')
461
-
462
- pixel_width = gt[1]
463
- pixel_height = -gt[5]
319
+ ds = gdal.Open(fic_lcc)
320
+ gt = ds.GetGeoTransform()
321
+ bbox = [[lat_max, lon_min], [lat_max, lon_max],
322
+ [lat_min, lon_max], [lat_min, lon_min]]
323
+ bbox_srs = 'EPSG:4326'
324
+ t_4326_to_merc = Transformer.from_crs(bbox_srs, LCC_proj, always_xy=True)
325
+ x_merc, y_merc = t_4326_to_merc.transform(
326
+ [point[1] for point in bbox], [point[0] for point in bbox])
327
+ xmin, xmax = min(x_merc), max(x_merc)
328
+ ymin, ymax = min(y_merc), max(y_merc)
329
+
330
+ cutline = os.path.join(user_output_dir, 'shp', 'user_bbox.shp')
331
+ pts_to_shp(bbox, cutline, bbox_srs)
332
+ pixel_width, pixel_height = gt[1], -gt[5]
464
333
 
465
334
  lenx = xmax-xmin
466
335
  leny = ymax-ymin
@@ -468,8 +337,13 @@ write_farsite_atm = false """
468
337
  len_wn = res_wind * nres
469
338
 
470
339
  # Number of Wind Ninja tiles
471
- nopt_x = int(lenx // len_wn + 1)
472
- nopt_y = int(leny // len_wn + 1)
340
+ if use_existing_dem:
341
+ # Balanced integer pixel windows, including exact multiples of the target size.
342
+ nopt_x = min(ds.RasterXSize, max(1, math.ceil(ds.RasterXSize * pixel_width / len_wn)))
343
+ nopt_y = min(ds.RasterYSize, max(1, math.ceil(ds.RasterYSize * pixel_height / len_wn)))
344
+ else:
345
+ nopt_x = int(lenx // len_wn + 1)
346
+ nopt_y = int(leny // len_wn + 1)
473
347
 
474
348
  nx = lenx/pixel_width / nopt_x
475
349
  ny = leny/pixel_height / nopt_y
@@ -478,7 +352,17 @@ write_farsite_atm = false """
478
352
  # DEM is small enough for WN
479
353
  name_tmp = 'tmp_0_0'
480
354
  fic_tmp = user_output_dir + name_tmp + ".tif"
481
- shutil.copy(fic_lcc, fic_tmp)
355
+ gdal.Translate(fic_tmp, ds)
356
+
357
+ elif use_existing_dem:
358
+ for i, j in itertools.product(range(nopt_x), range(nopt_y)):
359
+ window = tile_window(ds.RasterXSize, ds.RasterYSize, i, j, nopt_x, nopt_y, nadd)
360
+ gdal.Translate(os.path.join(user_output_dir, f'tmp_{i}_{j}.tif'), ds, srcWin=window)
361
+ x, y, w, h = window
362
+ west, north = gdal.ApplyGeoTransform(gt, x, y)
363
+ east, south = gdal.ApplyGeoTransform(gt, x + w, y + h)
364
+ pts_to_shp([[north, west], [north, east], [south, east], [south, west]],
365
+ os.path.join(user_output_dir, 'shp', f'utm_{i}_{j}.shp'), bbox_srs)
482
366
 
483
367
  else:
484
368
 
@@ -550,7 +434,6 @@ write_farsite_atm = false """
550
434
  # absolutely make sure there are no missing data. From the cut above can sometimes be missing 1px along the edge
551
435
  exec_str = '%sgdal_fillnodata.py %s %s'
552
436
  com_string = exec_str % (gdal_prefix, fic_tmp+'.tmp.tif', fic_tmp)
553
- print(com_string)
554
437
  subprocess.check_call([com_string], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
555
438
 
556
439
  os.remove(fic_tmp+'.tmp.tif')
@@ -622,16 +505,9 @@ write_farsite_atm = false """
622
505
  subprocess.check_call([cmd], stdout=subprocess.PIPE,
623
506
  shell=True)
624
507
 
625
- srs_out = osr.SpatialReference()
626
- srs_out.ImportFromEPSG(4326)
627
- exec_str = '%sgdalwarp -overwrite -te %f %f %f %f -r "cubicspline" -et 0 -cutline %s -crop_to_cutline -dstnodata -9999 -t_srs "%s" %s %s'
628
- com_string = exec_str % (gdal_prefix,
629
- X.lon_min, X.lat_min, X.lon_max, X.lat_max,
630
- os.path.join(user_output_dir,'shp', 'user_bbox.shp'),
631
- srs_out.ExportToProj4(),
632
- name_tif+'.tmp.tif',
633
- name_tif+'.tif')
634
- subprocess.check_call([com_string], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
508
+ gdal.Warp(name_tif + '.tif', name_tif + '.tmp.tif',
509
+ dstSRS='EPSG:4326', cutlineDSName=cutline, cropToCutline=True,
510
+ resampleAlg='cubicspline', errorThreshold=0, dstNodata=-9999)
635
511
  os.remove(name_tif+'.tmp.tif')
636
512
 
637
513
  pbar.update(1)
@@ -659,7 +535,7 @@ def pts_to_shp(points, fname, proj):
659
535
  shp_file = driver.CreateDataSource(fname)
660
536
 
661
537
  srs_out = osr.SpatialReference()
662
- srs_out.ImportFromProj4(proj)
538
+ srs_out.SetFromUserInput(proj)
663
539
 
664
540
  layer = shp_file.CreateLayer('mesh', srs_out, ogr.wkbPolygon)
665
541
  layer.CreateField(ogr.FieldDefn('Extent', ogr.OFTReal))
@@ -66,7 +66,7 @@ def call_WN_1dir(WINDNINJA_DATA, gdal_prefix, user_output_dir, fic_config_WN, li
66
66
  exec_cmd = wn_exe + ' ' + \
67
67
  fic_config_WN + ' --elevation_file ' + fic_dem_in + ' --mesh_resolution ' + str(
68
68
  res_wind) + ' --input_direction ' + str(int(wdir)) + ' --output_path ' + dir_tmp
69
- print(exec_cmd)
69
+
70
70
  try:
71
71
  os.environ["WINDNINJA_DATA"] = WINDNINJA_DATA
72
72
  out = subprocess.check_output([exec_cmd],
@@ -0,0 +1,106 @@
1
+ import itertools
2
+ import tempfile
3
+ import unittest
4
+ from pathlib import Path
5
+
6
+ import numpy as np
7
+ from osgeo import gdal, osr
8
+ from pyproj import Transformer
9
+
10
+ from windmapper.dem import largest_valid_window, prepare_existing_dem, tile_window
11
+
12
+
13
+ class TestDemWindows(unittest.TestCase):
14
+ def test_largest_rectangle_against_exhaustive_search(self):
15
+ ds = gdal.GetDriverByName('MEM').Create('', 3, 3, 1, gdal.GDT_Float32)
16
+ band = ds.GetRasterBand(1)
17
+ band.SetNoDataValue(-9999)
18
+ intervals = list(itertools.combinations(range(4), 2))
19
+ for bits in itertools.product([False, True], repeat=9):
20
+ valid = np.array(bits).reshape(3, 3)
21
+ band.WriteArray(np.where(valid, -125, -9999))
22
+ for anchor in (None, (1, 1)):
23
+ candidates = []
24
+ for (left, right), (top, bottom) in itertools.product(intervals, repeat=2):
25
+ if anchor and not (left <= anchor[0] < right and top <= anchor[1] < bottom):
26
+ continue
27
+ if valid[top:bottom, left:right].all():
28
+ candidates.append((left, top, right - left, bottom - top))
29
+ if candidates:
30
+ window = largest_valid_window(ds, anchor)
31
+ self.assertIn(window, candidates)
32
+ self.assertEqual(window[2] * window[3], max(w * h for _, _, w, h in candidates))
33
+ else:
34
+ with self.assertRaises(ValueError):
35
+ largest_valid_window(ds, anchor)
36
+
37
+ def test_masks_and_nonfinite_values(self):
38
+ ds = gdal.GetDriverByName('MEM').Create('', 7, 1, 2, gdal.GDT_Float32)
39
+ band = ds.GetRasterBand(1)
40
+ band.WriteArray(np.array([[17, -120, 0, np.nan, -9999, np.inf, 3]]))
41
+ band.CreateMaskBand(gdal.GMF_PER_DATASET)
42
+ band.GetMaskBand().WriteArray(np.array([[0, 255, 255, 255, 255, 255, 255]], dtype='uint8'))
43
+ alpha = ds.GetRasterBand(2)
44
+ alpha.SetColorInterpretation(gdal.GCI_AlphaBand)
45
+ alpha.WriteArray(np.array([[255, 255, 255, 255, 255, 255, 0]]))
46
+ self.assertEqual(largest_valid_window(ds), (1, 0, 2, 1))
47
+ for x in (0, 3, 4, 5, 6):
48
+ with self.assertRaises(ValueError):
49
+ largest_valid_window(ds, (x, 0))
50
+
51
+ def test_project_crop_and_anchor(self):
52
+ with tempfile.TemporaryDirectory() as tmp:
53
+ src = str(Path(tmp) / 'dem.tif')
54
+ ds = gdal.GetDriverByName('GTiff').Create(src, 16, 12, 1, gdal.GDT_Float32)
55
+ srs = osr.SpatialReference()
56
+ srs.ImportFromEPSG(32608)
57
+ ds.SetProjection(srs.ExportToWkt())
58
+ ds.SetGeoTransform((450000, 30, 0, 7600000, 0, -30))
59
+ band = ds.GetRasterBand(1)
60
+ band.SetNoDataValue(-9999)
61
+ z = np.full((12, 16), -120, dtype=np.float32)
62
+ z[:8, 8:] = -9999
63
+ band.WriteArray(z)
64
+ ds = None
65
+
66
+ to_geo = Transformer.from_crs('EPSG:32608', 'EPSG:4326', always_xy=True)
67
+ anchor = to_geo.transform(450000 + 14.5 * 30, 7600000 - 10.5 * 30)
68
+ out = str(Path(tmp) / 'cropped.tif')
69
+ for native in (True, False):
70
+ cropped = prepare_existing_dem(src, out, native, anchor)
71
+ self.assertTrue((cropped.ReadAsArray() == -120).all())
72
+ if native:
73
+ self.assertEqual((cropped.RasterXSize, cropped.RasterYSize), (16, 4))
74
+ self.assertEqual(cropped.GetGeoTransform(), (450000, 30, 0, 7599760, 0, -30))
75
+ cropped = None
76
+
77
+ with self.assertRaisesRegex(ValueError, 'outside'):
78
+ prepare_existing_dem(src, out, True, (-110, 50))
79
+
80
+ def test_rectify_non_square_cells_in_simulation_crs(self):
81
+ with tempfile.TemporaryDirectory() as tmp:
82
+ src = str(Path(tmp) / 'mercator.tif')
83
+ ds = gdal.GetDriverByName('GTiff').Create(src, 40, 20, 1, gdal.GDT_Float32)
84
+ srs = osr.SpatialReference()
85
+ srs.ImportFromProj4('+proj=merc +lat_ts=0 +datum=WGS84')
86
+ ds.SetProjection(srs.ExportToWkt())
87
+ ds.SetGeoTransform((0, 90, 0, 300, 0, -30))
88
+ ds.GetRasterBand(1).Fill(10)
89
+ ds = None
90
+ for native in (True, False):
91
+ cropped = prepare_existing_dem(src, str(Path(tmp) / 'out.tif'), native)
92
+ gt = cropped.GetGeoTransform()
93
+ self.assertEqual(gt[1], -gt[5])
94
+ self.assertEqual((gt[2], gt[4]), (0, 0))
95
+ self.assertTrue((cropped.ReadAsArray() == 10).all())
96
+ cropped = None
97
+
98
+ def test_tile_overlap_and_coverage(self):
99
+ for nx, ny in [(1, 1), (1, 3), (3, 1), (3, 2)]:
100
+ covered = np.zeros((73, 101), dtype=bool)
101
+ for i, j in itertools.product(range(nx), range(ny)):
102
+ x, y, w, h = tile_window(101, 73, i, j, nx, ny, 25)
103
+ self.assertTrue(0 <= x < x + w <= 101 and 0 <= y < y + h <= 73)
104
+ covered[y:y + h, x:x + w] = True
105
+ self.assertTrue(covered.all())
106
+ self.assertEqual(tile_window(101, 73, 0, 0, 3, 2, 25), (0, 12, 58, 61))
@@ -1,26 +0,0 @@
1
- __pycache__
2
- *.tif
3
- *.vrt
4
- *.asc
5
- *.prj
6
- WindNinja_cli
7
- .python-version
8
- .idea/
9
- docs/_build/
10
-
11
- *.egg-info
12
-
13
- spack-deploy/*
14
- .spack-env/*
15
- ._venv/*
16
- venv
17
-
18
-
19
- dist/*
20
-
21
- spack.lock
22
- /implementation/._view/*
23
- /implementation/.spack-env/*
24
- /implementation/view/*
25
- /implementation/windmapper_config
26
- /implementation/spack.lock
@@ -1,49 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 1,
6
- "metadata": {
7
- "collapsed": true,
8
- "pycharm": {
9
- "name": "#%%\n"
10
- }
11
- },
12
- "outputs": [
13
- {
14
- "name": "stdout",
15
- "output_type": "stream",
16
- "text": [
17
- "Warning: ecCodes 2.22.0 or higher is recommended. You are running version 2.20.0\n"
18
- ]
19
- }
20
- ],
21
- "source": [
22
- "import xarray as xr\n",
23
- "import numpy as np\n",
24
- "import rasterio as rio"
25
- ]
26
- }
27
- ],
28
- "metadata": {
29
- "kernelspec": {
30
- "name": "snowcast",
31
- "language": "python",
32
- "display_name": "snowcast"
33
- },
34
- "language_info": {
35
- "codemirror_mode": {
36
- "name": "ipython",
37
- "version": 2
38
- },
39
- "file_extension": ".py",
40
- "mimetype": "text/x-python",
41
- "name": "python",
42
- "nbconvert_exporter": "python",
43
- "pygments_lexer": "ipython2",
44
- "version": "2.7.6"
45
- }
46
- },
47
- "nbformat": 4,
48
- "nbformat_minor": 0
49
- }
@@ -1,6 +0,0 @@
1
- spack:
2
- specs:
3
- - py-pip
4
- - py-build
5
- - py-twine
6
- view: ./venv-mesher-deploy
@@ -1,3 +0,0 @@
1
- spack:
2
- specs: [py-windmapper]
3
- view: venv
@@ -1,55 +0,0 @@
1
- #!/usr/bin/env python
2
- import numpy as np
3
- import os
4
-
5
- # Write a text file containing information on the WN maps used as input to mesher
6
-
7
- # Directory where are stored to WN maps
8
- dir_wind = '/home/vvionnet/data3/Run_WindNinja/WN_Lajoie/param_lajoie'
9
-
10
- # Direcrtory where are stored mesher command
11
- dir_mesh = '/home/vvionnet/snow_models/mesher/lajoie_srtm'
12
-
13
- # Name of the wind maps:
14
- name_run_WN = 'ref-DEM-utm'
15
-
16
- # Number of wind maps to be used
17
- ncat = 12
18
-
19
- ############################
20
- #No change after these lines
21
- ############################
22
-
23
- # Angle between each wind maps
24
- delta_wind = 360/ncat
25
-
26
- list_wind = np.arange(0,360,delta_wind)
27
-
28
- # List of variables to be inclused in mesher
29
- list_var = ['U','V','spd_up']
30
-
31
- dic_suffix = {'U':'_U','V':'_V','spd_up':''}
32
-
33
- # File containing results
34
- fic_res = dir_mesh+"/config_WN.txt"
35
-
36
- if(os.path.isfile(fic_res)):
37
- os.remove(fic_res)
38
-
39
- file1 = open(fic_res,"w")
40
-
41
- for ii,ww in enumerate(list_wind):
42
-
43
- if(ii==0):
44
- ii=ncat
45
-
46
- for var in list_var:
47
- ll = "'Ninja"+str(ii)+dic_suffix[var]+"' : {'file':'"+dir_wind+"/"+name_run_WN+"_"+str(int(ww))+"_"+var+".vrt','method':'mean'}, \n"
48
- file1.write(ll)
49
-
50
- file1.write("\n")
51
-
52
-
53
-
54
-
55
-
File without changes
File without changes
File without changes
File without changes