forked from daspy/daspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAS_Driver.py
2333 lines (1906 loc) · 175 KB
/
DAS_Driver.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 -*-
'''
Copyright of DasPy:
Author - Xujun Han (Forschungszentrum Jülich, Germany)
DasPy was funded by:
1. Forschungszentrum Jülich, Agrosphere (IBG 3), Jülich, Germany
2. Cold and Arid Regions Environmental and Engineering Research Institute, Chinese Academy of Sciences, Lanzhou, PR China
3. Centre for High-Performance Scientific Computing in Terrestrial Systems: HPSC TerrSys, Geoverbund ABC/J, Jülich, Germany
Please include the following references related to DasPy:
1. Han, X., Li, X., He, G., Kumbhar, P., Montzka, C., Kollet, S., Miyoshi, T., Rosolem, R., Zhang, Y., Vereecken, H., and Franssen, H. J. H.:
DasPy 1.0 : the Open Source Multivariate Land Data Assimilation Framework in combination with the Community Land Model 4.5, Geosci. Model Dev. Discuss., 8, 7395-7444, 2015.
2. Han, X., Franssen, H. J. H., Rosolem, R., Jin, R., Li, X., and Vereecken, H.:
Correction of systematic model forcing bias of CLM using assimilation of cosmic-ray Neutrons and land surface temperature: a study in the Heihe Catchment, China, Hydrology and Earth System Sciences, 19, 615-629, 2015a.
3. Han, X., Franssen, H. J. H., Montzka, C., and Vereecken, H.:
Soil moisture and soil properties estimation in the Community Land Model with synthetic brightness temperature observations, Water Resour Res, 50, 6081-6105, 2014a.
4. Han, X., Franssen, H. J. H., Li, X., Zhang, Y. L., Montzka, C., and Vereecken, H.:
Joint Assimilation of Surface Temperature and L-Band Microwave Brightness Temperature in Land Data Assimilation, Vadose Zone J, 12, 0, 2013.
'''
from mpi4py import MPI
import multiprocessing, shutil
from DAS_Assim_Common import *
from DAS_Assim import *
from DAS_Misc import *
from DAS_Driver_Common import *
from DAS_Utilities import *
# Data Assimilation, Parameter Estimation, Bias Estimation
def DAS_Driver(mpi4py_comm, mpi4py_null, mpi4py_rank, mpi4py_size, mpi4py_name, Model_Driver,Do_DA_Flag, Def_Par_Sensitivity, Def_Par_Correlation, Def_Par_Optimized, Dim_Soil_Par, Dim_Veg_Par, Dim_PFT_Par, Dim_Hard_Par, Soil_Texture_Layer_Opt_Num, Observation_Box, LAI_Year_String, MODIS_LAI_Data_ID,\
Num_of_Days_Monthly, Start_Year, Start_Month, Start_Day, Start_Hour, Start_Minute, End_Year, End_Month, End_Day, End_Hour, End_Minute, Datetime_Start, Datetime_Start_Init, \
Datetime_End, Datetime_End_Init, Datetime_Initial, UTC_Zone, CLM_NA, NAvalue, Assim_Algorithm_Name, Station_XY, Station_XY_Index, dtime,\
NSLOTS, Feedback_Assim, Parameter_Optimization, Parameter_Regularization, Def_CDF_Matching, Bias_Estimation_Option_Model, Bias_Estimation_Option_Obs, Post_Inflation_Alpha, Def_Snow_Effects, N0, nlyr,\
Sub_Block_Ratio_Row, Sub_Block_Ratio_Col, Sub_Block_Index_Row_Mat_Vector, Sub_Block_Index_Col_Mat_Vector, Row_Offset, Col_Offset,
Row_Numbers_SubBlock_Array, Col_Numbers_SubBlock_Array, Sub_Block_Row_Start_Array, Sub_Block_Row_End_Array, Sub_Block_Col_Start_Array, Sub_Block_Col_End_Array,
Observation_Time_File_Path, Def_CESM_Multi_Instance, Constant_File_Name_Header, finidat_initial_CLM, finidat_initial_PFCLM, Def_PP, DAS_Fortran_Lib, Normal_Score_Trans, PDAF_Assim_Framework, PDAF_Filter_Type, \
Def_ParFor, Def_Region, Def_Initial, Irrig_Scheduling, Irrigation_Hours, Def_SpinUp, Def_First_Run, Def_Print, CLM_Flag, Def_ReBEL, Def_Localization, \
Num_Local_Obs_State, Num_Local_Obs_Par, Num_Local_Obs_Bias, eps, msw_infl, Def_Multiresolution, Def_Write_Initial, Ensemble_Number, Ensemble_Number_Predict, Call_Gstat_Flag, Write_DA_File_Flag, Use_Mask_Flag, Def_Figure_Output,\
Forcing_File_Path_Home, Soil_Layer_Num, Snow_Layer_Num, ParFlow_Layer_Num, maxpft, numrad, Density_of_liquid_water, Density_of_ice, Freezing_temperature_of_fresh_water, Plot_Analysis, Def_Debug, Initial_Perturbation, \
Weather_Forecast_Days, PicHeight, PicWidth, RegionName, Row_Numbers, Col_Numbers, Row_Numbers_String, Col_Numbers_String, Grid_Resolution_CEA_String, xllcenter, yllcenter, MODEL_X_Left, MODEL_X_Right, MODEL_Y_Lower, MODEL_Y_Upper,MODEL_CEA_X, MODEL_CEA_Y, Z_Resolution, Proj_String, \
Grid_Resolution_CEA, Grid_Resolution_GEO, mksrf_edgee, mksrf_edgew, mksrf_edges, mksrf_edgen, ntasks_CLM, rootpe_CLM, nthreads_CLM, omp_get_num_procs_ParFor, Low_Ratio_Par, High_Ratio_Par, Low_Ratio_Par_Uniform, High_Ratio_Par_Uniform, \
Soil_Par_Sens_Array, Veg_Par_Sens_Array, PFT_Par_Sens_Array, Hard_Par_Sens_Array, Region_Name, Run_Dir_Home, Model_Path, Hydraulic_File_Name, Mask_File, Observation_Path, DAS_Data_Path, DasPy_Path, DAS_Output_Path, DAS_Depends_Path,
octave, r, plt, cm, colors, inset_axes, fm, legend):
#print DasPy_Path+"ObsModel/COSMOS/COSMIC_Py.py"
#print DasPy_Path+"ObsModel/COSMOS/COSMIC_Py.py"
COSMIC_Py = imp.load_source("COSMIC_Py",DasPy_Path+"ObsModel/COSMOS/COSMIC_Py.py")
memory_profiler = []
COSMIC = imp.load_dynamic("COSMIC",DasPy_Path+"ObsModel/COSMOS/COSMIC.so")
num_processors = multiprocessing.cpu_count()
start = time.time()
UTM_Zone = int(round(mksrf_edgee/15.0)) # The Difference Between the UTM Time and the Local Time
diskless_flag = True
persist_flag = True
############################################ PFCLM ####################################################
COUP_OAS_PFL = False # whether to run coupled ParFlow or not
if Model_Driver == "PFCLM":
COUP_OAS_PFL = True
CESM_Init_Flag = 1 # whether is the first time to run ccsm_driver
############################################# PFCLM ###################################################
gelmna_threshold = 1.0 # Threshold to Stop Parameter Optimization (No Stop)
#gelmna_threshold = 1.08 # Threshold to Stop Parameter Optimization
if mpi4py_rank == 0:
if Def_Region == -1:
forcing_file_name = Forcing_File_Path_Home +"_Ens1/"+ Start_Year + '-' + Start_Month + '-' + Start_Day + '.nc'
if not os.path.exists(forcing_file_name):
print "Forcing file",forcing_file_name,"does not exists!!!!!!!!"
print "Please Change the Start Date and Time."
os.abort()
else:
forcing_file_name = Forcing_File_Path_Home +"/"+ Start_Year + '-' + Start_Month + '-' + Start_Day + '.nc'
if not os.path.exists(forcing_file_name):
print "Forcing file",forcing_file_name,"does not exists!!!!!!!!"
print "Please Change the Start Date and Time."
if Def_SpinUp != 1: # If we do multi years spinup, we could use one year foring to simulate multi years
os.abort()
PP_Port = 23335 + int(numpy.random.uniform(1000*Def_Region,2000*Def_Region))
active_nodes_server = []
else:
forcing_file_name = None
PP_Port = None
active_nodes_server = None
if Def_PP == 2:
mpi4py_comm.barrier()
mpi4py_comm.Barrier()
PP_Port = mpi4py_comm.bcast(PP_Port)
active_nodes_server = mpi4py_comm.bcast(active_nodes_server)
forcing_file_name = mpi4py_comm.bcast(forcing_file_name)
if mpi4py_rank == 0:
print "mpi4py_rank",mpi4py_rank,"PP_Port",PP_Port
job_server_node_array, active_nodes_server, PROCS_PER_NODE, PP_Port, PP_Servers_Per_Node = Start_ppserver(mpi4py_comm, mpi4py_rank, mpi4py_name, DAS_Output_Path, Ensemble_Number, DAS_Depends_Path, active_nodes_server, Def_Region, NSLOTS, Def_Print, DasPy_Path, Def_PP, Def_CESM_Multi_Instance, PP_Port)
if mpi4py_rank == 0:
restart_pp_server = (len(job_server_node_array) < 1)
print "-------------- restart_pp_server",restart_pp_server
else:
restart_pp_server = None
if Def_PP == 2:
mpi4py_comm.barrier()
mpi4py_comm.Barrier()
restart_pp_server = mpi4py_comm.bcast(restart_pp_server)
while Def_PP and restart_pp_server and Ensemble_Number > 1:
job_server_node_array = Stop_ppserver(mpi4py_rank, Def_PP, DAS_Depends_Path, job_server_node_array, NSLOTS, DasPy_Path, active_nodes_server, PP_Servers_Per_Node)
job_server_node_array, active_nodes_server, PROCS_PER_NODE, PP_Port, PP_Servers_Per_Node = Start_ppserver(mpi4py_comm, mpi4py_rank, mpi4py_name, DAS_Output_Path, Ensemble_Number, DAS_Depends_Path, active_nodes_server, Def_Region, NSLOTS, Def_Print, DasPy_Path, Def_PP, Def_CESM_Multi_Instance, PP_Port)
if Def_PP == 2:
mpi4py_comm.barrier()
mpi4py_comm.Barrier()
if mpi4py_rank == 0:
print "job_server_node_array",job_server_node_array
print "active_nodes_server",active_nodes_server
if mpi4py_rank == 0:
print "mpi4py_comm,mpi4py_rank,mpi4py_size,mpi4py_name",mpi4py_comm,mpi4py_rank,mpi4py_size,mpi4py_name
# MPI Split into Ensemble_Number Groups
mpi4py_comm_split = []
mpipy_comm_decomposition = 1
if Def_PP == 2:
if mpi4py_rank == 0:
mpipy_comm_decomposition = mpi4py_size/Ensemble_Number
else:
mpipy_comm_decomposition = None
mpipy_comm_decomposition = mpi4py_comm.bcast(mpipy_comm_decomposition)
if Ensemble_Number > 1:
color = mpi4py_rank/mpipy_comm_decomposition
key = mpi4py_rank
else:
color = 0
key = 0
mpi4py_comm_split = mpi4py_comm.Split(color, key)
mpi4py_comm.barrier()
mpi4py_comm.Barrier()
if mpi4py_rank == 0:
print "mpipy_comm_decomposition,mpi4py_comm_split",mpipy_comm_decomposition,mpi4py_comm_split
if Def_PP == 2:
print "mpi4py_comm_split.py2f()",mpi4py_comm_split.py2f(),"mpi4py_comm_split.Get_size()",mpi4py_comm_split.Get_size()
if Def_PP == 2:
ntasks_CLM[:] = mpi4py_comm_split.Get_size()
###################################################################
if Def_Region >= 9:
Grid_Resolution_GEO_Global = 360.0/43200.0
Resolution_Name = "1KM"
else:
Grid_Resolution_GEO_Global = 360.0/8640.0
Resolution_Name = "5KM"
# Path for DA
if mpi4py_rank == 0:
# Decrease the float bit
MODEL_CEA_X = MODEL_CEA_X - MODEL_X_Left
MODEL_CEA_Y = MODEL_CEA_Y - MODEL_Y_Lower
MODEL_X_Left = numpy.min(MODEL_CEA_X)
MODEL_X_Right = numpy.max(MODEL_CEA_X)
MODEL_Y_Lower = numpy.min(MODEL_CEA_Y)
MODEL_Y_Upper = numpy.max(MODEL_CEA_Y)
MODEL_X_Right = MODEL_X_Right - MODEL_X_Left
MODEL_X_Left = MODEL_X_Left - MODEL_X_Left
MODEL_Y_Upper = MODEL_Y_Upper - MODEL_Y_Lower
MODEL_Y_Lower = MODEL_Y_Lower - MODEL_Y_Lower
r.assign('MODEL_X_Left', MODEL_X_Left)
r.assign('MODEL_X_Right', MODEL_X_Right)
r.assign('MODEL_Y_Lower', MODEL_Y_Lower)
r.assign('MODEL_Y_Upper', MODEL_Y_Upper)
print "MODEL_X_Left,MODEL_X_Right,MODEL_Y_Lower,MODEL_Y_Upper"
print MODEL_X_Left,MODEL_X_Right,MODEL_Y_Lower,MODEL_Y_Upper
Run_Dir_Multi_Instance = []
Run_Dir_Array = []
Forcing_File_Path_Array = []
Forcing_File_Path_Array_Par = []
if Ensemble_Number == 1:
if not os.path.exists(Run_Dir_Home):
os.makedirs(Run_Dir_Home)
Run_Dir_Multi_Instance = Run_Dir_Home+"/"
Run_Dir_Array.append(Run_Dir_Home+"/")
Forcing_File_Path_Array.append(Forcing_File_Path_Home)
Forcing_File_Path_Array_Par.append(Forcing_File_Path_Home)
else:
Run_Dir_Multi_Instance = Run_Dir_Home+"_Ens/"
for Ens_Index in range(Ensemble_Number):
Run_Dir_Temp = Run_Dir_Home+"_Ens"+str(Ens_Index+1)+"/"
if Def_CESM_Multi_Instance == 1:
Run_Dir_Temp = Run_Dir_Multi_Instance
Forcing_File_Path_Temp = Forcing_File_Path_Home+"_Ens"+str(Ens_Index+1)+"/"
if not os.path.exists(Run_Dir_Temp):
os.makedirs(Run_Dir_Temp)
Run_Dir_Array.append(Run_Dir_Temp)
Forcing_File_Path_Array.append(Forcing_File_Path_Temp)
Forcing_File_Path_Array_Par.append(Forcing_File_Path_Home)
if not os.path.exists(Run_Dir_Multi_Instance):
os.makedirs(Run_Dir_Multi_Instance)
else:
Run_Dir_Multi_Instance = None
Run_Dir_Array = None
Forcing_File_Path_Array = None
Forcing_File_Path_Array_Par = None
if Def_PP == 2:
Run_Dir_Multi_Instance = mpi4py_comm.bcast(Run_Dir_Multi_Instance)
Run_Dir_Array = mpi4py_comm.bcast(Run_Dir_Array)
Forcing_File_Path_Array = mpi4py_comm.bcast(Forcing_File_Path_Array)
Forcing_File_Path_Array_Par = mpi4py_comm.bcast(Forcing_File_Path_Array_Par)
######################################################### NC Files
NC_FileName_Assimilation_2_Constant = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Constant.nc"
NC_FileName_Assimilation_2_Diagnostic = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Diagnostic.nc"
NC_FileName_Assimilation_2_Initial = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Initial.nc"
NC_FileName_Assimilation_2_Initial_Copy = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Initial_Copy.nc"
NC_FileName_Assimilation_2_Bias = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Bias.nc"
NC_FileName_Assimilation_2_Bias_Copy = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Bias_Copy.nc"
NC_FileName_Assimilation_2_Bias_Monthly = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Bias_Monthly.nc"
NC_FileName_Assimilation_2_Bias_Monthly_Copy = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Bias_Monthly_Copy.nc"
NC_FileName_Estimated_Bias = DAS_Output_Path+"Analysis/"+Region_Name+"/Estimated_Bias.nc"
NC_FileName_Assimilation_2_Parameter = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Parameter.nc"
NC_FileName_Assimilation_2_Parameter_Copy = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Parameter_Copy.nc"
NC_FileName_Assimilation_2_Parameter_Obs_Dim = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Parameter_Obs_Dim.nc"
NC_FileName_Assimilation_2_Parameter_Monthly = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Parameter_Monthly.nc"
NC_FileName_Assimilation_2_Parameter_Monthly_Copy = DAS_Output_Path+"Analysis/"+Region_Name+"/Assimilation_2_Parameter_Monthly_Copy.nc"
NC_FileName_Optimized_Parameter = DAS_Output_Path+"Analysis/"+Region_Name+"/Optimized_Parameter.nc"
NC_FileName_Soil_Moisture_Difference = DAS_Output_Path+"Analysis/"+Region_Name+"/Soil_Moisture_Difference.nc"
NC_FileName_Parameter_Space_Single = DAS_Output_Path+"Analysis/"+Region_Name+"/Parameter_Space_Single.nc"
DAS_File_Name_List = [NC_FileName_Assimilation_2_Constant, NC_FileName_Assimilation_2_Diagnostic,
NC_FileName_Assimilation_2_Initial, NC_FileName_Assimilation_2_Initial_Copy,
NC_FileName_Assimilation_2_Bias, NC_FileName_Assimilation_2_Bias_Copy,
NC_FileName_Assimilation_2_Bias_Monthly, NC_FileName_Assimilation_2_Bias_Monthly_Copy,
NC_FileName_Estimated_Bias,
NC_FileName_Assimilation_2_Parameter, NC_FileName_Assimilation_2_Parameter_Copy, NC_FileName_Assimilation_2_Parameter_Obs_Dim,
NC_FileName_Assimilation_2_Parameter_Monthly, NC_FileName_Assimilation_2_Parameter_Monthly_Copy,
NC_FileName_Optimized_Parameter, NC_FileName_Soil_Moisture_Difference, NC_FileName_Parameter_Space_Single]
########################################################################################
Soil_Thickness = numpy.asarray([0.01751282, 0.02757897, 0.04547003, 0.07496741, 0.1236004, 0.2037826, 0.3359806, 0.5539384, 0.91329, 1.50576, 2.48258, 4.093082, 6.748351, 11.12615, 13.85115])
Variable_List = ["Soil_Moisture","Surface_Temperature","Vegetation_Temperature","Canopy_Water","Albedo_BSA_Band_vis","Albedo_BSA_Band_nir","Albedo_WSA_Band_vis",
"Albedo_WSA_Band_nir","Emissivity","Snow_Depth","Snow_Cover_Fraction","Snow_Water_Equivalent","LAI","Sensible_Heat","Irrigation_Scheduling"]
Dim_CLM_State = len(Variable_List)
Variable_Assimilation_Flag = numpy.zeros(Dim_CLM_State,dtype=numpy.float32)
Initial_Perturbation_SM_Flag = numpy.array([0 for i in range(15)]) # whether to perturb the initial data
Initial_Perturbation_ST_Flag = numpy.array([0 for i in range(15)]) # whether to perturb the initial data
################################################################################ CLM Input File Names
finidat_initial_CLM_Copy = finidat_initial_CLM
fndepdat_name = "fndep_clm_hist_simyr1849-2006_1.9x2.5_c100428.nc"
fatmgrid_name = "griddata_"+Row_Numbers_String+"x"+Col_Numbers_String+".nc"
fatmlndfrc_name = "domain.lnd_"+Row_Numbers_String+"x"+Col_Numbers_String+"_"+Region_Name+".nc"
#fatmlndfrc_name = "fracdata_"+Row_Numbers_String+"x"+Col_Numbers_String+"_"+Region_Name+".nc"
fsurdat_name = "surfdata_"+Row_Numbers_String+"x"+Col_Numbers_String+"_"+Region_Name+".nc"
fglcmask_name = "glcmaskdata_"+Row_Numbers_String+"x"+Col_Numbers_String+"_Gland20km.nc"
flndtopo_name = "topodata_"+Row_Numbers_String+"x"+Col_Numbers_String+"_"+Region_Name+".nc"
fsnowoptics_name = "snicar_optics_5bnd_c090915.nc"
fsnowaging_name = "snicar_drdt_bst_fit_60_c070416.nc"
fpftcon_name = "pft-physiology.c130503.nc"
domain_name = "domain.lnd_"+Row_Numbers_String+"x"+Col_Numbers_String+"_"+Region_Name+".nc"
rdirc_name = "rdirc_0.5x0.5_simyr2000_c101124.nc"
popd_streams_name = "clmforc.Li_2012_hdm_0.5x0.5_AVHRR_simyr1850-2010_c130401.nc"
light_streams_name = "clmforc.Li_2012_climo1995-2011.T62.lnfm_c130327.nc"
CLM_File_Name_List = [fndepdat_name, fatmgrid_name, fatmlndfrc_name, fsurdat_name, fglcmask_name, flndtopo_name,
fsnowoptics_name, fsnowaging_name, fpftcon_name, domain_name, rdirc_name, popd_streams_name, light_streams_name]
##################DA Parameters
Dim_Observation_Quantity = 4
# Irrigation Parameters
irrig_nsteps_per_day = 1
PFT_Num = 1
PFT_Type_Index = 4
irrig_nsteps_per_day = 3600.0 / dtime * Irrigation_Hours * PFT_Num
N_Steps = []
SensorType = []
SensorVariable = []
SensorQuantity = []
Variable_ID = []
QC_ID = []
SensorResolution = []
Observation_File_Name = []
Soil_Layer_Index_DA = 0
Def_First_Run_RTM = 1 # whether is RTM called at the first time
if mpi4py_rank == 0:
##### Some Index Variables
column_len = []
pft_len = []
finidat_name_string = []
if Do_DA_Flag:
finidat_name_string = Run_Dir_Home+"_Ens" + str(1) +"/"+ finidat_initial_CLM
print '============================= Open the Model Initial File and Read the Index Data ==========================================='
#------------------------------------------- Read the CLM Initial File
print "Open Initial File:", finidat_name_string
try:
CLM_Initial_File = netCDF4.Dataset(finidat_name_string, 'r')
column_len = len(CLM_Initial_File.dimensions['column'])
pft_len = len(CLM_Initial_File.dimensions['pft'])
CLM_Initial_File.close()
except:
print finidat_name_string,"not exists!!!!!!!!!!!!!!!!!!!!!"
os.abort()
else:
finidat_name_string = None
column_len = None
pft_len = None
if Def_PP == 2:
mpi4py_comm.barrier()
mpi4py_comm.Barrier()
finidat_name_string = mpi4py_comm.bcast(finidat_name_string)
column_len = mpi4py_comm.bcast(column_len)
pft_len = mpi4py_comm.bcast(pft_len)
if mpi4py_rank == 0:
##### Some Index Variables
#############################################################
if not os.path.exists(DasPy_Path+"Analysis/DAS_Temp/"+Region_Name):
os.makedirs(DasPy_Path+"Analysis/DAS_Temp/"+Region_Name)
####################################################################################
Land_Mask_Data = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
Teta_Residual = numpy.ones((Soil_Layer_Num,Row_Numbers,Col_Numbers),dtype=numpy.float32)
Teta_Saturated = numpy.ones((Soil_Layer_Num,Row_Numbers,Col_Numbers),dtype=numpy.float32)
Teta_Field_Capacity = numpy.ones((Soil_Layer_Num,Row_Numbers,Col_Numbers),dtype=numpy.float32)
Teta_Wilting_Point = numpy.ones((Soil_Layer_Num,Row_Numbers,Col_Numbers),dtype=numpy.float32)
sucsat = numpy.ones((Soil_Layer_Num,Row_Numbers,Col_Numbers),dtype=numpy.float32)
bsw = numpy.ones((Soil_Layer_Num,Row_Numbers,Col_Numbers),dtype=numpy.float32)
watdry = numpy.ones((Soil_Layer_Num,Row_Numbers,Col_Numbers),dtype=numpy.float32)
watopt = numpy.ones((Soil_Layer_Num,Row_Numbers,Col_Numbers),dtype=numpy.float32)
watfc = numpy.ones((Soil_Layer_Num,Row_Numbers,Col_Numbers),dtype=numpy.float32)
smpso = []
smpsc = []
Sand_Top_Region = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
Clay_Top_Region = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
Organic_Top_Region = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
Bulk_Density_Top_Region = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
Sand_Sub_Region = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
Clay_Sub_Region = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
Organic_Sub_Region = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
Bulk_Density_Sub_Region = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
DEM_Data = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
PCT_PFT = numpy.zeros((maxpft, Row_Numbers, Col_Numbers),dtype=numpy.float32)
STD_ELEV = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32) # standard deviation of the elevation within a grid cell
topo_slope = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
PCT_LAKE = numpy.ones((Row_Numbers,Col_Numbers),dtype=numpy.float32)
Irrigation_Grid_Flag = numpy.zeros((Row_Numbers,Col_Numbers),dtype=numpy.bool)
micro_sigma = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
# Calculate the Vegetation Fraction
PCT_Veg = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
PCT_PFT_High = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
PCT_PFT_Low = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
PCT_PFT_WATER = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
PFT_Dominant_Index = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
Crop_Sum = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
Bare_Grid_Index = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
print "Open Hydraulic_File_Name",Hydraulic_File_Name
try:
Hydraulic_File = netCDF4.Dataset(Hydraulic_File_Name, 'r')
Teta_Residual = Hydraulic_File.variables['RES'][:,:,:]
Teta_Saturated = Hydraulic_File.variables['SAT'][:,:,:]
Teta_Field_Capacity = Hydraulic_File.variables['FC'][:,:,:]
Teta_Wilting_Point = Hydraulic_File.variables['WP'][:,:,:]
sucsat = Hydraulic_File.variables["sucsat"][:,:,:]
bsw = Hydraulic_File.variables["bsw"][:,:,:]
watdry = Hydraulic_File.variables["watdry"][:,:,:]
watopt = Hydraulic_File.variables["watopt"][:,:,:]
watfc = Hydraulic_File.variables["watfc"][:,:,:]
Hydraulic_File.close()
except:
print "Open Hydraulic_File_Name",Hydraulic_File_Name,"Failed!!"
os.abort()
# Make sure the minimum soil moisture larger than 0.05, because we assume the maximum bias is 0.05
Teta_Residual[numpy.where(Teta_Residual < 0.05)] = 0.05
pft_physiology_file_name = DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/pftdata/"+fpftcon_name
pft_physiology_file = netCDF4.Dataset(pft_physiology_file_name, "r")
smpso = pft_physiology_file.variables["smpso"][:]
smpsc = pft_physiology_file.variables["smpsc"][:]
#smpso = numpy.zeros(len(pft_physiology_file.dimensions['pft']))
#smpso[:] = -50000.0
#print z0mr,PFT_Height_Top[1:17]
pft_physiology_file.close()
# Create Plot Folder
if not os.path.exists(DasPy_Path+"Analysis/DAS_Temp/"+Region_Name):
os.makedirs(DasPy_Path+"Analysis/DAS_Temp/"+Region_Name)
if Use_Mask_Flag:
# find the model grids which are need to be assiilated.
# Read the Mask Grid
print "Read the Land Water Mask Data"
mkdatadomain_NC_FileName_In = DAS_Data_Path+"SysModel/CLM/tools/" + fatmlndfrc_name
print "mkdatadomain_NC_FileName_In",mkdatadomain_NC_FileName_In
mkdatadomain_NC_File_In = netCDF4.Dataset(mkdatadomain_NC_FileName_In, 'r+')
Land_Mask_Data = numpy.flipud(mkdatadomain_NC_File_In.variables['mask'][:,:])
mkdatadomain_NC_File_In.close()
Land_Mask_Data[numpy.where(Land_Mask_Data == 0.0)] = NAvalue
Data = numpy.ma.masked_where(Land_Mask_Data == NAvalue, Land_Mask_Data)
fig1 = plt.figure(figsize=(15, 10), dpi=80)
ax = fig1.add_subplot(1, 1, 1)
im1 = ax.imshow(Data, cmap=cm.jet)
ax.set_title('Land_Mask_Data')
plt.grid(True)
plt.savefig(DasPy_Path+"Analysis/DAS_Temp/"+Region_Name+"/Land_Mask_Data.png")
plt.close('all')
del Data
####################################################################
Corner_Row_Index = 0
Corner_Col_Index = 0
if Def_Region == -2:
Grid_Resolution_GEO_Global = 360.0/432000.0*5.0
Resolution_Name = "500m"
elif Def_Region == -1:
Grid_Resolution_GEO_Global = 360.0/432000.0
Resolution_Name = "100m"
elif Def_Region <= 8:
Grid_Resolution_GEO_Global = 360.0/43200.0
Resolution_Name = "1KM"
elif Def_Region >= 9:
Grid_Resolution_GEO_Global = 360.0/8640.0
Resolution_Name = "5KM"
Sand_Top_Region, Clay_Top_Region, Organic_Top_Region, Bulk_Density_Top_Region, Sand_Sub_Region, Clay_Sub_Region, Organic_Sub_Region, Bulk_Density_Sub_Region \
= Read_Soil_Texture(Def_Region, DAS_Data_Path, Resolution_Name, Region_Name, Row_Numbers, Col_Numbers, Corner_Row_Index, Corner_Col_Index)
print "*******************************Read CLM mksurfdata"
mksurfdata_NC_FileName_In = DAS_Data_Path+"SysModel/CLM/tools/" + fsurdat_name
print "mksurfdata_NC_FileName_In",mksurfdata_NC_FileName_In
print "************************Open*******************",mksurfdata_NC_FileName_In
mksurfdata_NC_File_In = netCDF4.Dataset(mksurfdata_NC_FileName_In, 'r')
#print mksurfdata_NC_File_In.variables
STD_ELEV = numpy.flipud(mksurfdata_NC_File_In.variables['STD_ELEV'][::]) # standard deviation of the elevation within a grid cell
topo_slope = numpy.flipud(mksurfdata_NC_File_In.variables['SLOPE'][::]) # mean topographic slop
DEM_Data = numpy.flipud(mksurfdata_NC_File_In.variables['TOPO'][::]) # mean elevation on land
# check for near zero slopes, set minimum value
topo_slope[numpy.where(topo_slope < 0.2)] = 0.2
for Pft_index in range(maxpft):
#print numpy.shape(mksurfdata_NC_File_In.variables['PCT_PFT'])
PCT_PFT[Pft_index,::] = numpy.flipud(mksurfdata_NC_File_In.variables['PCT_PFT'][Pft_index,:,:])
PCT_LAKE = numpy.flipud(mksurfdata_NC_File_In.variables['PCT_LAKE'][::])
mksurfdata_NC_File_In.close()
#################################
###########################3
# microtopographic parameter, units are meters
minslope=0.05
slopemax=0.4
maxslope=(slopemax - minslope)/(slopemax)
# try smooth function of slope
slopebeta=3.0
slopemax=0.4
slope0=slopemax**(-1.0/slopebeta)
micro_sigma = (topo_slope + slope0)**(-slopebeta)
# Calculate the Vegetation Fraction
PCT_Veg = numpy.sum(PCT_PFT[1:maxpft,:,:],axis=0)
PCT_PFT_High = numpy.sum(PCT_PFT[1:9,:,:],axis=0) / 100.0
PCT_PFT_Low = numpy.sum(PCT_PFT[9:maxpft,:,:],axis=0) / 100.0
PCT_PFT_WATER = PCT_LAKE / 100.0
PFT_Dominant_Index = numpy.argmax(PCT_PFT,axis=0)
numpy.savetxt("PFT_Dominant_Index_"+Region_Name+".txt",PFT_Dominant_Index)
w,h = plt.figaspect(float(Row_Numbers)/Col_Numbers)
fig1 = plt.figure(figsize=(w,h))
ax1 = fig1.add_subplot(1,1,1)
im1 = ax1.imshow(PFT_Dominant_Index, cmap=cm.jet, interpolation='bilinear')
plt.colorbar(im1)
if Def_Figure_Output:
plt.savefig("DataBase/PFT_Dominant_Index_"+Region_Name+".png")
#plt.show()
Bare_Grid_Index = numpy.where(PFT_Dominant_Index == 0)
#Bare_Grid_Index = numpy.where(PCT_PFT[0,:,:] == 100)
print "numpy.size(Bare_Grid_Index)",numpy.size(Bare_Grid_Index)
for Soil_Layer_Index in range(Soil_Layer_Num):
watopt[Soil_Layer_Index,:,:][Bare_Grid_Index] = watfc[Soil_Layer_Index,:,:][Bare_Grid_Index]
watdry[Soil_Layer_Index,:,:][Bare_Grid_Index] = Teta_Residual[Soil_Layer_Index,:,:][Bare_Grid_Index]
#watopt[Soil_Layer_Index,:,:] = Teta_Saturated[Soil_Layer_Index,:,:]
#watdry[Soil_Layer_Index,:,:] = Teta_Residual[Soil_Layer_Index,:,:]
print "************************"
print "numpy.max(watopt),numpy.min(watopt),numpy.max(watdry),numpy.min(watdry)"
print numpy.max(watopt),numpy.min(watopt),numpy.max(watdry),numpy.min(watdry)
# w,h = plt.figaspect(float(Row_Numbers)/Col_Numbers)
# fig1 = plt.figure(figsize=(w,h))
# ax1 = fig1.add_subplot(1,1,1)
# im1 = ax1.imshow(MONTHLY_LAI[6,:,:], cmap=cm.jet, interpolation='bilinear')
# plt.colorbar(im1)
# if Def_Figure_Output:
# plt.savefig("SysModel/CLM/Surfdata_Figure/"+Region_Name+"_PFT/"+"MONTHLY_LAI.png")
# plt.show()
##############################################################################
# COSMOS Circle Mask
Mask_X_COSMOS = MODEL_CEA_X
Mask_Y_COSMOS = MODEL_CEA_Y
COSMOS_Circle_Plot = numpy.zeros((Row_Numbers,Col_Numbers),dtype=numpy.float32)
COSMOS_Circle_Array = []
COSMOS_Circle_Index_Array = []
COSMOS_Circle_Num_Array = []
#for Station_Index in range(numpy.size(Station_XY)/2):
for Station_Index in range(numpy.size(Station_XY)/2-12):
COSMOS_Circle = numpy.zeros((Row_Numbers,Col_Numbers),dtype=numpy.bool)
COSMOS_Circle[::] = False
print "Station_"+str(Station_Index+1),Station_XY[Station_Index][0],Station_XY[Station_Index][1]
r.assign('X_Coordiates',Station_XY[Station_Index][0])
r.assign('Y_Coordiates',Station_XY[Station_Index][1])
r('xy <- cbind(X_Coordiates,Y_Coordiates)')
print r['xy']
print "========================== GEO to CEA"
r('xy_cea <- project(xy,"+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +a=6371228 +b=6371228 +units=m +ellps=WGS84 +no_defs")')
print 'x,y',r['xy_cea'][0][0],r['xy_cea'][0][1]
ii = r['xy_cea'][0][0]
jj = r['xy_cea'][0][1]
dist = numpy.sqrt(abs(ii - Mask_X_COSMOS) ** 2 + abs(jj - Mask_Y_COSMOS) ** 2)
COSMOS_Circle_Index = numpy.where(dist <= 300)
COSMOS_Circle[COSMOS_Circle_Index] = True
COSMOS_Circle_Array.append(COSMOS_Circle)
COSMOS_Circle_Index_Array.append(COSMOS_Circle_Index)
#print COSMOS_Circle_Index,numpy.size(COSMOS_Circle_Index)
COSMOS_Circle_Num = numpy.zeros_like(COSMOS_Circle_Index)
COSMOS_Circle_Num = numpy.floor(dist[COSMOS_Circle_Index] / Grid_Resolution_CEA)
#print COSMOS_Circle_Num
COSMOS_Circle_Num_Array.append(COSMOS_Circle_Num)
COSMOS_Circle_Plot[COSMOS_Circle] = 1.0
if Plot_Analysis:
fig1 = plt.figure(figsize=(15, 10), dpi=80)
ax = fig1.add_subplot(1, 1, 1)
im1 = ax.imshow(COSMOS_Circle_Plot, cmap=cm.jet)
ax.set_title('COSMOS_Circle_Plot')
plt.grid(True)
plt.savefig(DasPy_Path+"Analysis/DAS_Temp/"+Region_Name+"/COSMOS_Circle_Plot.png")
plt.close('all')
##########################################################
LONGXY_Mat = numpy.zeros((Row_Numbers, Col_Numbers), dtype=numpy.float32)
LATIXY_Mat = numpy.zeros((Row_Numbers, Col_Numbers), dtype=numpy.float32)
longitudes = numpy.linspace(mksrf_edgew+Grid_Resolution_GEO[0]/2.0,mksrf_edgee-Grid_Resolution_GEO[0]/2.0,Col_Numbers)
latitudes = numpy.linspace(mksrf_edges+Grid_Resolution_GEO[1]/2.0,mksrf_edgen-Grid_Resolution_GEO[1]/2.0,Row_Numbers)
LONGXY_Row = longitudes
LATIXY_Col = latitudes
for row in range(Row_Numbers):
LONGXY_Mat[row,:] = LONGXY_Row
for col in range(Col_Numbers):
LATIXY_Mat[:,col] = LATIXY_Col
#print LATIXY_Col
Mask_Index = numpy.zeros((Dim_CLM_State, Row_Numbers, Col_Numbers), dtype=numpy.bool)
Mask_Index[:,:,:] = False
#------------------------------------------- Data Assimilation Flags
print "Soil Moisture Products: SMAP(10km), AMSR-E(25km), SMOS(40km), ASCAT(12.5km,25km), MODIS(1km), ASAR(120m), PALSAR(60m)"
print "Soil Temperature Products: MODIS Terra and Aqua(1km)"
############################################################## For Bias Estimation #######################################
Bias_Remove_Start_Time_Array = ['' for i in range(Dim_CLM_State)]
# Flag to check wehter the Observation Bias of each observation type and each observation ensemble has been perturbed
Observation_Bias_Initialization_Flag = numpy.zeros((Dim_CLM_State,Dim_Observation_Quantity,Ensemble_Number),dtype=numpy.float32)
Model_Bias_Optimized = numpy.zeros((Ensemble_Number, Dim_CLM_State, numpy.size(Station_XY)/2), dtype=numpy.float32)
Observation_Bias_Optimized = numpy.zeros((Ensemble_Number, Dim_CLM_State, Dim_Observation_Quantity, numpy.size(Station_XY)/2), dtype=numpy.float32)
# Bias Estimation Range and Standard Deviation Defination
Model_Bias_Range = numpy.zeros((Dim_CLM_State,2),dtype=numpy.float32)
Observation_Bias_Range = numpy.zeros((Dim_CLM_State,Dim_Observation_Quantity,2),dtype=numpy.float32)
Model_Bias_Range_STD = numpy.zeros((Dim_CLM_State,2),dtype=numpy.float32)
Observation_Bias_Range_STD = numpy.zeros((Dim_CLM_State,Dim_Observation_Quantity,2),dtype=numpy.float32)
Model_Bias_STD = numpy.zeros(Dim_CLM_State,dtype=numpy.float32)
Observation_Bias_STD = numpy.zeros((Dim_CLM_State,Dim_Observation_Quantity),dtype=numpy.float32)
# Model State Ensemble Inflation STD
Model_State_Inflation_Range = numpy.zeros((Dim_CLM_State,2),dtype=numpy.float32)
Model_State_Inflation_Range_STD = numpy.zeros(Dim_CLM_State,dtype=numpy.float32)
########### Simulate Model Error
Additive_Noise_SM_Par = numpy.zeros((10,11),dtype=numpy.float32)
Additive_Noise_SM_Par[::] = numpy.array([[1.00E-3, 1.0, 0.7, 0.7, 0.6, 0.6, 0.6, 0.6, 0.4, 0.4, 0.4],
[7.00E-4, 0.7, 1.0, 0.7, 0.7, 0.6, 0.6, 0.6, 0.6, 0.4, 0.4],
[5.00E-4, 0.7, 0.7, 1.0, 0.7, 0.7, 0.6, 0.6, 0.6, 0.6, 0.4],
[3.00E-4, 0.6, 0.7, 0.7, 1.0, 0.7, 0.7, 0.6, 0.6, 0.6, 0.6],
[2.00E-5, 0.6, 0.6, 0.7, 0.7, 1.0, 0.7, 0.7, 0.6, 0.6, 0.6],
[2.00E-5, 0.6, 0.6, 0.6, 0.7, 0.7, 1.0, 0.7, 0.7, 0.6, 0.6],
[2.00E-5, 0.6, 0.6, 0.6, 0.6, 0.7, 0.7, 1.0, 0.7, 0.7, 0.6],
[1.50E-6, 0.4, 0.6, 0.6, 0.6, 0.6, 0.7, 0.7, 1.0, 0.7, 0.7],
[1.50E-6, 0.4, 0.4, 0.6, 0.6, 0.6, 0.6, 0.7, 0.7, 1.0, 0.7],
[5.00E-8, 0.4, 0.4, 0.4, 0.6, 0.6, 0.6, 0.6, 0.7, 0.7, 1.0]])
#print numpy.shape(Additive_Noise_SM_Par)
Additive_Noise_SM = numpy.zeros((Ensemble_Number,Soil_Layer_Num-5),dtype=numpy.float32)
Additive_Noise_ST = numpy.zeros((Ensemble_Number,2),dtype=numpy.float32)
Irrigation_Grid_Flag_Array = []
cols1d_ixy = numpy.zeros(column_len, dtype=numpy.integer)
cols1d_jxy = numpy.zeros(column_len, dtype=numpy.integer)
cols1d_ityplun = numpy.zeros(column_len, dtype=numpy.integer)
pfts1d_ixy = numpy.zeros(pft_len, dtype=numpy.integer)
pfts1d_jxy = numpy.zeros(pft_len, dtype=numpy.integer)
pfts1d_itypveg = numpy.zeros(pft_len, dtype=numpy.integer)
pfts1d_ci = numpy.zeros(pft_len, dtype=numpy.integer)
pfts1d_ityplun = numpy.zeros(pft_len, dtype=numpy.integer)
##### Some Index Variables
if Do_DA_Flag:
finidat_name_string = Run_Dir_Home+"_Ens" + str(1) +"/"+ finidat_initial_CLM
print '============================= Open the Model Initial File and Read the Index Data ==========================================='
#------------------------------------------- Read the CLM Initial File
print "Open Initial File:", finidat_name_string
try:
CLM_Initial_File = netCDF4.Dataset(finidat_name_string, 'r')
cols1d_ixy[:] = CLM_Initial_File.variables['cols1d_ixy'][:]
cols1d_jxy[:] = CLM_Initial_File.variables['cols1d_jxy'][:]
cols1d_ityplun[:] = CLM_Initial_File.variables['cols1d_ityplun'][:]
#numpy.savetxt('cols1d_ixy',cols1d_ixy)
#numpy.savetxt('cols1d_jxy',cols1d_jxy)
pfts1d_ixy[:] = CLM_Initial_File.variables['pfts1d_ixy'][:]
pfts1d_jxy[:] = CLM_Initial_File.variables['pfts1d_jxy'][:]
pfts1d_itypveg[:] = CLM_Initial_File.variables['pfts1d_itypveg'][:]
pfts1d_ci[:] = CLM_Initial_File.variables['pfts1d_ci'][:]
pfts1d_ityplun[:] = CLM_Initial_File.variables['pfts1d_ityplun'][:]
CLM_Initial_File.close()
except:
print finidat_name_string,"not exists!!!!!!!!!!!!!!!!!!!!!"
os.abort()
Analysis_Variable_Name = ['' for i in range(Dim_CLM_State)]
Soil_Sand_Clay_Sum = numpy.zeros((Soil_Texture_Layer_Opt_Num, Row_Numbers, Col_Numbers), dtype=numpy.float32)
print "################ Go to CLM"
Parameter_Soil_Optimized = numpy.zeros((Ensemble_Number, Dim_Soil_Par, numpy.size(Station_XY)/2), dtype=numpy.float32)
Parameter_PFT_Optimized = numpy.zeros((Ensemble_Number, Dim_PFT_Par, numpy.size(Station_XY)/2), dtype=numpy.float32)
Parameter_Hard_Optimized = numpy.zeros((Ensemble_Number, Dim_Hard_Par, numpy.size(Station_XY)/2), dtype=numpy.float32)
Parameter_Soil_PSRF = numpy.zeros((Dim_Soil_Par, numpy.size(Station_XY)/2), dtype=numpy.float32)
Parameter_PFT_PSRF = numpy.zeros((Dim_PFT_Par, numpy.size(Station_XY)/2), dtype=numpy.float32)
Parameter_Hard_PSRF = numpy.zeros((Dim_Hard_Par, numpy.size(Station_XY)/2), dtype=numpy.float32)
Parameter_Optimization_First_Flag = True
Mean_Index_Prop_Grid_Array_Sys = numpy.zeros((Dim_CLM_State, Row_Numbers, Col_Numbers),dtype=numpy.float32)
Model_Variance = numpy.zeros((Dim_CLM_State, Row_Numbers, Col_Numbers),dtype=numpy.float32)
Mask = numpy.zeros((Dim_CLM_State, 3, Row_Numbers, Col_Numbers),dtype=numpy.float32)
Mask_X = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
Mask_Y = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
Mask_X[::] = MODEL_CEA_X
Mask_Y[::] = MODEL_CEA_Y
###################################### CMEM Matrix
Clay_Fraction = []
Sand_Fraction = []
Soil_Density = numpy.zeros((Row_Numbers, Col_Numbers),dtype=numpy.float32)
CMEM_Work_Path_Array = []
Clay_Mat = []
Sand_Mat = []
ECOCVL_Mat = numpy.zeros((Row_Numbers, Col_Numbers), dtype=numpy.float32)
ECOCVH_Mat = numpy.zeros((Row_Numbers, Col_Numbers), dtype=numpy.float32)
ECOTVL_Mat = numpy.zeros((Row_Numbers, Col_Numbers), dtype=numpy.float32)
ECOTVH_Mat = numpy.zeros((Row_Numbers, Col_Numbers), dtype=numpy.float32)
ECOWAT_Mat = numpy.zeros((Row_Numbers, Col_Numbers), dtype=numpy.float32)
# Folder to Save Ensemble Mean
Mean_Dir = Run_Dir_Home+"_Ens_Mean"
if not os.path.exists(Mean_Dir):
os.makedirs(Mean_Dir)
if not os.path.exists(DAS_Output_Path+"Analysis/"+Region_Name):
os.makedirs(DAS_Output_Path+"Analysis/"+Region_Name)
for Block_Index in range(Sub_Block_Ratio_Row*Sub_Block_Ratio_Col):
if not os.path.exists(DAS_Output_Path+"Analysis/"+Region_Name+"/Block_"+str(Block_Index+1)):
os.makedirs(DAS_Output_Path+"Analysis/"+Region_Name+"/Block_"+str(Block_Index+1))
NC_File_In = netCDF4.Dataset(DAS_Data_Path + "SysModel/CLM/tools/"+fsurdat_name, 'r')
File_Format1 = NC_File_In.file_format
NC_File_In.close()
NC_File_In = netCDF4.Dataset(DAS_Data_Path + "SysModel/CLM/tools/"+fatmlndfrc_name, 'r')
File_Format2 = NC_File_In.file_format
NC_File_In.close()
NC_File_In = netCDF4.Dataset(DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/pftdata/"+fpftcon_name, 'r')
File_Format3 = NC_File_In.file_format
NC_File_In.close()
NC_File_In = netCDF4.Dataset(DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/rtmdata/"+rdirc_name, 'r')
File_Format4 = NC_File_In.file_format
NC_File_In.close()
NC_File_In = netCDF4.Dataset(DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/ndepdata/"+fndepdat_name, 'r')
File_Format5 = NC_File_In.file_format
NC_File_In.close()
if Def_First_Run == 1 and ((File_Format1 == 'NETCDF3_CLASSIC') or (File_Format1 == 'NETCDF3_64BIT') and \
(File_Format2 == 'NETCDF3_CLASSIC') or (File_Format2 == 'NETCDF3_64BIT') and \
(File_Format3 == 'NETCDF3_CLASSIC') or (File_Format3 == 'NETCDF3_64BIT') and \
(File_Format4 == 'NETCDF3_CLASSIC') or (File_Format4 == 'NETCDF3_64BIT') and \
(File_Format5 == 'NETCDF3_CLASSIC') or (File_Format5 == 'NETCDF3_64BIT') ):
print "Convert netCDF3 input to netCDF4 for CLM"
subprocess.call(DAS_Depends_Path+"bin/nccopy -k 3 "+DAS_Data_Path + "SysModel/CLM/tools/"+fatmlndfrc_name+" "+DAS_Data_Path + "SysModel/CLM/inputdata/"+fatmlndfrc_name,shell=True)
os.remove(DAS_Data_Path + "SysModel/CLM/tools/"+fatmlndfrc_name)
subprocess.call(DAS_Depends_Path+"bin/nccopy -d 4 "+DAS_Data_Path + "SysModel/CLM/inputdata/"+fatmlndfrc_name+" "+DAS_Data_Path + "SysModel/CLM/tools/"+fatmlndfrc_name,shell=True)
subprocess.call(DAS_Depends_Path+"bin/nccopy -k 3 "+DAS_Data_Path + "SysModel/CLM/tools/"+fsurdat_name+" "+DAS_Data_Path + "SysModel/CLM/inputdata/"+fsurdat_name,shell=True)
os.remove(DAS_Data_Path + "SysModel/CLM/tools/"+fsurdat_name)
subprocess.call(DAS_Depends_Path+"bin/nccopy -d 4 "+DAS_Data_Path + "SysModel/CLM/inputdata/"+fsurdat_name+" "+DAS_Data_Path + "SysModel/CLM/tools/"+fsurdat_name,shell=True)
subprocess.call(DAS_Depends_Path+"bin/nccopy -k 3 "+DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/pftdata/"+fpftcon_name+" "+DAS_Data_Path + "SysModel/CLM/inputdata/"+fpftcon_name,shell=True)
os.remove(DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/pftdata/"+fpftcon_name)
subprocess.call(DAS_Depends_Path+"bin/nccopy -d 4 "+DAS_Data_Path + "SysModel/CLM/inputdata/"+fpftcon_name+" "+DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/pftdata/"+fpftcon_name,shell=True)
subprocess.call(DAS_Depends_Path+"bin/nccopy -k 3 "+DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/rtmdata/"+rdirc_name+" "+DAS_Data_Path + "SysModel/CLM/inputdata/"+rdirc_name,shell=True)
os.remove(DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/rtmdata/"+rdirc_name)
subprocess.call(DAS_Depends_Path+"bin/nccopy -d 4 "+DAS_Data_Path + "SysModel/CLM/inputdata/"+rdirc_name+" "+DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/rtmdata/"+rdirc_name,shell=True)
subprocess.call(DAS_Depends_Path+"bin/nccopy -k 3 "+DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/ndepdata/"+fndepdat_name+" "+DAS_Data_Path + "SysModel/CLM/inputdata/"+fndepdat_name,shell=True)
os.remove(DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/ndepdata/"+fndepdat_name)
subprocess.call(DAS_Depends_Path+"bin/nccopy -d 4 "+DAS_Data_Path + "SysModel/CLM/inputdata/"+fndepdat_name+" "+DAS_Data_Path + "SysModel/CLM/inputdata/lnd/clm2/ndepdata/"+fndepdat_name,shell=True)
if (Def_First_Run == -1) and Ensemble_Number > 1:
copyLargeFile(NC_FileName_Assimilation_2_Initial_Copy, NC_FileName_Assimilation_2_Initial)
copyLargeFile(NC_FileName_Assimilation_2_Parameter_Copy, NC_FileName_Assimilation_2_Parameter)
copyLargeFile(NC_FileName_Assimilation_2_Parameter_Monthly_Copy, NC_FileName_Assimilation_2_Parameter_Monthly)
copyLargeFile(NC_FileName_Assimilation_2_Bias_Copy, NC_FileName_Assimilation_2_Bias)
copyLargeFile(NC_FileName_Assimilation_2_Bias_Monthly_Copy, NC_FileName_Assimilation_2_Bias_Monthly)
if Def_First_Run == 1:
print "**************** Prepare Initial netCDF file"
if os.path.exists(NC_FileName_Assimilation_2_Constant):
os.remove(NC_FileName_Assimilation_2_Constant)
print 'Write NetCDF File:',NC_FileName_Assimilation_2_Constant
NC_File_Out_Assimilation_2_Constant = netCDF4.Dataset(NC_FileName_Assimilation_2_Constant, 'w', diskless=True, persist=True, format='NETCDF4')
# Dim the dimensions of NetCDF
NC_File_Out_Assimilation_2_Constant.createDimension('lon', Col_Numbers)
NC_File_Out_Assimilation_2_Constant.createDimension('lat', Row_Numbers)
NC_File_Out_Assimilation_2_Constant.createDimension('Soil_Layer_Num', Soil_Layer_Num)
NC_File_Out_Assimilation_2_Constant.createDimension('ParFlow_Layer_Num', ParFlow_Layer_Num)
NC_File_Out_Assimilation_2_Constant.createDimension('Ensemble_Number', Ensemble_Number)
NC_File_Out_Assimilation_2_Constant.createDimension('Dim_CLM_State', Dim_CLM_State)
NC_File_Out_Assimilation_2_Constant.createDimension('maxpft', maxpft)
NC_File_Out_Assimilation_2_Constant.createVariable('Land_Mask_Data','f4',('lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['Land_Mask_Data'][:,:] = Land_Mask_Data
NC_File_Out_Assimilation_2_Constant.createVariable('PCT_LAKE','f4',('lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['PCT_LAKE'][:,:] = PCT_LAKE
NC_File_Out_Assimilation_2_Constant.createVariable('PCT_Veg','f4',('lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['PCT_Veg'][:,:] = PCT_Veg
NC_File_Out_Assimilation_2_Constant.createVariable('PCT_PFT','f4',('maxpft','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['PCT_PFT'][:,:] = PCT_PFT
#print "numpy.mean(numpy.sum(PCT_PFT[:,:,:],axis=0))",numpy.mean(numpy.sum(PCT_PFT[:,:,:],axis=0))
NC_File_Out_Assimilation_2_Constant.createVariable('STD_ELEV','f4',('lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['STD_ELEV'][:,:] = STD_ELEV
NC_File_Out_Assimilation_2_Constant.createVariable('DEM_Data','f4',('lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['DEM_Data'][:,:] = DEM_Data
NC_File_Out_Assimilation_2_Constant.createVariable('Bulk_Density_Top_Region','f4',('lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['Bulk_Density_Top_Region'][:,:] = Bulk_Density_Top_Region
NC_File_Out_Assimilation_2_Constant.createVariable('Bulk_Density_Sub_Region','f4',('lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['Bulk_Density_Sub_Region'][:,:] = Bulk_Density_Sub_Region
NC_File_Out_Assimilation_2_Constant.createVariable('Teta_Residual','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['Teta_Residual'][:,:,:] = Teta_Residual
NC_File_Out_Assimilation_2_Constant.createVariable('Teta_Saturated','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['Teta_Saturated'][:,:,:] = Teta_Saturated
NC_File_Out_Assimilation_2_Constant.createVariable('Teta_Field_Capacity','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['Teta_Field_Capacity'][:,:,:] = Teta_Field_Capacity
NC_File_Out_Assimilation_2_Constant.createVariable('Teta_Wilting_Point','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['Teta_Wilting_Point'][:,:,:] = Teta_Wilting_Point
NC_File_Out_Assimilation_2_Constant.createVariable('watopt','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['watopt'][:,:,:] = watopt
NC_File_Out_Assimilation_2_Constant.createVariable('watdry','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['watdry'][:,:,:] = watdry
NC_File_Out_Assimilation_2_Constant.createVariable('watfc','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['watfc'][:,:,:] = watfc
NC_File_Out_Assimilation_2_Constant.createVariable('PFT_Dominant_Index','f4',('lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.variables['PFT_Dominant_Index'][:,:] = PFT_Dominant_Index
del Land_Mask_Data,PCT_LAKE,PCT_Veg,PCT_PFT,STD_ELEV,DEM_Data
del Bulk_Density_Top_Region,Bulk_Density_Sub_Region,Teta_Residual,Teta_Saturated,Teta_Field_Capacity,Teta_Wilting_Point,watopt,watdry,watfc
NC_File_Out_Assimilation_2_Constant.createVariable('CLM_Soil_Layer_Thickness','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.createVariable('CLM_Soil_Layer_Thickness_Cumsum','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.createVariable('Soil_Layer_Thickness_Ratio_Moisture','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.createVariable('Soil_Layer_Thickness_Ratio_Temperature','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Constant.sync()
NC_File_Out_Assimilation_2_Constant.close()
NC_File_Out_Assimilation_2_Constant = netCDF4.Dataset(NC_FileName_Assimilation_2_Constant, 'r+', format='NETCDF4')
# Meters
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][0, :, :] = Soil_Thickness[0]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][1, :, :] = Soil_Thickness[1]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][2, :, :] = Soil_Thickness[2]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][3, :, :] = Soil_Thickness[3]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][4, :, :] = Soil_Thickness[4]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][5, :, :] = Soil_Thickness[5]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][6, :, :] = Soil_Thickness[6]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][7, :, :] = Soil_Thickness[7]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][8, :, :] = Soil_Thickness[8]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][9, :, :] = Soil_Thickness[9]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][10, :, :] = Soil_Thickness[10]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][11, :, :] = Soil_Thickness[11]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][12, :, :] = Soil_Thickness[12]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][13, :, :] = Soil_Thickness[13]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][14, :, :] = Soil_Thickness[14]
NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness_Cumsum'][:,:,:] = numpy.cumsum(numpy.asarray(NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][:,:,:]), axis=0)
for Soil_Layer_Index in range(Soil_Layer_Num):
#NC_File_Out_Assimilation_2_Constant.variables['Soil_Layer_Thickness_Ratio_Moisture'][Soil_Layer_Index, :, :] = numpy.exp(-1.0*NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness_Cumsum'][Soil_Layer_Index,:,:])
NC_File_Out_Assimilation_2_Constant.variables['Soil_Layer_Thickness_Ratio_Moisture'][Soil_Layer_Index, :, :] = numpy.exp(-1.0*NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][Soil_Layer_Index,:,:])
for Soil_Layer_Index in range(Soil_Layer_Num):
#NC_File_Out_Assimilation_2_Constant.variables['Soil_Layer_Thickness_Ratio_Temperature'][Soil_Layer_Index, :, :] = numpy.exp(-1.0*numpy.log(NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness_Cumsum'][Soil_Layer_Index,:,:]))
NC_File_Out_Assimilation_2_Constant.variables['Soil_Layer_Thickness_Ratio_Temperature'][Soil_Layer_Index, :, :] = numpy.exp(-1.0*numpy.log(NC_File_Out_Assimilation_2_Constant.variables['CLM_Soil_Layer_Thickness'][Soil_Layer_Index,:,:]))
Ratio_Temp = NC_File_Out_Assimilation_2_Constant.variables['Soil_Layer_Thickness_Ratio_Temperature'][0, :, :]
for Soil_Layer_Index in range(Soil_Layer_Num):
NC_File_Out_Assimilation_2_Constant.variables['Soil_Layer_Thickness_Ratio_Temperature'][Soil_Layer_Index, :, :] = \
numpy.asarray(NC_File_Out_Assimilation_2_Constant.variables['Soil_Layer_Thickness_Ratio_Temperature'][Soil_Layer_Index, :, :]) / Ratio_Temp
NC_File_Out_Assimilation_2_Constant.sync()
NC_File_Out_Assimilation_2_Constant.close()
print "**************** Prepare Initial netCDF file"
if os.path.exists(NC_FileName_Assimilation_2_Diagnostic):
os.remove(NC_FileName_Assimilation_2_Diagnostic)
print 'Write NetCDF File:',NC_FileName_Assimilation_2_Diagnostic
NC_File_Out_Assimilation_2_Diagnostic = netCDF4.Dataset(NC_FileName_Assimilation_2_Diagnostic, 'w', diskless=True, persist=True, format='NETCDF4')
# Dim the dimensions of NetCDF
NC_File_Out_Assimilation_2_Diagnostic.createDimension('lon', Col_Numbers)
NC_File_Out_Assimilation_2_Diagnostic.createDimension('lat', Row_Numbers)
NC_File_Out_Assimilation_2_Diagnostic.createDimension('Soil_Layer_Num', Soil_Layer_Num)
NC_File_Out_Assimilation_2_Diagnostic.createDimension('ParFlow_Layer_Num', ParFlow_Layer_Num)
NC_File_Out_Assimilation_2_Diagnostic.createDimension('Ensemble_Number', Ensemble_Number)
NC_File_Out_Assimilation_2_Diagnostic.createDimension('Dim_CLM_State', Dim_CLM_State)
NC_File_Out_Assimilation_2_Diagnostic.createVariable('Initial_SM_Noise','f4',('Ensemble_Number','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.variables['Initial_SM_Noise'][:,:,:] = 0.0
NC_File_Out_Assimilation_2_Diagnostic.createVariable('Initial_ST_Noise','f4',('Ensemble_Number','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.variables['Initial_ST_Noise'][:,:,:] = 0.0
NC_File_Out_Assimilation_2_Diagnostic.createVariable('Mask_Index','i4',('Dim_CLM_State','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.createVariable('CLM_Soil_Temperature_Ratio_Ensemble_Mat','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.createVariable('CLM_Soil_Moisture_Ratio_Ensemble_Mat_MultiScale','f4',('Soil_Layer_Num','lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.createVariable('CLM_Soil_Moisture_Ratio_Ensemble_Mat','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.variables['CLM_Soil_Moisture_Ratio_Ensemble_Mat'][:,:,:] = 1.0
NC_File_Out_Assimilation_2_Diagnostic.variables['CLM_Soil_Temperature_Ratio_Ensemble_Mat'][:,:,:] = 1.0
NC_File_Out_Assimilation_2_Diagnostic.variables['CLM_Soil_Moisture_Ratio_Ensemble_Mat_MultiScale'][:,:,:] = 1.0
NC_File_Out_Assimilation_2_Diagnostic.createVariable('Analysis_Grid_Array','f4',('Ensemble_Number','Dim_CLM_State','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.createVariable('Innovation_State','f4',('Ensemble_Number','Dim_CLM_State','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.createVariable('Increments_State','f4',('Ensemble_Number','Dim_CLM_State','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.createVariable('Observation','f4',('Dim_CLM_State','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.createVariable('CLM_2m_Air_Temperature_Ensemble_Mat','f4',('lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.createVariable('CLM_Air_Pressure_Ensemble_Mat','f4',('lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Diagnostic.sync()
NC_File_Out_Assimilation_2_Diagnostic.close()
print "**************** Prepare Initial netCDF file"
if os.path.exists(NC_FileName_Assimilation_2_Initial):
os.remove(NC_FileName_Assimilation_2_Initial)
if os.path.exists(NC_FileName_Assimilation_2_Initial_Copy):
os.remove(NC_FileName_Assimilation_2_Initial_Copy)
print 'Write NetCDF File:',NC_FileName_Assimilation_2_Initial
NC_File_Out_Assimilation_2_Initial = netCDF4.Dataset(NC_FileName_Assimilation_2_Initial, 'w', diskless=True, persist=True, format='NETCDF4')
# Dim the dimensions of NetCDF
NC_File_Out_Assimilation_2_Initial.createDimension('lon', Col_Numbers)
NC_File_Out_Assimilation_2_Initial.createDimension('lat', Row_Numbers)
NC_File_Out_Assimilation_2_Initial.createDimension('Soil_Layer_Num', Soil_Layer_Num)
NC_File_Out_Assimilation_2_Initial.createDimension('Ensemble_Number', Ensemble_Number)
NC_File_Out_Assimilation_2_Initial.createDimension('Dim_CLM_State', Dim_CLM_State)
NC_File_Out_Assimilation_2_Initial.createVariable('Prop_Grid_Array_Sys_Parallel','f4',('Dim_CLM_State','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('Prop_Grid_Array_H_Trans_Paralle','f4',('Dim_CLM_State','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Soil_Moisture_Ensemble_Mat_Parallel','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Soil_Temperature_Ensemble_Mat_Parallel','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('Prop_Grid_Array_Sys','f4',('Ensemble_Number','Dim_CLM_State','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('Prop_Grid_Array_H_Trans','f4',('Ensemble_Number','Dim_CLM_State','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Soil_Moisture_Ensemble_Mat','f4',('Soil_Layer_Num','lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Soil_Temperature_Ensemble_Mat','f4',('Soil_Layer_Num','lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Vegetation_Temperature_Ensemble_Mat','f4',('lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Ground_Temperature_Ensemble_Mat','f4',('lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Snow_Depth_Ensemble_Mat','f4',('lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Snow_Water_Ensemble_Mat','f4',('lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_INT_SNOW_Ensemble_Mat','f4',('lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_FH2OSFC_Ensemble_Mat','f4',('lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
#onset freezing degree days counters
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Onset_Freezing_Degree_Days_Counter_Ensemble_Mat','f4',('lat','lon','Ensemble_Number',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('Prop_Grid_Array_Sys_parm_infl','f4',('Dim_CLM_State','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Soil_Moisture_parm_infl','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Soil_Temperature_parm_infl','f4',('Soil_Layer_Num','lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Vegetation_Temperature_parm_infl','f4',('lat','lon',),zlib=True,least_significant_digit=None)
NC_File_Out_Assimilation_2_Initial.createVariable('CLM_Ground_Temperature_parm_infl','f4',('lat','lon',),zlib=True,least_significant_digit=None)