forked from PBosmanatm/ICLASS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postprocessing.py
1157 lines (1126 loc) · 79.3 KB
/
postprocessing.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 -*-
"""
@author: Bosman Peter
"""
#This script should be adapted to the optimisation performed
import numpy as np
import copy as cp
import matplotlib.pyplot as plt
import seaborn as sb
import os
import matplotlib.style as style
import scipy.interpolate as interp
import glob
import pickle
style.use('classic')
##############################
####### settings #############
##############################
nr_bins = 15 #for 1d-pdf
nr_bins2d = nr_bins #for 2d-pdf
interp_pdf = False #interpolate in 2d pdfs
if interp_pdf:
nr_bins_int = 200 #nr of bins after interpolation
remove_prev = True #remove everything starting with 'pp_'
plot_obsfit = False #plot fit with observations
constr_succes_state_ens = True #used in e.g. plot_1d_pdfs, can be turned off if unused to reduce number of calculations
if constr_succes_state_ens:
plot_1d_pdfs = False #plot 1d-pdf
plot_2d_pdfs = False #plot 2d_pdfs
plot_pdf_panels = True #plot figure panel with pdfs
plot_colored_corr_matr = True #plot a colored correlation matrix
if plot_colored_corr_matr:
showfullmatr = False #show the full symmetric matrix, or show only one half
TakeSubSample = True #Include another correl matrix based on subsample
if TakeSubSample:
Start = 0 #the index where to start, default is 0
SelectStep = 2 #The step size to sample the ensemble
print_estim_post_param_stdev = True #print estimated standard deviation of posterior parameters
print_NrStDev_from_truth = False #Only for OSSEs, print (for state parameters) posterior minus truth, normalised with posterior standard deviation
save_print_to_f = True #Write the output from the print statements in this file to a file called pp_print.txt
plot_co2profiles = False #plot co2 mixing ratios at multiple heights in one plot
plot_manual_fitpanels = False #panels of figures, showing obs and model
plot_auto_fitpanels = True #a panel of figures, showing obs and model. More automated, number of rows and nr of columns are specified by two variables
plot_enbal_panel = True #plot a figure panel with observations corrected for the energy balnce error using FracH
plotfontsize = 12 #plot font size (for some figures), except for legend
legendsize = plotfontsize - 1 #legend size of (some) plots
figformat = 'eps'#the format in which you want figure output, e.g. 'png'
load_stored_objects = True #load objects stored using the Pickle module
if load_stored_objects:
storefolder_objects = 'pickle_objects' #the folder where to load these objects from
load_second_optim = False #load a second optimisation (not just a second ensemble member)
if load_second_optim:
if load_stored_objects:
storefolder_objects2 = '../5param2obs/pickle_objects'
plot_two_optim_man_fitpanel =True #figure panel showing obs and model. Involving two optimisations (not just a second ensemble member)
load_third_optim = False #load a third optimisation (not just a third ensemble member)
if load_third_optim:
if load_stored_objects:
storefolder_objects3 = '../10paramnoise/pickle_objects'
plot_three_optim_man_fitpanel =True #figure panel showing obs and model. Involving three optimisations (not just three ensemble members)
if plot_obsfit or plot_auto_fitpanels:
plot_errbars_at_sca_obs = True #The y-location where to plot the error bars in the observation fit figures, if True the error bars will be placed around the scaled observations (if obs scales are used).
##############################
####### end settings #########
##############################
MultiWordHeaders = ['minimum costf', 'chi squared']#Used while reading Optstatsfile.txt
if load_stored_objects:
if storefolder_objects not in os.listdir():
raise Exception('Unexisting folder specified for storefolder_objects')
for i in range(len(MultiWordHeaders)):
MultiWordHeaders[i] = ''.join(MultiWordHeaders[i].split()) #removes the spaces in between
plt.rc('font', size=plotfontsize) #plot font size
display_names = {}
disp_nms_par = {}
disp_units_par = {}
disp_units = {}
if remove_prev:
filelist_list = []
# filelist_list += [glob.glob('pp_pdf_posterior*')]
filelist_list += [glob.glob('pp_*')] #add Everything starting with 'pp_' to the list
for filelist in filelist_list:
for filename in filelist:
if os.path.isfile(filename): #in case file occurs in two filelists in filelist_list, two attempts to remove would give error
os.remove(filename)
if load_stored_objects:
if 'priormodel.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/priormodel.pkl', 'rb') as input:
priormodel = pickle.load(input)
if 'priorinput.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/priorinput.pkl', 'rb') as input:
priorinput = pickle.load(input)
if 'obsvarlist.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/obsvarlist.pkl', 'rb') as input:
obsvarlist = pickle.load(input)
if 'disp_units.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/disp_units.pkl', 'rb') as input:
disp_units = pickle.load(input)
if 'disp_units_par.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/disp_units_par.pkl', 'rb') as input:
disp_units_par = pickle.load(input)
if 'display_names.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/display_names.pkl', 'rb') as input:
display_names = pickle.load(input)
if 'disp_nms_par.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/disp_nms_par.pkl', 'rb') as input:
disp_nms_par = pickle.load(input)
if 'optim.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/optim.pkl', 'rb') as input:
optim = pickle.load(input)
if 'obs_times.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/obs_times.pkl', 'rb') as input:
obs_times = pickle.load(input)
if 'measurement_error.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/measurement_error.pkl', 'rb') as input:
measurement_error = pickle.load(input)
if 'optimalinput.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/optimalinput.pkl', 'rb') as input:
optimalinput = pickle.load(input)
if 'optimalinput_onsp.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/optimalinput_onsp.pkl', 'rb') as input:
optimalinput_onsp = pickle.load(input)
if 'optimalmodel.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/optimalmodel.pkl', 'rb') as input:
optimalmodel = pickle.load(input)
if 'optimalmodel_onsp.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/optimalmodel_onsp.pkl', 'rb') as input:
optimalmodel_onsp = pickle.load(input)
if 'PertData_mems.pkl' in os.listdir(storefolder_objects):
with open(storefolder_objects+'/PertData_mems.pkl', 'rb') as input:
PertData_mems = pickle.load(input)
if load_second_optim:
if 'priormodel.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/priormodel.pkl', 'rb') as input:
priormodel2 = pickle.load(input)
if 'priorinput.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/priorinput.pkl', 'rb') as input:
priorinput2 = pickle.load(input)
if 'obsvarlist.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/obsvarlist.pkl', 'rb') as input:
obsvarlist2 = pickle.load(input)
if 'optim.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/optim.pkl', 'rb') as input:
optim2 = pickle.load(input)
if 'obs_times.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/obs_times.pkl', 'rb') as input:
obs_times2 = pickle.load(input)
if 'measurement_error.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/measurement_error.pkl', 'rb') as input:
measurement_error2 = pickle.load(input)
if 'optimalinput.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/optimalinput.pkl', 'rb') as input:
optimalinput2 = pickle.load(input)
if 'optimalinput_onsp.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/optimalinput_onsp.pkl', 'rb') as input:
optimalinput_onsp2 = pickle.load(input)
if 'optimalmodel.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/optimalmodel.pkl', 'rb') as input:
optimalmodel2 = pickle.load(input)
if 'optimalmodel_onsp.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/optimalmodel_onsp.pkl', 'rb') as input:
optimalmodel_onsp2 = pickle.load(input)
if 'PertData_mems.pkl' in os.listdir(storefolder_objects2):
with open(storefolder_objects2+'/PertData_mems.pkl', 'rb') as input:
PertData_mems2 = pickle.load(input)
if load_third_optim:
if 'priormodel.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/priormodel.pkl', 'rb') as input:
priormodel3 = pickle.load(input)
if 'priorinput.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/priorinput.pkl', 'rb') as input:
priorinput3 = pickle.load(input)
if 'obsvarlist.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/obsvarlist.pkl', 'rb') as input:
obsvarlist3 = pickle.load(input)
if 'optim.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/optim.pkl', 'rb') as input:
optim3 = pickle.load(input)
if 'obs_times.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/obs_times.pkl', 'rb') as input:
obs_times3 = pickle.load(input)
if 'measurement_error.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/measurement_error.pkl', 'rb') as input:
measurement_error3 = pickle.load(input)
if 'optimalinput.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/optimalinput.pkl', 'rb') as input:
optimalinput3 = pickle.load(input)
if 'optimalinput_onsp.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/optimalinput_onsp.pkl', 'rb') as input:
optimalinput_onsp3 = pickle.load(input)
if 'optimalmodel.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/optimalmodel.pkl', 'rb') as input:
optimalmodel3 = pickle.load(input)
if 'optimalmodel_onsp.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/optimalmodel_onsp.pkl', 'rb') as input:
optimalmodel_onsp3 = pickle.load(input)
if 'PertData_mems.pkl' in os.listdir(storefolder_objects3):
with open(storefolder_objects3+'/PertData_mems.pkl', 'rb') as input:
PertData_mems3 = pickle.load(input)
########################################
#### units and names for plots #########
########################################
# e.g. disp_units_par['theta'] = 'K' for parameter theta
# or disp_units['theta'] = 'K' for observations of theta
#or disp_nms_par['theta'] = r'$\theta$' #name to be displayed for parameter theta
#or display_names['wCO2'] = '$F_{CO2}$' #name to be displayed for observations of wCO2
display_names['wCO2'] = '$F_{CO2}$'
disp_units['wCO2'] = 'mg CO$_2$ m$^{-2}$s$^{-1}$'
disp_nms_par['theta'] = r'$\theta$' #name for parameter theta
disp_nms_par['advtheta'] = r'$adv_{\theta}$'
disp_nms_par['advq'] = '$adv_{q}$'
disp_nms_par['advCO2'] = '$adv_{CO2}$'
disp_nms_par['deltatheta'] = r'$\Delta_{\theta}$'
disp_nms_par['gammatheta'] = r'$\gamma_{\theta}$'
disp_nms_par['deltaq'] = '$\Delta_{q}$'
disp_nms_par['gammaq'] = '$\gamma_{q}$'
disp_nms_par['deltaCO2'] = '$\Delta_{CO2}$'
disp_nms_par['deltaCO2'] = '$\Delta_{CO2}$'
disp_nms_par['gammaCO2'] = '$\gamma_{CO2}$'
disp_nms_par['sca_sto'] = r'$\alpha_{sto}$'
disp_nms_par['alpha'] = r'$\alpha_{rad}$'
disp_nms_par['FracH'] = '$Frac_{H}$'
disp_nms_par['wg'] = '$w_{g}$'
disp_nms_par['R10'] = '$R_{10}$'
############################################
#### end units and names for plots #########
############################################
use_ensemble = False
with open('Optstatsfile.txt','r') as StatsFile:
for index, line in enumerate(StatsFile):
if 'optimal state without ensemble:' in line:
state = StatsFile.readline().split() #readline reads the next line
opt_state0 = StatsFile.readline().split()
elif 'optimal state with ensemble' in line:
opt_state = StatsFile.readline().split()
use_ensemble = True
elif 'index member with best state:' in line:
opt_sim_nr = int(StatsFile.readline().split()[-1]) #so go to next line, split, take last part and make an int
elif 'estim post state covar matrix:' in line:
line_to_check = StatsFile.readline()
if not ('Not enough successful optimisations' in line_to_check):
if 'Warning' in line_to_check:
StatsFile.readline()
post_cov_matr = np.zeros((len(state),len(state)))
for i in range(len(state)):
line_to_use = StatsFile.readline().split()[1:]
for j in range(len(state)):
post_cov_matr[i,j] = line_to_use[j]
StatsFile.readline()
elif 'estim post state corr matrix:' in line: #If there are not enough succesful optimisations, the line 'estim post state corr matrix:' will not be present
StatsFile.readline()
post_cor_matr = np.zeros((len(state),len(state)))
for i in range(len(state)):
line_to_use = StatsFile.readline().split()[1:]
for j in range(len(state)):
post_cor_matr[i,j] = line_to_use[j]
StatsFile.readline()
elif 'optimised ensemble members:' in line:
ensemble = []
headers_line = StatsFile.readline()
if 'perturbed non-state params:' in headers_line:
pert_non_state_param = True
else:
pert_non_state_param = False
headers = headers_line.split()
columncounter = 0
StateStartIndFound = False
SuccesColumn = False#wether the ensemble has a column 'successful'
for wordind in range(len(headers)):
if headers[wordind] in state and StateStartIndFound == False:
StateStartInd = columncounter
StateStartIndFound = True
elif headers[wordind] == 'successful':
SuccesColumnInd = columncounter
SuccesColumn = True
if wordind != range(len(headers))[-1]: #cause than wordind+1 as gives an IndexError
if not (headers[wordind]+headers[wordind+1] in MultiWordHeaders):
columncounter += 1
membernr = 0
line_to_use = StatsFile.readline().split()
memberdict = {}
i = StateStartInd
for item in state:
memberdict[item] = float(line_to_use[i])
i += 1
if SuccesColumn:
BooleanString = line_to_use[SuccesColumnInd]
if BooleanString == 'True':
memberdict['successful'] = True
else:
memberdict['successful'] = False
ensemble = np.append(ensemble,memberdict)
continueread = True
while continueread:
line_to_use = StatsFile.readline().split()
memberdict = {}
try:
if float(line_to_use[0]) - membernr != 1: #membernr is here the member number of the previous line
continueread = False
except (IndexError,ValueError) as e:
continueread = False
if continueread:
i = StateStartInd
for item in state:
memberdict[item] = float(line_to_use[i])
i += 1
if SuccesColumn:
BooleanString = line_to_use[SuccesColumnInd]
if BooleanString == 'True':
memberdict['successful'] = True
else:
memberdict['successful'] = False
ensemble = np.append(ensemble,memberdict)
membernr += 1
elif 'prior ensemble members:' in line:
ensemble_p = []
headers = StatsFile.readline().split()
columncounter_p = 0
StateStartIndFound = False
for wordind in range(len(headers)):
if headers[wordind] in state and StateStartIndFound == False:
StateStartInd_p = columncounter_p
StateStartIndFound = True
if wordind != range(len(headers))[-1]: #cause than wordind+1 as gives an IndexError
if not (headers[wordind]+headers[wordind+1] in MultiWordHeaders):
columncounter_p += 1
membernr_p = 0
line_to_use = StatsFile.readline().split()
memberdict_p = {}
i = StateStartInd_p
for item in state:
memberdict_p[item] = float(line_to_use[i])
i += 1
ensemble_p = np.append(ensemble_p,memberdict_p)
continueread = True
while continueread:
line_to_use = StatsFile.readline().split()
memberdict_p = {}
try:
if float(line_to_use[0]) - membernr_p != 1: #membernr_p is here the member number of the previous line
continueread = False
except (IndexError,ValueError) as e:
continueread = False
if continueread:
i = StateStartInd_p
for item in state:
memberdict_p[item] = float(line_to_use[i])
i += 1
ensemble_p = np.append(ensemble_p,memberdict_p)
membernr_p += 1
for item in state:
if item not in disp_units_par:
disp_units_par[item] = ''
if (use_ensemble and SuccesColumn): #if SuccesColumn, it means est_post_pdf_covmatr was set to True
success_ens = np.array([x['successful'] for x in ensemble[0:]],dtype=bool)
if print_estim_post_param_stdev and (np.sum(success_ens[1:]) > 1):
Print1 = 'Estimated standard deviation posterior parameters:'
print(Print1)
if save_print_to_f:
open('pp_print.txt','a').write('{0:>69s}'.format(Print1+'\n'))
for i in range(len(state)):
Print2 = state[i]+':'
print(Print2)
Print3 = np.sqrt(post_cov_matr[i,i])
print(Print3)
if save_print_to_f:
open('pp_print.txt','a').write('{0:>69s}'.format(Print2+'\n'))
open('pp_print.txt','a').write('{0:>69s}'.format(str(Print3)+'\n'))
if print_NrStDev_from_truth: #(This is for OSSEs)
truth = {} #Below, give the true parameters
truth['alpha'] = 0.20
truth['h'] = 350
truth['sca_sto'] = 1.0
truth['gammatheta'] = 0.003
truth['wg'] = 0.27
truth['CO2'] = 422
truth['advCO2'] = 0.0
truth['gammaq'] = -1e-6
truth['z0m'] = 0.02
truth['z0h'] = 0.02
truth['obs_sca_cf_CO2mh'] = 1.4
truth['FracH'] = 0.35
Print1 = 'Posterior minus truth, normalised with posterior standard deviation:'
print(Print1)
if save_print_to_f:
open('pp_print.txt','a').write('{0:>69s}'.format(Print1+'\n'))
for i in range(len(state)):
Print2 = state[i]+':'
print(Print2)
Print3 = (optimalinput.__dict__[state[i]] - truth[state[i]])/np.sqrt(post_cov_matr[i,i])
print(Print3)
if save_print_to_f:
open('pp_print.txt','a').write('{0:>69s}'.format(Print2+'\n'))
open('pp_print.txt','a').write('{0:>69s}'.format(str(Print3)+'\n'))
if np.sum(success_ens[1:]) > 1 and constr_succes_state_ens:
mean_state_post = np.zeros(len(state))
mean_state_prior = np.zeros(len(state))
succes_state_ens = np.zeros(len(state),dtype=list)
seq_suc_p = np.zeros(len(state),dtype=list)
for i in range(len(state)):
seq = np.array([x[state[i]] for x in ensemble[1:]]) #iterate over the dictionaries,gives array. We exclude the first optimisation, since it biases the sampling as we choose it ourselves.
succes_state_ens[i] = np.array([seq[x] for x in range(len(seq)) if success_ens[1:][x]])
mean_state_post[i] = np.mean(succes_state_ens[i]) #np.nanmean not necessary since we filter already for successful optimisations
seq_p = np.array([x[state[i]] for x in ensemble_p[1:]]) #iterate over the dictionaries,gives array . We exclude the first optimisation, since it biases the sampling as we choose it ourselves.
seq_suc_p[i] = np.array([seq_p[x] for x in range(len(seq_p)) if success_ens[1:][x]])
mean_state_prior[i] = np.mean(seq_suc_p[i])
if plot_1d_pdfs:
nbins = np.linspace(np.min(succes_state_ens[i]), np.max(succes_state_ens[i]), nr_bins + 1)
n, bins = np.histogram(succes_state_ens[i], nbins, density=1)
pdfx = np.zeros(n.size)
pdfy = np.zeros(n.size)
for k in range(n.size):
pdfx[k] = 0.5*(bins[k]+bins[k+1])
pdfy[k] = n[k]
fig = plt.figure()
plt.plot(pdfx,pdfy, linestyle='-', linewidth=2,color='red',label='post')
nbins_p = np.linspace(np.min(seq_suc_p[i]), np.max(seq_suc_p[i]), nr_bins + 1)
n_p, bins_p = np.histogram(seq_suc_p[i], nbins_p, density=1)
pdfx = np.zeros(n_p.size)
pdfy = np.zeros(n_p.size)
for k in range(n_p.size):
pdfx[k] = 0.5*(bins_p[k]+bins_p[k+1])
pdfy[k] = n_p[k]
plt.plot(pdfx,pdfy, linestyle='dashed', linewidth=2,color='gold',label='prior')
plt.axvline(mean_state_post[i], linestyle='-',linewidth=2,color='red',label = 'mean post')
plt.axvline(mean_state_prior[i], linestyle='dashed',linewidth=2,color='gold',label = 'mean prior')
plt.xlabel(state[i] + ' ('+ disp_units_par[state[i]] +')')
plt.ylabel('Probability density (-)')
plt.subplots_adjust(left=0.15, right=0.92, top=0.96, bottom=0.15,wspace=0.1)
plt.legend(loc=0, frameon=True,prop={'size':legendsize})
if not ('pp_pdf_posterior_'+state[i]+'.'+figformat).lower() in [x.lower() for x in os.listdir()]: #os.path.exists can be case-sensitive, depending on operating system
plt.savefig('pp_pdf_posterior_'+state[i]+'.'+figformat, format=figformat)
else:
itemname = state[i] + '_'
while ('pp_pdf_posterior_'+itemname+'.'+figformat).lower() in [x.lower() for x in os.listdir()]:
itemname += '_'
plt.savefig('pp_pdf_posterior_'+itemname+'.'+figformat, format=figformat)
if plot_pdf_panels:
plt.rc('font', size=22)
plotvars = ['advtheta','gammaq']
fig, ax = plt.subplots(1,2,figsize=(24,8))
succes_state_ens_var0 = succes_state_ens[state.index(plotvars[0])]
seq_suc_p_var0 = seq_suc_p[state.index(plotvars[0])]
mean_state_post_var0 = np.mean(succes_state_ens_var0) #np.nanmean not necessary since we filter already for successful optimisations
nbins = np.linspace(np.min(succes_state_ens_var0), np.max(succes_state_ens_var0), nr_bins + 1)
n, bins = np.histogram(succes_state_ens_var0, nbins, density=1)
pdfx = np.zeros(n.size)
pdfy = np.zeros(n.size)
for k in range(n.size):
pdfx[k] = 0.5*(bins[k]+bins[k+1])
pdfy[k] = n[k]
ax[0].plot(pdfx,pdfy, linestyle='-', linewidth=2,color='red',label='post')
nbins_p = np.linspace(np.min(seq_suc_p_var0), np.max(seq_suc_p_var0), nr_bins + 1)
n_p, bins_p = np.histogram(seq_suc_p_var0, nbins_p, density=1)
pdfx = np.zeros(n_p.size)
pdfy = np.zeros(n_p.size)
for k in range(n_p.size):
pdfx[k] = 0.5*(bins_p[k]+bins_p[k+1])
pdfy[k] = n_p[k]
ax[0].ticklabel_format(axis="both", style="sci", scilimits=(0,0))
ax[0].plot(pdfx,pdfy, linestyle='dashed', linewidth=2,color='gold',label='prior')
ax[0].axvline(mean_state_post_var0, linestyle='-',linewidth=2,color='red',label = 'mean post')
ax[0].axvline(mean_state_prior[state.index(plotvars[0])], linestyle='dashed',linewidth=2,color='gold',label = 'mean prior')
ax[0].set_xlabel(disp_nms_par[plotvars[0]] + ' ('+ disp_units_par[plotvars[0]] +')')
ax[0].set_ylabel('Probability density (-)')
succes_state_ens_var1 = succes_state_ens[state.index(plotvars[1])]
seq_suc_p_var1 = seq_suc_p[state.index(plotvars[1])]
mean_state_post_var1 = np.mean(succes_state_ens_var1) #np.nanmean not necessary since we filter already for successful optimisations
nbins = np.linspace(np.min(succes_state_ens_var1), np.max(succes_state_ens_var1), nr_bins + 1)
n, bins = np.histogram(succes_state_ens_var1, nbins, density=1)
pdfx = np.zeros(n.size)
pdfy = np.zeros(n.size)
for k in range(n.size):
pdfx[k] = 0.5*(bins[k]+bins[k+1])
pdfy[k] = n[k]
ax[1].plot(pdfx,pdfy, linestyle='-', linewidth=2,color='red',label='post')
nbins_p = np.linspace(np.min(seq_suc_p_var1), np.max(seq_suc_p_var1), nr_bins + 1)
n_p, bins_p = np.histogram(seq_suc_p_var1, nbins_p, density=1)
pdfx = np.zeros(n_p.size)
pdfy = np.zeros(n_p.size)
for k in range(n_p.size):
pdfx[k] = 0.5*(bins_p[k]+bins_p[k+1])
pdfy[k] = n_p[k]
ax[1].ticklabel_format(axis="both", style="sci", scilimits=(0,0))
ax[1].plot(pdfx,pdfy, linestyle='dashed', linewidth=2,color='gold',label='prior')
ax[1].axvline(mean_state_post_var1, linestyle='-',linewidth=2,color='red',label = 'mean post')
ax[1].axvline(mean_state_prior[state.index(plotvars[1])], linestyle='dashed',linewidth=2,color='gold',label = 'mean prior')
ax[1].set_xlabel(disp_nms_par[plotvars[1]] + ' ('+ disp_units_par[plotvars[1]] +')')
#ax[1].set_ylabel('Probability density (-)')
ax[1].legend(loc=0, frameon=True,prop={'size':21})
ax[0].annotate('(a)',
xy=(0.00, 1.082), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=20, fontweight='bold', ha='left', va='top')
ax[1].annotate('(b)',
xy=(0.00, 1.082), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=20, fontweight='bold',ha='left', va='top')
plt.subplots_adjust(left=0.05, right=0.96, top=0.93, bottom=0.10,wspace=0.1)
plt.savefig('pp_pdfpanel_posterior.'+figformat, format=figformat)
plt.rc('font', size=plotfontsize) #reset plot font size
if plot_2d_pdfs:
nbins = [nr_bins2d,nr_bins2d]
for i in range(len(state)):
for j in range(len(state)):
if j > i: #no need to have a pdf of both e.g. element 2 combined with 4, and 4 combined with 2
n, binsx, binsy = np.histogram2d(succes_state_ens[i],succes_state_ens[j], nbins, density=1)
x_1dpdf = np.zeros(np.size(binsx)-1)
y_1dpdf = np.zeros(np.size(binsy)-1)
for k in range(len(x_1dpdf)):
x_1dpdf[k] = 0.5*(binsx[k]+binsx[k+1])
for k in range(len(y_1dpdf)):
y_1dpdf[k] = 0.5*(binsy[k]+binsy[k+1])
fig = plt.figure()
if interp_pdf:
x_1dpdf_int = np.linspace(np.min(x_1dpdf),np.max(x_1dpdf),nr_bins_int)
y_1dpdf_int = np.linspace(np.min(y_1dpdf),np.max(y_1dpdf),nr_bins_int)
interpfunc = interp.interp2d(x_1dpdf,y_1dpdf,n)
n_int = interpfunc(x_1dpdf_int,y_1dpdf_int)
plot = plt.contourf(x_1dpdf_int,y_1dpdf_int,n_int,levels = nr_bins_int)
else:
plot = plt.contourf(x_1dpdf,y_1dpdf,n,levels = nr_bins2d)
cbar = plt.colorbar(plot)
cbar.set_label('density (-)', rotation=270, labelpad=20)
plt.xlabel(state[i] + ' ('+ disp_units_par[state[i]] +')')
plt.ylabel(state[j] + ' ('+ disp_units_par[state[j]] +')')
if not ('pdf2d_posterior_'+state[i]+'_'+state[j]+'.'+figformat).lower() in [x.lower() for x in os.listdir()]:
plt.savefig('pp_pdf2d_posterior_'+state[i]+'_'+state[j]+'.'+figformat, format=figformat)
else:
itemname = state[i]+'_'+state[j]+'_'
while ('pdf2d_posterior_'+itemname+'.'+figformat).lower() in [x.lower() for x in os.listdir()]:
itemname += '_'
plt.savefig('pp_pdf2d_posterior_'+itemname+'.'+figformat, format=figformat)
if plot_colored_corr_matr:
plt.figure()
sb.set(rc={'figure.figsize':(11,11)})
sb.set(font_scale=1.05)
disp_nms_state = []
for item in state:
if item in disp_nms_par:
disp_nms_state.append(disp_nms_par[item])
else:
disp_nms_state.append(item)
mask = None
if not showfullmatr:
mask = np.triu(np.ones(len(post_cor_matr)),k=1)
post_cor_matr_r = np.round(post_cor_matr,2) #_r to indicate rounded
plot = sb.heatmap(post_cor_matr_r,annot=True,xticklabels=disp_nms_state,yticklabels = disp_nms_state, cmap="RdBu_r",cbar_kws={'label': 'Correlation (-)'}, linewidths=0.7,annot_kws={"size": 8.9 },mask = mask)
plot.set_facecolor('white')
plot.tick_params(labelsize=11)
plt.ylim((len(state), 0))
plt.subplots_adjust(left=0.21, right=0.92, top=0.93, bottom=0.25,wspace=0.1)
plt.savefig('pp_correls.'+figformat)
#Now reset the plot params:
plt.rcParams.update(plt.rcParamsDefault)
style.use('classic')
plt.rc('font', size=plotfontsize) #reset plot font size
if TakeSubSample:
succes_state_ens_for_cor = np.zeros((len(state),len(succes_state_ens[0][Start::SelectStep])),dtype=float)
for i in range(len(state)):
succes_state_ens_for_cor[i,:] = succes_state_ens[i][Start::SelectStep]
post_cor_matr_ss = np.corrcoef(succes_state_ens_for_cor) #no ddof for np.corrcoef, gives DeprecationWarning
plt.figure()
Print1 = 'Nr of mems used for subsample colored_corr_matr:'
print(Print1)
Print2 = len(succes_state_ens_for_cor[0])
print(Print2)
if save_print_to_f:
open('pp_print.txt','a').write('{0:>69s}'.format(Print1+'\n'))
open('pp_print.txt','a').write('{0:>69s}'.format(str(Print2)+'\n'))
sb.set(rc={'figure.figsize':(11,11)})
sb.set(font_scale=1.05)
post_cor_matr_ss_r = np.round(post_cor_matr_ss,2)
plot = sb.heatmap(post_cor_matr_ss_r,annot=True,xticklabels=disp_nms_state,yticklabels = disp_nms_state, cmap="RdBu_r",cbar_kws={'label': 'Correlation (-)'}, linewidths=0.7,annot_kws={"size": 8.9 },mask = mask)
plot.set_facecolor('white')
plot.tick_params(labelsize=11)
plt.ylim((len(state), 0))
plt.subplots_adjust(left=0.21, right=0.92, top=0.93, bottom=0.25,wspace=0.1)
plt.savefig('pp_correls_subs.'+figformat)
#Now reset the plot params:
plt.rcParams.update(plt.rcParamsDefault)
style.use('classic')
plt.rc('font', size=plotfontsize) #plot font size
plt.figure()
sb.set(rc={'figure.figsize':(11,11)})
sb.set(font_scale=1.05)
post_cor_matr_diff = post_cor_matr_ss-post_cor_matr
post_cor_matr_diff_r = np.round(post_cor_matr_diff,2)
plot = sb.heatmap(post_cor_matr_diff_r,annot=True,xticklabels=disp_nms_state,yticklabels = disp_nms_state, cmap="RdBu_r",cbar_kws={'label': 'Diff in correlation (-)'}, linewidths=0.7,annot_kws={"size": 8.9 },mask = mask)
plot.set_facecolor('white')
plot.tick_params(labelsize=11)
plt.ylim((len(state), 0))
plt.subplots_adjust(left=0.21, right=0.92, top=0.93, bottom=0.25,wspace=0.1)
plt.savefig('pp_correls_diff.'+figformat)
post_cor_matr_diff_to_av = []#the one that we will estimate the mean off, excludes the diagonal elements
for i in range(len(post_cor_matr_diff)):
for j in range(len(post_cor_matr_diff[0])):
if j < i:
post_cor_matr_diff_to_av.append(post_cor_matr_diff[i,j])
Print3 = 'Mean abs value of change when using subsample colored_corr_matr:'
print(Print3)
Print4 = np.mean(np.abs(post_cor_matr_diff_to_av))
print(Print4)
if save_print_to_f:
open('pp_print.txt','a').write('{0:>69s}'.format(Print3+'\n'))
open('pp_print.txt','a').write('{0:>69s}'.format(str(Print4)+'\n'))
#Now reset the plot params:
plt.rcParams.update(plt.rcParamsDefault)
style.use('classic')
plt.rc('font', size=plotfontsize) #reset plot font size
if plot_obsfit:
for i in range(len(obsvarlist)):
unsca = 1 #a scale for plotting the obs with different units
if (disp_units[obsvarlist[i]] == 'g/kg' or disp_units[obsvarlist[i]] == 'g kg$^{-1}$') and (obsvarlist[i] == 'q' or obsvarlist[i].startswith('qmh')): #q can be plotted differently for clarity
unsca = 1000
fig = plt.figure()
if ('obs_sca_cf_'+obsvarlist[i] in state) and plot_errbars_at_sca_obs:
y_loc = optimalinput.__dict__['obs_sca_cf_'+obsvarlist[i]]*unsca*optim.__dict__['obs_'+obsvarlist[i]]
else:
y_loc = unsca*optim.__dict__['obs_'+obsvarlist[i]]
plt.errorbar(obs_times[obsvarlist[i]]/3600,y_loc,yerr=unsca*optim.__dict__['error_obs_'+obsvarlist[i]],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
plt.errorbar(obs_times[obsvarlist[i]]/3600,y_loc,yerr=unsca*measurement_error[obsvarlist[i]],ecolor='black',fmt='None',label = '$\sigma_{I}$')
plt.plot(priormodel.out.t,unsca*priormodel.out.__dict__[obsvarlist[i]], ls='dashed', marker='None',color='gold',linewidth = 2.0,label = 'prior')
plt.plot(priormodel.out.t,unsca*optimalmodel.out.__dict__[obsvarlist[i]], linestyle='-', marker='None',color='red',linewidth = 2.0,label = 'post')
if use_ensemble:
if pert_non_state_param and opt_sim_nr != 0:
plt.plot(priormodel.out.t,unsca*optimalmodel_onsp.out.__dict__[obsvarlist[i]], linestyle='dashdot', marker='None',color='magenta',linewidth = 2.0,label = 'post onsp')
plt.plot(obs_times[obsvarlist[i]]/3600,unsca*optim.__dict__['obs_'+obsvarlist[i]], linestyle=' ', marker='*',color = 'black',ms=10, label = 'obs')
if 'obs_sca_cf_'+obsvarlist[i] in state: #plot the obs scaled with the scaling factors (if applicable)
plt.plot(obs_times[obsvarlist[i]]/3600,optimalinput.__dict__['obs_sca_cf_'+obsvarlist[i]]*unsca*optim.__dict__['obs_'+obsvarlist[i]], linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs sca')
plt.ylabel(display_names[obsvarlist[i]] +' ('+ disp_units[obsvarlist[i]] + ')')
plt.xlabel('time (h)')
plt.subplots_adjust(left=0.18, right=0.92, top=0.96, bottom=0.15,wspace=0.1)
plt.legend(prop={'size':legendsize},loc=0)
if not ('pp_fig_fit_'+obsvarlist[i]+'.'+figformat).lower() in [x.lower() for x in os.listdir()]: #os.path.exists can be case-sensitive, depending on operating system
plt.savefig('pp_fig_fit_'+obsvarlist[i]+'.'+figformat, format=figformat)
else:
itemname = obsvarlist[i] + '_'
while ('pp_fig_fit_'+itemname+'.'+figformat).lower() in [x.lower() for x in os.listdir()]:
itemname += '_'
plt.savefig('pp_fig_fit_'+itemname+'.'+figformat, format=figformat)#Windows cannnot have a file 'fig_fit_h' and 'fig_fit_H' in the same folder. The while loop can also handle e.g. the combination of variables Abc, ABC and abc
if 'FracH' in state:
if 'H' in obsvarlist:
enbal_corr_H = optim.obs_H + optimalinput.FracH * optim.EnBalDiffObs_atHtimes
fig = plt.figure()
plt.errorbar(obs_times['H']/3600,enbal_corr_H,yerr=optim.__dict__['error_obs_H'],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
plt.errorbar(obs_times['H']/3600,enbal_corr_H,yerr=measurement_error['H'],ecolor='black',fmt='None',label = '$\sigma_{I}$')
plt.plot(priormodel.out.t,priormodel.out.H, ls='dashed', marker='None',color='gold',linewidth = 2.0,label = 'prior')
plt.plot(optimalmodel.out.t,optimalmodel.out.H, linestyle='-', marker='None',color='red',linewidth = 2.0,label = 'post')
if use_ensemble:
if pert_non_state_param and opt_sim_nr != 0:
plt.plot(optimalmodel.out.t,optimalmodel_onsp.out.H, linestyle='dashdot', marker='None',color='magenta',linewidth = 2.0,label = 'post onsp')
plt.plot(obs_times['H']/3600,optim.__dict__['obs_'+'H'], linestyle=' ', marker='*',color = 'black',ms=10,label = 'obs ori')
plt.plot(obs_times['H']/3600,enbal_corr_H, linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs cor')
plt.ylabel('H (' + disp_units['H']+')')
plt.xlabel('time (h)')
plt.legend(prop={'size':legendsize},loc=0)
plt.subplots_adjust(left=0.18, right=0.92, top=0.96, bottom=0.15,wspace=0.1)
plt.savefig('pp_fig_fit_enbalcorrH.'+figformat, format=figformat)
if 'LE' in obsvarlist:
enbal_corr_LE = optim.obs_LE + (1 - optimalinput.FracH) * optim.EnBalDiffObs_atLEtimes
fig = plt.figure()
plt.errorbar(obs_times['LE']/3600,enbal_corr_LE,yerr=optim.__dict__['error_obs_LE'],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
plt.errorbar(obs_times['LE']/3600,enbal_corr_LE,yerr=measurement_error['LE'],ecolor='black',fmt='None',label = '$\sigma_{I}$')
plt.plot(priormodel.out.t,priormodel.out.LE, ls='dashed', marker='None',color='gold',linewidth = 2.0,label = 'prior')
plt.plot(optimalmodel.out.t,optimalmodel.out.LE, linestyle='-', marker='None',color='red',linewidth = 2.0,label = 'post')
if use_ensemble:
if pert_non_state_param and opt_sim_nr != 0:
plt.plot(optimalmodel.out.t,optimalmodel_onsp.out.LE, linestyle='dashdot', marker='None',color='magenta',linewidth = 2.0,label = 'post onsp')
plt.plot(obs_times['LE']/3600,optim.__dict__['obs_'+'LE'], linestyle=' ', marker='*',color = 'black',ms=10,label = 'obs ori')
plt.plot(obs_times['LE']/3600,enbal_corr_LE, linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs cor')
plt.ylabel('LE (' + disp_units['LE']+')')
plt.xlabel('time (h)')
plt.legend(prop={'size':legendsize},loc=0)
plt.subplots_adjust(left=0.18, right=0.92, top=0.96, bottom=0.15,wspace=0.1)
plt.savefig('pp_fig_fit_enbalcorrLE.'+figformat, format=figformat)
if plot_co2profiles:
#The following code can be used to plot profiles of CO2 (adapt depending on the optimisation performed)
profileheights = np.array([priorinput.CO2measuring_height4,priorinput.CO2measuring_height3,priorinput.CO2measuring_height2,priorinput.CO2measuring_height])
colorlist = ['red','gold','green','blue','orange','pink']
markerlist = ['x','v','s','p']
fig = plt.figure()
i = 0
startind = 30*60/priorinput.dt
if not (int(startind) == startind):
raise Exception('Invalid starting index for CO2 profiles')
for ti in range(int(startind),priormodel.tsteps,120): #int since a float is not allowed as a starting number for range
color = colorlist[i]
plt.plot(priormodel.out.__dict__['CO2mh'][ti],profileheights[3], linestyle=' ', marker='o',color=color,label = 'pmod t='+str((priorinput.tstart*3600+ti*priorinput.dt)/3600))
plt.plot(priormodel.out.__dict__['CO2mh2'][ti],profileheights[2], linestyle=' ', marker='o',color=color)
plt.plot(priormodel.out.__dict__['CO2mh3'][ti],profileheights[1], linestyle=' ', marker='o',color=color)
plt.plot(priormodel.out.__dict__['CO2mh4'][ti],profileheights[0], linestyle=' ', marker='o',color=color)
i += 1
plt.ylabel('height (m)')
plt.xlabel('CO2 mixing ratio ('+disp_units['CO2mh']+')')
plt.ylim([np.min(profileheights)-0.01*(np.max(profileheights)-np.min(profileheights)),np.max(profileheights)+0.01*(np.max(profileheights)-np.min(profileheights))])
i = 0
for ti in range(0,len(obs_times['CO2mh']),2):
marker = markerlist[i]
color = colorlist[i]
plt.plot(optim.obs_CO2mh[ti],profileheights[3], linestyle=' ', marker=marker,color=color,label = 'obs t='+str((obs_times['CO2mh'][ti])/3600))
plt.plot(optim.obs_CO2mh2[ti],profileheights[2], linestyle=' ', marker=marker,color=color)
plt.plot(optim.obs_CO2mh3[ti],profileheights[1], linestyle=' ', marker=marker,color=color)
plt.plot(optim.obs_CO2mh4[ti],profileheights[0], linestyle=' ', marker=marker,color=color)
i += 1
plt.legend(fontsize=legendsize,loc=0) #plt.legend(fontsize=8,loc=0)
plt.subplots_adjust(left=0.17, right=0.92, top=0.96, bottom=0.15,wspace=0.1)
plt.savefig('pp_fig_'+'CO2'+'_profile_prior.'+figformat, format=figformat)
fig = plt.figure()
i = 0
for ti in range(int(startind),priormodel.tsteps,120):
color = colorlist[i]
plt.plot(optimalmodel.out.__dict__['CO2mh'][ti],profileheights[3], linestyle=' ', marker='o',color=color,label = 'mod t='+str((priorinput.tstart*3600+ti*priorinput.dt)/3600))
plt.plot(optimalmodel.out.__dict__['CO2mh2'][ti],profileheights[2], linestyle=' ', marker='o',color=color)
plt.plot(optimalmodel.out.__dict__['CO2mh3'][ti],profileheights[1], linestyle=' ', marker='o',color=color)
plt.plot(optimalmodel.out.__dict__['CO2mh4'][ti],profileheights[0], linestyle=' ', marker='o',color=color)
i += 1
plt.ylabel('height (m)')
plt.xlabel('CO2 mixing ratio ('+disp_units['CO2mh']+')')
plt.ylim([np.min(profileheights)-0.01*(np.max(profileheights)-np.min(profileheights)),np.max(profileheights)+0.01*(np.max(profileheights)-np.min(profileheights))])
i = 0
for ti in range(0,len(obs_times['CO2mh']),2):
marker = markerlist[i]
color = colorlist[i]
plt.plot(optim.obs_CO2mh[ti],profileheights[3], linestyle=' ', marker=marker,color=color,label = 'obs t='+str((obs_times['CO2mh'][ti])/3600))
plt.plot(optim.obs_CO2mh2[ti],profileheights[2], linestyle=' ', marker=marker,color=color)
plt.plot(optim.obs_CO2mh3[ti],profileheights[1], linestyle=' ', marker=marker,color=color)
plt.plot(optim.obs_CO2mh4[ti],profileheights[0], linestyle=' ', marker=marker,color=color)
i += 1
plt.legend(fontsize=legendsize,loc=0)
plt.subplots_adjust(left=0.17, right=0.92, top=0.96, bottom=0.15,wspace=0.1)
plt.savefig('pp_fig_'+'CO2'+'_profile.'+figformat, format=figformat)
if plot_manual_fitpanels:
plotvars = ['h','qmh']
unsca = np.ones(len(plotvars)) #a scale for plotting the obs with different units
for i in range(len(plotvars)):
if (disp_units[plotvars[i]] == 'g/kg' or disp_units[plotvars[i]] == 'g kg$^{-1}$') and (plotvars[i] == 'q' or plotvars[i].startswith('qmh')): #q can be plotted differently for clarity
unsca[i] = 1000
plt.rc('font', size=22)
fig, ax = plt.subplots(1,2,figsize=(24,8))
ax[0].errorbar(obs_times[plotvars[0]]/3600,unsca[0]*optim.__dict__['obs_'+plotvars[0]],yerr=unsca[0]*optim.__dict__['error_obs_'+plotvars[0]],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
ax[0].errorbar(obs_times[plotvars[0]]/3600,unsca[0]*optim.__dict__['obs_'+plotvars[0]],yerr=unsca[0]*measurement_error[plotvars[0]],ecolor='black',fmt='None',label = '$\sigma_{I}$')
ax[0].plot(priormodel.out.t,unsca[0]*priormodel.out.__dict__[plotvars[0]], ls='dashed', marker='None',color='gold',linewidth = 4.0,label = 'prior',dashes = (4,4))
ax[0].plot(optimalmodel.out.t,unsca[0]*optimalmodel.out.__dict__[plotvars[0]], linestyle='-', marker='None',color='red',linewidth = 4.0,label = 'post')
ax[0].plot(obs_times[plotvars[0]]/3600,unsca[0]*optim.__dict__['obs_'+plotvars[0]], linestyle=' ', marker='*',color = 'black',ms=10,label = 'obs')
if 'obs_sca_cf_'+plotvars[0] in state: #plot the obs scaled with the scaling factors (if applicable)
ax[0].plot(obs_times[plotvars[0]]/3600,optimalinput.__dict__['obs_sca_cf_'+plotvars[0]]*unsca[0]*optim.__dict__['obs_'+plotvars[0]], linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs sca')
ax[0].set_ylabel(display_names[plotvars[0]] +' ('+ disp_units[plotvars[0]] + ')')
ax[0].set_xlabel('time (h)')
ax[1].errorbar(obs_times[plotvars[1]]/3600,unsca[1]*optim.__dict__['obs_'+plotvars[1]],yerr=unsca[1]*optim.__dict__['error_obs_'+plotvars[1]],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
ax[1].errorbar(obs_times[plotvars[1]]/3600,unsca[1]*optim.__dict__['obs_'+plotvars[1]],yerr=unsca[1]*measurement_error[plotvars[1]],ecolor='black',fmt='None',label = '$\sigma_{I}$')
ax[1].plot(priormodel.out.t,unsca[1]*priormodel.out.__dict__[plotvars[1]], ls='dashed', marker='None',color='gold',linewidth = 4.0,label = 'prior',dashes = (4,4))
ax[1].plot(optimalmodel.out.t,unsca[1]*optimalmodel.out.__dict__[plotvars[1]], linestyle='-', marker='None',color='red',linewidth = 4.0,label = 'post')
ax[1].plot(obs_times[plotvars[1]]/3600,unsca[1]*optim.__dict__['obs_'+plotvars[1]], linestyle=' ', marker='*',color = 'black',ms=10, label = 'obs')
if 'obs_sca_cf_'+plotvars[1] in state: #plot the obs scaled with the scaling factors (if applicable)
ax[1].plot(obs_times[plotvars[1]]/3600,optimalinput.__dict__['obs_sca_cf_'+plotvars[1]]*unsca[1]*optim.__dict__['obs_'+plotvars[1]], linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs sca')
ax[1].set_ylabel(display_names[plotvars[1]] +' ('+ disp_units[plotvars[1]] + ')')
ax[1].set_xlabel('time (h)')
ax[1].legend(loc=0, frameon=False,prop={'size':21})
ax[0].annotate('(a)',
xy=(0.00, 1.07), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=20, fontweight='bold', ha='left', va='top')
ax[1].annotate('(b)',
xy=(0.00, 1.07), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=20, fontweight='bold',ha='left', va='top')
plt.savefig('pp_fig_fitpanel1.'+figformat, format=figformat)
plt.rc('font', size=17)
plotvars = ['h','qmh','wCO2','Tmh'] #first the var for 0,0 than 0,1 than 1,0 than 1,1
unsca = np.ones(len(plotvars)) #a scale for plotting the obs with different units
for i in range(len(plotvars)):
if (disp_units[plotvars[i]] == 'g/kg' or disp_units[plotvars[i]] == 'g kg$^{-1}$') and (plotvars[i] == 'q' or plotvars[i].startswith('qmh')): #q can be plotted differently for clarity
unsca[i] = 1000
fig, ax = plt.subplots(2,2,figsize=(16,12))
ax[0,0].errorbar(obs_times[plotvars[0]]/3600,unsca[0]*optim.__dict__['obs_'+plotvars[0]],yerr=unsca[0]*optim.__dict__['error_obs_'+plotvars[0]],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
ax[0,0].errorbar(obs_times[plotvars[0]]/3600,unsca[0]*optim.__dict__['obs_'+plotvars[0]],yerr=unsca[0]*measurement_error[plotvars[0]],ecolor='black',fmt='None',label = '$\sigma_{I}$')
ax[0,0].plot(priormodel.out.t,unsca[0]*priormodel.out.__dict__[plotvars[0]], ls='dashed', marker='None',color='gold',linewidth = 4.0,label = 'prior',dashes = (4,4))
ax[0,0].plot(optimalmodel.out.t,unsca[0]*optimalmodel.out.__dict__[plotvars[0]], linestyle='-', marker='None',color='red',linewidth = 4.0,label = 'post')
ax[0,0].plot(obs_times[plotvars[0]]/3600,unsca[0]*optim.__dict__['obs_'+plotvars[0]], linestyle=' ', marker='*',color = 'black',ms=10,label = 'obs')
if 'obs_sca_cf_'+plotvars[0] in state: #plot the obs scaled with the scaling factors (if applicable)
ax[0,0].plot(obs_times[plotvars[0]]/3600,optimalinput.__dict__['obs_sca_cf_'+plotvars[0]]*unsca[0]*optim.__dict__['obs_'+plotvars[0]], linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs sca')
ax[0,0].set_ylabel(display_names[plotvars[0]] +' ('+ disp_units[plotvars[0]] + ')')
ax[0,0].set_xlabel('time (h)')
ax[0,1].errorbar(obs_times[plotvars[1]]/3600,unsca[1]*optim.__dict__['obs_'+plotvars[1]],yerr=unsca[1]*optim.__dict__['error_obs_'+plotvars[1]],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
ax[0,1].errorbar(obs_times[plotvars[1]]/3600,unsca[1]*optim.__dict__['obs_'+plotvars[1]],yerr=unsca[1]*measurement_error[plotvars[1]],ecolor='black',fmt='None',label = '$\sigma_{I}$')
ax[0,1].plot(priormodel.out.t,unsca[1]*priormodel.out.__dict__[plotvars[1]], ls='dashed', marker='None',color='gold',linewidth = 4.0,label = 'prior',dashes = (4,4))
ax[0,1].plot(optimalmodel.out.t,unsca[1]*optimalmodel.out.__dict__[plotvars[1]], linestyle='-', marker='None',color='red',linewidth = 4.0,label = 'post')
ax[0,1].plot(obs_times[plotvars[1]]/3600,unsca[1]*optim.__dict__['obs_'+plotvars[1]], linestyle=' ', marker='*',color = 'black',ms=10, label = 'obs')
if 'obs_sca_cf_'+plotvars[1] in state: #plot the obs scaled with the scaling factors (if applicable)
ax[0,1].plot(obs_times[plotvars[1]]/3600,optimalinput.__dict__['obs_sca_cf_'+plotvars[1]]*unsca[1]*optim.__dict__['obs_'+plotvars[1]], linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs sca')
ax[0,1].set_ylabel(display_names[plotvars[1]] +' ('+ disp_units[plotvars[1]] + ')')
ax[0,1].set_xlabel('time (h)')
ax[1,0].errorbar(obs_times[plotvars[2]]/3600,unsca[2]*optim.__dict__['obs_'+plotvars[2]],yerr=unsca[2]*optim.__dict__['error_obs_'+plotvars[2]],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
ax[1,0].errorbar(obs_times[plotvars[2]]/3600,unsca[2]*optim.__dict__['obs_'+plotvars[2]],yerr=unsca[2]*measurement_error[plotvars[2]],ecolor='black',fmt='None',label = '$\sigma_{I}$')
ax[1,0].plot(priormodel.out.t,unsca[2]*priormodel.out.__dict__[plotvars[2]], ls='dashed', marker='None',color='gold',linewidth = 4.0,label = 'prior',dashes = (4,4))
ax[1,0].plot(optimalmodel.out.t,unsca[2]*optimalmodel.out.__dict__[plotvars[2]], linestyle='-', marker='None',color='red',linewidth = 4.0,label = 'post')
ax[1,0].plot(obs_times[plotvars[2]]/3600,unsca[2]*optim.__dict__['obs_'+plotvars[2]], linestyle=' ', marker='*',color = 'black',ms=10,label = 'obs')
if 'obs_sca_cf_'+plotvars[2] in state: #plot the obs scaled with the scaling factors (if applicable)
ax[1,0].plot(obs_times[plotvars[2]]/3600,optimalinput.__dict__['obs_sca_cf_'+plotvars[2]]*unsca[2]*optim.__dict__['obs_'+plotvars[2]], linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs sca')
ax[1,0].set_ylabel(display_names[plotvars[2]] +' ('+ disp_units[plotvars[2]] + ')')
ax[1,0].set_xlabel('time (h)')
ax[1,1].errorbar(obs_times[plotvars[3]]/3600,unsca[3]*optim.__dict__['obs_'+plotvars[3]],yerr=unsca[3]*optim.__dict__['error_obs_'+plotvars[3]],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
ax[1,1].errorbar(obs_times[plotvars[3]]/3600,unsca[3]*optim.__dict__['obs_'+plotvars[3]],yerr=unsca[3]*measurement_error[plotvars[3]],ecolor='black',fmt='None',label = '$\sigma_{I}$')
ax[1,1].plot(priormodel.out.t,unsca[3]*priormodel.out.__dict__[plotvars[3]], ls='dashed', marker='None',color='gold',linewidth = 4.0,label = 'prior',dashes = (4,4))
ax[1,1].plot(optimalmodel.out.t,unsca[3]*optimalmodel.out.__dict__[plotvars[3]], linestyle='-', marker='None',color='red',linewidth = 4.0,label = 'post')
ax[1,1].plot(obs_times[plotvars[3]]/3600,unsca[3]*optim.__dict__['obs_'+plotvars[3]], linestyle=' ', marker='*',color = 'black',ms=10, label = 'obs')
if 'obs_sca_cf_'+plotvars[3] in state: #plot the obs scaled with the scaling factors (if applicable)
ax[1,1].plot(obs_times[plotvars[3]]/3600,optimalinput.__dict__['obs_sca_cf_'+plotvars[3]]*unsca[3]*optim.__dict__['obs_'+plotvars[3]], linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs sca')
ax[1,1].set_ylabel(display_names[plotvars[3]] +' ('+ disp_units[plotvars[3]] + ')')
ax[1,1].set_xlabel('time (h)')
ax[1,1].legend(loc=0, frameon=False,prop={'size':17})
ax[0,0].annotate('(a)',
xy=(0.00, 1.07), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=16, fontweight='bold', ha='left', va='top')
ax[0,1].annotate('(b)',
xy=(0.00, 1.07), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=16, fontweight='bold',ha='left', va='top')
ax[1,0].annotate('(c)',
xy=(0.00, 1.07), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=16, fontweight='bold',ha='left', va='top')
ax[1,1].annotate('(d)',
xy=(0.00, 1.07), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=16, fontweight='bold',ha='left', va='top')
plt.savefig('pp_fig_fitpanel2.'+figformat, format=figformat)
plt.rc('font', size=plotfontsize) #reset plot font size
if plot_enbal_panel:
plt.rc('font', size=19)
fig, ax = plt.subplots(1,2,figsize=(24,8))
enbal_corr_H = optim.obs_H + optimalinput.FracH * optim.EnBalDiffObs_atHtimes
ax[0].errorbar(obs_times['H']/3600,enbal_corr_H,yerr=optim.__dict__['error_obs_H'],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
ax[0].errorbar(obs_times['H']/3600,enbal_corr_H,yerr=measurement_error['H'],ecolor='black',fmt='None',label = '$\sigma_{I}$')
ax[0].plot(priormodel.out.t,priormodel.out.H, ls='dashed', marker='None',color='gold',linewidth = 2.0,label = 'prior')
ax[0].plot(optimalmodel.out.t,optimalmodel.out.H, linestyle='-', marker='None',color='red',linewidth = 2.0,label = 'post')
if use_ensemble:
if pert_non_state_param and opt_sim_nr != 0:
ax[0].plot(optimalmodel.out.t,optimalmodel_onsp.out.H, linestyle='dashdot', marker='None',color='magenta',linewidth = 2.0,label = 'post onsp')
ax[0].plot(obs_times['H']/3600,optim.__dict__['obs_'+'H'], linestyle=' ', marker='*',color = 'black',ms=10,label = 'obs ori')
ax[0].plot(obs_times['H']/3600,enbal_corr_H, linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs cor')
ax[0].set_ylabel('H (' + disp_units['H']+')')
ax[0].set_xlabel('Time (h)')
enbal_corr_LE = optim.obs_LE + (1 - optimalinput.FracH) * optim.EnBalDiffObs_atLEtimes
ax[1].errorbar(obs_times['LE']/3600,enbal_corr_LE,yerr=optim.__dict__['error_obs_LE'],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
ax[1].errorbar(obs_times['LE']/3600,enbal_corr_LE,yerr=measurement_error['LE'],ecolor='black',fmt='None',label = '$\sigma_{I}$')
ax[1].plot(priormodel.out.t,priormodel.out.LE, ls='dashed', marker='None',color='gold',linewidth = 2.0,label = 'prior')
ax[1].plot(optimalmodel.out.t,optimalmodel.out.LE, linestyle='-', marker='None',color='red',linewidth = 2.0,label = 'post')
if use_ensemble:
if pert_non_state_param and opt_sim_nr != 0:
ax[1].plot(optimalmodel.out.t,optimalmodel_onsp.out.LE, linestyle='dashdot', marker='None',color='magenta',linewidth = 2.0,label = 'post onsp')
ax[1].plot(obs_times['LE']/3600,optim.__dict__['obs_'+'LE'], linestyle=' ', marker='*',color = 'black',ms=10,label = 'obs ori')
ax[1].plot(obs_times['LE']/3600,enbal_corr_LE, linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs cor')
ax[1].set_ylabel('LE (' + disp_units['LE']+')')
ax[1].set_xlabel('Time (h)')
ax[1].legend(prop={'size':18},loc=0)
plt.subplots_adjust(left=0.10, right=0.94, top=0.94, bottom=0.15,wspace=0.1)
ax[0].annotate('(a)',
xy=(0.00, 1.07), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=20, fontweight='bold', ha='left', va='top')
ax[1].annotate('(b)',
xy=(0.00, 1.07), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=20, fontweight='bold',ha='left', va='top')
plt.savefig('pp_fig_enbalpanel.'+figformat, format=figformat)
plt.rc('font', size=plotfontsize) #reset plot font size
if plot_auto_fitpanels:
#Below is a more automatised panel plot:
plt.rc('font', size=17)
plotvars = ['h','qmh','wCO2','Tmh'] #first the var for 0,0 than 0,1 than 1,0 than 1,1
annotatelist = ['(a)','(b)','(c)','(d)']
unsca = np.ones(len(plotvars)) #a scale for plotting the obs with different units
for i in range(len(plotvars)):
if (disp_units[plotvars[i]] == 'g/kg' or disp_units[plotvars[i]] == 'g kg$^{-1}$') and (plotvars[i] == 'q' or plotvars[i].startswith('qmh')): #q can be plotted differently for clarity
unsca[i] = 1000
nr_rows,nr_cols = 2,2
fig, ax = plt.subplots(nr_rows,nr_cols,figsize=(16,12))
k = 0
for i in range(nr_rows):
for j in range(nr_cols):
if ('obs_sca_cf_'+plotvars[k] in state) and plot_errbars_at_sca_obs:
y_loc = optimalinput.__dict__['obs_sca_cf_'+plotvars[k]]*unsca[k]*optim.__dict__['obs_'+plotvars[k]]
else:
y_loc = unsca[k]*optim.__dict__['obs_'+plotvars[k]]
ax[i,j].errorbar(obs_times[plotvars[k]]/3600,y_loc,yerr=unsca[k]*optim.__dict__['error_obs_'+plotvars[k]],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
ax[i,j].errorbar(obs_times[plotvars[k]]/3600,y_loc,yerr=unsca[k]*measurement_error[plotvars[k]],ecolor='black',fmt='None',label = '$\sigma_{I}$')
ax[i,j].plot(priormodel.out.t,unsca[k]*priormodel.out.__dict__[plotvars[k]], ls='dashed', marker='None',color='gold',linewidth = 4.0,label = 'prior',dashes = (4,4))
ax[i,j].plot(optimalmodel.out.t,unsca[k]*optimalmodel.out.__dict__[plotvars[k]], linestyle='-', marker='None',color='red',linewidth = 4.0,label = 'post')
ax[i,j].plot(obs_times[plotvars[k]]/3600,unsca[k]*optim.__dict__['obs_'+plotvars[k]], linestyle=' ', marker='*',color = 'black',ms=10,label = 'obs')
if 'obs_sca_cf_'+plotvars[k] in state: #plot the obs scaled with the scaling factors (if applicable)
ax[i,j].plot(obs_times[plotvars[k]]/3600,optimalinput.__dict__['obs_sca_cf_'+plotvars[k]]*unsca[k]*optim.__dict__['obs_'+plotvars[k]], linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs sca')
ax[i,j].set_ylabel(display_names[plotvars[k]] +' ('+ disp_units[plotvars[k]] + ')')
ax[i,j].set_xlabel('time (h)')
ax[i,j].annotate(annotatelist[k],
xy=(0.00, 1.07), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=16, fontweight='bold', ha='left', va='top')
k += 1
ax[1,1].legend(loc=0, frameon=False,prop={'size':19})
plt.savefig('pp_fig_fitpanel_auto1.'+figformat, format=figformat)
#And another one
plotvars = ['Tmh2','Tmh7','CO2mh','CO2mh2'] #first the var for 0,0 than 0,1 than 1,0 than 1,1
annotatelist = ['(a)','(b)','(c)','(d)']
unsca = np.ones(len(plotvars)) #a scale for plotting the obs with different units
for i in range(len(plotvars)):
if (disp_units[plotvars[i]] == 'g/kg' or disp_units[plotvars[i]] == 'g kg$^{-1}$') and (plotvars[i] == 'q' or plotvars[i].startswith('qmh')): #q can be plotted differently for clarity
unsca[i] = 1000
nr_rows,nr_cols = 2,2
fig, ax = plt.subplots(nr_rows,nr_cols,figsize=(16,12))
k = 0
for i in range(nr_rows):
for j in range(nr_cols):
if ('obs_sca_cf_'+plotvars[k] in state) and plot_errbars_at_sca_obs:
y_loc = optimalinput.__dict__['obs_sca_cf_'+plotvars[k]]*unsca[k]*optim.__dict__['obs_'+plotvars[k]]
else:
y_loc = unsca[k]*optim.__dict__['obs_'+plotvars[k]]
ax[i,j].errorbar(obs_times[plotvars[k]]/3600,y_loc,yerr=unsca[k]*optim.__dict__['error_obs_'+plotvars[k]],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2,capsize = 0)
ax[i,j].errorbar(obs_times[plotvars[k]]/3600,y_loc,yerr=unsca[k]*measurement_error[plotvars[k]],ecolor='black',fmt='None',label = '$\sigma_{I}$')
ax[i,j].plot(priormodel.out.t,unsca[k]*priormodel.out.__dict__[plotvars[k]], ls='dashed', marker='None',color='gold',linewidth = 4.0,label = 'prior',dashes = (4,4))
ax[i,j].plot(optimalmodel.out.t,unsca[k]*optimalmodel.out.__dict__[plotvars[k]], linestyle='-', marker='None',color='red',linewidth = 4.0,label = 'post')
ax[i,j].plot(obs_times[plotvars[k]]/3600,unsca[k]*optim.__dict__['obs_'+plotvars[k]], linestyle=' ', marker='*',color = 'black',ms=10,label = 'obs')
if 'obs_sca_cf_'+plotvars[k] in state: #plot the obs scaled with the scaling factors (if applicable)
ax[i,j].plot(obs_times[plotvars[k]]/3600,optimalinput.__dict__['obs_sca_cf_'+plotvars[k]]*unsca[k]*optim.__dict__['obs_'+plotvars[k]], linestyle=' ', marker='o',color = 'red',ms=10,label = 'obs sca')
ax[i,j].set_ylabel(display_names[plotvars[k]] +' ('+ disp_units[plotvars[k]] + ')')
ax[i,j].set_xlabel('time (h)')
ax[i,j].annotate(annotatelist[k],
xy=(0.00, 1.07), xytext=(0,0),
xycoords=('axes fraction', 'axes fraction'),
textcoords='offset points',
size=16, fontweight='bold', ha='left', va='top')
k += 1
ax[1,1].legend(loc=0, frameon=False,prop={'size':19})
plt.savefig('pp_fig_fitpanel_auto2.'+figformat, format=figformat)
plt.rc('font', size=plotfontsize) #reset plot font size
#And another one
plotvars = ['h','qmh','wCO2','Tmh','Tmh2','Tmh7','CO2mh','CO2mh2'] #first the var for 0,0 than 0,1 than 1,0 than 1,1
annotatelist = ['(a)','(b)','(c)','(d)','(e)','(f)','(g)','(h)']
unsca = np.ones(len(plotvars)) #a scale for plotting the obs with different units
for i in range(len(plotvars)):
if (disp_units[plotvars[i]] == 'g/kg' or disp_units[plotvars[i]] == 'g kg$^{-1}$') and (plotvars[i] == 'q' or plotvars[i].startswith('qmh')): #q can be plotted differently for clarity
unsca[i] = 1000
nr_rows,nr_cols = 4,2
fig, ax = plt.subplots(nr_rows,nr_cols,figsize=(23,24))
mfs = 20 #font size
k = 0
for i in range(nr_rows):
for j in range(nr_cols):
if ('obs_sca_cf_'+plotvars[k] in state) and plot_errbars_at_sca_obs:
y_loc = optimalinput.__dict__['obs_sca_cf_'+plotvars[k]]*unsca[k]*optim.__dict__['obs_'+plotvars[k]]
else:
y_loc = unsca[k]*optim.__dict__['obs_'+plotvars[k]]
ax[i,j].errorbar(obs_times[plotvars[k]]/3600,y_loc,yerr=unsca[k]*optim.__dict__['error_obs_'+plotvars[k]],ecolor='lightgray',fmt='None',label = '$\sigma_{O}$', elinewidth=2.5,capsize = 0)
ax[i,j].errorbar(obs_times[plotvars[k]]/3600,y_loc,yerr=unsca[k]*measurement_error[plotvars[k]],ecolor='black',fmt='None',label = '$\sigma_{I}$', elinewidth=1.5,capsize = 5)
ax[i,j].plot(priormodel.out.t,unsca[k]*priormodel.out.__dict__[plotvars[k]], ls='dashed', marker='None',color='gold',linewidth = 4.0,label = 'prior',dashes = (4,4))
ax[i,j].plot(optimalmodel.out.t,unsca[k]*optimalmodel.out.__dict__[plotvars[k]], linestyle='-', marker='None',color='red',linewidth = 4.0,label = 'post')
ax[i,j].plot(obs_times[plotvars[k]]/3600,unsca[k]*optim.__dict__['obs_'+plotvars[k]], linestyle=' ', marker='*',color = 'black',ms=12,label = 'obs')
if 'obs_sca_cf_'+plotvars[k] in state: #plot the obs scaled with the scaling factors (if applicable)
ax[i,j].plot(obs_times[plotvars[k]]/3600,optimalinput.__dict__['obs_sca_cf_'+plotvars[k]]*unsca[k]*optim.__dict__['obs_'+plotvars[k]], linestyle=' ', marker='o',color = 'red',ms=12,label = 'obs sca')
ax[i,j].set_ylabel(display_names[plotvars[k]] +' ('+ disp_units[plotvars[k]] + ')',fontsize=mfs)
ax[i,j].set_xlabel('Time (h)',fontsize=mfs)