forked from GEOS-ESM/FVdycoreCubed_GridComp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fv_regrid_c2c.F90
1926 lines (1683 loc) · 81.1 KB
/
fv_regrid_c2c.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
#include "MAPL_ErrLog.h"
module fv_regrid_c2c
#ifdef MAPL_MODE
#define DEALLOCGLOB_(A) if(associated(A)) then;A=0;if(MAPL_ShmInitialized) then; call MAPL_DeAllocNodeArray(A,rc=status);else; deallocate(A);endif;NULLIFY(A);endif
#endif
use fv_arrays_mod, only: REAL4, REAL8, FVPRC
use fms_mod, only: file_exist, read_data, field_exist
use fms_io_mod, only: get_tile_string, field_size
use mpp_mod, only: mpp_error, FATAL, NOTE, mpp_broadcast,mpp_npes
use mpp_parameter_mod, only: AGRID_PARAM=>AGRID
use mpp_domains_mod, only: mpp_get_tile_id, domain2d, mpp_update_domains, mpp_get_boundary, DGRID_NE
use tracer_manager_mod, only: get_tracer_names, get_number_tracers, get_tracer_index
use field_manager_mod, only: MODEL_ATMOS
use MAPL
use gFTL_StringVector
use gFTL_StringIntegerMap
use, intrinsic :: iso_fortran_env, only: REAL64, REAL32
use fv_arrays_mod, only: fv_atmos_type, fv_grid_type, fv_grid_bounds_type, FVPRC, REAL4, REAL8
use fv_diagnostics_mod,only: prt_maxmin
use fv_mp_mod, only: is_master, ng, mp_barrier, mp_gather, mp_bcst, &
is,js,ie,je, isd,jsd,ied,jed, fill_corners, YDir
use fv_grid_utils_mod, only: ptop_min
use fv_grid_utils_mod, only: normalize_vect
use fv_mapz_mod, only: mappm
use fv_surf_map_mod, only: surfdrv
use fv_timing_mod, only: timing_on, timing_off
use init_hydro_mod, only: p_var
use fv_timing_mod, only: timing_on, timing_off
use mpp_mod, only: mpp_pe
use memutils_mod, only: print_memuse_stats
use fv_regridding_utils
use ESMF
implicit none
#include "mpif.h"
private
real(FVPRC), parameter :: PI = MAPL_PI_R8
real(FVPRC), parameter :: OMEGA = MAPL_OMEGA
real(FVPRC), parameter :: GRAV = MAPL_GRAV
real(FVPRC), parameter :: KAPPA = MAPL_KAPPA
real(FVPRC), parameter :: RDGAS = MAPL_RGAS
real(FVPRC), parameter :: RVGAS = MAPL_RVAP
real(FVPRC), parameter :: CP_AIR = MAPL_CP
real(FVPRC), parameter:: zvir = rvgas/rdgas - 1.
public get_geos_ic
integer, SAVE :: tile, npes_x, npes_y
integer :: status
integer :: IUNIT=15
integer :: OUNIT=16
interface read_topo_file
module procedure read_topo_file_r4
module procedure read_topo_file_r8
end interface
contains
subroutine read_topo_file_r4(fname,output,grid,rc)
character(len=*), intent(in) :: fname
type(ESMF_Grid), intent(in) :: grid
real(real4), intent(inout) :: output(:,:)
integer, intent(out), optional :: rc
integer :: status,dims(3),funit
real, allocatable :: input(:,:)
call MAPL_GridGet(grid,globalCellCountPerDim=dims,_RC)
allocate(input(dims(1),dims(2)))
open(newunit=funit,file=trim(fname),form='unformatted',iostat=status)
_VERIFY(status)
read(funit)input
call ArrayScatter(local_array=output,global_array=input,grid=grid,_RC)
_RETURN(_SUCCESS)
end subroutine read_topo_file_r4
subroutine read_topo_file_r8(fname,output,grid,rc)
character(len=*), intent(in) :: fname
type(ESMF_Grid), intent(in) :: grid
real(real8), intent(inout) :: output(:,:)
integer, intent(out), optional :: rc
integer :: status,dims(3),funit
real, allocatable :: input(:,:)
real(real8), allocatable :: input_r8(:,:)
call MAPL_GridGet(grid,globalCellCountPerDim=dims,_RC)
allocate(input(dims(1),dims(2)),input_r8(dims(2),dims(2)))
open(newunit=funit,file=trim(fname),form='unformatted',iostat=status)
_VERIFY(status)
read(funit)input
input_r8 = input
call ArrayScatter(local_array=output,global_array=input_r8,grid=grid,_RC)
_RETURN(_SUCCESS)
end subroutine read_topo_file_r8
subroutine get_geos_ic( Atm, extra_rst, rstcube, gridOut)
type(fv_atmos_type), intent(inout) :: Atm(:)
type(fv_rst), pointer, intent(inout) :: extra_rst(:)
logical :: rstcube
type(ESMF_Grid), intent(inout) :: gridOut
real(FVPRC):: alpha = 0.
integer i,j
logical :: cubed_sphere,fv_diag_ic
! * Initialize coriolis param:
do j=jsd,jed+1
do i=isd,ied+1
Atm(1)%gridstruct%fc(i,j) = 2.*omega*( -1.*cos(Atm(1)%gridstruct%grid(i,j,1))*cos(Atm(1)%gridstruct%grid(i,j,2))*sin(alpha) + &
sin(Atm(1)%gridstruct%grid(i,j,2))*cos(alpha) )
enddo
enddo
do j=jsd,jed
do i=isd,ied
Atm(1)%gridstruct%f0(i,j) = 2.*omega*( -1.*cos(Atm(1)%gridstruct%agrid(i,j,1))*cos(Atm(1)%gridstruct%agrid(i,j,2))*sin(alpha) + &
sin(Atm(1)%gridstruct%agrid(i,j,2))*cos(alpha) )
enddo
enddo
cubed_sphere=.true.
fv_diag_ic=.false.
call mpp_update_domains( Atm(1)%gridstruct%f0, Atm(1)%domain )
if ( cubed_sphere ) call fill_corners(Atm(1)%gridstruct%f0, Atm(1)%npx, Atm(1)%npy, YDir)
Atm(1)%phis = 0.
do i=1,size(extra_rst)
do j=1,size(extra_rst(i)%vars)
call extra_rst(i)%vars(j)%alloc_var(isd,ied,jsd,jed)
enddo
enddo
if (allocated(Atm(1)%q)) deallocate( Atm(1)%q )
allocate ( Atm(1)%q(isd:ied,jsd:jed,Atm(1)%npz,Atm(1)%ncnst) )
call get_geos_cubed_ic( Atm, extra_rst, gridOut )
call prt_maxmin('T', Atm(1)%pt, is, ie, js, je, ng, Atm(1)%npz, 1.0_FVPRC)
Atm(1)%flagstruct%moist_phys=.false.
call p_var(Atm(1)%npz, is, ie, js, je, Atm(1)%ak(1), ptop_min, &
Atm(1)%delp, Atm(1)%delz, Atm(1)%pt, Atm(1)%ps, &
Atm(1)%pe, Atm(1)%peln, Atm(1)%pk, Atm(1)%pkz, &
kappa, Atm(1)%q, ng, Atm(1)%ncnst, dble(Atm(1)%gridstruct%area),Atm(1)%flagstruct%dry_mass, &
Atm(1)%flagstruct%adjust_dry_mass, Atm(1)%flagstruct%mountain, Atm(1)%flagstruct%moist_phys, &
Atm(1)%flagstruct%hydrostatic, Atm(1)%flagstruct%nwat, Atm(1)%domain,Atm(1)%flagstruct%make_nh, do_pkz=.false.)
end subroutine get_geos_ic
subroutine get_geos_cubed_ic( Atm, extra_rst, gridOut )
use GHOST_CUBSPH_mod, only : A_grid, ghost_cubsph_update
use CUB2CUB_mod, only: get_c2c_weight, &
interpolate_c2c
type(fv_atmos_type), intent(inout) :: Atm(:)
type(fv_rst), pointer, intent(inout) :: extra_rst(:)
type(ESMF_Grid), intent(inout) :: gridOut
character(len=128) :: fname, fname1
real(FVPRC), allocatable:: pkz0(:,:)
real(FVPRC), allocatable:: ps0(:,:), gz0(:,:), t0(:,:,:), q0(:,:,:),qlev(:,:)
real(FVPRC), allocatable:: u0(:,:,:), v0(:,:,:)
real(FVPRC), allocatable:: ak0(:), bk0(:)
integer :: i, j, k, l, iq, im, jm, km, npts, npx, npy, npz
integer :: ntiles=6
character (len=8) :: imc, jmc
real(FVPRC) psc(is:ie,js:je)
real(FVPRC) gzc(is:ie,js:je)
real(FVPRC), allocatable:: tp(:,:,:), qp(:,:,:,:)
real(FVPRC), allocatable:: ua(:,:,:), va(:,:,:)
real(REAL64), allocatable :: akbk_r8(:)
real(REAL64), dimension(:,:,:,:), allocatable :: corner_in, corner_out
integer :: is_i,ie_i, js_i,je_i
integer :: isd_i,ied_i, jsd_i,jed_i
integer :: ng_i = 5
type(domain2d) :: domain_i
real(FVPRC), allocatable :: ebuffer(:,:)
real(FVPRC), allocatable :: nbuffer(:,:)
real(FVPRC), allocatable :: wbuffer(:,:)
real(FVPRC), allocatable :: sbuffer(:,:)
integer (kind=MPI_OFFSET_KIND) :: offset
integer :: filetype,nqmap
type(Netcdf4_Fileformatter) :: formatter
type(FileMetadata), allocatable :: cfg(:)
integer :: nDims, nVars, ivar, n_ungrid
character(len=128) :: vname
real(FVPRC), allocatable :: gslice_r4(:,:)
real*8, allocatable :: gslice_r8(:,:)
integer :: tileoff,lvar_cnt,ifile,nlev
type(fv_rst), pointer :: tracer_bundles(:) => null()
!bma added
character(len=:), pointer :: var_name
type(StringVariableMap), pointer :: variables
type(Variable), pointer :: myVariable
type(StringVector) :: all_moist_vars
type(StringVector), pointer :: var_dimensions
type(StringVectorIterator) :: siter
type(StringVector) :: moist_variables
type(StringIntegerMap) :: moist_tracers
integer, pointer :: iptr
character(len=:), pointer :: cptr
type(StringIntegerMapIterator) :: iter
type(CubedSphereGridFactory) :: cs_factory
type(ESMF_Grid) :: gridIn
class(AbstractRegridder), pointer :: regridder=>null()
npx = Atm(1)%npx
npy = Atm(1)%npy
npz = Atm(1)%npz
! Zero out all initial tracer fields:
Atm(1)%q = 0.
! Read input FV core restart file
fname = "fvcore_internal_restart_in"
if( file_exist(fname) ) then
allocate(cfg(1))
call formatter%open(fname,pFIO_READ,rc=status)
cfg(1) = formatter%read(rc=status)
im =cfg(1)%get_dimension('lon',rc=status)
jm =cfg(1)%get_dimension('lat',rc=status)
km =cfg(1)%get_dimension('lev',rc=status)
allocate(gslice_r8(im,jm),stat=status)
if(is_master()) write(*,*) 'Using GEOS restart:', fname
if ( file_exist(fname) ) then
if(is_master()) write(*,*) 'External IC dimensions:', im , jm , km
if(is_master()) write(*,*) 'Interpolating to :', npx-1, (npy-1)*6, npz
else
call mpp_error(FATAL,'==> Error from get_geos_ic: &
& field not found')
endif
cs_factory = CubedSphereGridFactory(nx=Atm(1)%layout(1),ny=Atm(1)%layout(2),im_world=im,lm=km)
gridIn = grid_manager%make_grid(cs_factory,rc=status)
regridder => new_regridder_manager%make_regridder(gridIn,gridOut,REGRID_METHOD_BILINEAR,rc=status)
!--------------------------------------------------------------------!
! setup input cubed-sphere domain !
!--------------------------------------------------------------------!
call mpp_domain_decomp(domain_i,im+1,(jm/ntiles)+1,ntiles,ng_i,ntiles, &
is_i,ie_i,js_i,je_i,isd_i,ied_i,jsd_i,jed_i,tile)
!--------------------------------------------------------------------!
! initialize cubed sphere grid: in !
!--------------------------------------------------------------------!
allocate(corner_in(2,is_i:ie_i+1,js_i:je_i+1,tile:tile))
call init_cubsph_grid(im+1, is_i,ie_i, js_i,je_i, ntiles, corner_in)
call print_memuse_stats('get_geos_cubed_ic: init corner_in')
!--------------------------------------------------------------------!
! initialize cubed sphere grid: out !
!--------------------------------------------------------------------!
allocate(corner_out(2,is:ie+1,js:je+1,tile:tile))
do l=tile,tile
do j=js,je+1
do i=is,ie+1
corner_out(1,i,j,l) = Atm(1)%gridstruct%grid(i,j,1)
corner_out(2,i,j,l) = Atm(1)%gridstruct%grid(i,j,2)
enddo
enddo
enddo
call print_memuse_stats('get_geos_cubed_ic: init corner_out')
npts = im
call print_memuse_stats('get_geos_cubed_ic: get_c2c_weight')
allocate ( ak0(km+1) )
allocate ( bk0(km+1) )
allocate ( akbk_r8(km+1) )
call MAPL_VarRead(formatter,"AK",akbk_r8)
ak0 = akbk_r8
call MAPL_VarRead(formatter,"BK",akbk_r8)
bk0 = akbk_r8
deallocate ( akbk_r8 )
call print_memuse_stats('get_geos_cubed_ic: read ak/bk')
close (IUNIT)
! Read U
allocate ( u0(isd_i:ied_i,jsd_i:jed_i+1,km) )
u0(:,:,:) = 0.0
tileoff = (tile-1)*(jm/ntiles)
do k=1,km
call MAPL_VarRead(formatter,"U",gslice_r8,lev=k)
u0(is_i:ie_i,js_i:je_i,k) = gslice_r8(is_i:ie_i,tileoff+js_i:tileoff+je_i)
enddo
call print_memuse_stats('get_geos_cubed_ic: read U')
! Read V
allocate ( v0(isd_i:ied_i+1,jsd_i:jed_i,km) )
v0(:,:,:) = 0.0
tileoff = (tile-1)*(jm/ntiles)
do k=1,km
call MAPL_VarRead(formatter,"V",gslice_r8,lev=k)
v0(is_i:ie_i,js_i:je_i,k) = gslice_r8(is_i:ie_i,tileoff+js_i:tileoff+je_i)
enddo
call print_memuse_stats('get_geos_cubed_ic: read V')
allocate ( sbuffer(is_i:ie_i,km) )
allocate ( wbuffer(js_i:je_i,km) )
allocate ( nbuffer(is_i:ie_i,km) )
allocate ( ebuffer(js_i:je_i,km) )
call mpp_get_boundary(u0, v0, domain_i, &
wbuffery=wbuffer, ebuffery=ebuffer, &
sbufferx=sbuffer, nbufferx=nbuffer, &
gridtype=DGRID_NE )
do k=1,km
do i=is_i,ie_i
u0(i,je_i+1,k) = nbuffer(i,k)
enddo
do j=js_i,je_i
v0(ie_i+1,j,k) = ebuffer(j,k)
enddo
enddo
deallocate ( sbuffer )
deallocate ( wbuffer )
deallocate ( nbuffer )
deallocate ( ebuffer )
call mpp_update_domains( u0, v0, domain_i, gridtype=DGRID_NE, complete=.true. )
call prt_maxmin(' U_geos', u0, is_i, ie_i, js_i, je_i, ng_i, km, 1.0_FVPRC)
call prt_maxmin(' V_geos', v0, is_i, ie_i, js_i, je_i, ng_i, km, 1.0_FVPRC)
allocate ( ua(is:ie,js:je,km) )
allocate ( va(is:ie,js:je,km) )
call interp_c2c_vect(npts, npts, npx-1, npy-1, km, ntiles, domain_i, &
is,ie, js,je, isd_i,ied_i, jsd_i,jed_i, is_i,ie_i, js_i,je_i, &
u0, v0, ua, va, regridder, corner_in(:,is_i:ie_i+1,js_i:je_i+1,tile), corner_out, Atm(1)%gridstruct)
deallocate ( v0 )
deallocate ( u0 )
deallocate ( corner_in )
deallocate ( corner_out )
! Read T
allocate ( t0(isd_i:ied_i,jsd_i:jed_i,km) )
t0(:,:,:) = 0.0
tileoff = (tile-1)*(jm/ntiles)
do k=1,km
call MAPL_VarRead(formatter,"PT",gslice_r8,lev=k)
t0(is_i:ie_i,js_i:je_i,k) = gslice_r8(is_i:ie_i,tileoff+js_i:tileoff+je_i)
enddo
call print_memuse_stats('get_geos_cubed_ic: read T')
! Read PE at Surface only
allocate ( ps0(isd_i:ied_i,jsd_i:jed_i) )
ps0(:,:) = 0.0
tileoff = (tile-1)*(jm/ntiles)
call MAPL_VarRead(formatter,"PE",gslice_r8,lev=km+1)
ps0(is_i:ie_i,js_i:je_i) = gslice_r8(is_i:ie_i,tileoff+js_i:tileoff+je_i)
call mpp_update_domains(ps0, domain_i)
! Read PKZ
allocate ( pkz0(isd_i:ied_i,jsd_i:jed_i) )
pkz0(:,:) = 0.0
tileoff = (tile-1)*(jm/ntiles)
do k=1,km
call MAPL_VarRead(formatter,"PKZ",gslice_r8,lev=k)
pkz0(is_i:ie_i,js_i:je_i) = gslice_r8(is_i:ie_i,tileoff+js_i:tileoff+je_i)
t0(is_i:ie_i,js_i:je_i,k) = t0(is_i:ie_i,js_i:je_i,k)*pkz0(is_i:ie_i,js_i:je_i)
enddo
call print_memuse_stats('get_geos_cubed_ic: converted T')
deallocate ( pkz0 )
call formatter%close()
deallocate(cfg)
deallocate(gslice_r8)
allocate ( gz0(isd_i:ied_i,jsd_i:jed_i) )
gz0(:,:) = 0.0
write(imc, "(i8)") im
write(jmc, "(i8)") jm
imc = adjustl(imc)
jmc = adjustl(jmc)
write(fname1, "('topo_DYN_ave_',a,'x',a,'.data')") trim(imc), trim(jmc)
if (.not. file_exist(fname1)) then
call mpp_error(FATAL,'get_geos_cubed_ic: cannot find topo_DYN_ave file')
endif
call print_memuse_stats('get_geos_cubed_ic: '//TRIM(fname1)//' being read')
offset = 4
call read_topo_file(fname1,gz0(is_i:ie_i,js_i:je_i),gridIn)
!call parallel_read_file_r4(fname1, npts, is_i,ie_i, js_i,je_i, 1, offset, gz0(is_i:ie_i,js_i:je_i))
call mpp_update_domains(gz0, domain_i)
gz0 = gz0*grav
! Read cubed-sphere phis from file since IMPORT is not ready yet
offset = 4
!call parallel_read_file_r4('topo_dynave.data', Atm(1)%npx-1, is,ie, js,je, 1, offset, Atm(1)%phis(is:ie,js:je))
call read_topo_file('topo_dynave.data',atm(1)%phis(is:ie,js:je),gridOut)
call mpp_update_domains(Atm(1)%phis, Atm(1)%domain)
Atm(1)%phis = Atm(1)%phis*grav
call print_memuse_stats('get_geos_cubed_ic: phis')
! Horiz Interp for surface pressure
call prt_maxmin('PS_geos', ps0, is_i, ie_i, js_i, je_i, ng_i, 1, 1.0_FVPRC)
call regridder%regrid(ps0(is_i:ie_i,js_i:je_i),psc(is:ie,js:je),rc=status)
deallocate ( ps0 )
! Horiz Interp for surface height
call prt_maxmin('GZ_geos', gz0, is_i, ie_i, js_i, je_i, ng_i, 1, 1.0/grav)
call regridder%regrid(gz0(is_i:ie_i,js_i:je_i),gzc(is:ie,js:je),rc=status)
deallocate ( gz0 )
! Horiz Interp for Q
allocate ( q0(isd_i:ied_i,jsd_i:jed_i,km) )
allocate ( qp(is:ie,js:je,km,Atm(1)%ncnst) )
q0(:,:,:) = 0.0
qp(:,:,:,:) = 0.0
! Horiz Interp for moist tracers
! is there a moist restart file to interpolate?
! Read in tracers: only sphum at this point
if( file_exist("moist_internal_restart_in") ) then
if (is_master()) print*, 'Trying to interpolate moist_internal_restart_in'
call MAPL_NCIOGetFileType("moist_internal_restart_in",filetype)
lvar_cnt = 0
allocate(gslice_r4(im,jm))
allocate(cfg(1))
call formatter%open("moist_internal_restart_in",pFIO_READ,rc=status)
cfg(1) = formatter%read(rc=status)
all_moist_vars = MAPL_IOGetNonDimVars(cfg(1))
siter = all_moist_vars%begin()
Variables => cfg(1)%get_variables()
do while(siter /= all_moist_vars%end())
var_name => siter%get()
myVariable => variables%at(var_name)
var_dimensions => myVariable%get_dimensions()
ndims = var_dimensions%size()
if (ndims == 3) call moist_variables%push_back(trim(var_name))
call siter%next()
enddo
if (moist_variables%size() /= atm(1)%ncnst) call mpp_error(FATAL,'Wrong number of variables in moist file')
tileoff = (tile-1)*(jm/ntiles)
lvar_cnt=2
do ivar=1,Atm(1)%ncnst
vname = moist_variables%at(ivar)
if (trim(vname)=='Q') then
iq=1
else
iq=lvar_cnt
lvar_cnt=lvar_cnt+1
end if
call moist_tracers%insert(trim(vname),iq)
do k=1,km
call MAPL_VarRead(formatter,vname,gslice_r4,lev=k)
q0(is_i:ie_i,js_i:je_i,k)=gslice_r4(is_i:ie_i,tileoff+js_i:tileoff+je_i)
call regridder%regrid(q0(is_i:ie_i,js_i:je_i,k),qp(:,:,k,iq),rc=status)
enddo
call prt_maxmin( 'Q_geos_moist', q0, is_i, ie_i, js_i, je_i, ng_i, km, 1._FVPRC)
enddo
call formatter%close()
deallocate(cfg)
deallocate(gslice_r4)
end if
! Horiz Interp for extra tracers
! make copy of input on input levs
call copy_fv_rst(extra_rst,tracer_bundles)
do i=1,size(extra_rst)
do j=1,size(extra_rst(i)%vars)
if (extra_rst(i)%vars(j)%nLev/=1) then
if (extra_rst(i)%vars(j)%nLev == npz) then
tracer_bundles(i)%vars(j)%nLev=km
call tracer_bundles(i)%vars(j)%alloc_var(is,ie,js,je,km)
else if (extra_rst(i)%vars(j)%nLev == npz+1) then
tracer_bundles(i)%vars(j)%nLev=km+1
call tracer_bundles(i)%vars(j)%alloc_var(is,ie,js,je,km+1)
end if
else
call tracer_bundles(i)%vars(j)%alloc_Var(is,ie,js,je)
end if
enddo
enddo
do ifile=1,size(tracer_bundles)
if (is_master()) print*, 'Trying to interpolate: ',trim(tracer_bundles(ifile)%file_name)
allocate(gslice_r4(im,jm))
allocate(cfg(1))
call formatter%open(trim(tracer_bundles(ifile)%file_name),pFIO_READ,rc=status)
cfg(1) = formatter%read(rc=status)
call MAPL_IOCountNonDimVars(cfg(1),nvars,rc=status)
tileoff = (tile-1)*(jm/ntiles)
allocate ( qlev(isd_i:ied_i,jsd_i:jed_i) )
qlev(:,:) = 0.0
do ivar=1,size(tracer_bundles(ifile)%vars)
nlev=tracer_bundles(ifile)%vars(ivar)%nLev
vname = trim(tracer_bundles(ifile)%vars(ivar)%name)
if (tracer_bundles(ifile)%vars(ivar)%rank ==2) then
call MAPL_VarRead(formatter,vname,gslice_r4)
qlev(is_i:ie_i,js_i:je_i)=gslice_r4(is_i:ie_i,tileoff+js_i:tileoff+je_i)
call regridder%regrid(qlev(is_i:ie_i,js_i:je_i),tracer_bundles(ifile)%vars(ivar)%ptr2d(is:ie,js:je),rc=status)
else if (tracer_bundles(ifile)%vars(ivar)%rank ==3) then
do k=1,nlev
call MAPL_VarRead(formatter,vname,gslice_r4,lev=k)
qlev(is_i:ie_i,js_i:je_i)=gslice_r4(is_i:ie_i,tileoff+js_i:tileoff+je_i)
call regridder%regrid(qlev(is_i:ie_i,js_i:je_i),tracer_bundles(ifile)%vars(ivar)%ptr3d(is:ie,js:je,k),rc=status)
enddo
else if (tracer_bundles(ifile)%vars(ivar)%rank ==4) then
do n_ungrid=1,tracer_bundles(ifile)%vars(ivar)%n_ungrid
do k=1,nlev
call MAPL_VarRead(formatter,vname,gslice_r4,lev=k,offset2=n_ungrid)
qlev(is_i:ie_i,js_i:je_i)=gslice_r4(is_i:ie_i,tileoff+js_i:tileoff+je_i)
call regridder%regrid(qlev(is_i:ie_i,js_i:je_i),tracer_bundles(ifile)%vars(ivar)%ptr4d(is:ie,js:je,k,n_ungrid),rc=status)
enddo
enddo
end if
!call prt_maxmin( 'Q_geos_gocart', q0, is_i, ie_i, js_i, je_i, ng_i, km, 1._FVPRC)
enddo
call formatter%close()
deallocate(cfg)
deallocate(gslice_r4)
deallocate(qlev)
enddo
! Horiz Interp for T
deallocate ( q0 )
call mpp_update_domains(t0, domain_i)
call prt_maxmin( 'T_geos', t0, is_i, ie_i, js_i, je_i, ng_i, km, 1.0_FVPRC)
allocate ( tp(is:ie,js:je,km) )
do k=1,km
call regridder%regrid(t0(is_i:ie_i,js_i:je_i,k),tp(:,:,k),rc=status)
enddo
deallocate ( t0 )
! Horz/Vert remap for scalars
nqmap = Atm(1)%ncnst
call remap_scalar(im, jm, km, npz, nqmap, nqmap, ak0, bk0, psc, gzc, tp, qp, Atm(1),tracer_bundles,extra_rst)
deallocate ( tp )
deallocate ( qp )
call print_memuse_stats('get_geos_cubed_ic: remap_scalar')
! Horz/Vert remap for U/V
call remap_winds(im, jm, km, npz, ak0, bk0, psc, ua, va, Atm)
deallocate ( ua )
deallocate ( va )
call print_memuse_stats('get_geos_cubed_ic: remap_winds')
else
call mpp_error(FATAL,'==> Error from get_geos_ic: &
& Expected file '//trim(fname)//' does not exist')
endif
if (allocated(bk0)) deallocate ( bk0 )
if (allocated(ak0)) deallocate ( ak0 )
! Finished, let's check the results !
call prt_maxmin('GZ_model', Atm(1)%phis, is, ie, js, je, ng, 1, 1.0/grav)
call prt_maxmin('PS_model', Atm(1)%ps, is, ie, js, je, ng, 1, 0.01_FVPRC)
call prt_maxmin('DP_model', Atm(1)%delp, is, ie, js, je, ng, npz, 1.0_FVPRC)
call prt_maxmin(' U_model', Atm(1)%u, is, ie, js, je, ng, npz, 1.0_FVPRC)
call prt_maxmin(' V_model', Atm(1)%v, is, ie, js, je, ng, npz, 1.0_FVPRC)
call prt_maxmin('PT_model', Atm(1)%pt, is, ie, js, je, ng, npz, 1.0_FVPRC)
! Range check the MOIST tracers
! Iterate over tracer names
iter = moist_tracers%begin()
do while (iter /= moist_tracers%end())
iptr => iter%value()
cptr => iter%key()
if (.not.match(cptr)) then
do k=1,npz
do j=js,je
do i=is,ie
Atm(1)%q(i,j,k,iptr) = MIN(Atm(1)%q(i,j,k,iptr),1.d0)
Atm(1)%q(i,j,k,iptr) = MAX(Atm(1)%q(i,j,k,iptr),0.d0)
enddo
enddo
enddo
endif
call iter%next()
enddo
do iq=1,Atm(1)%ncnst
call prt_maxmin('QP_model', Atm(1)%q(is:ie,js:je,1:npz,iq), is, ie, js, je, 0, npz, 1._FVPRC)
enddo
do i=1,size(tracer_bundles)
do j=1,size(tracer_bundles(i)%vars)
call tracer_bundles(i)%vars(j)%dealloc_var()
enddo
enddo
deallocate(tracer_bundles)
contains
function match(var_name) result(inList)
character(len=*) :: var_name
logical :: inList
integer :: i
character(len=10) :: exclude_vars(3)
exclude_vars(1)="Q"
exclude_vars(2)="NCPL"
exclude_vars(3)="NCPI"
inList = .false.
do i=1,size(exclude_vars)
if (trim(exclude_vars(i))==trim(var_name)) inList = .true.
enddo
end function match
end subroutine get_geos_cubed_ic
subroutine remap_coef( im, jm, lon, lat, id1, id2, jdc, s2c, agrid, bd )
type(fv_grid_bounds_type), intent(IN) :: bd
integer, intent(in):: im, jm
real(FVPRC), intent(in):: lon(im), lat(jm)
real(FVPRC), intent(out):: s2c(bd%is:bd%ie,bd%js:bd%je,4)
integer, intent(out), dimension(bd%is:bd%ie,bd%js:bd%je):: id1, id2, jdc
real(FVPRC), intent(in):: agrid(bd%isd:bd%ied,bd%jsd:bd%jed,2)
! local:
real(FVPRC) :: rdlon(im)
real(FVPRC) :: rdlat(jm)
real(FVPRC):: a1, b1
integer i,j, i1, i2, jc, i0, j0
integer :: is, ie, js, je
integer :: isd, ied, jsd, jed
is = bd%is
ie = bd%ie
js = bd%js
je = bd%je
isd = bd%isd
ied = bd%ied
jsd = bd%jsd
jed = bd%jed
do i=1,im-1
rdlon(i) = 1. / (lon(i+1) - lon(i))
enddo
rdlon(im) = 1. / (lon(1) + 2.*pi - lon(im))
do j=1,jm-1
rdlat(j) = 1. / (lat(j+1) - lat(j))
enddo
! * Interpolate to cubed sphere cell center
do 5000 j=js,je
do i=is,ie
if ( agrid(i,j,1)>lon(im) ) then
i1 = im; i2 = 1
a1 = (agrid(i,j,1)-lon(im)) * rdlon(im)
elseif ( agrid(i,j,1)<lon(1) ) then
i1 = im; i2 = 1
a1 = (agrid(i,j,1)+2.*pi-lon(im)) * rdlon(im)
else
do i0=1,im-1
if ( agrid(i,j,1)>=lon(i0) .and. agrid(i,j,1)<=lon(i0+1) ) then
i1 = i0; i2 = i0+1
a1 = (agrid(i,j,1)-lon(i1)) * rdlon(i0)
go to 111
endif
enddo
endif
111 continue
if ( agrid(i,j,2)<lat(1) ) then
jc = 1
b1 = 0.
elseif ( agrid(i,j,2)>lat(jm) ) then
jc = jm-1
b1 = 1.
else
do j0=1,jm-1
if ( agrid(i,j,2)>=lat(j0) .and. agrid(i,j,2)<=lat(j0+1) ) then
jc = j0
b1 = (agrid(i,j,2)-lat(jc)) * rdlat(jc)
go to 222
endif
enddo
endif
222 continue
if ( a1<0.0 .or. a1>1.0 .or. b1<0.0 .or. b1>1.0 ) then
write(*,*) 'gid=', mpp_pe(), i,j,a1, b1
endif
s2c(i,j,1) = (1.-a1) * (1.-b1)
s2c(i,j,2) = a1 * (1.-b1)
s2c(i,j,3) = a1 * b1
s2c(i,j,4) = (1.-a1) * b1
id1(i,j) = i1
id2(i,j) = i2
jdc(i,j) = jc
enddo !i-loop
5000 continue ! j-loop
end subroutine remap_coef
subroutine remap_winds(im, jm, km, npz, ak0, bk0, psc, ua, va, Atm)
type(fv_atmos_type), intent(inout) :: Atm(:)
integer, intent(in):: im, jm, km, npz
real(FVPRC), intent(in):: ak0(km+1), bk0(km+1)
real(FVPRC), intent(in):: psc(is:ie,js:je)
real(FVPRC), intent(in), dimension(is:ie,js:je,km):: ua, va
! local:
real(FVPRC), dimension(isd:ied,jsd:jed,npz):: ut, vt ! winds
real(FVPRC), dimension(is:ie, km+1):: pe0
real(FVPRC), dimension(is:ie,npz+1):: pe1
real(FVPRC), dimension(is:ie,npz):: qn1
integer i,j,k
ut = 0.0
vt = 0.0
do 5000 j=js,je
do k=1,km+1
do i=is,ie
pe0(i,k) = ak0(k) + bk0(k)*psc(i,j)
enddo
enddo
do k=1,npz+1
do i=is,ie
pe1(i,k) = Atm(1)%ak(k) + Atm(1)%bk(k)*Atm(1)%ps(i,j)
enddo
enddo
!------
! map u
!------
call mappm(km, pe0, ua(is:ie,j,1:km), npz, pe1, qn1, is,ie, -1, 4, Atm(1)%ptop)
do k=1,npz
do i=is,ie
ut(i,j,k) = qn1(i,k)
enddo
enddo
!------
! map v
!------
call mappm(km, pe0, va(is:ie,j,1:km), npz, pe1, qn1, is,ie, -1, 4, Atm(1)%ptop)
do k=1,npz
do i=is,ie
vt(i,j,k) = qn1(i,k)
enddo
enddo
5000 continue
call prt_maxmin('UT', ut, is, ie, js, je, ng, npz, 1.0_FVPRC)
call prt_maxmin('VT', vt, is, ie, js, je, ng, npz, 1.0_FVPRC)
!----------------------------------------------
! winds: lat-lon ON A to Cubed-D transformation:
!----------------------------------------------
call cubed_a2d(Atm(1)%npx, Atm(1)%npy, npz, ut, vt, Atm(1)%u, Atm(1)%v, Atm(1)%gridstruct, &
Atm(1)%domain, Atm(1)%bd )
if (is_master()) write(*,*) 'done remap_winds'
end subroutine remap_winds
subroutine remap_wz(im, jm, km, npz, mg, ak0, bk0, psc, wa, wz, Atm)
type(fv_atmos_type), intent(inout) :: Atm(:)
integer, intent(in):: im, jm, km, npz
integer, intent(in):: mg ! mg = 0 for delz; mg=3 for w
real(FVPRC), intent(in):: ak0(km+1), bk0(km+1)
real(FVPRC), intent(in):: psc(is:ie,js:je)
real(FVPRC), intent(in), dimension(is:ie,js:je,km):: wa
real(FVPRC), intent(out):: wz(is-mg:ie+mg,js-mg:je+mg,npz)
! local:
real(FVPRC), dimension(is:ie, km+1):: pe0
real(FVPRC), dimension(is:ie,npz+1):: pe1
real(FVPRC), dimension(is:ie,npz):: qn1
integer i,j,k
do 5000 j=js,je
do k=1,km+1
do i=is,ie
pe0(i,k) = ak0(k) + bk0(k)*psc(i,j)
enddo
enddo
do k=1,npz+1
do i=is,ie
pe1(i,k) = Atm(1)%ak(k) + Atm(1)%bk(k)*Atm(1)%ps(i,j)
enddo
enddo
!------
! map w
!------
call mappm(km, pe0, wa(is:ie,j,1:km), npz, pe1, qn1, is,ie, -1, 4, Atm(1)%ptop)
do k=1,npz
do i=is,ie
wz(i,j,k) = qn1(i,k)
enddo
enddo
5000 continue
! call prt_maxmin('WZ', wz, is, ie, js, je, mg, npz, 1._FVPRC, is_master())
! if (is_master()) write(*,*) 'done remap_wz'
end subroutine remap_wz
subroutine remap_xyz( im, jbeg, jend, jm, km, npz, nq, ncnst, lon, lat, ak0, bk0, ps0, gz0, &
ua, va, ta, qa, Atm )
type(fv_atmos_type), intent(inout), target :: Atm
integer, intent(in):: im, jm, km, npz, nq, ncnst
integer, intent(in):: jbeg, jend
real(FVPRC), intent(in):: lon(im), lat(jm), ak0(km+1), bk0(km+1)
real(FVPRC), intent(in):: gz0(im,jbeg:jend), ps0(im,jbeg:jend)
real(FVPRC), intent(in), dimension(im,jbeg:jend,km):: ua, va, ta
real(FVPRC), intent(in), dimension(im,jbeg:jend,km,ncnst):: qa
real(FVPRC), pointer, dimension(:,:,:) :: agrid
! local:
real(FVPRC), dimension(Atm%bd%isd:Atm%bd%ied,Atm%bd%jsd:Atm%bd%jed,npz):: ut, vt ! winds
real(FVPRC), dimension(Atm%bd%is:Atm%bd%ie,km):: up, vp, tp
real(FVPRC), dimension(Atm%bd%is:Atm%bd%ie,km+1):: pe0, pn0
real(FVPRC) pt0(km), gz(km+1), pk0(km+1)
real(FVPRC) qp(Atm%bd%is:Atm%bd%ie,km,ncnst)
real(FVPRC), dimension(Atm%bd%is:Atm%bd%ie,npz):: qn1
real(FVPRC), dimension(Atm%bd%is:Atm%bd%ie,npz+1):: pe1, pn1
real(FVPRC) :: rdlon(im)
real(FVPRC) :: rdlat(jm)
real(FVPRC):: a1, b1, c1, c2, c3, c4
real(FVPRC):: gzc, psc, pst
integer i,j,k, i1, i2, jc, i0, j0, iq
! integer sphum, liq_wat, ice_wat, cld_amt
integer sphum
integer :: is, ie, js, je
integer :: isd, ied, jsd, jed
is = Atm%bd%is
ie = Atm%bd%ie
js = Atm%bd%js
je = Atm%bd%je
isd = Atm%bd%isd
ied = Atm%bd%ied
jsd = Atm%bd%jsd
jed = Atm%bd%jed
!!NOTE: Only Atm is used in this routine.
agrid => Atm%gridstruct%agrid
sphum = get_tracer_index(MODEL_ATMOS, 'sphum')
! liq_wat = get_tracer_index(MODEL_ATMOS, 'liq_wat')
! ice_wat = get_tracer_index(MODEL_ATMOS, 'ice_wat')
! cld_amt = get_tracer_index(MODEL_ATMOS, 'cld_amt')
if ( sphum/=1 ) then
call mpp_error(FATAL,'SPHUM must be 1st tracer')
endif
pk0(1) = ak0(1)**kappa
do i=1,im-1
rdlon(i) = 1. / (lon(i+1) - lon(i))
enddo
rdlon(im) = 1. / (lon(1) + 2.*pi - lon(im))
do j=1,jm-1
rdlat(j) = 1. / (lat(j+1) - lat(j))
enddo
! * Interpolate to cubed sphere cell center
do 5000 j=js,je
do i=is,ie
pe0(i,1) = ak0(1)
pn0(i,1) = log(ak0(1))
enddo
do i=is,ie
if ( agrid(i,j,1)>lon(im) ) then
i1 = im; i2 = 1
a1 = (agrid(i,j,1)-lon(im)) * rdlon(im)
elseif ( agrid(i,j,1)<lon(1) ) then
i1 = im; i2 = 1
a1 = (agrid(i,j,1)+2.*pi-lon(im)) * rdlon(im)
else
do i0=1,im-1
if ( agrid(i,j,1)>=lon(i0) .and. agrid(i,j,1)<=lon(i0+1) ) then
i1 = i0; i2 = i0+1
a1 = (agrid(i,j,1)-lon(i1)) * rdlon(i0)
go to 111
endif
enddo
endif
111 continue
if ( agrid(i,j,2)<lat(1) ) then
jc = 1
b1 = 0.
elseif ( agrid(i,j,2)>lat(jm) ) then
jc = jm-1
b1 = 1.
else
do j0=1,jm-1
if ( agrid(i,j,2)>=lat(j0) .and. agrid(i,j,2)<=lat(j0+1) ) then
jc = j0
b1 = (agrid(i,j,2)-lat(jc)) * rdlat(jc)
go to 222
endif
enddo
endif
222 continue
#ifndef DEBUG_REMAP
if ( a1<0.0 .or. a1>1.0 .or. b1<0.0 .or. b1>1.0 ) then
write(*,*) i,j,a1, b1
endif
#endif
c1 = (1.-a1) * (1.-b1)
c2 = a1 * (1.-b1)
c3 = a1 * b1
c4 = (1.-a1) * b1
! Interpolated surface pressure
psc = c1*ps0(i1,jc ) + c2*ps0(i2,jc ) + &
c3*ps0(i2,jc+1) + c4*ps0(i1,jc+1)
! Interpolated surface geopotential
gzc = c1*gz0(i1,jc ) + c2*gz0(i2,jc ) + &
c3*gz0(i2,jc+1) + c4*gz0(i1,jc+1)
! 3D fields:
do iq=1,ncnst
! if ( iq==sphum .or. iq==liq_wat .or. iq==ice_wat .or. iq==cld_amt ) then
do k=1,km
qp(i,k,iq) = c1*qa(i1,jc, k,iq) + c2*qa(i2,jc, k,iq) + &
c3*qa(i2,jc+1,k,iq) + c4*qa(i1,jc+1,k,iq)
enddo
! endif
enddo
do k=1,km
up(i,k) = c1*ua(i1,jc, k) + c2*ua(i2,jc, k) + &
c3*ua(i2,jc+1,k) + c4*ua(i1,jc+1,k)
vp(i,k) = c1*va(i1,jc, k) + c2*va(i2,jc, k) + &
c3*va(i2,jc+1,k) + c4*va(i1,jc+1,k)
tp(i,k) = c1*ta(i1,jc, k) + c2*ta(i2,jc, k) + &
c3*ta(i2,jc+1,k) + c4*ta(i1,jc+1,k)
! Virtual effect:
tp(i,k) = tp(i,k)*(1.+zvir*qp(i,k,sphum))
enddo
! Tracers:
do k=2,km+1
pe0(i,k) = ak0(k) + bk0(k)*psc
pn0(i,k) = log(pe0(i,k))
pk0(k) = pe0(i,k)**kappa
enddo
#ifdef USE_DATA_ZS
Atm% ps(i,j) = psc
Atm%phis(i,j) = gzc
#else
! * Adjust interpolated ps to model terrain
gz(km+1) = gzc
do k=km,1,-1
gz(k) = gz(k+1) + rdgas*tp(i,k)*(pn0(i,k+1)-pn0(i,k))
enddo
! Only lowest layer potential temp is needed
pt0(km) = tp(i,km)/(pk0(km+1)-pk0(km))*(kappa*(pn0(i,km+1)-pn0(i,km)))
if( Atm%phis(i,j)>gzc ) then
do k=km,1,-1
if( Atm%phis(i,j) < gz(k) .and. &
Atm%phis(i,j) >= gz(k+1) ) then
pst = pk0(k) + (pk0(k+1)-pk0(k))*(gz(k)-Atm%phis(i,j))/(gz(k)-gz(k+1))
go to 123
endif
enddo
else
! Extrapolation into the ground
pst = pk0(km+1) + (gzc-Atm%phis(i,j))/(cp_air*pt0(km))
endif
123 Atm%ps(i,j) = pst**(1./kappa)
#endif
enddo !i-loop
! * Compute delp from ps