-
Notifications
You must be signed in to change notification settings - Fork 11
/
flowline_TIModel.py
1151 lines (1040 loc) · 52 KB
/
flowline_TIModel.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
import numpy as np
import xarray as xr
import logging
import warnings
import oggm
import copy
from oggm import entity_task
from oggm.core.flowline import FileModel
from oggm.exceptions import InvalidWorkflowError
# import the MBsandbox modules
from MBsandbox.mbmod_daily_oneflowline import TIModel, TIModel_Sfc_Type, RandomMassBalance_TIModel
from MBsandbox.mbmod_daily_oneflowline import (MultipleFlowlineMassBalance_TIModel,
ConstantMassBalance_TIModel,
AvgClimateMassBalance_TIModel)
from oggm.core.flowline import flowline_model_run
from oggm.core.massbalance import ConstantMassBalance
from oggm import cfg, utils
from oggm.exceptions import InvalidParamsError
log = logging.getLogger(__name__)
### maybe these won't be necessary if the OGGM core flowline run_from_climate_data
# and run_from_constant_data are enough flexible to use another MultipleFlowlineMassBalance
# model ...
# # do it similar as in run_from_climate_data()
@entity_task(log)
def run_from_climate_data_TIModel(gdir, ys=None, ye=None, min_ys=None,
max_ys=None,
fixed_geometry_spinup_yr=None,
store_monthly_step=False,
store_model_geometry=None,
climate_filename='climate_historical',
climate_type='',
climate_input_filesuffix='',
output_filesuffix='',
init_model_filesuffix=None,
init_model_yr=None,
init_model_fls=None,
zero_initial_glacier=False,
bias=0,
melt_f=None,
precipitation_factor=None,
temperature_bias=None,
mb_type='mb_monthly', grad_type='cte',
mb_model_sub_class=TIModel,
kwargs_for_TIModel_Sfc_Type={},
reset=True,
no_qc=False,
all_from_json = False,
json_filename='',
**kwargs):
""" Runs a glacier with climate input from e.g. W5E5 or a GCM.
This will initialize a
:py:class:`MBsandbox.MultipleFlowlineMassBalance_TIModel`,
and run a :py:func:`oggm.core.flowline.flowline_model_run`.
same as in run_from_climate_data but compatible with TIModel
Parameters:
----------------------------
gdir : :py:class:`oggm.GlacierDirectory`
the glacier directory to process
ys : int
start year of the model run (default: from the glacier geometry
date if init_model_filesuffix is None, else init_model_yr)
ye : int
end year of the model run (default: last year of the provided
climate file)
min_ys : int
if you want to impose a minimum start year, regardless if the glacier
inventory date is earlier (e.g. if climate data does not reach).
max_ys : int
if you want to impose a maximum start year, regardless if the glacier
inventory date is later (e.g. if climate data does not reach).
fixed_geometry_spinup_yr : int
if set to an integer, the model will artificially prolongate
all outputs of run_until_and_store to encompass all time stamps
starting from the chosen year. The only output affected are the
glacier wide diagnostic files - all other outputs are set
to constants during "spinup"
store_monthly_step : bool
whether to store the diagnostic data at a monthly time step or not
(default is yearly)
#TODO: should this be included?
#store_model_geometry : bool
# whether to store the full model geometry run file to disk or not.
# (new in OGGM v1.4.1: default is to follow
# cfg.PARAMS['store_model_geometry'])
climate_filename : str
name of the climate file, e.g. 'climate_historical' (default) or
'gcm_data'
climate_type : str
if we use 'gcm_data', this is the climate calibration dataset
(e.g. e.g. 'W5E5' or 'WFDE5_CRU')
if this is empty, the climate_input_filesuffix is used
climate_input_filesuffix: str
filesuffix for the input climate file,
if we use 'climate_historical', this can be e.g. 'W5E5' or 'WFDE5_CRU',
if we use 'gcm_data', it can be 'ISIMIP3b_ensemble_ssp'
output_filesuffix : str
for the output file
init_model_filesuffix : str
if you want to start from a previous model run state. Can be
combined with `init_model_yr`
init_model_yr : int
the year of the initial run you want to start from. The default
is to take the last year of the simulation.
init_model_fls : []
list of flowlines to use to initialise the model (the default is the
present_time_glacier file from the glacier directory).
Ignored if `init_model_filesuffix` is set
zero_initial_glacier : bool
if true, the ice thickness is set to zero before the simulation
bias : float
equal to the residual in TIModel, best is to leave it at 0 !
melt_f:
calibrated melt_f (float) or 'from_json', then the saved json
file from the right prcp-fac and climate is opened and that melt_f is chosen
temperature_bias : float
add a bias to the temperature timeseries
precipitation_factor: float
multiply a factor to the precipitation time series
use the value from the calibration!
no_qc : boolean
default is False (so qc is done if melt_f comes not from json).
but can be set to True if no quality check is wanted when melt_f is set directly!
all_from_json : boolean
take temp. bias, prcp. fac and melt_f from json file.
That json file is used which starts with json_filename
json_filename : string
this is one of the calibration options, e.g. : 'calib_only_geod_temp_b_0_pf_fit_via_winter_mb'
kwargs : dict
kwargs to pass to the FluxBasedModel instance
"""
if climate_type == '':
climate_type = climate_input_filesuffix
if init_model_filesuffix is not None:
fp = gdir.get_filepath('model_geometry',
filesuffix=init_model_filesuffix)
fmod = FileModel(fp)
if init_model_yr is None:
init_model_yr = fmod.last_yr
fmod.run_until(init_model_yr)
init_model_fls = fmod.fls
if ys is None:
ys = init_model_yr
try:
rgi_year = gdir.rgi_date.year
except AttributeError:
rgi_year = gdir.rgi_date
# Take from rgi date if not set yet
if ys is None:
# The RGI timestamp is in calendar date - we convert to hydro date,
# i.e. 2003 becomes 2004 (so that we don't count the MB year 2003
# in the simulation)
# See also: https://github.com/OGGM/oggm/issues/1020
ys = rgi_year + 1
if ys <= rgi_year and init_model_filesuffix is None:
log.warning('You are attempting to run_with_climate_data at dates '
'prior to the RGI inventory date. This may indicate some '
'problem in your workflow. Consider using '
'`fixed_geometry_spinup_yr` for example.')
# Final crop
if min_ys is not None:
ys = ys if ys > min_ys else min_ys
if max_ys is not None:
ys = ys if ys < max_ys else max_ys
if melt_f == 'from_json':
fs = '_{}_{}_{}'.format(climate_type, mb_type, grad_type)
d = gdir.read_json(filename='melt_f_geod', filesuffix=fs)
# get the calibrated melt_f that suits to the prcp factor
try:
melt_f_chosen = d['melt_f_pf_{}'.format(np.round(precipitation_factor, 2))]
# get the corrected ref_hgt so that we can apply this again on the mb model
# if otherwise not melt_f could be found!
ref_hgt_calib_diff = d['ref_hgt_calib_diff']
except:
raise InvalidWorkflowError('there is no calibrated melt_f for this precipitation factor, glacier, climate'
'mb_type and grad_type, need to run first melt_f_calib_geod_prep_inversion'
'with these options!')
#pd_inv_melt_f = pd.read_csv(melt_f_file, index_col='RGIId')
#melt_f_chosen = pd_inv_melt_f['melt_f_opt'].loc[gdir.rgi_id]
# use same pf as from initialisation and calibration
#np.testing.assert_allclose(precipitation_factor, pd_inv_melt_f['pf'])
elif all_from_json:
if kwargs_for_TIModel_Sfc_Type == {}:
sfc_type = False
else:
sfc_type = kwargs_for_TIModel_Sfc_Type['melt_f_change']
if (sfc_type is not False) and (sfc_type is not 'False'):
if kwargs_for_TIModel_Sfc_Type['melt_f_update'] == 'annual':
fs_new = '_{}_sfc_type_{}_annual_{}_{}'.format('W5E5', sfc_type, mb_type,
grad_type)
else:
fs_new = '_{}_sfc_type_{}_{}_{}'.format('W5E5', sfc_type, mb_type,
grad_type)
else:
fs_new = '_{}_sfc_type_{}_{}_{}'.format('W5E5', sfc_type, mb_type,
grad_type)
# json_filename = 'melt_f_geod_opt_winter_mb_approx_std'
# get the calibrated melt_f that suits to the prcp factor
try:
d = gdir.read_json(filename=json_filename,
filesuffix=fs_new)
# get the corrected ref_hgt so that we can apply this again on the mb model
# if otherwise not melt_f could be found!
precipitation_factor = d['pf']
melt_f_chosen = d['melt_f']
temperature_bias = d['temp_bias']
except:
raise InvalidWorkflowError(
'there is no calibrated melt_f for this precipitation factor, glacier, climate'
'mb_type and grad_type, need to do the calibration first!')
else:
melt_f_chosen = melt_f
mb = MultipleFlowlineMassBalance_TIModel(gdir, mb_model_class=mb_model_sub_class,
prcp_fac=precipitation_factor,
melt_f=melt_f_chosen,
filename=climate_filename,
bias=bias,
input_filesuffix=climate_input_filesuffix,
mb_type=mb_type,
grad_type=grad_type,
# check_calib_params=check_calib_params,
**kwargs_for_TIModel_Sfc_Type)
# if temperature_bias is not None:
# mb.temp_bias = temperature_bias
if precipitation_factor is not None:
mb.prcp_fac = precipitation_factor
if temperature_bias is not None:
mb.temp_bias = temperature_bias
if melt_f == 'from_json':
# instead of the quality check we corrected the height already inside of
# melt_f_calib_geod_prep_inversion if no suitable melt_f was found
# let's just check if this has worked
if not climate_filename == 'gcm_data':
np.testing.assert_allclose(ref_hgt_calib_diff,
mb.flowline_mb_models[-1].ref_hgt - mb.flowline_mb_models[-1].uncorrected_ref_hgt)
else:
if not no_qc:
# do the quality check!
mb.flowline_mb_models[-1].historical_climate_qc_mod(gdir)
if ye is None:
# Decide from climate (we can run the last year with data as well)
ye = mb.flowline_mb_models[0].ye + 1
#if isinstance(mb_model_sub_class, TIModel_Sfc_Type):
if init_model_fls is None:
fls = gdir.read_pickle('model_flowlines')
else:
fls = copy.deepcopy(init_model_fls)
if reset and mb_model_sub_class == TIModel_Sfc_Type:
mb.flowline_mb_models[-1].reset_pd_mb_bucket(init_model_fls = fls)
return flowline_model_run(gdir, output_filesuffix=output_filesuffix,
mb_model=mb, ys=ys, ye=ye,
store_monthly_step=store_monthly_step,
store_model_geometry=store_model_geometry,
init_model_fls=init_model_fls,
zero_initial_glacier=zero_initial_glacier,
fixed_geometry_spinup_yr=fixed_geometry_spinup_yr,
**kwargs)
@entity_task(log)
def run_random_climate_TIModel(gdir, nyears=1000, y0=None, halfsize=15,
mb_model_sub_class=TIModel,
temperature_bias=None,
mb_type='mb_monthly', grad_type='cte',
bias=0, seed=None,
melt_f=None,
precipitation_factor=None,
store_monthly_step=False,
store_model_geometry=None,
climate_filename='climate_historical',
climate_type='',
climate_input_filesuffix='',
output_filesuffix='', init_model_fls=None,
zero_initial_glacier=False,
unique_samples=False, #melt_f_file=None,
reset = True,
no_qc=False,
kwargs_for_TIModel_Sfc_Type={},
all_from_json = False,
json_filename='',
**kwargs):
"""Runs the random mass-balance model for a given number of years.
copy of run_random_climate --> needs to be tested ...
This will initialize a
:py:class:`MBsandbox.MultipleFlowlineMassBalance_TIModel`,
and run a :py:func:`oggm.core.flowline.flowline_model_run`.
Parameters
----------
gdir : :py:class:`oggm.GlacierDirectory`
the glacier directory to process
nyears : int
length of the simulation
y0 : int, optional
central year of the random climate period. The default is to be
centred on t*.
halfsize : int, optional
the half-size of the time window (window size = 2 * halfsize + 1)
bias : float
equal to the residual in TIModel, best is to leave it at 0 !
seed : int
seed for the random generator. If you ignore this, the runs will be
different each time. Setting it to a fixed seed across glaciers can
be useful if you want to have the same climate years for all of them
store_monthly_step : bool
whether to store the diagnostic data at a monthly time step or not
(default is yearly)
#TODO: should this be included?
#store_model_geometry : bool
# whether to store the full model geometry run file to disk or not.
# (new in OGGM v1.4.1: default is to follow
# cfg.PARAMS['store_model_geometry'])
climate_filename : str
name of the climate file, e.g. 'climate_historical' (default) or
'gcm_data'
climate_type : str
if we use 'gcm_data', this is the climate calibration dataset
(e.g. e.g. 'W5E5' or 'WFDE5_CRU')
if this is empty, the climate_input_filesuffix is used
climate_input_filesuffix: str
filesuffix for the input climate file,
if we use 'climate_historical', this can be e.g. 'W5E5' or 'WFDE5_CRU',
if we use 'gcm_data', it can be 'ISIMIP3b_ensemble_ssp'
output_filesuffix : str
for the output file
init_model_filesuffix : str
if you want to start from a previous model run state. Can be
combined with `init_model_yr`
init_model_yr : int
the year of the initial run you want to start from. The default
is to take the last year of the simulation.
init_model_fls : []
list of flowlines to use to initialise the model (the default is the
present_time_glacier file from the glacier directory).
Ignored if `init_model_filesuffix` is set
zero_initial_glacier : bool
if true, the ice thickness is set to zero before the simulation
melt_f:
calibrated melt_f (float) or 'from_json', then the saved json
file from the right prcp-fac and climate is opened and that melt_f is chosen
temperature_bias : float
add a bias to the temperature timeseries
precipitation_factor: float
multiply a factor to the precipitation time series
use the value from the calibration!
unique_samples: bool
if true, chosen random mass-balance years will only be available once
per random climate period-length
if false, every model year will be chosen from the random climate
period with the same probability
no_qc : boolean
default is False (so qc is done if melt_f comes not from json).
but can be set to True if no quality check is wanted when melt_f is set directly!
all_from_json : boolean
take temp. bias, prcp. fac and melt_f from json file.
That json file is used which starts with json_filename
json_filename : string
this is one of the calibration options, e.g. : 'calib_only_geod_temp_b_0_pf_fit_via_winter_mb'
kwargs : dict
kwargs to pass to the FluxBasedModel instance
"""
if climate_type == '':
climate_type = climate_input_filesuffix
if melt_f == 'from_json':
fs = '_{}_{}_{}'.format(climate_type, mb_type, grad_type)
d = gdir.read_json(filename='melt_f_geod', filesuffix=fs)
# get the calibrated melt_f that suits to the prcp factor
try:
melt_f_chosen = d['melt_f_pf_{}'.format(np.round(precipitation_factor, 2))]
# get the corrected ref_hgt so that we can apply this again on the mb model
# if otherwise not melt_f could be found!
ref_hgt_calib_diff = d['ref_hgt_calib_diff']
except:
raise InvalidWorkflowError('there is no calibrated melt_f for this precipitation factor, glacier, climate'
'mb_type and grad_type, need to run first melt_f_calib_geod_prep_inversion'
'with these options!')
#pd_inv_melt_f = pd.read_csv(melt_f_file, index_col='RGIId')
#melt_f_chosen = pd_inv_melt_f['melt_f_opt'].loc[gdir.rgi_id]
# use same pf as from initialisation and calibration
#np.testing.assert_allclose(precipitation_factor, pd_inv_melt_f['pf'])
elif all_from_json:
if kwargs_for_TIModel_Sfc_Type == {}:
sfc_type = False
else:
sfc_type = kwargs_for_TIModel_Sfc_Type['melt_f_change']
if (sfc_type is not False) and (sfc_type is not 'False'):
if kwargs_for_TIModel_Sfc_Type['melt_f_update'] == 'annual':
fs_new = '_{}_sfc_type_{}_annual_{}_{}'.format('W5E5', sfc_type, mb_type,
grad_type)
else:
fs_new = '_{}_sfc_type_{}_{}_{}'.format('W5E5', sfc_type, mb_type,
grad_type)
else:
fs_new = '_{}_sfc_type_{}_{}_{}'.format('W5E5', sfc_type, mb_type,
grad_type)
# json_filename = 'melt_f_geod_opt_winter_mb_approx_std'
# get the calibrated melt_f that suits to the prcp factor
try:
d = gdir.read_json(filename=json_filename,
filesuffix=fs_new)
# get the corrected ref_hgt so that we can apply this again on the mb model
# if otherwise not melt_f could be found!
precipitation_factor = d['pf']
melt_f_chosen = d['melt_f']
temperature_bias = d['temp_bias']
except:
raise InvalidWorkflowError(
'there is no calibrated melt_f for this precipitation factor, glacier, climate'
'mb_type and grad_type, need to do the calibration first!')
else:
melt_f_chosen = melt_f
mb = MultipleFlowlineMassBalance_TIModel(gdir,
mb_model_class=RandomMassBalance_TIModel,
y0=y0, halfsize=halfsize,
melt_f=melt_f_chosen,
prcp_fac=precipitation_factor,
mb_type=mb_type,
grad_type=grad_type,
bias = bias,
seed=seed,
mb_model_sub_class = mb_model_sub_class,
filename=climate_filename,
input_filesuffix=climate_input_filesuffix,
unique_samples=unique_samples,
**kwargs_for_TIModel_Sfc_Type)
if precipitation_factor is not None:
mb.prcp_fac = precipitation_factor
if temperature_bias is not None:
mb.temp_bias = temperature_bias
if melt_f == 'from_json':
# instead of the quality check we corrected the height already inside of
# melt_f_calib_geod_prep_inversion if no suitable melt_f was found
# let's just check if this has worked
np.testing.assert_allclose(ref_hgt_calib_diff,
mb.flowline_mb_models[-1].mbmod.ref_hgt - mb.flowline_mb_models[-1].mbmod.uncorrected_ref_hgt)
else:
if not no_qc:
# do the quality check!
mb.flowline_mb_models[-1].historical_climate_qc_mod(gdir)
if init_model_fls is None:
fls = gdir.read_pickle('model_flowlines')
else:
fls = copy.deepcopy(init_model_fls)
if reset and mb_model_sub_class == TIModel_Sfc_Type:
mb.flowline_mb_models[-1].mbmod.reset_pd_mb_bucket(init_model_fls = fls)
# do once the spinup manually but then not again
if mb_model_sub_class == TIModel_Sfc_Type:
spinup_yrs = kwargs_for_TIModel_Sfc_Type['spinup_yrs']
mb.flowline_mb_models[-1].mbmod.get_specific_mb(year=np.arange(y0-halfsize-spinup_yrs,
y0-halfsize),
fls=fls)
# spinup is done, now set the spinup_yrs to zero for the actual run!!!
mb.flowline_mb_models[-1].mbmod.spinup_yrs = 0
return flowline_model_run(gdir, output_filesuffix=output_filesuffix,
mb_model=mb, ys=0, ye=nyears,
store_monthly_step=store_monthly_step,
store_model_geometry=store_model_geometry,
init_model_fls=init_model_fls,
zero_initial_glacier=zero_initial_glacier,
**kwargs)
# work in Process:
# problem: don't have a constant mb TIModel, this would be quite a lot of work ...
# not yet adapted at all, first need a new ConstantMbModel_TIModel!!
@entity_task(log)
def run_constant_climate_TIModel(gdir, nyears=1000, y0=None, halfsize=15,
bias=None, temperature_bias=None,
precipitation_factor=None,
mb_type='mb_monthly', grad_type='cte',
melt_f=None,
store_monthly_step=False,
store_model_geometry=None,
init_model_filesuffix=None,
init_model_yr=None,
output_filesuffix='',
climate_filename='climate_historical',
climate_type='',
climate_input_filesuffix='',
mb_model_sub_class=TIModel,
init_model_fls=None,
zero_initial_glacier=False,
kwargs_for_TIModel_Sfc_Type = {},
reset = True,
interpolation_optim=False,
use_avg_climate=False,
no_qc=False,
all_from_json = False,
json_filename='',
**kwargs):
"""Runs the constant mass-balance model of the TIModel
for a given number of years.
This is equivalent to run_constant_climate but is compatible with TIModel
This will initialize a
:py:class:`oggm.core.massbalance.MultipleFlowlineMassBalance_TIModel`,
and run a :py:func:`oggm.core.flowline.flowline_model_run`.
Parameters
----------
gdir : :py:class:`oggm.GlacierDirectory`
the glacier directory to process
nyears : int
length of the simulation (default: as long as needed for reaching
equilibrium)
y0 : int
central year of the requested climate period. The default is to be
centred on t*.
halfsize : int, optional
the half-size of the time window (window size = 2 * halfsize + 1)
bias : float
bias of the mb model. Default is to use the calibrated one, which
is often a better idea. For t* experiments it can be useful to set it
to zero
temperature_bias : float
add a bias to the temperature timeseries
precipitation_factor: float
multiply a factor to the precipitation time series
default is None and means that the precipitation factor from the
calibration is applied which is cfg.PARAMS['prcp_scaling_factor']
store_monthly_step : bool
whether to store the diagnostic data at a monthly time step or not
(default is yearly)
store_model_geometry : bool
whether to store the full model geometry run file to disk or not.
(new in OGGM v1.4.1: default is to follow
cfg.PARAMS['store_model_geometry'])
init_model_filesuffix : str
if you want to start from a previous model run state. Can be
combined with `init_model_yr`
init_model_yr : int
the year of the initial run you want to start from. The default
is to take the last year of the simulation.
climate_filename : str
name of the climate file, e.g. 'climate_historical' (default) or
'gcm_data'
climate_type : str
if we use 'gcm_data', this is the climate calibration dataset
(e.g. e.g. 'W5E5' or 'WFDE5_CRU')
if this is empty, the climate_input_filesuffix is used
climate_input_filesuffix: str
filesuffix for the input climate file,
if we use 'climate_historical', this can be e.g. 'W5E5' or 'WFDE5_CRU',
if we use 'gcm_data', it can be 'ISIMIP3b_ensemble_ssp'
output_filesuffix : str
this add a suffix to the output file (useful to avoid overwriting
previous experiments)
mb_model_sub_class : class
which child class of TIModel_Parent should be used, either TIModel (default)
or TIModel_Sfc_Type
zero_initial_glacier : bool
if true, the ice thickness is set to zero before the simulation
init_model_fls : []
list of flowlines to use to initialise the model (the default is the
present_time_glacier file from the glacier directory)
kwargs_for_TIModel_Sfc_Type : dict
default is empty dictionary, kwargs to pass to the TIModel_Sfc_Type instance,
to change these params: melt_f_ratio_snow_to_ice, melt_f_update, spinup_yrs,
tau_e_fold_yr, melt_f_change; if mb_model_sub_class is TIModel, this should be
an empty dict!
no_qc : boolean
default is False (so qc is done if melt_f comes not from json).
but can be set to True if no quality check is wanted when melt_f is set directly!
all_from_json : boolean
take temp. bias, prcp. fac and melt_f from json file.
That json file is used which starts with json_filename
json_filename : string
this is one of the calibration options, e.g. : 'calib_only_geod_temp_b_0_pf_fit_via_winter_mb'
kwargs : dict
kwargs to pass to the FluxBasedModel instance
"""
if climate_type == '':
climate_type = climate_input_filesuffix
if init_model_filesuffix is not None:
fp = gdir.get_filepath('model_geometry',
filesuffix=init_model_filesuffix)
fmod = FileModel(fp)
if init_model_yr is None:
init_model_yr = fmod.last_yr
fmod.run_until(init_model_yr)
init_model_fls = fmod.fls
if melt_f == 'from_json':
fs = '_{}_{}_{}'.format(climate_type, mb_type, grad_type)
d = gdir.read_json(filename='melt_f_geod', filesuffix=fs)
# get the calibrated melt_f that suits to the prcp factor
try:
melt_f_chosen = d['melt_f_pf_{}'.format(np.round(precipitation_factor, 2))]
# get the corrected ref_hgt so that we can apply this again on the mb model
# if otherwise not melt_f could be found!
ref_hgt_calib_diff = d['ref_hgt_calib_diff']
except:
raise InvalidWorkflowError('there is no calibrated melt_f for this precipitation factor, glacier, climate'
'mb_type and grad_type, need to run first melt_f_calib_geod_prep_inversion'
'with these options!')
#pd_inv_melt_f = pd.read_csv(melt_f_file, index_col='RGIId')
#melt_f_chosen = pd_inv_melt_f['melt_f_opt'].loc[gdir.rgi_id]
# use same pf as from initialisation and calibration
#np.testing.assert_allclose(precipitation_factor, pd_inv_melt_f['pf'])
elif all_from_json:
if kwargs_for_TIModel_Sfc_Type == {}:
sfc_type = False
else:
sfc_type = kwargs_for_TIModel_Sfc_Type['melt_f_change']
if (sfc_type is not False) and (sfc_type is not 'False'):
if kwargs_for_TIModel_Sfc_Type['melt_f_update'] == 'annual':
fs_new = '_{}_sfc_type_{}_annual_{}_{}'.format('W5E5', sfc_type, mb_type,
grad_type)
else:
fs_new = '_{}_sfc_type_{}_{}_{}'.format('W5E5', sfc_type, mb_type,
grad_type)
else:
fs_new = '_{}_sfc_type_{}_{}_{}'.format('W5E5', sfc_type, mb_type,
grad_type)
# json_filename = 'melt_f_geod_opt_winter_mb_approx_std'
# get the calibrated melt_f that suits to the prcp factor
try:
d = gdir.read_json(filename=json_filename,
filesuffix=fs_new)
# get the corrected ref_hgt so that we can apply this again on the mb model
# if otherwise not melt_f could be found!
precipitation_factor = d['pf']
melt_f_chosen = d['melt_f']
temperature_bias = d['temp_bias']
except:
raise InvalidWorkflowError(
'there is no calibrated melt_f for this precipitation factor, glacier, climate'
'mb_type and grad_type, need to do the calibration first!')
else:
melt_f_chosen = melt_f
if mb_model_sub_class == TIModel and kwargs_for_TIModel_Sfc_Type != {}:
raise InvalidWorkflowError('if mb_model_sub_class is TIModel,'
' this should be an empty dict!')
if use_avg_climate:
mb_sub_model_class = AvgClimateMassBalance_TIModel
else:
mb_sub_model_class = ConstantMassBalance_TIModel
mb = MultipleFlowlineMassBalance_TIModel(gdir,
mb_model_class=mb_sub_model_class,
y0=y0, halfsize=halfsize,
bias=bias,
melt_f=melt_f_chosen,
prcp_fac=precipitation_factor,
mb_type=mb_type,
grad_type=grad_type,
filename=climate_filename,
input_filesuffix=climate_input_filesuffix,
mb_model_sub_class=mb_model_sub_class,
interpolation_optim=interpolation_optim,
**kwargs_for_TIModel_Sfc_Type)
if precipitation_factor is not None:
mb.prcp_fac = precipitation_factor
if temperature_bias is not None:
mb.temp_bias = temperature_bias
if melt_f == 'from_json':
# instead of the quality check we corrected the height already inside of
# melt_f_calib_geod_prep_inversion if no suitable melt_f was found
# let's just check if this has worked)
np.testing.assert_allclose(ref_hgt_calib_diff,
mb.flowline_mb_models[-1].mbmod.ref_hgt - mb.flowline_mb_models[
-1].mbmod.uncorrected_ref_hgt)
else:
if not no_qc:
# do the quality check!
mb.flowline_mb_models[-1].historical_climate_qc_mod(gdir)
if init_model_fls is None:
fls = gdir.read_pickle('model_flowlines')
else:
fls = copy.deepcopy(init_model_fls)
if reset and mb_model_sub_class == TIModel_Sfc_Type:
mb.flowline_mb_models[-1].reset_pd_mb_bucket(init_model_fls = fls)
return flowline_model_run(gdir, output_filesuffix=output_filesuffix,
mb_model=mb, ys=0, ye=nyears,
store_monthly_step=store_monthly_step,
store_model_geometry=store_model_geometry,
init_model_fls=init_model_fls,
zero_initial_glacier=zero_initial_glacier,
**kwargs)
@entity_task(log)
def run_with_hydro_daily(gdir, run_task=None, ref_area_from_y0=False,
ref_area_yr=None, ref_geometry_filesuffix=None,
fixed_geometry_spinup_yr=None, Testing=False, store_annual=True, **kwargs):
"""Run the flowline model and add hydro diagnostics on daily basis (experimental!).
Parameters
----------
run_task : func
any of the `run_*`` tasks in the MBSandbox.flowline_TIModel module.
The mass-balance model used needs to have the `add_climate` output
kwarg available though.
ref_area_yr : int
the hydrological output is computed over a reference area, which
per default is the largest area covered by the glacier in the simulation
period. Use this kwarg to force a specific area to the state of the
glacier at the provided simulation year.
ref_area_from_y0 : bool
overwrite ref_area_yr to the first year of the timeseries
ref_geometry_filesuffix : str
this kwarg allows to copy the reference area from a previous simulation
(useful for projections with historical spinup for example).
Set to a model_geometry file filesuffix that is present in the
current directory (e.g. `_historical` for pre-processed gdirs).
If set, ref_area_yr and ref_area_from_y0 refer to this file instead.
fixed_geometry_spinup_yr : int
if set to an integer, the model will artificially prolongate
all outputs of run_until_and_store to encompass all time stamps
starting from the chosen year. The only output affected are the
glacier wide diagnostic files - all other outputs are set
to constants during "spinup"
Testing: if set to true, the 29th of February is set to nan values in non-leap years, so that the remaining days
are at the same index in non-leap and leap years, if set to false the last 366th day in non-leap years
is set to zero
store_annual: whether to store annual outputs or only daily outputs
**kwargs : all valid kwargs for ``run_task``
"""
# Make sure it'll return something
kwargs['return_value'] = True
# Check that kwargs are compatible
if kwargs.get('store_monthly_step', False):
raise InvalidParamsError('run_with_hydro only compatible with '
'store_monthly_step=False.')
if kwargs.get('mb_elev_feedback', 'annual') != 'annual':
raise InvalidParamsError('run_with_hydro_daily only compatible with '
"mb_elev_feedback='annual' (yes, even "
"when asked for monthly hydro output).")
out = run_task(gdir, fixed_geometry_spinup_yr=fixed_geometry_spinup_yr,
**kwargs)
if out is None:
raise InvalidWorkflowError('The run task ({}) did not run '
'successfully.'.format(run_task.__name__))
do_spinup = fixed_geometry_spinup_yr is not None
if do_spinup:
start_dyna_model_yr = out.y0
# Mass balance model used during the run
mb_mod = out.mb_model
# Glacier geometry during the run
suffix = kwargs.get('output_filesuffix', '')
# We start by fetching the reference model geometry
# The one we just computed
fmod = FileModel(gdir.get_filepath('model_geometry', filesuffix=suffix))
# The last one is the final state - we can't compute MB for that
years = fmod.years[:-1]
if ref_geometry_filesuffix:
if not ref_area_from_y0 and ref_area_yr is None:
raise InvalidParamsError('If `ref_geometry_filesuffix` is set, '
'users need to specify `ref_area_from_y0`'
' or `ref_area_yr`')
# User provided
fmod_ref = FileModel(gdir.get_filepath('model_geometry',
filesuffix=ref_geometry_filesuffix))
else:
# ours as well
fmod_ref = fmod
# Check input
if ref_area_from_y0:
ref_area_yr = fmod_ref.years[0]
# Geometry at year yr to start with + off-glacier snow bucket
if ref_area_yr is not None:
if ref_area_yr not in fmod_ref.years:
raise InvalidParamsError('The chosen ref_area_yr is not '
'available!')
fmod_ref.run_until(ref_area_yr)
# Geometry at y0 to start with + off-glacier snow bucket
bin_area_2ds = []
bin_elev_2ds = []
ref_areas = []
snow_buckets = []
for fl in fmod_ref.fls:
# Glacier area on bins
bin_area = fl.bin_area_m2
ref_areas.append(bin_area)
# snow_buckets.append(bin_area * 0)
# snow_buckets.append(np.zeros(len(bin_area)))
snow_buckets.append(np.zeros(len(bin_area)))
# Output 2d data
shape = len(years), len(bin_area)
bin_area_2ds.append(np.empty(shape, np.float64))
bin_elev_2ds.append(np.empty(shape, np.float64))
# Ok now fetch all geometry data in a first loop
# We do that because we might want to get the largest possible area (default)
# and we want to minimize the number of calls to run_until
for i, yr in enumerate(years):
fmod.run_until(yr)
for fl_id, (fl, bin_area_2d, bin_elev_2d) in \
enumerate(zip(fmod.fls, bin_area_2ds, bin_elev_2ds)):
# Time varying bins
bin_area_2d[i, :] = fl.bin_area_m2
bin_elev_2d[i, :] = fl.surface_h
if ref_area_yr is None:
# Ok we get the max area instead
for ref_area, bin_area_2d in zip(ref_areas, bin_area_2ds):
ref_area[:] = bin_area_2d.max(axis=0)
# Ok now we have arrays, we can work with that
# -> second time varying loop is for mass-balance
ntime = len(years) + 1
# for each year store 366 values #last one should be 0 or nann in non leap years
oshape = (ntime, 366)
# for daily usage
seconds = cfg.SEC_IN_DAY
out = {
'off_area': {
'description': 'Off-glacier area',
'unit': 'm 2',
'data': np.zeros(ntime),
},
'on_area': {
'description': 'On-glacier area',
'unit': 'm 2',
'data': np.zeros(ntime),
},
'melt_off_glacier': {
'description': 'Off-glacier melt',
'unit': 'kg yr-1',
'data': np.zeros(oshape),
},
'melt_on_glacier': {
'description': 'On-glacier melt',
'unit': 'kg yr-1',
'data': np.zeros(oshape),
},
'melt_residual_off_glacier': {
'description': 'Off-glacier melt due to MB model residual',
'unit': 'kg yr-1',
'data': np.zeros(oshape),
},
'melt_residual_on_glacier': {
'description': 'On-glacier melt due to MB model residual',
'unit': 'kg yr-1',
'data': np.zeros(oshape),
},
'liq_prcp_off_glacier': {
'description': 'Off-glacier liquid precipitation',
'unit': 'kg yr-1',
'data': np.zeros(oshape),
},
'liq_prcp_on_glacier': {
'description': 'On-glacier liquid precipitation',
'unit': 'kg yr-1',
'data': np.zeros(oshape),
},
'snowfall_off_glacier': {
'description': 'Off-glacier solid precipitation',
'unit': 'kg yr-1',
'data': np.zeros(oshape),
},
'snowfall_on_glacier': {
'description': 'On-glacier solid precipitation',
'unit': 'kg yr-1',
'data': np.zeros(oshape),
},
'snow_bucket': {
'description': 'Off-glacier snow reservoir (state variable)',
'unit': 'kg',
'data': np.zeros(oshape),
},
'model_mb': {
'description': 'Annual mass-balance from dynamical model',
'unit': 'kg yr-1',
'data': np.zeros(ntime),
},
'residual_mb': {
'description': 'Difference (before correction) between mb model and dyn model melt',
'unit': 'kg yr-1',
'data': np.zeros(oshape),
},
}
# Initialize
fmod.run_until(years[0])
prev_model_vol = fmod.volume_m3
for i, yr in enumerate(years):
for fl_id, (ref_area, snow_bucket, bin_area_2d, bin_elev_2d) in \
enumerate(zip(ref_areas, snow_buckets, bin_area_2ds, bin_elev_2ds)):
bin_area = bin_area_2d[i, :]
bin_elev = bin_elev_2d[i, :]
# Make sure we have no negative contribution when glaciers are out
off_area = utils.clip_min(ref_area - bin_area, 0)
try:
try:
mb_out = mb_mod.get_daily_mb(bin_elev, fl_id=fl_id,
year=yr,
add_climate=True)
mb, _, _, prcp, prcpsol = mb_out
except:
raise InvalidWorkflowError('Run with hydro daily needs a daily MB '
'model, so it can only run with TIModel.')
except ValueError as e:
if 'too many values to unpack' in str(e):
raise InvalidWorkflowError('Run with hydro needs a MB '
'model able to add climate '
'info to `get_annual_mb`.')
raise
# Here we use mass (kg/time) not ice volume (mb is m ice per second)
mb *= seconds * cfg.PARAMS['ice_density']
# Bias of the mb model is a fake melt term that we need to deal with
#check if year is leap year
days_in_year = len(np.sum(prcpsol, axis=0))
if mb_mod.bias != 0:
raise InvalidWorkflowError('run_with_hydro_daily cant handle '
'mb_model.bias != 0.' )
# on daily basis prcp has shape (bins, days in year) bin_area must have shape (bins,1)
bin_area = bin_area[:, np.newaxis]
off_area = off_area[:, np.newaxis]
liq_prcp_on_g = (prcp - prcpsol) * bin_area
liq_prcp_off_g = (prcp - prcpsol) * off_area
prcpsol_on_g = prcpsol * bin_area
prcpsol_off_g = prcpsol * off_area
# IMPORTANT: This should NORMALLY never be negative unless
# Some weird geometry or numerical issues
melt_on_g = (prcpsol - mb) * bin_area
melt_off_g = (prcpsol - mb) * off_area
# These thresholds are arbitrary for now. Numbers are usually much
# larger
if np.any(melt_on_g < -1):