-
Notifications
You must be signed in to change notification settings - Fork 10
/
CubeToLatLon.F90
2246 lines (1817 loc) · 69.7 KB
/
CubeToLatLon.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
! $Id$
#include "MAPL_ErrLog.h"
#define DEALLOCGLOB_(A) call deallocGlob(A,status);VERIFY_(status)
#define DEALLOCLOCL_(A) if(associated(A)) then; deallocate(A, stat=STATUS); VERIFY_(STATUS); NULLIFY(A); endif
#ifdef TAU_PROFILE
#undef _ASSERT
#define _ASSERT(A,'needs informative message')
#undef VERIFY_
#define VERIFY_(A)
#undef RETURN_
#define RETURN_(A)
#endif
Module CubeLatLonTransformMod
use ESMF
use MAPL
use, intrinsic :: iso_fortran_env, only: REAL64, REAL32
implicit none
private
public T_CubeLatLonTransform
public T_CubeCubeTransform
public CubeLatLonIsCreated
public CubeLatLonSubset
public CubeLatLonCreate
public CubeLatLonDestroy
public CubeToLatLon
public CubeCubeIsCreated
public CubeCubeCreate
public CubeCubeDestroy
public CubeToCube
public LatLonToCube
public CartesianToSpherical
public SphericalToCartesian
public RunTileTransform
public MAPL_RegridLSCreate
public TileTransformTranspose
public get_conservative_weights !JK patch for conservative interp
public RestaggerWindsCube
include 'mpif.h'
character*30 :: Iam="CubeToLatLon in line "
type T_CubeLatLonTransform
private
real(REAL64),pointer :: weight(:,:,:) => null()
real(REAL64),pointer :: l2c(:,:,:) => null()
integer, pointer :: index (:,:,:) => null()
integer, pointer :: id1(:,:) => null()
integer, pointer :: id2(:,:) => null()
integer, pointer :: jdc(:,:) => null()
logical :: Created=.false.
character(len=120) :: name
integer :: npx, npy, nlon, nlat
! global
real(REAL64), pointer :: ee1(:,:,:) => null()
real(REAL64), pointer :: ee2(:,:,:) => null()
real(REAL64), pointer :: ff1(:,:,:) => null()
real(REAL64), pointer :: ff2(:,:,:) => null()
real(REAL64), pointer :: gg1(:,:,:) => null()
real(REAL64), pointer :: gg2(:,:,:) => null()
! local
real(REAL64), pointer :: e1(:,:,:) => null()
real(REAL64), pointer :: e2(:,:,:) => null()
real(REAL64), pointer :: f1(:,:,:) => null()
real(REAL64), pointer :: f2(:,:,:) => null()
real(REAL64), pointer :: g1(:,:,:) => null()
real(REAL64), pointer :: g2(:,:,:) => null()
!
real(REAL64), pointer :: elon(:,:,:) => null()
real(REAL64), pointer :: elat(:,:,:) => null()
real(REAL64), pointer :: elon_local(:,:,:) => null()
real(REAL64), pointer :: elat_local(:,:,:) => null()
!
logical :: lsCreated = .false.
type(MAPL_LocStream) :: locStIn
type(MAPL_LocStream) :: locStOut
type(MAPL_LocStreamXform) :: XformInOut
type(MAPL_LocStreamXform) :: XformOutIn
logical :: subset = .false.
end type T_CubeLatLonTransform
type T_CubeCubeTransform
private
real(REAL64),pointer :: weight(:,:,:,:) => NULL()
integer, pointer :: index (:,:,:,:) => NULL()
real(REAL64), pointer :: ee1(:,:,:) => NULL()
real(REAL64), pointer :: ee2(:,:,:) => NULL()
real(REAL64), pointer :: ff1(:,:,:) => NULL()
real(REAL64), pointer :: ff2(:,:,:) => NULL()
logical :: Created=.false.
character(len=120) :: name
integer :: npx, npy, npxout, npyout
end type T_CubeCubeTransform
interface CubeToLatLon
module procedure CubeToLatLonr8
module procedure CubeToLatLonr4
end interface
interface LatLonToCube
module procedure LatLonToCuber8
module procedure LatLonToCuber4
end interface
interface SphericalToCartesian
module procedure SphericalToCartesianR4
module procedure SphericalToCartesianREAL64
module procedure SphericalToCartesianR4C2C
end interface
interface CartesianToSpherical
module procedure CartesianToSphericalR4
module procedure CartesianToSphericalREAL64
module procedure CartesianToSphericalR4C2C
end interface
integer, parameter :: ntiles=6
integer, parameter :: ndims=2
integer, parameter :: r8=REAL64
integer, parameter :: maxstring=120
real(REAL64), parameter :: PI=3.14159265358979323846d0
! This EXTERNAL subroutine is in the fv directory
! and has real*8 interfaces
interface
subroutine GetWeights(npx, npy, nlat, nlon, &
index, weight, id1, id2, jdc, l2c, &
ee1, ee2, ff1, ff2, gg1, gg2, &
e1, e2, f1, f2, g1, g2, &
sublons, sublats, AmNodeRoot, WriteNetcdf)
use, intrinsic :: iso_fortran_env, only: REAL64
integer, intent(in ) :: npx, npy
integer, intent(in ) :: nlon, nlat
integer, intent( out) :: index(3,nlon,nlat)
real(REAL64), intent( out) :: weight(4,nlon,nlat)
integer, intent( out) :: id1(npx,npy)
integer, intent( out) :: id2(npx,npy)
integer, intent( out) :: jdc(npx,npy)
real(REAL64), intent( out) :: l2c(4,npx,npy)
real(REAL64), intent( out) :: ee1(npx,npy,3)
real(REAL64), intent( out) :: ee2(npx,npy,3)
real(REAL64), intent( out) :: ff1(npx,npy,3)
real(REAL64), intent( out) :: ff2(npx,npy,3)
real(REAL64), intent( out) :: gg1(npx,npy,3)
real(REAL64), intent( out) :: gg2(npx,npy,3)
real(REAL64), pointer :: e1(:,:,:)
real(REAL64), pointer :: e2(:,:,:)
real(REAL64), pointer :: f1(:,:,:)
real(REAL64), pointer :: f2(:,:,:)
real(REAL64), pointer :: g1(:,:,:)
real(REAL64), pointer :: g2(:,:,:)
real(REAL64), intent(in) :: sublons(:)
real(REAL64), intent(in) :: sublats(:)
logical , optional :: AmNodeRoot
logical , optional :: WriteNetcdf
end subroutine GetWeights
end interface
interface
subroutine GetWeightsC2C(npx, npy, npxout, npyout, index, weight, &
ee1, ee2, ff1, ff2)
use, intrinsic :: iso_fortran_env, only: REAL64
integer, intent(in ) :: npx, npy
integer, intent(in ) :: npxout, npyout
integer, intent( out) :: index(:,:,:,:)
real(REAL64), intent( out) :: weight(:,:,:,:)
real(REAL64), intent( out) :: ee1(:,:,:)
real(REAL64), intent( out) :: ee2(:,:,:)
real(REAL64), intent( out) :: ff1(:,:,:)
real(REAL64), intent( out) :: ff2(:,:,:)
end subroutine GetWeightsC2C
end interface
!JK patch for conservative interp----------------------------
integer , save :: NT_tiles=-1
integer , allocatable, dimension(:) :: Tile_LL_ii, Tile_LL_jj
integer , allocatable, dimension(:) :: Tile_CS_nx, Tile_CS_ny
real(REAL64), allocatable, dimension(:) :: Tile_LL_wfrac, Tile_CS_wfrac
real(REAL64), allocatable, dimension(:) :: Tile_area, Tile_x, Tile_y
logical, save :: DO_CONSERVATIVE=.false.
!JK patch for conservative interp----------------------------
interface deallocGlob
module procedure deallocGlob_i4_2
module procedure deallocGlob_i4_3
module procedure deallocGlob_i4_4
module procedure deallocGlob_r8_3
module procedure deallocGlob_r8_4
end interface deallocGlob
interface deallocLoc
module procedure deallocLocl_r8_3
end interface deallocLoc
contains
subroutine CubeLatLonDestroy( Tr, rc)
type(T_CubeLatLonTransform), intent(inout) :: Tr
integer, optional, intent( out) :: rc
integer :: status
call MAPL_SyncSharedMemory(rc=STATUS)
VERIFY_(STATUS)
DEALLOCGLOB_(Tr%index)
DEALLOCGLOB_(Tr%weight)
DEALLOCGLOB_(Tr%l2c)
DEALLOCGLOB_(Tr%id1)
DEALLOCGLOB_(Tr%id2)
DEALLOCGLOB_(Tr%jdc)
DEALLOCGLOB_(Tr%ee1)
DEALLOCGLOB_(Tr%ee2)
DEALLOCGLOB_(Tr%ff1)
DEALLOCGLOB_(Tr%ff2)
DEALLOCGLOB_(Tr%gg1)
DEALLOCGLOB_(Tr%gg2)
DEALLOCLOCL_(Tr%elon)
DEALLOCLOCL_(Tr%elat)
DEALLOCLOCL_(Tr%e1)
DEALLOCLOCL_(Tr%e2)
DEALLOCLOCL_(Tr%f1)
DEALLOCLOCL_(Tr%f2)
DEALLOCLOCL_(Tr%g1)
DEALLOCLOCL_(Tr%g2)
Tr%Created = .false.
if (Tr%lsCreated) then
!ALT: if we created LocStream tile transforms we should destroy them
! unfortunately MAPL does not destroy LocationStreams yet
end if
RETURN_(_SUCCESS)
end subroutine CubeLatLonDestroy
logical function CubeLatLonIsCreated(Tr)
type(T_CubeLatLonTransform), intent(in ) :: Tr
CubeLatLonIsCreated = Tr%Created
end function CubeLatLonIsCreated
subroutine CubeLatLonSubset(Tr,doSubset)
logical, intent(in ) :: doSubset
type(T_CubeLatLonTransform) :: Tr
Tr%subset=dosubset
end subroutine CubeLatLonSubset
function CubeLatLonCreate( npx, npy, nlon, nlat, lons, lats, local_ij, rc ) result(Tr)
integer, intent(in ) :: npx, npy
integer, intent(in ) :: nlon, nlat
real(REAL64), intent(in ) :: lons(:), lats(:)
integer, optional, intent(in) :: local_ij(2,2)
type(T_CubeLatLonTransform) :: Tr
integer, optional, intent(out) :: rc
! npx : inner dimension of global cube arrays (number of cells along cube edge)
! npy : outer dimension of global cube arrays ( 6*npx )
! nlon : inner dimension of global LL arrays (Number of longitude points)
! nlat : outer dimension of global LL arrays (Number of latitude points)
! lons : the local nlon longitudes of LL grid, in radians
! lats : the local nlat latitudes of LL grid, in radians
! local_ij : bounding box in (i,j) for local portion of lat-lon grid
! Tr : The structure that holds the output transform
! rc : return code
! Creates all necessary data to transform fields between Cube and LatLon
! grids, in bothe directions. Data is stored in the output transform Tr. The
! transforms, Tr,is transposable by the MAPL transforming routines.
! Locals
!-------
integer :: npts, status
integer :: i, j
real(REAL64), allocatable :: slon(:), slat(:)
real(REAL64), allocatable :: clon(:), clat(:)
integer :: i0, i1, j0, j1
! global vector rotations to be copied into local Tr versions
! real(REAL64), pointer :: ee1(:,:,:) => null()
! real(REAL64), pointer :: ee2(:,:,:) => null()
! real(REAL64), pointer :: ff1(:,:,:) => null()
! real(REAL64), pointer :: ff2(:,:,:) => null()
! real(REAL64), pointer :: gg1(:,:,:) => null()
! real(REAL64), pointer :: gg2(:,:,:) => null()
! Begin
!------
_ASSERT(.not.Tr%Created,'needs informative message')
npts = npx + 1
write(Tr%name,'(i5.5,"x",i5.5,"_c2l_",i5.5,"x",i5.5)') npx,npy,nlon,nlat
! write(*,'(i5.5,"x",i5.5,"_c2l_",i5.5,"x",i5.5)') npx,npy,nlon,nlat
Tr%npx = npx
Tr%npy = npy
Tr%nlon = nlon
Tr%nlat = nlat
! allocate storage for weights and indeces for C2L
!-------------------------------------------------
DEALLOCGLOB_(Tr%index)
DEALLOCGLOB_(Tr%weight)
DEALLOCGLOB_(Tr%l2c)
DEALLOCGLOB_(Tr%id1)
DEALLOCGLOB_(Tr%id2)
DEALLOCGLOB_(Tr%jdc)
DEALLOCLOCL_(Tr%elon)
DEALLOCLOCL_(Tr%elat)
DEALLOCLOCL_(Tr%e1)
DEALLOCLOCL_(Tr%e2)
DEALLOCLOCL_(Tr%f1)
DEALLOCLOCL_(Tr%f2)
DEALLOCLOCL_(Tr%g1)
DEALLOCLOCL_(Tr%g2)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%index,(/3,nlon,nlat/),rc=STATUS)
else
allocate(Tr%index(3,nlon,nlat),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%weight,(/4,nlon,nlat/),rc=STATUS)
else
allocate(Tr%weight(4,nlon,nlat),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%l2c,(/4,npx,npy/),rc=STATUS)
else
allocate(Tr%l2c(4,npx,npy),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%id1,(/npx,npy/),rc=STATUS)
else
allocate(Tr%id1(npx,npy),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%id2,(/npx,npy/),rc=STATUS)
else
allocate(Tr%id2(npx,npy),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%jdc,(/npx,npy/),rc=STATUS)
else
allocate(Tr%jdc(npx,npy),stat=status)
end if
VERIFY_(STATUS)
allocate(Tr%elon(size(lons),size(lats),3),stat=STATUS)
VERIFY_(STATUS)
if (present(local_ij)) then
i0 = local_ij(1,1)
i1 = local_ij(2,1)
j0 = local_ij(1,2)
j1 = local_ij(2,2)
else
i0 = 1
i1 = size(lons)
j0 = 1
j1 = size(lats)
end if
tr%elon_local => tr%elon(i0:i1,j0:j1,:)
allocate(Tr%elat(size(lons),size(lats),3),stat=STATUS)
VERIFY_(STATUS)
tr%elat_local => tr%elat(i0:i1,j0:j1,:)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%ee1,(/npx,npy,3/),rc=STATUS)
else
allocate(Tr%ee1(npx,npy,3),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%ee2,(/npx,npy,3/),rc=STATUS)
else
allocate(Tr%ee2(npx,npy,3),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%ff1,(/npx,npy,3/),rc=STATUS)
else
allocate(Tr%ff1(npx,npy,3),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%ff2,(/npx,npy,3/),rc=STATUS)
else
allocate(Tr%ff2(npx,npy,3),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%gg1,(/npx,npy,3/),rc=STATUS)
else
allocate(Tr%gg1(npx,npy,3),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%gg2,(/npx,npy,3/),rc=STATUS)
else
allocate(Tr%gg2(npx,npy,3),stat=status)
end if
VERIFY_(STATUS)
! Argument AmNodeRoot passed to GetWeights identifies if we're using SHMEM
! and then only the NodeRoot gets weights, otherwise everyone does
!-------------------------------------------------------------------------
call GetWeights(npx, npy, nlat, nlon, Tr%index, Tr%weight, &
Tr%id1, Tr%id2, Tr%jdc, Tr%l2c, &
Tr%ee1, Tr%ee2, Tr%ff1, Tr%ff2, Tr%gg1, Tr%gg2, &
Tr%e1, Tr%e2, Tr%f1, Tr%f2, Tr%g1, Tr%g2, lons, lats, &
AmNodeRoot = (MAPL_AmNodeRoot .or. (.not. MAPL_ShmInitialized)) &
#ifdef WRITE_WEIGHTS_TO_FILE
, WriteNetcdf = MAPL_am_I_root() &
#endif
)
! Deallocate large global vector rotation transforms
call MAPL_SyncSharedMemory(rc=STATUS)
VERIFY_(STATUS)
! DEALLOCGLOB_(Tr%ee1)
! DEALLOCGLOB_(Tr%ee2)
! DEALLOCGLOB_(Tr%ff1)
! DEALLOCGLOB_(Tr%ff2)
! DEALLOCGLOB_(Tr%gg1)
! DEALLOCGLOB_(Tr%gg2)
! Cartesian to latlon spherical on latlon grid
allocate(slat(size(lats)),clat(size(lats)))
allocate(slon(size(lons)),clon(size(lons)))
do j=1,size(lats)
SLAT(j) = SIN(lats(j))
CLAT(j) = COS(lats(j))
end do
do I=1,size(lons)
SLON(I) = sin(lons(i) - PI)
CLON(I) = cos(lons(i) - PI)
end DO
do j=1,size(lats)
do I=1,size(lons)
Tr%elon(I,J,1) = -SLON(I)
Tr%elon(I,J,2) = CLON(I)
Tr%elon(I,J,3) = 0.0
Tr%elat(I,J,1) = -SLAT(J)*CLON(I)
Tr%elat(I,J,2) = -SLAT(J)*SLON(I)
Tr%elat(I,J,3) = CLAT(J)
end do
end do
deallocate(slon,clon,slat,clat)
Tr%Created=.true.
RETURN_(_SUCCESS)
end function CubeLatLonCreate
subroutine CubeCubeDestroy( Tr, rc)
type(T_CubeCubeTransform), intent(inout) :: Tr
integer, optional, intent( out) :: rc
integer :: status
DEALLOCGLOB_(Tr%weight)
DEALLOCGLOB_(Tr%index)
DEALLOCGLOB_(Tr%ee1)
DEALLOCGLOB_(Tr%ee2)
DEALLOCGLOB_(Tr%ff1)
DEALLOCGLOB_(Tr%ff2)
Tr%Created = .false.
RETURN_(_SUCCESS)
end subroutine CubeCubeDestroy
logical function CubeCubeIsCreated(Tr)
type(T_CubeCubeTransform), intent(in ) :: Tr
CubeCubeIsCreated = Tr%Created
end function CubeCubeIsCreated
function CubeCubeCreate( npx, npy, npxout, npyout, rc ) result(Tr)
integer, intent(in ) :: npx, npy
integer, intent(in ) :: npxout, npyout
type(T_CubeCubeTransform) :: Tr
integer, optional, intent(out) :: rc
! Locals
!-------
integer :: npts, status
integer, parameter :: ntiles=6
! Real*8 are needed to make fv calls.
!-----------------------------------
! Begin
!------
_ASSERT(.not.Tr%Created,'needs informative message')
!ALT npts = npx + 1
npts = npxout ! + 1
write(Tr%name,'(i5.5,"x",i5.5,"_c2c_",i5.5,"x",i5.5)') npx,npy,npxout,npyout
Tr%npx = npx
Tr%npy = npy
Tr%npxout = npxout
Tr%npyout = npyout
! allocate storage for weights and indeces for C2C
!-------------------------------------------------
DEALLOCGLOB_(Tr%weight)
DEALLOCGLOB_(Tr%index)
DEALLOCGLOB_(Tr%ee1)
DEALLOCGLOB_(Tr%ee2)
DEALLOCGLOB_(Tr%ff1)
DEALLOCGLOB_(Tr%ff2)
! ALT: index and weight are allocated at the output grid resolution
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%weight,(/4,npxout,npyout/6,6/),rc=STATUS)
else
allocate(Tr%weight(4,npxout,npyout/6,6),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%index,(/3,npxout,npyout/6,6/),rc=STATUS)
else
allocate(Tr%index(3,npxout,npyout/6,6),stat=status)
end if
VERIFY_(STATUS)
! ALT: ff1 and ff2 are allocated at the input grid resolution
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%ff1,(/npx,npy,3/),rc=STATUS)
else
allocate(Tr%ff1(npx,npy,3),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%ff2,(/npx,npy,3/),rc=STATUS)
else
allocate(Tr%ff2(npx,npy,3),stat=status)
end if
VERIFY_(STATUS)
! ALT: ee1 and ee2 are allocated at the output grid resolution
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%ee1,(/npxout,npyout,3/),rc=STATUS)
else
allocate(Tr%ee1(npxout,npyout,3),stat=status)
end if
VERIFY_(STATUS)
if(MAPL_ShmInitialized) then
call MAPL_AllocNodeArray(Tr%ee2,(/npxout,npyout,3/),rc=STATUS)
else
allocate(Tr%ee2(npxout,npyout,3),stat=status)
end if
VERIFY_(STATUS)
if (MAPL_AmNodeRoot .or. (.not. MAPL_ShmInitialized)) then
call GetWeightsC2C(npx, npy, npxout, npyout, Tr%index, Tr%weight, &
Tr%ee1, Tr%ee2, Tr%ff1, Tr%ff2)
end if
call MAPL_SyncSharedMemory(rc=STATUS)
VERIFY_(STATUS)
Tr%Created=.true.
RETURN_(_SUCCESS)
end function CubeCubeCreate
subroutine CubeToCube(Tr, data_cs_in, data_cs_out, rc)
type(T_CubeCubeTransform), intent(in ) :: Tr
real(REAL32), intent(inout) :: data_cs_in(:,:)
real(REAL32), intent(inout) :: data_cs_out(:,:)
integer, optional, intent(out) :: rc
! Locals
!-------
integer :: nx,j1,j2,status,itile
real(REAL64), allocatable :: var_cs_in(:,:,:), var_cs_out(:,:,:)
_ASSERT(Tr%Created,'needs informative message')
nx = Tr%npx
!--------------------------------------------------------------------!
! perform interpolation !
!--------------------------------------------------------------------!
allocate ( var_cs_in(0:nx+1,0:nx+1,ntiles),stat=status)
VERIFY_(STATUS)
var_cs_in=0.0
do itile=1,ntiles
j1 = nx*(itile-1) + 1
j2 = nx*(itile-1) + nx
var_cs_in(1:nx,1:nx,itile)=data_cs_in(:,j1:j2)
end do
nx = Tr%npxout
allocate ( var_cs_out(0:nx+1,0:nx+1,ntiles),stat=status)
VERIFY_(STATUS)
var_cs_out=0.0
call C2CInterp(var_cs_in, var_cs_out, Tr%index, Tr%weight)
do itile=1,ntiles
j1 = nx*(itile-1) + 1
j2 = nx*(itile-1) + nx
data_cs_out(:,j1:j2) = var_cs_out(1:nx,1:nx,itile)
end do
deallocate ( var_cs_in, var_cs_out, stat=status)
VERIFY_(STATUS)
RETURN_(_SUCCESS)
end subroutine CubeToCube
subroutine C2CInterp(var_in, var_out, index_c2c, weight_c2c)
!------------------------------------------------------------------!
! do bilinear interpolation from cubed sphere to latlon grid !
! using precalculated weights from get_weight !
!------------------------------------------------------------------!
real(REAL64), dimension(0:,0:,:), intent(inout) :: var_in
real(REAL64), dimension(0:,0:,:), intent(inout) :: var_out
real(REAL64), dimension(:,:,:,:), intent(in ) :: weight_c2c
integer, dimension(:,:,:,:), intent(in ) :: index_c2c
!------------------------------------------------------------------!
! local variables !
!------------------------------------------------------------------!
integer :: i, j, l, jx, ic, jc, lc, nx, ny
nx = size(var_out,1)-2
ny = size(var_out,2)-2
call GhostCube(var_in)
FACES: do l=1,ntiles
JLOOP: do j=1,ny
ILOOP: do i=1,nx
ic=index_c2c(1,i,j,l)
jc=index_c2c(2,i,j,l)
lc=index_c2c(3,i,j,l)
var_out(i,j,l)=weight_c2c(1,i,j,l)*var_in(ic ,jc , lc) &
+weight_c2c(2,i,j,l)*var_in(ic ,jc+1, lc) &
+weight_c2c(3,i,j,l)*var_in(ic+1,jc+1, lc) &
+weight_c2c(4,i,j,l)*var_in(ic+1,jc , lc)
enddo ILOOP
enddo JLOOP
enddo FACES
return
end subroutine C2CInterp
subroutine get_conservative_weights(name)
real(REAL64), allocatable :: FF_LL(:), FF_CS(:), CS_data(:,:), LL_data(:,:)
character(len=120) :: name
integer :: n, n_temp, nlon_, nlat_, nx_g, ny_g, UNIT
integer :: STATUS
type (ESMF_VM) :: vm
DO_CONSERVATIVE = .true.
call ESMF_VMGetCurrent(vm, rc=status)
!---read the weight fractions and connectivities---------------------------
UNIT = GETFILE(name, DO_OPEN=0, ALL_PES=.true., RC=STATUS)
open (UNIT=UNIT, FILE=NAME)
if ( MAPL_am_I_root() ) read(UNIT,*) NT_tiles, n_temp, n_temp
if ( MAPL_am_I_root() ) read(UNIT,*) n_temp
if ( MAPL_am_I_root() ) read(UNIT,*) name
if ( MAPL_am_I_root() ) read(UNIT,*) nlon_
if ( MAPL_am_I_root() ) read(UNIT,*) nlat_
if ( MAPL_am_I_root() ) read(UNIT,*) name
if ( MAPL_am_I_root() ) read(UNIT,*) nx_g
if ( MAPL_am_I_root() ) read(UNIT,*) ny_g
call MAPL_CommsBcast(vm, DATA=NT_tiles, N=1, ROOT=0, RC=status)
call MAPL_CommsBcast(vm, DATA=nlon_, N=1, ROOT=0, RC=status)
call MAPL_CommsBcast(vm, DATA=nlat_, N=1, ROOT=0, RC=status)
call MAPL_CommsBcast(vm, DATA=nx_g , N=1, ROOT=0, RC=status)
call MAPL_CommsBcast(vm, DATA=ny_g , N=1, ROOT=0, RC=status)
allocate (Tile_LL_ii(NT_tiles), Tile_LL_jj(NT_tiles))
allocate (Tile_CS_nx(NT_tiles), Tile_CS_ny(NT_tiles))
allocate (Tile_LL_wfrac(NT_tiles), Tile_CS_wfrac(NT_tiles))
allocate (Tile_area(NT_tiles), Tile_x(NT_tiles), Tile_y(NT_tiles))
do n=1,NT_tiles
if ( MAPL_am_I_root() ) then
read(UNIT,*) n_temp, Tile_area(n), Tile_x(n), Tile_y(n), &
Tile_LL_ii(n), Tile_LL_jj(n), Tile_LL_wfrac(n),&
n_temp, Tile_CS_nx(n), Tile_CS_ny(n), Tile_CS_wfrac(n),&
n_temp
endif
enddo
call MAPL_CommsBcast(vm, DATA=Tile_LL_ii, N=NT_tiles, ROOT=0, RC=status)
call MAPL_CommsBcast(vm, DATA=Tile_LL_jj, N=NT_tiles, ROOT=0, RC=status)
call MAPL_CommsBcast(vm, DATA=Tile_CS_nx, N=NT_tiles, ROOT=0, RC=status)
call MAPL_CommsBcast(vm, DATA=Tile_CS_ny, N=NT_tiles, ROOT=0, RC=status)
call MAPL_CommsBcast(vm, DATA=Tile_LL_wfrac, N=NT_tiles, ROOT=0, RC=status)
call MAPL_CommsBcast(vm, DATA=Tile_CS_wfrac, N=NT_tiles, ROOT=0, RC=status)
call FREE_FILE(UNIT)
!---normalizing the weight fractions--------------------------------------
allocate(LL_data(nlon_, nlat_), CS_data(nx_g, ny_g))
allocate(FF_CS(nt_tiles), FF_LL(nt_tiles))
CS_data(:,:)=0.; LL_data(:,:)=0.
do n=1,ntiles
CS_data(Tile_CS_nx(n),Tile_CS_ny(n))=&
CS_data(Tile_CS_nx(n),Tile_CS_ny(n))+Tile_CS_wfrac(n)
LL_data(Tile_LL_ii(n),Tile_LL_jj(n))=&
LL_data(Tile_LL_ii(n),Tile_LL_jj(n))+Tile_LL_wfrac(n)
enddo
FF_CS=0.; FF_LL=0.
do n=1,ntiles
FF_CS(n)=CS_data(Tile_CS_nx(n),Tile_CS_ny(n))
FF_LL(n)=LL_data(Tile_LL_ii(n),Tile_LL_jj(n))
enddo
do n=1,ntiles
Tile_CS_wfrac(n)=Tile_CS_wfrac(n)/FF_CS(n)
Tile_LL_wfrac(n)=Tile_LL_wfrac(n)/FF_LL(n)
enddo
deallocate(FF_CS, FF_LL)
deallocate(LL_data, CS_data)
end subroutine get_conservative_weights
subroutine CubeToLatLonr8( Tr, data_cs, data_ll, transpose, misval, rc)
type(T_CubeLatLonTransform), intent(in ) :: Tr
real(REAL64), intent(inout) :: data_cs(:,:)
real(REAL64), intent(inout) :: data_ll(:,:)
logical, optional, intent(in ) :: transpose
real(REAL32), optional, intent(in ) :: misval
integer, optional, intent(out) :: rc
! Locals
!-------
integer :: nx,j1,j2,status,itile
real(REAL64), allocatable :: var_cs(:,:,:)
_ASSERT(Tr%Created,'needs informative message')
nx = Tr%npx
!--------------------------------------------------------------------!
! perform interpolation !
!--------------------------------------------------------------------!
allocate ( var_cs(0:nx+1,0:nx+1,ntiles),stat=status)
VERIFY_(STATUS)
_ASSERT(.not. (transpose .and. present(misval)),'needs informative message')
var_cs=0.0
if(.not.transpose) then
data_ll=0.0
do itile=1,ntiles
j1 = nx*(itile-1) + 1
j2 = nx*(itile-1) + nx
var_cs(1:nx,1:nx,itile)=data_cs(:,j1:j2)
enddo
end if
call C2LInterp(var_cs, data_ll, Tr%index, Tr%weight,&
misval, Tr%subset, transpose)
if(transpose) then
do itile=1,ntiles
j1 = nx*(itile-1) + 1
j2 = nx*(itile-1) + nx
data_cs(:,j1:j2) = var_cs(1:nx,1:nx,itile)
enddo
end if
deallocate ( var_cs ,stat=status)
VERIFY_(STATUS)
RETURN_(_SUCCESS)
end subroutine CubeToLatLonr8
subroutine CubeToLatLonr4( Tr, data_cs, data_ll, transpose, misval, rc)
type(T_CubeLatLonTransform), intent(in ) :: Tr
real(REAL32), intent(inout) :: data_cs(:,:)
real(REAL32), intent(inout) :: data_ll(:,:)
logical, optional, intent(in ) :: transpose
real(REAL32), optional, intent(in ) :: misval
integer, optional, intent(out) :: rc
! Locals
!-------
integer :: nx,j1,j2,status,itile
real(REAL64), allocatable :: var_cs(:,:,:), data_ll8(:,:)
!JK patch for conservative interp---------------
real(REAL64), allocatable :: data_cs8(:,:)
_ASSERT(Tr%Created,'needs informative message')
nx = Tr%npx
!--------------------------------------------------------------------!
! perform interpolation !
!--------------------------------------------------------------------!
allocate ( var_cs(0:nx+1,0:nx+1,ntiles),stat=status )
VERIFY_(STATUS)
allocate ( data_ll8(size(data_ll,1),size(data_ll,2)),stat=status)
VERIFY_(STATUS)
if (DO_CONSERVATIVE) then
allocate ( data_cs8(size(data_cs,1),size(data_cs,2)),stat=status )
VERIFY_(STATUS)
endif
_ASSERT(.not. (transpose .and. present(misval)),'needs informative message')
var_cs=0.0
if(.not.transpose) then
do itile=1,ntiles
j1 = nx*(itile-1) + 1
j2 = nx*(itile-1) + nx
var_cs(1:nx,1:nx,itile)=data_cs(:,j1:j2)
enddo
if (DO_CONSERVATIVE) data_cs8=data_cs !JK for conservative interp---
else
data_ll8=data_ll
end if
if (DO_CONSERVATIVE) then !JK for conservative interp---
if(.not.transpose) then
call CToL_interp &
(data_ll8, data_cs8, NT_Tiles, Tr%nlat, Tr%nlon, Tr%npx, Tr%npy)
else
call CToL_interp_b&
(data_ll8, data_cs8, NT_Tiles, Tr%nlat, Tr%nlon, Tr%npx, Tr%npy)
endif
else !JK for conservative interp---
call C2LInterp(var_cs, data_ll8, Tr%index, Tr%weight,&
misval, Tr%subset, transpose)
endif !JK for conservative interp---
if(transpose) then
do itile=1,ntiles
j1 = nx*(itile-1) + 1
j2 = nx*(itile-1) + nx
data_cs(:,j1:j2) = var_cs(1:nx,1:nx,itile)
enddo
if (DO_CONSERVATIVE) data_cs=data_cs8 !JK for conservative interp---
else
data_ll=data_ll8
end if
if (DO_CONSERVATIVE) deallocate ( data_cs8 ) !JK for conservative interp---
deallocate ( var_cs, data_ll8 )
RETURN_(_SUCCESS)
end subroutine CubeToLatLonr4
subroutine LatLonToCuber8( Tr, data_ll, data_cs, transpose, misval, rc)
type(T_CubeLatLonTransform), intent(in ) :: Tr
real(REAL64), intent(inout) :: data_ll(:,:)
real(REAL64), intent(inout) :: data_cs(:,:)
logical, optional, intent(in ) :: transpose
real(REAL32), optional, intent(in ) :: misval
integer, optional, intent(out) :: rc
! Locals
!-------
integer :: nx,j1,j2,status,itile
real(REAL64), allocatable :: var_cs(:,:,:)
_ASSERT(Tr%Created,'needs informative message')
nx = Tr%npx
!--------------------------------------------------------------------!
! perform interpolation !
!--------------------------------------------------------------------!
allocate ( var_cs(0:nx+1,0:nx+1,ntiles),stat=status)
VERIFY_(STATUS)
var_cs=0.
if(transpose) then
data_ll=0.
do itile=1,ntiles
j1 = nx*(itile-1) + 1
j2 = nx*(itile-1) + nx
var_cs(1:nx,1:nx,itile) = data_cs(:,j1:j2)
enddo
end if
call L2CInterp(data_ll, var_cs, Tr%id1, Tr%id2, Tr%jdc, &
Tr%l2c, misval, transpose)
if(.not.transpose) then
do itile=1,ntiles
j1 = nx*(itile-1) + 1
j2 = nx*(itile-1) + nx
data_cs(:,j1:j2) = var_cs(1:nx,1:nx,itile)
enddo
end if
deallocate ( var_cs ,STAT=STATUS)
VERIFY_(STATUS)
RETURN_(_SUCCESS)
end subroutine LatLonToCuber8
subroutine LatLonToCuber4( Tr, data_ll, data_cs, transpose, misval, rc)
type(T_CubeLatLonTransform), intent(in ) :: Tr
real(REAL32), intent(inout) :: data_ll(:,:)
real(REAL32), intent(inout) :: data_cs(:,:)
logical, optional, intent(in ) :: transpose
real(REAL32), optional, intent(in ) :: misval
integer, optional, intent(out) :: rc
! Locals