-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-producer.py
697 lines (597 loc) · 34.3 KB
/
run-producer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#!/usr/bin/python
# -*- coding: UTF-8
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/. */
# Authors:
# Michael Berg-Mohnicke <michael.berg@zalf.de>
#
# Maintainers:
# Currently maintained by the authors.
#
# This file has been created at the Institute of
# Landscape Systems Analysis at the ZALF.
# Copyright (C: Leibniz Centre for Agricultural Landscape Research (ZALF)
from collections import defaultdict
import copy
import csv
from datetime import date, timedelta
import json
import math
import numpy as np
import os
from pyproj import CRS, Transformer
import sqlite3
import sqlite3 as cas_sq3
import sys
import time
import zmq
import geopandas as gpd
import rasterio
from rasterio.transform import from_origin
from rasterio import features
import monica_io3
import soil_io3
import monica_run_lib as Mrunlib
PATHS = {
# adjust the local path to your environment
"re-local-remote": {
# "include-file-base-path": "/home/berg/GitHub/monica-parameters/", # path to monica-parameters
"path-to-climate-dir": "/beegfs/common/data/climate/",
# mounted path to archive or hard drive with climate data
"monica-path-to-climate-dir": "/monica_data/climate-data/",
# mounted path to archive accessable by monica executable
"path-to-data-dir": "./data/", # mounted path to archive or hard drive with data
"path-debug-write-folder": "./debug-out/",
},
# adjust the local path to your environmen
"ow-local-remote": {
# "include-file-base-path": "/home/berg/GitHub/monica-parameters/", # path to monica-parameters
"path-to-climate-dir": "/beegfs/common/data/climate/",
# mounted path to archive or hard drive with climate data
"monica-path-to-climate-dir": "/monica_data/climate-data/",
# mounted path to archive accessable by monica executable
"path-to-data-dir": "./data/", # mounted path to archive or hard drive with data
"path-debug-write-folder": "./debug-out/",
},
"mbm-local-remote": {
# "include-file-base-path": "/home/berg/GitHub/monica-parameters/", # path to monica-parameters
"path-to-climate-dir": "/run/user/1000/gvfs/sftp:host=login01.cluster.zalf.de,user=rpm/beegfs/common/data/climate/",
# mounted path to archive or hard drive with climate data
"monica-path-to-climate-dir": "/monica_data/climate-data/",
# mounted path to archive accessable by monica executable
"path-to-data-dir": "./data/", # mounted path to archive or hard drive with data
"path-debug-write-folder": "./debug-out/",
},
"mbm-local-local": {
# "include-file-base-path": "/home/berg/GitHub/monica-parameters/", # path to monica-parameters
"path-to-climate-dir": "/run/user/1000/gvfs/sftp:host=login01.cluster.zalf.de,user=rpm/beegfs/common/data/climate/",
# mounted path to archive or hard drive with climate data
"monica-path-to-climate-dir": "/run/user/1000/gvfs/sftp:host=login01.cluster.zalf.de,user=rpm/beegfs/common/data/climate/",
# mounted path to archive accessable by monica executable
"path-to-data-dir": "./data/", # mounted path to archive or hard drive with data
"path-debug-write-folder": "./debug-out/",
},
"remoteProducer-remoteMonica": {
# "include-file-base-path": "/monica-parameters/", # path to monica-parameters
"path-to-climate-dir": "/data/", # mounted path to archive or hard drive with climate data
"monica-path-to-climate-dir": "/monica_data/climate-data/",
# mounted path to archive accessable by monica executable
"path-to-data-dir": "./data/", # mounted path to archive or hard drive with data
"path-debug-write-folder": "/out/debug-out/",
}
}
DATA_SOIL_DB = "germany/buek200.sqlite"
DATA_GRID_HEIGHT = "germany/dem_1000_25832_etrs89-utm32n.asc"
DATA_GRID_SLOPE = "germany/slope_1000_25832_etrs89-utm32n.asc"
DATA_GRID_LAND_USE = "germany/landuse_1000_31469_gk5.asc"
DATA_GRID_SOIL = "germany/buek200_1000_25832_etrs89-utm32n.asc"
#DATA_GRID_SOIL_OW = "germany/buek200_1000_25832_etrs89-utm32n_OW.asc"
#DATA_GRID_CROPS = "germany/permanent-grass-mask-BB_1000_25832_etrs89-utm32n.asc" #nodata was 0 in the file assigned below no data realigned to be -9999
DATA_GRID_CROPS = "germany/permanent-grass-mask-BB_1000_25832_etrs89-utm32n-realigned.asc" #nodata = -9999
#DATA_GRID_CROPS = "germany/OWgermany-crop-ww_1000_25832_etrs89-utm32n.asc" # Added as a cropmap for winter wheat OW
# ORIGINAL DATA_GRID_SOIL = "germany/buek200_1000_25832_etrs89-utm32n.asc"
# DATA_GRID_CROPS = "germany/crops-all2017-2019_1000_25832_etrs89-utm32n.asc"
# DATA_GRID_CROPS = "germany/dwd-stations-pheno_1000_25832_etrs89-utm32n.asc"
# DATA_GRID_CROPS = "germany/germany-complete_1000_25832_etrs89-utm32n.asc"
# DATA_GRID_IRRIGATION = "germany/irrigation_1000_25832_etrs89-utm32n_wc_18.asc"
DATA_GRID_GW_MIN = "germany/gwl-min_1000_25832_etrs89-utm32.asc" # min groundwater level map
DATA_GRID_GW_MAX = "germany/gwl-max_1000_25832_etrs89-utm32.asc" # max groundwater level map
DATA_GRID_GW_MEAN = "germany/gwl-mean_1000_25832_etrs89-utm32.asc" # mean groundwater level map
TEMPLATE_PATH_LATLON = "{path_to_climate_dir}/latlon-to-rowcol.json"
# TEMPLATE_PATH_LATLON = "data/latlon-to-rowcol.json"
TEMPLATE_PATH_CLIMATE_CSV = "{gcm}/{rcm}/{scenario}/{ensmem}/{version}/row-{crow}/col-{ccol}.csv"
# Additional data for masking the regions
NUTS1_REGIONS = "data/germany/NUTS250_N1.shp"
TEMPLATE_PATH_HARVEST = "{path_to_data_dir}/projects/monica-germany/ILR_SEED_HARVEST_doys_{crop_id}.csv"
gdf = gpd.read_file(NUTS1_REGIONS)
DEBUG_DONOT_SEND = False
DEBUG_WRITE = False
DEBUG_ROWS = 10
DEBUG_WRITE_FOLDER = "./debug_out"
DEBUG_WRITE_CLIMATE = False
## Add an argument in the run_producer function and make a loop with changing of the value of the additional parameter (sensitivity analysis)
## Make a list of the parameter values first
# commandline parameters e.g "server=localhost port=6666 shared_id=2"
def run_producer(server={"server": None, "port": None}, shared_id=None):
context = zmq.Context()
socket = context.socket(zmq.PUSH) # pylint: disable=no-member
# config_and_no_data_socket = context.socket(zmq.PUSH)
config = {
"mode": "re-local-remote",
"server-port": server["port"] if server["port"] else "6667",
"server": server["server"] if server["server"] else "login01.cluster.zalf.de",
"start-row": "0",
"end-row": "-1",
"path_to_dem_grid": "",
"sim.json": "sim.json",
"crop.json": "crop.json",
"site.json": "site.json",
"setups-file": "sim_setups_VK.csv",
"run-setups": "[1]",
"shared_id": shared_id
}
# read commandline args only if script is invoked directly from commandline
if len(sys.argv) > 1 and __name__ == "__main__":
for arg in sys.argv[1:]:
k, v = arg.split("=")
if k in config:
config[k] = v
print("config:", config)
# select paths
paths = PATHS[config["mode"]]
# open soil db connection
soil_db_con = sqlite3.connect(paths["path-to-data-dir"] + DATA_SOIL_DB)
# soil_db_con = cas_sq3.connect(paths["path-to-data-dir"] + DATA_SOIL_DB) #CAS.
# connect to monica proxy (if local, it will try to connect to a locally started monica)
socket.connect("tcp://" + config["server"] + ":" + str(config["server-port"]))
# read setup from csv file
setups = Mrunlib.read_sim_setups(config["setups-file"])
rs_ranges = config["run-setups"][1:-1].split(",")
run_setups = []
for rsr in rs_ranges:
rs_r = rsr.split("-")
if 1 < len(rs_r) <= 2:
run_setups.extend(range(int(rs_r[0]), int(rs_r[1])+1))
elif len(rs_r) == 1:
run_setups.append(int(rs_r[0]))
#run_setups = json.loads(config["run-setups"])
print("read sim setups: ", config["setups-file"])
# transforms geospatial coordinates from one coordinate reference system to another
# transform wgs84 into gk5
soil_crs_to_x_transformers = {}
wgs84_crs = CRS.from_epsg(4326)
utm32_crs = CRS.from_epsg(25832)
# transformers[wgs84] = Transformer.from_crs(wgs84_crs, gk5_crs, always_xy=True)
ilr_seed_harvest_data = defaultdict(
lambda: {"interpolate": None, "data": defaultdict(dict), "is-winter-crop": None})
# Load grids
## note numpy is able to load from a compressed file, ending with .gz or .bz2
# soil data
path_to_soil_grid = paths["path-to-data-dir"] + DATA_GRID_SOIL
soil_epsg_code = int(path_to_soil_grid.split("/")[-1].split("_")[2])
soil_crs = CRS.from_epsg(soil_epsg_code)
if wgs84_crs not in soil_crs_to_x_transformers:
soil_crs_to_x_transformers[wgs84_crs] = Transformer.from_crs(soil_crs, wgs84_crs)
soil_metadata, _ = Mrunlib.read_header(path_to_soil_grid)
soil_grid = np.loadtxt(path_to_soil_grid, dtype=int, skiprows=6)
print("read: ", path_to_soil_grid)
# height data for germany
path_to_dem_grid = paths["path-to-data-dir"] + DATA_GRID_HEIGHT
dem_epsg_code = int(path_to_dem_grid.split("/")[-1].split("_")[2])
dem_crs = CRS.from_epsg(dem_epsg_code)
if dem_crs not in soil_crs_to_x_transformers:
soil_crs_to_x_transformers[dem_crs] = Transformer.from_crs(soil_crs, dem_crs)
dem_metadata, _ = Mrunlib.read_header(path_to_dem_grid)
dem_grid = np.loadtxt(path_to_dem_grid, dtype=float, skiprows=6)
dem_interpolate = Mrunlib.create_ascii_grid_interpolator(dem_grid, dem_metadata)
print("read: ", path_to_dem_grid)
# slope data
path_to_slope_grid = paths["path-to-data-dir"] + DATA_GRID_SLOPE
slope_epsg_code = int(path_to_slope_grid.split("/")[-1].split("_")[2])
slope_crs = CRS.from_epsg(slope_epsg_code)
if slope_crs not in soil_crs_to_x_transformers:
soil_crs_to_x_transformers[slope_crs] = Transformer.from_crs(soil_crs, slope_crs)
slope_metadata, _ = Mrunlib.read_header(path_to_slope_grid)
slope_grid = np.loadtxt(path_to_slope_grid, dtype=float, skiprows=6)
slope_interpolate = Mrunlib.create_ascii_grid_interpolator(slope_grid, slope_metadata)
print("read: ", path_to_slope_grid)
# land use data
path_to_landuse_grid = paths["path-to-data-dir"] + DATA_GRID_LAND_USE
landuse_epsg_code = int(path_to_landuse_grid.split("/")[-1].split("_")[2])
landuse_crs = CRS.from_epsg(landuse_epsg_code)
if landuse_crs not in soil_crs_to_x_transformers:
soil_crs_to_x_transformers[landuse_crs] = Transformer.from_crs(soil_crs, landuse_crs)
landuse_meta, _ = Mrunlib.read_header(path_to_landuse_grid)
landuse_grid = np.loadtxt(path_to_landuse_grid, dtype=int, skiprows=6)
landuse_interpolate = Mrunlib.create_ascii_grid_interpolator(landuse_grid, landuse_meta)
print("read: ", path_to_landuse_grid)
# crop mask data
path_to_crop_grid = paths["path-to-data-dir"] + DATA_GRID_CROPS
crop_epsg_code = int(path_to_crop_grid.split("/")[-1].split("_")[2])
crop_crs = CRS.from_epsg(crop_epsg_code)
if crop_crs not in soil_crs_to_x_transformers:
soil_crs_to_x_transformers[crop_crs] = Transformer.from_crs(soil_crs, crop_crs)
crop_meta, _ = Mrunlib.read_header(path_to_crop_grid)
crop_grid = np.loadtxt(path_to_crop_grid, dtype=int, skiprows=6)
crop_interpolate = Mrunlib.create_ascii_grid_interpolator(crop_grid, crop_meta)
print("read: ", path_to_crop_grid)
# # irrigation data
# path_to_irrigation_grid = paths["path-to-data-dir"] + DATA_GRID_IRRIGATION
# irrigation_epsg_code = int(path_to_irrigation_grid.split("/")[-1].split("_")[2])
# irrigation_crs = CRS.from_epsg(irrigation_epsg_code)
# if irrigation_crs not in soil_crs_to_x_transformers:
# soil_crs_to_x_transformers[irrigation_crs] = Transformer.from_crs(soil_crs, irrigation_crs)
# irrigation_metadata, _ = Mrunlib.read_header(path_to_irrigation_grid)
# irrigation_grid = np.loadtxt(path_to_irrigation_grid, dtype=int, skiprows=6)
# irrigation_interpolate = Mrunlib.create_ascii_grid_interpolator(irrigation_grid, irrigation_metadata, False)
# print("read: ", path_to_irrigation_grid)
# min groundwater level data
path_to_gw_min_grid = paths["path-to-data-dir"] + DATA_GRID_GW_MIN
gw_min_epsg_code = int(path_to_gw_min_grid.split("/")[-1].split("_")[2])
gw_min_crs = CRS.from_epsg(gw_min_epsg_code)
if gw_min_crs not in soil_crs_to_x_transformers:
soil_crs_to_x_transformers[gw_min_crs] = Transformer.from_crs(soil_crs, gw_min_crs)
gw_min_metadata, _ = Mrunlib.read_header(path_to_gw_min_grid)
gw_min_grid = np.loadtxt(path_to_gw_min_grid, dtype=float, skiprows=6)
gw_min_interpolate = Mrunlib.create_ascii_grid_interpolator(gw_min_grid, gw_min_metadata)
print("read: ", path_to_gw_min_grid)
# max groundwater level data
path_to_gw_max_grid = paths["path-to-data-dir"] + DATA_GRID_GW_MAX
gw_max_epsg_code = int(path_to_gw_max_grid.split("/")[-1].split("_")[2])
gw_max_crs = CRS.from_epsg(gw_max_epsg_code)
if gw_max_crs not in soil_crs_to_x_transformers:
soil_crs_to_x_transformers[gw_max_crs] = Transformer.from_crs(soil_crs, gw_max_crs)
gw_max_metadata, _ = Mrunlib.read_header(path_to_gw_max_grid)
gw_max_grid = np.loadtxt(path_to_gw_max_grid, dtype=float, skiprows=6)
gw_max_interpolate = Mrunlib.create_ascii_grid_interpolator(gw_max_grid, gw_max_metadata)
print("read: ", path_to_gw_max_grid)
# mean groundwater level data
path_to_gw_mean_grid = paths["path-to-data-dir"] + DATA_GRID_GW_MEAN
gw_mean_epsg_code = int(path_to_gw_mean_grid.split("/")[-1].split("_")[2])
gw_mean_crs = CRS.from_epsg(gw_mean_epsg_code)
if gw_mean_crs not in soil_crs_to_x_transformers:
soil_crs_to_x_transformers[gw_mean_crs] = Transformer.from_crs(soil_crs, gw_mean_crs)
gw_mean_metadata, _ = Mrunlib.read_header(path_to_gw_mean_grid)
gw_mean_grid = np.loadtxt(path_to_gw_mean_grid, dtype=float, skiprows=6)
gw_mean_interpolate = Mrunlib.create_ascii_grid_interpolator(gw_mean_grid, gw_mean_metadata)
print("read: ", path_to_gw_mean_grid)
# Create the function for the mask. This function will later use the additional column in a setup file!
def create_mask_from_shapefile(NUTS1_REGIONS, region_name, path_to_soil_grid):
regions_df = gpd.read_file(NUTS1_REGIONS)
region = regions_df[regions_df["NUTS_NAME"] == region_name]
# This is needed to read the transformation data correctly from the file. With the original opening it does not work
with rasterio.open(path_to_soil_grid) as dataset:
soil_grid = dataset.read(1)
transform = dataset.transform
rows, cols = soil_grid.shape
mask = rasterio.features.geometry_mask([region.geometry.values[0]], out_shape=(rows, cols), transform=transform,
invert=True)
return mask
sent_env_count = 0
start_time = time.perf_counter()
listOfClimateFiles = set()
# run calculations for each setup
for _, setup_id in enumerate(run_setups):
if setup_id not in setups:
continue
start_setup_time = time.perf_counter()
setup = setups[setup_id]
gcm = setup["gcm"]
rcm = setup["rcm"]
scenario = setup["scenario"]
ensmem = setup["ensmem"]
version = setup["version"]
crop_id = setup["crop-id"]
region_name = setup["region_name"]
## extract crop_id from crop-id name that has possible an extenstion
crop_id_short = crop_id.split('_')[0]
if region_name and len(region_name) > 0:
# Create the soil mask for the specific region
path_to_soil_grid_ow = paths["path-to-data-dir"] + DATA_GRID_SOIL
mask = create_mask_from_shapefile(NUTS1_REGIONS, region_name, path_to_soil_grid_ow)
# Apply the soil mask to the soil grid
soil_grid_copy = soil_grid.copy()
soil_grid[mask == False] = -8888
soil_grid[soil_grid_copy == -9999] = -9999
cdict = {}
# path to latlon-to-rowcol.json
# path = TEMPLATE_PATH_LATLON.format(path_to_climate_dir=paths["path-to-climate-dir"] + setup["climate_path_to_latlon_file"] + "/")
path = TEMPLATE_PATH_LATLON.format(
path_to_climate_dir=paths["path-to-climate-dir"] + setup["climate_path_to_latlon_file"] + "/")
climate_data_interpolator = Mrunlib.create_climate_geoGrid_interpolator_from_json_file(path, wgs84_crs,
soil_crs, cdict)
print("created climate_data to gk5 interpolator: ", path)
# read template sim.json
with open(setup.get("sim.json", config["sim.json"])) as _:
sim_json = json.load(_)
# change start and end date according to setup
if setup["start_date"]:
sim_json["climate.csv-options"]["start-date"] = str(setup["start_date"])
if setup["end_date"]:
sim_json["climate.csv-options"]["end-date"] = str(setup["end_date"])
# sim_json["include-file-base-path"] = paths["include-file-base-path"]
# read template site.json
with open(setup.get("site.json", config["site.json"])) as _:
site_json = json.load(_)
if len(scenario) > 0 and scenario[:3].lower() == "rcp":
site_json["EnvironmentParameters"]["rcp"] = scenario
# read template crop.json
with open(setup.get("crop.json", config["crop.json"])) as _:
crop_json = json.load(_)
crop_json["CropParameters"]["__enable_vernalisation_factor_fix__"] = setup[
"use_vernalisation_fix"] if "use_vernalisation_fix" in setup else False
# create environment template from json templates
env_template = monica_io3.create_env_json_from_json_config({
"crop": crop_json,
"site": site_json,
"sim": sim_json,
"climate": ""
})
# set shared id in template
if config["shared_id"]:
env_template["sharedId"] = config["shared_id"]
scols = int(soil_metadata["ncols"])
srows = int(soil_metadata["nrows"])
scellsize = int(soil_metadata["cellsize"])
xllcorner = int(soil_metadata["xllcorner"])
yllcorner = int(soil_metadata["yllcorner"])
nodata_value = int(soil_metadata["nodata_value"])
# unknown_soil_ids = set()
soil_id_cache = {}
print("All Rows x Cols: " + str(srows) + "x" + str(scols))
# cs__ = open("coord_mapping_etrs89-utm32n_to_wgs84-latlon.csv", "w")
# cs__.write("row,col,center_25832_etrs89-utm32n_r,center_25832_etrs89-utm32n_h,center_lat,center_lon\n")
for srow in range(0, srows):
print(srow, end=", ")
if srow < int(config["start-row"]):
continue
elif int(config["end-row"]) > 0 and srow > int(config["end-row"]):
break
for scol in range(0, scols):
soil_id = int(soil_grid[srow, scol])
if soil_id == nodata_value:
continue
# get coordinate of clostest climate element of real soil-cell
sh = yllcorner + (scellsize / 2) + (srows - srow - 1) * scellsize
sr = xllcorner + (scellsize / 2) + scol * scellsize
# inter = crow/ccol encoded into integer
crow, ccol = climate_data_interpolator(sr, sh)
crop_grid_id = int(crop_grid[srow, scol])
# print(crop_grid_id)
if crop_grid_id != 1 or soil_id == -8888:
# print("row/col:", srow, "/", scol, "is not a crop pixel.")
env_template["customId"] = {
"setup_id": setup_id,
"srow": srow, "scol": scol,
"crow": int(crow), "ccol": int(ccol),
"soil_id": soil_id,
"env_id": sent_env_count,
"nodata": True,
}
if not DEBUG_DONOT_SEND:
socket.send_json(env_template)
# print("sent nodata env ", sent_env_count, " customId: ", env_template["customId"])
sent_env_count += 1
continue
tcoords = {}
if soil_id in soil_id_cache:
soil_profile = soil_id_cache[soil_id]
else:
soil_profile = soil_io3.soil_parameters(soil_db_con, soil_id)
soil_id_cache[soil_id] = soil_profile
if len(soil_profile) == 0:
env_template["customId"] = {
"setup_id": setup_id,
"srow": srow, "scol": scol,
"crow": int(crow), "ccol": int(ccol),
"soil_id": soil_id,
"env_id": sent_env_count,
"nodata": True,
}
if not DEBUG_DONOT_SEND:
socket.send_json(env_template)
# print("sent nodata env ", sent_env_count, " customId: ", env_template["customId"])
sent_env_count += 1
continue
# check if current grid cell is used for agriculture
if setup["landcover"]:
if landuse_crs not in tcoords:
tcoords[landuse_crs] = soil_crs_to_x_transformers[landuse_crs].transform(sr, sh)
lur, luh = tcoords[landuse_crs]
landuse_id = landuse_interpolate(lur, luh)
if landuse_id not in [2, 3, 4]:
continue
if dem_crs not in tcoords:
tcoords[dem_crs] = soil_crs_to_x_transformers[dem_crs].transform(sr, sh)
demr, demh = tcoords[dem_crs]
height_nn = dem_interpolate(demr, demh)
if slope_crs not in tcoords:
tcoords[slope_crs] = soil_crs_to_x_transformers[slope_crs].transform(sr, sh)
slr, slh = tcoords[slope_crs]
slope = slope_interpolate(slr, slh)
if gw_min_crs not in tcoords:
tcoords[gw_min_crs] = soil_crs_to_x_transformers[gw_min_crs].transform(sr, sh)
gw_min_r, gw_min_h = tcoords[gw_min_crs]
min_groundwater_depth = gw_min_interpolate(gw_min_r, gw_min_h)
if gw_max_crs not in tcoords:
tcoords[gw_max_crs] = soil_crs_to_x_transformers[gw_max_crs].transform(sr, sh)
gw_max_r, gw_max_h = tcoords[gw_max_crs]
max_groundwater_depth = gw_max_interpolate(gw_max_r, gw_max_h)
if gw_mean_crs not in tcoords:
tcoords[gw_mean_crs] = soil_crs_to_x_transformers[gw_mean_crs].transform(sr, sh)
gw_mean_r, gw_mean_h = tcoords[gw_mean_crs]
mean_groundwater_depth = gw_mean_interpolate(gw_mean_r, gw_mean_h)
# if irrigation_crs not in tcoords:
# tcoords[irrigation_crs] = soil_crs_to_x_transformers[irrigation_crs].transform(sr, sh)
# irr_r, irr_h = tcoords[irrigation_crs]
# irrigation = int(irrigation_interpolate(irr_r, irr_h))
env_template["params"]["userCropParameters"]["__enable_T_response_leaf_expansion__"] = setup[
"LeafExtensionModifier"]
# print("soil:", soil_profile)
env_template["params"]["siteParameters"]["SoilProfileParameters"] = soil_profile
# setting groundwater level
# if setup["groundwater-level"]:
# groundwaterlevel = 20
# layer_depth = 0
# for layer in soil_profile:
# if layer.get("is_in_groundwater", False):
# groundwaterlevel = layer_depth
# # print("setting groundwaterlevel of soil_id:", str(soil_id), "to", groundwaterlevel, "m")
# break
# layer_depth += Mrunlib.get_value(layer["Thickness"])
# env_template["params"]["userEnvironmentParameters"]["MinGroundwaterDepthMonth"] = 3
# env_template["params"]["userEnvironmentParameters"]["MinGroundwaterDepth"] = [
# max(0, groundwaterlevel - 0.2), "m"]
# env_template["params"]["userEnvironmentParameters"]["MaxGroundwaterDepth"] = [
# groundwaterlevel + 0.2, "m"]
if setup["groundwater-level"] == "MINMAX":
# Assign min and max groundwater depths to the environment template
env_template["params"]["userEnvironmentParameters"]["MinGroundwaterDepthMonth"] = 3
env_template["params"]["userEnvironmentParameters"]["MinGroundwaterDepth"] = [
min_groundwater_depth / 100, "m"]
env_template["params"]["userEnvironmentParameters"]["MaxGroundwaterDepth"] = [
max_groundwater_depth / 100, "m"]
# elif setup["groundwater-level"] == "MEAN":
# # Assign mean groundwater depth to the environment template
# env_template["params"]["userEnvironmentParameters"]["MinGroundwaterDepthMonth"] = 3
# env_template["params"]["userEnvironmentParameters"]["MinGroundwaterDepth"] = [
# mean_groundwater_depth / 100, "m"]
# env_template["params"]["userEnvironmentParameters"]["MaxGroundwaterDepth"] = [
# mean_groundwater_depth / 100, "m"]
elif setup["groundwater-level"] == "MIN":
# Assign min and max groundwater depths to the environment template
env_template["params"]["userEnvironmentParameters"]["MinGroundwaterDepthMonth"] = 3
env_template["params"]["userEnvironmentParameters"]["MinGroundwaterDepth"] = [
min_groundwater_depth / 100, "m"]
env_template["params"]["userEnvironmentParameters"]["MaxGroundwaterDepth"] = [
min_groundwater_depth / 100, "m"]
elif setup["groundwater-level"] == "MAX":
# Assign min and max groundwater depths to the environment template
env_template["params"]["userEnvironmentParameters"]["MinGroundwaterDepthMonth"] = 3
env_template["params"]["userEnvironmentParameters"]["MinGroundwaterDepth"] = [
max_groundwater_depth / 100, "m"]
env_template["params"]["userEnvironmentParameters"]["MaxGroundwaterDepth"] = [
max_groundwater_depth / 100, "m"]
# setting impenetrable layer
if setup["impenetrable-layer"]:
impenetrable_layer_depth = Mrunlib.get_value(
env_template["params"]["userEnvironmentParameters"]["LeachingDepth"])
layer_depth = 0
for layer in soil_profile:
if layer.get("is_impenetrable", False):
impenetrable_layer_depth = layer_depth
# print("setting leaching depth of soil_id:", str(soil_id), "to", impenetrable_layer_depth, "m")
break
layer_depth += Mrunlib.get_value(layer["Thickness"])
env_template["params"]["userEnvironmentParameters"]["LeachingDepth"] = [impenetrable_layer_depth,
"m"]
env_template["params"]["siteParameters"]["ImpenetrableLayerDepth"] = [impenetrable_layer_depth, "m"]
if setup["elevation"]:
env_template["params"]["siteParameters"]["heightNN"] = float(height_nn)
if setup["slope"]:
env_template["params"]["siteParameters"]["slope"] = slope / 100.0
if setup["latitude"]:
clat, _ = cdict[(crow, ccol)]
env_template["params"]["siteParameters"]["Latitude"] = clat
if setup["CO2"]:
env_template["params"]["userEnvironmentParameters"]["AtmosphericCO2"] = float(setup["CO2"])
if setup["O3"]:
env_template["params"]["userEnvironmentParameters"]["AtmosphericO3"] = float(setup["O3"])
if setup["FieldConditionModifier"]:
env_template["cropRotation"][0]["worksteps"][0]["crop"]["cropParams"]["species"][
"FieldConditionModifier"] = float(setup["FieldConditionModifier"])
if setup["StageTemperatureSum"]:
stage_ts = setup["StageTemperatureSum"].split('_')
stage_ts = [int(temp_sum) for temp_sum in stage_ts]
orig_stage_ts = env_template["cropRotation"][0]["worksteps"][0]["crop"]["cropParams"]["cultivar"][
"StageTemperatureSum"][0]
if len(stage_ts) != len(orig_stage_ts):
stage_ts = orig_stage_ts
print('The provided StageTemperatureSum array is not '
'sufficiently long. Falling back to original StageTemperatureSum')
env_template["cropRotation"][0]["worksteps"][0]["crop"]["cropParams"]["cultivar"][
"StageTemperatureSum"][0] = stage_ts
env_template["params"]["simulationParameters"]["UseNMinMineralFertilisingMethod"] = setup[
"fertilization"]
env_template["params"]["simulationParameters"]["NitrogenResponseOn"] = setup["NitrogenResponseOn"]
env_template["params"]["simulationParameters"]["WaterDeficitResponseOn"] = setup[
"WaterDeficitResponseOn"]
env_template["params"]["simulationParameters"]["EmergenceMoistureControlOn"] = setup[
"EmergenceMoistureControlOn"]
env_template["params"]["simulationParameters"]["EmergenceFloodingControlOn"] = setup[
"EmergenceFloodingControlOn"]
env_template["csvViaHeaderOptions"] = sim_json["climate.csv-options"]
subpath_to_csv = TEMPLATE_PATH_CLIMATE_CSV.format(gcm=gcm, rcm=rcm, scenario=scenario, ensmem=ensmem,
version=version, crow=str(crow), ccol=str(ccol))
for _ in range(4):
subpath_to_csv = subpath_to_csv.replace("//", "/")
env_template["pathToClimateCSV"] = [
paths["monica-path-to-climate-dir"] + setup["climate_path_to_csvs"] + "/" + subpath_to_csv]
if setup["incl_hist"]:
hist_subpath_to_csv = TEMPLATE_PATH_CLIMATE_CSV.format(gcm=gcm, rcm=rcm, scenario="historical",
ensmem=ensmem, version=version,
crow=str(crow), ccol=str(ccol))
for _ in range(4):
hist_subpath_to_csv = hist_subpath_to_csv.replace("//", "/")
env_template["pathToClimateCSV"].insert(0, paths["monica-path-to-climate-dir"] + setup[
"climate_path_to_csvs"] + "/" + hist_subpath_to_csv)
print("pathToClimateCSV:", env_template["pathToClimateCSV"])
if DEBUG_WRITE_CLIMATE:
listOfClimateFiles.add(subpath_to_csv)
env_template["customId"] = {
"setup_id": setup_id,
"srow": srow, "scol": scol,
"crow": int(crow), "ccol": int(ccol),
"soil_id": soil_id,
"env_id": sent_env_count,
"nodata": False
}
if not DEBUG_DONOT_SEND:
socket.send_json(env_template)
print("sent env ", sent_env_count, " customId: ", env_template["customId"])
sent_env_count += 1
# write debug output, as json file
if DEBUG_WRITE:
debug_write_folder = paths["path-debug-write-folder"]
if not os.path.exists(debug_write_folder):
os.makedirs(debug_write_folder)
if sent_env_count < DEBUG_ROWS:
path_to_debug_file = debug_write_folder + "/row_" + str(sent_env_count - 1) + "_" + str(
setup_id) + ".json"
if not os.path.isfile(path_to_debug_file):
with open(path_to_debug_file, "w") as _:
_.write(json.dumps(env_template))
else:
print("WARNING: Row ", (sent_env_count - 1), " already exists")
# print("unknown_soil_ids:", unknown_soil_ids)
#if env_template:
# env_template["pathToClimateCSV"] = ""
# env_template["customId"] = {
# "setup_id": setup_id,
# "nodata": True,
# "no_of_sent_envs": sent_env_count,
# }
# socket.send_json(env_template)
# print("crows/cols:", crows_cols)
# cs__.close()
stop_setup_time = time.perf_counter()
print("\nSetup ", sent_env_count, " envs took ", (stop_setup_time - start_setup_time), " seconds")
sent_env_count = 0
stop_time = time.perf_counter()
# write summary of used json files
if DEBUG_WRITE_CLIMATE:
debug_write_folder = paths["path-debug-write-folder"]
if not os.path.exists(debug_write_folder):
os.makedirs(debug_write_folder)
path_to_climate_summary = debug_write_folder + "/climate_file_list" + ".csv"
with open(path_to_climate_summary, "w") as _:
_.write('\n'.join(listOfClimateFiles))
try:
print("sending ", (sent_env_count - 1), " envs took ", (stop_time - start_time), " seconds")
# print("ran from ", start, "/", row_cols[start], " to ", end, "/", row_cols[end]
print("exiting run_producer()")
except Exception:
raise
if __name__ == "__main__":
run_producer()