-
Notifications
You must be signed in to change notification settings - Fork 14
/
model_grid.F90
2509 lines (2089 loc) · 106 KB
/
model_grid.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!> @file
!! @brief Specify input and target model grids.
!! @author Larissa Reames CIWRO/NOAA/NSSL/FRDD
!> Sets up the ESMF grid objects for the input data grid and target grid.
!!
!! @author Larissa Reames CIWRO/NOAA/NSSL/FRDD
module model_grid
use esmf
use ESMF_LogPublicMod
use utils_mod
use constants_module
use misc_definitions_module
use map_utils_mod
use program_setup
implicit none
private
integer, public :: nCells_input
!< cells on input grid
integer, public :: nVert_input
!< vertices on input grid
integer, public :: nz_input
!< number of input grid atm layers
integer, public :: nzp1_input
!< number of input grid atm layer interfaces
integer, public :: nsoil_input
!< number of input soil levels
!real, public :: dx
! !< grid size (m) of target grid
character(50), public :: start_time
!< simulation start time input data
character(20), public,allocatable :: valid_time(:,:)
!< valid forecast time input data
integer, public :: strlen
!< StrLen on input file
real, public :: config_dt
!< timestep (seconds) of input data
integer, public :: lsm_scheme
!< land surface scheme input data
integer, public :: mp_scheme
!< microphysics scheme input data
integer, public :: conv_scheme
!< convection scheme input data
!real, public :: cen_lat
! !< target grid projection center latitude
!real, public :: cen_lon
! !< target grid projection center longitude
!real, public :: moad_cen_lat
! !< target grid moad center latitude
!real, public :: pole_lat
! !< target grid projection pole latitude
!real, public :: pole_lon
! !< target grid projection pole longitude
integer, public :: ip1_target
!< ip1_target plus 1
integer, public :: jp1_target
!< jp1_target plus 1
integer, allocatable, public :: elemIDs(:)
!< IDs of the elements on present PET
integer, allocatable, public :: nodeIDs(:)
!< IDs of the nodes on present PET
integer, public :: nCellsPerPET
!< Number of cells on this PET
integer, public :: nNodesPerPET
!< Number of nodes on this PET
type(esmf_mesh), public :: input_grid
!< input grid esmf grid object
type(esmf_grid), public :: target_grid
!< target grid esmf grid object.
type(esmf_field), public :: cell_latitude_input_grid
!< latitude of grid center, input grid
type(esmf_field), public :: cell_longitude_input_grid
!< longitude of grid center, input grid
type(esmf_field), public :: node_latitude_input_grid
!< latitude of grid center, input grid
type(esmf_field), public :: node_longitude_input_grid
!< longitude of grid center, input grid
type(esmf_field), public :: zgrid_input_grid
!< esmf field to hold level height on input grid
type(esmf_field), public :: zgrid_target_grid
!< esmf field to hold level height on target grid
type(esmf_field), public :: u_input_grid
!< esmf field to hold u wind on the input grid
type(esmf_field), public :: u_target_grid
!< esnf field to hold u wind on the target grid
type(esmf_field), public :: v_input_grid
!< esmf field to hold v wind on the input grid
type(esmf_field), public :: v_target_grid
!< esmf field to hold v wind on the target grid
type(esmf_field), public :: u_target_grid_nostag
!< esmf field to hold v wind on the target grid mass points
type(esmf_field), public :: v_target_grid_nostag
!< esmf field to hold v wind on the target grid mass points
type(esmf_field), public :: latitude_target_grid
!< latitude of grid center, target grid
type(esmf_field), public :: longitude_target_grid
!< longitude of grid center, target grid
type(esmf_field), public :: latitude_u_target_grid
!< latitude of grid u stagger, target
!grid
type(esmf_field), public :: longitude_u_target_grid
!< longitude of grid u stagger, target
!grid
type(esmf_field), public :: latitude_v_target_grid
!< latitude of grid v stagger, target
!grid
type(esmf_field), public :: longitude_v_target_grid
!< longitude of grid v stagger, target
!grid
real(esmf_kind_r8), allocatable , public :: zs_target_grid(:,:)
!< soil center depth, target grid
type(esmf_field), public :: hgt_input_grid, hgt_target_grid
!< surface elevation, target grid
type(esmf_field), public :: mapfac_m_target_grid
!< target grid map factor at grid
! center
type(esmf_field), public :: mapfac_u_target_grid
!< target grid map factor at u stagger
! points
type(esmf_field), public :: mapfac_v_target_grid
!< target grid map factor at v stagger
! points
type(esmf_field), public :: sina_target_grid
!< Sine of grid rotation angle
! valid only for grid_type="lambert"
type(esmf_field), public :: cosa_target_grid
!< Cosine of grid rotation angle
! valid only for grid_type="lambert"
type(esmf_field), public :: sina_u_target_grid
!< Sine of grid rotation angle for u winds
! valid only for grid_type="lambert"
type(esmf_field), public :: cosa_u_target_grid
!< Cosine of grid rotation angle for u wind
! valid only for grid_type="lambert"
type(esmf_field), public :: sina_v_target_grid
!< Sine of grid rotation angle for v wind
! valid only for grid_type="lambert"
type(esmf_field), public :: cosa_v_target_grid
!< Cosine of grid rotation angle for v wind
! valid only for grid_type="lambert"
integer, public :: n_diag_fields
!< number of fields read from the diag file
type(esmf_fieldbundle), public :: input_diag_bundle
!< bundle to hold input diag fields
type(esmf_fieldbundle), public :: target_diag_bundle
!< bundle to hold target diag fields
integer, public :: n_hist_fields_2d_patch
!< number of 2d fields read from the hist file
!< to use with patch regridding
integer, public :: n_hist_fields_2d_cons
!< number of 2d fields read from the hist file
!< to use with conservative regridding
integer, public :: n_hist_fields_2d_nstd
!< number of 2d fields read from the hist file
!< to use with nearest source to destination
!< regridding
integer, public :: n_hist_fields_3d_vert
!< number of 3d fields read from the hist file
!< to be defined on input grid vertices and
!< regridded with bilinear interpolation to
!< target grid cells
integer, public :: n_hist_fields_3d_nz
!< number of 3d fields read from the hist file
!< with vertical dimension nVertLevels
integer, public :: n_hist_fields_3d_nzp1
!< number of 3d fields read from the hist file
!< with vertical dimension nVertLevelsp1
integer, public :: n_hist_fields_soil
!< number of soil fields read from the hist file
integer, public :: diag_out_interval
!< output_interval from diag file
integer, public :: do_u_interp
!< whether 3d u is requested for interpolation
integer, public :: do_v_interp
!< whether 3d v is requested for interpolation
integer, public :: do_u10_interp
!< whether 10-m u is requested for interpolation
integer, public :: do_v10_interp
!< whether 10-m v is requested for interpolation
integer, public :: u10_ind
!< index of u10 in target_diag_bundle
integer, public :: v10_ind
!< index of v10 in target_diag_bundle
character(50), allocatable, public :: target_diag_names(:), &
target_hist_names_2d_cons(:), &
target_hist_names_2d_nstd(:), &
target_hist_names_2d_patch(:), &
target_hist_names_3d_nzp1(:), &
target_hist_names_3d_nz(:), &
target_hist_names_3d_vert(:), &
target_hist_names_soil(:)
!< Arrays to hold target field names
character(50), allocatable, public :: target_diag_units(:), &
target_hist_units_2d_cons(:), &
target_hist_units_2d_nstd(:), &
target_hist_units_2d_patch(:), &
target_hist_units_3d_nzp1(:), &
target_hist_units_3d_nz(:), &
target_hist_units_3d_vert(:), &
target_hist_units_soil(:)
!< Arrays to hold target field units
character(200), allocatable, public :: target_diag_longname(:), &
target_hist_longname_2d_cons(:), &
target_hist_longname_2d_nstd(:), &
target_hist_longname_2d_patch(:), &
target_hist_longname_3d_nzp1(:), &
target_hist_longname_3d_nz(:), &
target_hist_longname_3d_vert(:), &
target_hist_longname_soil(:)
!< Arrays to hold target field longname
type(esmf_fieldbundle), public :: input_hist_bundle_2d_patch, &
input_hist_bundle_2d_cons, &
input_hist_bundle_2d_nstd, &
input_hist_bundle_3d_nz, &
input_hist_bundle_3d_nzp1, &
input_hist_bundle_3d_vert, &
input_hist_bundle_soil
!< bundles to hold input hist fields
type(esmf_fieldbundle), public :: target_hist_bundle_2d_patch, &
target_hist_bundle_2d_cons, &
target_hist_bundle_2d_nstd, &
target_hist_bundle_3d_nz, &
target_hist_bundle_3d_nzp1, &
target_hist_bundle_3d_vert, &
target_hist_bundle_soil
!< bundles to hold target hist fields
public :: define_target_grid
public :: define_input_grid
public :: cleanup_input_target_grid_data
contains
!> Define input grid object for MPAS input data.
!!
!! @param [in] localpet ESMF local persistent execution thread
!! @param [in] npets Number of persistent execution threads
!! @author Larissa Reames CIWRO/NOAA/NSSL/FRDD
subroutine define_input_grid(localpet,npets)
use mpi
use netcdf
use program_setup, only : grid_file_input_grid
implicit none
character(len=500) :: the_file, dimname
integer, intent(in) :: localpet, npets
integer :: error, i, j, k, rc, n, lmi(1), lma(1)
integer :: NX, NY, format
integer :: ncid,id_var, id_dim, dimsize, nVertThis
integer :: nCells, nVertices, maxEdges, dimids(2)
integer :: cell_start, cell_end, dims(3), temp1, &
temp2, temp3, temp(1), clb(1), cub(1)
integer, allocatable :: elemTypes2(:), vertOnCell(:,:), &
nodesPET(:), nodeIDs_temp(:), &
elementConn_temp(:), elementConn(:), &
unique_nodes(:),node_pets(:),myCells(:)
real(esmf_kind_r8), allocatable :: latCell(:), lonCell(:), &
latVert(:), lonVert(:), &
nodeCoords(:), &
nodeCoords_temp(:), &
elemCoords(:), hgt(:)
real(esmf_kind_r8), pointer :: data_1d(:), data_1d2(:)
real(esmf_kind_r8), parameter :: PI=4.D0*DATAN(1.D0)
integer :: myCells_num
the_file = grid_file_input_grid
if (localpet==0) print*,'- OPEN MPAS INPUT FILE: ',trim(the_file)
!error=nf90_open_par(trim(the_file),NF90_NOWRITE,MPI_COMM_WORLD, MPI_INFO_NULL, ncid)
error=nf90_open(trim(the_file),nf90_nowrite,ncid) ! CSS
if (error /=0) call error_handler("OPENING MPAS INPUT FILE",error)
!Get nCells size
if (localpet==0) print*,'- READ nCells'
error = nf90_inq_dimid(ncid,'nCells', id_dim)
call netcdf_err(error, 'reading nCells id')
error=nf90_inquire_dimension(ncid,id_dim,len=nCells)
call netcdf_err(error, 'reading nCells')
nCells_input = nCells
!Get nVertices size
if (localpet==0) print*,'- READ nVertices'
error = nf90_inq_dimid(ncid,'nVertices',id_dim)
call netcdf_err(error, 'reading nVertices id')
error=nf90_inquire_dimension(ncid,id_dim,len=nVertices)
call netcdf_err(error, 'reading nVertices')
nVert_input = nVertices
!Get nVertLevels size
if (localpet==0) print*,'- READ nVertLevels'
error = nf90_inq_dimid(ncid,'nVertLevels',id_dim)
call netcdf_err(error, 'reading nVertLevels id')
error=nf90_inquire_dimension(ncid,id_dim,len=nz_input)
call netcdf_err(error, 'reading nVertLevels')
!Get nVertLevelsP1 size
if (localpet==0) print*,'- READ nVertLevelsP1'
error = nf90_inq_dimid(ncid,'nVertLevelsP1',id_dim)
call netcdf_err(error, 'reading nVertLevelsP1 id')
error=nf90_inquire_dimension(ncid,id_dim,len=nzp1_input)
call netcdf_err(error, 'reading nVertLevelsP1')
!Get maxEdges size
if (localpet==0) print*,'- READ maxEdges'
error = nf90_inq_dimid(ncid,'maxEdges',id_dim)
call netcdf_err(error, 'reading maxEdges id')
error=nf90_inquire_dimension(ncid,id_dim,len=maxEdges)
call netcdf_err(error, 'reading maxEdges')
!Get nSoilLevels size
if (localpet==0) print*,'- READ nSoilLevels'
error = nf90_inq_dimid(ncid,'nSoilLevels',id_dim)
call netcdf_err(error, 'reading nSoilLevels id')
error=nf90_inquire_dimension(ncid,id_dim,len=nsoil_input)
call netcdf_err(error, 'reading nSoilLevels')
allocate(latCell(nCells))
allocate(lonCell(nCells))
allocate(hgt(nCells))
allocate(latVert(nVertices))
allocate(lonVert(nVertices))
allocate(vertOnCell(maxEdges,nCells))
allocate(zs_target_grid(nsoil_input,1))
! GET CELL CENTER LAT/LON
if (localpet==0) print*,'- READ LONCELL ID'
error=nf90_inq_varid(ncid, 'lonCell', id_var)
call netcdf_err(error, 'reading lonCell id')
if (localpet==0) print*,'- READ LONCELL'
error=nf90_get_var(ncid, id_var, start=(/1/), count=(/nCells/),values=lonCell)
call netcdf_err(error, 'reading lonCell')
if (localpet==0) print*,'- READ LATCELL ID'
error=nf90_inq_varid(ncid, 'latCell', id_var)
call netcdf_err(error, 'reading latCell id')
if (localpet==0) print*,'- READ LATCELL'
error=nf90_get_var(ncid, id_var, start=(/1/), count=(/nCells/),values=latCell)
call netcdf_err(error, 'reading latCell')
! GET VERTEX LAT/LON
if (localpet==0) print*,'- READ LONVERTEX ID'
error=nf90_inq_varid(ncid, 'lonVertex', id_var)
call netcdf_err(error, 'reading lonVertex id')
if (localpet==0) print*,'- READ LONVERTEX'
error=nf90_get_var(ncid, id_var, start=(/1/),count=(/nVertices/),values=lonVert)
call netcdf_err(error, 'reading lonVertex')
if (localpet==0) print*,'- READ LATVERTEX ID'
error=nf90_inq_varid(ncid, 'latVertex', id_var)
call netcdf_err(error, 'reading latVertex id')
if (localpet==0) print*,'- READ LATVERTEX'
error=nf90_get_var(ncid, id_var, start=(/1/),count=(/nVertices/),values=latVert)
call netcdf_err(error, 'reading latVertex')
! SOIL CENTER DEPTHS
if (localpet==0) print*,'- READ ZS ID'
error=nf90_inq_varid(ncid, 'zs', id_var)
call netcdf_err(error, 'reading zs id')
if (localpet==0) print*,'- READ ZS'
error=nf90_get_var(ncid, id_var, start=(/1,1/),count=(/nsoil_input,1/),values=zs_target_grid)
call netcdf_err(error, 'reading ZS')
if (localpet==0) print*,'- READ HGT'
error=nf90_inq_varid(ncid, 'ter', id_var)
call netcdf_err(error, 'reading ter id')
error=nf90_get_var(ncid, id_var, start=(/1/),count=(/nCells/), values=hgt)
call netcdf_err(error, 'reading ter')
if (localpet==0) print*,"- NUMBER OF CELLS ON INPUT GRID ", nCells_input
if (localpet==0) print*,"- NUMBER OF NODES ON INPUT GRID ", nVert_input
!-----------------------------------------------------------------------
! Create ESMF grid object for the model grid.
!-----------------------------------------------------------------------
if (localpet==0) print*,'- READ verticesOnCell ID'
error=nf90_inq_varid(ncid, 'verticesOnCell', id_var)
call netcdf_err(error, 'reading verticesOnCell id')
error = nf90_inquire_variable(ncid,id_var,dimids=dims)
call netcdf_err(error, 'reading verticesOnCell dims')
if (localpet==0) print*,'- READ verticesOnCell'
error=nf90_get_var(ncid, id_var, start=(/1,1/),count=(/maxEdges,nCells/),values=vertOnCell)
call netcdf_err(error, 'reading verticesOnCell')
error = nf90_close(ncid)
nVertThis = 0
k = 0
if (block_decomp_file=='NULL') then
!nCellsPerPET = ceiling(real(nCells)/real(npets))
!cell_start = localpet*nCellsPerPET+1
!cell_end = min(localpet*nCellsPerPET+nCellsPerPET,nCells)
call para_range(1, nCells, npets, localpet, cell_start, cell_end)
nCellsPerPET = cell_end - cell_start + 1
allocate(elemIDs(nCellsPerPET))
!$OMP PARALLEL DO $PRIVATE(i,j)
do i = cell_start, cell_end
j = i - cell_start + 1
elemIDs(j) = i
enddo
!$OMP END PARALLEL DO
else
call read_block_decomp_file(localpet,npets,block_decomp_file,nCells,elemIDs,nCellsPerPET)
endif
allocate(elementConn_temp(maxEdges*nCellsPerPET))
allocate(elemCoords(2*nCellsPerPET))
allocate(elemTypes2(nCellsPerPET))
if (localpet==0) print*, "- Create PET-local element connectivity "
!$OMP PARALLEL DO $PRIVATE(i,j,n)
do i = 1,nCellsPerPET
j = elemIDs(i)
elemTypes2(i) = count(vertOnCell(:,j)/=0)
nVertThis = nVertThis + elemTypes2(i)
elemCoords(2*i-1) = lonCell(j)*180.0_esmf_kind_r8/PI
if (elemCoords(2*i-1) > 180.0_esmf_kind_r8) then
elemCoords(2*i-1) = elemCoords(2*i-1) - 360.0_esmf_kind_r8
endif
elemCoords(i*2) = latCell(j)*180.0_esmf_kind_r8/PI
elementConn_temp(maxEdges*(i-1)+1:maxEdges*i) = vertOnCell(:,j)
enddo
!$OMP END PARALLEL DO
call unique_sort(elementConn_temp,maxEdges*nCellsPerPET,nodeIDs)
nNodesPerPET=size(nodeIDs)
allocate(nodeCoords(2*nNodesPerPET))
!$OMP PARALLEL DO
do j = 1,nNodesPerPET
i = nodeIDs(j)
nodeCoords(2*j-1) = lonVert(i)*180.0_esmf_kind_r8/PI
if (nodeCoords(2*j-1) > 180.0_esmf_kind_r8) then
nodeCoords(2*j-1) = nodeCoords(2*j-1) - 360.0_esmf_kind_r8
endif
nodeCoords(2*j) = latVert(i)*180.0_esmf_kind_r8/PI
enddo
!$OMP END PARALLEL DO
allocate(elementConn(nVertThis))
nVertThis = 0
!$OMP PARALLEL DO
do i = 1,nCellsPerPET
k = elemIDs(i)
do j = 1,maxEdges
! if(vertOnCell(j,i)/=0) then
! temp = FINDLOC(nodeIDs,vertOnCell(j,elemIDs(i)))
if(vertOnCell(j,k)/=0) then
temp = FINDLOC(nodeIDs,vertOnCell(j,k))
elementConn(nVertThis+1) = temp(1)
nVertThis = nVertThis + 1
endif
enddo
enddo
!$OMP END PARALLEL DO
if (localpet==0) print*, "- CREATE MESH -"
input_grid = ESMF_MeshCreate(parametricDim=2, &
spatialDim=2, &
nodeIDs= nodeIDs, &
nodeCoords = nodeCoords, &
elementIDs = elemIDs, &
elementTypes=elemTypes2, &
elementConn = elementConn, &
elementCoords=elemCoords, &
coordSys=ESMF_COORDSYS_SPH_DEG, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN MeshCreate", rc)
! After the creation we are through with the arrays, so they may be deallocated.
deallocate(elemCoords,elemTypes2)
deallocate(nodeCoords)
deallocate(elementConn_temp, elementConn)
!deallocate(nodeIDs_temp)
!-----------------------------------------------------------------------
! Create lat/lon arrays on input element grid
!-----------------------------------------------------------------------
if (localpet==0) print*,"- CALL FieldCreate FOR INPUT GRID CELL LATITUDE."
cell_latitude_input_grid = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_ELEMENT, &
name="input_grid_cell_latitude", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldCreate", error)
if (localpet==0) print*,"- CALL FieldCreate FOR INPUT GRID CELL LONGITUDE."
cell_longitude_input_grid = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_ELEMENT, &
name="input_grid_cell_longitude", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldCreate", error)
call ESMF_FieldGet(cell_latitude_input_grid, farrayPtr=data_1d,rc = rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", error)
call ESMF_FieldGet(cell_longitude_input_grid,farrayPtr=data_1d2, &
computationalLBound=clb, computationalUBound=cub, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldGet", error)
do j = 1, nCellsPerPET
i = elemIDs(j)
data_1d(j) = latCell(i)
data_1d2(j) = lonCell(i)
enddo
nullify(data_1d,data_1d2)
!-----------------------------------------------------------------------
! Create lat/lon arrays on input node grid
!-----------------------------------------------------------------------
if (localpet==0) print*,"- CALL FieldCreate FOR INPUT GRID VERTEX LATITUDE."
node_latitude_input_grid = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_NODE, &
name="input_grid_node_latitude", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldCreate", error)
if (localpet==0) print*,"- CALL FieldCreate FOR INPUT GRID VERTEX LONGITUDE."
node_longitude_input_grid = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_NODE, &
name="input_grid_node_longitude", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldCreate", error)
call ESMF_FieldGet(node_latitude_input_grid, farrayPtr=data_1d,rc = rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGet", error)
call ESMF_FieldGet(node_longitude_input_grid,farrayPtr=data_1d2, &
computationalLBound=clb, computationalUBound=cub, rc=rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGet", error)
!call unique_sort(nodeIDs,k,unique_nodes)
call ESMF_MeshGet(input_grid,numOwnedNodes=n)
allocate(unique_nodes(n))
allocate(node_pets(nNodesPerPET))
call ESMF_MeshGet(input_grid,nodeOwners=node_pets)
j = 1
do i = 1,nNodesPerPET
if (node_pets(i)==localpet) then
unique_nodes(j) = nodeIDs(i)
j = j + 1
endif
enddo
do j = 1,n
i = unique_nodes(j)
data_1d(j) = latVert(i)
data_1d2(j) = lonVert(i)
enddo
nNodesPerPET = n
nullify(data_1d,data_1d2)
if (localpet==0) print*,"- CALL FieldCreate FOR INPUT GRID HGT."
hgt_input_grid = ESMF_FieldCreate(input_grid, &
typekind=ESMF_TYPEKIND_R8, &
meshloc=ESMF_MESHLOC_ELEMENT, &
name="input_grid_hgt", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldCreate", error)
call ESMF_FieldGet(hgt_input_grid, farrayPtr=data_1d,rc = rc)
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGet", error)
do j = 1,nCellsPerPET
i = elemIDs(j)
data_1d(j) = hgt(i)
enddo
nullify(data_1d)
deallocate(lonCell, latCell, lonVert, latVert,vertOnCell)
end subroutine define_input_grid
!> Setup the esmf grid object for the target grid.
!!
!! @param [in] localpet ESMF local persistent execution thread
!! @param [in] npets Number of persistent execution threads
!! @author Larissa Reames CIWRO/NOAA/NSSL/FRDD
subroutine define_target_grid(localpet, npets)
use program_setup, only : target_grid_type
implicit none
integer, intent(in) :: localpet, npets
if(trim(target_grid_type) == 'file') then
call define_target_grid_file(localpet,npets)
else
call define_target_grid_params(localpet,npets)
endif
end subroutine define_target_grid
subroutine define_target_grid_params(localpet,npets)
use netcdf
use llxy_module
implicit none
integer, intent(in) :: localpet, npets
integer :: error, extra, i, j, clb(2), cub(2)
real(esmf_kind_r8), allocatable :: latitude_one(:,:), longitude_one(:,:), &
latitude_corner_one(:,:), &
longitude_corner_one(:,:), &
longitude_u_one(:,:), latitude_u_one(:,:), &
longitude_v_one(:,:), latitude_v_one(:,:), &
mapfac_m_one(:,:), mapfac_u_one(:,:), &
mapfac_v_one(:,:)
integer :: ncid,id_var, id_dim
real(esmf_kind_r8), pointer :: lat_src_ptr(:,:), lon_src_ptr(:,:),&
mapptr(:,:), cosa_ptr(:,:), sina_ptr(:,:)
type(esmf_polekind_flag) :: polekindflag(2)
ip1_target = i_target + 1
jp1_target = j_target + 1
!----------------------------------------------------------------------
! Fill proj object for target grid projection
!----------------------------------------------------------------------
call push_source_projection(proj_code, stand_lon, truelat1, truelat2, &
dxkm, dykm, dlatdeg, dlondeg, known_x, known_y, &
known_lat, known_lon)
!-----------------------------------------------------------------------
! Create ESMF grid object for the model grid.
!-----------------------------------------------------------------------
if (.not. is_regional ) then
polekindflag(1:2) = ESMF_POLEKIND_MONOPOLE
print*,"- CALL GridCreate1PeriDim FOR INPUT GRID."
target_grid = ESMF_GridCreate1PeriDim(minIndex=(/1,1/), &
maxIndex=(/i_target,j_target/), &
polekindflag=polekindflag, &
periodicDim=1, &
poleDim=2, &
coordSys=ESMF_COORDSYS_SPH_DEG, &
regDecomp=(/1,npets/), &
indexflag=ESMF_INDEX_GLOBAL, rc=error)
if(ESMF_logFoundError(rcToCheck=error, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__)) &
call error_handler("IN GridCreate1PeriDim", error)
else
if (localpet==0) print*,"- CALL GridCreateNoPeriDim FOR TARGET MODEL GRID"
target_grid = ESMF_GridCreateNoPeriDim(maxIndex=(/i_target,j_target/), &
indexflag=ESMF_INDEX_GLOBAL, &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN GridCreateNoPeriDim", error)
endif
if (localpet==0) print*,"- CALL GridAddCoord FOR INPUT GRID CENTER."
call ESMF_GridAddCoord(target_grid, &
staggerloc=ESMF_STAGGERLOC_CENTER, rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN GridAddCoord", error)
if (localpet==0) print*,"- CALL GridAddCoord FOR INPUT GRID CORNER."
call ESMF_GridAddCoord(target_grid, &
staggerloc=ESMF_STAGGERLOC_CORNER, rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN GridAddCoord", error)
if (localpet==0) print*,"- CALL GridAddCoord FOR INPUT GRID EDGE1."
call ESMF_GridAddCoord(target_grid, &
staggerloc=ESMF_STAGGERLOC_EDGE1, rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN GridAddCoord", error)
if (localpet==0) print*,"- CALL GridAddCoord FOR INPUT GRID EDGE2."
call ESMF_GridAddCoord(target_grid, &
staggerloc=ESMF_STAGGERLOC_EDGE2, rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN GridAddCoord", error)
!-----------------------------------------------------------------------
! Generate lat/lon array values for various staggers
!-----------------------------------------------------------------------
!Grid centers
call ESMF_GridGetCoord(target_grid, &
staggerLoc=ESMF_STAGGERLOC_CENTER, &
coordDim=2, &
computationalLBound=clb, &
computationalUBound=cub, &
farrayPtr=lat_src_ptr, &
rc=error)
allocate(latitude_one(clb(1):cub(1),clb(2):cub(2)))
allocate(longitude_one(clb(1):cub(1),clb(2):cub(2)))
allocate(mapfac_m_one(clb(1):cub(1),clb(2):cub(2)))
call get_lat_lon_fields(latitude_one, longitude_one, clb(1),clb(2),cub(1),cub(2),M)
call get_map_factor(latitude_one, longitude_one, mapfac_m_one, mapfac_m_one, clb(1),clb(2),cub(1),cub(2))
!y-dir stagger (V)
call ESMF_GridGetCoord(target_grid, &
staggerLoc=ESMF_STAGGERLOC_EDGE2, &
coordDim=2, &
computationalLBound=clb, &
computationalUBound=cub, &
farrayPtr=lat_src_ptr, &
rc=error)
allocate(latitude_v_one(clb(1):cub(1),clb(2):cub(2)))
allocate(longitude_v_one(clb(1):cub(1),clb(2):cub(2)))
allocate(mapfac_v_one(clb(1):cub(1),clb(2):cub(2)))
call get_lat_lon_fields(latitude_v_one, longitude_v_one, clb(1),clb(2),cub(1),cub(2), V)
call get_map_factor(latitude_v_one, longitude_v_one, mapfac_v_one, mapfac_v_one, clb(1),clb(2),cub(1),cub(2))
!x-dir stagger (U)
call ESMF_GridGetCoord(target_grid, &
staggerLoc=ESMF_STAGGERLOC_EDGE1, &
coordDim=2, &
computationalLBound=clb, &
computationalUBound=cub, &
farrayPtr=lat_src_ptr, &
rc=error)
allocate(latitude_u_one(clb(1):cub(1),clb(2):cub(2)))
allocate(longitude_u_one(clb(1):cub(1),clb(2):cub(2)))
allocate(mapfac_u_one(clb(1):cub(1),clb(2):cub(2)))
call get_lat_lon_fields(latitude_u_one, longitude_u_one, clb(1),clb(2),cub(1),cub(2), U)
call get_map_factor(latitude_u_one, longitude_u_one, mapfac_u_one, mapfac_u_one, clb(1),clb(2),cub(1),cub(2))
!corner
call ESMF_GridGetCoord(target_grid, &
staggerLoc=ESMF_STAGGERLOC_CORNER, &
coordDim=2, &
computationalLBound=clb, &
computationalUBound=cub, &
farrayPtr=lat_src_ptr, &
rc=error)
allocate(latitude_corner_one(clb(1):cub(1),clb(2):cub(2)))
allocate(longitude_corner_one(clb(1):cub(1),clb(2):cub(2)))
call get_lat_lon_fields(latitude_corner_one, longitude_corner_one, clb(1),clb(2),cub(1),cub(2),CORNER)
!print*, minval(latitude_corner_one), maxval(latitude_corner_one), minval(longitude_corner_one), maxval(longitude_corner_one)
!-----------------------------------------------------------------------
! Read the mask and lat/lons.
!-----------------------------------------------------------------------
! Grid Centers
if (localpet==0) print*,"- CALL FieldCreate FOR TARGET GRID LATITUDE."
latitude_target_grid = ESMF_FieldCreate(target_grid, &
typekind=ESMF_TYPEKIND_R8, &
staggerloc=ESMF_STAGGERLOC_CENTER, &
name="target_grid_latitude", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN FieldCreate", error)
if (localpet==0) print*,"- CALL FieldCreate FOR TARGET GRID LONGITUDE."
longitude_target_grid = ESMF_FieldCreate(target_grid, &
typekind=ESMF_TYPEKIND_R8, &
staggerloc=ESMF_STAGGERLOC_CENTER, &
name="target_grid_longitude", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldCreate", error)
if (localpet==0) print*, "- CALL FieldGet FOR TARGET GRID LATITUDE."
call ESMF_FieldGet(latitude_target_grid, farrayPtr=lat_src_ptr,rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGET", error)
if (localpet==0) print*, "- CALL FieldGet FOR TARGET GRID LONGITUDE."
call ESMF_FieldGet(longitude_target_grid, farrayPtr=lon_src_ptr, &
computationalLBound=clb, computationalUBound=cub, &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGet", error)
do j = clb(2),cub(2)
do i = clb(1),cub(1)
lat_src_ptr(i,j) = latitude_one(i,j)
lon_src_ptr(i,j) = longitude_one(i,j)
enddo
enddo
nullify(lat_src_ptr, lon_src_ptr)
! E-W Stagger
if (localpet==0) print*,"- CALL FieldCreate FOR TARGET GRID LATITUDE."
latitude_u_target_grid = ESMF_FieldCreate(target_grid, &
typekind=ESMF_TYPEKIND_R8, &
staggerloc=ESMF_STAGGERLOC_EDGE1, &
name="target_grid_latitude", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldCreate", error)
if (localpet==0) print*,"- CALL FieldCreate FOR TARGET GRID LONGITUDE."
longitude_u_target_grid = ESMF_FieldCreate(target_grid, &
typekind=ESMF_TYPEKIND_R8, &
staggerloc=ESMF_STAGGERLOC_EDGE1, &
name="target_grid_longitude", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldCreate", error)
if (localpet==0) print*, "- CALL FieldGet FOR TARGET GRID LATITUDE."
call ESMF_FieldGet(latitude_u_target_grid, farrayPtr=lat_src_ptr,rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGET", error)
if (localpet==0) print*, "- CALL FieldGet FOR TARGET GRID LONGITUDE."
call ESMF_FieldGet(longitude_u_target_grid, farrayPtr=lon_src_ptr, &
computationalLBound=clb, computationalUBound=cub, &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGet", error)
do j = clb(2),cub(2)
do i = clb(1),cub(1)
lat_src_ptr(i,j) = latitude_u_one(i,j)
lon_src_ptr(i,j) = longitude_u_one(i,j)
enddo
enddo
nullify(lat_src_ptr, lon_src_ptr)
! N-S Stagger
if (localpet==0) print*,"- CALL FieldCreate FOR TARGET GRID LATITUDE."
latitude_v_target_grid = ESMF_FieldCreate(target_grid, &
typekind=ESMF_TYPEKIND_R8, &
staggerloc=ESMF_STAGGERLOC_EDGE2, &
name="target_grid_latitude", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldCreate", error)
if (localpet==0) print*,"- CALL FieldCreate FOR TARGET GRID LONGITUDE."
longitude_v_target_grid = ESMF_FieldCreate(target_grid, &
typekind=ESMF_TYPEKIND_R8, &
staggerloc=ESMF_STAGGERLOC_EDGE2, &
name="target_grid_longitude", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldCreate", error)
if (localpet==0) print*, "- CALL FieldGet FOR TARGET GRID LATITUDE."
call ESMF_FieldGet(latitude_v_target_grid, farrayPtr=lat_src_ptr,rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGET", error)
if (localpet==0) print*, "- CALL FieldGet FOR TARGET GRID LONGITUDE."
call ESMF_FieldGet(longitude_v_target_grid, farrayPtr=lon_src_ptr, &
computationalLBound=clb, computationalUBound=cub, &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldGet", error)
!print*, localpet, cub(1), cub(2)
do j = clb(2),cub(2)
do i = clb(1),cub(1)
lat_src_ptr(i,j) = latitude_v_one(i,j)
lon_src_ptr(i,j) = longitude_v_one(i,j)
enddo
enddo
nullify(lat_src_ptr, lon_src_ptr)
if (localpet==0) print*,"- CALL FieldCreate FOR TARGET GRID HGT."
hgt_target_grid = ESMF_FieldCreate(target_grid, &
typekind=ESMF_TYPEKIND_R8, &
staggerloc=ESMF_STAGGERLOC_CENTER, &
name="target_grid_hgt", &
rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
call error_handler("IN FieldCreate", error)
!Create and fill coordinates
if (localpet==0) print*,"- CALL GridGetCoord FOR INPUT GRID X-COORD."
nullify(lon_src_ptr)
call ESMF_GridGetCoord(target_grid, &
staggerLoc=ESMF_STAGGERLOC_CENTER, &
coordDim=1, &
farrayPtr=lon_src_ptr, rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN GridGetCoord", error)
if (localpet==0) print*,"- CALL GridGetCoord FOR INPUT GRID Y-COORD."
nullify(lat_src_ptr)
call ESMF_GridGetCoord(target_grid, &
staggerLoc=ESMF_STAGGERLOC_CENTER, &
coordDim=2, &
computationalLBound=clb, &
computationalUBound=cub, &
farrayPtr=lat_src_ptr, rc=error)
if(ESMF_logFoundError(rcToCheck=error,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
call error_handler("IN GridGetCoord", error)
do j = clb(2),cub(2)
do i = clb(1), cub(1)
lon_src_ptr(i,j)=longitude_one(i,j)
lat_src_ptr(i,j)=latitude_one(i,j)
enddo
enddo
nullify(lon_src_ptr)
nullify(lat_src_ptr)
if (localpet==0) print*,"- CALL GridGetCoord FOR TARGET GRID X-COORD."
call ESMF_GridGetCoord(target_grid, &
staggerLoc=ESMF_STAGGERLOC_CORNER, &
coordDim=1, &
farrayPtr=lon_src_ptr, rc=error)
if(ESMF_logFoundError(rcToCheck=error, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__)) &
call error_handler("IN GridGetCoord", error)
if (localpet==0) print*,"- CALL GridGetCoord FOR TARGET GRID Y-COORD."
call ESMF_GridGetCoord(target_grid, &
staggerLoc=ESMF_STAGGERLOC_CORNER, &
coordDim=2, &
computationalLBound=clb, &
computationalUBound=cub, &
farrayPtr=lat_src_ptr, rc=error)
if(ESMF_logFoundError(rcToCheck=error, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__)) &
call error_handler("IN GridGetCoord", error)
do j = clb(2),cub(2)
do i = clb(1), cub(1)
lon_src_ptr(i,j)=longitude_corner_one(i,j)
lat_src_ptr(i,j)=latitude_corner_one(i,j)
enddo
enddo
nullify(lon_src_ptr,lat_src_ptr)
if (localpet==0) print*,"- CALL GridGetCoord FOR TARGET GRID EDGE1 X-COORD."
call ESMF_GridGetCoord(target_grid, &
staggerLoc=ESMF_STAGGERLOC_EDGE1, &
coordDim=1, &
farrayPtr=lon_src_ptr, rc=error)
if(ESMF_logFoundError(rcToCheck=error, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__,file=__FILE__))&
call error_handler("IN GridGetCoord", error)
if (localpet==0) print*,"- CALL GridGetCoord FOR TARGET GRID EDGE1 Y-COORD."
call ESMF_GridGetCoord(target_grid, &
staggerLoc=ESMF_STAGGERLOC_EDGE1, &
coordDim=2, &
computationalLBound=clb, &