forked from NordicESMhub/OsloCTM3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.f90
2857 lines (2514 loc) · 100 KB
/
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
!//=========================================================================
!// Oslo CTM3
!//=========================================================================
!// Based on UCI CTM core p-7.1 (1/2013).
!//
!// Ole Amund Sovde, April 2015
!//=========================================================================
!// Routines for setting up grid.
!//=========================================================================
module grid
!//-----------------------------------------------------------------------
!// MODULE: grid
!// DESCRIPTION: Routines for setting up grid.
!// This is the UCI p-grid.f, but slightly modified
!// to CTM3 f90.
!//
!// Contains
!// subroutine SET_GRID
!// subroutine LABELG
!// subroutine DIAGBLK
!// subroutine GAUSST2
!// subroutine DBLDBL
!// subroutine AIRSET
!// subroutine SURF_IN
!// subroutine gridICAO
!//
!//-----------------------------------------------------------------------
!//-----------------------------------------------------------------------
implicit none
!//-----------------------------------------------------------------------
character(len=*), parameter, private :: f90file = 'grid.f90'
!// ----------------------------------------------------------------------
public
!//-----------------------------------------------------------------------
contains
!//-----------------------------------------------------------------------
subroutine SET_GRID(GM0000,JMPOLR)
!//---------------------------------------------------------------------
!// General grid set up, global only, handles EC and GISS grids
!// Latitude (J=1:JPAR) grid:
!// LGAUGRD = .true. - use Gaussian grid for latitude, else uniform grid
!// JMPOLR (regualr grid only)
!// =0 =>polar box same delta-lat as others,
!// =1 =>polar box is halfsize (eg GISS JPAR=46)
!// Longitude (I=1:IM) grid: always regular, uniform spacing
!// GM0000 = I-coord of the Greenwich Merid:
!/ 1.5 => mid-box[1] on GM
!// IPARW/2+1 => left-box[1] at Dateline
!// Currently for EC, GM0000=1.5; for GISS4x5, GM0000=37.0 & JMPOLR=1
!//
!// Pressure/Altitude (L=1:LM) grid: based on eta coords
!//---------------------------------------------------------------------
use cmn_precision, only: r8
use cmn_size, only: IPARW, JPARW, IPAR, JPAR, LGAUGRD, &
NTRUNW, NMMAX, IDGRD, JDGRD
use cmn_ctm, only: LDEG, IMEPZ, IDTLN, WGLYE, WGLYG, ALP, ALPV, &
DD, SS, TRIG, IFAX, TRIGU, AREAXYW, AREAXY, &
YGRD, YDGRD, XGRD, XDGRD, YEDG, YDEDG, XEDG, XDEDG, &
YDGRDW, XDGRDW, YDEDGW, XDEDGW, &
ZDEGI, ZDEGJ, IMAP, JMAP, &
DISTX, DISTY, &
AREAG, TLAT, TLATE, TLNG, TLNGE
use cmn_met, only: metTYPE, PMEAN, PMEANW
use cmn_parameters, only: CPI, C2PI, CPI180, ZPI180, A0
use utilities, only: ctmExitC
use ncutils, only: get_netcdf_var_1d
!//---------------------------------------------------------------------
implicit none
!//---------------------------------------------------------------------
!// Input
real(r8), intent(in):: GM0000
integer, intent(in):: JMPOLR
!// Locals
integer :: I,J,IMB2,JMB2
real(r8) :: DEL_JD,DEL_I,DEL_ID
real(r8) :: WGAULAT(JPARW),WGAUWT(JPARW), WYEDG1P1(JPARW+1)
real(r8) :: YEDG1P1(JPAR+1)
real(r8), dimension(JPARW) :: YGRDW
real(r8), dimension(JPARW+1) :: YEDGW
real(r8), dimension(IPARW) :: XGRDW
real(r8), dimension(IPARW+1) :: XEDGW
!// lat/lon from NorESM, real native resolution
real(r8), allocatable, dimension(:) :: inLat, inLatE
!//---------------------------------------------------------------------
character(len=*), parameter :: subr='set_grid'
!// --------------------------------------------------------------------
write(6,'(a)') '--------------------------------------------'// &
'---------------------------'
write(6,'(a)') f90file//':'//subr//': Initialising horizontal grid'
JMB2 = JPARW/2
IMB2 = IPARW/2
if (mod(IPARW,2) .ne. 0) &
call ctmExitC(f90file//':'//subr//': IPARW must be even')
!// check EPZ fit:
do J = 1, JPAR
if (mod(IPARW, IMEPZ(J)) .ne. 0) &
call ctmExitC(f90file//':'//subr//': IMEPZ/IM misfit')
end do
!// LONGITUDE grid is always regular and set by IPARW and GM000
!//---------------------------------------------------------------------
DEL_ID = 360._r8 / real(IPARW, r8)
do I = 1, IPARW+1
XDEDGW(I) = (real(I, r8) - GM0000) * DEL_ID
end do
do I = 1, IPARW
XDGRDW(I) = 0.5_r8 * (XDEDGW(I) + XDEDGW(I+1))
end do
do I = 1, IPARW+1
XEDGW(I) = XDEDGW(I) * CPI180
end do
do I = 1, IPARW
XGRDW(I) = XDGRDW(I) * CPI180
end do
!// Will skip negative values for labeling (produces 170W instead of 190E)
!// Not important.
!// for computation XEDGW must be montonic, for labeling shift to
!// neg. longit.
!do I = 1, IPARW+1
! if (XDEDGW(I) .gt. 180._r8) XDEDGW(I) = XDEDGW(I) - 360._r8
!end do
!do I = 1, IPARW
! if (XDGRDW(I) .gt. 180._r8) XDGRDW(I) = XDGRDW(I) - 360._r8
!end do
!// set dateline box
IDTLN = mod(int(GM0000) + IPAR/2 - 1, IPAR) + 1
!// LATITUDE grid can be Gauss-pt, or regular, incl. half-size boxes
!// at pole,
!// WYEDG1P1 = sin(latitude) of box edges (-1 to +1) [temporary]
!//---------------------------------------------------------------------
if (mod(JPARW, 2) .ne. 0) &
call ctmExitC(f90file//':'//subr//': JPARW must be even')
!// Meteorology type (EC or other)
!//---------------------------------------------------------------------
if (METTYPE(1:6) .eq. 'norESM') then
!// How to use the grids
!// Most data are given on lat/lon parameters, which should
!// be grid centers. However, lat starts at 90S and ends at 90N.
!// This is because the polar cap is treated as a single box
!// in transport, so each polar pie piece has the extention of
!// a half grid box.
!//
!// All variables on lat/lon are given as grid center values,
!// e.g. rain and convective fluxes.
!//
!// U and V are also given on lat/lon, while US and VS are
!// given on slat/slon (the staggered values). However,
!// US is (lon/slat) and VS is (slon,lat), so this is not
!// what we want.
!//
!// The polar pies will be very small, so I will combine it with
!// the polar-most grid box. Hence, if NorESM has 96 latitudes
!// the CTM will use 94.
!// First read NorESM native data
call get_netcdf_var_1d('norESMgrid.nc', 'lat', inLat)
call get_netcdf_var_1d('norESMgrid.nc', 'ilat', inLatE)
!// New edges from pole to pole
YDEDGW(1) = -90._r8
!// Skip the next-to-polar-pie box
YDEDGW(2:JPARW) = inLatE(2:JPARW)
YDEDGW(JPARW+1) = 90._r8
do J = 1,JPARW+1
YEDGW(J) = YDEDGW(J) * CPI180
WYEDG1P1(J) = sin(YEDGW(J))
write(6,'(i4,f12.5)') J,YDEDGW(J)
end do
!// Skip the next-to-polar-pie box
!// and calculate new center value
YDGRDW(1) = 0.5_r8 * (-90._r8 + inLatE(2))
YDGRDW(2:JPARW-1) = inLat(3:JPARW) !// Skip the next-to-polar-pie box
YDGRDW(JPARW) = 0.5_r8 * (inLatE(JPARW) + 90._r8)
do J = 1,JPARW
YGRDW(J) = YDGRDW(J) * CPI180
WGAULAT(J) = sin(YGRDW(J))
write(6,'(i4,f12.5)') J,YDGRDW(J)
end do
do J = 1,JPARW+1
WGLYE(J) = Asin(WYEDG1P1(J))
enddo
do J = 1,JPARW
WGLYG(J) = Asin(WGAULAT(J))
enddo
deallocate(inLat, inLatE)
!// Now two questions remain:
!// 1. What to do with winds
!// U needs to be shifted (U(I) is to be flux into box I)
!// At poles, the grid point shifts halfway towards meridional
!// staggered location.
!// V: In CTM3, V(J) is wind (converted to flux) into grid box J.
!// It is by default set to zero at the poles???
!// NorESM-V thus has to be shifted polewards, so that
!// V(1) = inV(1) !// wind into pie piece
!// do J=2,JPAR
!// V(J) = 0.5d0 * (inV(J-1) + inV(J))
!// end do
!// 2. What to do with grid center polar cap values for other
!// species?
!// I will use the polar pie pieces as though they are given
!// in their new centers.
else if (METTYPE(1:9) .eq. 'ECMWF_IFS' .or. &
METTYPE(1:10) .eq. 'ECMWF_oIFS') then
!// EC grids
!//------------------------------------------------------------------
if (.not. LGAUGRD) then
!// Regular latitude grid
DEL_JD = 180._r8 / real(JPARW - JMPOLR, r8)
YDEDGW(1) = -90._r8
YDEDGW(2) = -90._r8 + DEL_JD / real(1 + JMPOLR, r8)
do J = 3, JPARW
YDEDGW(J) = YDEDGW(2) + real(J-2, r8) * DEL_JD
end do
YDEDGW(JPARW+1) = 90._r8
do J = 1, JPARW+1
YEDGW(J) = YDEDGW(J)*CPI180
WYEDG1P1(J) = sin(YDEDGW(J)*CPI180)
end do
do J = 1, JPARW
YGRDW(J) = asin(0.5_r8 * (WYEDG1P1(J) + WYEDG1P1(J+1)))
YDGRDW(J) = ZPI180 * YGRDW(J)
end do
do J = 1, JPARW+1
WGLYE(J) = Asin(WYEDG1P1(J))
end do
do J = 1, JPARW
WGLYG(J) = YGRDW(J)
end do
else
!// Gaussian latitude grid
call GAUSST2(JPARW, WGAULAT, WGAUWT)
WYEDG1P1(1) = -1._r8
do J = 2, JMB2
WYEDG1P1(J) = WYEDG1P1(J-1) + WGAUWT(J-1)
end do
WYEDG1P1(JMB2+1) = 0._r8
do J = JMB2+2, JPARW+1
WYEDG1P1(J) = -WYEDG1P1(JPARW+2-J)
end do
do J = 1, JPARW+1
WGLYE(J) = Asin(WYEDG1P1(J))
end do
do J = 1, JPARW
WGLYG(J) = Asin(WGAULAT(J))
end do
do J = 1, JPARW+1
YEDGW(J) = asin(WYEDG1P1(J))
YDEDGW(J) = ZPI180 * YEDGW(J)
end do
do J = 1, JPARW
YGRDW(J) = asin(WGAULAT(J))
YDGRDW(J) = ZPI180 * YGRDW(J)
end do
end if !// if (.not. LGAUGRD) then
!// EC: set up Spectral to Grid transform functions: Assoc-Leg Polys
do J = 1, JMB2
call LEGGEN(ALP(1,J),YGRDW(J),NTRUNW,NMMAX)
end do
do J = 1, JMB2
call LEGGEN(ALPV(1,J),YEDGW(J+1),NTRUNW,NMMAX)
end do
!// EC: transforms from Vorticiy and Divergence to U and V
call FFT_DDSS (NTRUNW,A0,NMMAX,DD,SS)
!// EC: FFT transforms, use grid
call FFT_SET (TRIG,IFAX,IPARW)
!// EC: trig functions for U (shift to boundaries)
DEL_I = C2PI / real(IPARW, r8)
do I = 1, IMB2
TRIGU(2*I-1) = cos(DEL_I*(real(I, r8)-0.5_r8))
TRIGU(2*I ) = sin(DEL_I*(real(I, r8)-0.5_r8))
end do
else
write(6,'(a)') f90file//':'//subr// &
': Unknown metTYPE: '//trim(metTYPE)
stop
end if !// if (METTYPE(1:2) .eq. 'EC') then
if (.false.) then
!// REGULAR grid can be found as this
!//------------------------------------------------------------------
DEL_JD = 180._r8 / real(JPARW - JMPOLR, r8)
YDEDGW(1) = -90._r8
YDEDGW(2) = -90._r8 + DEL_JD/real(1 + JMPOLR, r8)
do J = 3, JPARW
YDEDGW(J) = YDEDGW(2) + real(J-2, r8) * DEL_JD
end do
YDEDGW(JPARW+1) = 90._r8
do J = 1, JPARW+1
YEDGW(J) = YDEDGW(J) * CPI180
WYEDG1P1(J) = sin(YDEDGW(J) * CPI180)
end do
do J = 1,JPARW
YGRDW(J) = asin(0.5_r8*(WYEDG1P1(J)+WYEDG1P1(J+1)))
YDGRDW(J) = ZPI180 * YGRDW(J)
end do
end if !// if (METTYPE(1:2) .eq. 'EC') then
!// Area of grid boxes
!//---------------------------------------------------------------------
do J = 1,JPARW
do I = 1,IPARW
AREAXYW(I,J)= A0*A0 * (XEDGW(I+1) - XEDGW(I)) &
* (WYEDG1P1(J+1) - WYEDG1P1(J))
end do
end do
!// Define lat-lon of ctm grid and weightings if horizontal
!// resolution degraded
!//---------------------------------------------------------------------
call DBLDBL(YGRD,YDGRD,XGRD,XDGRD,YEDG,YDEDG,XEDG,XDEDG, &
YGRDW,YDGRDW,XGRDW,XDGRDW,YEDGW,YDEDGW,XEDGW,XDEDGW, &
ZDEGI,ZDEGJ,IMAP,JMAP, &
IPARW,JPARW,IPAR,JPAR,IDGRD,JDGRD,LDEG)
YEDG1P1(1) = -1._r8
do J = 2, JPAR
YEDG1P1(J) = sin(YEDG(J))
end do
YEDG1P1(JPAR+1) = 1._r8
!// DIAGNOSTICS AND WEIGHTING FACTORS
!//---------------------------------------------------------------------
!//>>>>>this new gridding does not yet deal with nested, doubled grids!
!// AREAS: use the IPAR,JPAR rather than IPARW,JPARW in case of re-grid
do J = 1, JPAR
DISTY(J) = A0*(YEDG(J+1)-YEDG(J))
end do
do J = 1, JPAR
DISTX(J) = A0 * (XEDG(2) - XEDG(1)) * 0.5_r8 &
* (cos(YEDG(J)) + cos(YEDG(J+1)))
end do
do J = 1, JPAR
do I = 1, IPAR
AREAXY(I,J) = A0*A0 * (XEDG(I+1) - XEDG(I)) &
* (YEDG1P1(J+1) - YEDG1P1(J))
end do
end do
!// Global total area
AREAG = sum(AREAXY)
!// Labels
!//---------------------------------------------------------------------
do J = 1, JPAR
call LABELG(YDGRD(J),TLAT(J),1)
end do
do J = 1, JPAR+1
call LABELG(YDEDG(J),TLATE(J),1)
end do
do I = 1, IPAR
call LABELG(XDGRD(I),TLNG(I),2)
end do
do I = 1, IPAR+1
call LABELG(XDEDG(I),TLNGE(I),2)
end do
!// Write-out grid data
!//---------------------------------------------------------------------
!// will need to shift absolute global grid to actual window calc later!
write(6,*) ' ------------------grid----------------------'
write(6,'(a,1p,e15.8)') ' AREA globe(m^2) : ', AREAG
write(6,'(A)') 'Area of grid boxes (m^2) 1:JPAR '
write(6,'(1P10E10.3)') (AREAXY(1,J),J=1,JPAR)
write(6,'(a)') ' MID-POINT of GRID BOXES'
write(6,'(a,i5)') ' J=1:',JPAR
write(6,'(5(i5,f9.5,f6.1,a5))') (J, YGRD(J), YDGRD(J), TLAT(J), J=1,JPAR)
write(6,'(a,i5)') ' I=1:',IPAR
write(6,'(5(i5,f9.5,f6.1,a5))') (I, XGRD(I), XDGRD(I), TLNG(I), I=1,IPAR)
write(6,'(a,i8)') ' IDATELINE=', IDTLN
write(6,*) ' EDGES of GRID'
write(6,'(a,i5)') ' J=1:',JPAR+1
write(6,'(5(i5,f9.5,f6.1,a5))') (J, YEDG(J), YDEDG(J), TLATE(J), J=1,JPAR+1)
write(6,'(a,i5)') ' I=1:',IPAR+1
write(6,'(5(i5,f9.5,f6.1,a5))') (I, XEDG(I), XDEDG(I), TLNGE(I), I=1,IPAR+1)
write(6,'(a)') ' IMEPZ = polar box averaging over met fields'
write(6,'(8(i6,a4,i4))') (J, TLAT(J), IMEPZ(J), J=1,JPAR)
!//---------------------------------------------------------------------
end subroutine SET_GRID
!//-----------------------------------------------------------------------
!//-----------------------------------------------------------------------
subroutine SET_GRID_VERT()
!//---------------------------------------------------------------------
!// General grid set up, global only and vertical only.
!// Uses hybrid coordinates (ETAAW and ETABW), along with
!// PMEANW and PMEAN.
!//
!// Separated out vertical part from SET_GRID, since mean pressure
!// needed horizontal part om SET_GRID and the vertical part needed
!// mean pressure.
!//
!// Ole Amund Sovde, March 2016
!//---------------------------------------------------------------------
use cmn_precision, only: r8
use cmn_size, only: IPARW, JPARW, LPARW, IPAR, JPAR, LPAR, &
IDGRD, JDGRD
use cmn_ctm, only: AREAXY, &
ETAAW, ETABW, ETAA, ETAB, &
LMMAP, XLMMAP, XYZA, XYZB, XYA, XYB, PIJL, &
ZEDG, ZGRD, &
TALT, TALTE
use cmn_met, only: PMEAN, PMEANW
use cmn_parameters, only: G0
use ncutils, only: get_netcdf_var_1d
!//---------------------------------------------------------------------
implicit none
!//---------------------------------------------------------------------
!// Input
!// Locals
integer :: I,J,L,LW
real(r8) :: SUMXYZ, AIRGLB
!//---------------------------------------------------------------------
character(len=*), parameter :: subr='set_grid_vert'
!// --------------------------------------------------------------------
write(6,'(a)') '--------------------------------------------'// &
'---------------------------'
write(6,'(a)') f90file//':'//subr//': Initialising vertical grid'
!// PRESSURE/ALTITUDE grid, remap the global wind grid (W)
!//---------------------------------------------------------------------
!// if LPAR < LPARW (met field layers) use LMMAP to collapse
!// met-layers on CTM L
!// Calculate new ETAA & ETAB for CTM(1:LM) with remapped vertical
!// grid(1:LPARW)
do LW = 1, LPARW+1
ETAAW(LW) = 0.01_r8 * ETAAW(LW)
end do
ETAA(1) = ETAAW(1)
ETAB(1) = ETABW(1)
do LW = 1, LPARW
L = LMMAP(LW)
ETAA(L+1) = ETAAW(LW+1)
ETAB(L+1) = ETABW(LW+1)
end do
!// LPAR is top anyway
!LM = LMMAP(LPARW)
!// Calculate weighting for LW=1:LPARW to merged CTM layer L=1:LM
do LW = 1, LPARW
L = LMMAP(LW)
XLMMAP(LW) = ((ETAAW(LW) - ETAAW(LW+1)) &
+ 1.e3_r8 * (ETABW(LW) - ETABW(LW+1)) ) / ((ETAA(L) - ETAA(L+1)) &
+ 1.e3_r8 * (ETAB(L) - ETAB(L+1)) )
end do
!// Air mass factors for each grid box:
!// air mass(I,J,L) in kg = XYZA() + XYZB()*surface pressure (mbar!!)
SUMXYZ = 0._r8
do L = 1, LPAR
do J = 1, JPAR
do I = 1, IPAR
XYZA(I,J,L) = (ETAA(L) - ETAA(L+1)) * AREAXY(I,J) * 1.e2_r8 / G0
XYZB(I,J,L) = (ETAB(L) - ETAB(L+1)) * AREAXY(I,J) * 1.e2_r8 / G0
SUMXYZ = SUMXYZ + XYZB(I,J,L)
end do
end do
end do
do J = 1, JPAR
do I = 1,IPAR
XYZA(I,J,LPAR+1) = ETAA(LPAR+1) * AREAXY(I,J) * 1.e2_r8 / G0
XYZB(I,J,LPAR+1) = ETAB(LPAR+1) * AREAXY(I,J) * 1.e2_r8 / G0
end do
end do
!// Column air mass factors XYA, XYB do not include > model top
do J = 1, JPAR
do I = 1, IPAR
XYA(I,J) = 0._r8
XYB(I,J) = 0._r8
do L = 1, LPAR
XYA(I,J) = XYA(I,J) + XYZA(I,J,L)
XYB(I,J) = XYB(I,J) + XYZB(I,J,L)
end do
end do
end do
AIRGLB = 0._r8
do L = 1, LPAR
do J = 1, JPAR
do I = 1, IPAR
AIRGLB = AIRGLB + XYZA(I,J,L) + XYZB(I,J,L) * PMEAN(I,J)
end do
end do
end do
!// Create new 3-D P-grid for interpolating sources, etc. using PMEAN
!// PIJL(I,J,L=1:LM+1) is pressure at edges
do L = 1, LPAR+1
do J = 1, JPAR
do I = 1,IPAR
PIJL(I,J,L) = ETAA(L) + ETAB(L) * PMEAN(I,J)
end do
end do
end do
do L = 1, LPAR+1
ZEDG(L) = ETAA(L) + ETAB(L) * 1000._r8
call LABELG(ZEDG(L),TALTE(L),3)
end do
do L = 1, LPAR
ZGRD(L) = 0.5_r8 * (ZEDG(L) + ZEDG(L+1))
call LABELG(ZGRD(L),TALT(L),3)
end do
write(6,*) ' --------------vertical grid----------------------'
write(6,'(a,1p,e15.8)') ' AIR mass wet (kg) : ', AIRGLB
write(6,'(a)') ' MID-POINT of GRID BOXES'
write(6,'(a)') ' LW wting ==> L (lbl)'
write(6,'(i5,f10.5,i5,a5)') &
(LW, XLMMAP(LW),LMMAP(LW),TALT(LMMAP(LW)), LW=1,LPARW)
write(6,*) ' EDGES of GRID'
write(6,'(a,i5)') ' L=1:',LPAR+1
write(6,'(1x,a1,i2,a2,a5,f9.3,a4)') &
('L',L-1,'.5', TALTE(L), ZEDG(L),' hPa', L=1,LPAR+1)
!//---------------------------------------------------------------------
end subroutine SET_GRID_VERT
!//-----------------------------------------------------------------------
!//-----------------------------------------------------------------------
subroutine set_mean_psfc(infile)
!//---------------------------------------------------------------------
!// Reads 2D annual mean surface pressure from file (PMEANW) to be
!// used as topography (through PMEAN).
!//
!// Reads netCDF file and interpolates to model resolution.
!//
!// Ole Amund Sovde, March 2016
!//---------------------------------------------------------------------
use cmn_precision, only: r8
use cmn_size, only: IPARW, JPARW, IPAR, JPAR, &
NTRUNW, NMMAX, IDGRD, JDGRD
use cmn_ctm, only: ZDEGI, ZDEGJ, IMAP, JMAP, &
XDGRDW, YDGRDW, XDEDGW, YDEDGW, AREAXYW, AREAXY, &
PMEANG, AREAG
use cmn_met, only: PMEAN, PMEANW
use regridding, only: E_GRID, TRUNG8
use ncutils, only: get_netcdf_var_1d, get_netcdf_var_2d
!//---------------------------------------------------------------------
implicit none
!//---------------------------------------------------------------------
character(len=*), intent(in) :: infile
integer :: I, J, I1, J1
!// Mean surface pressure
real(r8), allocatable :: inPMEAN(:,:),inAREA(:,:), inXDEDG(:),inYDEDG(:)
!//---------------------------------------------------------------------
character(len=*), parameter :: subr='set_mean_psfc'
!//---------------------------------------------------------------------
write(6,'(a)') '--------------------------------------------'// &
'---------------------------'
write(6,'(a)') f90file//':'//subr// &
': Read mean surf press from: '//trim(INFILE)
!// Fetch data from netcdf file
!// lon/lat for interfaces
call get_netcdf_var_1d( trim(infile), 'ilon', inXDEDG )
call get_netcdf_var_1d( trim(infile), 'ilat', inYDEDG )
I1 = size(inXDEDG)-1
J1 = size(inYDEDG)-1
!// surface pressure and area
allocate( inPMEAN(I1, J1), inAREA(I1, J1) )
call get_netcdf_var_2d( trim(infile), 'Pmean', inPMEAN, I1, J1 )
call get_netcdf_var_2d( trim(infile), 'gridarea', inAREA, I1, J1 )
!// Interpolate to native resolution (IPARW,JPARW)
!// Must multiply by area and divide by area after interpolation
inPMEAN(:,:) = inPMEAN(:,:) * inAREA(:,:)
call E_GRID(inPmean,inXDEDG,inYDEDG,I1,J1, &
PMEANW,XDEDGW,YDEDGW,IPARW,JPARW,1)
deallocate( inXDEDG, inYDEDG, inPMEAN, inAREA)
PMEANW(:,:) = PMEANW(:,:) / AREAXYW(:,:)
!// Find PMEAN
call TRUNG8(PMEANW, PMEAN, ZDEGI, ZDEGJ, IMAP, JMAP, IDGRD, &
JDGRD, IPARW, JPARW, IPAR, JPAR, 1, 1)
write(6,'(a,2f12.5)') 'Min/max native mean pressure [hPa]: ', &
minval(pmeanw),maxval(pmeanw)
write(6,'(a,2f12.5)') 'Min/max mean pressure [hPa]: ', &
minval(pmean),maxval(pmean)
!// Mean global surface pressure
PMEANG = 0._r8
do J = 1, JPAR
do I = 1, IPAR
PMEANG = PMEANG + PMEAN(I,J) * AREAXY(I,J)
end do
end do
PMEANG = PMEANG / AREAG
write(6,'(a,f15.8)') 'Global mean surface pressure [hPa]: ',PMEANG
!//---------------------------------------------------------------------
end subroutine set_mean_psfc
!//-----------------------------------------------------------------------
!//-----------------------------------------------------------------------
subroutine LABELG(X,TITLX,NDEX)
!//---------------------------------------------------------------------
!// Generate char*4 titles for lat(NDEX=1), long(NDEX=2) or vert(NDEX=3)
!//---------------------------------------------------------------------
use cmn_precision, only: r8
!//---------------------------------------------------------------------
implicit none
!//---------------------------------------------------------------------
!// Input
real(r8), intent(in) :: X
integer, intent(in) :: NDEX
!// Output
character(len=4), intent(out) :: TITLX
!// Locals
character(len=1), parameter, dimension(2) :: TITLNS =['N','S']
character(len=1), parameter, dimension(2) :: TITLEW =['E','W']
character(len=1), parameter, dimension(12) :: &
TITLNN =[' ','1','2','3','4','5','6','7','8','9','0','-']
real(r8) :: XABS
integer :: I1000,I0100,I0010,I0001,I100,I010,I001,I10,I01,IXABS
!//---------------------------------------------------------------------
!// latitude (NDEX=1)
XABS = abs(X)
if (NDEX .eq. 1) then
IXABS = int(XABS + 0.5_r8)
I10 = IXABS / 10
I01 = IXABS - 10 * I10
if (I01 .eq. 0) I01 = 10
I10 = min(10, I10)
TITLX(1:1) = TITLNN(1)
TITLX(2:2) = TITLNN(I10+1)
TITLX(3:3) = TITLNN(I01+1)
if (X .ge. 0._r8) then
TITLX(4:4) = TITLNS(1)
else
TITLX(4:4) = TITLNS(2)
end if
end if
!// longitude (NDEX=2)
if (NDEX .eq. 2) then
IXABS = int(XABS + 0.5_r8)
I100 = IXABS / 100
I010 = (IXABS - 100 * I100) / 10
I001 = IXABS - 100 * I100 - 10 * I010
if (I100 .gt. 0 .AND. I010 .eq. 0) I010 = 10
if (I001 .eq. 0) I001 = 10
I100 = min(10, I100)
TITLX(1:1) = TITLNN(I100+1)
TITLX(2:2) = TITLNN(I010+1)
TITLX(3:3) = TITLNN(I001+1)
if (X .ge. 0._r8) then
TITLX(4:4) = TITLEW(1)
else
TITLX(4:4) = TITLEW(2)
end if
end if
!// pressure (NDEX=3) - nearest mbar only for now
if (NDEX .eq. 3) then
if (XABS .gt. 1._r8) then
IXABS = int(XABS + 0.5_r8)
I1000 = IXABS / 1000
I0100 = (IXABS - 1000 * I1000) / 100
I0010 = (IXABS - 1000 * I1000 - 100 * I0100) / 10
I0001 = IXABS - 1000 * I1000 - 100 * I0100 - 10 * I0010
I1000 = min(I1000, 10)
if (I1000 .gt. 0 .AND. I0100 .eq. 0) I0100 = 10
if (I0100 .gt. 0 .AND. I0010 .eq. 0) I0010 = 10
if (I0001 .eq. 0) I0001 = 10
TITLX(1:1) = TITLNN(I1000+1)
TITLX(2:2) = TITLNN(I0100+1)
TITLX(3:3) = TITLNN(I0010+1)
TITLX(4:4) = TITLNN(I0001+1)
else
TITLX(1:1) = TITLNN(1)
TITLX(2:2) = TITLNN(1)
TITLX(3:3) = TITLNN(1)
XABS = 10._r8 * XABS
TITLX(3:3) = TITLNN(12)
if(XABS .lt. 1._r8) then
XABS = 10._r8 * XABS
TITLX(2:2) = TITLNN(12)
if(XABS .lt. 1._r8) then
XABS = 10._r8 * XABS
TITLX(1:1) = TITLNN(12)
end if
end if
I0001 = int(XABS + 0.50_r8)
I0001 = min(I0001, 9)
TITLX(4:4) = TITLNN(I0001+1)
end if
end if
!//---------------------------------------------------------------------
end subroutine LABELG
!//-----------------------------------------------------------------------
!//-----------------------------------------------------------------------
subroutine LABELG2(X,TITLX,NDEX)
!//---------------------------------------------------------------------
!// Generate char*4 titles for lat(NDEX=1), long(NDEX=2) or vert(NDEX=3)
!// A bit more elegant?
!//---------------------------------------------------------------------
use cmn_precision, only: r8
!//---------------------------------------------------------------------
implicit none
!//---------------------------------------------------------------------
!// Input
real(r8), intent(in) :: X
integer, intent(in) :: NDEX
!// Output
character(len=4), intent(out) :: TITLX
!// Locals
character(len=1), parameter, dimension(2) :: TITLNS =['N','S']
character(len=1), parameter, dimension(2) :: TITLEW =['E','W']
real(r8) :: XABS
integer :: IABS
!//---------------------------------------------------------------------
!// latitude (NDEX=1)
XABS = abs(X)
if (NDEX .eq. 1) then
write(TITLX(1:3),'(I3)') nint(XABS)
if (X .ge. 0._r8) then
TITLX(4:4) = TITLNS(1)
else
TITLX(4:4) = TITLNS(2)
end if
end if
!// longitude (NDEX=2)
if (NDEX .eq. 2) then
if (X .ge. 0._r8 .and. X .le. 180._r8) then
!// Latitude is E
write(TITLX(1:3),'(I3)') nint(XABS)
TITLX(4:4) = TITLEW(1)
else if (X .gt. 180._r8) then
!// Change to negative for W
write(TITLX(1:3),'(I3)') nint(abs(X - 360._r8))
TITLX(4:4) = TITLEW(2)
else
!// Latitude is W
write(TITLX(1:3),'(I3)') nint(XABS)
TITLX(4:4) = TITLEW(2)
end if
end if
!// pressure (NDEX=3) - nearest mbar only for now
if (NDEX .eq. 3) then
if (XABS .gt. 1._r8) then
write(TITLX(1:4),'(I4)') nint(XABS)
else
!// Altarnative
TITLX(1:3) = ' -'
if(XABS .lt. 1._r8) then
XABS = 10._r8 * XABS
TITLX(2:2) = '-'
if(XABS .lt. 1._r8) then
XABS = 10._r8 * XABS
TITLX(1:1) = '-'
end if
end if
IABS = min(nint(XABS), 9)
write(TITLX(4:4),'(i1)') IABS
end if
end if
!//---------------------------------------------------------------------
end subroutine LABELG2
!//-----------------------------------------------------------------------
!//-----------------------------------------------------------------------
subroutine DIAGBLK(XLNG,YLAT,XDEDG,YDEDG, IBOX,JBOX,IPAR,JPAR,IDTLN)
!//---------------------------------------------------------------------
!// Finds grid box start/end indices for box diagnose.
!//---------------------------------------------------------------------
use cmn_precision, only: r8
!//---------------------------------------------------------------------
implicit none
!//---------------------------------------------------------------------
!// Input
integer, intent(in) :: IPAR, JPAR, IDTLN
real(r8), intent(in) :: XDEDG(IPAR+1), YDEDG(JPAR+1)
!// Input/Output
real(r8), intent(inout) :: XLNG(2), YLAT(2)
!// Output
integer, intent(out) :: IBOX(2), JBOX(2)
!// Locals
integer :: I, J1,J2,I1,I2
!//---------------------------------------------------------------------
YLAT(1) = min(YDEDG(JPAR+1), max(YDEDG(1), YLAT(1)))
YLAT(2) = min(YDEDG(JPAR+1), max(YDEDG(1), YLAT(2)))
J1 = 2
do while (YLAT(1) .gt. YDEDG(J1))
J1 = J1 + 1
end do
J1 = J1 - 1
J2 = J1 + 1
do while (YLAT(2) .gt. YDEDG(J2))
J2 = J2 + 1
end do
J2 = J2 - 1
JBOX(1) = J1
JBOX(2) = J2
!// Note that IDTLN should be the I-box either across the dateline:
!// XDEDG(IDTLN) = +179. & XDEDG(IDTLN+1) = -179.
!// or the first box on the east side of dateline:
!// XDEDG(IDTLN) = +180. & XDEDG(IDTLN+1) = -178.
!// Then, XDEDG(IDTLN+1) is the smallest (most negative) longitude.
!// and XDEDG(IDTLN) is the largest longitude value.
if (XLNG(1) .gt. XDEDG(IDTLN)) XLNG(1) = -180._r8
if (XLNG(2) .gt. XDEDG(IDTLN)) XLNG(2) = -180._r8
I1 = IDTLN + 1
I = mod(I1-1, IPAR) + 1
do while (XLNG(1) .gt. XDEDG(I))
I1 = I1 + 1
I = mod(I1-1, IPAR) + 1
end do
I1 = I1 - 1
I2 = IDTLN + 1
I = mod(I2-1, IPAR) + 1
do while (XLNG(2) .gt. XDEDG(I))
I2 = I2 + 1
I = mod(I2-1, IPAR) + 1
end do
I2 = I2 - 1
!// The sequence I1:I2 is monotonic, but may cross into I2>IPAR
IBOX(1) = mod(I1-1, IPAR) + 1
IBOX(2) = mod(I2-1, IPAR) + 1
if (IBOX(2) .lt. IBOX(1)) IBOX(2) = IPAR + IBOX(2)
!//---------------------------------------------------------------------
end subroutine DIAGBLK
!//-----------------------------------------------------------------------
!//-----------------------------------------------------------------------
subroutine DIAG_LTSTN(XLNG,YLAT,XDEDG,YDEDG,GM0_LT,LTM1,LTM2, &
N,ISTN,JSTN,IPAR,JPAR,IDTLN,NOPSTL,LTSTN)
!//---------------------------------------------------------------------
!// Finds grid box indices for station diagnose.
!//---------------------------------------------------------------------
use cmn_precision, only: r8
!//---------------------------------------------------------------------
implicit none
!//---------------------------------------------------------------------
!// Input
integer, intent(in) :: IPAR, JPAR, IDTLN, NOPSTL, N
real(r8), intent(in) :: XDEDG(IPAR+1),YDEDG(JPAR+1),GM0_LT(IPAR+1)
!// Input/Output
real(r8), intent(inout) :: XLNG, YLAT, LTM1, LTM2
!// Output
integer, intent(out) :: ISTN, JSTN
logical, intent(out) :: LTSTN(NOPSTL)
!// Locals
real(r8) :: DTSTEP, LTGRD1, LTGRD2, DIFT1, DIFT2
integer :: I, J, K, M, II, K1, K2
!//---------------------------------------------------------------------
character(len=*), parameter :: subr='DIAG_LTSTN'
!// --------------------------------------------------------------------
!// check for sign change at dateline (eg, box[1] from +175. to -175.
if (XLNG .gt. XDEDG(IDTLN)) XLNG = -180._r8
II = IDTLN + 1
I = mod(II-1, IPAR) + 1
do while (XLNG .gt. XDEDG(I))
II = II + 1
I = mod(II-1, IPAR) + 1
end do
II = II - 1
ISTN = mod(II-1, IPAR) + 1
YLAT = min(YDEDG(JPAR+1), max(YDEDG(1),YLAT) )
J = 2
do while (YLAT .gt. YDEDG(J))
J = J + 1
end do
JSTN = J - 1
DTSTEP = 24._r8 / real(NOPSTL, r8)
if (LTM2 .lt. LTM1) LTM2 = LTM2 + 24._r8
I = ISTN
K = 0
do M = 1, NOPSTL
LTGRD1 = dmod((real(M-1, r8) * DTSTEP + GM0_LT(I)), 24._r8)
LTGRD2 = dmod((real(M-1, r8) * DTSTEP + GM0_LT(I+1)), 24._r8)
if (LTGRD2 .lt. LTGRD1) LTGRD2 = LTGRD2 + 24._r8
if (LTM1 .le. LTGRD1 .and. LTGRD2 .le. LTM2) then
LTSTN(M) = .true.
K = K + 1
!//write(6,'(A,5I5,2(3X,2f6.2),F9.2)') 'NOPSTL/N/K/I/M', &
!// NOPSTL,N,K,I,M,LTM1,LTM2,LTGRD1,LTGRD2,XLNG
else if (LTM2 .ge. 24._r8) then
if (LTM1 .le. LTGRD1 + 24._r8 .and. LTGRD2 + 24._r8 .le. LTM2) then
LTSTN(M) = .true.
K = K + 1
!//write(6,'(A,5I5,2(3X,2f6.2),F9.2)') 'NOPSTL/N/K/I/M', &
!// NOPSTL,N,K,I,M,LTM1,LTM2,LTGRD1,LTGRD2,XLNG
else
LTSTN(M) = .false.
end if
else
LTSTN(M) = .false.
end if
end do
K2 = 0
if (K .eq. 0) then
do M = 1, NOPSTL
K1 = M + 1
LTGRD1 = dmod((real(M-1, r8) * DTSTEP + GM0_LT(I+1)), 24._r8)
LTGRD2 = dmod((real(K1-1, r8) * DTSTEP + GM0_LT(I)), 24._r8)
if (LTGRD2 .lt. LTGRD1) LTGRD2 = LTGRD2 + 24._r8
if (LTGRD1 .le. LTM1 .and. LTM2 .le. LTGRD2) then
K2 = M
DIFT1 = LTM1 - LTGRD1
DIFT2 = LTGRD2 - LTM2
end if
end do
if (DIFT2 .gt. DIFT1) then
write(6,'(A,I4,A,2F6.2,A,2F6.2)') 'STN', N, &
' Local time diag. interval is changed from ', &
LTM1,LTM2,' to ', &
dmod((real(K2-1, r8) * DTSTEP + GM0_LT(I)), 24._r8), LTM2
LTSTN(K2) = .true.
LTM1 = dmod((real(K2-1, r8) * DTSTEP + GM0_LT(I)), 24._r8)
else
write(6,'(A,I4,A,2F6.2,A,2F6.2)') 'STN', N, &
' Local time diag. interval is changed from ', &
LTM1,LTM2,' to ', &
LTM1,dmod((real(K2, r8) * DTSTEP + GM0_LT(I+1)), 24._r8)
LTSTN(K2+1) = .true.
LTM2 = dmod((real(K2, r8) * DTSTEP + GM0_LT(I+1)), 24._r8)
end if
end if
!// write(6,'(I2,24(1X,4L1))') N,(LTSTN(K),K=1,NOPSTL)
if (K .eq. 0 .and. K2 .eq. 0) then
write(6,'(A,I5)') f90file//':'//subr// &
': check local time diag. interval at STN',N
write(6,'(A)') '*** job killed ***'
stop
end if
!//---------------------------------------------------------------------
end subroutine DIAG_LTSTN