-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-producer_bgr.py
560 lines (467 loc) · 29.7 KB
/
run-producer_bgr.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
#!/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 monica_io3
import soil_io3
import monica_run_lib as Mrunlib
PATHS = {
# adjust the local path to your environment
"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/",
"path-to-coords": "/run/user/1000/gvfs/sftp:host=login01.cluster.zalf.de,user=rpm/beegfs/rpm/projects/monica/project/klimertrag/bgr/"
},
"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/",
"path-to-coords": "/run/user/1000/gvfs/sftp:host=login01.cluster.zalf.de,user=rpm/beegfs/rpm/projects/monica/project/klimertrag/bgr/"
},
"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/",
"path-to-coords": "/project/"
}
}
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_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"
TEMPLATE_PATH_LATLON = "{path_to_climate_dir}/latlon-to-rowcol.json"
TEMPLATE_PATH_CLIMATE_CSV = "dwd/csvs/germany/row-{crow}/col-{ccol}.csv"
TEMPLATE_PATH_HARVEST = "{path_to_data_dir}/projects/monica-germany/ILR_SEED_HARVEST_doys_{crop_id}.csv"
def read_csv(path_to_setups_csv, key="id", key_as_int=True):
"read sim setup from csv file"
with open(path_to_setups_csv) as _:
key_to_data = {}
# determine seperator char
dialect = csv.Sniffer().sniff(_.read(), delimiters=';,\t')
_.seek(0)
# read csv with seperator char
reader = csv.reader(_, dialect)
header_cols = next(reader)
for row in reader:
data = {}
for i, header_col in enumerate(header_cols):
value = row[i]
if value.lower() in ["true", "false"]:
value = value.lower() == "true"
if header_col == key:
value = int(value) if key_as_int else value
data[header_col] = value
k = int(data[key]) if key_as_int else data[key]
key_to_data[k] = data
return key_to_data
# commandline parameters e.g "server=localhost port=6666 shared_id=2"
def run_producer(server = {"server": None, "port": None}):
context = zmq.Context()
socket = context.socket(zmq.PUSH) # pylint: disable=no-member
#config_and_no_data_socket = context.socket(zmq.PUSH)
config = {
"mode": "mbm-local-remote", ## local:"cj-local-remote" remote "mbm-local-remote"
"server-port": server["port"] if server["port"] else "6666", ## local: 6667, remote 6666
"server": server["server"] if server["server"] else "login01.cluster.zalf.de",
"sim.json": "sim_bgr.json",
"crop.json": "crop_bgr.json",
"site.json": "site.json",
"setups-file": "sim_setups_bgr.csv",
"run-setups": "[1,2,3,4,5,6,7,8]",
"coords_filename": "all_coord_shuffled_anonymous_additional2.csv",
"id_col_name": "dummy_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)
# 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"])
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)
utm32_to_latlon_transformer = Transformer.from_crs(utm32_crs, wgs84_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)
soil_interpolate = Mrunlib.create_ascii_grid_interpolator(soil_grid, soil_metadata)
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)
sent_env_count = 1
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]
scenario = ""#setup["scenario"]
crop_id = setup["crop-id"]
## extract crop_id from crop-id name that has possible an extenstion
crop_id_short = crop_id.split('_')[0]
# add crop id from setup file
try:
#read seed/harvest dates for each crop_id
path_harvest = TEMPLATE_PATH_HARVEST.format(path_to_data_dir=paths["path-to-data-dir"], crop_id=crop_id_short)
print("created seed harvest gk5 interpolator and read data: ", path_harvest)
Mrunlib.create_seed_harvest_geoGrid_interpolator_and_read_data(path_harvest, wgs84_crs, utm32_crs, ilr_seed_harvest_data)
except IOError:
path_harvest = TEMPLATE_PATH_HARVEST.format(path_to_data_dir=paths["path-to-data-dir"], crop_id=crop_id_short)
print("Couldn't read file:", path_harvest)
continue
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"] + "dwd/csvs/")
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 acording 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
# set the current crop used for this run id
crop_json["cropRotation"][2] = crop_id
# 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": ""
})
soil_id_cache = {}
jobs = read_csv(paths["path-to-coords"] + config["coords_filename"], config["id_col_name"], key_as_int=False)
for _, data in jobs.items():
x_west = float(data["x_west"])
#x_east = float(data["x_east"])
#y_north = float(data["y_north"])
y_south = float(data["y_south"])
id = str(data["dummy_id"])
coords = []
for x, hor_label in enumerate(["W", "", "E"]):
for y, vert_label in enumerate(["S", "", "N"]):
r = x_west + x*1000 + 500
h = y_south + y*1000 + 500
label = "C" if len(vert_label) + len(hor_label) == 0 else vert_label + hor_label
id_ = crop_id_short + "_" + id + "_" + label
coords.append({"id": id_, "r": r, "h": h})
for coord in coords:
r = float(coord["r"])
h = float(coord["h"])
lon, lat = utm32_to_latlon_transformer.transform(r, h)
soil_id = int(soil_interpolate(r, h))
crow, ccol = climate_data_interpolator(r, h)
crop_grid_id = crop_interpolate(r, h)
# print(crop_grid_id)
if crop_grid_id != 1:
continue
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
worksteps = env_template["cropRotation"][0]["worksteps"]
sowing_ws = next(filter(lambda ws: ws["type"][-6:] == "Sowing", worksteps))
harvest_ws = next(filter(lambda ws: ws["type"][-7:] == "Harvest", worksteps))
ilr_interpolate = ilr_seed_harvest_data[crop_id_short]["interpolate"]
seed_harvest_cs = ilr_interpolate(r, h) if ilr_interpolate else None
# set external seed/harvest dates
if seed_harvest_cs:
seed_harvest_data = ilr_seed_harvest_data[crop_id_short]["data"][seed_harvest_cs]
if seed_harvest_data:
is_winter_crop = ilr_seed_harvest_data[crop_id_short]["is-winter-crop"]
if setup["sowing-date"] == "fixed": # fixed indicates that regionally fixed sowing dates will be used
sowing_date = seed_harvest_data["sowing-date"]
elif setup["sowing-date"] == "auto": # auto indicates that automatic sowng dates will be used that vary between regions
sowing_date = seed_harvest_data["latest-sowing-date"]
elif setup["sowing-date"] == "fixed1": # fixed1 indicates that a fixed sowing date will be used that is the same for entire germany
sowing_date = sowing_ws["date"]
sds = [int(x) for x in sowing_date.split("-")]
sd = date(2001, sds[1], sds[2])
sdoy = sd.timetuple().tm_yday
if setup["harvest-date"] == "fixed": # fixed indicates that regionally fixed harvest dates will be used
harvest_date = seed_harvest_data["harvest-date"]
elif setup["harvest-date"] == "auto": # auto indicates that automatic harvest dates will be used that vary between regions
harvest_date = seed_harvest_data["latest-harvest-date"]
elif setup["harvest-date"] == "auto1": # fixed1 indicates that a fixed harvest date will be used that is the same for entire germany
harvest_date = harvest_ws["latest-date"]
# print("sowing_date:", sowing_date, "harvest_date:", harvest_date)
# print("sowing_date:", sowing_ws["date"], "harvest_date:", sowing_ws["date"])
hds = [int(x) for x in harvest_date.split("-")]
hd = date(2001, hds[1], hds[2])
hdoy = hd.timetuple().tm_yday
esds = [int(x) for x in seed_harvest_data["earliest-sowing-date"].split("-")]
esd = date(2001, esds[1], esds[2])
# sowing after harvest should probably never occur in both fixed setup!
if setup["sowing-date"] == "fixed" and setup["harvest-date"] == "fixed":
#calc_harvest_date = date(2000, 12, 31) + timedelta(days=min(hdoy, sdoy-1))
if is_winter_crop:
calc_harvest_date = date(2000, 12, 31) + timedelta(days=min(hdoy, sdoy-1))
else:
calc_harvest_date = date(2000, 12, 31) + timedelta(days=hdoy)
sowing_ws["date"] = seed_harvest_data["sowing-date"]
harvest_ws["date"] = "{:04d}-{:02d}-{:02d}".format(hds[0], calc_harvest_date.month, calc_harvest_date.day)
#print("dates: ", int(seed_harvest_cs), ":", sowing_ws["date"])
#print("dates: ", int(seed_harvest_cs), ":", harvest_ws["date"])
elif setup["sowing-date"] == "fixed" and setup["harvest-date"] == "auto":
if is_winter_crop:
calc_harvest_date = date(2000, 12, 31) + timedelta(days=min(hdoy, sdoy-1))
else:
calc_harvest_date = date(2000, 12, 31) + timedelta(days=hdoy)
sowing_ws["date"] = seed_harvest_data["sowing-date"]
harvest_ws["latest-date"] = "{:04d}-{:02d}-{:02d}".format(hds[0], calc_harvest_date.month, calc_harvest_date.day)
#print("dates: ", int(seed_harvest_cs), ":", sowing_ws["date"])
#print("dates: ", int(seed_harvest_cs), ":", harvest_ws["latest-date"])
elif setup["sowing-date"] == "fixed" and setup["harvest-date"] == "auto1":
if is_winter_crop:
calc_harvest_date = date(2000, 12, 31) + timedelta(days=min(hdoy, sdoy - 1))
else:
calc_harvest_date = date(2000, 12, 31) + timedelta(days=hdoy)
sowing_ws["date"] = seed_harvest_data["sowing-date"]
harvest_ws["latest-date"] = "{:04d}-{:02d}-{:02d}".format(hds[0], hds[1], hds[2])
#print("dates: ", int(seed_harvest_cs), ":", sowing_ws["date"])
#print("dates: ", int(seed_harvest_cs), ":", harvest_ws["latest-date"])
elif setup["sowing-date"] == "auto" and setup["harvest-date"] == "fixed":
sowing_ws["earliest-date"] = seed_harvest_data["earliest-sowing-date"] if esd > date(esd.year, 6, 20) else "{:04d}-{:02d}-{:02d}".format(sds[0], 6, 20)
calc_sowing_date = date(2000, 12, 31) + timedelta(days=max(hdoy+1, sdoy))
sowing_ws["latest-date"] = "{:04d}-{:02d}-{:02d}".format(sds[0], calc_sowing_date.month, calc_sowing_date.day)
harvest_ws["date"] = seed_harvest_data["harvest-date"]
#print("dates: ", int(seed_harvest_cs), ":", sowing_ws["earliest-date"], "<", sowing_ws["latest-date"])
#print("dates: ", int(seed_harvest_cs), ":", harvest_ws["date"])
elif setup["sowing-date"] == "auto" and setup["harvest-date"] == "auto":
sowing_ws["earliest-date"] = seed_harvest_data["earliest-sowing-date"] if esd > date(esd.year, 6, 20) else "{:04d}-{:02d}-{:02d}".format(sds[0], 6, 20)
if is_winter_crop:
calc_harvest_date = date(2000, 12, 31) + timedelta(days=min(hdoy, sdoy-1))
else:
calc_harvest_date = date(2000, 12, 31) + timedelta(days=hdoy)
sowing_ws["latest-date"] = seed_harvest_data["latest-sowing-date"]
harvest_ws["latest-date"] = "{:04d}-{:02d}-{:02d}".format(hds[0], calc_harvest_date.month, calc_harvest_date.day)
#print("dates: ", int(seed_harvest_cs), ":", sowing_ws["earliest-date"], "<", sowing_ws["latest-date"])
#print("dates: ", int(seed_harvest_cs), ":", harvest_ws["latest-date"])
elif setup["sowing-date"] == "fixed1" and setup["harvest-date"] == "fixed":
#calc_harvest_date = date(2000, 12, 31) + timedelta(days=min(hdoy, sdoy-1))
if is_winter_crop:
calc_harvest_date = date(2000, 12, 31) + timedelta(days=min(hdoy, sdoy-1))
else:
calc_harvest_date = date(2000, 12, 31) + timedelta(days=hdoy)
sowing_ws["date"] = sowing_date
# print(seed_harvest_data["sowing-date"])
harvest_ws["date"] = "{:04d}-{:02d}-{:02d}".format(hds[0], calc_harvest_date.month, calc_harvest_date.day)
#print("dates: ", int(seed_harvest_cs), ":", sowing_ws["date"])
#print("dates: ", int(seed_harvest_cs), ":", harvest_ws["date"])
#print("dates: ", int(seed_harvest_cs), ":", sowing_ws["earliest-date"], "<", sowing_ws["latest-date"] )
#print("dates: ", int(seed_harvest_cs), ":", harvest_ws["latest-date"], "<", sowing_ws["earliest-date"], "<", sowing_ws["latest-date"] )
# print("dates: ", int(seed_harvest_cs), ":", sowing_ws["date"])
# print("dates: ", int(seed_harvest_cs), ":", harvest_ws["date"])
if len(soil_profile) == 0:
continue
tcoords = {}
# 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(r, h)
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(r, h)
demr, demh = tcoords[dem_crs]
height_nn = float(dem_interpolate(demr, demh))
if slope_crs not in tcoords:
tcoords[slope_crs] = soil_crs_to_x_transformers[slope_crs].transform(r, h)
slr, slh = tcoords[slope_crs]
slope = float(slope_interpolate(slr, slh))
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 False and 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"]
# setting impenetrable layer
if False and 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"] = 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"]["UseAutomaticIrrigation"] = setup["irrigation"]
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(crow=str(crow), ccol=str(ccol))
env_template["pathToClimateCSV"] = paths["monica-path-to-climate-dir"] + subpath_to_csv
env_template["customId"] = {
"setup_id": setup_id,
"id": id,
"coord_id": coord["id"],
"crop_id_short": crop_id_short,
"lat": lat, "lon": lon
}
socket.send_json(env_template)
print("sent env ", sent_env_count, " customId: ", env_template["customId"])
sent_env_count += 1
#print("crows/cols:", crows_cols)
#cs__.close()
stop_setup_time = time.perf_counter()
print("Setup ", (sent_env_count-1), " envs took ", (stop_setup_time - start_setup_time), " seconds")
stop_time = time.perf_counter()
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()