-
Notifications
You must be signed in to change notification settings - Fork 14
/
input_data.F90
1195 lines (980 loc) · 49 KB
/
input_data.F90
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
!> @file
!! @brief Read atmospheric and surface data from MPAS diag, hist, or forecast files.
!! @author Larissa Reames CIWRO/NOAA/NSSL
!> Read atmospheric and/or surface input MPAS grid.
!! Supported formats include INITIalization file, forecast diag file (only 2d diagnostic
!! fields), and full 3d forecast files.
!!
!! Public variables are defined below: "input" indicates field
!! associated with the input grid.
!!
!! @author Larissa Reames CIWRO/NOAA/NSSL
module input_data
use esmf
use netcdf
use utils_mod
use program_setup, only : hist_file_input_grid, &
diag_file_input_grid, &
grid_file_input_grid, &
interp_diag, interp_hist, &
wrf_mod_vars
use model_grid, only : input_grid, &
nCells_input, nVert_input, &
nz_input, nzp1_input, &
nsoil_input, strlen, &
valid_time, config_dt, &
start_time, lsm_scheme, &
mp_scheme, conv_scheme, &
cell_latitude_input_grid, &
cell_longitude_input_grid, &
zgrid_input_grid, &
input_diag_bundle, &
target_diag_names, &
input_hist_bundle_2d_cons, &
input_hist_bundle_2d_patch, &
input_hist_bundle_2d_nstd, &
input_hist_bundle_3d_nz, &
input_hist_bundle_3d_nzp1, &
input_hist_bundle_3d_vert, &
input_hist_bundle_soil, &
target_hist_names_2d_cons, &
target_hist_names_2d_nstd, &
target_hist_names_2d_patch, &
target_hist_names_3d_nz, &
target_hist_names_3d_nzp1, &
target_hist_names_3d_vert, &
target_hist_names_soil, &
target_diag_units, &
target_hist_units_2d_cons, &
target_hist_units_2d_nstd, &
target_hist_units_2d_patch, &
target_hist_units_3d_nzp1, &
target_hist_units_3d_vert, &
target_hist_units_3d_nz, &
target_hist_units_soil, &
target_diag_longname, &
target_hist_longname_2d_cons, &
target_hist_longname_2d_nstd, &
target_hist_longname_2d_patch, &
target_hist_longname_3d_nzp1, &
target_hist_longname_3d_nz, &
target_hist_longname_3d_vert, &
target_hist_longname_soil, &
n_diag_fields, &
n_hist_fields_2d_cons, &
n_hist_fields_2d_nstd, &
n_hist_fields_2d_patch, &
n_hist_fields_3d_vert, &
n_hist_fields_3d_nz, &
n_hist_fields_3d_nzp1, &
n_hist_fields_3d_vert, &
n_hist_fields_soil, &
elemIDs, nCellsPerPET, &
nodeIDs,diag_out_interval, &
nNodesPerPET, &
u_input_grid, &
v_input_grid, &
do_u_interp, &
do_v_interp, &
do_u10_interp, &
do_v10_interp, &
u10_ind, v10_ind
implicit none
private
public :: read_input_data
contains
!> Read input grid data driver.
!!
!! @param[in] localpet ESMF local persistent execution thread
!! @author Larissa Reames CIWRO/NOAA/NSSL
subroutine read_input_data(localpet)
implicit none
include 'mpif.h'
integer, intent(in) :: localpet
if (interp_diag) then
call read_input_diag_data(localpet)
endif
if (interp_hist) then
call read_input_hist_data(localpet)
endif
if (.not. interp_diag .and. .not. interp_hist) then
call error_handler(" SET INTERP_DIAG AND/OR INTERP_HIST TO TRUE TO OBTAIN OUTPUT", -1)
endif
end subroutine read_input_data
!> Read input grid diag data.
!!
!! @param[in] localpet ESMF local persistent execution thread
!! @author Larissa Reames CIWRO/NOAA/NSSL
subroutine read_input_diag_data(localpet)
character(len=500) :: the_file
character(len=50) :: vname
integer, intent(in) :: localpet
integer :: error, ncid, rc
integer :: id_dim
integer :: id_var, i, j, nodes, ndims
type(esmf_field),allocatable :: fields(:)
real(esmf_kind_r8), allocatable :: dummy(:), dummy2(:,:,:)
real(esmf_kind_r8), pointer :: varptr(:), varptr2(:,:)
call init_input_diag_fields(localpet)
if (localpet==0) print*,"- READ INPUT DIAG DATA."
the_file = trim(diag_file_input_grid)
error=nf90_open(trim(the_file),nf90_nowrite,ncid)
call netcdf_err(error, 'opening: '//trim(the_file) )
!---------------------------------------------------------------------------
! Initialize esmf atmospheric fields.
!---------------------------------------------------------------------------
allocate(fields(n_diag_fields))
allocate(target_diag_units(n_diag_fields))
allocate(target_diag_longname(n_diag_fields))
call ESMF_FieldBundleGet(input_diag_bundle, fieldList=fields, &
itemorderflag=ESMF_ITEMORDER_ADDORDER, &
rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldBundleGet", rc)
call ESMF_MeshGet(input_grid, nodeCount = nodes, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN MeshGet", rc)
allocate(dummy(nCells_input))
allocate(dummy2(nz_input, nCells_input,1))
do i = 1,n_diag_fields
call ESMF_FieldGet(fields(i), name=vname, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
if(vname=='u10') then
do_u10_interp = 1
u10_ind = i
endif
if(vname=='v10') then
do_v10_interp = 1
v10_ind = i
endif
if (localpet==0) print*,"- READ ", trim(vname)
error=nf90_inq_varid(ncid, trim(vname), id_var)
call netcdf_err(error, 'reading field id - '//trim(vname) )
error=nf90_inquire_variable(ncid, id_var, ndims=ndims)
call netcdf_err(error, 'reading variable dims' )
if (ndims == 2) then
call ESMF_FieldGet(fields(i), farrayPtr=varptr, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
error=nf90_get_var(ncid, id_var, dummy)
call netcdf_err(error, 'reading field' )
if (localpet==0) print*,"- SET ON MESH ", trim(vname)
do j = 1, nCellsPerPET
varptr(j) = dummy(elemIDs(j))
enddo
!if (localpet==0) print*, localpet, minval(varptr), maxval(varptr)
nullify(varptr)
elseif (ndims == 3) then
call ESMF_FieldGet(fields(i), farrayPtr=varptr2, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGet", rc)
error=nf90_get_var(ncid, id_var, dummy2)
call netcdf_err(error, 'reading field' )
if (localpet==0) print*,"- SET ON MESH ", trim(vname)
do j = 1, nCellsPerPET
varptr2(j,:) = dummy2(:,elemIDs(j),1)
enddo
!if (localpet==0) print*, localpet, minval(varptr2), maxval(varptr2)
nullify(varptr2)
endif
error=nf90_get_att(ncid,id_var,'units',target_diag_units(i))
call netcdf_err(error, 'reading field units' )
error=nf90_get_att(ncid,id_var,'long_name',target_diag_longname(i))
call netcdf_err(error, 'reading field long_name' )
enddo
if (localpet==0) print*,'- TRY TO READ GLOBAL ATTRIBUTE START TIME FROM DIAG FILE'
error = nf90_get_att(ncid,NF90_GLOBAL,'config_start_time',start_time)
if ( error .ne. 0 ) then
if (interp_hist) then
if (localpet==0) then
print*,'GETTING GLOBAL ATTRIBUTE START TIME FROM DIAG FILE FAILED'
print*,'...THAT IS OK...TRY TO GET FROM HISTORY FILE'
endif
else
call netcdf_err(error, 'reading config_start_time')
endif
endif
if (localpet==0) print*,'- TRY TO READ GLOBAL ATTRIBUTE CONFIG_DT FROM DIAG FILE'
error = nf90_get_att(ncid,NF90_GLOBAL,'config_dt',config_dt)
if (error .ne. 0) then
if (localpet==0) then
print*,'GETTING GLOBAL ATTRIBUTE CONFIG_DT FROM DIAG FILE FAILED'
print*,'...THAT IS OK...SET TO 0 AND TRY TO GET FROM HISTORY FILE'
endif
config_dt = 0.0
endif
if (localpet==0) print*,'- read output_interval from diag file'
error = nf90_get_att(ncid,NF90_GLOBAL,'output_interval',diag_out_interval)
if (error .ne. 0) then
if (localpet==0) print*, 'error reading output_interval from diag file, setting to 0'
diag_out_interval = 0
endif
if (localpet==0) print*, "getting xtime"
error = nf90_inq_dimid(ncid,'StrLen',id_var)
call netcdf_err(error, 'reading strlen dim id')
error = nf90_inquire_dimension(ncid,id_var,len=strlen)
allocate(valid_time(1,strlen))
if (localpet==0) print*, "strlen = ", strlen
error = nf90_inq_varid(ncid,'xtime',id_var)
call netcdf_err(error, 'reading xtime id')
error = nf90_get_var(ncid, id_var, valid_time)
call netcdf_err(error, 'getting xtime')
error = nf90_close(ncid)
deallocate( dummy)
end subroutine read_input_diag_data
subroutine init_input_diag_fields(localpet)
implicit none
integer, intent(in) :: localpet
integer :: i, rc
type(esmf_field),allocatable :: diag_fields(:)
character(50), allocatable :: input_diag_names(:)
character(50) :: fname
fname = 'diaglist'
call read_varlist(localpet,fname,n_diag_fields,input_diag_names, target_diag_names)
allocate(diag_fields(n_diag_fields))
if (localpet==0) print*,"- INITIALIZE INPUT DIAG FIELDS."
do i = 1, n_diag_fields
if (input_diag_names(i) == "refl10cm") then
if (localpet==0) print*, "- INIT FIELD ", input_diag_names(i)
diag_fields(i) = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_ELEMENT, &
ungriddedLBound=(/1/), &
ungriddedUBound=(/nz_input/), &
name=input_diag_names(i), rc=rc)
if(ESMF_logFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__))&
call error_handler("IN FieldCreate", rc)
else
if (localpet==0) print*, "- INIT FIELD ", input_diag_names(i)
diag_fields(i) = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_ELEMENT, &
name=input_diag_names(i), rc=rc)
if(ESMF_logFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldCreate", rc)
endif
enddo
input_diag_bundle = ESMF_FieldBundleCreate(fieldList=diag_fields, &
name="input diag data", rc=rc)
if(ESMF_logFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldBundleCreate", rc)
deallocate(diag_fields, input_diag_names)
end subroutine init_input_diag_fields
!> Read input grid hist data.
!!
!! @param[in] localpet ESMF local persistent execution thread
!! @author Larissa Reames CIWRO/NOAA/NSSL
subroutine read_input_hist_data(localpet)
character(len=500) :: the_file
character(len=50) :: vname, att_text
integer, intent(in) :: localpet
integer :: error, ncid, rc
integer :: id_dim
integer :: id_var, i, j, nodes
type(esmf_field),allocatable :: fields(:)
real(esmf_kind_r8), allocatable :: dummy2(:,:), dummy3(:,:,:)
real(esmf_kind_r8), pointer :: varptr(:), varptr2(:,:)
call init_input_hist_fields(localpet)
if (localpet==0) print*,"- READ INPUT HIST DATA."
the_file = trim(hist_file_input_grid)
error=nf90_open(trim(the_file),nf90_nowrite,ncid)
call netcdf_err(error, 'opening: '//trim(the_file) )
!---------------------------------------------------------------------------
! Read global attributes for use when creating output file
!---------------------------------------------------------------------------
if (localpet==0) print*,'- READ GLOBAL ATTRIBUTE LSM SCHEME'
error = nf90_get_att(ncid,NF90_GLOBAL,'config_lsm_scheme',att_text)
!call netcdf_err(error, 'reading config_lsm_scheme')
if (error .ne. 0) then
lsm_scheme = 0
elseif (INDEX(att_text,'noah') .ne. 0) then
lsm_scheme = 2
elseif (INDEX(att_text, 'ruc') .ne. 0) then
lsm_scheme = 3
endif
if (localpet==0) print*,'- READ GLOBAL ATTRIBUTE START TIME'
error = nf90_get_att(ncid,NF90_GLOBAL,'config_start_time',start_time)
call netcdf_err(error, 'reading config_start_time')
if (localpet==0) print*,'- READ GLOBAL ATTRIBUTE LMP SCHEME'
error = nf90_get_att(ncid,NF90_GLOBAL,'config_microp_scheme',att_text)
!call netcdf_err(error, 'reading config_microp_scheme')
if (error .ne. 0) then
mp_scheme = 0
elseif (trim(att_text) == 'mp_thompson') then
mp_scheme = 8
elseif (trim(att_text) == 'mp_nssl2m') then
mp_scheme = 18
endif
if (localpet==0) print*,'- READ GLOBAL ATTRIBUTE CONVECTION SCHEME'
error = nf90_get_att(ncid,NF90_GLOBAL,'config_convection_scheme',att_text)
!call netcdf_err(error, 'reading config_conv_scheme')
if (error .ne. 0) then
conv_scheme = 0
elseif (trim(att_text) == 'cu_ntiedke') then
conv_scheme = 16
elseif (trim(att_text) == 'cu_kain_fritsch') then
conv_scheme = 1
elseif (trim(att_text) == 'cu_grell_freitas') then
conv_scheme = 3
endif
if (localpet==0) print*,'- READ GLOBAL ATTRIBUTE CONFIG_DT'
error = nf90_get_att(ncid,NF90_GLOBAL,'config_dt',config_dt)
!call netcdf_err(error, 'reading config_dt')
if (error .ne. 0) config_dt = 0.0
vname = 'xtime'
if (localpet==0) print*, '- READ TIMES VARIABLE'
error = nf90_inq_dimid(ncid,'StrLen',id_var)
call netcdf_err(error, 'reading strlen dim id')
error = nf90_inquire_dimension(ncid,id_var,len=strlen)
if ( .not. allocated(valid_time)) allocate(valid_time(1,strlen)) !may have been allocated in read_input_diag_data, hence the condition
error = nf90_inq_varid(ncid,vname,id_var)
call netcdf_err(error, 'reading xtime id')
if (localpet==0) print*, "getting xtime"
error = nf90_get_var(ncid, id_var, valid_time)
call netcdf_err(error, 'getting xtime')
!---------------------------------------------------------------------------
! Initialize 2d esmf atmospheric fields for bilinear/patch interpolation
!---------------------------------------------------------------------------
if (localpet==0) print*, "Begin reading variables"
if (n_hist_fields_2d_patch > 0) then
if (localpet==0) print*, "read 2d hist"
allocate(fields(n_hist_fields_2d_patch))
allocate(target_hist_units_2d_patch(n_hist_fields_2d_patch))
allocate(target_hist_longname_2d_patch(n_hist_fields_2d_patch))
call ESMF_FieldBundleGet(input_hist_bundle_2d_patch, fieldList=fields, &
itemorderflag=ESMF_ITEMORDER_ADDORDER, &
rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldBundleGet", rc)
call ESMF_MeshGet(input_grid, nodeCount = nodes, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN MeshGet", rc)
allocate(dummy2(nCells_input,1))
do i = 1,n_hist_fields_2d_patch
call ESMF_FieldGet(fields(i), name=vname, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
if (localpet==0) print*, vname
call ESMF_FieldGet(fields(i), farrayPtr=varptr, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
if (localpet==0) print*,"- READ ", trim(vname)
error=nf90_inq_varid(ncid, trim(vname), id_var)
call netcdf_err(error, 'reading field id' )
error=nf90_get_var(ncid, id_var, dummy2)
call netcdf_err(error, 'reading field' )
error=nf90_get_att(ncid,id_var,'units',target_hist_units_2d_patch(i))
call netcdf_err(error, 'reading field units' )
error=nf90_get_att(ncid,id_var,'long_name',target_hist_longname_2d_patch(i))
call netcdf_err(error, 'reading field long_name' )
if (localpet==0) print*,"- SET ON MESH ", trim(vname)
do j = 1, nCellsPerPET
varptr(j) = dummy2(elemIDs(j),1)
enddo
nullify(varptr)
enddo
deallocate(dummy2)
deallocate(fields)
endif
!---------------------------------------------------------------------------
! Initialize 2d esmf atmospheric fields for conservative interpolation
!---------------------------------------------------------------------------
if (n_hist_fields_2d_cons > 0) then
if (localpet==0) print*, "read 2d hist cons"
allocate(fields(n_hist_fields_2d_cons))
allocate(target_hist_units_2d_cons(n_hist_fields_2d_cons))
allocate(target_hist_longname_2d_cons(n_hist_fields_2d_cons))
call ESMF_FieldBundleGet(input_hist_bundle_2d_cons, fieldList=fields, &
itemorderflag=ESMF_ITEMORDER_ADDORDER, &
rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldBundleGet", rc)
call ESMF_MeshGet(input_grid, nodeCount = nodes, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN MeshGet", rc)
allocate(dummy2(nCells_input,1))
do i = 1,n_hist_fields_2d_cons
call ESMF_FieldGet(fields(i), name=vname, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
call ESMF_FieldGet(fields(i), farrayPtr=varptr, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
if (localpet==0) print*,"- READ ", trim(vname)
error=nf90_inq_varid(ncid, trim(vname), id_var)
call netcdf_err(error, 'reading field id' )
error=nf90_get_var(ncid, id_var, dummy2)
call netcdf_err(error, 'reading field' )
error=nf90_get_att(ncid,id_var,'units',target_hist_units_2d_cons(i))
call netcdf_err(error, 'reading field units' )
error=nf90_get_att(ncid,id_var,'long_name',target_hist_longname_2d_cons(i))
call netcdf_err(error, 'reading field long_name' )
if (localpet==0) print*,"- SET ON MESH ", trim(vname)
do j = 1, nCellsPerPET
varptr(j) = dummy2(elemIDs(j),1)
enddo
nullify(varptr)
enddo
deallocate(dummy2)
deallocate(fields)
endif
!---------------------------------------------------------------------------------
! Initialize 2d esmf atmospheric fields for nearest source to dest interpolation
!---------------------------------------------------------------------------------
if (n_hist_fields_2d_nstd > 0) then
allocate(fields(n_hist_fields_2d_nstd))
allocate(target_hist_units_2d_nstd(n_hist_fields_2d_nstd))
allocate(target_hist_longname_2d_nstd(n_hist_fields_2d_nstd))
call ESMF_FieldBundleGet(input_hist_bundle_2d_nstd, fieldList=fields, &
itemorderflag=ESMF_ITEMORDER_ADDORDER, &
rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldBundleGet", rc)
call ESMF_MeshGet(input_grid, nodeCount = nodes, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN MeshGet", rc)
allocate(dummy2(nCells_input,1))
do i = 1,n_hist_fields_2d_nstd
call ESMF_FieldGet(fields(i), name=vname, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
call ESMF_FieldGet(fields(i), farrayPtr=varptr, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
if (localpet==0) print*,"- READ ", trim(vname)
error=nf90_inq_varid(ncid, trim(vname), id_var)
call netcdf_err(error, 'reading field id' )
error=nf90_get_var(ncid, id_var, dummy2)
call netcdf_err(error, 'reading field' )
error=nf90_get_att(ncid,id_var,'units',target_hist_units_2d_nstd(i))
call netcdf_err(error, 'reading field units' )
error=nf90_get_att(ncid,id_var,'long_name',target_hist_longname_2d_nstd(i))
call netcdf_err(error, 'reading field long_name' )
if (localpet==0) print*,"- SET ON MESH ", trim(vname)
do j = 1, nCellsPerPET
varptr(j) = dummy2(elemIDs(j),1)
enddo
nullify(varptr)
enddo
deallocate(dummy2)
deallocate(fields)
endif
!---------------------------------------------------------------------------
! Initialize 3d esmf soil fields for bilinear/patch interpolation
!---------------------------------------------------------------------------
if (n_hist_fields_soil > 0) then
allocate(fields(n_hist_fields_soil))
allocate(target_hist_units_soil(n_hist_fields_soil))
allocate(target_hist_longname_soil(n_hist_fields_soil))
call ESMF_FieldBundleGet(input_hist_bundle_soil, fieldList=fields, &
itemorderflag=ESMF_ITEMORDER_ADDORDER, &
rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldBundleGet", rc)
call ESMF_MeshGet(input_grid, nodeCount = nodes, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN MeshGet", rc)
allocate(dummy3(nsoil_input,nCells_input,1))
do i = 1,n_hist_fields_soil
call ESMF_FieldGet(fields(i), name=vname, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
call ESMF_FieldGet(fields(i), farrayPtr=varptr2, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
if (localpet==0) print*,"- READ ", trim(vname)
error=nf90_inq_varid(ncid, trim(vname), id_var)
call netcdf_err(error, 'reading field id' )
error=nf90_get_var(ncid, id_var, dummy3)
call netcdf_err(error, 'reading field' )
error=nf90_get_att(ncid,id_var,'units',target_hist_units_soil(i))
call netcdf_err(error, 'reading field units' )
error=nf90_get_att(ncid,id_var,'long_name',target_hist_longname_soil(i))
call netcdf_err(error, 'reading field long_name' )
if (localpet==0) print*,"- SET ON MESH ", trim(vname)
do j = 1, nCellsPerPET
varptr2(j,:) = dummy3(:,elemIDs(j),1)
enddo
nullify(varptr2)
enddo
deallocate(dummy3)
deallocate(fields)
endif
!---------------------------------------------------------------------------
! Initialize 3d esmf atmospheric fields with nVertLevels vertical dimension
!---------------------------------------------------------------------------
if (n_hist_fields_3d_nz > 0 ) then
allocate(fields(n_hist_fields_3d_nz))
allocate(target_hist_units_3d_nz(n_hist_fields_3d_nz))
allocate(target_hist_longname_3d_nz(n_hist_fields_3d_nz))
call ESMF_FieldBundleGet(input_hist_bundle_3d_nz, fieldList=fields, &
itemorderflag=ESMF_ITEMORDER_ADDORDER, &
rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldBundleGet", rc)
call ESMF_MeshGet(input_grid, nodeCount = nodes, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN MeshGet", rc)
allocate(dummy3(nz_input,nCells_input,1))
do i = 1,n_hist_fields_3d_nz
call ESMF_FieldGet(fields(i), name=vname, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
call ESMF_FieldGet(fields(i), farrayPtr=varptr2, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
if (localpet==0) print*,"- READ ", trim(vname)
error=nf90_inq_varid(ncid, trim(vname), id_var)
call netcdf_err(error, 'reading field id' )
error=nf90_get_var(ncid, id_var, dummy3)
call netcdf_err(error, 'reading field' )
error=nf90_get_att(ncid,id_var,'units',target_hist_units_3d_nz(i))
call netcdf_err(error, 'reading field units' )
error=nf90_get_att(ncid,id_var,'long_name',target_hist_longname_3d_nz(i))
call netcdf_err(error, 'reading field long_name' )
if (localpet==0) print*,"- SET ON MESH ", trim(vname)
do j = 1, nCellsPerPET
varptr2(j,:) = dummy3(:,elemIDs(j),1)
enddo
nullify(varptr2)
enddo
deallocate(dummy3)
deallocate(fields)
endif
!---------------------------------------------------------------------------
! Initialize 3d esmf atmospheric fields U and V if requested
!---------------------------------------------------------------------------
if (do_u_interp==1) then
allocate(dummy3(nz_input,nCells_input,1))
call ESMF_fieldGet(u_input_grid, farrayPtr=varptr2,rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
if (localpet==0) print*, "- READ uReconstructZonal "
error=nf90_inq_varid(ncid, "uReconstructZonal", id_var)
call netcdf_err(error, 'reading field id' )
error=nf90_get_var(ncid, id_var, dummy3)
call netcdf_err(error, 'reading field' )
if (localpet==0) print*,"- SET ON MESH uReconstructZonal"
do j = 1, nCellsPerPET
varptr2(j,:) = dummy3(:,elemIDs(j),1)
enddo
nullify(varptr2)
deallocate(dummy3)
endif
if (do_v_interp==1) then
allocate(dummy3(nz_input,nCells_input,1))
call ESMF_fieldGet(v_input_grid, farrayPtr=varptr2,rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
if (localpet==0) print*, "- READ uReconstructMeridional "
error=nf90_inq_varid(ncid, "uReconstructMeridional", id_var)
call netcdf_err(error, 'reading field id' )
error=nf90_get_var(ncid, id_var, dummy3)
call netcdf_err(error, 'reading field' )
if (localpet==0) print*,"- SET ON MESH uReconstructMeridional"
do j = 1, nCellsPerPET
varptr2(j,:) = dummy3(:,elemIDs(j),1)
enddo
nullify(varptr2)
deallocate(dummy3)
endif
!-------------------------------------------------------------------------------
! Initialize 3d esmf atmospheric fields with nVertLevels+1 vertical dimension
!-------------------------------------------------------------------------------
if (n_hist_fields_3d_nzp1 > 0 ) then
allocate(fields(n_hist_fields_3d_nzp1))
allocate(target_hist_units_3d_nzp1(n_hist_fields_3d_nzp1))
allocate(target_hist_longname_3d_nzp1(n_hist_fields_3d_nzp1))
call ESMF_FieldBundleGet(input_hist_bundle_3d_nzp1, fieldList=fields, &
itemorderflag=ESMF_ITEMORDER_ADDORDER, &
rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldBundleGet", rc)
call ESMF_MeshGet(input_grid, nodeCount = nodes, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN MeshGet", rc)
allocate(dummy3(nzp1_input,nCells_input,1))
do i = 1,n_hist_fields_3d_nzp1
call ESMF_FieldGet(fields(i), name=vname, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
call ESMF_FieldGet(fields(i), farrayPtr=varptr2, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", rc)
if (localpet==0) print*,"- READ ", trim(vname)
error=nf90_inq_varid(ncid, trim(vname), id_var)
call netcdf_err(error, 'reading field id' )
error=nf90_get_var(ncid, id_var, dummy3)
call netcdf_err(error, 'reading field' )
error=nf90_get_att(ncid,id_var,'units',target_hist_units_3d_nzp1(i))
call netcdf_err(error, 'reading field units' )
error=nf90_get_att(ncid,id_var,'long_name',target_hist_longname_3d_nzp1(i))
call netcdf_err(error, 'reading field long_name' )
if (localpet==0) print*,"- SET ON MESH ", trim(vname)
do j = 1, nCellsPerPET
varptr2(j,:) = dummy3(:,elemIDs(j),1)
enddo
!if (localpet==0) print*, vname, minval(varptr2), maxval(varptr2)
nullify(varptr2)
enddo
deallocate(dummy3)
deallocate(fields)
endif
!---------------------------------------------------------------------------
! Initialize 3d esmf atmospheric fields with nVertLevels vertical dimension and
! defined on unstructured mesh nodes
!---------------------------------------------------------------------------
if (n_hist_fields_3d_vert > 0 ) then
allocate(fields(n_hist_fields_3d_vert))
allocate(target_hist_units_3d_vert(n_hist_fields_3d_vert))
allocate(target_hist_longname_3d_vert(n_hist_fields_3d_vert))
call ESMF_FieldBundleGet(input_hist_bundle_3d_vert, fieldList=fields, &
itemorderflag=ESMF_ITEMORDER_ADDORDER, &
rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldBundleGet", rc)
call ESMF_MeshGet(input_grid, nodeCount = nodes, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN MeshGet", rc)
allocate(dummy3(nz_input,nVert_input,1))
do i = 1,n_hist_fields_3d_vert
call ESMF_FieldGet(fields(i), name=vname, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGet", rc)
call ESMF_FieldGet(fields(i), farrayPtr=varptr2, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGet", rc)
if (localpet==0) print*,"- READ ", trim(vname)
error=nf90_inq_varid(ncid, trim(vname), id_var)
call netcdf_err(error, 'reading field id' )
error=nf90_get_var(ncid, id_var, dummy3)
call netcdf_err(error, 'reading field' )
error=nf90_get_att(ncid,id_var,'units',target_hist_units_3d_vert(i))
call netcdf_err(error, 'reading field units' )
error=nf90_get_att(ncid,id_var,'long_name',target_hist_longname_3d_vert(i))
call netcdf_err(error, 'reading field long_name' )
if (localpet==0) print*,"- SET ON MESH ", trim(vname)
do j = 1, nNodesPerPET
varptr2(j,:) = dummy3(:,nodeIDs(j),1)
enddo
nullify(varptr2)
enddo
deallocate(dummy3)
deallocate(fields)
endif
error = nf90_close(ncid)
end subroutine read_input_hist_data
subroutine init_input_hist_fields(localpet)
implicit none
integer, intent(in) :: localpet
integer :: i, j, k, n, rc
integer :: n_hist_fields_2d, n_hist_fields_3d
!type(esmf_field), allocatable :: hist_fields_2d_cons(:), &
! hist_fields_2d_nstd(:), &
! hist_fields_2d_patch(:)
character(50), allocatable :: input_hist_names_2d(:), &
input_hist_names_2d_cons(:), &
input_hist_names_2d_nstd(:), &
input_hist_names_2d_patch(:), &
input_hist_names_3d(:), &
input_hist_names_3d_nz(:), &
input_hist_names_soil(:), &
input_hist_names_3d_nzp1(:), &
input_hist_names_3d_vert(:), &
target_hist_names_2d(:), &
target_hist_names_3d(:)
type(esmf_field), allocatable :: fields(:)
character(50) :: cons_vars(2), nstd_vars(4), nzp1_vars(2), &
vert_vars(1)
character(50) :: fname
cons_vars = (/'snow ','snowh'/)
nstd_vars = (/'ivgtyp ','isltyp ', 'xland ','landmask'/)
nzp1_vars = (/'zgrid','w '/)
vert_vars = (/'vorticity'/)
n_hist_fields_2d_cons = 0
n_hist_fields_2d_nstd = 0
n_hist_fields_2d_patch = 0
n_hist_fields_3d_vert = 0
n_hist_fields_3d_nz = 0
n_hist_fields_3d_nzp1 = 0
fname = 'histlist_2d'
call read_varlist(localpet, fname,n_hist_fields_2d,input_hist_names_2d, target_hist_names_2d)
fname = 'histlist_3d'
call read_varlist(localpet,fname,n_hist_fields_3d,input_hist_names_3d, target_hist_names_3d)
fname = 'histlist_soil'
call read_varlist(localpet,fname,n_hist_fields_soil,input_hist_names_soil, target_hist_names_soil)
do i = 1, n_hist_fields_2d
if (any(cons_vars == input_hist_names_2d(i))) then
n_hist_fields_2d_cons = n_hist_fields_2d_cons + 1
elseif (any(nstd_vars == input_hist_names_2d(i))) then
n_hist_fields_2d_nstd = n_hist_fields_2d_nstd + 1
else
n_hist_fields_2d_patch = n_hist_fields_2d_patch + 1
endif
enddo
allocate(input_hist_names_2d_cons(n_hist_fields_2d_cons))
allocate(input_hist_names_2d_nstd(n_hist_fields_2d_nstd))
allocate(input_hist_names_2d_patch(n_hist_fields_2d_patch))
allocate(target_hist_names_2d_cons(n_hist_fields_2d_cons))
allocate(target_hist_names_2d_nstd(n_hist_fields_2d_nstd))
allocate(target_hist_names_2d_patch(n_hist_fields_2d_patch))
j = 0
k = 0
n = 0
do i = 1, n_hist_fields_2d
if (any(cons_vars == input_hist_names_2d(i))) then
j = j+1
input_hist_names_2d_cons(j) = input_hist_names_2d(i)
target_hist_names_2d_cons(j) = target_hist_names_2d(i)
elseif (any(nstd_vars == input_hist_names_2d(i))) then
k = k+1
input_hist_names_2d_nstd(k) = input_hist_names_2d(i)
target_hist_names_2d_nstd(k) = target_hist_names_2d(i)
else
n = n+1
input_hist_names_2d_patch(n) = input_hist_names_2d(i)
target_hist_names_2d_patch(n) = target_hist_names_2d(i)
endif
enddo
do i = 1, n_hist_fields_3d
if(localpet==0) print*, i
if (wrf_mod_vars .and. input_hist_names_3d(i)=="uReconstructZonal") then
do_u_interp = 1
cycle
elseif ( wrf_mod_vars .and. input_hist_names_3d(i)=="uReconstructMeridional") then
do_v_interp = 1
cycle
elseif (any(nzp1_vars == input_hist_names_3d(i))) then
n_hist_fields_3d_nzp1 = n_hist_fields_3d_nzp1 + 1
elseif (any(vert_vars == input_hist_names_3d(i))) then
n_hist_fields_3d_vert = n_hist_fields_3d_vert + 1
else
n_hist_fields_3d_nz = n_hist_fields_3d_nz + 1
endif
enddo
allocate(input_hist_names_3d_nz(n_hist_fields_3d_nz))
allocate(input_hist_names_3d_nzp1(n_hist_fields_3d_nzp1))
allocate(input_hist_names_3d_vert(n_hist_fields_3d_vert))
allocate(target_hist_names_3d_nz(n_hist_fields_3d_nz))
allocate(target_hist_names_3d_nzp1(n_hist_fields_3d_nzp1))
allocate(target_hist_names_3d_vert(n_hist_fields_3d_vert))
j = 0
k = 0
n = 0
if (do_u_interp==1) then
if (localpet==0) print*, "- INIT FIELD uReconstructZonal"
u_input_grid = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_ELEMENT, &
name="uReconstructZonal", &
ungriddedLBound=(/1/), &
ungriddedUBound=(/nz_input/), rc=rc)
if(ESMF_logFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldCreate", rc)
endif
if (do_v_interp==1) then
if (localpet==0) print*, "- INIT FIELD uReconstructMeridonal"
v_input_grid = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_ELEMENT, &
name="uReconstructMeridonal", &
ungriddedLBound=(/1/), &
ungriddedUBound=(/nz_input/), rc=rc)
if(ESMF_logFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldCreate", rc)
endif
do i = 1, n_hist_fields_3d
if(localpet==0) print*, i, input_hist_names_3d(i)
if (do_u_interp==1 .and. input_hist_names_3d(i)=="uReconstructZonal") then
cycle
elseif (do_v_interp==1 .and. input_hist_names_3d(i) == "uReconstructMeridional") then
cycle
elseif (any(nzp1_vars == input_hist_names_3d(i))) then
j = j+1
input_hist_names_3d_nzp1(j) = input_hist_names_3d(i)
target_hist_names_3d_nzp1(j) = target_hist_names_3d(i)
elseif (any(vert_vars == input_hist_names_3d(i))) then
n = n + 1
input_hist_names_3d_vert(n) = input_hist_names_3d(i)
target_hist_names_3d_vert(n) = target_hist_names_3d(i)
else
k = k+1
input_hist_names_3d_nz(k) = input_hist_names_3d(i)
target_hist_names_3d_nz(k) = target_hist_names_3d(i)
endif
enddo
if (localpet==0) print*,"- INITIALIZE INPUT HIST FIELDS."
if (n_hist_fields_2d_cons > 0) then
allocate(fields(n_hist_fields_2d_cons))
do i = 1, n_hist_fields_2d_cons
if (localpet==0) print*, "- INIT 2D CONSERVATIVE FIELD ", input_hist_names_2d_cons(i)
fields(i) = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_ELEMENT, &
name=input_hist_names_2d_cons(i), rc=rc)
if(ESMF_logFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldCreate", rc)
enddo
input_hist_bundle_2d_cons = ESMF_FieldBundleCreate(fieldList=fields, &
name="input hist 2d data cons", rc=rc)
if(ESMF_logFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldBundleCreate", rc)
deallocate(fields)
endif
if (n_hist_fields_2d_nstd > 0) then
allocate(fields(n_hist_fields_2d_nstd))
do i = 1, n_hist_fields_2d_nstd
if (localpet==0) print*, "- INIT 2D NEAREST FIELD ", input_hist_names_2d_nstd(i)
fields(i) = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_ELEMENT, &