forked from ec-jrc/pyPoseidon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telemac.py
1451 lines (1198 loc) · 53.2 KB
/
telemac.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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Schism model of pyposeidon. It controls the creation, execution & output of a complete simulation based on SCHISM.
"""
# Copyright 2018 European Union
# This file is part of pyposeidon.
# Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence").
# Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the Licence for the specific language governing permissions and limitations under the Licence.
import os
import datetime
import numpy as np
import sys
import json
import pandas as pd
import glob
import xarray as xr
import geopandas as gp
import shapely
import jinja2
from searvey import ioc
import shutil
from scipy.spatial import Delaunay
# local modules
import pyposeidon
import pyposeidon.mesh as pmesh
import pyposeidon.meteo as pmeteo
import pyposeidon.dem as pdem
from pyposeidon.paths import DATA_PATH
from pyposeidon.utils.get_value import get_value
from pyposeidon.utils.converter import myconverter
from pyposeidon.utils.cpoint import closest_n_points
from pyposeidon.utils import data
from pyposeidon.utils.norm import normalize_column_names
from pyposeidon.utils.norm import normalize_varnames
from pyposeidon.utils.obs import get_obs_data
from pyposeidon.utils.post import export_xarray
# telemac (needs telemac stack on conda)
from telapy.api.t2d import Telemac2d
from telapy.api.t3d import Telemac3d
from telapy.api.art import Artemis
from telapy.api.wac import Tomawac
from execution.telemac_cas import TelemacCas
from data_manip.formats.selafin import Selafin
from data_manip.extraction.telemac_file import TelemacFile
from pretel.extract_contour import detecting_boundaries
from pretel.generate_atm import generate_atm
from pretel.extract_contour import sorting_boundaries
from utils.progressbar import ProgressBar
from utils.geometry import get_weights
from utils.geometry import interp
from pretel.manip_telfile import alter
from xarray_selafin.xarray_backend import SelafinAccessor
from mpi4py import MPI
import numpy as np
import logging
from . import tools
logger = logging.getLogger(__name__)
TELEMAC_NAME = "telemac"
def divisors(n):
result = set()
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
result.add(i)
result.add(n // i)
return sorted(result)
def calculate_time_step_hourly_multiple(resolution, min_water_depth=0.1, courant_number=50):
"""
Calculate the maximum allowable time step for a shallow water equation model
based on the CFL condition, rounded to the closest divisor of an hour.
:param resolution: Spatial resolution (Δx) in meters.
:param min_water_depth: Minimum water depth (h_min) in meters. Default is 1 meter.
:param courant_number: Courant number (C), typically less than 1 for explicit schemes. Can be higher for implicit schemes.
:return: Maximum allowable time step (Δt) in seconds, rounded to the closest divisor of an hour.
"""
resolution *= 111.389 # degrees to meters conversion factor
raw_time_step = courant_number * resolution / (9.81 * min_water_depth) ** 0.5
# Get all divisors of an hour (3600 seconds)
hour_divisors = divisors(3600)
# Find the closest divisor of an hour to the raw time step
closest_divisor = min(hour_divisors, key=lambda x: abs(x - raw_time_step))
return closest_divisor
# Helper functions
def remove(path):
try:
if os.path.isfile(path):
os.remove(path) # Remove a file
elif os.path.isdir(path):
if not os.listdir(path): # Check if the directory is empty
os.rmdir(path)
else:
shutil.rmtree(path)
except OSError as e:
print(f"Error: {e.strerror}")
def is_ccw(tris, meshx, meshy):
x1, x2, x3 = meshx[tris].T
y1, y2, y3 = meshy[tris].T
return (y3 - y1) * (x2 - x1) > (y2 - y1) * (x3 - x1)
def flip(tris):
return np.column_stack((tris[:, 2], tris[:, 1], tris[:, 0]))
def is_overlapping(tris, meshx):
PIR = 180
x1, x2, x3 = meshx[tris].T
return np.logical_or(abs(x2 - x1) > PIR, abs(x3 - x1) > PIR)
def get_det_mask(tris, meshx, meshy):
t12 = meshx[tris[:, 1]] - meshx[tris[:, 0]]
t13 = meshx[tris[:, 2]] - meshx[tris[:, 0]]
t22 = meshy[tris[:, 1]] - meshy[tris[:, 0]]
t23 = meshy[tris[:, 2]] - meshy[tris[:, 0]]
return t12 * t23 - t22 * t13 > 0
def contains_pole(x, y):
return np.any(y == 90, axis=0)
def fix_glob_connectivity(x, y, ikle2, corrections):
# Assuming slf.meshx, slf.meshy, and slf.ikle2 are NumPy arrays
ikle2 = np.array(ikle2)
# Ensure all triangles are CCW
ccw_mask = is_ccw(ikle2, x, y)
ikle2[~ccw_mask] = flip(ikle2[~ccw_mask])
# triangles accross the dateline
m_ = is_overlapping(ikle2, x)
ikle2[m_] = flip(ikle2[m_])
# special case : pole triangles
pole_mask = contains_pole(x[ikle2].T, y[ikle2].T)
# manual additional corrections
if corrections is not None:
for rev in corrections["reverse"]:
ikle2[rev : rev + 1] = flip(ikle2[rev : rev + 1])
for rem in corrections["remove"]:
pole_mask[rem] = True
# Check for negative determinant
detmask = ~get_det_mask(ikle2, x, y)
logger.debug(f"reversed {detmask.sum()} triangles")
logger.debug(f"pole triangles: {np.where(pole_mask)[0]}")
logger.debug("[TEMPORARY FIX]: REMOVE THE POLE TRIANGLES")
return ikle2[~pole_mask, :]
def write_netcdf(ds, outpath):
fileOut = os.path.splitext(outpath)[0] + ".nc"
ds.to_netcdf(fileOut)
def write_meteo(outpath, geo, ds, gtype="grid", ttype="time", input360=False):
lon = ds.longitude.values
if input360:
lon[lon > 180] -= 360
lat = ds.latitude.values
if gtype == "grid":
nx1d = len(lon)
ny1d = len(lat)
x = np.tile(lon, ny1d).reshape(ny1d, nx1d).T.ravel()
y = np.tile(lat, nx1d)
else:
x = lon
y = lat
if ttype == "time":
t0 = pd.Timestamp(ds.time.values[0])
elif ttype == "step":
t0 = pd.Timestamp(ds.time.values)
seconds = ds.step.values / 1e9
ds.time = pd.to_datetime(t0 + pd.Timedelta(seconds=seconds))
geo = xr.open_dataset(geo, engine = 'selafin')
in_xy = np.vstack((x,y)).T
out_xy = np.vstack((geo.x,geo.y)).T
logger.info(f"Geting interp weights from {len(x)} on {len(geo.x)} nodes")
vert, wgts, u_x, g_x = get_weights(in_xy, out_xy)
data_vars = {}
var_attrs = {}
dtype = np.float64
dims = ["time", "node"]
shape = (len(ds.time), len(geo.x))
coords = {
"x": ("node", geo.x.data),
"y": ("node", geo.y.data),
"time": ds.time,
}
# Define a mapping from the original variable names to the new ones
var_map = {
"u10": ("WINDX", "WINDX", "M/S"),
"v10": ("WINDY", "WINDY", "M/S"),
"msl": ("PATM", "PATM", "PASCAL"),
"tmp": ("TAIR", "TEMPERATURE", "DEGREES C"),
}
for var in ds.data_vars:
if var in var_map:
# attributes
var_attrs[var_map[var][0]] = (var_map[var][1], var_map[var][2])
# data
data = np.empty(shape, dtype=dtype)
for it, t_ in enumerate(ds.time):
tmp = np.ravel(np.transpose(ds.isel(time=it)[var].values))
data[it, :] = interp(tmp, vert, wgts, u_x, g_x)
data_vars[var_map[var][0]] = xr.Variable(dims=dims, data=data)
atm = xr.Dataset(data_vars=data_vars, coords=coords)
atm.attrs["date_start"] = [t0.year, t0.month, t0.day, t0.hour, t0.minute, t0.second]
atm.attrs["ikle2"] = geo.attrs['ikle2']
atm.attrs["variables"] = var_attrs
atm.selafin.write(outpath)
def get_boundary_settings(boundary_type, glo_node, bnd_node):
settings = {
"lihbor": 5 if boundary_type == "open" else 2,
"liubor": 6 if boundary_type == "open" else 2,
"livbor": 6 if boundary_type == "open" else 2,
"hbor": 0.0,
"ubor": 0.0,
"vbor": 0.0,
"aubor": 0.0,
"litbor": 5 if boundary_type == "open" else 2,
"tbor": 0.0,
"atbor": 0.0,
"btbor": 0.0,
"nbor": glo_node + 1,
"k": bnd_node + 1,
}
return settings
def format_value(value, width, precision=3, is_float=False):
if is_float:
return f"{value:{5}.{precision}f}"
else:
return f"{value:{2}}"
def detect_boundary_points_optimized(connectivity_table, npoints):
# Building adjacency list from connectivity table
adjacency_list = {i: [] for i in range(npoints)}
for row in connectivity_table:
for i in range(3):
adjacency_list[row[i]].extend(row[np.arange(3) != i])
buf = []
connectivity_bnd_elt = []
pbar = ProgressBar(npoints)
for i in range(npoints):
pbar.update(i)
# Directly accessing the connections for each point
connections = adjacency_list[i]
uniq, count = np.unique(connections, return_counts=True)
temp = uniq[count == 1].tolist()
buf.extend(temp)
if temp:
if i in temp:
temp.remove(i)
temp.append(i)
connectivity_bnd_elt.append(temp)
pbar.finish()
bnd_points = np.unique(buf)
return np.array(connectivity_bnd_elt), bnd_points
def write_cli(inTel, ds, outCli=None, tel_module="telemac2d", global_=True):
"""
(This function is a modification of the existing extract_contour() function
in scripts/python3/pretel/extract_contour.py of the TELEMAC scripts)
Generic function for extraction of contour from a mesh (with our without
boundary file)
@param inTel (str) Path to the mesh file
@param ds (xr.Dataset) xarray Dataset of the mesh file (used to extract the boundary types)
@param outCli (str) Path to the output contour file
@returns (list) List of polygons
"""
# TELEMAC script PART
domains = []
tel = TelemacFile(inTel)
connectivity_bnd_elt, bnd_points = detect_boundary_points_optimized(tel.ikle2, tel.npoin2)
boundaries_ordered, bnd_idx_left, bnd_elt_left = sorting_boundaries(
tel, bnd_points, connectivity_bnd_elt, global_=global_
)
domains.append(boundaries_ordered)
i = 1
while bnd_elt_left.size != 0:
i += 1
boundaries_ordered, bnd_idx_left, bnd_elt_left = sorting_boundaries(
tel, bnd_idx_left, bnd_elt_left, global_=global_
)
domains.append(boundaries_ordered)
# custom pyposeidon part
if outCli is None:
outCli = os.path.splitext(inTel)[0] + ".cli"
node_to_type = dict(zip(ds.node.values, ds.type.values)) # mapping from node to type
domains_bnd = []
lines = []
bnd_node = 0
for domain in domains:
poly_bnd = []
for bnd in domain:
coord_bnd = []
for i, glo_node in enumerate(bnd[:-1]): # not taking the last node (not repeating)
x, y = tel.meshx[glo_node], tel.meshy[glo_node]
coord_bnd.append((x, y))
# Determine boundary type for the current node
boundary_type = node_to_type.get(glo_node, "Unknown")
if boundary_type == "open":
# Get previous and next node indices in a circular manner
prev_node = bnd[i - 1] if i > 0 else bnd[-2] # -2 to skip the repeated last node
next_node = bnd[i + 1] if i < len(bnd) - 2 else bnd[0] # Wrapping around to the first node
# Get boundary types for previous and next nodes
prev_boundary_type = node_to_type.get(prev_node, "Unknown")
next_boundary_type = node_to_type.get(next_node, "Unknown")
# If both adjacent nodes are not 'open', then bnd is closed
if prev_boundary_type != "open" and next_boundary_type != "open":
boundary_type = "Unknown"
boundary_settings = get_boundary_settings(boundary_type, glo_node, bnd_node)
keys_order = [
"lihbor",
"liubor",
"livbor",
"hbor",
"ubor",
"vbor",
"aubor",
"litbor",
"tbor",
"atbor",
"btbor",
"nbor",
"k",
]
if tel_module == "telemac2d":
line = " ".join(str(boundary_settings[key]) for key in keys_order)
lines.append(line)
bnd_node += 1
poly_bnd.append((coord_bnd, bnd))
domains_bnd.append(poly_bnd)
# Writing to file
with open(outCli, "w") as f:
for line in lines:
formatted_line = " ".join(
format_value(value, 3, is_float=isinstance(value, float)) for value in line.split()
)
f.write(f"{formatted_line}\n")
return domains_bnd
def write_cas(outpath, tel_module, params):
# Load the Jinja2 template
env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.join(DATA_PATH)))
# Define the custom filter for tidal bounds
def repeat_value(value, count):
return ";".join([str(value)] * count)
env.filters["repeat_value"] = repeat_value
template = env.get_template(tel_module + ".cas")
ouput = template.render(params)
with open(os.path.join(outpath, tel_module + ".cas"), "w") as f:
f.write(ouput)
class Telemac:
def __init__(self, **kwargs):
"""
Create a TELEMAC model:
by default : TELEMAC2D is used.
Args:
rfolder str: the TELEMAC module to use. Can be one of the following: telemac3d, telemac2d, artemis, tomawac
"""
tel_module = kwargs.get("module", "telemac2d")
if tel_module not in ["telemac3d", "telemac2d", "artemis", "tomawac"]:
raise ValueError("module must be one of the following: telemac3d, telemac2d, artemis, tomawac")
rfolder = kwargs.get("rfolder", None)
if rfolder:
self.read_folder(**kwargs)
self.geometry = kwargs.get("geometry", None)
if self.geometry:
if isinstance(self.geometry, dict):
self.lon_min = self.geometry["lon_min"]
self.lon_max = self.geometry["lon_max"]
self.lat_min = self.geometry["lat_min"]
self.lat_max = self.geometry["lat_max"]
elif self.geometry == "global":
logger.warning("geometry is 'global'")
elif isinstance(self.geometry, str):
try:
geo = gp.GeoDataFrame.from_file(self.geometry)
except:
logger.error("geometry argument not a valid geopandas file")
sys.exit(1)
(
self.lon_min,
self.lat_min,
self.lon_max,
self.lat_max,
) = geo.total_bounds
else:
logger.warning("no geometry given")
# coastlines
coastlines = kwargs.get("coastlines", None)
if coastlines is not None:
try:
coast = gp.GeoDataFrame.from_file(coastlines)
except:
coast = gp.GeoDataFrame(coastlines)
self.coastlines = coast
if not hasattr(self, "start_date"):
start_date = kwargs.get("start_date", None)
self.start_date = pd.to_datetime(start_date)
if not hasattr(self, "end_date"):
if "time_frame" in kwargs:
time_frame = kwargs.get("time_frame", None)
self.end_date = self.start_date + pd.to_timedelta(time_frame)
elif "end_date" in kwargs:
end_date = kwargs.get("end_date", None)
self.end_date = pd.to_datetime(end_date)
self.time_frame = self.end_date - self.start_date
if not hasattr(self, "rdate"):
self.rdate = get_value(self, kwargs, "rdate", self.start_date)
if not hasattr(self, "end_date"):
# ---------------------------------------------------------------------
logger.warning("model not set properly, No end_date\n")
# ---------------------------------------------------------------------
self.tstep = kwargs.get("tstep", None)
self.tag = tel_module
self.tide = kwargs.get("tide", False)
tpxo_ = kwargs.get("tpxo_source", None)
if tpxo_ is not None:
self.tide = True
self.atm = kwargs.get("atm", True)
self.monitor = kwargs.get("monitor", False)
self.solver_name = TELEMAC_NAME
# restart
restart = get_value(self, kwargs, "hotstart", None)
if restart:
self.restart = pd.to_datetime(restart)
else:
self.restart = None
# specific to meteo grib files
self.gtype = get_value(self, kwargs, "meteo_gtype", "grid")
self.ttype = get_value(self, kwargs, "meteo_ttype", "time")
self.ncsize = get_value(self, kwargs, "ncsize", 1)
# convert -180/180 to 0-360
self.input360 = get_value(self, kwargs, "meteo_input360", False)
for attr, value in kwargs.items():
if not hasattr(self, attr):
setattr(self, attr, value)
setattr(self, "misc", {})
# ============================================================================================
# CONFIG
# ============================================================================================
def config(self, config_file=None, output=False, **kwargs):
dic = get_value(self, kwargs, "parameters", None)
# param_file = get_value(self,kwargs,'config_file',None)
if config_file:
# ---------------------------------------------------------------------
logger.info("reading parameter file {}\n".format(config_file))
# ---------------------------------------------------------------------
else:
# ---------------------------------------------------------------------
logger.info("using default " + self.tag + " json config file ...\n")
# ---------------------------------------------------------------------
config_file = os.path.join(DATA_PATH, self.tag + ".json")
# Load the parameters from the JSON file
with open(config_file, "r") as json_file:
config = json.load(json_file)
params = config["params"]
# update key values
if "telemac" in self.tag:
params["datestart"] = self.start_date.strftime("%Y;%m;%d")
params["timestart"] = self.start_date.strftime("%H;%M;%S")
elif "tomawac" in self.tag:
params["datestart"] = self.start_date.strftime("%Y%m%d%H%M")
if hasattr(self, "time_frame"):
duration = pd.to_timedelta(self.time_frame).total_seconds()
else:
self.time_frame = self.end_date - self.start_date
duration = self.time_frame.total_seconds()
params["duration"] = duration
# export grid data every hour
res_min = get_value(self, kwargs, "resolution_min", 0.5)
if self.tstep:
tstep = self.tstep
else:
tstep = calculate_time_step_hourly_multiple(res_min)
params["tstep"] = tstep
params["nb_tsteps"] = int(duration / tstep)
params["tstep_graph"] = int(3600 / tstep)
params["tstep_list"] = int(3600 / tstep)
params["ncsize"] = self.ncsize
# tide
if self.tide:
params["tide"] = True
params["initial_conditions"] = "TPXO SATELLITE ALTIMETRY"
# hotstart
if self.restart is not None:
hotout = int((self.restart - self.rdate).total_seconds() / (params["tstep"] * params["tstep_graph"]))
params["hotstart"] = True
params["restart_tstep"] = hotout
# update
if dic:
for key in dic.keys():
params[key] = dic[key]
if "params" in kwargs.keys():
for key in kwargs["params"].keys():
params[key] = kwargs["params"][key]
self.params = params
if output:
# save params
# ---------------------------------------------------------------------
logger.info("output " + self.tag + " CAS file ...\n")
# ---------------------------------------------------------------------
path = get_value(self, kwargs, "rpath", "./telemac/")
write_cas(path, self.tag, params)
# ============================================================================================
# METEO
# ============================================================================================
def force(self, **kwargs):
meteo_source = get_value(self, kwargs, "meteo_source", None)
kwargs.update({"meteo_source": meteo_source})
flag = get_value(self, kwargs, "update", ["all"])
z = {**self.__dict__, **kwargs} # merge self and possible kwargs
# check if files exist
if flag:
if ("meteo" in flag) | ("all" in flag):
self.meteo = pmeteo.Meteo(**z)
else:
logger.info("skipping meteo ..\n")
else:
self.meteo = pmeteo.Meteo(**z)
if hasattr(self, "meteDataseto"):
# add 1 hour for Schism issue with end time
ap = self.meteo.Dataset.isel(time=-1)
ap["time"] = ap.time.values + pd.to_timedelta("1H")
self.meteo.Dataset = xr.concat([self.meteo.Dataset, ap], dim="time")
def to_force(self, geo, outpath):
# # WRITE METEO FILE
logger.info("saving meteo file.. ")
out_file = os.path.join(outpath, "input_wind.slf")
write_meteo(out_file, geo, self.meteo.Dataset, gtype=self.gtype, ttype=self.ttype, input360=self.input360)
# ============================================================================================
# TPXO
# ============================================================================================
def tpxo(self, tpxo_suffix="tpxo9_atlas_30_v4", **kwargs):
tpxo_source = get_value(self, kwargs, "tpxo_source", None)
path = get_value(self, kwargs, "rpath", "./telemac")
lat_min = np.round(self.lat_min - 1 if self.lat_min > -89 else self.lat_min, 2)
lat_max = np.round(self.lat_max + 1 if self.lat_max < 89 else self.lat_max, 2)
lon_min = np.round(self.lon_min - 1, 2)
lon_max = np.round(self.lon_max + 1, 2)
if lon_min < 0 and lon_max < 0:
lon_min += 360
lon_max += 360
if tpxo_source is None:
# ---------------------------------------------------------------------
logger.error("model not set properly: define tpxo_source\n")
raise ValueError("model not set properly: define tpxo_source")
# ---------------------------------------------------------------------
os.makedirs(os.path.join(path, "TPXO"), exist_ok=True)
logger.info("extracting tides..\n")
#
lines = []
lines.append(f"{path}/TPXO/input_sources !")
lines.append(f"{path}/TPXO/Model_Local ! Local area Model name")
lines.append(f"{lat_min} {lat_max} ! Lat limits (degrees N)")
lines.append(f"{lon_min} {lon_max} ! Lon limits (degrees E, range -180 +360)")
with open(f"{path}/TPXO/setup.local", "w") as file:
file.write("\n".join(lines))
# input sources
lines = []
lines.append(f"{tpxo_source}/h_*_{tpxo_suffix}")
lines.append(f"{tpxo_source}/u_*_{tpxo_suffix}")
lines.append(f"{tpxo_source}/grid_{tpxo_suffix}")
with open(f"{path}/TPXO/input_sources", "w") as file:
file.write("\n".join(lines))
# output files
lines = []
lines.append(f"{path}/TPXO/h_LOCAL")
lines.append(f"{path}/TPXO/uv_LOCAL")
lines.append(f"{path}/TPXO/grid_LOCAL")
with open(f"{path}/TPXO/Model_Local", "w") as file:
file.write("\n".join(lines))
os.system(f"cp {DATA_PATH}/extract_local_model {path}/TPXO/extract_local_model")
os.system(f"{path}/TPXO/extract_local_model<{path}/TPXO/setup.local")
# ============================================================================================
# DEM
# ============================================================================================
def bath(self, **kwargs):
# z = self.__dict__.copy()
if self.mesh.Dataset is not None:
kwargs["grid_x"] = self.mesh.Dataset.SCHISM_hgrid_node_x.values
kwargs["grid_y"] = self.mesh.Dataset.SCHISM_hgrid_node_y.values
dpath = get_value(self, kwargs, "dem_source", None)
kwargs.update({"dem_source": dpath})
flag = get_value(self, kwargs, "update", ["all"])
# check if files exist
if flag:
if ("dem" in flag) | ("all" in flag):
kwargs.update(
{
"lon_min": self.lon_min,
"lat_min": self.lat_min,
"lon_max": self.lon_max,
"lat_max": self.lat_max,
}
)
self.dem.Dataset = pdem.dem_on_mesh(self.dem.Dataset, **kwargs)
if kwargs.get("adjust_dem", True):
coastline = kwargs.get("coastlines", None)
if coastline is None:
logger.warning("coastlines not present, aborting adjusting dem\n")
elif "adjusted" in self.dem.Dataset.data_vars.keys():
logger.info("Dem already adjusted\n")
elif "fval" in self.dem.Dataset.data_vars.keys():
logger.info("Dem already adjusted\n")
else:
self.dem.adjust(coastline, **kwargs)
try:
try:
bat = -self.dem.Dataset.fval.values.astype(float) # minus for the hydro run
if np.isinf(bat).sum() != 0:
raise Exception("Bathymetry contains Infs")
if np.isnan(bat).sum() != 0:
raise Exception("Bathymetry contains NaNs")
logger.warning("Bathymetric values fval contain NaNs, using ival values ..\n")
except:
bat = -self.dem.Dataset.ival.values.astype(float) # minus for the hydro run
self.mesh.Dataset.depth.loc[: bat.size] = bat
logger.info("updating bathymetry ..\n")
except AttributeError as e:
logger.info("Keeping bathymetry in hgrid.gr3 due to {}\n".format(e))
else:
logger.info("dem from mesh file\n")
@staticmethod
def mesh_to_slf(
x, y, z, tri, outpath, tag="telemac2d", chezy=None, manning=0.027, friction_type="chezy", **kwargs
):
#
ds = xr.Dataset(
{
"B": (("time", "node"), z[np.newaxis,:]),
# Add other variables as needed
},
coords={
"x": ("node", x),
"y": ("node", y),
"time": [pd.Timestamp.now()],
}
)
ds.attrs['ikle2'] = tri + 1
# bottom friction only in the case of TELEMAC2D
if tag == "telemac2d":
if chezy:
c = np.ones(len(z)) * chezy
else:
if friction_type == "chezy":
c = (abs(z) ** (1 / 6)) / manning
else:
print("only Chezy implemented so far! ")
sys.exit()
ds['W'] = xr.Variable(dims=["node"], data=c)
logger.info("Manning file created..\n")
ds.selafin.write(outpath)
def to_slf(self, outpath, global_=True, friction_type="chezy", **kwargs):
corrections = get_value(self, kwargs, "mesh_corrections", {"reverse": [], "remove": []})
X = self.mesh.Dataset.SCHISM_hgrid_node_x.data
Y = self.mesh.Dataset.SCHISM_hgrid_node_y.data
# depth
Z = self.mesh.Dataset.depth.data
nan_mask = np.isnan(Z)
inf_mask = np.isinf(Z)
if np.any(nan_mask) or np.any(inf_mask):
Z[nan_mask] = 0
Z[inf_mask] = 0
logger.info("Z contains . Cleaning needed.")
# connectivity
IKLE2 = self.mesh.Dataset.SCHISM_hgrid_face_nodes.data
remove(outpath)
if global_:
# adjust triangles orientation on the dateline
# and also suppress the pole triangles (for now)
IKLE2 = fix_glob_connectivity(X, Y, IKLE2, corrections)
# write mesh
chezy = get_value(self, kwargs, "chezy", None)
manning = get_value(self, kwargs, "manning", 0.027)
self.mesh_to_slf(X, Y, Z, IKLE2, outpath, self.tag, chezy, manning, friction_type)
# ============================================================================================
# EXECUTION
# ============================================================================================
def create(self, **kwargs):
if not kwargs:
kwargs = self.__dict__.copy()
# Set background dem as scale for mesh generation
dpath = get_value(self, kwargs, "dem_source", None)
if dpath:
self.dem = pdem.Dem(**kwargs)
# kwargs.update({"dem_source": self.dem.Dataset})
else:
logger.info("no dem available\n")
# Mesh
self.mesh = pmesh.set(type="tri2d", **kwargs)
# set lat/lon from file
if self.mesh.Dataset is not None:
kwargs.update({"lon_min": self.mesh.Dataset.SCHISM_hgrid_node_x.values.min()})
kwargs.update({"lon_max": self.mesh.Dataset.SCHISM_hgrid_node_x.values.max()})
kwargs.update({"lat_min": self.mesh.Dataset.SCHISM_hgrid_node_y.values.min()})
kwargs.update({"lat_max": self.mesh.Dataset.SCHISM_hgrid_node_y.values.max()})
self.lon_min = self.mesh.Dataset.SCHISM_hgrid_node_x.values.min()
self.lon_max = self.mesh.Dataset.SCHISM_hgrid_node_x.values.max()
self.lat_min = self.mesh.Dataset.SCHISM_hgrid_node_y.values.min()
self.lat_max = self.mesh.Dataset.SCHISM_hgrid_node_y.values.max()
# get bathymetry
if self.mesh.Dataset is not None:
self.bath(**kwargs)
# get meteo
if self.atm:
self.force(**kwargs)
# get tide
if self.tide:
self.tpxo(**kwargs)
self.config(**kwargs)
def output(self, **kwargs):
path = get_value(self, kwargs, "rpath", "./telemac/")
flag = get_value(self, kwargs, "update", ["all"])
split_by = get_value(self, kwargs, "meteo_split_by", None)
if not os.path.exists(path):
os.makedirs(path)
# Mesh related files
if self.mesh.Dataset is not None:
# WRITE GEO FILE
try:
bat = self.dem.Dataset.fval.values.astype(float) # minus for the hydro run
if np.isnan(bat).sum() != 0:
raise Exception("Bathymetry contains NaNs")
logger.warning("Bathymetric values fval contain NaNs, using ival values ..\n")
except:
bat = self.dem.Dataset.ival.values.astype(float) # minus for the hydro run
self.mesh.Dataset.depth.loc[: bat.size] = bat
logger.info("saving geometry file.. ")
geo = os.path.join(path, "geo.slf")
self.to_slf(geo, global_=True)
write_netcdf(self.mesh.Dataset, geo)
# WRITE METEO FILE
logger.info("saving meteo file.. ")
meteo = os.path.join(path, "input_wind.slf")
self.atm = write_meteo(
meteo, geo, self.meteo.Dataset, gtype=self.gtype, ttype=self.ttype, input360=self.input360
)
# WRITE BOUNDARY FILE
logger.info("saving boundary file.. ")
domain = write_cli(geo, self.mesh.Dataset)
self.params["N_bc"] = len(domain[0])
# WRITE CAS FILE
logger.info("saving CAS file.. ")
write_cas(path, self.tag, self.params)
# ---------------------------------------------------------------------
logger.info("output done\n")
# ---------------------------------------------------------------------
def run(self, **kwargs):
calc_dir = get_value(self, kwargs, "rpath", "./telemac/")
cwd = os.getcwd()
# ---------------------------------------------------------------------
logger.info("executing model\n")
# ---------------------------------------------------------------------
comm = MPI.COMM_WORLD
cas_file = self.tag + ".cas"
os.chdir(calc_dir)
if not tools.is_mpirun_installed():
logger.warning("mpirun is not installed, ending.. \n")
return
# Creation of the instance Telemac2d
study = Telemac2d(cas_file, user_fortran=None, comm=comm, stdout=0, recompile=True)
# Testing construction of variable list
_ = study.variables
study.set_case()
# Initialization
study.init_state_default()
# Run all time steps
ntimesteps = study.get("MODEL.NTIMESTEPS")
pbar = ProgressBar(ntimesteps)
for it in range(ntimesteps):
study.run_one_time_step()
pbar.update(it)
# Ending the run
study.finalize()
pbar.finish()
#
os.chdir(cwd)
def save(self, **kwargs):
path = get_value(self, kwargs, "rpath", "./telemac/")
lista = [key for key, value in self.__dict__.items() if key not in ["meteo", "dem", "mesh"]]
dic = {k: self.__dict__.get(k, None) for k in lista}
mesh = self.__dict__.get("mesh", None)
if isinstance(mesh, str):
dic.update({"mesh": mesh})
else:
dic.update({"mesh": mesh.__class__.__name__})
dem = self.__dict__.get("dem", None)
if isinstance(dem, str):
dic.update({"dem": dem})
elif isinstance(dem, pdem.Dem):
dic.update({"dem_attrs": dem.Dataset.elevation.attrs})
meteo = self.__dict__.get("meteo", None)
if isinstance(meteo, str):
dic.update({"meteo": meteo})
elif isinstance(meteo, pmeteo.Meteo):
try:
dic.update({"meteo": [meteo.Dataset.attrs]})
except:
dic.update({"meteo": [x.attrs for x in meteo.Dataset]})
coastlines = self.__dict__.get("coastlines", None)
if coastlines is not None:
# Save the path to the serialized coastlines - #130
coastlines_database = os.path.join(path, "coastlines.json")
coastlines.to_file(coastlines_database)
dic.update({"coastlines": coastlines_database})
else:
dic.update({"coastlines": None})
dic["version"] = pyposeidon.__version__
for attr, value in dic.items():
if isinstance(value, datetime.datetime):
dic[attr] = value.isoformat()
if isinstance(value, pd.Timedelta):
dic[attr] = value.isoformat()
if isinstance(value, pd.DataFrame):
dic[attr] = value.to_dict()
if isinstance(value, gp.GeoDataFrame):
dic[attr] = value.to_json()
filename = os.path.join(path, f"{self.tag}_model.json")
json.dump(dic, open(filename, "w"), indent=4, default=myconverter)
def execute(self, **kwargs):
self.create(**kwargs)
self.output(**kwargs)
if self.monitor:
self.set_obs()
self.save(**kwargs)
self.run(**kwargs)
def read_folder(self, rfolder, **kwargs):
self.rpath = rfolder
geo = glob.glob(os.path.join(rfolder, "/geo.slf"))
cli = glob.glob(os.path.join(rfolder, "/geo.cli"))
mfiles = glob.glob(os.path.join(rfolder, "/input_wind.slf"))
mykeys = ["start_year", "start_month", "start_day"]
sdate = [self.params["opt"][x] for x in mykeys] # extract date info from param.nml
sd = "-".join(str(item) for item in sdate) # join in str
sd = pd.to_datetime(sd) # convert to datestamp