forked from NOAA-EMC/fv3atm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_regional.F90
2857 lines (2644 loc) · 116 KB
/
post_regional.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
!-----------------------------------------------------------------------
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!-----------------------------------------------------------------------
!
module post_regional
use module_fv3_io_def, only : wrttasks_per_group,filename_base, &
lon1, lat1, lon2, lat2, dlon, dlat, &
cen_lon, cen_lat, dxin=>dx, dyin=>dy, &
stdlat1, stdlat2, output_grid
use write_internal_state, only : wrt_internal_state
implicit none
include 'mpif.h'
integer mype, nbdl
logical setvar_atmfile, setvar_sfcfile, read_postcntrl
public post_run_regional, post_getattr_regional
contains
subroutine post_run_regional(wrt_int_state,mypei,mpicomp,lead_write, &
mynfhr,mynfmin,mynfsec)
!
! revision history:
! Jul 2019 J. Wang create interface to run inline post for FV3
! Sep 2020 J. Dong/J. Wang create interface to run inline post for FV3-LAM
!
!-----------------------------------------------------------------------
!*** run post on write grid comp
!-----------------------------------------------------------------------
!
use ctlblk_mod, only : komax,ifhr,ifmin,modelname,datapd,fld_info, &
npset,grib,gocart_on,icount_calmict, jsta, &
jend,im, nsoil, filenameflat
use gridspec_mod, only : maptype, gridtype,latstart,latlast, &
lonstart,lonlast
use grib2_module, only : gribit2,num_pset,nrecout,first_grbtbl
use xml_perl_data,only : paramset
!
!-----------------------------------------------------------------------
!
implicit none
!
!-----------------------------------------------------------------------
!
type(wrt_internal_state),intent(in) :: wrt_int_state
integer,intent(in) :: mypei
integer,intent(in) :: mpicomp
integer,intent(in) :: lead_write
integer,intent(in) :: mynfhr
integer,intent(in) :: mynfmin
integer,intent(in) :: mynfsec
!
!-----------------------------------------------------------------------
!*** LOCAL VARIABLES
!-----------------------------------------------------------------------
!
integer n,nwtpg,ieof,lcntrl,ierr,i,j,k,jts,jte,mynsoil
integer,allocatable :: jstagrp(:),jendgrp(:)
integer,save :: kpo,kth,kpv
logical,save :: log_postalct=.false.
real,dimension(komax),save :: po, th, pv
logical :: Log_runpost
character(255) :: post_fname*255
integer,save :: iostatusD3D=-1
!
real(kind=8) :: btim0, btim1, btim2, btim3,btim4,btim5,btim6,btim7
!
print *,'in post_run start'
!-----------------------------------------------------------------------
!*** set up dimensions
!-----------------------------------------------------------------------
!
btim0 = MPI_Wtime()
modelname = "FV3R"
grib = "grib2"
gridtype = "A"
nsoil = 4
mype = mypei
nwtpg = wrt_int_state%petcount
jts = wrt_int_state%lat_start !<-- Starting J of this write task's subsection
jte = wrt_int_state%lat_end !<-- Ending J of this write task's subsection
maptype = wrt_int_state%post_maptype
nbdl = wrt_int_state%FBCount
if(mype==0) print *,'in post_run,jts=',jts,'jte=',jte,'nwtpg=',nwtpg,'nwtpg=',nwtpg, &
'jts=',jts,'jte=',jte,'maptype=',maptype,'nbdl=',nbdl,'log_postalct=',log_postalct
!
!-----------------------------------------------------------------------
!*** set up fields to run post
!-----------------------------------------------------------------------
!
if (.not.log_postalct) then
!
allocate(jstagrp(nwtpg),jendgrp(nwtpg))
!
do n=0,nwtpg-1
jstagrp(n+1) = wrt_int_state%lat_start_wrtgrp(n+1)
jendgrp(n+1) = wrt_int_state%lat_end_wrtgrp (n+1)
enddo
if(mype==0) print *,'in post_run,jstagrp=',jstagrp,'jendgrp=',jendgrp
!-----------------------------------------------------------------------
!*** read namelist for pv,th,po
!-----------------------------------------------------------------------
!
call read_postnmlt(kpo,kth,kpv,po,th,pv,wrt_int_state%post_nlunit, &
wrt_int_state%post_namelist)
!
!-----------------------------------------------------------------------
!*** allocate post variables
!-----------------------------------------------------------------------
!
if(mype==0) print *,'in post_run,be post_alctvars, dim=',wrt_int_state%im, &
wrt_int_state%jm, wrt_int_state%lm,'mype=',mype,'wrttasks_per_group=', &
wrttasks_per_group,'lead_write=',lead_write,'jts=',jts,'jte=',jte, &
'jstagrp=',jstagrp,'jendgrp=',jendgrp
call post_alctvars(wrt_int_state%im,wrt_int_state%jm, &
wrt_int_state%lm,mype,wrttasks_per_group,lead_write, &
mpicomp,jts,jte,jstagrp,jendgrp)
!
!-----------------------------------------------------------------------
!*** read namelist for pv,th,po
!-----------------------------------------------------------------------
!
log_postalct = .true.
first_grbtbl = .true.
read_postcntrl = .true.
!
ENDIF
!
!-----------------------------------------------------------------------
!*** fill post variables with values from forecast results
!-----------------------------------------------------------------------
!
ifhr = mynfhr
ifmin = mynfmin
if (ifhr == 0 ) ifmin = 0
if(mype==0) print *,'bf set_postvars,ifmin=',ifmin,'ifhr=',ifhr
setvar_atmfile=.false.
setvar_sfcfile=.false.
call set_postvars_regional(wrt_int_state,mpicomp,setvar_atmfile, &
setvar_sfcfile)
! print *,'af set_postvars,setvar_atmfile=',setvar_atmfile, &
! 'setvar_sfcfile=',setvar_sfcfile
!
if (setvar_atmfile.and.setvar_sfcfile) then
! 20190807 no need to call microinit for GFDLMP
! call MICROINIT
!
if(grib=="grib2" .and. read_postcntrl) then
if (ifhr == 0) then
filenameflat = 'postxconfig-NT_FH00.txt'
call read_xml()
if(mype==0) print *,'af read_xml at fh00,name=',trim(filenameflat)
else if(ifhr > 0) then
filenameflat = 'postxconfig-NT.txt'
if(size(paramset)>0) then
do i=1,size(paramset)
if (size(paramset(i)%param)>0) then
deallocate(paramset(i)%param)
nullify(paramset(i)%param)
endif
enddo
deallocate(paramset)
nullify(paramset)
endif
num_pset = 0
call read_xml()
if(mype==0) print *,'af read_xml,name=',trim(filenameflat),'ifhr=',ifhr
read_postcntrl = .false.
endif
endif
!
IEOF = 0
npset = 0
icount_calmict = 0
do while( IEOF == 0)
!
if(grib == "grib2") then
npset = npset + 1
call set_outflds(kth,th,kpv,pv)
if(allocated(datapd))deallocate(datapd)
allocate(datapd(wrt_int_state%im,jte-jts+1,nrecout+100))
!$omp parallel do default(none),private(i,j,k),shared(nrecout,jend,jsta,im,datapd)
do k=1,nrecout+100
do j=1,jend+1-jsta
do i=1,im
datapd(i,j,k) = 0.
enddo
enddo
enddo
call get_postfilename(post_fname)
if (mype==0) write(0,*)'post_fname=',trim(post_fname)
!
if ( ieof == 0) call process(kth,kpv,th(1:kth),pv(1:kpv),iostatusD3D)
!
call mpi_barrier(mpicomp,ierr)
call gribit2(post_fname)
if(allocated(datapd))deallocate(datapd)
if(allocated(fld_info))deallocate(fld_info)
if(npset >= num_pset) exit
endif
!
enddo
!
endif
end subroutine post_run_regional
!
!-----------------------------------------------------------------------
!
subroutine post_getattr_regional(wrt_int_state)
!
use esmf
use ctlblk_mod, only: im, jm, mpi_comm_comp,gdsdegr,spval
use masks, only: gdlat, gdlon, dx, dy
use gridspec_mod, only: latstart, latlast, lonstart, &
lonlast, cenlon, cenlat, dxval, &
dyval, truelat2, truelat1,psmapf, &
lonstartv, lonlastv, cenlonv, &
latstartv, latlastv, cenlatv, &
latstart_r,latlast_r,lonstart_r, &
lonlast_r, STANDLON, maptype, gridtype
!
implicit none
!
type(wrt_internal_state),intent(inout) :: wrt_int_state
!
! local variable
integer i,j,k,n,kz, attcount, nfb
integer ni,naryi,nr4,nr8,rc
integer aklen,varival
real(4) varr4val
real(8) varr8val
character(80) attName, hydrostatics, fldname
type(ESMF_TypeKind_Flag) :: typekind
real(4), dimension(:), allocatable :: ak4,bk4
real(8), dimension(:), allocatable :: ak8,bk8
type(ESMF_FieldBundle) :: fldbundle
!
print *,'in post_reional, attr'
do nfb=1, wrt_int_state%FBcount
fldbundle = wrt_int_state%wrtFB(nfb)
! set grid spec:
print*,'in post_getattr_lam,output_grid=',trim(output_grid),'nfb=',nfb
print*,'in post_getattr_lam, lon1=',lon1,lon2,lat1,lat2,dlon,dlat
gdsdegr = 1000000.
if(trim(output_grid) == 'regional_latlon') then
MAPTYPE=0
gridtype='A'
if( lon1<0 ) then
lonstart = nint((lon1+360.)*gdsdegr)
else
lonstart = nint(lon1*gdsdegr)
endif
if( lon2<0 ) then
lonlast = nint((lon2+360.)*gdsdegr)
else
lonlast = nint(lon2*gdsdegr)
endif
latstart = nint(lat1*gdsdegr)
latlast = nint(lat2*gdsdegr)
dxval = dlon*gdsdegr
dyval = dlat*gdsdegr
print*,'lonstart,latstart,dyval,dxval', &
lonstart,lonlast,latstart,latlast,dyval,dxval
else if(trim(output_grid) == 'lambert_conformal') then
MAPTYPE=1
GRIDTYPE='A'
if( cen_lon<0 ) then
cenlon = nint((cen_lon+360.)*gdsdegr)
else
cenlon = nint(cen_lon*gdsdegr)
endif
cenlat = cen_lat*gdsdegr
if( lon1<0 ) then
lonstart = nint((lon1+360.)*gdsdegr)
else
lonstart = nint(lon1*gdsdegr)
endif
if( lon2<0 ) then
lonlast = nint((lon2+360.)*gdsdegr)
else
lonlast = nint(lon2*gdsdegr)
endif
latstart = nint(lat1*gdsdegr)
latlast = nint(lat2*gdsdegr)
truelat1 = nint(stdlat1*gdsdegr)
truelat2 = nint(stdlat2*gdsdegr)
if(dxin<spval) then
dxval = dxin*1.0e3
dyval = dyin*1.0e3
else
dxval = spval
dyval = spval
endif
STANDLON = cenlon
else if(trim(output_grid) == 'rotated_latlon') then
MAPTYPE=207
GRIDTYPE='A'
if( cen_lon<0 ) then
cenlon = nint((cen_lon+360.)*gdsdegr)
else
cenlon = nint(cen_lon*gdsdegr)
endif
cenlat = cen_lat*gdsdegr
if( lon1<0 ) then
lonstart = nint((lon1+360.)*gdsdegr)
else
lonstart = nint(lon1*gdsdegr)
endif
if( lon2<0 ) then
lonlast = nint((lon2+360.)*gdsdegr)
else
lonlast = nint(lon2*gdsdegr)
endif
latstart = nint(lat1*gdsdegr)
latlast = nint(lat2*gdsdegr)
if(dlon<spval) then
dxval = dlon*gdsdegr
dyval = dlat*gdsdegr
else
dxval = spval
dyval = spval
endif
print*,'rotated latlon,lonstart,latstart,cenlon,cenlat,dyval,dxval', &
lonstart,lonlast,latstart,latlast,cenlon,cenlat,dyval,dxval
endif
! look at the field bundle attributes
call ESMF_AttributeGet(fldbundle, convention="NetCDF", purpose="FV3", &
attnestflag=ESMF_ATTNEST_OFF, Count=attcount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__,file=__FILE__))return ! bail out
!
aklen=0.
do i=1, attCount
call ESMF_AttributeGet(fldbundle, convention="NetCDF", purpose="FV3", &
attnestflag=ESMF_ATTNEST_OFF, attributeIndex=i, name=attName, &
typekind=typekind, itemCount=n, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__,file=__FILE__))return ! bail out
if (typekind==ESMF_TYPEKIND_I4 ) then
if(n==1) then
call ESMF_AttributeGet(fldbundle, convention="NetCDF", purpose="FV3", &
name=trim(attName), value=varival, rc=rc)
if (trim(attName) == 'ncnsto') wrt_int_state%ntrac=varival
if (trim(attName) == 'ncld') wrt_int_state%ncld=varival
if (trim(attName) == 'nsoil') wrt_int_state%nsoil=varival
if (trim(attName) == 'fhzero') wrt_int_state%fhzero=varival
if (trim(attName) == 'imp_physics') wrt_int_state%imp_physics=varival
endif
else if (typekind==ESMF_TYPEKIND_R4) then
if(n==1) then
call ESMF_AttributeGet(fldbundle, convention="NetCDF", purpose="FV3", &
name=trim(attName), value=varr4val, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
if (trim(attName) == 'dtp') then
wrt_int_state%dtp=varr4val
endif
else if(n>1) then
if(trim(attName) =="ak") then
if(allocated(wrt_int_state%ak)) deallocate(wrt_int_state%ak)
allocate(wrt_int_state%ak(n))
call ESMF_AttributeGet(fldbundle, convention="NetCDF", purpose="FV3", &
name=trim(attName), valueList=wrt_int_state%ak, rc=rc)
wrt_int_state%lm = n-1
else if(trim(attName) =="bk") then
if(allocated(wrt_int_state%bk)) deallocate(wrt_int_state%bk)
allocate(wrt_int_state%bk(n))
call ESMF_AttributeGet(fldbundle, convention="NetCDF", purpose="FV3", &
name=trim(attName), valueList=wrt_int_state%bk, rc=rc)
endif
endif
else if (typekind==ESMF_TYPEKIND_R8) then
if(n==1) then
call ESMF_AttributeGet(fldbundle, convention="NetCDF", purpose="FV3", &
name=trim(attName), value=varr8val, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
if (trim(attName) == 'dtp') then
wrt_int_state%dtp=varr8val
endif
else if(n>1) then
if(trim(attName) =="ak") then
if(allocated(wrt_int_state%ak)) deallocate(wrt_int_state%ak)
allocate(wrt_int_state%ak(n))
call ESMF_AttributeGet(fldbundle, convention="NetCDF", purpose="FV3", &
name=trim(attName), valueList=wrt_int_state%ak, rc=rc)
wrt_int_state%lm = n-1
else if(trim(attName) =="bk") then
if(allocated(wrt_int_state%bk)) deallocate(wrt_int_state%bk)
allocate(wrt_int_state%bk(n))
call ESMF_AttributeGet(fldbundle, convention="NetCDF", purpose="FV3", &
name=trim(attName), valueList=wrt_int_state%bk, rc=rc)
endif
wrt_int_state%lm = size(wrt_int_state%ak) - 1
endif
endif
!
enddo
!
enddo !end nfb
!
print *,'in post_getattr, dtp=',wrt_int_state%dtp
!
end subroutine post_getattr_regional
!-----------------------------------------------------------------------
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!-----------------------------------------------------------------------
!
subroutine set_postvars_regional(wrt_int_state,mpicomp,setvar_atmfile, &
setvar_sfcfile)
!
! revision history:
! Jul 2019 J. Wang Initial code
!
!-----------------------------------------------------------------------
!*** set up post fields from nmint_state
!-----------------------------------------------------------------------
!
use esmf
use vrbls3d, only: t, q, uh, vh, wh, alpint, dpres, zint, zmid, o3, &
qqr, qqs, cwm, qqi, qqw, qqg, omga, cfr, pmid, &
q2, rlwtt, rswtt, tcucn, tcucns, train, el_pbl, &
pint, exch_h, ref_10cm
use vrbls2d, only: f, pd, sigt4, fis, pblh, ustar, z0, ths, qs, twbs,&
qwbs, avgcprate, cprate, avgprec, prec, lspa, sno,&
cldefi, th10, q10, tshltr, pshltr, tshltr, albase,&
avgalbedo, avgtcdc, czen, czmean, mxsnal, radot, &
cfrach, cfracl, cfracm, avgcfrach, qshltr, &
avgcfracl, avgcfracm, cnvcfr, islope, cmc, grnflx,&
vegfrc, acfrcv, ncfrcv, acfrst, ncfrst, ssroff, &
bgroff, rlwin, &
rlwtoa, cldwork, alwin, alwout, alwtoa, rswin, &
rswinc, rswout, aswin, auvbin, auvbinc, aswout, &
aswtoa, sfcshx, sfclhx, subshx, snopcx, sfcux, &
sfcvx, sfcuvx, gtaux, gtauy, potevp, u10, v10, &
smstav, smstot, ivgtyp, isltyp, sfcevp, sfcexc, &
acsnow, acsnom, sst, thz0, qz0, uz0, vz0, ptop, &
htop, pbot, hbot, ptopl, pbotl, ttopl, ptopm, &
pbotm, ttopm, ptoph, pboth, pblcfr, ttoph, runoff,&
maxtshltr, mintshltr, maxrhshltr, minrhshltr, &
dzice, smcwlt, suntime, fieldcapa, htopd, hbotd, &
htops, hbots, aswintoa, maxqshltr, minqshltr, &
acond, sr, u10h, v10h, avgedir, avgecan, &
avgetrans, avgesnow, avgprec_cont, avgcprate_cont,&
avisbeamswin, avisdiffswin, airbeamswin, airdiffswin, &
alwoutc, alwtoac, aswoutc, aswtoac, alwinc, aswinc,&
avgpotevp, snoavg, ti, si, cuppt
use soil, only: sldpth, sh2o, smc, stc
use masks, only: lmv, lmh, htm, vtm, gdlat, gdlon, dx, dy, hbm2, sm, sice
use ctlblk_mod, only: im, jm, lm, lp1, jsta, jend, jsta_2l, jend_2u, jsta_m,jend_m, &
lsm, pt, imp_physics, spval, mpi_comm_comp, gdsdegr, &
tprec, tclod, trdlw, trdsw, tsrfc, tmaxmin, theat, &
ardlw, ardsw, asrfc, avrain, avcnvc, iSF_SURFACE_PHYSICS,&
td3d, idat, sdat, ifhr, ifmin, dt, nphs, dtq2, pt_tbl, &
alsl, spl, ihrst
use params_mod, only: erad, dtr, capa, p1000
use gridspec_mod,only: latstart, latlast, lonstart, lonlast, cenlon, cenlat, &
dxval, dyval, truelat2, truelat1, psmapf, cenlat, &
lonstartv, lonlastv, cenlonv, latstartv, latlastv, &
cenlatv,latstart_r,latlast_r,lonstart_r,lonlast_r, &
maptype, gridtype, STANDLON
use lookup_mod, only: thl, plq, ptbl, ttbl, rdq, rdth, rdp, rdthe, pl, &
qs0, sqs, sthe, ttblq, rdpq, rdtheq, stheq, the0q, the0
use physcons, only: grav => con_g, fv => con_fvirt, rgas => con_rd, &
eps => con_eps, epsm1 => con_epsm1
use rqstfld_mod
!
! use write_internal_state, only: wrt_internal_state
!
!-----------------------------------------------------------------------
!
implicit none
!
include 'mpif.h'
!
!-----------------------------------------------------------------------
!
type(wrt_internal_state),intent(in) :: wrt_int_state
integer,intent(in) :: mpicomp
logical,intent(inout) :: setvar_atmfile,setvar_sfcfile
!
!-----------------------------------------------------------------------
!
integer i, ip1, j, l, k, n, iret, ibdl, rc, kstart, kend
integer i1,i2,j1,j2,k1,k2
integer ista,iend,fieldDimCount,gridDimCount,ncount_field
integer jdate(8)
logical foundland, foundice, found, mvispresent
integer totalLBound3d(3), totalUBound3d(3)
real(4) rinc(5), fillvalue
real(8) fillvalue8
real tlmh,RADI,TMP,ES,TV,RHOAIR,tem,tstart,dtp
real, dimension(:),allocatable :: ak5, bk5
real(ESMF_KIND_R4),dimension(:,:),pointer :: arrayr42d
real(ESMF_KIND_R8),dimension(:,:),pointer :: arrayr82d
real(ESMF_KIND_R4),dimension(:,:,:),pointer :: arrayr43d
real(ESMF_KIND_R8),dimension(:,:,:),pointer :: arrayr83d
real,dimension(:), allocatable :: slat,qstl
real,external::FPVSNEW
real,dimension(:,:),allocatable :: dummy, p2d, t2d, q2d, qs2d, &
cw2d, cfr2d
character(len=80) :: fieldname, wrtFBName
type(ESMF_Grid) :: wrtGrid
type(ESMF_Field) :: theField
type(ESMF_Field), allocatable :: fcstField(:)
type(ESMF_TypeKind_Flag) :: typekind
type(ESMF_TypeKind_Flag) :: attTypeKind
real, parameter :: small=1.e-6
!
!-----------------------------------------------------------------------
!*** INTEGER SCALAR/1D HISTORY VARIABLES
!-----------------------------------------------------------------------
!
imp_physics = wrt_int_state%imp_physics !set GFS mp physics to 99 for Zhao scheme
dtp = wrt_int_state%dtp
iSF_SURFACE_PHYSICS = 2
spval = 9.99e20
!
! nems gfs has zhour defined
tprec = float(wrt_int_state%fhzero)
tclod = tprec
trdlw = tprec
trdsw = tprec
tsrfc = tprec
tmaxmin = tprec
td3d = tprec
if(mype==0)print*,'MP_PHYSICS= ',imp_physics,'nbdl=',nbdl, 'tprec=',tprec,'tclod=',tclod, &
'dtp=',dtp,'tmaxmin=',tmaxmin,'jsta=',jsta,jend,im,jm
!
!$omp parallel do default(shared),private(i,j)
do j=jsta,jend
do i=1,im
gdlat(i,j) = wrt_int_state%latPtr(i,j)
gdlon(i,j) = wrt_int_state%lonPtr(i,j)
enddo
enddo
!$omp parallel do default(none),private(i,j,ip1), &
!$omp& shared(jsta,jend_m,im,dx,gdlat,gdlon,dy)
do j = jsta, jend_m
do i = 1, im
ip1 = i + 1
if (ip1 > im) ip1 = ip1 - im
dx(i,j) = erad*cos(gdlat(i,j)*dtr)*(gdlon(ip1,j)-gdlon(i,j))*dtr
dy(i,j) = erad*(gdlat(i,j)-gdlat(i,j+1))*dtr ! like A*DPH
end do
end do
!
if(.not. allocated(ak5)) allocate(ak5(lm+1),bk5(lm+1))
do i=1,lm+1
ak5(i) = wrt_int_state%ak(i)
bk5(i) = wrt_int_state%bk(i)
enddo
!$omp parallel do default(none) private(i,j) shared(jsta,jend,im,f,gdlat)
do j=jsta,jend
do i=1,im
f(I,J) = 1.454441e-4*sin(gdlat(i,j)*dtr) ! 2*omeg*sin(phi)
end do
end do
!
pt = ak5(1)
! GFS may not have model derived radar ref.
! TKE
! cloud amount
!$omp parallel do default(none),private(i,j,l), &
!$omp& shared(lm,jsta,jend,im,spval,ref_10cm,q2,cfr)
do l=1,lm
do j=jsta,jend
do i=1,im
ref_10cm(i,j,l) = SPVAL
q2(i,j,l) = SPVAL
cfr(i,j,l) = SPVAL
enddo
enddo
enddo
! GFS does not have surface specific humidity
! inst sensible heat flux
! inst latent heat flux
!$omp parallel do default(none),private(i,j),shared(jsta,jend,im,spval,qs,twbs,qwbs,ths)
do j=jsta,jend
do i=1,im
qs(i,j) = SPVAL
twbs(i,j) = SPVAL
qwbs(i,j) = SPVAL
ths(i,j) = SPVAL
enddo
enddo
! GFS set up DT to compute accumulated fields, set it to one
dtq2 = wrt_int_state%dtp
nphs = 2.
dt = dtq2/nphs
!
! GFS does not have convective cloud efficiency
! similated precip
! 10 m theta
! 10 m humidity
! snow free albedo
!$omp parallel do default(none), private(i,j), shared(jsta,jend,im,spval), &
!$omp& shared(cldefi,lspa,th10,q10,albase)
do j=jsta,jend
do i=1,im
cldefi(i,j) = SPVAL
lspa(i,j) = SPVAL
th10(i,j) = SPVAL
q10(i,j) = SPVAL
albase(i,j) = SPVAL
enddo
enddo
! GFS does not have convective precip
!$omp parallel do default(none) private(i,j) shared(jsta,jend,im,cprate)
do j=jsta,jend
do i=1,im
cprate(i,j) = 0.
enddo
enddo
! GFS probably does not use zenith angle, czen, czmean
! inst surface outgoing longwave, radot
! inst cloud fraction for high, middle, and low cloud,
! cfrach
! inst ground heat flux, grnflx
!$omp parallel do default(none) private(i,j) shared(jsta,jend,im,spval), &
!$omp& shared(czen,czmean,radot,cfrach,cfracl,cfracm,grnflx)
do j=jsta,jend
do i=1,im
czen(i,j) = SPVAL
czmean(i,j) = SPVAL
radot(i,j) = SPVAL
cfrach(i,j) = SPVAL
cfracl(i,j) = SPVAL
cfracm(i,j) = SPVAL
grnflx(i,j) = SPVAL
enddo
enddo
!
! GFS doesn not yet output soil layer thickness, assign SLDPTH to be the same as nam
sldpth(1) = 0.10
sldpth(2) = 0.3
sldpth(3) = 0.6
sldpth(4) = 1.0
! GFS does not output time averaged convective and strat cloud fraction, set acfrcv to spval, n
! cfrcv to 1
! time averaged cloud fraction, set acfrst to spval, ncfrst to 1
! UNDERGROUND RUNOFF, bgroff
! inst incoming sfc longwave, rlwin
! inst model top outgoing longwave,rlwtoa
! inst incoming sfc shortwave, rswin
! inst incoming clear sky sfc shortwave, rswinc
! inst outgoing sfc shortwave, rswout
! snow phase change heat flux, snopcx
! GFS does not use total momentum flux,sfcuvx
!$omp parallel do default(none),private(i,j),shared(jsta,jend,im,spval), &
!$omp& shared(acfrcv,ncfrcv,acfrst,ncfrst,bgroff,rlwin,rlwtoa,rswin,rswinc,rswout,snopcx,sfcuvx)
do j=jsta,jend
do i=1,im
acfrcv(i,j) = spval
ncfrcv(i,j) = 1.0
acfrst(i,j) = spval
ncfrst(i,j) = 1.0
bgroff(i,j) = spval
rlwin(i,j) = spval
rlwtoa(i,j) = spval
rswin(i,j) = spval
rswinc(i,j) = spval
rswout(i,j) = spval
snopcx(i,j) = spval
sfcuvx(i,j) = spval
enddo
enddo
! GFS incoming sfc longwave has been averaged over 6 hr bucket, set ARDLW to 1
ardlw = 1.0
! GFS incoming sfc longwave has been averaged, set ARDLW to 1
ardsw = 1.0
! GFS surface flux has been averaged, set ASRFC to 1
asrfc = 1.0
! GFS does not have temperature tendency due to long wave radiation
! temperature tendency due to short wave radiation
! temperature tendency due to latent heating from convection
! temperature tendency due to latent heating from grid scale
do l=1,lm
!$omp parallel do default(none),private(i,j),shared(jsta_2l,jend_2u,im,spval,l), &
!$omp& shared(rlwtt,rswtt,tcucn,tcucns,train)
do j=jsta_2l,jend_2u
do i=1,im
rlwtt(i,j,l) = spval
rswtt(i,j,l) = spval
tcucn(i,j,l) = spval
tcucns(i,j,l) = spval
train(i,j,l) = spval
enddo
enddo
enddo
! set avrain to 1
avrain = 1.0
avcnvc = 1.0
theat = 6.0 ! just in case GFS decides to output T tendency
! GFS does not have temperature tendency due to latent heating from grid scale
train = spval
! GFS does not have soil moisture availability, smstav
! accumulated surface evaporatio, sfcevp
! averaged accumulated snow, acsnow
! snow melt,acsnom
! humidity at roughness length, qz0
! u at roughness length, uz0
! v at roughness length, vz0
! shelter rh max, maxrhshltr
! shelter rh min, minrhshltr
!$omp parallel do default(none),private(i,j),shared(jsta_2l,jend_2u,im,spval), &
!$omp& shared(smstav,sfcevp,acsnow,acsnom,qz0,uz0,vz0,maxrhshltr,minrhshltr)
do j=jsta_2l,jend_2u
do i=1,im
smstav(i,j) = spval
sfcevp(i,j) = spval
acsnow(i,j) = spval
acsnom(i,j) = spval
qz0(i,j) = spval
uz0(i,j) = spval
vz0(i,j) = spval
maxrhshltr(i,j) = SPVAL
minrhshltr(i,j) = SPVAL
enddo
enddo
! GFS does not have mixing length,el_pbl
! exchange coefficient, exch_h
do l=1,lm
!$omp parallel do default(none),private(i,j),shared(jsta_2l,jend_2u,im,l,spval,el_pbl,exch_h)
do j=jsta_2l,jend_2u
do i=1,im
el_pbl(i,j,l) = spval
exch_h(i,j,l) = spval
enddo
enddo
enddo
! GFS does not have deep convective cloud top and bottom fields
!$omp parallel do default(none),private(i,j),shared(jsta_2l,jend_2u,im,spval), &
!$omp& shared(htopd,hbotd,htops,hbots,cuppt)
do j=jsta_2l,jend_2u
do i=1,im
htopd(i,j) = SPVAL
hbotd(i,j) = SPVAL
htops(i,j) = SPVAL
hbots(i,j) = SPVAL
cuppt(i,j) = SPVAL
enddo
enddo
!
! get inital date
sdat(1) = wrt_int_state%idate(2) !month
sdat(2) = wrt_int_state%idate(3) !day
sdat(3) = wrt_int_state%idate(1) !year
ihrst = wrt_int_state%idate(4) !hour
idat(1) = wrt_int_state%fdate(2)
idat(2) = wrt_int_state%fdate(3)
idat(3) = wrt_int_state%fdate(1)
idat(4) = wrt_int_state%fdate(4)
idat(5) = wrt_int_state%fdate(5)
!
if(mype==0) print *,'idat=',idat,'sdat=',sdat,'ihrst=',ihrst
! CALL W3DIFDAT(JDATE,IDATE,0,RINC)
!
! if(mype==0)print *,' rinc=',rinc
! ifhr = nint(rinc(2)+rinc(1)*24.)
! if(mype==0)print *,' ifhr=',ifhr
! ifmin = nint(rinc(3))
! if(ifhr /= nint(fhour))print*,'find wrong Grib file';stop
! if(mype==0)print*,' in INITPOST ifhr ifmin =',ifhr,ifmin
!
tstart = 0.
!
!** initialize cloud water and ice mixing ratio
!$omp parallel do default(none),private(i,j,l),shared(lm,jsta,jend,im), &
!$omp& shared(qqw,qqr,qqs,qqi)
do l = 1,lm
do j = jsta, jend
do i = 1,im
qqw(i,j,l) = 0.
qqr(i,j,l) = 0.
qqs(i,j,l) = 0.
qqi(i,j,l) = 0.
enddo
enddo
enddo
!
!-----------------------------------------------------------------------------
! get post fields
!-----------------------------------------------------------------------------
!
foundland = .false.
foundice = .false.
if(mype==0) print *,'dong haha', wrt_int_state%FBCount
get_lsmsk: do ibdl=1, wrt_int_state%FBCount
! find lans sea mask
found = .false.
call ESMF_FieldBundleGet(wrt_int_state%wrtFB(ibdl),fieldName='land',isPresent=found, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
! if(mype==0) print *,'ibdl=',ibdl,'land, found=',found
if (found) then
call ESMF_FieldBundleGet(wrt_int_state%wrtFB(ibdl),'land',field=theField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
call ESMF_FieldGet(theField, localDe=0, farrayPtr=arrayr42d, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
call ESMF_AttributeGet(theField, convention="NetCDF", purpose="FV3", &
name='_FillValue', value=fillvalue, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
print *,'in post_lam, get land field value,fillvalue=',fillvalue
ista = lbound(arrayr42d,1)
iend = ubound(arrayr42d,1)
!$omp parallel do default(none),private(i,j),shared(jsta,jend,ista,iend,spval,arrayr42d,sm,fillValue)
do j=jsta, jend
do i=ista, iend
if (arrayr42d(i,j) /= spval .and. abs(arrayr42d(i,j)-fillValue)>small ) then
sm(i,j) = 1.- arrayr42d(i,j)
else
sm(i,j) = spval
endif
enddo
enddo
foundland = .true.
endif
! find ice fraction
found = .false.
call ESMF_FieldBundleGet(wrt_int_state%wrtFB(ibdl),'icec',isPresent=found, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
! if(mype==0) print *,'ibdl=',ibdl,'ice, found=',found
if (found) then
call ESMF_FieldBundleGet(wrt_int_state%wrtFB(ibdl),'icec',field=theField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
call ESMF_FieldGet(theField, localDe=0, farrayPtr=arrayr42d, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
call ESMF_AttributeGet(theField, convention="NetCDF", purpose="FV3", &
name='_FillValue', value=fillvalue, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
print *,'in post_lam, get icec field value,fillvalue=',fillvalue
ista = lbound(arrayr42d,1)
iend = ubound(arrayr42d,1)
!$omp parallel do default(none) private(i,j) shared(jsta,jend,ista,iend,spval,sice,arrayr42d,sm,fillValue)
do j=jsta, jend
do i=ista, iend
sice(i,j) = arrayr42d(i,j)
if(abs(arrayr42d(i,j)-fillvalue)<small) sice(i,j) = spval
if (sm(i,j) /= spval .and. sm(i,j) == 0.0) sice(i,j) = 0.0
enddo
enddo
foundice = .true.
endif
enddo get_lsmsk
if (.not.foundland .or. .not.foundice) then
rc=999
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
endif
if(mype==0) print *,'after find sm and sice,imp_physics=',imp_physics,'nbdl=',wrt_int_state%FBCount
!
file_loop_all: do ibdl=1, wrt_int_state%FBCount
!
! get grid dimension count
if(mype==0) print *,'in setvar, read field, ibdl=',ibdl,'idim=', &
ista,iend,'jdim=',jsta,jend
call ESMF_FieldBundleGet(wrt_int_state%wrtFB(ibdl), grid=wrtGrid, &
fieldCount=ncount_field, name=wrtFBName,rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
call ESMF_GridGet(wrtgrid, dimCount=gridDimCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
if(mype==0) print *,'in setvar, allocate fcstField,ibdl=',ibdl,'count=',ncount_field,'wrtFBname=',trim(wrtFBName)
allocate(fcstField(ncount_field))
call ESMF_FieldBundleGet(wrt_int_state%wrtFB(ibdl), &
fieldList=fcstField, itemorderflag=ESMF_ITEMORDER_ADDORDER, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
if(mype==0) print *,'in setvar, read field, ibdl=',ibdl, 'nfield=',ncount_field
do n=1, ncount_field
!
call ESMF_FieldGet(fcstField(n),typekind=typekind, name=fieldname, &
dimCount=fieldDimCount,rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
if (index(trim(fieldname),"vector") >0) cycle
!
!** for 2D fields
if (fieldDimCount == 2) then
if (typekind == ESMF_TYPEKIND_R4) then
call ESMF_FieldGet(fcstField(n), localDe=0, farrayPtr=arrayr42d, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
call ESMF_AttributeGet(fcstField(n), convention="NetCDF", purpose="FV3", &
name='_FillValue', value=fillValue, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
! fillValue=9.99E20
print *,'in post_lam, get field ',trim(fieldname),' fillValue=',fillValue
else if (typekind == ESMF_TYPEKIND_R8) then
call ESMF_FieldGet(fcstField(n), localDe=0, farrayPtr=arrayr82d, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
call ESMF_AttributeGet(fcstField(n), convention="NetCDF", purpose="FV3", &
name='_FillValue', value=fillValue8, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
fillValue = fillValue8
print *,'in post_lam, get field ',trim(fieldname),' fillvalue r82r4=',fillvalue
allocate( arrayr42d(ista:iend,jsta:jend))
!$omp parallel do default(none) private(i,j) shared(jsta,jend,ista,iend,arrayr42d,arrayr82d)
do j=jsta, jend
do i=ista, iend
arrayr42d(i,j) = arrayr82d(i,j)
enddo
enddo
endif
! Terrain height (*G later)
if(trim(fieldname)=='hgtsfc') then
!$omp parallel do default(none) private(i,j) shared(jsta,jend,ista,iend,fis,arrayr42d,fillValue,spval)
do j=jsta,jend
do i=ista, iend
fis(i,j)=arrayr42d(i,j)
if( abs(arrayr42d(i,j)-fillValue) < small) fis(i,j)=spval
enddo
enddo
endif
! Surface pressure
! if(trim(fieldname)=='pressfc') then
! !$omp parallel do private(i,j)
! do j=jsta,jend
! do i=ista, iend
! pint(i,j)=arrayr42d(i,j)
! enddo
! enddo
! endif
! PBL height using nemsio
if(trim(fieldname)=='hpbl') then
!$omp parallel do default(none) private(i,j) shared(jsta,jend,ista,iend,pblh,arrayr42d,fillValue,spval)
do j=jsta,jend
do i=ista, iend