-
Notifications
You must be signed in to change notification settings - Fork 153
/
gridio_gfs.f90
4898 lines (4565 loc) · 189 KB
/
gridio_gfs.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 gridio
!$$$ module documentation block
!
! module: gridio subroutines for reading and writing
! ensemble members files using
! EnKF internal format. A separate
! program must be run before and
! after the EnKF analysis to convert
! to and from the native model format.
!
! prgmmr: whitaker org: esrl/psd date: 2009-02-23
!
! abstract: I/O for ensemble member files.
!
! Public Functions:
! readgriddata, writegriddata
!
! this version reads and writes NCEP GFS sigma files.
!
! Public Variables: None
!
! Modules Used: constants (must be pre-initialized).
!
! program history log:
! 2009-02-23 Initial version.
! 2015-06-29 Add ability to read/write multiple time levels
! 2016-05-02 shlyaeva: Modification for reading state vector from table
! 2016-04-20 Modify to handle the updated nemsio sig file (P, DP, DPDT
! removed)
! 2016-11-29 shlyaeva: Add reading/calculating tsen, qi, ql. Pass filenames and
! hours to read routine to read separately state and control files.
! Pass levels and dimenstions to read/write routines (dealing with
! prse: nlevs + 1 levels). Pass "reducedgrid" parameter.
! 2017-06-14 Adding functionality to optionally write non-inflated ensembles,
! a required input for EFSO calculations
! 2019-03-13 Add precipitation components
! 2019-07-10 Add convective clouds
! 2022-07-21 Draper: added read/write for sfc file for nc io (writeincrements, and readgridata)
!
! attributes:
! language: f95
!
!$$$
use constants, only: zero,one,cp,fv,rd,tiny_r_kind,max_varname_length,t0c,r0_05
use params, only: nlons,nlats,nlevs,use_gfs_nemsio,pseudo_rh, &
cliptracers,datapath,imp_physics,use_gfs_ncio,cnvw_option, &
nanals
use kinds, only: i_kind,r_double,r_kind,r_single
use gridinfo, only: ntrunc,npts ! gridinfo must be called first!
use specmod, only: sptezv_s, sptez_s, init_spec_vars, ndimspec => nc, &
isinitialized
use reducedgrid_mod, only: regtoreduced, reducedtoreg
use mpisetup, only: nproc, numproc
use mpimod, only: mpi_comm_world, mpi_sum, mpi_real4, mpi_real8, mpi_rtype
use mpeu_util, only: getindex
implicit none
private
public :: readgriddata, readgriddata_pnc, writegriddata, writegriddata_pnc
public :: writeincrement, writeincrement_pnc
contains
subroutine readgriddata_pnc(vars3d,vars2d,n3d,n2d,levels,ndim,ntimes, &
fileprefixes,filesfcprefixes,reducedgrid,grdin,qsat)
use module_ncio, only: Dataset, Variable, Dimension, open_dataset,&
quantize_data,read_attribute, close_dataset, get_dim, read_vardata
implicit none
character(len=max_varname_length), dimension(n2d), intent(in) :: vars2d
character(len=max_varname_length), dimension(n3d), intent(in) :: vars3d
integer, intent(in) :: n2d, n3d
integer, dimension(0:n3d), intent(in) :: levels
integer, intent(in) :: ndim, ntimes
character(len=120), dimension(7), intent(in) :: fileprefixes
character(len=120), dimension(7), intent(in) :: filesfcprefixes
logical, intent(in) :: reducedgrid
real(r_single), dimension(npts,ndim,ntimes,1), intent(out) :: grdin
real(r_double), dimension(npts,nlevs,ntimes,1), intent(out) :: qsat
character(len=500) :: filename,sfcfilename
character(len=7) charnanal
real(r_kind) :: kap,kapr,kap1,clip,qi_coef
real(r_kind), allocatable, dimension(:,:) :: vmassdiv
real(r_single), allocatable, dimension(:,:) :: pressi,pslg,values_2d
real(r_kind), dimension(nlons*nlats) :: ug,vg
real(r_single), dimension(npts,nlevs) :: tv, q, cw
real(r_kind), dimension(ndimspec) :: vrtspec,divspec
real(r_kind), allocatable, dimension(:) :: psg,pstend,ak,bk
real(r_single),allocatable,dimension(:,:,:) :: ug3d,vg3d
type(Dataset) :: dset
type(Dimension) :: londim,latdim,levdim
integer(i_kind) :: u_ind, v_ind, tv_ind, q_ind, oz_ind, cw_ind
integer(i_kind) :: qr_ind, qs_ind, qg_ind
integer(i_kind) :: tsen_ind, ql_ind, qi_ind, prse_ind
integer(i_kind) :: ps_ind, pst_ind, sst_ind
integer(i_kind) :: k,iret,nb,i,imem,idvc,nlonsin,nlatsin,nlevsin,ne,nanal
logical ice
logical use_full_hydro
integer(i_kind), allocatable, dimension(:) :: mem_pe, lev_pe1, lev_pe2, iocomms
integer(i_kind) :: iope, ionumproc, iolevs, krev, ierr
integer(i_kind) :: ncstart(3), nccount(3)
! mpi gatherv things
integer(i_kind), allocatable, dimension(:) :: recvcounts, displs
real(r_single), dimension(nlons,nlats,nlevs) :: ug3d_0, vg3d_0
logical :: read_sfc_file, read_atm_file
call set_ncio_file_flags(vars3d, n3d, vars2d, n2d, read_sfc_file, read_atm_file)
if (read_sfc_file) then
print *,'paranc not supported for reading surface files'
call mpi_barrier(mpi_comm_world,ierr)
call mpi_finalize(ierr)
endif
! figure out what member to read and do MPI sub-communicator things
allocate(mem_pe(0:numproc-1))
allocate(iocomms(nanals))
imem = 1
do i=0,numproc-1
mem_pe(i) = imem
imem = imem + 1
if (imem > nanals) imem = 1
end do
nanal = mem_pe(nproc)
call mpi_comm_split(mpi_comm_world, mem_pe(nproc), nproc, iocomms(mem_pe(nproc)), iret)
call mpi_comm_rank(iocomms(mem_pe(nproc)), iope, iret)
call mpi_comm_size(iocomms(mem_pe(nproc)), ionumproc, iret)
! figure out what levels to read on this sub-communicator's PE
allocate(lev_pe1(0:ionumproc-1))
allocate(lev_pe2(0:ionumproc-1))
iolevs = nlevs/ionumproc
do i=0,ionumproc-1
lev_pe1(i) = (i * iolevs) + 1
lev_pe2(i) = ((i + 1) * iolevs)
if (i == ionumproc-1) lev_pe2(i) = lev_pe2(i) + modulo(nlevs, ionumproc)
end do
ncstart = (/1, 1, lev_pe1(iope)/)
nccount = (/nlons, nlats, lev_pe2(iope) - lev_pe1(iope)+1/)
! some mpi gatherv calculations
allocate(recvcounts(ionumproc))
allocate(displs(ionumproc))
do i=0, ionumproc-1
recvcounts(i+1) = (lev_pe2(i) - lev_pe1(i)+1)*nlons*nlats
displs(i+1) = ((lev_pe1(i)-1)*nlons*nlats)
end do
! loop through times and do the read
ne = 1
backgroundloop: do nb=1,ntimes
write(charnanal,'(a3, i3.3)') 'mem', nanal
filename = trim(adjustl(datapath))//trim(adjustl(fileprefixes(nb)))//trim(charnanal)
sfcfilename = trim(adjustl(datapath))//trim(adjustl(filesfcprefixes(nb)))//trim(charnanal)
if (use_gfs_ncio) then
dset = open_dataset(filename, paropen=.true., mpicomm=iocomms(mem_pe(nproc)))
londim = get_dim(dset,'grid_xt'); nlonsin = londim%len
latdim = get_dim(dset,'grid_yt'); nlatsin = latdim%len
levdim = get_dim(dset,'pfull'); nlevsin = levdim%len
idvc=2
else
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** parallel read only supported for netCDF' , ' PROGRAM STOPS'
call stop2(23)
end if
ice = .false. ! calculate qsat w/resp to ice?
kap = rd/cp
kapr = cp/rd
kap1 = kap+one
clip = tiny(vg(1))
u_ind = getindex(vars3d, 'u') !< indices in the state or control var arrays
v_ind = getindex(vars3d, 'v') ! U and V (3D)
tv_ind = getindex(vars3d, 'tv') ! Tv (3D)
q_ind = getindex(vars3d, 'q') ! Q (3D)
oz_ind = getindex(vars3d, 'oz') ! Oz (3D)
cw_ind = getindex(vars3d, 'cw') ! CW (3D)
tsen_ind = getindex(vars3d, 'tsen') !sensible T (3D)
ql_ind = getindex(vars3d, 'ql') ! QL (3D)
qi_ind = getindex(vars3d, 'qi') ! QI (3D)
prse_ind = getindex(vars3d, 'prse')
qr_ind = getindex(vars3d, 'qr') ! QR (3D)
qs_ind = getindex(vars3d, 'qs') ! QS (3D)
qg_ind = getindex(vars3d, 'qg') ! QG (3D)
ps_ind = getindex(vars2d, 'ps') ! Ps (2D)
pst_ind = getindex(vars2d, 'pst') ! Ps tendency (2D) // equivalent of
! old logical massbal_adjust, if non-zero
sst_ind = getindex(vars2d, 'sst')
use_full_hydro = ( ql_ind > 0 .and. qi_ind > 0 .and. &
qr_ind > 0 .and. qs_ind > 0 .and. qg_ind > 0 )
! Currently, we do not let precipiation to affect the enkf analysis
! The following line will be removed after testing
use_full_hydro = .false.
if (.not. isinitialized) call init_spec_vars(nlons,nlats,ntrunc,4)
allocate(pressi(nlons*nlats,nlevs+1))
allocate(pslg(npts,nlevs))
allocate(psg(nlons*nlats))
if (pst_ind > 0) allocate(vmassdiv(nlons*nlats,nlevs),pstend(nlons*nlats))
call read_vardata(dset, 'pressfc', values_2d,errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading ps, iret= ',iret,' PROGRAM STOPS'
call stop2(31)
endif
psg = 0.01_r_kind*reshape(values_2d,(/nlons*nlats/))
call read_attribute(dset, 'ak', ak)
call read_attribute(dset, 'bk', bk)
if (nanal .eq. 1 .and. iope==0) then
print *,'time level ',nb
print *,'---------------'
endif
! pressure at interfaces
do k=1,nlevs+1
! k=1 in ak,bk is model top
pressi(:,k) = 0.01_r_kind*ak(nlevs-k+2)+bk(nlevs-k+2)*psg
if (nanal .eq. 1 .and. iope==0) print *,'netcdf, min/max pressi',k,minval(pressi(:,k)),maxval(pressi(:,k))
enddo
deallocate(ak,bk,values_2d)
call read_vardata(dset, 'ugrd', ug3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading ugrd, iret= ',iret,' PROGRAM STOPS'
call stop2(22)
endif
call read_vardata(dset, 'vgrd', vg3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading vgrd, iret= ',iret,' PROGRAM STOPS'
call stop2(23)
endif
call mpi_gatherv(ug3d, recvcounts(iope+1), mpi_real4, ug3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
call mpi_gatherv(vg3d, recvcounts(iope+1), mpi_real4, vg3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
if (iope==0) then
do k=1,nlevs
krev = nlevs-k+1
ug = reshape(ug3d_0(:,:,krev),(/nlons*nlats/))
vg = reshape(vg3d_0(:,:,krev),(/nlons*nlats/))
if (u_ind > 0) call copytogrdin(ug,grdin(:,levels(u_ind-1) + k,nb,ne))
if (v_ind > 0) call copytogrdin(vg,grdin(:,levels(v_ind-1) + k,nb,ne))
! calculate vertical integral of mass flux div (ps tendency)
! this variable is analyzed in order to enforce mass balance in the analysis
if (pst_ind > 0) then
krev = nlevs-k+1
ug = ug*(pressi(:,krev)-pressi(:,krev+1))
vg = vg*(pressi(:,krev)-pressi(:,krev+1))
call sptezv_s(divspec,vrtspec,ug,vg,-1) ! u,v to div,vrt
call sptez_s(divspec,vmassdiv(:,krev),1) ! divspec to divgrd
endif
enddo
end if
call read_vardata(dset,'tmp', ug3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading tmp, iret= ',iret,' PROGRAM STOPS'
call stop2(24)
endif
call read_vardata(dset,'spfh', vg3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading spfh, iret= ',iret,' PROGRAM STOPS'
call stop2(25)
endif
call mpi_gatherv(ug3d, recvcounts(iope+1), mpi_real4, ug3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
call mpi_gatherv(vg3d, recvcounts(iope+1), mpi_real4, vg3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
if (iope==0) then
do k=1,nlevs
krev = nlevs-k+1
ug = reshape(ug3d_0(:,:,krev),(/nlons*nlats/))
vg = reshape(vg3d_0(:,:,krev),(/nlons*nlats/))
if (tsen_ind > 0) call copytogrdin(ug,grdin(:,levels(tsen_ind-1)+k,nb,ne))
call copytogrdin(vg, q(:,k))
ug = ug * ( 1.0 + fv*vg ) ! convert T to Tv
call copytogrdin(ug,tv(:,k))
if (tv_ind > 0) grdin(:,levels(tv_ind-1)+k,nb,ne) = tv(:,k)
if (q_ind > 0) grdin(:,levels( q_ind-1)+k,nb,ne) = q(:,k)
end do
end if
if (oz_ind > 0) then
call read_vardata(dset, 'o3mr', ug3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading o3mr, iret= ',iret,' PROGRAM STOPS'
call stop2(26)
endif
if (cliptracers) where (ug3d < clip) ug3d = clip
call mpi_gatherv(ug3d, recvcounts(iope+1), mpi_real4, ug3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
if (iope==0) then
do k=1,nlevs
krev = nlevs-k+1
ug = reshape(ug3d_0(:,:,krev),(/nlons*nlats/))
call copytogrdin(ug,grdin(:,levels(oz_ind-1)+k,nb,ne))
end do
end if
endif
! Read in hydrometeor fields based on control/state variables listed in anavinfo table
if (use_full_hydro) then
if(ql_ind > 0) then
call read_vardata(dset, 'clwmr', ug3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading clwmr, iret= ',iret,' PROGRAM STOPS'
call stop2(26)
endif
if (cliptracers) where (ug3d < clip) ug3d = clip
call mpi_gatherv(ug3d, recvcounts(iope+1), mpi_real4, ug3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
if (iope==0) then
do k=1,nlevs
krev = nlevs-k+1
ug = reshape(ug3d_0(:,:,krev),(/nlons*nlats/))
call copytogrdin(ug,grdin(:,levels(ql_ind-1)+k,nb,ne))
end do
end if
endif
if(qi_ind > 0) then
call read_vardata(dset, 'icmr', ug3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading icmr, iret= ',iret,' PROGRAM STOPS'
call stop2(26)
endif
if (cliptracers) where (ug3d < clip) ug3d = clip
call mpi_gatherv(ug3d, recvcounts(iope+1), mpi_real4, ug3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
if (iope==0) then
do k=1,nlevs
krev = nlevs-k+1
ug = reshape(ug3d_0(:,:,krev),(/nlons*nlats/))
call copytogrdin(ug,grdin(:,levels(qi_ind-1)+k,nb,ne))
end do
end if
endif
if(qr_ind > 0) then
call read_vardata(dset, 'rwmr', ug3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading rwmr, iret= ',iret,' PROGRAM STOPS'
call stop2(26)
endif
if (cliptracers) where (ug3d < clip) ug3d = clip
call mpi_gatherv(ug3d, recvcounts(iope+1), mpi_real4, ug3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
if (iope==0) then
do k=1,nlevs
krev = nlevs-k+1
ug = reshape(ug3d_0(:,:,krev),(/nlons*nlats/))
call copytogrdin(ug,grdin(:,levels(qr_ind-1)+k,nb,ne))
end do
end if
endif
if(qs_ind > 0) then
call read_vardata(dset, 'snmr', ug3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading snmr, iret= ',iret,' PROGRAM STOPS'
call stop2(26)
endif
if (cliptracers) where (ug3d < clip) ug3d = clip
call mpi_gatherv(ug3d, recvcounts(iope+1), mpi_real4, ug3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
if (iope==0) then
do k=1,nlevs
krev = nlevs-k+1
ug = reshape(ug3d_0(:,:,krev),(/nlons*nlats/))
call copytogrdin(ug,grdin(:,levels(qs_ind-1)+k,nb,ne))
end do
end if
endif
if(qg_ind > 0) then
call read_vardata(dset, 'grle', ug3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading grle, iret= ',iret,' PROGRAM STOPS'
call stop2(26)
endif
if (cliptracers) where (ug3d < clip) ug3d = clip
call mpi_gatherv(ug3d, recvcounts(iope+1), mpi_real4, ug3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
if (iope==0) then
do k=1,nlevs
krev = nlevs-k+1
ug = reshape(ug3d_0(:,:,krev),(/nlons*nlats/))
call copytogrdin(ug,grdin(:,levels(qg_ind-1)+k,nb,ne))
end do
end if
endif
else
! Handle non-precipiting hydrometeors
! if control or state variable is cw, make sure combine background ql and qi to cw
if (cw_ind > 0 .or. ql_ind > 0 .or. qi_ind > 0) then
call read_vardata(dset, 'clwmr', ug3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading clwmr, iret= ',iret,' PROGRAM STOPS'
call stop2(27)
endif
if (imp_physics == 11) then
call read_vardata(dset, 'icmr', vg3d, ncstart=ncstart, nccount=nccount, errcode=iret)
if (iret /= 0) then
write(6,*)'READGRIDDATA_PNC: ***FATAL ERROR*** reading icmr, iret= ',iret,' PROGRAM STOPS'
call stop2(28)
endif
ug3d = ug3d + vg3d
endif
if (cliptracers) where (ug3d < clip) ug3d = clip
call mpi_gatherv(ug3d, recvcounts(iope+1), mpi_real4, ug3d_0, recvcounts, displs,&
mpi_real4, 0, iocomms(mem_pe(nproc)),iret)
if (iope==0) then
do k=1,nlevs
krev = nlevs-k+1
ug = reshape(ug3d_0(:,:,krev),(/nlons*nlats/))
call copytogrdin(ug,cw(:,k))
if (cw_ind > 0) grdin(:,levels(cw_ind-1)+k,nb,ne) = cw(:,k)
end do
end if
endif
endif
deallocate(ug3d,vg3d)
! surface pressure
if (ps_ind > 0 .and. iope==0) then
call copytogrdin(psg,grdin(:,levels(n3d) + ps_ind,nb,ne))
endif
! surface pressure tendency
if (pst_ind > 0 .and. iope==0) then
pstend = sum(vmassdiv,2)
if (nanal .eq. 1 .and. iope==0) &
print *,nanal,'min/max first-guess ps tend',minval(pstend),maxval(pstend)
call copytogrdin(pstend,grdin(:,levels(n3d) + pst_ind,nb,ne))
endif
if (iope==0) then
do k=1,nlevs
! pressure at bottom of layer interface (for gps jacobian, see prsltmp in setupbend.f90)
if (prse_ind > 0) then
ug(:) = pressi(:,k)
call copytogrdin(ug,pslg(:,k))
! Jacobian for gps in pressure is saved in different units in GSI; need to
! multiply pressure by 0.1
grdin(:,levels(prse_ind-1)+k,nb,ne) = 0.1*pslg(:,k)
endif
! layer pressure from phillips vertical interolation (used for qsat
! calculation)
ug(:) = ((pressi(:,k)**kap1-pressi(:,k+1)**kap1)/&
(kap1*(pressi(:,k)-pressi(:,k+1))))**kapr
call copytogrdin(ug,pslg(:,k))
end do
if (pseudo_rh) then
call genqsat1(q,qsat(:,:,nb,ne),pslg,tv,ice,npts,nlevs)
else
qsat(:,:,nb,ne) = 1._r_double
end if
end if
! cloud derivatives
! Currently, we do not let precipiation to affect the enkf analysis
! The following line will be removed after testing
use_full_hydro = .true.
if (.not. use_full_hydro .and. iope==0) then
if (ql_ind > 0 .or. qi_ind > 0) then
do k=1,nlevs
do i = 1, npts
qi_coef = -r0_05*(tv(i,k)/(one+fv*q(i,k))-t0c)
qi_coef = max(zero,qi_coef)
qi_coef = min(one,qi_coef) ! 0<=qi_coef<=1
if (ql_ind > 0) then
grdin(i,levels(ql_ind-1)+k,nb,ne) = cw(i,k)*(one-qi_coef)
endif
if (qi_ind > 0) then
grdin(i,levels(qi_ind-1)+k,nb,ne) = cw(i,k)*qi_coef
endif
enddo
enddo
endif
endif
if (sst_ind > 0 .and. iope==0) then
grdin(:,levels(n3d)+sst_ind, nb,ne) = zero
endif
! bring all the subdomains back to the main PE
call mpi_barrier(iocomms(mem_pe(nproc)), iret)
deallocate(pressi,pslg)
deallocate(psg)
if (pst_ind > 0) deallocate(vmassdiv,pstend)
call close_dataset(dset)
call mpi_barrier(iocomms(mem_pe(nproc)), iret)
end do backgroundloop ! loop over backgrounds to read in
! remove the sub communicators
call mpi_barrier(iocomms(mem_pe(nproc)), iret)
call mpi_comm_free(iocomms(mem_pe(nproc)), iret)
call mpi_barrier(mpi_comm_world, iret)
return
contains
! copying to grdin (calling regtoreduced if reduced grid)
subroutine copytogrdin(field, grdin)
implicit none
real(r_kind), dimension(:), intent(in) :: field
real(r_single), dimension(:), intent(inout) :: grdin
if (reducedgrid) then
call regtoreduced(field, grdin)
else
grdin = field
endif
end subroutine copytogrdin
end subroutine readgriddata_pnc
subroutine readgriddata(nanal1,nanal2,vars3d,vars2d,n3d,n2d,levels,ndim,ntimes, &
fileprefixes,filesfcprefixes,reducedgrid,grdin,qsat)
use sigio_module, only: sigio_head, sigio_data, sigio_sclose, sigio_sropen, &
sigio_srohdc, sigio_sclose, sigio_aldata, sigio_axdata
use nemsio_module, only: nemsio_gfile,nemsio_open,nemsio_close,&
nemsio_getfilehead,nemsio_getheadvar,nemsio_realkind,nemsio_charkind,&
nemsio_readrecv,nemsio_init,nemsio_setheadvar,nemsio_writerecv
use module_ncio, only: Dataset, Variable, Dimension, open_dataset,&
quantize_data,read_attribute, close_dataset, get_dim, read_vardata
implicit none
integer, intent(in) :: nanal1,nanal2
character(len=max_varname_length), dimension(n2d), intent(in) :: vars2d
character(len=max_varname_length), dimension(n3d), intent(in) :: vars3d
integer, intent(in) :: n2d, n3d
integer, dimension(0:n3d), intent(in) :: levels
integer, intent(in) :: ndim, ntimes
character(len=120), dimension(7), intent(in) :: fileprefixes
character(len=120), dimension(7), intent(in) :: filesfcprefixes
logical, intent(in) :: reducedgrid
real(r_single), dimension(npts,ndim,ntimes,nanal2-nanal1+1), intent(out) :: grdin
real(r_double), dimension(npts,nlevs,ntimes,nanal2-nanal1+1), intent(out) :: qsat
character(len=500) :: filename
character(len=500) :: filenamesfc
character(len=7) charnanal
real(r_kind) :: kap,kapr,kap1,clip,qi_coef
real(r_kind), allocatable, dimension(:,:) :: vmassdiv
real(r_single), allocatable, dimension(:,:) :: pressi,pslg,values_2d
real(r_kind), dimension(nlons*nlats) :: ug,vg
real(r_single), dimension(npts,nlevs) :: tv, q, cw
real(r_single), dimension(npts,nlevs) :: ql, qi, qr, qs, qg
real(r_kind), dimension(ndimspec) :: vrtspec,divspec
real(r_kind), allocatable, dimension(:) :: psg,pstend,ak,bk
real(r_single),allocatable,dimension(:,:,:) :: nems_vcoord, ug3d,vg3d
real(nemsio_realkind), dimension(nlons*nlats) :: nems_wrk,nems_wrk2
type(sigio_head) :: sighead
type(sigio_data) :: sigdata
type(nemsio_gfile) :: gfile
type(Dataset) :: dset, dset_sfc
type(Dimension) :: londim,latdim,levdim
type(nemsio_gfile) :: gfilesfc
integer(i_kind) :: u_ind, v_ind, tv_ind, q_ind, oz_ind, cw_ind
integer(i_kind) :: qr_ind, qs_ind, qg_ind
integer(i_kind) :: tsen_ind, ql_ind, qi_ind, prse_ind
integer(i_kind) :: ps_ind, pst_ind, sst_ind
integer(i_kind) :: tmp2m_ind, spfh2m_ind, soilt1_ind, soilt2_ind, soilt3_ind
integer(i_kind) :: soilt4_ind,slc1_ind, slc2_ind, slc3_ind, slc4_ind
integer(i_kind) :: k,iunitsig,iret,nb,i,idvc,nlonsin,nlatsin,nlevsin,ne,nanal
integer(i_kind) :: nlonsin_sfc,nlatsin_sfc
logical ice
logical use_full_hydro
logical read_sfc_file, read_atm_file
use_full_hydro = .false.
! determine which files will be read in
call set_ncio_file_flags(vars3d, n3d, vars2d, n2d, read_sfc_file, read_atm_file)
ne = 0
ensmemloop: do nanal=nanal1,nanal2
ne = ne + 1
backgroundloop: do nb=1,ntimes
if (nanal > 0) then
write(charnanal,'(a3, i3.3)') 'mem', nanal
else
charnanal = 'ensmean'
endif
iunitsig = 77
filename = trim(adjustl(datapath))//trim(adjustl(fileprefixes(nb)))//trim(charnanal)
filenamesfc = trim(adjustl(datapath))//trim(adjustl(filesfcprefixes(nb)))//trim(charnanal)
if (use_gfs_nemsio) then
call nemsio_init(iret=iret)
if(iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_init, iret=',iret
call stop2(23)
end if
call nemsio_open(gfile,filename,'READ',iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_open, iret=',iret
call stop2(23)
endif
call nemsio_getfilehead(gfile,iret=iret, dimx=nlonsin, dimy=nlatsin,&
dimz=nlevsin,idvc=idvc)
if (nlons /= nlonsin .or. nlats /= nlatsin .or. nlevs /= nlevsin) then
print *,'incorrect dims in nemsio file'
print *,'expected',nlons,nlats,nlevs
print *,'got',nlonsin,nlatsin,nlevsin
call stop2(23)
end if
if (cnvw_option) then
call nemsio_open(gfilesfc,filenamesfc,'READ',iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with sfc nemsio_open, iret=',iret
else
call nemsio_getfilehead(gfilesfc,iret=iret, dimx=nlonsin_sfc, dimy=nlatsin_sfc)
if (nlons /= nlonsin_sfc .or. nlats /= nlatsin_sfc) then
print *,'incorrect dims in nemsio sfc file'
print *,'expected',nlons,nlats
print *,'got',nlonsin_sfc,nlatsin_sfc
end if
endif
endif
else if (use_gfs_ncio) then
dset = open_dataset(filename)
londim = get_dim(dset,'grid_xt'); nlonsin = londim%len
latdim = get_dim(dset,'grid_yt'); nlatsin = latdim%len
levdim = get_dim(dset,'pfull'); nlevsin = levdim%len
idvc=2
else
call sigio_srohdc(iunitsig,trim(filename), &
sighead,sigdata,iret)
if (iret /= 0) then
print *,'error reading file in gridio ',trim(filename)
call stop2(23)
end if
endif
ice = .false. ! calculate qsat w/resp to ice?
kap = rd/cp
kapr = cp/rd
kap1 = kap+one
u_ind = getindex(vars3d, 'u') !< indices in the state or control var arrays
v_ind = getindex(vars3d, 'v') ! U and V (3D)
tv_ind = getindex(vars3d, 'tv') ! Tv (3D)
q_ind = getindex(vars3d, 'q') ! Q (3D)
oz_ind = getindex(vars3d, 'oz') ! Oz (3D)
cw_ind = getindex(vars3d, 'cw') ! CW (3D)
tsen_ind = getindex(vars3d, 'tsen') !sensible T (3D)
ql_ind = getindex(vars3d, 'ql') ! QL (3D)
qi_ind = getindex(vars3d, 'qi') ! QI (3D)
prse_ind = getindex(vars3d, 'prse')
qr_ind = getindex(vars3d, 'qr') ! QR (3D)
qs_ind = getindex(vars3d, 'qs') ! QS (3D)
qg_ind = getindex(vars3d, 'qg') ! QG (3D)
ps_ind = getindex(vars2d, 'ps') ! Ps (2D)
pst_ind = getindex(vars2d, 'pst') ! Ps tendency (2D) // equivalent of
! old logical massbal_adjust, if non-zero
sst_ind = getindex(vars2d, 'sst')
use_full_hydro = ( ql_ind > 0 .and. qi_ind > 0 .and. &
qr_ind > 0 .and. qs_ind > 0 .and. qg_ind > 0 )
! if (nproc == 0) then
! print *, 'indices: '
! print *, 'u: ', u_ind, ', v: ', v_ind, ', tv: ', tv_ind, ', tsen: ', tsen_ind
! print *, 'q: ', q_ind, ', oz: ', oz_ind, ', cw: ', cw_ind, ', qi: ', qi_ind
! print *, 'ql: ', ql_ind, ', prse: ', prse_ind
! print *, 'ps: ', ps_ind, ', pst: ', pst_ind, ', sst: ', sst_ind
! endif
if (read_atm_file) then
if (.not. isinitialized) call init_spec_vars(nlons,nlats,ntrunc,4)
allocate(pressi(nlons*nlats,nlevs+1))
allocate(pslg(npts,nlevs))
allocate(psg(nlons*nlats))
if (pst_ind > 0) allocate(vmassdiv(nlons*nlats,nlevs),pstend(nlons*nlats))
if (use_gfs_nemsio) then
call nemsio_readrecv(gfile,'pres','sfc',1,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(ps), iret=',iret
call stop2(23)
endif
psg = 0.01_r_kind*nems_wrk ! convert ps to millibars.
if (allocated(nems_vcoord)) deallocate(nems_vcoord)
allocate(nems_vcoord(nlevs+1,3,2))
call nemsio_getfilehead(gfile,iret=iret,vcoord=nems_vcoord)
if ( iret /= 0 ) then
write(6,*)' gridio: ***ERROR*** problem reading header ', &
'vcoord, Status = ',iret
call stop2(99)
endif
allocate(ak(nlevs+1),bk(nlevs+1))
if ( idvc == 0 ) then ! sigma coordinate, old file format.
ak = zero
bk = nems_vcoord(1:nlevs+1,1,1)
elseif ( idvc == 1 ) then ! sigma coordinate
ak = zero
bk = nems_vcoord(1:nlevs+1,2,1)
elseif ( idvc == 2 .or. idvc == 3 ) then ! hybrid coordinate
ak = 0.01_r_kind*nems_vcoord(1:nlevs+1,1,1) ! convert to mb
bk = nems_vcoord(1:nlevs+1,2,1)
else
write(6,*)'gridio: ***ERROR*** INVALID value for idvc=',idvc
call stop2(85)
endif
if (nanal .eq. 1) then
print *,'time level ',nb
print *,'---------------'
endif
! pressure at interfaces
do k=1,nlevs+1
pressi(:,k)=ak(k)+bk(k)*psg
if (nanal .eq. 1) print *,'nemsio, min/max pressi',k,minval(pressi(:,k)),maxval(pressi(:,k))
enddo
deallocate(ak,bk)
else if (use_gfs_ncio) then
call read_vardata(dset, 'pressfc', values_2d,errcode=iret)
if (iret /= 0) then
print *,'error reading ps'
call stop2(31)
endif
psg = 0.01_r_kind*reshape(values_2d,(/nlons*nlats/))
call read_attribute(dset, 'ak', ak)
call read_attribute(dset, 'bk', bk)
if (nanal .eq. 1) then
print *,'time level ',nb
print *,'---------------'
endif
! pressure at interfaces
do k=1,nlevs+1
! k=1 in ak,bk is model top
pressi(:,k) = 0.01_r_kind*ak(nlevs-k+2)+bk(nlevs-k+2)*psg
if (nanal .eq. 1) print *,'netcdf, min/max pressi',k,minval(pressi(:,k)),maxval(pressi(:,k))
enddo
deallocate(ak,bk)
else
vrtspec = sigdata%ps
call sptez_s(vrtspec,psg,1)
!==> input psg is ln(ps) in centibars - convert to ps in millibars.
psg = 10._r_kind*exp(psg)
allocate(ak(nlevs+1),bk(nlevs+1))
if (sighead%idvc .eq. 0) then ! sigma coordinate, old file format.
ak = zero
bk = sighead%si(1:nlevs+1)
else if (sighead%idvc == 1) then ! sigma coordinate
ak = zero
bk = sighead%vcoord(1:nlevs+1,2)
else if (sighead%idvc == 2 .or. sighead%idvc == 3) then ! hybrid coordinate
bk = sighead%vcoord(1:nlevs+1,2)
ak = 0.01_r_kind*sighead%vcoord(1:nlevs+1,1) ! convert to mb
else
print *,'unknown vertical coordinate type',sighead%idvc
call stop2(32)
end if
!==> pressure at interfaces.
if (nanal .eq. 1) then
print *,'time level ',nb
print *,'---------------'
endif
do k=1,nlevs+1
pressi(:,k)=ak(k)+bk(k)*psg
if (nanal .eq. 1) print *,'sigio, min/max pressi',k,minval(pressi(:,k)),maxval(pressi(:,k))
enddo
deallocate(ak,bk)
endif
!==> get U,V,temp,q,ps on gaussian grid.
! u is first nlevs, v is second, t is third, then tracers.
if (use_gfs_nemsio) then
clip=tiny_r_kind
do k=1,nlevs
call nemsio_readrecv(gfile,'ugrd','mid layer',k,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(ugrd), iret=',iret
call stop2(23)
endif
ug = nems_wrk
call nemsio_readrecv(gfile,'vgrd','mid layer',k,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(vgrd), iret=',iret
call stop2(23)
endif
vg = nems_wrk
if (u_ind > 0) call copytogrdin(ug,grdin(:,levels(u_ind-1) + k,nb,ne))
if (v_ind > 0) call copytogrdin(vg,grdin(:,levels(v_ind-1) + k,nb,ne))
! calculate vertical integral of mass flux div (ps tendency)
! this variable is analyzed in order to enforce mass balance in the analysis
if (pst_ind > 0) then
ug = ug*(pressi(:,k)-pressi(:,k+1))
vg = vg*(pressi(:,k)-pressi(:,k+1))
call sptezv_s(divspec,vrtspec,ug,vg,-1) ! u,v to div,vrt
call sptez_s(divspec,vmassdiv(:,k),1) ! divspec to divgrd
endif
call nemsio_readrecv(gfile,'tmp','mid layer',k,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(tmp), iret=',iret
call stop2(23)
endif
call nemsio_readrecv(gfile,'spfh','mid layer',k,nems_wrk2,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(spfh), iret=',iret
call stop2(23)
endif
if (cliptracers) where (nems_wrk2 < clip) nems_wrk2 = clip
ug = nems_wrk
if (tsen_ind > 0) call copytogrdin(ug,grdin(:,levels(tsen_ind-1)+k,nb,ne))
nems_wrk = nems_wrk * ( 1.0 + fv*nems_wrk2 ) ! convert T to Tv
ug = nems_wrk
vg = nems_wrk2
call copytogrdin(ug,tv(:,k))
call copytogrdin(vg, q(:,k))
if (tv_ind > 0) grdin(:,levels(tv_ind-1)+k,nb,ne) = tv(:,k)
if (q_ind > 0) grdin(:,levels( q_ind-1)+k,nb,ne) = q(:,k)
if (oz_ind > 0) then
call nemsio_readrecv(gfile,'o3mr','mid layer',k,nems_wrk2,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(o3mr), iret=',iret
call stop2(23)
endif
if (cliptracers) where (nems_wrk2 < clip) nems_wrk2 = clip
ug = nems_wrk2
call copytogrdin(ug,grdin(:,levels(oz_ind-1)+k,nb,ne))
endif
if (.not. use_full_hydro) then
if (cw_ind > 0 .or. ql_ind > 0 .or. qi_ind > 0) then
call nemsio_readrecv(gfile,'clwmr','mid layer',k,nems_wrk2,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(clwmr), iret=',iret
call stop2(23)
endif
if (imp_physics == 11) then
call nemsio_readrecv(gfile,'icmr','mid layer',k,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(icmr), iret=',iret
call stop2(23)
else
nems_wrk2 = nems_wrk2 + nems_wrk
endif
endif
if (cnvw_option) then
call nemsio_readrecv(gfilesfc,'cnvcldwat','mid layer',k,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(cnvw), iret=',iret
call stop2(23)
else
nems_wrk2 = nems_wrk2 + nems_wrk
end if
end if
if (cliptracers) where (nems_wrk2 < clip) nems_wrk2 = clip
ug = nems_wrk2
call copytogrdin(ug,cw(:,k))
if (cw_ind > 0) grdin(:,levels(cw_ind-1)+k,nb,ne) = cw(:,k)
endif
else
if (ql_ind > 0) then
call nemsio_readrecv(gfile,'clwmr','mid layer',k,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(clwmr), iret=',iret
call stop2(23)
endif
if (cliptracers) where (nems_wrk < clip) nems_wrk = clip
ug = nems_wrk
call copytogrdin(ug,ql(:,k))
grdin(:,levels(ql_ind-1)+k,nb,ne) = ql(:,k)
endif
if (qi_ind > 0) then
call nemsio_readrecv(gfile,'icmr','mid layer',k,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(icmr), iret=',iret
call stop2(23)
endif
if (cliptracers) where (nems_wrk < clip) nems_wrk = clip
ug = nems_wrk
call copytogrdin(ug,qi(:,k))
grdin(:,levels(qi_ind-1)+k,nb,ne) = qi(:,k)
endif
if (qr_ind > 0) then
call nemsio_readrecv(gfile,'rwmr','mid layer',k,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(rwmr), iret=',iret
call stop2(23)
endif
if (cliptracers) where (nems_wrk < clip) nems_wrk = clip
ug = nems_wrk
call copytogrdin(ug,qr(:,k))
grdin(:,levels(qr_ind-1)+k,nb,ne) = qr(:,k)
endif
if (qs_ind > 0) then
call nemsio_readrecv(gfile,'snmr','mid layer',k,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(snmr), iret=',iret
call stop2(23)
endif
if (cliptracers) where (nems_wrk < clip) nems_wrk = clip
ug = nems_wrk
call copytogrdin(ug,qs(:,k))
grdin(:,levels(qs_ind-1)+k,nb,ne) = qs(:,k)
endif
if (qg_ind > 0) then
call nemsio_readrecv(gfile,'grle','mid layer',k,nems_wrk,iret=iret)
if (iret/=0) then
write(6,*)'gridio/readgriddata: gfs model: problem with nemsio_readrecv(grle), iret=',iret
call stop2(23)
endif
if (cliptracers) where (nems_wrk < clip) nems_wrk = clip
ug = nems_wrk
call copytogrdin(ug,qg(:,k))
grdin(:,levels(qg_ind-1)+k,nb,ne) = qg(:,k)
endif
endif ! use_full_hydro
enddo
else if (use_gfs_ncio) then
call read_vardata(dset, 'ugrd', ug3d,errcode=iret)
if (iret /= 0) then
print *,'error reading ugrd'
call stop2(22)
endif
call read_vardata(dset, 'vgrd', vg3d,errcode=iret)
if (iret /= 0) then
print *,'error reading vgrd'
call stop2(23)
endif
do k=1,nlevs
ug = reshape(ug3d(:,:,nlevs-k+1),(/nlons*nlats/))
vg = reshape(vg3d(:,:,nlevs-k+1),(/nlons*nlats/))
if (u_ind > 0) call copytogrdin(ug,grdin(:,levels(u_ind-1) + k,nb,ne))
if (v_ind > 0) call copytogrdin(vg,grdin(:,levels(v_ind-1) + k,nb,ne))
! calculate vertical integral of mass flux div (ps tendency)
! this variable is analyzed in order to enforce mass balance in the analysis
if (pst_ind > 0) then
ug = ug*(pressi(:,k)-pressi(:,k+1))
vg = vg*(pressi(:,k)-pressi(:,k+1))
call sptezv_s(divspec,vrtspec,ug,vg,-1) ! u,v to div,vrt
call sptez_s(divspec,vmassdiv(:,k),1) ! divspec to divgrd
endif
enddo
call read_vardata(dset,'tmp', ug3d,errcode=iret)
if (iret /= 0) then
print *,'error reading tmp'
call stop2(24)
endif
call read_vardata(dset,'spfh', vg3d,errcode=iret)
if (iret /= 0) then
print *,'error reading spfh'
call stop2(25)
endif
do k=1,nlevs
ug = reshape(ug3d(:,:,nlevs-k+1),(/nlons*nlats/))
vg = reshape(vg3d(:,:,nlevs-k+1),(/nlons*nlats/))
if (tsen_ind > 0) call copytogrdin(ug,grdin(:,levels(tsen_ind-1)+k,nb,ne))
call copytogrdin(vg, q(:,k))
ug = ug * ( 1.0 + fv*vg ) ! convert T to Tv
call copytogrdin(ug,tv(:,k))
if (tv_ind > 0) grdin(:,levels(tv_ind-1)+k,nb,ne) = tv(:,k)
if (q_ind > 0) grdin(:,levels( q_ind-1)+k,nb,ne) = q(:,k)
enddo
if (oz_ind > 0) then
call read_vardata(dset, 'o3mr', ug3d,errcode=iret)
if (iret /= 0) then
print *,'error reading o3mr'
call stop2(26)
endif
if (cliptracers) where (ug3d < clip) ug3d = clip
do k=1,nlevs
ug = reshape(ug3d(:,:,nlevs-k+1),(/nlons*nlats/))
call copytogrdin(ug,grdin(:,levels(oz_ind-1)+k,nb,ne))
enddo
endif
if (cw_ind > 0 .or. ql_ind > 0 .or. qi_ind > 0) then
call read_vardata(dset, 'clwmr', ug3d,errcode=iret)
if (iret /= 0) then
print *,'error reading clwmr'
call stop2(27)
endif
if (imp_physics == 11) then
call read_vardata(dset, 'icmr', vg3d,errcode=iret)
if (iret /= 0) then
print *,'error reading icmr'
call stop2(28)
endif
ug3d = ug3d + vg3d
endif
if (cliptracers) where (ug3d < clip) ug3d = clip
do k=1,nlevs