forked from xmingzh/Modified-WAM2layers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCon_E_Recyc_Masterscript.py
1123 lines (974 loc) · 68.1 KB
/
Con_E_Recyc_Masterscript.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 16 13:24:45 2016
@author: Ent00002
"""
#delayed runs
#import time
#time.sleep(500)
#%% Import libraries
import numpy as np
import scipy.io as sio
import calendar
from getconstants import getconstants
from Fluxes_and_States_script import FLUX_States_mainfunction
from timeit import default_timer as timer
import os
import gc
#%% Code (no need to look at this for running)
def get_Sa_track_backward(latitude,longitude,time_reduce,Kvf,Region,Fa_E_top,Fa_N_top,Fa_E_down,Fa_N_down,
Fa_Vert,E,P,W_top,W_down,Sa_track_top_last,Sa_track_down_last):
# make P_region matrix
Region3D = np.tile(np.reshape(Region,[1,len(latitude),len(longitude)]),[len(P[:,0,0]),1,1])
P_region = Region3D * P
# Total moisture in the column
W = W_top + W_down
# separate the direction of the vertical flux and make it absolute
# Van der Ent, R. J., L. Wang-Erlandsson, P. W. Keys, and H. H. G. Savenije, Contrasting roles of interception and
#transpiration in the hydrological cycle – Part 2: Moisture recycling, Earth System Dynamics Discussions, 5, 281–
#326, 2014.
# Here based on the (B8) and (B7) in the above referrence, it should be Fa_Vert (positive upward)
# Not Fa_Vert (positive downward) as stated in the above referrence
Fa_upward = np.zeros(np.shape(Fa_Vert))
with np.errstate(invalid='ignore'):
Fa_upward[Fa_Vert >= 0 ] = Fa_Vert[Fa_Vert >= 0 ]
Fa_downward = np.zeros(np.shape(Fa_Vert))
with np.errstate(invalid='ignore'):
Fa_downward[Fa_Vert <= 0 ] = Fa_Vert[Fa_Vert <= 0 ]
Fa_upward = np.abs(Fa_upward)
Fa_downward=np.abs(Fa_downward)
# include the vertical dispersion
if Kvf == 0:
pass
# do nothing
else:
Fa_upward = (1.+Kvf) * Fa_upward
Fa_upward[Fa_Vert <= 0] = np.abs(Fa_Vert[Fa_Vert <= 0]) * Kvf
Fa_downward = (1.+Kvf) * Fa_downward
Fa_downward[Fa_Vert >= 0] = np.abs(Fa_Vert[Fa_Vert >= 0]) * Kvf
# define the horizontal fluxes over the boundaries
if isglobal==1:
# fluxes over the eastern boundary
Fa_E_top_boundary = np.nan*np.zeros(np.shape(Fa_E_top))
Fa_E_top_boundary[:,:,:-1] = 0.5 * (Fa_E_top[:,:,:-1] + Fa_E_top[:,:,1:])
Fa_E_top_boundary[:,:,-1] = 0.5 * (Fa_E_top[:,:,-1] + Fa_E_top[:,:,0])
Fa_E_down_boundary = np.nan*np.zeros(np.shape(Fa_E_down))
Fa_E_down_boundary[:,:,:-1] = 0.5 * (Fa_E_down[:,:,:-1] + Fa_E_down[:,:,1:])
Fa_E_down_boundary[:,:,-1] = 0.5 * (Fa_E_down[:,:,-1] + Fa_E_down[:,:,0])
# find out where the positive and negative fluxes are
Fa_E_top_pos = np.ones(np.shape(Fa_E_top))
Fa_E_down_pos = np.ones(np.shape(Fa_E_down))
Fa_E_top_pos[Fa_E_top_boundary < 0] = 0
Fa_E_down_pos[Fa_E_down_boundary < 0] = 0
Fa_E_top_neg = Fa_E_top_pos - 1
Fa_E_down_neg = Fa_E_down_pos - 1
# separate directions west-east (all positive numbers)
Fa_E_top_WE = Fa_E_top_boundary * Fa_E_top_pos
Fa_E_top_EW = Fa_E_top_boundary * Fa_E_top_neg
Fa_E_down_WE = Fa_E_down_boundary * Fa_E_down_pos
Fa_E_down_EW = Fa_E_down_boundary * Fa_E_down_neg
# fluxes over the western boundary
Fa_W_top_WE = np.nan*np.zeros(np.shape(P))
Fa_W_top_WE[:,:,1:] = Fa_E_top_WE[:,:,:-1]
Fa_W_top_WE[:,:,0] = Fa_E_top_WE[:,:,-1]
Fa_W_top_EW = np.nan*np.zeros(np.shape(P))
Fa_W_top_EW[:,:,1:] = Fa_E_top_EW[:,:,:-1]
Fa_W_top_EW[:,:,0] = Fa_E_top_EW[:,:,-1]
Fa_W_down_WE = np.nan*np.zeros(np.shape(P))
Fa_W_down_WE[:,:,1:] = Fa_E_down_WE[:,:,:-1]
Fa_W_down_WE[:,:,0] = Fa_E_down_WE[:,:,-1]
Fa_W_down_EW = np.nan*np.zeros(np.shape(P))
Fa_W_down_EW[:,:,1:] = Fa_E_down_EW[:,:,:-1]
Fa_W_down_EW[:,:,0] = Fa_E_down_EW[:,:,-1]
if isglobal==0:
# fluxes over the eastern boundary
Fa_E_top_boundary = np.nan*np.zeros(np.shape(Fa_E_top))
Fa_E_top_boundary[:,:,:-1] = 0.5 * (Fa_E_top[:,:,:-1] + Fa_E_top[:,:,1:])
Fa_E_down_boundary = np.nan*np.zeros(np.shape(Fa_E_down))
Fa_E_down_boundary[:,:,:-1] = 0.5 * (Fa_E_down[:,:,:-1] + Fa_E_down[:,:,1:])
# find out where the positive and negative fluxes are
Fa_E_top_pos = np.ones(np.shape(Fa_E_top))
Fa_E_down_pos = np.ones(np.shape(Fa_E_down))
Fa_E_top_pos[Fa_E_top_boundary < 0] = 0
Fa_E_down_pos[Fa_E_down_boundary < 0] = 0
Fa_E_top_neg = Fa_E_top_pos - 1
Fa_E_down_neg = Fa_E_down_pos - 1
# separate directions west-east (all positive numbers)
Fa_E_top_WE = Fa_E_top_boundary * Fa_E_top_pos
Fa_E_top_EW = Fa_E_top_boundary * Fa_E_top_neg
Fa_E_down_WE = Fa_E_down_boundary * Fa_E_down_pos
Fa_E_down_EW = Fa_E_down_boundary * Fa_E_down_neg
# fluxes over the western boundary
Fa_W_top_WE = np.nan*np.zeros(np.shape(P))
Fa_W_top_WE[:,:,1:] = Fa_E_top_WE[:,:,:-1]
Fa_W_top_EW = np.nan*np.zeros(np.shape(P))
Fa_W_top_EW[:,:,1:] = Fa_E_top_EW[:,:,:-1]
Fa_W_down_WE = np.nan*np.zeros(np.shape(P))
Fa_W_down_WE[:,:,1:] = Fa_E_down_WE[:,:,:-1]
Fa_W_down_EW = np.nan*np.zeros(np.shape(P))
Fa_W_down_EW[:,:,1:] = Fa_E_down_EW[:,:,:-1]
# fluxes over the northern boundary
Fa_N_top_boundary = np.nan*np.zeros(np.shape(Fa_N_top))
Fa_N_top_boundary[:,1:,:] = 0.5 * ( Fa_N_top[:,:-1,:] + Fa_N_top[:,1:,:] )
Fa_N_down_boundary = np.nan*np.zeros(np.shape(Fa_N_down))
Fa_N_down_boundary[:,1:,:] = 0.5 * ( Fa_N_down[:,:-1,:] + Fa_N_down[:,1:,:] )
# find out where the positive and negative fluxes are
Fa_N_top_pos = np.ones(np.shape(Fa_N_top))
Fa_N_down_pos = np.ones(np.shape(Fa_N_down))
Fa_N_top_pos[Fa_N_top_boundary < 0] = 0
Fa_N_down_pos[Fa_N_down_boundary < 0] = 0
Fa_N_top_neg = Fa_N_top_pos - 1
Fa_N_down_neg = Fa_N_down_pos - 1
# separate directions south-north (all positive numbers)
Fa_N_top_SN = Fa_N_top_boundary * Fa_N_top_pos
Fa_N_top_NS = Fa_N_top_boundary * Fa_N_top_neg
Fa_N_down_SN = Fa_N_down_boundary * Fa_N_down_pos
Fa_N_down_NS = Fa_N_down_boundary * Fa_N_down_neg
# fluxes over the southern boundary
Fa_S_top_SN = np.nan*np.zeros(np.shape(P))
Fa_S_top_SN[:,:-1,:] = Fa_N_top_SN[:,1:,:]
Fa_S_top_NS = np.nan*np.zeros(np.shape(P))
Fa_S_top_NS[:,:-1,:] = Fa_N_top_NS[:,1:,:]
Fa_S_down_SN = np.nan*np.zeros(np.shape(P))
Fa_S_down_SN[:,:-1,:] = Fa_N_down_SN[:,1:,:]
Fa_S_down_NS = np.nan*np.zeros(np.shape(P))
Fa_S_down_NS[:,:-1,:] = Fa_N_down_NS[:,1:,:]
# defining size of output
Sa_track_down = np.zeros(np.shape(W_down))
Sa_track_top = np.zeros(np.shape(W_top))
# assign begin values of output == last (but first index) values of the previous time slot
Sa_track_down[-1,:,:] = Sa_track_down_last
Sa_track_top[-1,:,:] = Sa_track_top_last
# defining sizes of tracked moisture
Sa_track_after_Fa_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_after_Fa_P_E_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_E_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_W_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_N_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_S_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_after_Fa_top = np.zeros(np.shape(Sa_track_top_last))
Sa_track_after_Fa_P_E_top = np.zeros(np.shape(Sa_track_top_last))
Sa_track_E_top = np.zeros(np.shape(Sa_track_top_last))
Sa_track_W_top = np.zeros(np.shape(Sa_track_top_last))
Sa_track_N_top = np.zeros(np.shape(Sa_track_top_last))
Sa_track_S_top = np.zeros(np.shape(Sa_track_top_last))
# define sizes of total moisture
Sa_E_down = np.zeros(np.shape(Sa_track_down_last))
Sa_W_down = np.zeros(np.shape(Sa_track_down_last))
Sa_N_down = np.zeros(np.shape(Sa_track_down_last))
Sa_S_down = np.zeros(np.shape(Sa_track_down_last))
Sa_E_top = np.zeros(np.shape(Sa_track_top_last))
Sa_W_top = np.zeros(np.shape(Sa_track_top_last))
Sa_N_top = np.zeros(np.shape(Sa_track_top_last))
Sa_S_top = np.zeros(np.shape(Sa_track_top_last))
# define variables that find out what happens to the water
len_time=np.int(24/time_reduce)
north_loss = np.zeros((len_time,1,len(longitude)))
south_loss = np.zeros((len_time,1,len(longitude)))
down_to_top = np.zeros(np.shape(P))
top_to_down = np.zeros(np.shape(P))
water_lost = np.zeros(np.shape(P))
water_lost_down = np.zeros(np.shape(P))
water_lost_top = np.zeros(np.shape(P))
# Sa calculation backward in time
for t in np.arange(len_time,0,-1):
if isglobal==1:
# down: define values of total moisture
Sa_E_down[0,:,:-1] = W_down[t,:,1:] # Atmospheric storage of the cell to the east [m3]
Sa_E_down[0,:,-1] = W_down[t,:,0] # Atmospheric storage of the cell to the east [m3]
Sa_W_down[0,:,1:] = W_down[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
Sa_W_down[0,:,0] = W_down[t,:,-1] # Atmospheric storage of the cell to the west [m3]
# top: define values of total moisture
Sa_E_top[0,:,:-1] = W_top[t,:,1:] # Atmospheric storage of the cell to the east [m3]
Sa_E_top[0,:,-1] = W_top[t,:,0] # Atmospheric storage of the cell to the east [m3]
Sa_W_top[0,:,1:] = W_top[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
Sa_W_top[0,:,0] = W_top[t,:,-1] # Atmospheric storage of the cell to the west [m3]
# down: define values of tracked moisture of neighbouring grid cells
Sa_track_E_down[0,:,:-1] = Sa_track_down[t,:,1:] # Atmospheric tracked storage of the cell to the east [m3]
Sa_track_E_down[0,:,-1] = Sa_track_down[t,:,0] #Atmospheric tracked storage of the cell to the east [m3]
Sa_track_W_down[0,:,1:] = Sa_track_down[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
Sa_track_W_down[0,:,0] = Sa_track_down[t,:,-1] # Atmospheric storage of the cell to the west [m3]
#Top
Sa_track_E_top[0,:,:-1] = Sa_track_top[t,:,1:] # Atmospheric tracked storage of the cell to the east [m3]
Sa_track_E_top[0,:,-1] = Sa_track_top[t,:,0] # Atmospheric tracked storage of the cell to the east [m3]
Sa_track_W_top[0,:,1:] = Sa_track_top[t,:,:-1] # Atmospheric tracked storage of the cell to the west [m3]
Sa_track_W_top[0,:,0] = Sa_track_top[t,:,-1] # Atmospheric tracked storage of the cell to the west [m3]
if isglobal==0:
# down: define values of total moisture
Sa_E_down[0,:,:-1] = W_down[t,:,1:] # Atmospheric storage of the cell to the east [m3]
Sa_W_down[0,:,1:] = W_down[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
# top: define values of total moisture
Sa_E_top[0,:,:-1] = W_top[t,:,1:] # Atmospheric storage of the cell to the east [m3]
Sa_W_top[0,:,1:] = W_top[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
# down: define values of tracked moisture of neighbouring grid cells
Sa_track_E_down[0,:,:-1] = Sa_track_down[t,:,1:] # Atmospheric tracked storage of the cell to the east [m3]
Sa_track_W_down[0,:,1:] = Sa_track_down[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
#Top
Sa_track_E_top[0,:,:-1] = Sa_track_top[t,:,1:] # Atmospheric tracked storage of the cell to the east [m3]
Sa_track_W_top[0,:,1:] = Sa_track_top[t,:,:-1] # Atmospheric tracked storage of the cell to the west [m3]
Sa_N_down[0,1:,:] = W_down[t,0:-1,:] # Atmospheric storage of the cell to the north [m3]
Sa_S_down[0,:-1,:] = W_down[t,1:,:] # Atmospheric storage of the cell to the south [m3]
Sa_N_top[0,1:,:] = W_top[t,:-1,:] # Atmospheric storage of the cell to the north [m3]
Sa_S_top[0,:-1,:] = W_top[t,1:,:] # Atmospheric storage of the cell to the south [m3]
Sa_track_N_down[0,1:,:] = Sa_track_down[t,:-1,:] # Atmospheric storage of the cell to the north [m3]
Sa_track_S_down[0,:-1,:] = Sa_track_down[t,1:,:] # Atmospheric storage of the cell to the south [m3]
Sa_track_N_top[0,1:,:] = Sa_track_top[t,:-1,:] # Atmospheric tracked storage of the cell to the north [m3]
Sa_track_S_top[0,:-1,:] = Sa_track_top[t,1:,:] # Atmospheric tracked storage of the cell to the south [m3]
# down: calculate with moisture fluxes
if isglobal==1:
Sa_track_after_Fa_down[0,1:-1,:] = (Sa_track_down[t,1:-1,:]
+ Fa_E_down_WE[t-1,1:-1,:] * (Sa_track_E_down[0,1:-1,:] / Sa_E_down[0,1:-1,:])
- Fa_E_down_EW[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
- Fa_W_down_WE[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
+ Fa_W_down_EW[t-1,1:-1,:] * (Sa_track_W_down[0,1:-1,:] / Sa_W_down[0,1:-1,:])
+ Fa_N_down_SN[t-1,1:-1,:] * (Sa_track_N_down[0,1:-1,:] / Sa_N_down[0,1:-1,:])
- Fa_N_down_NS[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
- Fa_S_down_SN[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
+ Fa_S_down_NS[t-1,1:-1,:] * (Sa_track_S_down[0,1:-1,:] / Sa_S_down[0,1:-1,:])
- Fa_downward[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
+ Fa_upward[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:]))
if isglobal==0:
Sa_track_after_Fa_down[0,1:-1,1:-1] = (Sa_track_down[t,1:-1,1:-1]
+ Fa_E_down_WE[t-1,1:-1,1:-1] * (Sa_track_E_down[0,1:-1,1:-1] / Sa_E_down[0,1:-1,1:-1])
- Fa_E_down_EW[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
- Fa_W_down_WE[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
+ Fa_W_down_EW[t-1,1:-1,1:-1] * (Sa_track_W_down[0,1:-1,1:-1] / Sa_W_down[0,1:-1,1:-1])
+ Fa_N_down_SN[t-1,1:-1,1:-1] * (Sa_track_N_down[0,1:-1,1:-1] / Sa_N_down[0,1:-1,1:-1])
- Fa_N_down_NS[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
- Fa_S_down_SN[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
+ Fa_S_down_NS[t-1,1:-1,1:-1] * (Sa_track_S_down[0,1:-1,1:-1] / Sa_S_down[0,1:-1,1:-1])
- Fa_downward[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
+ Fa_upward[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1]))
# top: calculate with moisture fluxes
if isglobal==1:
Sa_track_after_Fa_top[0,1:-1,:] = (Sa_track_top[t,1:-1,:]
+ Fa_E_top_WE[t-1,1:-1,:] * (Sa_track_E_top[0,1:-1,:] / Sa_E_top[0,1:-1,:])
- Fa_E_top_EW[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
- Fa_W_top_WE[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
+ Fa_W_top_EW[t-1,1:-1,:] * (Sa_track_W_top[0,1:-1,:] / Sa_W_top[0,1:-1,:])
+ Fa_N_top_SN[t-1,1:-1,:] * (Sa_track_N_top[0,1:-1,:] / Sa_N_top[0,1:-1,:])
- Fa_N_top_NS[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:]/ W_top[t,1:-1,:])
- Fa_S_top_SN[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
+ Fa_S_top_NS[t-1,1:-1,:] * (Sa_track_S_top[0,1:-1,:] / Sa_S_top[0,1:-1,:])
+ Fa_downward[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
- Fa_upward[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:]))
if isglobal==0:
Sa_track_after_Fa_top[0,1:-1,1:-1] = (Sa_track_top[t,1:-1,1:-1]
+ Fa_E_top_WE[t-1,1:-1,1:-1] * (Sa_track_E_top[0,1:-1,1:-1] / Sa_E_top[0,1:-1,1:-1])
- Fa_E_top_EW[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
- Fa_W_top_WE[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
+ Fa_W_top_EW[t-1,1:-1,1:-1] * (Sa_track_W_top[0,1:-1,1:-1] / Sa_W_top[0,1:-1,1:-1])
+ Fa_N_top_SN[t-1,1:-1,1:-1] * (Sa_track_N_top[0,1:-1,1:-1] / Sa_N_top[0,1:-1,1:-1])
- Fa_N_top_NS[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1]/ W_top[t,1:-1,1:-1])
- Fa_S_top_SN[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
+ Fa_S_top_NS[t-1,1:-1,1:-1] * (Sa_track_S_top[0,1:-1,1:-1] / Sa_S_top[0,1:-1,1:-1])
+ Fa_downward[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
- Fa_upward[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1]))
# losses to the west and east
west_loss = np.zeros((len_time,len(latitude),1))
east_loss = np.zeros((len_time,len(latitude),1))
west_loss[t-1,:,0] = (Fa_W_top_EW[t-1,:,1] * (Sa_track_top[t,:,1] / W_top[t,:,1])
+ Fa_W_down_EW[t-1,:,1] * (Sa_track_down[t,:,1] / W_down[t,:,1]))
east_loss[t-1,:,0] = (Fa_E_top_WE[t-1,:,-2] * (Sa_track_top[t,:,-2] / W_top[t,:,-2])
+ Fa_E_down_WE[t-1,:,-2] * (Sa_track_down[t,:,-2] / W_down[t,:,-2]))
# losses to the north and south
north_loss[t-1,0,:] = (Fa_N_top_NS[t-1,1,:] * (Sa_track_top[t,1,:] / W_top[t,1,:])
+ Fa_N_down_NS[t-1,1,:] * (Sa_track_down[t,1,:] / W_down[t,1,:]))
south_loss[t-1,0,:] = (Fa_S_top_SN[t-1,-2,:] * (Sa_track_top[t,-2,:] / W_top[t,-2,:])
+ Fa_S_down_SN[t-1,-2,:] * (Sa_track_down[t,-2,:] / W_down[t,-2,:]))
# down: add precipitation and subtract evaporation
Sa_track_after_Fa_P_E_down[0,1:-1,:] = (Sa_track_after_Fa_down[0,1:-1,:]
+ P_region[t-1,1:-1,:] * (W_down[t,1:-1,:] / W[t,1:-1,:])
- E[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:]))
# top: add precipitation
Sa_track_after_Fa_P_E_top[0,1:-1,:] = (Sa_track_after_Fa_top[0,1:-1,:]
+ P_region[t-1,1:-1,:] * (W_top[t,1:-1,:] / W[t,1:-1,:]))
# down and top: redistribute unaccounted water that is otherwise lost from the sytem
down_to_top[t-1,:,:] = np.reshape(np.maximum(0, np.reshape(Sa_track_after_Fa_P_E_down, (np.size(Sa_track_after_Fa_P_E_down))) - np.reshape(W_down[t-1,:,:],
(np.size(W_down[t-1,:,:])))), (len(latitude),len(longitude)))
top_to_down[t-1,:,:] = np.reshape(np.maximum(0, np.reshape(Sa_track_after_Fa_P_E_top, (np.size(Sa_track_after_Fa_P_E_top))) - np.reshape(W_top[t-1,:,:],
(np.size(W_top[t-1,:,:])))), (len(latitude),len(longitude)))
Sa_track_after_all_down = Sa_track_after_Fa_P_E_down - down_to_top[t-1,:,:] + top_to_down[t-1,:,:]
Sa_track_after_all_top = Sa_track_after_Fa_P_E_top - top_to_down[t-1,:,:] + down_to_top[t-1,:,:]
# down and top: water lost to the system:
water_lost_down[t-1,:,:] = np.reshape(np.maximum(0, np.reshape(Sa_track_after_all_down, (np.size(Sa_track_after_all_down))) - np.reshape(W_down[t-1,:,:],
(np.size(W_down[t-1,:,:])))), (len(latitude),len(longitude)))
water_lost_top[t-1,:,:] = np.reshape(np.maximum(0, np.reshape(Sa_track_after_all_top, (np.size(Sa_track_after_all_top))) - np.reshape(W_top[t-1,:,:],
(np.size(W_top[t-1,:,:])))), (len(latitude),len(longitude)))
water_lost = water_lost_down + water_lost_top
# down: determine Sa_region of this next timestep 100% stable
Sa_track_down[t-1,1:-1,:] = np.reshape(np.maximum(0,np.minimum(np.reshape(W_down[t-1,1:-1,:], np.size(W_down[t-1,1:-1,:])), np.reshape(Sa_track_after_all_down[0,1:-1,:],
np.size(Sa_track_after_all_down[0,1:-1,:])))), (len(latitude[1:-1]),len(longitude)))
# top: determine Sa_region of this next timestep 100% stable
Sa_track_top[t-1,1:-1,:] = np.reshape(np.maximum(0,np.minimum(np.reshape(W_top[t-1,1:-1,:], np.size(W_top[t-1,1:-1,:])), np.reshape(Sa_track_after_all_top[0,1:-1,:],
np.size(Sa_track_after_all_top[0,1:-1,:])))), (len(latitude[1:-1]),len(longitude)))
if isglobal==1:
return Sa_track_top,Sa_track_down,north_loss,south_loss,down_to_top,top_to_down,water_lost
if isglobal==0:
return Sa_track_top,Sa_track_down,north_loss,south_loss,down_to_top,top_to_down,water_lost,west_loss,east_loss
#%% Code
def get_Sa_track_backward_TIME(latitude,longitude,time_reduce,timestep,Kvf,Region,Fa_E_top,Fa_N_top,Fa_E_down,Fa_N_down,Fa_Vert,E,P,
W_top,W_down,Sa_track_top_last,Sa_track_down_last,Sa_time_top_last,Sa_time_down_last):
# make P_region matrix
Region3D = np.tile(np.reshape(Region,[1,len(latitude),len(longitude)]),[len(P[:,0,0]),1,1])
P_region = Region3D * P
# Total moisture in the column
W = W_top + W_down
# separate the direction of the vertical flux and make it absolute
# Van der Ent, R. J., L. Wang-Erlandsson, P. W. Keys, and H. H. G. Savenije, Contrasting roles of interception and
#transpiration in the hydrological cycle – Part 2: Moisture recycling, Earth System Dynamics Discussions, 5, 281–
#326, 2014.
# Here based on the (B8) and (B7) in the above referrence, it should be Fa_Vert (positive upward)
# Not Fa_Vert (positive downward) as stated in the above referrence
Fa_upward = np.zeros(np.shape(Fa_Vert))
with np.errstate(invalid='ignore'):
Fa_upward[Fa_Vert >= 0 ] = Fa_Vert[Fa_Vert >= 0 ]
Fa_downward = np.zeros(np.shape(Fa_Vert))
with np.errstate(invalid='ignore'):
Fa_downward[Fa_Vert <= 0 ] = Fa_Vert[Fa_Vert <= 0 ]
#Fa_upward = np.abs(Fa_upward)
Fa_downward = np.abs(Fa_downward)
# include the vertical dispersion
if Kvf == 0:
pass
# do nothing
else:
Fa_upward = (1.+Kvf) * Fa_upward
with np.errstate(invalid='ignore'):
Fa_upward[Fa_Vert <= 0] = np.abs(Fa_Vert[Fa_Vert <= 0]) * Kvf
Fa_downward = (1.+Kvf) * Fa_downward
with np.errstate(invalid='ignore'):
Fa_downward[Fa_Vert >= 0] = np.abs(Fa_Vert[Fa_Vert >= 0]) * Kvf
# define the horizontal fluxes over the boundaries
if isglobal==1:
# fluxes over the eastern boundary
Fa_E_top_boundary = np.nan*np.zeros(np.shape(Fa_E_top))
Fa_E_top_boundary[:,:,:-1] = 0.5 * (Fa_E_top[:,:,:-1] + Fa_E_top[:,:,1:])
Fa_E_top_boundary[:,:,-1] = 0.5 * (Fa_E_top[:,:,-1] + Fa_E_top[:,:,0])
Fa_E_down_boundary = np.nan*np.zeros(np.shape(Fa_E_down))
Fa_E_down_boundary[:,:,:-1] = 0.5 * (Fa_E_down[:,:,:-1] + Fa_E_down[:,:,1:])
Fa_E_down_boundary[:,:,-1] = 0.5 * (Fa_E_down[:,:,-1] + Fa_E_down[:,:,0])
# find out where the positive and negative fluxes are
Fa_E_top_pos = np.ones(np.shape(Fa_E_top))
Fa_E_down_pos = np.ones(np.shape(Fa_E_down))
Fa_E_top_pos[Fa_E_top_boundary < 0] = 0
Fa_E_down_pos[Fa_E_down_boundary < 0] = 0
Fa_E_top_neg = Fa_E_top_pos - 1
Fa_E_down_neg = Fa_E_down_pos - 1
# separate directions west-east (all positive numbers)
Fa_E_top_WE = Fa_E_top_boundary * Fa_E_top_pos
Fa_E_top_EW = Fa_E_top_boundary * Fa_E_top_neg
Fa_E_down_WE = Fa_E_down_boundary * Fa_E_down_pos
Fa_E_down_EW = Fa_E_down_boundary * Fa_E_down_neg
# fluxes over the western boundary
Fa_W_top_WE = np.nan*np.zeros(np.shape(P))
Fa_W_top_WE[:,:,1:] = Fa_E_top_WE[:,:,:-1]
Fa_W_top_WE[:,:,0] = Fa_E_top_WE[:,:,-1]
Fa_W_top_EW = np.nan*np.zeros(np.shape(P))
Fa_W_top_EW[:,:,1:] = Fa_E_top_EW[:,:,:-1]
Fa_W_top_EW[:,:,0] = Fa_E_top_EW[:,:,-1]
Fa_W_down_WE = np.nan*np.zeros(np.shape(P))
Fa_W_down_WE[:,:,1:] = Fa_E_down_WE[:,:,:-1]
Fa_W_down_WE[:,:,0] = Fa_E_down_WE[:,:,-1]
Fa_W_down_EW = np.nan*np.zeros(np.shape(P))
Fa_W_down_EW[:,:,1:] = Fa_E_down_EW[:,:,:-1]
Fa_W_down_EW[:,:,0] = Fa_E_down_EW[:,:,-1]
if isglobal==0:
# fluxes over the eastern boundary
Fa_E_top_boundary = np.nan*np.zeros(np.shape(Fa_E_top))
Fa_E_top_boundary[:,:,:-1] = 0.5 * (Fa_E_top[:,:,:-1] + Fa_E_top[:,:,1:])
Fa_E_down_boundary = np.nan*np.zeros(np.shape(Fa_E_down))
Fa_E_down_boundary[:,:,:-1] = 0.5 * (Fa_E_down[:,:,:-1] + Fa_E_down[:,:,1:])
# find out where the positive and negative fluxes are
Fa_E_top_pos = np.ones(np.shape(Fa_E_top))
Fa_E_down_pos = np.ones(np.shape(Fa_E_down))
with np.errstate(invalid='ignore'):
Fa_E_top_pos[Fa_E_top_boundary < 0] = 0
Fa_E_down_pos[Fa_E_down_boundary < 0] = 0
Fa_E_top_neg = Fa_E_top_pos - 1
Fa_E_down_neg = Fa_E_down_pos - 1
# separate directions west-east (all positive numbers)
Fa_E_top_WE = Fa_E_top_boundary * Fa_E_top_pos
Fa_E_top_EW = Fa_E_top_boundary * Fa_E_top_neg
Fa_E_down_WE = Fa_E_down_boundary * Fa_E_down_pos
Fa_E_down_EW = Fa_E_down_boundary * Fa_E_down_neg
# fluxes over the western boundary
Fa_W_top_WE = np.nan*np.zeros(np.shape(P))
Fa_W_top_WE[:,:,1:] = Fa_E_top_WE[:,:,:-1]
Fa_W_top_EW = np.nan*np.zeros(np.shape(P))
Fa_W_top_EW[:,:,1:] = Fa_E_top_EW[:,:,:-1]
Fa_W_down_WE = np.nan*np.zeros(np.shape(P))
Fa_W_down_WE[:,:,1:] = Fa_E_down_WE[:,:,:-1]
Fa_W_down_EW = np.nan*np.zeros(np.shape(P))
Fa_W_down_EW[:,:,1:] = Fa_E_down_EW[:,:,:-1]
# fluxes over the northern boundary
Fa_N_top_boundary = np.nan*np.zeros(np.shape(Fa_N_top))
Fa_N_top_boundary[:,1:,:] = 0.5 * ( Fa_N_top[:,:-1,:] + Fa_N_top[:,1:,:] )
Fa_N_down_boundary = np.nan*np.zeros(np.shape(Fa_N_down))
Fa_N_down_boundary[:,1:,:] = 0.5 * ( Fa_N_down[:,:-1,:] + Fa_N_down[:,1:,:] )
# find out where the positive and negative fluxes are
Fa_N_top_pos = np.ones(np.shape(Fa_N_top))
Fa_N_down_pos = np.ones(np.shape(Fa_N_down))
with np.errstate(invalid='ignore'):
Fa_N_top_pos[Fa_N_top_boundary < 0] = 0
Fa_N_down_pos[Fa_N_down_boundary < 0] = 0
Fa_N_top_neg = Fa_N_top_pos - 1
Fa_N_down_neg = Fa_N_down_pos - 1
# separate directions south-north (all positive numbers)
Fa_N_top_SN = Fa_N_top_boundary * Fa_N_top_pos
Fa_N_top_NS = Fa_N_top_boundary * Fa_N_top_neg
Fa_N_down_SN = Fa_N_down_boundary * Fa_N_down_pos
Fa_N_down_NS = Fa_N_down_boundary * Fa_N_down_neg
# fluxes over the southern boundary
Fa_S_top_SN = np.nan*np.zeros(np.shape(P))
Fa_S_top_SN[:,:-1,:] = Fa_N_top_SN[:,1:,:]
Fa_S_top_NS = np.nan*np.zeros(np.shape(P))
Fa_S_top_NS[:,:-1,:] = Fa_N_top_NS[:,1:,:]
Fa_S_down_SN = np.nan*np.zeros(np.shape(P))
Fa_S_down_SN[:,:-1,:] = Fa_N_down_SN[:,1:,:]
Fa_S_down_NS = np.nan*np.zeros(np.shape(P))
Fa_S_down_NS[:,:-1,:] = Fa_N_down_NS[:,1:,:]
# defining size of output
Sa_track_down = np.zeros(np.shape(W_down))
Sa_track_top = np.zeros(np.shape(W_top))
Sa_time_down = np.zeros(np.shape(W_down))
Sa_time_top = np.zeros(np.shape(W_top))
# assign begin values of output == last (but first index) values of the previous time slot
Sa_track_down[-1,:,:] = Sa_track_down_last
Sa_track_top[-1,:,:] = Sa_track_top_last
Sa_time_down[-1,:,:] = Sa_time_down_last
Sa_time_top[-1,:,:] = Sa_time_top_last
# defining sizes of tracked moisture
Sa_track_after_Fa_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_after_Fa_P_E_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_E_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_W_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_N_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_S_down = np.zeros(np.shape(Sa_track_down_last))
Sa_track_after_Fa_top = np.zeros(np.shape(Sa_track_top_last))
Sa_track_after_Fa_P_E_top = np.zeros(np.shape(Sa_track_top_last))
Sa_track_E_top = np.zeros(np.shape(Sa_track_top_last))
Sa_track_W_top = np.zeros(np.shape(Sa_track_top_last))
Sa_track_N_top = np.zeros(np.shape(Sa_track_top_last))
Sa_track_S_top = np.zeros(np.shape(Sa_track_top_last))
# define sizes of total moisture
Sa_E_down = np.zeros(np.shape(Sa_track_down_last))
Sa_W_down = np.zeros(np.shape(Sa_track_down_last))
Sa_N_down = np.zeros(np.shape(Sa_track_down_last))
Sa_S_down = np.zeros(np.shape(Sa_track_down_last))
Sa_E_top = np.zeros(np.shape(Sa_track_top_last))
Sa_W_top = np.zeros(np.shape(Sa_track_top_last))
Sa_N_top = np.zeros(np.shape(Sa_track_top_last))
Sa_S_top = np.zeros(np.shape(Sa_track_top_last))
# define variables that find out what happens to the water
len_time=np.int(24/time_reduce)
north_loss = np.zeros((len_time,1,len(longitude)))
south_loss = np.zeros((len_time,1,len(longitude)))
down_to_top = np.zeros(np.shape(P))
top_to_down = np.zeros(np.shape(P))
water_lost = np.zeros(np.shape(P))
water_lost_down = np.zeros(np.shape(P))
water_lost_top = np.zeros(np.shape(P))
# Sa calculation backward in time
for t in np.arange(len_time,0,-1):
if isglobal==1:
# down: define values of total moisture
Sa_E_down[0,:,:-1] = W_down[t,:,1:] # Atmospheric storage of the cell to the east [m3]
Sa_E_down[0,:,-1] = W_down[t,:,0] # Atmospheric storage of the cell to the east [m3]
Sa_W_down[0,:,1:] = W_down[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
Sa_W_down[0,:,0] = W_down[t,:,-1] # Atmospheric storage of the cell to the west [m3]
# top: define values of total moisture
Sa_E_top[0,:,:-1] = W_top[t,:,1:] # Atmospheric storage of the cell to the east [m3]
Sa_E_top[0,:,-1] = W_top[t,:,0] # Atmospheric storage of the cell to the east [m3]
Sa_W_top[0,:,1:] = W_top[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
Sa_W_top[0,:,0] = W_top[t,:,-1] # Atmospheric storage of the cell to the west [m3]
# down: define values of tracked moisture of neighbouring grid cells
Sa_track_E_down[0,:,:-1] = Sa_track_down[t,:,1:] # Atmospheric tracked storage of the cell to the east [m3]
Sa_track_E_down[0,:,-1] = Sa_track_down[t,:,0] #Atmospheric tracked storage of the cell to the east [m3]
Sa_track_W_down[0,:,1:] = Sa_track_down[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
Sa_track_W_down[0,:,0] = Sa_track_down[t,:,-1] # Atmospheric storage of the cell to the west [m3]
# top: define values of tracked moisture of neighbouring grid cells
Sa_track_E_top[0,:,:-1] = Sa_track_top[t,:,1:] # Atmospheric tracked storage of the cell to the east [m3]
Sa_track_E_top[0,:,-1] = Sa_track_top[t,:,0] # Atmospheric tracked storage of the cell to the east [m3]
Sa_track_W_top[0,:,1:] = Sa_track_top[t,:,:-1] # Atmospheric tracked storage of the cell to the west [m3]
Sa_track_W_top[0,:,0] = Sa_track_top[t,:,-1] # Atmospheric tracked storage of the cell to the west [m3]
if isglobal==0:
# down: define values of total moisture
Sa_E_down[0,:,:-1] = W_down[t,:,1:] # Atmospheric storage of the cell to the east [m3]
Sa_W_down[0,:,1:] = W_down[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
# top: define values of total moisture
Sa_E_top[0,:,:-1] = W_top[t,:,1:] # Atmospheric storage of the cell to the east [m3]
Sa_W_top[0,:,1:] = W_top[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
# down: define values of tracked moisture of neighbouring grid cells
Sa_track_E_down[0,:,:-1] = Sa_track_down[t,:,1:] # Atmospheric tracked storage of the cell to the east [m3]
Sa_track_W_down[0,:,1:] = Sa_track_down[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
# top: define values of tracked moisture of neighbouring grid cells
Sa_track_E_top[0,:,:-1] = Sa_track_top[t,:,1:] # Atmospheric tracked storage of the cell to the east [m3]
Sa_track_W_top[0,:,1:] = Sa_track_top[t,:,:-1] # Atmospheric tracked storage of the cell to the west [m3]
Sa_N_down[0,1:,:] = W_down[t,0:-1,:] # Atmospheric storage of the cell to the north [m3]
Sa_S_down[0,:-1,:] = W_down[t,1:,:] # Atmospheric storage of the cell to the south [m3]
Sa_N_top[0,1:,:] = W_top[t,:-1,:] # Atmospheric storage of the cell to the north [m3]
Sa_S_top[0,:-1,:] = W_top[t,1:,:] # Atmospheric storage of the cell to the south [m3]
Sa_track_N_down[0,1:,:] = Sa_track_down[t,:-1,:] # Atmospheric storage of the cell to the north [m3]
Sa_track_S_down[0,:-1,:] = Sa_track_down[t,1:,:] # Atmospheric storage of the cell to the south [m3]
Sa_track_N_top[0,1:,:] = Sa_track_top[t,:-1,:] # Atmospheric tracked storage of the cell to the north [m3]
Sa_track_S_top[0,:-1,:] = Sa_track_top[t,1:,:] # Atmospheric tracked storage of the cell to the south [m3]
# down: calculate with moisture fluxes
if isglobal==1:
Sa_track_after_Fa_down[0,1:-1,:] = (Sa_track_down[t,1:-1,:]
+ Fa_E_down_WE[t-1,1:-1,:] * (Sa_track_E_down[0,1:-1,:] / Sa_E_down[0,1:-1,:])
- Fa_E_down_EW[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
- Fa_W_down_WE[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
+ Fa_W_down_EW[t-1,1:-1,:] * (Sa_track_W_down[0,1:-1,:] / Sa_W_down[0,1:-1,:])
+ Fa_N_down_SN[t-1,1:-1,:] * (Sa_track_N_down[0,1:-1,:] / Sa_N_down[0,1:-1,:])
- Fa_N_down_NS[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
- Fa_S_down_SN[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
+ Fa_S_down_NS[t-1,1:-1,:] * (Sa_track_S_down[0,1:-1,:] / Sa_S_down[0,1:-1,:])
- Fa_downward[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
+ Fa_upward[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:]))
if isglobal==0:
Sa_track_after_Fa_down[0,1:-1,1:-1] = (Sa_track_down[t,1:-1,1:-1]
+ Fa_E_down_WE[t-1,1:-1,1:-1] * (Sa_track_E_down[0,1:-1,1:-1] / Sa_E_down[0,1:-1,1:-1])
- Fa_E_down_EW[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
- Fa_W_down_WE[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
+ Fa_W_down_EW[t-1,1:-1,1:-1] * (Sa_track_W_down[0,1:-1,1:-1] / Sa_W_down[0,1:-1,1:-1])
+ Fa_N_down_SN[t-1,1:-1,1:-1] * (Sa_track_N_down[0,1:-1,1:-1] / Sa_N_down[0,1:-1,1:-1])
- Fa_N_down_NS[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
- Fa_S_down_SN[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
+ Fa_S_down_NS[t-1,1:-1,1:-1] * (Sa_track_S_down[0,1:-1,1:-1] / Sa_S_down[0,1:-1,1:-1])
- Fa_downward[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
+ Fa_upward[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1]))
# top: calculate with moisture fluxes
if isglobal==1:
Sa_track_after_Fa_top[0,1:-1,:] = (Sa_track_top[t,1:-1,:]
+ Fa_E_top_WE[t-1,1:-1,:] * (Sa_track_E_top[0,1:-1,:] / Sa_E_top[0,1:-1,:])
- Fa_E_top_EW[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
- Fa_W_top_WE[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
+ Fa_W_top_EW[t-1,1:-1,:] * (Sa_track_W_top[0,1:-1,:] / Sa_W_top[0,1:-1,:])
+ Fa_N_top_SN[t-1,1:-1,:] * (Sa_track_N_top[0,1:-1,:] / Sa_N_top[0,1:-1,:])
- Fa_N_top_NS[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:]/ W_top[t,1:-1,:])
- Fa_S_top_SN[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
+ Fa_S_top_NS[t-1,1:-1,:] * (Sa_track_S_top[0,1:-1,:] / Sa_S_top[0,1:-1,:])
+ Fa_downward[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
- Fa_upward[t-1,1:-1,:] * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:]))
if isglobal==0:
Sa_track_after_Fa_top[0,1:-1,1:-1] = (Sa_track_top[t,1:-1,1:-1]
+ Fa_E_top_WE[t-1,1:-1,1:-1] * (Sa_track_E_top[0,1:-1,1:-1] / Sa_E_top[0,1:-1,1:-1])
- Fa_E_top_EW[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
- Fa_W_top_WE[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
+ Fa_W_top_EW[t-1,1:-1,1:-1] * (Sa_track_W_top[0,1:-1,1:-1] / Sa_W_top[0,1:-1,1:-1])
+ Fa_N_top_SN[t-1,1:-1,1:-1] * (Sa_track_N_top[0,1:-1,1:-1] / Sa_N_top[0,1:-1,1:-1])
- Fa_N_top_NS[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1]/ W_top[t,1:-1,1:-1])
- Fa_S_top_SN[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
+ Fa_S_top_NS[t-1,1:-1,1:-1] * (Sa_track_S_top[0,1:-1,1:-1] / Sa_S_top[0,1:-1,1:-1])
+ Fa_downward[t-1,1:-1,1:-1] * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
- Fa_upward[t-1,1:-1,1:-1] * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1]))
# losses to the west and east
west_loss = np.zeros((len_time,len(latitude),1))
east_loss = np.zeros((len_time,len(latitude),1))
west_loss[t-1,:,0] = (Fa_W_top_EW[t-1,:,1] * (Sa_track_top[t,:,1] / W_top[t,:,1])
+ Fa_W_down_EW[t-1,:,1] * (Sa_track_down[t,:,1] / W_down[t,:,1]))
east_loss[t-1,:,0] = (Fa_E_top_WE[t-1,:,-2] * (Sa_track_top[t,:,-2] / W_top[t,:,-2])
+ Fa_E_down_WE[t-1,:,-2] * (Sa_track_down[t,:,-2] / W_down[t,:,-2]))
# losses to the north and south
north_loss[t-1,0,:] = (Fa_N_top_NS[t-1,1,:] * (Sa_track_top[t,1,:] / W_top[t,1,:])
+ Fa_N_down_NS[t-1,1,:] * (Sa_track_down[t,1,:] / W_down[t,1,:]))
south_loss[t-1,0,:] = (Fa_S_top_SN[t-1,-2,:] * (Sa_track_top[t,-2,:] / W_top[t,-2,:])
+ Fa_S_down_SN[t-1,-2,:] * (Sa_track_down[t,-2,:] / W_down[t,-2,:]))
# down: add precipitation and subtract evaporation
Sa_track_after_Fa_P_E_down[0,1:-1,:] = (Sa_track_after_Fa_down[0,1:-1,:]
+ P_region[t-1,1:-1,:] * (W_down[t,1:-1,:] / W[t,1:-1,:])
- E[t-1,1:-1,:] * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:]))
# top: add precipitation
Sa_track_after_Fa_P_E_top[0,1:-1,:] = (Sa_track_after_Fa_top[0,1:-1,:]
+ P_region[t-1,1:-1,:] * (W_top[t,1:-1,:] / W[t,1:-1,:]))
# down and top: redistribute unaccounted water that is otherwise lost from the sytem
down_to_top[t-1,:,:] = np.reshape(np.maximum(0, np.reshape(Sa_track_after_Fa_P_E_down, (np.size(Sa_track_after_Fa_P_E_down))) - np.reshape(W_down[t-1,:,:],
(np.size(W_down[t-1,:,:])))), (len(latitude),len(longitude)))
top_to_down[t-1,:,:] = np.reshape(np.maximum(0, np.reshape(Sa_track_after_Fa_P_E_top, (np.size(Sa_track_after_Fa_P_E_top))) - np.reshape(W_top[t-1,:,:],
(np.size(W_top[t-1,:,:])))), (len(latitude),len(longitude)))
Sa_track_after_all_down = Sa_track_after_Fa_P_E_down - down_to_top[t-1,:,:] + top_to_down[t-1,:,:]
Sa_track_after_all_top = Sa_track_after_Fa_P_E_top - top_to_down[t-1,:,:] + down_to_top[t-1,:,:]
# down and top: water lost to the system:
water_lost_down[t-1,:,:] = np.reshape(np.maximum(0, np.reshape(Sa_track_after_all_down, (np.size(Sa_track_after_all_down))) - np.reshape(W_down[t-1,:,:],
(np.size(W_down[t-1,:,:])))), (len(latitude),len(longitude)))
water_lost_top[t-1,:,:] = np.reshape(np.maximum(0, np.reshape(Sa_track_after_all_top, (np.size(Sa_track_after_all_top))) - np.reshape(W_top[t-1,:,:],
(np.size(W_top[t-1,:,:])))), (len(latitude),len(longitude)))
water_lost = water_lost_down + water_lost_top
# down: determine Sa_region of this next timestep 100% stable
Sa_track_down[t-1,1:-1,:] = np.reshape(np.maximum(0,np.minimum(np.reshape(W_down[t-1,1:-1,:], np.size(W_down[t-1,1:-1,:])), np.reshape(Sa_track_after_all_down[0,1:-1,:],
np.size(Sa_track_after_all_down[0,1:-1,:])))), (len(latitude[1:-1]),len(longitude)))
# top: determine Sa_region of this next timestep 100% stable
Sa_track_top[t-1,1:-1,:] = np.reshape(np.maximum(0,np.minimum(np.reshape(W_top[t-1,1:-1,:], np.size(W_top[t-1,1:-1,:])), np.reshape(Sa_track_after_all_top[0,1:-1,:],
np.size(Sa_track_after_all_top[0,1:-1,:])))), (len(latitude[1:-1]),len(longitude)))
#############################################################
# timetracking start
# defining sizes of timed moisture
Sa_time_after_Fa_down = np.zeros(np.shape(Sa_time_down_last))
Sa_time_after_Fa_P_E_down = np.zeros(np.shape(Sa_time_down_last))
Sa_time_E_down = np.zeros(np.shape(Sa_time_down_last))
Sa_time_W_down = np.zeros(np.shape(Sa_time_down_last))
Sa_time_N_down = np.zeros(np.shape(Sa_time_down_last))
Sa_time_S_down = np.zeros(np.shape(Sa_time_down_last))
Sa_time_after_Fa_top = np.zeros(np.shape(Sa_time_top_last))
Sa_time_after_Fa_P_E_top = np.zeros(np.shape(Sa_time_top_last))
Sa_time_E_top = np.zeros(np.shape(Sa_time_top_last))
Sa_time_W_top = np.zeros(np.shape(Sa_time_top_last))
Sa_time_N_top = np.zeros(np.shape(Sa_time_top_last))
Sa_time_S_top = np.zeros(np.shape(Sa_time_top_last))
# time increase
ti = timestep
# down: define values of timeed moisture of neighbouring grid cells
if isglobal==1:
Sa_time_E_down[0,:,:-1] = Sa_time_down[t,:,1:] # Atmospheric timeed storage of the cell to the east [s]
Sa_time_E_down[0,:,-1] = Sa_time_down[t,:,0] # Atmospheric timeed storage of the cell to the east [s]
Sa_time_W_down[0,:,1:] = Sa_time_down[t,:,:-1] # Atmospheric timeed storage of the cell to the west [s]
Sa_time_W_down[0,:,0] = Sa_time_down[t,:,-1] # Atmospheric timeed storage of the cell to the west [s]
if isglobal==0:
Sa_time_E_down[0,:,:-1] = Sa_time_down[t,:,1:] # Atmospheric timeed storage of the cell to the east [s]
Sa_time_W_down[0,:,1:] = Sa_time_down[t,:,:-1] # Atmospheric timeed storage of the cell to the west [s]
Sa_time_N_down[0,1:,:] = Sa_time_down[t,:-1,:] # Atmospheric timeed storage of the cell to the north [s]
Sa_time_S_down[0,:-1,:] = Sa_time_down[t,1:,:] # Atmospheric timeed storage of the cell to the south [s]
# down: calculate with moisture fluxes
if isglobal==1:
with np.errstate(divide='ignore', invalid='ignore'):
Sa_time_after_Fa_down[0,1:-1,:] = ((Sa_track_down[t,1:-1,:] * (ti + Sa_time_down[t,1:-1,:])
+ Fa_E_down_WE[t-1,1:-1,:] * (ti + Sa_time_E_down[0,1:-1,:]) * (Sa_track_E_down[0,1:-1,:] / Sa_E_down[0,1:-1,:])
- Fa_E_down_EW[t-1,1:-1,:] * (ti + Sa_time_down[t,1:-1,:]) * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
- Fa_W_down_WE[t-1,1:-1,:] * (ti + Sa_time_down[t,1:-1,:]) * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
+ Fa_W_down_EW[t-1,1:-1,:] * (ti + Sa_time_W_down[0,1:-1,:]) * (Sa_track_W_down[0,1:-1,:] / Sa_W_down[0,1:-1,:])
+ Fa_N_down_SN[t-1,1:-1,:] * (ti + Sa_time_N_down[0,1:-1,:]) * (Sa_track_N_down[0,1:-1,:] / Sa_N_down[0,1:-1,:])
- Fa_N_down_NS[t-1,1:-1,:] * (ti + Sa_time_down[t,1:-1,:]) * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
- Fa_S_down_SN[t-1,1:-1,:] * (ti + Sa_time_down[t,1:-1,:]) * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
+ Fa_S_down_NS[t-1,1:-1,:] * (ti + Sa_time_S_down[0,1:-1,:]) * (Sa_track_S_down[0,1:-1,:] / Sa_S_down[0,1:-1,:])
- Fa_downward[t-1,1:-1,:] * (ti + Sa_time_down[t,1:-1,:]) * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
+ Fa_upward[t-1,1:-1,:] * (ti + Sa_time_top[t,1:-1,:]) * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
) / Sa_track_after_Fa_down[0,1:-1,:])
if isglobal==0:
with np.errstate(divide='ignore', invalid='ignore'):
Sa_time_after_Fa_down[0,1:-1,1:-1] = ((Sa_track_down[t,1:-1,1:-1] * (ti + Sa_time_down[t,1:-1,1:-1])
+ Fa_E_down_WE[t-1,1:-1,1:-1] * (ti + Sa_time_E_down[0,1:-1,1:-1]) * (Sa_track_E_down[0,1:-1,1:-1] / Sa_E_down[0,1:-1,1:-1])
- Fa_E_down_EW[t-1,1:-1,1:-1] * (ti + Sa_time_down[t,1:-1,1:-1]) * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
- Fa_W_down_WE[t-1,1:-1,1:-1] * (ti + Sa_time_down[t,1:-1,1:-1]) * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
+ Fa_W_down_EW[t-1,1:-1,1:-1] * (ti + Sa_time_W_down[0,1:-1,1:-1]) * (Sa_track_W_down[0,1:-1,1:-1] / Sa_W_down[0,1:-1,1:-1])
+ Fa_N_down_SN[t-1,1:-1,1:-1] * (ti + Sa_time_N_down[0,1:-1,1:-1]) * (Sa_track_N_down[0,1:-1,1:-1] / Sa_N_down[0,1:-1,1:-1])
- Fa_N_down_NS[t-1,1:-1,1:-1] * (ti + Sa_time_down[t,1:-1,1:-1]) * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
- Fa_S_down_SN[t-1,1:-1,1:-1] * (ti + Sa_time_down[t,1:-1,1:-1]) * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
+ Fa_S_down_NS[t-1,1:-1,1:-1] * (ti + Sa_time_S_down[0,1:-1,1:-1]) * (Sa_track_S_down[0,1:-1,1:-1] / Sa_S_down[0,1:-1,1:-1])
- Fa_downward[t-1,1:-1,1:-1] * (ti + Sa_time_down[t,1:-1,1:-1]) * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
+ Fa_upward[t-1,1:-1,1:-1] * (ti + Sa_time_top[t,1:-1,1:-1]) * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
) / Sa_track_after_Fa_down[0,1:-1,1:-1])
where_are_NaNs = np.isnan(Sa_time_after_Fa_down)
Sa_time_after_Fa_down[where_are_NaNs] = 0
# top: define values of timeed moisture of neighbouring grid cells
if isglobal==1:
Sa_time_E_top[0,:,:-1] = Sa_time_top[t,:,1:] # Atmospheric storage of the cell to the east [m3]
Sa_time_E_top[0,:,-1] = Sa_time_top[t,:,0] # Atmospheric storage of the cell to the east [m3]
Sa_time_W_top[0,:,1:] = Sa_time_top[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
Sa_time_W_top[0,:,0] = Sa_time_top[t,:,-1] # Atmospheric storage of the cell to the west [m3]
if isglobal==0:
Sa_time_E_top[0,:,:-1] = Sa_time_top[t,:,1:] # Atmospheric storage of the cell to the east [m3]
Sa_time_W_top[0,:,1:] = Sa_time_top[t,:,:-1] # Atmospheric storage of the cell to the west [m3]
Sa_time_N_top[0,1:,:] = Sa_time_top[t,:-1,:] # Atmospheric storage of the cell to the north [m3]
Sa_time_S_top[0,:-1,:] = Sa_time_top[t,1:,:] # Atmospheric storage of the cell to the south [m3]
# top: calculate with moisture fluxes
if isglobal==1:
with np.errstate(divide='ignore', invalid='ignore'):
Sa_time_after_Fa_top[0,1:-1,:] = ((Sa_track_top[t,1:-1,:] * (ti + Sa_time_top[t,1:-1,:])
+ Fa_E_top_WE[t-1,1:-1,:] * (ti + Sa_time_E_top[0,1:-1,:]) * (Sa_track_E_top[0,1:-1,:] / Sa_E_top[0,1:-1,:])
- Fa_E_top_EW[t-1,1:-1,:] * (ti + Sa_time_top[t,1:-1,:]) * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
- Fa_W_top_WE[t-1,1:-1,:] * (ti + Sa_time_top[t,1:-1,:]) * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
+ Fa_W_top_EW[t-1,1:-1,:] * (ti + Sa_time_W_top[0,1:-1,:]) * (Sa_track_W_top[0,1:-1,:] / Sa_W_top[0,1:-1,:])
+ Fa_N_top_SN[t-1,1:-1,:] * (ti + Sa_time_N_top[0,1:-1,:]) * (Sa_track_N_top[0,1:-1,:] / Sa_N_top[0,1:-1,:])
- Fa_N_top_NS[t-1,1:-1,:] * (ti + Sa_time_top[t,1:-1,:]) * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
- Fa_S_top_SN[t-1,1:-1,:] * (ti + Sa_time_top[t,1:-1,:]) * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
+ Fa_S_top_NS[t-1,1:-1,:] * (ti + Sa_time_S_top[0,1:-1,:]) * (Sa_track_S_top[0,1:-1,:] / Sa_S_top[0,1:-1,:])
+ Fa_downward[t-1,1:-1,:] * (ti + Sa_time_down[t,1:-1,:]) * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
- Fa_upward[t-1,1:-1,:] * (ti + Sa_time_top[t,1:-1,:]) * (Sa_track_top[t,1:-1,:] / W_top[t,1:-1,:])
) / Sa_track_after_Fa_top[0,1:-1,:])
if isglobal==0:
with np.errstate(divide='ignore', invalid='ignore'):
Sa_time_after_Fa_top[0,1:-1,1:-1] = ((Sa_track_top[t,1:-1,1:-1] * (ti + Sa_time_top[t,1:-1,1:-1])
+ Fa_E_top_WE[t-1,1:-1,1:-1] * (ti + Sa_time_E_top[0,1:-1,1:-1]) * (Sa_track_E_top[0,1:-1,1:-1] / Sa_E_top[0,1:-1,1:-1])
- Fa_E_top_EW[t-1,1:-1,1:-1] * (ti + Sa_time_top[t,1:-1,1:-1]) * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
- Fa_W_top_WE[t-1,1:-1,1:-1] * (ti + Sa_time_top[t,1:-1,1:-1]) * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
+ Fa_W_top_EW[t-1,1:-1,1:-1] * (ti + Sa_time_W_top[0,1:-1,1:-1]) * (Sa_track_W_top[0,1:-1,1:-1] / Sa_W_top[0,1:-1,1:-1])
+ Fa_N_top_SN[t-1,1:-1,1:-1] * (ti + Sa_time_N_top[0,1:-1,1:-1]) * (Sa_track_N_top[0,1:-1,1:-1] / Sa_N_top[0,1:-1,1:-1])
- Fa_N_top_NS[t-1,1:-1,1:-1] * (ti + Sa_time_top[t,1:-1,1:-1]) * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
- Fa_S_top_SN[t-1,1:-1,1:-1] * (ti + Sa_time_top[t,1:-1,1:-1]) * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
+ Fa_S_top_NS[t-1,1:-1,1:-1] * (ti + Sa_time_S_top[0,1:-1,1:-1]) * (Sa_track_S_top[0,1:-1,1:-1] / Sa_S_top[0,1:-1,1:-1])
+ Fa_downward[t-1,1:-1,1:-1] * (ti + Sa_time_down[t,1:-1,1:-1]) * (Sa_track_down[t,1:-1,1:-1] / W_down[t,1:-1,1:-1])
- Fa_upward[t-1,1:-1,1:-1] * (ti + Sa_time_top[t,1:-1,1:-1]) * (Sa_track_top[t,1:-1,1:-1] / W_top[t,1:-1,1:-1])
) / Sa_track_after_Fa_top[0,1:-1,1:-1])
where_are_NaNs = np.isnan(Sa_time_after_Fa_top)
Sa_time_after_Fa_top[where_are_NaNs] = 0
# down: add precipitation and substract evaporation
with np.errstate(divide='ignore', invalid='ignore'):
Sa_time_after_Fa_P_E_down[0,1:-1,:] = ((Sa_track_after_Fa_down[0,1:-1,:] * Sa_time_after_Fa_down[0,1:-1,:]
+ P_region[t-1,1:-1,:] * ti/2. * (W_down[t,1:-1,:] / W[t,1:-1,:])
- E[t-1,1:-1,:] * (ti + Sa_time_down[t,1:-1,:]) * (Sa_track_down[t,1:-1,:] / W_down[t,1:-1,:])
) / Sa_track_after_Fa_P_E_down[0,1:-1,:])
where_are_NaNs = np.isnan(Sa_time_after_Fa_P_E_down)
Sa_time_after_Fa_P_E_down[where_are_NaNs] = 0
# top: add precipitation
with np.errstate(divide='ignore', invalid='ignore'):
Sa_time_after_Fa_P_E_top[0,1:-1,:] = ((Sa_track_after_Fa_top[0,1:-1,:] * Sa_time_after_Fa_top[0,1:-1,:]
+ P_region[t-1,1:-1,:] * ti/2 * (W_top[t,1:-1,:] / W[t,1:-1,:])
) / Sa_track_after_Fa_P_E_top[0,1:-1,:])
where_are_NaNs = np.isnan(Sa_time_after_Fa_P_E_top)
Sa_time_after_Fa_P_E_top[where_are_NaNs] = 0
# down: redistribute water
with np.errstate(divide='ignore', invalid='ignore'):
Sa_time_after_all_down = ((Sa_track_after_Fa_P_E_down * Sa_time_after_Fa_P_E_down
- down_to_top[t-1,:,:] * Sa_time_after_Fa_P_E_down
+ top_to_down[t-1,:,:] * Sa_time_after_Fa_P_E_top
) / Sa_track_after_all_down)
where_are_NaNs = np.isnan(Sa_time_after_all_down)
Sa_time_after_all_down[where_are_NaNs] = 0
# top: redistribute water
with np.errstate(divide='ignore', invalid='ignore'):
Sa_time_after_all_top = ((Sa_track_after_Fa_P_E_top * Sa_time_after_Fa_P_E_top
- top_to_down[t-1,:,:] * Sa_time_after_Fa_P_E_top
+ down_to_top[t-1,:,:] * Sa_time_after_Fa_P_E_down
) / Sa_track_after_all_top)
where_are_NaNs = np.isnan(Sa_time_after_all_top)
Sa_time_after_all_top[where_are_NaNs] = 0
# down: determine Sa_region of this next timestep 100% stable
Sa_time_down[t-1,1:-1,:] = Sa_time_after_all_down[0,1:-1,:]
# top: determine Sa_region of this next timestep 100% stable
Sa_time_top[t-1,1:-1,:] = Sa_time_after_all_top[0,1:-1,:]
#############################################################
if isglobal==1:
return Sa_time_top,Sa_time_down,Sa_track_top,Sa_track_down,north_loss,south_loss,down_to_top,top_to_down,water_lost
if isglobal==0:
return Sa_time_top,Sa_time_down,Sa_track_top,Sa_track_down,north_loss,south_loss,down_to_top,top_to_down,water_lost,west_loss,east_loss
#%% Function for the output
def output_notime(W_down,E,P,Sa_track_down,Fa_E_down,Fa_E_top,Fa_N_down,Fa_N_top,latitude,longitude,north_loss,south_loss,west_loss,
east_loss,water_lost):
# compute tracked evaporation
E_track = E[:,:,:] * (Sa_track_down[1:,:,:] / W_down[1:,:,:])
E_day = np.zeros((1,len(latitude),len(longitude)))
E_track_day = np.zeros((1,len(latitude),len(longitude)))
P_day = np.zeros((1,len(latitude),len(longitude)))
north_loss_day = np.zeros((1,1,len(longitude)))
south_loss_day = np.zeros((1,1,len(longitude)))
water_lost_day = np.zeros((1,len(latitude),len(longitude)))
west_loss_day = np.zeros((1,len(latitude),1))
east_loss_day = np.zeros((1,len(latitude),1))
# Sa_track_down_day = np.zeros((1,len(latitude),len(longitude)))
# Sa_track_top_day = np.zeros((1,len(latitude),len(longitude)))
# W_down_day = np.zeros((1,len(latitude),len(longitude)))
# W_top_day = np.zeros((1,len(latitude),len(longitude)))
Fa_E_down_day = np.zeros((1,len(latitude),len(longitude)))
Fa_E_top_day = np.zeros((1,len(latitude),len(longitude)))
Fa_N_down_day = np.zeros((1,len(latitude),len(longitude)))
Fa_N_top_day = np.zeros((1,len(latitude),len(longitude)))
# Fa_Vert_day = np.zeros((365+ly,len(latitude),len(longitude)))
# save per day
E_day[0,:,:] = np.sum(E, axis =0)
E_track_day[0,:,:] = np.sum(E_track, axis =0)
P_day[0,:,:] = np.sum(P, axis =0)
north_loss_day[0,:,:] = np.sum(north_loss, axis =0)
south_loss_day[0,:,:] = np.sum(south_loss, axis =0)
water_lost_day[0,:,:] = np.sum(water_lost, axis =0)
west_loss_day[0,:,:] = np.sum(west_loss, axis =0)
east_loss_day[0,:,:] = np.sum(east_loss, axis =0)
# Sa_track_down_day[0,:,:] = np.mean(Sa_track_down[1:,:,:], axis =0)
# Sa_track_top_day[0,:,:] = np.mean(Sa_track_top[1:,:,:], axis =0)
# W_down_day[0,:,:] = np.mean(W_down[1:,:,:], axis =0)
# W_top_day[0,:,:] = np.mean(W_top[1:,:,:], axis =0)
# Fluxes output
Fa_E_down_day[0,:,:] = np.sum(Fa_E_down, axis =0)
Fa_E_top_day[0,:,:] = np.sum(Fa_E_top, axis =0)
Fa_N_down_day[0,:,:] = np.sum(Fa_N_down, axis =0)
Fa_N_top_day[0,:,:] = np.sum(Fa_N_top, axis =0)
# Fa_Vert_day[0,:,:] = np.sum(Fa_Vert, axis =0)
return E_day,E_track_day,P_day,north_loss_day,south_loss_day,west_loss_day,east_loss_day,water_lost_day,Fa_E_down_day,Fa_E_top_day,Fa_N_down_day,Fa_N_top_day
def output_time(W_down,E,P,Sa_track_down,Fa_E_down,Fa_E_top,Fa_N_down,Fa_N_top,latitude,longitude,north_loss,south_loss,west_loss,
east_loss,water_lost,Sa_time_down):
# compute tracked evaporation
E_track = E[:,:,:] * (Sa_track_down[1:,:,:] / W_down[1:,:,:])
E_day = np.zeros((1,len(latitude),len(longitude)))
E_track_day = np.zeros((1,len(latitude),len(longitude)))
P_day = np.zeros((1,len(latitude),len(longitude)))
north_loss_day = np.zeros((1,1,len(longitude)))
south_loss_day = np.zeros((1,1,len(longitude)))
water_lost_day = np.zeros((1,len(latitude),len(longitude)))
west_loss_day = np.zeros((1,len(latitude),1))
east_loss_day = np.zeros((1,len(latitude),1))
Fa_E_down_day = np.zeros((1,len(latitude),len(longitude)))
Fa_E_top_day = np.zeros((1,len(latitude),len(longitude)))
Fa_N_down_day = np.zeros((1,len(latitude),len(longitude)))
Fa_N_top_day = np.zeros((1,len(latitude),len(longitude)))
E_time_day = np.zeros((1,len(latitude),len(longitude)))
# save per day
E_day[0,:,:] = np.sum(E, axis =0)
E_track_day[0,:,:] = np.sum(E_track, axis =0)
P_day[0,:,:] = np.sum(P, axis =0)
north_loss_day[0,:,:] = np.sum(north_loss, axis =0)
south_loss_day[0,:,:] = np.sum(south_loss, axis =0)
water_lost_day[0,:,:] = np.sum(water_lost, axis =0)
west_loss_day[0,:,:] = np.sum(west_loss, axis =0)
east_loss_day[0,:,:] = np.sum(east_loss, axis =0)
# compute tracked evaporation time
E_time = 0.5 * ( Sa_time_down[:-1,:,:] + Sa_time_down[1:,:,:] ) # seconds
# save per day
with np.errstate(divide='ignore', invalid='ignore'):
E_time_day[0,:,:] = np.sum((E_time * E_track), axis = 0) / E_track_day[0,:,:] # seconds
# remove nans
where_are_NaNs = np.isnan(E_time_day)
E_time_day[where_are_NaNs] = 0
# Fluxes output
Fa_E_down_day[0,:,:] = np.sum(Fa_E_down, axis =0)
Fa_E_top_day[0,:,:] = np.sum(Fa_E_top, axis =0)
Fa_N_down_day[0,:,:] = np.sum(Fa_N_down, axis =0)
Fa_N_top_day[0,:,:] = np.sum(Fa_N_top, axis =0)
return E_day,E_track_day,P_day,north_loss_day,south_loss_day,west_loss_day,east_loss_day,water_lost_day,Fa_E_down_day,Fa_E_top_day,Fa_N_down_day,Fa_N_top_day,E_time_day
#%% Runtime & Results
#%% Input
# BEGIN OF INPUT1 (FILL THIS IN)
years = np.arange(2018,1979,-1) # fill in the years backward
#yearpart = np.arange(363,-1,-1) # for a full (leap)year fill in (365,-1,-1)
# Manage the extent of your dataset
# Define the latitude and longitude cell numbers to consider and corresponding lakes that should be considered part of the land
latnrs = np.arange(196,433) # 41N - -16N
lonnrs = np.arange(880,1400) #40E - 170E
isglobal = 0 # fill in 1 for global computations (i.e. Earth round), fill in 0 for a local domain with boundaries
time_reduce=1/6 #the reduced timestep, unit is hour
timestep=time_reduce*3600 #unit is s
# obtain the constants
invariant_data = r'/public/home/mzxiao/ERA5/landseamask.nc'#invariants
latitude,longitude,lsm,g,density_water,A_gridcell,L_N_gridcell,L_S_gridcell,L_EW_gridcell = getconstants(latnrs,lonnrs,invariant_data)
# BEGIN OF INPUT 2 (FILL THIS IN)
# The focus Region
Region_s=np.zeros(np.shape(lsm))
# The selected regionls
Region_s[265:274,1169:1180]=1 # N:21.75-23.75 E: 112.25-114.75
lsm_s=lsm*Region_s
Region=lsm_s[latnrs,:][:,lonnrs]
Region[Region>0.8]=1 # Change the lake also as land, which is not in lsm
Kvf = 3 # vertical dispersion factor (advection only is 0, dispersion the same size of the advective flux is 1, for stability don't make this more than 3)
timetracking = 0 # 0 for not tracking time and 1 for tracking time
veryfirstrun = 0 # type '1' if no run has been done before from which can be continued, otherwise type '0'
interdata_folder = r'/public/home/mzxiao/WAM2layersPython_modify/interdata'
input_folder = r'/public/home/mzxiao/ERA5'
#END OF INPUT
#%% Datapaths (FILL THIS IN)
# Check if interdata folder exists:
# assert os.path.isdir(interdata_folder), "Please create the interdata_folder before running the script"
# Check if sub interdata folder exists otherwise create it:
sub_interdata_folder = os.path.join(interdata_folder, 'continental_backward')
if os.path.isdir(sub_interdata_folder):
pass
else:
os.makedirs(sub_interdata_folder)
def data_path_ea(years,yearpart):
save_empty_arrays_ld_track = os.path.join(sub_interdata_folder, str(years[0]+1) + '-' + str(0) + 'Sa_track.mat')
save_empty_arrays_ld_time = os.path.join(sub_interdata_folder, str(years[0]+1) + '-' + str(0) + 'Sa_time.mat')
save_empty_arrays_track = os.path.join(sub_interdata_folder, str(years[0]) + '-' + str(yearpart[0]+1) + 'Sa_track.mat')
save_empty_arrays_time = os.path.join(sub_interdata_folder, str(years[0]) + '-' + str(yearpart[0]+1) + 'Sa_time.mat')
return save_empty_arrays_ld_track,save_empty_arrays_ld_time,save_empty_arrays_track,save_empty_arrays_time