-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamic.py
1021 lines (822 loc) · 62.7 KB
/
dynamic.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
from scipy.interpolate import RegularGridInterpolator, griddata, interp1d
from scipy.integrate import solve_ivp
from tempfile import TemporaryDirectory
from subprocess import run
import datetime
import numpy as np
import netCDF4 as nc
import requests
import shutil
import os
import time
import warnings
from observation import Observation
warnings.simplefilter(action='ignore')
def forecast(observation: Observation, next_rcm_time):
rcm_datetime0 = observation.time
iceberg_lat0 = observation.lat
iceberg_lon0 = observation.lon
iceberg_length = observation.length
grounded_status = observation.grounded
use_temporary_directory = True
wgrib_path = './wgrib/'
bathy_data_path = './GEBCO_Bathymetric_Data/gebco_2024.nc'
deg_radius = 10
g = 9.80665
rho_water = 1023.6
rho_air = 1.225
omega = 7.292115e-5
Cw = 0.7867
Ca = 1.1857
C_wave = 0.6
am = 0.5
Re = 6371e3
def dist_bearing(Re, lat1, lat2, lon1, lon2):
def arccot(x):
return np.pi / 2. - np.arctan(x)
lat1 = lat1 * np.pi / 180.
lat2 = lat2 * np.pi / 180.
lon1 = lon1 * np.pi / 180.
lon2 = lon2 * np.pi / 180.
L = Re * (np.arccos((np.sin(lat1)) * (np.sin(lat2)) + (np.cos(lat1)) * (np.cos(lat2)) * (np.cos(lon2 - lon1))))
Az = (arccot(((np.cos(lat1)) * (np.tan(lat2)) - (np.sin(lat1)) * (np.cos(lon2 - lon1))) / (np.sin(lon2 - lon1)))) * 180. / np.pi
if Az < 0:
Az = Az + 360.
if Az >= 360:
Az = Az - 360.
if (lon1 == lon2) and (lat1 > lat2):
Az = 180.
if (lon1 == lon2) and (lat1 < lat2):
Az = 0.
if (lon1 == lon2) and (lat1 == lat2):
L = 0.
Az = 0.
return L, Az
def dist_course(Re, lat1, lon1, dist, course):
lat1 = lat1 * np.pi / 180.
lon1 = lon1 * np.pi / 180.
course = course * np.pi / 180.
lat2 = (np.arcsin(np.sin(lat1) * np.cos(dist / Re) + np.cos(lat1) * np.sin(dist / Re) * np.cos(course))) * 180. / np.pi
lon2 = (lon1 + np.arctan2(np.sin(course) * np.sin(dist / Re) * np.cos(lat1), np.cos(dist / Re) - np.sin(lat1) * np.sin(lat2))) * 180. / np.pi
return lat2, lon2
def iceberg_acc(iceberg_lat, iceberg_u, iceberg_v, iceberg_sail, iceberg_draft, iceberg_length, dt, am, omega, Cw, Ca, C_wave, g, rho_air, rho_water,
u_wind, v_wind, u_curr, v_curr, ssh_grad_x, ssh_grad_y, Hs, wave_dir):
iceberg_keel = iceberg_length * iceberg_draft
wind_dir = 90. - np.arctan2(v_wind, u_wind) * 180. / np.pi
if wind_dir < 0:
wind_dir = wind_dir + 360.
if np.isnan(wave_dir):
wave_dir = wind_dir
Fa_E = 0.5 * rho_air * Ca * iceberg_sail * np.sqrt((u_wind - iceberg_u) ** 2 + (v_wind - iceberg_v) ** 2) * (u_wind - iceberg_u)
Fw_E = 0.5 * rho_water * Cw * iceberg_keel * np.sqrt((u_curr[0] - iceberg_u) ** 2 + (v_curr[0] - iceberg_v) ** 2) * (u_curr[0] - iceberg_u)
f = 2. * omega * np.sin(iceberg_lat * np.pi / 180.)
Fc_E = (iceberg_mass + am * iceberg_mass) * f * iceberg_v
Fp_E = (iceberg_mass + am * iceberg_mass) * (u_curr[1] - u_curr[0]) / dt + f * v_curr[0]
Fs_E = -(iceberg_mass + am * iceberg_mass) * g * ssh_grad_x
Fr_E = 0.25 * C_wave * rho_water * g * ((0.5 * Hs) ** 2) * iceberg_length * np.sin(wave_dir * np.pi / 180.)
Fa_N = 0.5 * rho_air * Ca * iceberg_sail * np.sqrt((u_wind - iceberg_u) ** 2 + (v_wind - iceberg_v) ** 2) * (v_wind - iceberg_v)
Fw_N = 0.5 * rho_water * Cw * iceberg_keel * np.sqrt((u_curr[0] - iceberg_u) ** 2 + (v_curr[0] - iceberg_v) ** 2) * (v_curr[0] - iceberg_v)
Fc_N = -(iceberg_mass + am * iceberg_mass) * f * iceberg_u
Fp_N = (iceberg_mass + am * iceberg_mass) * (v_curr[1] - v_curr[0]) / dt - f * u_curr[0]
Fs_N = -(iceberg_mass + am * iceberg_mass) * g * ssh_grad_y
Fr_N = 0.25 * C_wave * rho_water * g * ((0.5 * Hs) ** 2) * iceberg_length * np.cos(wave_dir * np.pi / 180.)
F_sum_E = Fa_E + Fw_E + Fc_E + Fp_E + Fs_E + Fr_E
F_sum_N = Fa_N + Fw_N + Fc_N + Fp_N + Fs_N + Fr_N
ib_acc_E = F_sum_E / (iceberg_mass + am * iceberg_mass)
ib_acc_N = F_sum_N / (iceberg_mass + am * iceberg_mass)
return ib_acc_E, ib_acc_N
iceberg_u0 = 0.
iceberg_v0 = 0.
rcm_datetime0 = np.datetime64(rcm_datetime0)
if not isinstance(iceberg_length, (int, float)) or iceberg_length <= 0:
iceberg_length = 100.
iceberg_draft = 1.78 * (iceberg_length ** 0.71) # meters
iceberg_mass = 0.43 * (iceberg_length ** 2.9) * 1000. # kg
iceberg_sail = 0.077 * (iceberg_length ** 2) # m ** 2
bathy_data = nc.Dataset(bathy_data_path)
bathy_lat = bathy_data.variables['lat'][:]
bathy_lon = bathy_data.variables['lon'][:]
bathy_depth = -bathy_data.variables['elevation'][:] # lat x lon
bathy_data.close()
bathy_interp = RegularGridInterpolator((bathy_lat, bathy_lon), bathy_depth, method='linear', bounds_error=True, fill_value=np.nan)
iceberg_bathy_depth0 = bathy_interp([[iceberg_lat0, iceberg_lon0]])[0]
if grounded_status == 'not grounded' and iceberg_bathy_depth0 <= iceberg_draft:
iceberg_draft = iceberg_bathy_depth0 - 1.
next_rcm_time = np.datetime64(next_rcm_time)
forecast_time = rcm_datetime0
dirname_wind_waves = str(np.datetime64('today'))
d_wind_waves = dirname_wind_waves.replace('-', '')
dirname_curr_ssh = str(np.datetime64('today'))
d_curr_ssh = dirname_curr_ssh.replace('-', '')
if grounded_status == 'not grounded':
url = 'https://dd.meteo.gc.ca/model_gem_global/15km/grib2/lat_lon/12/240/CMC_glb_UGRD_TGL_10_latlon.15x.15_' + d_wind_waves + '12_P240.grib2'
response = requests.head(url)
if response.status_code == 200 and forecast_time >= np.datetime64(str(np.datetime64('today')) + 'T12:00:00'):
hour_utc_str_wind = '12'
else:
hour_utc_str_wind = '00'
url = 'https://dd.weather.gc.ca/model_riops/netcdf/forecast/polar_stereographic/3d/18/084/' + d_curr_ssh + 'T18Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P084.nc'
response = requests.head(url)
if response.status_code == 200 and forecast_time >= np.datetime64(str(np.datetime64('today')) + 'T18:00:00'):
hour_utc_str_curr_ssh = '18'
else:
url = 'https://dd.weather.gc.ca/model_riops/netcdf/forecast/polar_stereographic/3d/12/084/' + d_curr_ssh + 'T12Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P084.nc'
response = requests.head(url)
if response.status_code == 200 and forecast_time >= np.datetime64(str(np.datetime64('today')) + 'T12:00:00'):
hour_utc_str_curr_ssh = '12'
else:
url = ('https://dd.weather.gc.ca/model_riops/netcdf/forecast/polar_stereographic/3d/06/084/' + d_curr_ssh +
'T06Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P084.nc')
response = requests.head(url)
if response.status_code == 200 and forecast_time >= np.datetime64(str(np.datetime64('today')) + 'T06:00:00'):
hour_utc_str_curr_ssh = '06'
else:
hour_utc_str_curr_ssh = '00'
url = 'https://dd.weather.gc.ca/model_gdwps/25km/12/' + d_wind_waves + 'T12Z_MSC_GDWPS_HTSGW_Sfc_LatLon0.25_PT240H.grib2'
response = requests.head(url)
if response.status_code == 200 and forecast_time >= np.datetime64(str(np.datetime64('today')) + 'T12:00:00'):
hour_utc_str_waves = '12'
else:
hour_utc_str_waves = '00'
forecast_times_wind = []
forecast_times_waves = []
forecast_times_curr_ssh = []
forecast_start_time_wind = np.datetime64(dirname_wind_waves + 'T' + hour_utc_str_wind + ':00:00')
forecast_start_time_waves = np.datetime64(dirname_wind_waves + 'T' + hour_utc_str_waves + ':00:00')
forecast_start_time_curr_ssh = np.datetime64(dirname_curr_ssh + 'T' + hour_utc_str_curr_ssh + ':00:00')
time_count_wind = forecast_start_time_wind
time_count_waves = forecast_start_time_waves
time_count_curr_ssh = forecast_start_time_curr_ssh
while time_count_wind <= next_rcm_time + np.timedelta64(3, 'h'):
forecast_times_wind.append(time_count_wind)
time_count_wind = time_count_wind + np.timedelta64(3, 'h')
while time_count_waves <= next_rcm_time + np.timedelta64(3, 'h'):
forecast_times_waves.append(time_count_waves)
time_count_waves = time_count_waves + np.timedelta64(3, 'h')
while time_count_curr_ssh <= next_rcm_time + np.timedelta64(1, 'h'):
forecast_times_curr_ssh.append(time_count_curr_ssh)
time_count_curr_ssh = time_count_curr_ssh + np.timedelta64(1, 'h')
forecast_times_wind = np.array(forecast_times_wind, dtype='datetime64[s]')
forecast_times_waves = np.array(forecast_times_waves, dtype='datetime64[s]')
forecast_times_curr_ssh = np.array(forecast_times_curr_ssh, dtype='datetime64[s]')
forecast_times_wind_hours = (forecast_times_wind.astype('datetime64[h]') - forecast_start_time_wind.astype('datetime64[h]')).astype(int)
forecast_times_waves_hours = (forecast_times_waves.astype('datetime64[h]') - forecast_start_time_waves.astype('datetime64[h]')).astype(int)
forecast_times_curr_ssh_hours = (forecast_times_curr_ssh.astype('datetime64[h]') - forecast_start_time_curr_ssh.astype('datetime64[h]')).astype(int)
with (TemporaryDirectory() as directory):
if not use_temporary_directory:
directory = './GDPS_wind_forecast_grib2_files'
if not os.path.isdir('./GDPS_wind_forecast_grib2_files/'):
os.mkdir('./GDPS_wind_forecast_grib2_files/')
if not os.path.isdir('./GDPS_wind_forecast_netcdf_files/'):
os.mkdir('./GDPS_wind_forecast_netcdf_files/')
if not os.path.isdir('./GDWPS_wave_forecast_grib2_files/'):
os.mkdir('./GDWPS_wave_forecast_grib2_files/')
if not os.path.isdir('./GDWPS_wave_forecast_netcdf_files/'):
os.mkdir('./GDWPS_wave_forecast_netcdf_files/')
if not os.path.isdir('./RIOPS_ocean_forecast_netcdf_files/'):
os.mkdir('./RIOPS_ocean_forecast_netcdf_files/')
if not os.path.isdir(directory + '/' + dirname_wind_waves):
os.mkdir(directory + '/' + dirname_wind_waves)
for i in range(len(forecast_times_wind)):
url = 'https://dd.meteo.gc.ca/model_gem_global/15km/grib2/lat_lon/' + hour_utc_str_wind + '/' + \
str(forecast_times_wind_hours[i]).zfill(3) + '/CMC_glb_UGRD_TGL_10_latlon.15x.15_' + \
d_wind_waves + hour_utc_str_wind + '_P' + str(forecast_times_wind_hours[i]).zfill(3) + '.grib2'
fname = directory + '/' + dirname_wind_waves + '/CMC_glb_UGRD_TGL_10_latlon.15x.15_' + d_wind_waves + hour_utc_str_wind + '_P' + \
str(forecast_times_wind_hours[i]).zfill(3) + '.grib2'
flag = True
while flag:
try:
r = requests.get(url, allow_redirects=True, timeout=5.0)
open(fname, 'wb').write(r.content)
flag = False
except:
print('Error: could not download forecast zonal wind velocity file, retrying...')
fname = directory + '/' + dirname_wind_waves + '/CMC_glb_UGRD_TGL_10_latlon.15x.15_' + d_wind_waves + hour_utc_str_wind + '_P' + \
str(forecast_times_wind_hours[i]).zfill(3) + '.grib2'
run(wgrib_path + 'wgrib2.exe ' + fname + ' -netcdf ' + directory + '/' + dirname_wind_waves + '/CMC_glb_UGRD_TGL_10_latlon.15x.15_' + \
d_wind_waves + hour_utc_str_wind + '_P' + str(forecast_times_wind_hours[i]).zfill(3) + '.nc')
if not use_temporary_directory:
if not os.path.isdir('./GDPS_wind_forecast_netcdf_files/' + dirname_wind_waves):
os.mkdir('./GDPS_wind_forecast_netcdf_files/' + dirname_wind_waves)
shutil.move(fname[:-6] + '.nc', './GDPS_wind_forecast_netcdf_files/' + dirname_wind_waves + '/CMC_glb_UGRD_TGL_10_latlon.15x.15_' +
d_wind_waves + hour_utc_str_wind + '_P' + str(forecast_times_wind_hours[i]).zfill(3) + '.nc')
url = 'https://dd.meteo.gc.ca/model_gem_global/15km/grib2/lat_lon/' + hour_utc_str_wind + '/' + \
str(forecast_times_wind_hours[i]).zfill(3) + '/CMC_glb_VGRD_TGL_10_latlon.15x.15_' + \
d_wind_waves + hour_utc_str_wind + '_P' + str(forecast_times_wind_hours[i]).zfill(3) + '.grib2'
fname = directory + '/' + dirname_wind_waves + '/CMC_glb_VGRD_TGL_10_latlon.15x.15_' + d_wind_waves + hour_utc_str_wind + '_P' + \
str(forecast_times_wind_hours[i]).zfill(3) + '.grib2'
flag = True
while flag:
try:
r = requests.get(url, allow_redirects=True, timeout=5.0)
open(fname, 'wb').write(r.content)
flag = False
except:
print('Error: could not download forecast meridional wind velocity file, retrying...')
fname = directory + '/' + dirname_wind_waves + '/CMC_glb_VGRD_TGL_10_latlon.15x.15_' + d_wind_waves + hour_utc_str_wind + '_P' + \
str(forecast_times_wind_hours[i]).zfill(3) + '.grib2'
run(wgrib_path + 'wgrib2.exe ' + fname + ' -netcdf ' + directory + '/' + dirname_wind_waves + '/CMC_glb_VGRD_TGL_10_latlon.15x.15_' + \
d_wind_waves + hour_utc_str_wind + '_P' + str(forecast_times_wind_hours[i]).zfill(3) + '.nc')
if not use_temporary_directory:
if not os.path.isdir('./GDPS_wind_forecast_netcdf_files/' + dirname_wind_waves):
os.mkdir('./GDPS_wind_forecast_netcdf_files/' + dirname_wind_waves)
shutil.move(fname[:-6] + '.nc', './GDPS_wind_forecast_netcdf_files/' + dirname_wind_waves + '/CMC_glb_VGRD_TGL_10_latlon.15x.15_' +
d_wind_waves + hour_utc_str_wind + '_P' + str(forecast_times_wind_hours[i]).zfill(3) + '.nc')
if not use_temporary_directory:
directory = './GDWPS_wave_forecast_grib2_files'
if not os.path.isdir(directory + '/' + dirname_wind_waves):
os.mkdir(directory + '/' + dirname_wind_waves)
for i in range(len(forecast_times_waves)):
url = 'https://dd.weather.gc.ca/model_gdwps/25km/' + hour_utc_str_waves + '/' + d_wind_waves + 'T' + hour_utc_str_waves + \
'Z_MSC_GDWPS_HTSGW_Sfc_LatLon0.25_PT' + str(forecast_times_waves_hours[i]).zfill(3) + 'H.grib2'
fname = directory + '/' + dirname_wind_waves + '/' + d_wind_waves + 'T' + hour_utc_str_waves + \
'Z_MSC_GDWPS_HTSGW_Sfc_LatLon0.25_PT' + str(forecast_times_waves_hours[i]).zfill(3) + 'H.grib2'
flag = True
while flag:
try:
r = requests.get(url, allow_redirects=True, timeout=5.0)
open(fname, 'wb').write(r.content)
flag = False
except:
print('Error: could not download forecast significant wave height file, retrying...')
fname = directory + '/' + dirname_wind_waves + '/' + d_wind_waves + 'T' + hour_utc_str_waves + \
'Z_MSC_GDWPS_HTSGW_Sfc_LatLon0.25_PT' + str(forecast_times_waves_hours[i]).zfill(3) + 'H.grib2'
run(wgrib_path + 'wgrib2.exe ' + fname + ' -netcdf ' + directory + '/' + dirname_wind_waves + '/' + d_wind_waves + 'T' + hour_utc_str_waves + \
'Z_MSC_GDWPS_HTSGW_Sfc_LatLon0.25_PT' + str(forecast_times_waves_hours[i]).zfill(3) + 'H.nc')
if not use_temporary_directory:
if not os.path.isdir('./GDWPS_wave_forecast_netcdf_files/' + dirname_wind_waves):
os.mkdir('./GDWPS_wave_forecast_netcdf_files/' + dirname_wind_waves)
shutil.move(fname[:-6] + '.nc', './GDWPS_wave_forecast_netcdf_files/' + dirname_wind_waves + '/' + d_wind_waves + 'T' + hour_utc_str_waves + \
'Z_MSC_GDWPS_HTSGW_Sfc_LatLon0.25_PT' + str(forecast_times_waves_hours[i]).zfill(3) + 'H.nc')
url = 'https://dd.weather.gc.ca/model_gdwps/25km/' + hour_utc_str_waves + '/' + d_wind_waves + 'T' + hour_utc_str_waves + \
'Z_MSC_GDWPS_WVDIR_Sfc_LatLon0.25_PT' + str(forecast_times_waves_hours[i]).zfill(3) + 'H.grib2'
fname = directory + '/' + dirname_wind_waves + '/' + d_wind_waves + 'T' + hour_utc_str_waves + \
'Z_MSC_GDWPS_WVDIR_Sfc_LatLon0.25_PT' + str(forecast_times_waves_hours[i]).zfill(3) + 'H.grib2'
flag = True
while flag:
try:
r = requests.get(url, allow_redirects=True, timeout=5.0)
open(fname, 'wb').write(r.content)
flag = False
except:
print('Error: could not download forecast wave direction file, retrying...')
fname = directory + '/' + dirname_wind_waves + '/' + d_wind_waves + 'T' + hour_utc_str_waves + \
'Z_MSC_GDWPS_WVDIR_Sfc_LatLon0.25_PT' + str(forecast_times_waves_hours[i]).zfill(3) + 'H.grib2'
run(wgrib_path + 'wgrib2.exe ' + fname + ' -netcdf ' + directory + '/' + dirname_wind_waves + '/' + d_wind_waves + 'T' + hour_utc_str_waves + \
'Z_MSC_GDWPS_WVDIR_Sfc_LatLon0.25_PT' + str(forecast_times_waves_hours[i]).zfill(3) + 'H.nc')
if not use_temporary_directory:
if not os.path.isdir('./GDWPS_wave_forecast_netcdf_files/' + dirname_wind_waves):
os.mkdir('./GDWPS_wave_forecast_netcdf_files/' + dirname_wind_waves)
shutil.move(fname[:-6] + '.nc', './GDWPS_wave_forecast_netcdf_files/' + dirname_wind_waves + '/' + d_wind_waves + 'T' + hour_utc_str_waves + \
'Z_MSC_GDWPS_WVDIR_Sfc_LatLon0.25_PT' + str(forecast_times_waves_hours[i]).zfill(3) + 'H.nc')
if not use_temporary_directory:
directory = './RIOPS_ocean_forecast_netcdf_files'
if not os.path.isdir(directory + '/' + dirname_curr_ssh):
os.mkdir(directory + '/' + dirname_curr_ssh)
for i in range(len(forecast_times_curr_ssh)):
url = 'https://dd.weather.gc.ca/model_riops/netcdf/forecast/polar_stereographic/3d/' + hour_utc_str_curr_ssh + '/' + \
str(forecast_times_curr_ssh_hours[i]).zfill(3) + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
fname = directory + '/' + dirname_curr_ssh + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
flag = True
while flag:
try:
r = requests.get(url, allow_redirects=True, timeout=5.0)
open(fname, 'wb').write(r.content)
flag = False
except:
print('Error: could not download forecast zonal ocean current file, retrying...')
url = 'https://dd.weather.gc.ca/model_riops/netcdf/forecast/polar_stereographic/3d/' + hour_utc_str_curr_ssh + '/' + \
str(forecast_times_curr_ssh_hours[i]).zfill(3) + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_VOMECRTY_DBS-all_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
fname = directory + '/' + dirname_curr_ssh + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_VOMECRTY_DBS-all_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
flag = True
while flag:
try:
r = requests.get(url, allow_redirects=True, timeout=5.0)
open(fname, 'wb').write(r.content)
flag = False
except:
print('Error: could not download forecast meridional ocean current file, retrying...')
url = 'https://dd.weather.gc.ca/model_riops/netcdf/forecast/polar_stereographic/2d/' + hour_utc_str_curr_ssh + '/' + \
str(forecast_times_curr_ssh_hours[i]).zfill(3) + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_SOSSHEIG_SFC_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
fname = directory + '/' + dirname_curr_ssh + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_SOSSHEIG_SFC_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
flag = True
while flag:
try:
r = requests.get(url, allow_redirects=True, timeout=5.0)
open(fname, 'wb').write(r.content)
flag = False
except:
print('Error: could not download forecast sea surface height file, retrying...')
for i in range(len(forecast_times_curr_ssh)):
fname = directory + '/' + dirname_curr_ssh + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_SOSSHEIG_SFC_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
ssh_data = nc.Dataset(fname)
curr_ssh_lat = ssh_data.variables['latitude'][:] # lat x lon
curr_ssh_lon = ssh_data.variables['longitude'][:] # lat x lon
ssh = np.squeeze(ssh_data.variables['sossheig'][:]) # lat x lon
ssh_data.close()
fname = directory + '/' + dirname_curr_ssh + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
u_curr_data = nc.Dataset(fname)
depth_curr = u_curr_data.variables['depth'][:]
u_curr = np.squeeze(u_curr_data.variables['vozocrtx'][:]) # depth x lat x lon
u_curr_data.close()
fname = directory + '/' + dirname_curr_ssh + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_VOMECRTY_DBS-all_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
v_curr_data = nc.Dataset(fname)
v_curr = np.squeeze(v_curr_data.variables['vomecrty'][:]) # depth x lat x lon
v_curr_data.close()
distance = np.sqrt((curr_ssh_lat - iceberg_lat0) ** 2 + (curr_ssh_lon - (iceberg_lon0 + 360.)) ** 2)
# Find the indices of the nearest grid point
nearest_idx = np.unravel_index(np.argmin(distance, axis=None), distance.shape)
nearest_lat_idx, nearest_lon_idx = nearest_idx
# Define the index range to restrict to within 10 indices in all directions
lat_min = max(nearest_lat_idx - deg_radius, 0)
lat_max = min(nearest_lat_idx + deg_radius, curr_ssh_lat.shape[0] - 1)
lon_min = max(nearest_lon_idx - deg_radius, 0)
lon_max = min(nearest_lon_idx + deg_radius, curr_ssh_lon.shape[1] - 1)
# Slice the lat, lon, and ssh arrays within the 10x10 index range
curr_ssh_lat = curr_ssh_lat[lat_min:lat_max + 1, lon_min:lon_max + 1]
curr_ssh_lon = curr_ssh_lon[lat_min:lat_max + 1, lon_min:lon_max + 1]
ssh = ssh[lat_min:lat_max + 1, lon_min:lon_max + 1]
u_curr = u_curr[:, lat_min:lat_max + 1, lon_min:lon_max + 1]
v_curr = v_curr[:, lat_min:lat_max + 1, lon_min:lon_max + 1]
ssh_grad_x = np.empty((len(curr_ssh_lat[:, 0]), len(curr_ssh_lon[0, :]) - 1))
ssh_grad_y = np.empty((len(curr_ssh_lat[:, 0]) - 1, len(curr_ssh_lon[0, :])))
ssh_grad_x_lat = np.empty((len(curr_ssh_lat[:, 0]), len(curr_ssh_lon[0, :]) - 1))
ssh_grad_y_lat = np.empty((len(curr_ssh_lat[:, 0]) - 1, len(curr_ssh_lon[0, :])))
ssh_grad_x_lon = np.empty((len(curr_ssh_lat[:, 0]), len(curr_ssh_lon[0, :]) - 1))
ssh_grad_y_lon = np.empty((len(curr_ssh_lat[:, 0]) - 1, len(curr_ssh_lon[0, :])))
for k in range(len(curr_ssh_lat[:, 0])):
for n in range(len(curr_ssh_lon[0, :]) - 1):
grid_pt_dist, grid_pt_bearing = dist_bearing(Re, curr_ssh_lat[k, n], curr_ssh_lat[k, n + 1], curr_ssh_lon[k, n], curr_ssh_lon[k, n + 1])
ssh_grad_x_lat[k, n], ssh_grad_x_lon[k, n] = dist_course(Re, curr_ssh_lat[k, n], curr_ssh_lon[k, n], grid_pt_dist / 2., grid_pt_bearing)
ssh_grad = (ssh[k, n + 1] - ssh[k, n]) / grid_pt_dist
ssh_grad_x[k, n] = ssh_grad * np.sin(grid_pt_bearing * np.pi / 180.)
for k in range(len(curr_ssh_lat[:, 0]) - 1):
for n in range(len(curr_ssh_lon[0, :])):
grid_pt_dist, grid_pt_bearing = dist_bearing(Re, curr_ssh_lat[k, n], curr_ssh_lat[k + 1, n], curr_ssh_lon[k, n], curr_ssh_lon[k + 1, n])
ssh_grad_y_lat[k, n], ssh_grad_y_lon[k, n] = dist_course(Re, curr_ssh_lat[k, n], curr_ssh_lon[k, n], grid_pt_dist / 2., grid_pt_bearing)
ssh_grad = (ssh[k + 1, n] - ssh[k, n]) / grid_pt_dist
ssh_grad_y[k, n] = ssh_grad * np.cos(grid_pt_bearing * np.pi / 180.)
fname = directory + '/' + dirname_curr_ssh + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_SOSSHEIG_SFC_GRAD_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
with nc.Dataset(fname, 'w', format='NETCDF4') as ncfile:
ncfile.createDimension('x_gradient_latitude', len(curr_ssh_lat[:, 0]))
ncfile.createDimension('x_gradient_longitude', len(curr_ssh_lon[0, :]) - 1)
ncfile.createDimension('y_gradient_latitude', len(curr_ssh_lat[:, 0]) - 1)
ncfile.createDimension('y_gradient_longitude', len(curr_ssh_lon[0, :]))
x_gradient_latitude_var = ncfile.createVariable('ssh_grad_x_lat', 'f8',
('x_gradient_latitude', 'x_gradient_longitude',))
x_gradient_longitude_var = ncfile.createVariable('ssh_grad_x_lon', 'f8',
('x_gradient_latitude', 'x_gradient_longitude',))
y_gradient_latitude_var = ncfile.createVariable('ssh_grad_y_lat', 'f8',
('y_gradient_latitude', 'y_gradient_longitude',))
y_gradient_longitude_var = ncfile.createVariable('ssh_grad_y_lon', 'f8',
('y_gradient_latitude', 'y_gradient_longitude',))
ssh_grad_x_var = ncfile.createVariable('ssh_grad_x', 'f4', ('x_gradient_latitude', 'x_gradient_longitude',))
ssh_grad_y_var = ncfile.createVariable('ssh_grad_y', 'f4', ('y_gradient_latitude', 'y_gradient_longitude',))
x_gradient_latitude_var[:] = ssh_grad_x_lat
x_gradient_longitude_var[:] = ssh_grad_x_lon
y_gradient_latitude_var[:] = ssh_grad_y_lat
y_gradient_longitude_var[:] = ssh_grad_y_lon
ssh_grad_x_var[:] = ssh_grad_x
ssh_grad_y_var[:] = ssh_grad_y
fname = directory + '/' + dirname_curr_ssh + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
with nc.Dataset(fname, 'w', format='NETCDF4') as ncfile:
ncfile.createDimension('latitude', len(curr_ssh_lat[:, 0]))
ncfile.createDimension('longitude', len(curr_ssh_lon[0, :]))
ncfile.createDimension('depth', len(depth_curr))
latitude_var = ncfile.createVariable('latitude', 'f8', ('latitude', 'longitude',))
longitude_var = ncfile.createVariable('longitude', 'f8', ('latitude', 'longitude',))
depth_var = ncfile.createVariable('depth', 'f8', ('depth',))
u_curr_var = ncfile.createVariable('vozocrtx', 'f4', ('depth', 'latitude', 'longitude',))
latitude_var[:] = curr_ssh_lat
longitude_var[:] = curr_ssh_lon
depth_var[:] = depth_curr
u_curr_var[:] = u_curr
fname = directory + '/' + dirname_curr_ssh + '/' + d_curr_ssh + 'T' + hour_utc_str_curr_ssh + \
'Z_MSC_RIOPS_VOMECRTY_DBS-all_PS5km_P' + str(forecast_times_curr_ssh_hours[i]).zfill(3) + '.nc'
with nc.Dataset(fname, 'w', format='NETCDF4') as ncfile:
ncfile.createDimension('latitude', len(curr_ssh_lat[:, 0]))
ncfile.createDimension('longitude', len(curr_ssh_lon[0, :]))
ncfile.createDimension('depth', len(depth_curr))
latitude_var = ncfile.createVariable('latitude', 'f8', ('latitude', 'longitude',))
longitude_var = ncfile.createVariable('longitude', 'f8', ('latitude', 'longitude',))
depth_var = ncfile.createVariable('depth', 'f8', ('depth',))
v_curr_var = ncfile.createVariable('vomecrty', 'f4', ('depth', 'latitude', 'longitude',))
latitude_var[:] = curr_ssh_lat
longitude_var[:] = curr_ssh_lon
depth_var[:] = depth_curr
v_curr_var[:] = v_curr
iceberg_times = [forecast_time]
# Add hourly intervals until close to the end time
current_time = forecast_time
while current_time + np.timedelta64(1, 'h') < next_rcm_time:
current_time += np.timedelta64(1, 'h')
iceberg_times.append(current_time)
# Append the exact end time for the remainder interval
iceberg_times.append(next_rcm_time)
# Convert to list for easier inspection, if desired
iceberg_times = list(iceberg_times)
iceberg_times_dt = [float((iceberg_times[i + 1] - iceberg_times[i]) / np.timedelta64(1, 's')) for i in range(len(iceberg_times) - 1)]
# Convert to list for easier inspection if desired
iceberg_times_dt = list(iceberg_times_dt)
iceberg_lats = np.empty((len(iceberg_times),))
iceberg_lons = np.empty((len(iceberg_times),))
iceberg_us = np.empty((len(iceberg_times),))
iceberg_vs = np.empty((len(iceberg_times),))
iceberg_lats[0] = iceberg_lat0
iceberg_lons[0] = iceberg_lon0
iceberg_us[0] = iceberg_u0
iceberg_vs[0] = iceberg_v0
for i in range(len(iceberg_times) - 1):
iceberg_lat = iceberg_lats[i]
iceberg_lon = iceberg_lons[i]
iceberg_u = iceberg_us[i]
iceberg_v = iceberg_vs[i]
iceberg_time = iceberg_times[i]
iceberg_time2 = iceberg_times[i + 1]
# The base time of the NetCDF files
base_time = forecast_times_wind[0]
# File time increments in hours (Pxxx values)
time_increments = np.arange(forecast_times_wind_hours[0], forecast_times_wind_hours[-1], 3)
file_times = base_time + time_increments.astype('timedelta64[h]')
# Find the time just before and just after the forecast_time
before_idx = np.where(file_times <= iceberg_time)[0][-1]
try:
after_idx = np.where(file_times > iceberg_time)[0][0]
except:
after_idx = -1
# The corresponding NetCDF files
date_only = str(base_time.astype('datetime64[D]')).replace('-', '')
u_wind_file_before = 'CMC_glb_UGRD_TGL_10_latlon.15x.15_' + date_only + hour_utc_str_wind + '_P' + str(time_increments[before_idx]).zfill(3) + '.nc'
u_wind_file_after = 'CMC_glb_UGRD_TGL_10_latlon.15x.15_' + date_only + hour_utc_str_wind + '_P' + str(time_increments[after_idx]).zfill(3) + '.nc'
v_wind_file_before = 'CMC_glb_VGRD_TGL_10_latlon.15x.15_' + date_only + hour_utc_str_wind + '_P' + str(time_increments[before_idx]).zfill(3) + '.nc'
v_wind_file_after = 'CMC_glb_VGRD_TGL_10_latlon.15x.15_' + date_only + hour_utc_str_wind + '_P' + str(time_increments[after_idx]).zfill(3) + '.nc'
forecast_time_wind_before = forecast_times_wind[before_idx]
forecast_time_wind_after = forecast_times_wind[after_idx]
base_time = forecast_times_waves[0]
# File time increments in hours (Pxxx values)
time_increments = np.arange(forecast_times_waves_hours[0], forecast_times_waves_hours[-1], 3)
file_times = base_time + time_increments.astype('timedelta64[h]')
# Find the time just before and just after the forecast_time
before_idx = np.where(file_times <= iceberg_time)[0][-1]
try:
after_idx = np.where(file_times > iceberg_time)[0][0]
except:
after_idx = -1
# The corresponding NetCDF files
date_only = str(base_time.astype('datetime64[D]')).replace('-', '')
Hs_file_before = date_only + 'T' + hour_utc_str_waves + 'Z_MSC_GDWPS_HTSGW_Sfc_LatLon0.25_PT' + \
str(time_increments[before_idx]).zfill(3) + 'H.nc'
Hs_file_after = date_only + 'T' + hour_utc_str_waves + 'Z_MSC_GDWPS_HTSGW_Sfc_LatLon0.25_PT' + \
str(time_increments[after_idx]).zfill(3) + 'H.nc'
wave_dir_file_before = date_only + 'T' + hour_utc_str_waves + 'Z_MSC_GDWPS_WVDIR_Sfc_LatLon0.25_PT' + \
str(time_increments[before_idx]).zfill(3) + 'H.nc'
wave_dir_file_after = date_only + 'T' + hour_utc_str_waves + 'Z_MSC_GDWPS_WVDIR_Sfc_LatLon0.25_PT' + \
str(time_increments[after_idx]).zfill(3) + 'H.nc'
forecast_time_waves_before = forecast_times_waves[before_idx]
forecast_time_waves_after = forecast_times_waves[after_idx]
base_time = forecast_times_curr_ssh[0]
# File time increments in hours (Pxxx values)
time_increments = np.arange(forecast_times_curr_ssh_hours[0], forecast_times_curr_ssh_hours[-1], 1)
file_times = base_time + time_increments.astype('timedelta64[h]')
# Find the time just before and just after the forecast_time
before_idx = np.where(file_times <= iceberg_time)[0][-1]
try:
after_idx = np.where(file_times > iceberg_time)[0][0]
except:
after_idx = -1
# The corresponding NetCDF files
date_only = str(base_time.astype('datetime64[D]')).replace('-', '')
u_curr_file_before = date_only + 'T' + hour_utc_str_curr_ssh + 'Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P' + \
str(time_increments[before_idx]).zfill(3) + '.nc'
u_curr_file_after = date_only + 'T' + hour_utc_str_curr_ssh + 'Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P' + \
str(time_increments[after_idx]).zfill(3) + '.nc'
v_curr_file_before = date_only + 'T' + hour_utc_str_curr_ssh + 'Z_MSC_RIOPS_VOMECRTY_DBS-all_PS5km_P' + \
str(time_increments[before_idx]).zfill(3) + '.nc'
v_curr_file_after = date_only + 'T' + hour_utc_str_curr_ssh + 'Z_MSC_RIOPS_VOMECRTY_DBS-all_PS5km_P' + \
str(time_increments[after_idx]).zfill(3) + '.nc'
ssh_grad_file_before = date_only + 'T' + hour_utc_str_curr_ssh + 'Z_MSC_RIOPS_SOSSHEIG_SFC_GRAD_PS5km_P' + \
str(time_increments[before_idx]).zfill(3) + '.nc'
ssh_grad_file_after = date_only + 'T' + hour_utc_str_curr_ssh + 'Z_MSC_RIOPS_SOSSHEIG_SFC_GRAD_PS5km_P' + \
str(time_increments[after_idx]).zfill(3) + '.nc'
forecast_time_curr_ssh_before = forecast_times_curr_ssh[before_idx]
forecast_time_curr_ssh_after = forecast_times_curr_ssh[after_idx]
before_idx = np.where(file_times <= iceberg_time2)[0][-1]
try:
after_idx = np.where(file_times > iceberg_time2)[0][0]
except:
after_idx = -1
# The corresponding NetCDF files
u_curr_file_before2 = date_only + 'T' + hour_utc_str_curr_ssh + 'Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P' + \
str(time_increments[before_idx]).zfill(3) + '.nc'
u_curr_file_after2 = date_only + 'T' + hour_utc_str_curr_ssh + 'Z_MSC_RIOPS_VOZOCRTX_DBS-all_PS5km_P' + \
str(time_increments[after_idx]).zfill(3) + '.nc'
v_curr_file_before2 = date_only + 'T' + hour_utc_str_curr_ssh + 'Z_MSC_RIOPS_VOMECRTY_DBS-all_PS5km_P' + \
str(time_increments[before_idx]).zfill(3) + '.nc'
v_curr_file_after2 = date_only + 'T' + hour_utc_str_curr_ssh + 'Z_MSC_RIOPS_VOMECRTY_DBS-all_PS5km_P' + \
str(time_increments[after_idx]).zfill(3) + '.nc'
forecast_time_curr_ssh_before2 = forecast_times_curr_ssh[before_idx]
forecast_time_curr_ssh_after2 = forecast_times_curr_ssh[after_idx]
if not use_temporary_directory:
directory = './GDPS_wind_forecast_netcdf_files'
fname = directory + '/' + dirname_wind_waves + '/' + u_wind_file_before
u_wind_data_before = nc.Dataset(fname)
lat_wind = u_wind_data_before.variables['latitude'][:]
lon_wind = u_wind_data_before.variables['longitude'][:]
u_wind_before = np.squeeze(u_wind_data_before.variables['UGRD_10maboveground'][:]) # lat x lon
u_wind_data_before.close()
fname = directory + '/' + dirname_wind_waves + '/' + u_wind_file_after
u_wind_data_after = nc.Dataset(fname)
u_wind_after = np.squeeze(u_wind_data_after.variables['UGRD_10maboveground'][:]) # lat x lon
u_wind_data_after.close()
f_u_wind_before = RegularGridInterpolator((lat_wind, lon_wind), u_wind_before, method='linear', bounds_error=True, fill_value=np.nan)
f_u_wind_after = RegularGridInterpolator((lat_wind, lon_wind), u_wind_after, method='linear', bounds_error=True, fill_value=np.nan)
u_wind_before_ib = float(f_u_wind_before([iceberg_lat, iceberg_lon]))
u_wind_after_ib = float(f_u_wind_after([iceberg_lat, iceberg_lon]))
t1 = (forecast_time_wind_before - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
t2 = (forecast_time_wind_after - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
t_new = (iceberg_time - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
# Calculate the interpolation weight
weight = (t_new - t1) / (t2 - t1)
# Perform the linear interpolation for the scalar value
u_wind_ib = u_wind_before_ib + weight * (u_wind_after_ib - u_wind_before_ib)
fname = directory + '/' + dirname_wind_waves + '/' + v_wind_file_before
v_wind_data_before = nc.Dataset(fname)
lat_wind = v_wind_data_before.variables['latitude'][:]
lon_wind = v_wind_data_before.variables['longitude'][:]
v_wind_before = np.squeeze(v_wind_data_before.variables['VGRD_10maboveground'][:]) # lat x lon
v_wind_data_before.close()
fname = directory + '/' + dirname_wind_waves + '/' + v_wind_file_after
v_wind_data_after = nc.Dataset(fname)
v_wind_after = np.squeeze(v_wind_data_after.variables['VGRD_10maboveground'][:]) # lat x lon
v_wind_data_after.close()
f_v_wind_before = RegularGridInterpolator((lat_wind, lon_wind), v_wind_before, method='linear', bounds_error=True, fill_value=np.nan)
f_v_wind_after = RegularGridInterpolator((lat_wind, lon_wind), v_wind_after, method='linear', bounds_error=True, fill_value=np.nan)
v_wind_before_ib = float(f_v_wind_before([iceberg_lat, iceberg_lon]))
v_wind_after_ib = float(f_v_wind_after([iceberg_lat, iceberg_lon]))
# Perform the linear interpolation for the scalar value
v_wind_ib = v_wind_before_ib + weight * (v_wind_after_ib - v_wind_before_ib)
if not use_temporary_directory:
directory = './GDWPS_wave_forecast_netcdf_files'
fname = directory + '/' + dirname_wind_waves + '/' + Hs_file_before
Hs_data_before = nc.Dataset(fname)
lat_waves = Hs_data_before.variables['latitude'][:]
lon_waves = Hs_data_before.variables['longitude'][:]
Hs_before = np.squeeze(Hs_data_before.variables['HTSGW_surface'][:]) # lat x lon
Hs_data_before.close()
fname = directory + '/' + dirname_wind_waves + '/' + Hs_file_after
Hs_data_after = nc.Dataset(fname)
Hs_after = np.squeeze(Hs_data_after.variables['HTSGW_surface'][:]) # lat x lon
Hs_data_after.close()
f_Hs_before = RegularGridInterpolator((lat_waves, lon_waves), Hs_before, method='nearest', bounds_error=True, fill_value=np.nan)
f_Hs_after = RegularGridInterpolator((lat_waves, lon_waves), Hs_after, method='nearest', bounds_error=True, fill_value=np.nan)
Hs_before_ib = float(f_Hs_before([iceberg_lat, iceberg_lon + 360.]))
Hs_after_ib = float(f_Hs_after([iceberg_lat, iceberg_lon + 360.]))
t1 = (forecast_time_waves_before - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
t2 = (forecast_time_waves_after - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
# Calculate the interpolation weight
weight = (t_new - t1) / (t2 - t1)
# Perform the linear interpolation for the scalar value
Hs_ib = Hs_before_ib + weight * (Hs_after_ib - Hs_before_ib)
fname = directory + '/' + dirname_wind_waves + '/' + wave_dir_file_before
wave_dir_data_before = nc.Dataset(fname)
wave_dir_before = np.squeeze(wave_dir_data_before.variables['WVDIR_surface'][:]) # lat x lon
wave_dir_data_before.close()
fname = directory + '/' + dirname_wind_waves + '/' + wave_dir_file_after
wave_dir_data_after = nc.Dataset(fname)
wave_dir_after = np.squeeze(wave_dir_data_after.variables['WVDIR_surface'][:]) # lat x lon
wave_dir_data_after.close()
wave_E_before = np.sin(wave_dir_before * np.pi / 180.)
wave_E_after = np.sin(wave_dir_after * np.pi / 180.)
wave_N_before = np.cos(wave_dir_before * np.pi / 180.)
wave_N_after = np.cos(wave_dir_after * np.pi / 180.)
f_wave_E_before = RegularGridInterpolator((lat_waves, lon_waves), wave_E_before, method='nearest', bounds_error=True, fill_value=np.nan)
f_wave_E_after = RegularGridInterpolator((lat_waves, lon_waves), wave_E_after, method='nearest', bounds_error=True, fill_value=np.nan)
wave_E_before_ib = float(f_wave_E_before([iceberg_lat, iceberg_lon + 360.]))
wave_E_after_ib = float(f_wave_E_after([iceberg_lat, iceberg_lon + 360.]))
f_wave_N_before = RegularGridInterpolator((lat_waves, lon_waves), wave_N_before, method='nearest', bounds_error=True, fill_value=np.nan)
f_wave_N_after = RegularGridInterpolator((lat_waves, lon_waves), wave_N_after, method='nearest', bounds_error=True, fill_value=np.nan)
wave_N_before_ib = float(f_wave_N_before([iceberg_lat, iceberg_lon + 360.]))
wave_N_after_ib = float(f_wave_N_after([iceberg_lat, iceberg_lon + 360.]))
wave_E_ib = wave_E_before_ib + weight * (wave_E_after_ib - wave_E_before_ib)
wave_N_ib = wave_N_before_ib + weight * (wave_N_after_ib - wave_N_before_ib)
wave_dir_ib = 90. - np.arctan2(wave_N_ib, wave_E_ib) * 180. / np.pi
if wave_dir_ib < 0:
wave_dir_ib = wave_dir_ib + 360.
if not use_temporary_directory:
directory = './RIOPS_ocean_forecast_netcdf_files'
fname = directory + '/' + dirname_curr_ssh + '/' + u_curr_file_before
u_curr_data_before = nc.Dataset(fname)
lat_curr = u_curr_data_before.variables['latitude'][:] # lat x lon
lon_curr = u_curr_data_before.variables['longitude'][:] # lat x lon
depth_curr = u_curr_data_before.variables['depth'][:]
u_curr_before = np.squeeze(u_curr_data_before.variables['vozocrtx'][:]) # depth x lat x lon
u_curr_data_before.close()
fname = directory + '/' + dirname_curr_ssh + '/' + u_curr_file_after
u_curr_data_after = nc.Dataset(fname)
u_curr_after = np.squeeze(u_curr_data_after.variables['vozocrtx'][:]) # depth x lat x lon
u_curr_data_after.close()
fname = directory + '/' + dirname_curr_ssh + '/' + v_curr_file_before
v_curr_data_before = nc.Dataset(fname)
v_curr_before = np.squeeze(v_curr_data_before.variables['vomecrty'][:]) # depth x lat x lon
v_curr_data_before.close()
fname = directory + '/' + dirname_curr_ssh + '/' + v_curr_file_after
v_curr_data_after = nc.Dataset(fname)
v_curr_after = np.squeeze(v_curr_data_after.variables['vomecrty'][:]) # depth x lat x lon
v_curr_data_after.close()
loc_depth = np.argwhere(depth_curr <= iceberg_draft)
loc_depth = np.append(loc_depth, loc_depth[-1] + 1)
depth_curr_ib = depth_curr[loc_depth]
depth_curr_ib_interp = np.arange(0., iceberg_draft, 0.001)
u_curr_before = u_curr_before[loc_depth, :, :]
u_curr_after = u_curr_after[loc_depth, :, :]
v_curr_before = v_curr_before[loc_depth, :, :]
v_curr_after = v_curr_after[loc_depth, :, :]
points_curr = np.array([lat_curr.ravel(), lon_curr.ravel()]).T # Shape (n_points, 2)
u_curr_before_depth_list = []
u_curr_after_depth_list = []
v_curr_before_depth_list = []
v_curr_after_depth_list = []
for n in range(len(depth_curr_ib)):
u_curr_before_select = np.squeeze(u_curr_before[n, :, :])
u_curr_after_select = np.squeeze(u_curr_after[n, :, :])
u_curr_before_temp = griddata(points_curr, u_curr_before_select.ravel(),(iceberg_lat, iceberg_lon + 360.), method='linear')
u_curr_after_temp = griddata(points_curr, u_curr_after_select.ravel(),(iceberg_lat, iceberg_lon + 360.), method='linear')
u_curr_before_depth_list.append(u_curr_before_temp)
u_curr_after_depth_list.append(u_curr_after_temp)
v_curr_before_select = np.squeeze(v_curr_before[n, :, :])
v_curr_after_select = np.squeeze(v_curr_after[n, :, :])
v_curr_before_temp = griddata(points_curr, v_curr_before_select.ravel(),(iceberg_lat, iceberg_lon + 360.), method='linear')
v_curr_after_temp = griddata(points_curr, v_curr_after_select.ravel(),(iceberg_lat, iceberg_lon + 360.), method='linear')
v_curr_before_depth_list.append(v_curr_before_temp)
v_curr_after_depth_list.append(v_curr_after_temp)
u_curr_before_depth_list = [float(val) for val in u_curr_before_depth_list]
u_curr_after_depth_list = [float(val) for val in u_curr_after_depth_list]
interp_func = interp1d(depth_curr_ib, u_curr_before_depth_list, kind='linear', fill_value='extrapolate')
u_curr_before_depth_list = interp_func(depth_curr_ib_interp)
interp_func = interp1d(depth_curr_ib, u_curr_after_depth_list, kind='linear', fill_value='extrapolate')
u_curr_after_depth_list = interp_func(depth_curr_ib_interp)
u_curr_before_ib = np.nanmean(u_curr_before_depth_list)
u_curr_after_ib = np.nanmean(u_curr_after_depth_list)
v_curr_before_depth_list = [float(val) for val in v_curr_before_depth_list]
v_curr_after_depth_list = [float(val) for val in v_curr_after_depth_list]
interp_func = interp1d(depth_curr_ib, v_curr_before_depth_list, kind='linear', fill_value='extrapolate')
v_curr_before_depth_list = interp_func(depth_curr_ib_interp)
interp_func = interp1d(depth_curr_ib, v_curr_after_depth_list, kind='linear', fill_value='extrapolate')
v_curr_after_depth_list = interp_func(depth_curr_ib_interp)
v_curr_before_ib = np.nanmean(v_curr_before_depth_list)
v_curr_after_ib = np.nanmean(v_curr_after_depth_list)
t1 = (forecast_time_curr_ssh_before - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
t2 = (forecast_time_curr_ssh_after - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
# Calculate the interpolation weight
weight = (t_new - t1) / (t2 - t1)
# Perform the linear interpolation for the scalar value
u_curr_ib = u_curr_before_ib + weight * (u_curr_after_ib - u_curr_before_ib)
v_curr_ib = v_curr_before_ib + weight * (v_curr_after_ib - v_curr_before_ib)
fname = directory + '/' + dirname_curr_ssh + '/' + u_curr_file_before2
u_curr_data_before2 = nc.Dataset(fname)
lat_curr = u_curr_data_before2.variables['latitude'][:] # lat x lon
lon_curr = u_curr_data_before2.variables['longitude'][:] # lat x lon
depth_curr = u_curr_data_before2.variables['depth'][:]
u_curr_before2 = np.squeeze(u_curr_data_before2.variables['vozocrtx'][:]) # depth x lat x lon
u_curr_data_before2.close()
fname = directory + '/' + dirname_curr_ssh + '/' + u_curr_file_after2
u_curr_data_after2 = nc.Dataset(fname)
u_curr_after2 = np.squeeze(u_curr_data_after2.variables['vozocrtx'][:]) # depth x lat x lon
u_curr_data_after2.close()
fname = directory + '/' + dirname_curr_ssh + '/' + v_curr_file_before2
v_curr_data_before2 = nc.Dataset(fname)
v_curr_before2 = np.squeeze(v_curr_data_before2.variables['vomecrty'][:]) # depth x lat x lon
v_curr_data_before2.close()
fname = directory + '/' + dirname_curr_ssh + '/' + v_curr_file_after2
v_curr_data_after2 = nc.Dataset(fname)
v_curr_after2 = np.squeeze(v_curr_data_after2.variables['vomecrty'][:]) # depth x lat x lon
v_curr_data_after2.close()
loc_depth = np.argwhere(depth_curr <= iceberg_draft)
loc_depth = np.append(loc_depth, loc_depth[-1] + 1)
depth_curr_ib = depth_curr[loc_depth]
depth_curr_ib_interp = np.arange(0., iceberg_draft, 0.001)
u_curr_before2 = u_curr_before2[loc_depth, :, :]
u_curr_after2 = u_curr_after2[loc_depth, :, :]
v_curr_before2 = v_curr_before2[loc_depth, :, :]
v_curr_after2 = v_curr_after2[loc_depth, :, :]
points_curr = np.array([lat_curr.ravel(), lon_curr.ravel()]).T # Shape (n_points, 2)
u_curr_before2_depth_list = []
u_curr_after2_depth_list = []
v_curr_before2_depth_list = []
v_curr_after2_depth_list = []
for n in range(len(depth_curr_ib)):
u_curr_before2_select = np.squeeze(u_curr_before2[n, :, :])
u_curr_after2_select = np.squeeze(u_curr_after2[n, :, :])
u_curr_before2_temp = griddata(points_curr, u_curr_before2_select.ravel(),(iceberg_lat, iceberg_lon + 360.), method='linear')
u_curr_after2_temp = griddata(points_curr, u_curr_after2_select.ravel(), (iceberg_lat, iceberg_lon + 360.), method='linear')
u_curr_before2_depth_list.append(u_curr_before2_temp)
u_curr_after2_depth_list.append(u_curr_after2_temp)
v_curr_before2_select = np.squeeze(v_curr_before2[n, :, :])
v_curr_after2_select = np.squeeze(v_curr_after2[n, :, :])
v_curr_before2_temp = griddata(points_curr, v_curr_before2_select.ravel(),(iceberg_lat, iceberg_lon + 360.), method='linear')
v_curr_after2_temp = griddata(points_curr, v_curr_after2_select.ravel(), (iceberg_lat, iceberg_lon + 360.), method='linear')
v_curr_before2_depth_list.append(v_curr_before2_temp)
v_curr_after2_depth_list.append(v_curr_after2_temp)
u_curr_before2_depth_list = [float(val) for val in u_curr_before2_depth_list]
u_curr_after2_depth_list = [float(val) for val in u_curr_after2_depth_list]
interp_func = interp1d(depth_curr_ib, u_curr_before2_depth_list, kind='linear', fill_value='extrapolate')
u_curr_before2_depth_list = interp_func(depth_curr_ib_interp)
interp_func = interp1d(depth_curr_ib, u_curr_after2_depth_list, kind='linear', fill_value='extrapolate')
u_curr_after2_depth_list = interp_func(depth_curr_ib_interp)
u_curr_before2_ib = np.nanmean(u_curr_before2_depth_list)
u_curr_after2_ib = np.nanmean(u_curr_after2_depth_list)
v_curr_before2_depth_list = [float(val) for val in v_curr_before2_depth_list]
v_curr_after2_depth_list = [float(val) for val in v_curr_after2_depth_list]
interp_func = interp1d(depth_curr_ib, v_curr_before2_depth_list, kind='linear', fill_value='extrapolate')
v_curr_before2_depth_list = interp_func(depth_curr_ib_interp)
interp_func = interp1d(depth_curr_ib, v_curr_after2_depth_list, kind='linear', fill_value='extrapolate')
v_curr_after2_depth_list = interp_func(depth_curr_ib_interp)
v_curr_before2_ib = np.nanmean(v_curr_before2_depth_list)
v_curr_after2_ib = np.nanmean(v_curr_after2_depth_list)
t1 = (forecast_time_curr_ssh_before2 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
t2 = (forecast_time_curr_ssh_after2 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
# Calculate the interpolation weight
weight = (t_new - t1) / (t2 - t1)
# Perform the linear interpolation for the scalar value
u_curr_ib2 = u_curr_before2_ib + weight * (u_curr_after2_ib - u_curr_before2_ib)
v_curr_ib2 = v_curr_before2_ib + weight * (v_curr_after2_ib - v_curr_before2_ib)
fname = directory + '/' + dirname_curr_ssh + '/' + ssh_grad_file_before
ssh_grad_data_before = nc.Dataset(fname)
ssh_grad_x_lat = ssh_grad_data_before.variables['ssh_grad_x_lat'][:] # lat x lon
ssh_grad_x_lon = ssh_grad_data_before.variables['ssh_grad_x_lon'][:] # lat x lon
ssh_grad_y_lat = ssh_grad_data_before.variables['ssh_grad_y_lat'][:] # lat x lon
ssh_grad_y_lon = ssh_grad_data_before.variables['ssh_grad_y_lon'][:] # lat x lon
ssh_grad_x_before = np.squeeze(ssh_grad_data_before.variables['ssh_grad_x'][:]) # lat x lon
ssh_grad_y_before = np.squeeze(ssh_grad_data_before.variables['ssh_grad_y'][:]) # lat x lon
ssh_grad_data_before.close()
fname = directory + '/' + dirname_curr_ssh + '/' + ssh_grad_file_after
ssh_grad_data_after = nc.Dataset(fname)
ssh_grad_x_after = np.squeeze(ssh_grad_data_after.variables['ssh_grad_x'][:]) # lat x lon
ssh_grad_y_after = np.squeeze(ssh_grad_data_after.variables['ssh_grad_y'][:]) # lat x lon
ssh_grad_data_after.close()
points_ssh_grad_x = np.array([ssh_grad_x_lat.ravel(), ssh_grad_x_lon.ravel()]).T # Shape (n_points, 2)
points_ssh_grad_y = np.array([ssh_grad_y_lat.ravel(), ssh_grad_y_lon.ravel()]).T # Shape (n_points, 2)
ssh_grad_x_before_ib = griddata(points_ssh_grad_x, ssh_grad_x_before.ravel(), (iceberg_lat, iceberg_lon + 360.), method='linear')
ssh_grad_y_before_ib = griddata(points_ssh_grad_y, ssh_grad_y_before.ravel(), (iceberg_lat, iceberg_lon + 360.), method='linear')
ssh_grad_x_after_ib = griddata(points_ssh_grad_x, ssh_grad_x_after.ravel(), (iceberg_lat, iceberg_lon + 360.), method='linear')
ssh_grad_y_after_ib = griddata(points_ssh_grad_y, ssh_grad_y_after.ravel(), (iceberg_lat, iceberg_lon + 360.), method='linear')
ssh_grad_x_ib = ssh_grad_x_before_ib + weight * (ssh_grad_x_after_ib - ssh_grad_x_before_ib)
ssh_grad_y_ib = ssh_grad_y_before_ib + weight * (ssh_grad_y_after_ib - ssh_grad_y_before_ib)
def duv_dt(t, uv):
iceberg_u_init, iceberg_v_init = uv
ib_acc_E, ib_acc_N = iceberg_acc(iceberg_lat, iceberg_u_init, iceberg_v_init, iceberg_sail, iceberg_draft, iceberg_length,
iceberg_times_dt[i], am, omega, Cw, Ca, C_wave, g, rho_air, rho_water,
u_wind_ib, v_wind_ib, [u_curr_ib, u_curr_ib2], [v_curr_ib, v_curr_ib2],
ssh_grad_x_ib, ssh_grad_y_ib, Hs_ib, wave_dir_ib)
return ib_acc_E, ib_acc_N
solution = solve_ivp(duv_dt, (0., iceberg_times_dt[i]), [iceberg_u, iceberg_v], method='BDF', t_eval=[0., iceberg_times_dt[i]])
# Results
iceberg_u_end = solution.y[0][-1] # Final u-velocity
iceberg_v_end = solution.y[1][-1] # Final v-velocity
if grounded_status == 'grounded':
iceberg_u_end = 0.
iceberg_v_end = 0.
iceberg_x = np.nanmean([iceberg_u, iceberg_u_end]) * iceberg_times_dt[i]
iceberg_y = np.nanmean([iceberg_v, iceberg_v_end]) * iceberg_times_dt[i]
iceberg_dist = np.sqrt(iceberg_x ** 2 + iceberg_y ** 2)
iceberg_course = 90. - np.arctan2(iceberg_y, iceberg_x) * 180. / np.pi
if iceberg_course < 0:
iceberg_course = iceberg_course + 360.
iceberg_lat2, iceberg_lon2 = dist_course(Re, iceberg_lat, iceberg_lon, iceberg_dist, iceberg_course)
iceberg_us[i + 1] = iceberg_u_end
iceberg_vs[i + 1] = iceberg_v_end
iceberg_lats[i + 1] = iceberg_lat2
iceberg_lons[i + 1] = iceberg_lon2
bathy_data = nc.Dataset(bathy_data_path)
bathy_lat = bathy_data.variables['lat'][:]
bathy_lon = bathy_data.variables['lon'][:]
bathy_depth = -bathy_data.variables['elevation'][:] # lat x lon
bathy_data.close()
bathy_interp = RegularGridInterpolator((bathy_lat, bathy_lon), bathy_depth, method='linear', bounds_error=True, fill_value=np.nan)