-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp_vars.F90
719 lines (591 loc) · 23.1 KB
/
interp_vars.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
!----------------------------------------------------------------------------------------
subroutine interpolation(grid, flnm, nalt, dz)
use netcdf
use module_grid
implicit none
type(gfsgrid), intent(inout) :: grid
character(len=*), intent(in) :: flnm
integer, intent(in) :: nalt
real, intent(in) :: dz
integer :: i, rc
print *, 'Enter interpolation'
print *, 'flnm = ', trim(flnm)
grid%nalt = nalt
allocate(grid%alt(grid%nalt))
do i = 1, grid%nalt
grid%alt(i) = real(i-1)*dz
end do
call create_coord(grid, flnm)
call create_var_attr(grid)
!End define mode.
rc = nf90_enddef(grid%ncid)
if(rc /= nf90_noerr) then
write(unit=0, fmt='(a,i6,a)') "Problem to enddef ncid: <", grid%ncid, ">."
write(unit=0, fmt='(2a)') "Error status: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
end if
print *, 'before process'
call process(grid)
print *, 'after process'
print *, 'Leave interpolation'
end subroutine interpolation
!---------------------------------------------------------------------------
subroutine create_global_attr(ncid, filename, title)
implicit none
integer, intent(in) :: ncid
character(len = *), intent(in) :: filename, title
!put global attributes
call nc_putGlobalCharAttr(ncid, 'filename', trim(filename))
call nc_putGlobalCharAttr(ncid, 'title', trim(title))
end subroutine create_global_attr
!----------------------------------------------------------------------------------------
subroutine create_coord(grid, flnm)
use netcdf
use module_grid
implicit none
type(gfsgrid), intent(inout) :: grid
character(len=*), intent(in) :: flnm
integer :: i, nd, rc, ncid
integer, dimension(2) :: dimids
logical :: fileExists
!print *, 'Enter create_coord'
!print *, 'flnm = ', trim(flnm)
!print *, 'grid%nlon = ', grid%nlon
!print *, 'grid%nlat = ', grid%nlat
!print *, 'grid%nalt = ', grid%nalt
!print *, 'grid%npth = ', grid%npth
!print *, 'grid%npruv = ', grid%npruv
!print *, 'grid%lon = ', grid%lon
!print *, 'grid%lat = ', grid%lat
!print *, 'grid%alt = ', grid%alt
!print *, 'grid%pth = ', grid%pth
!print *, 'grid%pruv = ', grid%pruv
rc = nf90_noerr
grid%output_flnm = trim(flnm)
!Create the file.
inquire(file=trim(flnm), exist=fileExists)
if (fileExists) then
open(unit=1234, iostat=rc, file=trim(flnm), status='old')
if(rc == 0) close(1234, status='delete')
end if
rc = nf90_create(trim(flnm), NF90_NETCDF4, ncid)
call check_status(rc)
grid%ncid = ncid
!print *, 'ncid = ', ncid
rc = nf90_def_dim(ncid, 'lon', grid%nlon, grid%dimidlon)
call check_status(rc)
rc = nf90_def_dim(ncid, 'lat', grid%nlat, grid%dimidlat)
call check_status(rc)
rc = nf90_def_dim(ncid, 'alt', grid%nalt, grid%dimidalt)
call check_status(rc)
call create_global_attr(ncid, flnm, 'Data in Height level')
dimids(1) = grid%dimidlon
nd = 1
!--Field lon
call nc_putAxisAttr(ncid, nd, dimids, NF90_REAL, &
'lon', &
"Lontitude Coordinate", &
"degree_east", &
"Longitude" )
dimids(1) = grid%dimidlat
!--Field lat
call nc_putAxisAttr(ncid, nd, dimids, NF90_REAL, &
'lat', &
"Latitude Coordinate", &
"degree_north", &
"Latitude" )
dimids(1) = grid%dimidalt
!--Field alt
call nc_putAxisAttr(ncid, nd, dimids, NF90_REAL, &
'alt', &
"Altitude Coordinate", &
"m", &
"Altitude" )
!write lon
call nc_put1Dvar0(ncid, 'lon', grid%lon, 1, grid%nlon)
!write lat
call nc_put1Dvar0(ncid, 'lat', grid%lat, 1, grid%nlat)
!write alt
call nc_put1Dvar0(ncid, 'alt', grid%alt, 1, grid%nalt)
!print *, 'Leave create_coord'
end subroutine create_coord
!-------------------------------------------------------------------------------------
subroutine create_var_attr(grid)
use netcdf
use module_grid
implicit none
type(gfsgrid), intent(inout) :: grid
integer, dimension(6) :: dimids, chunksize
integer :: rc, nd, i
integer :: missing_int
real :: missing_real
character(len=80) :: long_name, units, coordinates
print *, 'Enter create_var_attr'
missing_real = -1.0e38
missing_int = -999999
print *, 'grid%nVars = ', grid%nVars
chunksize(1) = grid%nlon
chunksize(2) = grid%nlat
chunksize(3) = grid%nalt
do i = 1, grid%nVars
if(grid%vars(i)%nDims < 2) then
cycle
end if
long_name = trim(grid%vars(i)%varname)
units = 'unknown'
coordinates = 'alt lat lon'
dimids(1) = grid%dimidlon
dimids(2) = grid%dimidlat
dimids(3) = grid%dimidalt
nd = 3
if('TMP_P0_L1_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Ground or water surface temperature'
units = 'K'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'TSK', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('TMP_P0_L102_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Sea Level Air Temperature'
units = 'K'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'TSL', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('PWAT_P0_L200_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Precipitable water'
units = 'kg m-2'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'PW', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('PRATE_P0_L1_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Precipitation rate'
units = 'kg m-2 s-1'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'PRATE', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('PRMSL_P0_L101_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Mean sea level'
units = 'Pa'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'SLP', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('HGT_P0_L1_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Terrain Height'
units = 'gpm'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'TER', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('TMP_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'Temperature'
units = 'K'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'T', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('HGT_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'Pressure'
units = 'Pa'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'P', trim(long_name), trim(units), &
trim(coordinates), missing_real)
rc = nf90_get_var(grid%fileid, grid%varids(i), grid%hgt)
call check_status(rc)
!call hgt2z(grid)
else if('SPFH_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'Specific humidity'
units = 'kg kg-1'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'Q', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('RH_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'Relative humidity'
units = '%'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'RH', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('UGRD_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'U-component of wind'
units = 'm s-1'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'U', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('VGRD_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'V-component of wind'
units = 'm s-1'
call nc_putAttrWithChunking(grid%ncid, nd, dimids, chunksize, NF90_REAL, &
'V', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else
cycle
end if
print *, 'Var No. ', i, ': name: ', trim(grid%vars(i)%varname)
end do
print *, 'Leave create_var_attr'
end subroutine create_var_attr
!-------------------------------------------------------------------------------------
subroutine create_var_attr_nochunking(grid)
use netcdf
use module_grid
implicit none
type(gfsgrid), intent(inout) :: grid
integer, dimension(6) :: dimids
integer :: rc, nd, i
integer :: missing_int
real :: missing_real
character(len=80) :: long_name, units, coordinates
print *, 'Enter create_var_attr'
missing_real = -1.0e38
missing_int = -999999
print *, 'grid%nVars = ', grid%nVars
do i = 1, grid%nVars
if(grid%vars(i)%nDims < 2) then
cycle
end if
long_name = trim(grid%vars(i)%varname)
units = 'unknown'
coordinates = 'alt lat lon'
dimids(1) = grid%dimidlon
dimids(2) = grid%dimidlat
dimids(3) = grid%dimidalt
nd = 3
if('TMP_P0_L1_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Ground or water surface temperature'
units = 'K'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'TSK', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('TMP_P0_L102_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Sea Level Air Temperature'
units = 'K'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'TSL', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('PWAT_P0_L200_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Precipitable water'
units = 'kg m-2'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'PW', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('PRATE_P0_L1_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Precipitation rate'
units = 'kg m-2 s-1'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'PRATE', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('PRMSL_P0_L101_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Mean sea level'
units = 'Pa'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'SLP', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('HGT_P0_L1_GLL0' == trim(grid%vars(i)%varname)) then
coordinates = 'lat lon'
nd = 2
long_name = 'Terrain Height'
units = 'gpm'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'TER', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('TMP_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'Temperature'
units = 'K'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'T', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('HGT_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'Pressure'
units = 'Pa'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'P', trim(long_name), trim(units), &
trim(coordinates), missing_real)
rc = nf90_get_var(grid%fileid, grid%varids(i), grid%hgt)
call check_status(rc)
!call hgt2z(grid)
else if('SPFH_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'Specific humidity'
units = 'kg kg-1'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'Q', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('RH_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'Relative humidity'
units = '%'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'RH', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('UGRD_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'U-component of wind'
units = 'm s-1'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'U', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else if('VGRD_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
long_name = 'V-component of wind'
units = 'm s-1'
call nc_putAttr(grid%ncid, nd, dimids, NF90_REAL, &
'V', trim(long_name), trim(units), &
trim(coordinates), missing_real)
else
cycle
end if
print *, 'Var No. ', i, ': name: ', trim(grid%vars(i)%varname)
end do
print *, 'Leave create_var_attr'
end subroutine create_var_attr_nochunking
!----------------------------------------------------------------------------------------
subroutine process(grid)
use netcdf
use module_grid
implicit none
type(gfsgrid), intent(inout) :: grid
integer :: i, n, rc
real, dimension(:,:), allocatable :: var2d
real, dimension(:,:,:), allocatable :: var3d
print *, 'Enter process'
allocate(var2d(grid%nlon, grid%nlat))
allocate(var3d(grid%nlon, grid%nlat, grid%nalt))
do i = 1, grid%nVars
if(grid%vars(i)%nDims < 2) cycle
rc = nf90_inquire_variable(grid%fileid, grid%varids(i), &
name=grid%vars(i)%varname)
call check_status(rc)
if('TMP_P0_L1_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), var2d)
call check_status(rc)
call flip(grid, var2d)
call nc_put2Dvar0(grid%ncid, 'TSK', &
var2d, 1, grid%nlon, 1, grid%nlat)
else if('TMP_P0_L102_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), var2d)
call check_status(rc)
call flip(grid, var2d)
call nc_put2Dvar0(grid%ncid, 'TSL', &
var2d, 1, grid%nlon, 1, grid%nlat)
else if('PWAT_P0_L200_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), var2d)
call check_status(rc)
call flip(grid, var2d)
call nc_put2Dvar0(grid%ncid, 'PW', &
var2d, 1, grid%nlon, 1, grid%nlat)
else if('PRATE_P0_L1_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), var2d)
call check_status(rc)
call flip(grid, var2d)
call nc_put2Dvar0(grid%ncid, 'PRATE', &
var2d, 1, grid%nlon, 1, grid%nlat)
else if('PRMSL_P0_L101_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), var2d)
call check_status(rc)
call flip(grid, var2d)
call nc_put2Dvar0(grid%ncid, 'SLP', &
var2d, 1, grid%nlon, 1, grid%nlat)
else if('HGT_P0_L1_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), var2d)
call check_status(rc)
call flip(grid, var2d)
call nc_put2Dvar0(grid%ncid, 'TER', &
var2d, 1, grid%nlon, 1, grid%nlat)
else if('HGT_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
call z2p(grid, var3d)
call nc_put3Dvar0(grid%ncid, 'P', &
var3d, 1, grid%nlon, 1, grid%nlat, 1, grid%nalt)
else if('TMP_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), grid%var3dt)
call check_status(rc)
call t2z(grid, var3d)
call nc_put3Dvar0(grid%ncid, 'T', &
var3d, 1, grid%nlon, 1, grid%nlat, 1, grid%nalt)
else if('SPFH_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), grid%var3dt)
call check_status(rc)
call t2z(grid, var3d)
call nc_put3Dvar0(grid%ncid, 'Q', &
var3d, 1, grid%nlon, 1, grid%nlat, 1, grid%nalt)
else if('RH_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), grid%var3du)
call check_status(rc)
call u2z(grid, var3d)
call nc_put3Dvar0(grid%ncid, 'RH', &
var3d, 1, grid%nlon, 1, grid%nlat, 1, grid%nalt)
else if('UGRD_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), grid%var3du)
call check_status(rc)
call u2z(grid, var3d)
call nc_put3Dvar0(grid%ncid, 'U', &
var3d, 1, grid%nlon, 1, grid%nlat, 1, grid%nalt)
else if('VGRD_P0_L100_GLL0' == trim(grid%vars(i)%varname)) then
rc = nf90_get_var(grid%fileid, grid%varids(i), grid%var3du)
call check_status(rc)
call u2z(grid, var3d)
call nc_put3Dvar0(grid%ncid, 'V', &
var3d, 1, grid%nlon, 1, grid%nlat, 1, grid%nalt)
else
cycle
end if
print *, 'Var No. ', i, ': name: ', trim(grid%vars(i)%varname)
end do
deallocate(var2d)
deallocate(var3d)
print *, 'Leave process'
end subroutine process
!----------------------------------------------------------------------
subroutine hgt2z(grid)
use module_grid
implicit none
type(gfsgrid), intent(inout) :: grid
integer :: i, j, k
do k = 1, grid%npth
do j = 1, grid%nlat
do i = 1, grid%nlon
grid%hgt(i,j,k) = grid%hgt(i,j,k)/9.8
end do
end do
end do
end subroutine hgt2z
!----------------------------------------------------------------------
subroutine z2p(grid, var3d)
use module_grid
implicit none
type(gfsgrid), intent(in) :: grid
real, dimension(grid%nlon, grid%nlat, grid%nalt), intent(out) :: var3d
integer :: i, j, k, n, jj
real :: d, v
do j = 1, grid%nlat
jj = grid%nlat + 1 - j
do i = 1, grid%nlon
n = grid%npth - 1
do k = 1, grid%nalt
if(grid%alt(k) <= grid%hgt(i,j,grid%npth)) then
d = (grid%alt(k) - grid%hgt(i,j,grid%npth)) &
/ (grid%hgt(i,j,grid%npth-1) - grid%hgt(i,j,grid%npth))
v = d*grid%pth(grid%npth-1) + (1.0-d)*grid%pth(grid%npth)
else if(grid%alt(k) >= grid%hgt(i,j,1)) then
d = (grid%alt(k) - grid%hgt(i,j,2)) &
/ (grid%hgt(i,j,1) - grid%hgt(i,j,2))
v = d*grid%pth(1) + (1.0-d)*grid%pth(2)
else
do while (n > 1)
if(grid%alt(k) <= grid%hgt(i,j,n)) then
d = (grid%alt(k) - grid%hgt(i,j,n+1)) &
/ (grid%hgt(i,j,n) - grid%hgt(i,j,n+1))
v = d*grid%pth(n) + (1.0-d)*grid%pth(n+1)
exit
end if
n = n - 1
end do
end if
var3d(i, jj, k) = v
end do
end do
end do
end subroutine z2p
!----------------------------------------------------------------------
subroutine t2z(grid, var3d)
use module_grid
implicit none
type(gfsgrid), intent(in) :: grid
real, dimension(grid%nlon, grid%nlat, grid%nalt), intent(out) :: var3d
integer :: i, j, k, n, jj
real :: d, v
do j = 1, grid%nlat
jj = grid%nlat + 1 - j
do i = 1, grid%nlon
n = grid%npth - 1
do k = 1, grid%nalt
if(grid%alt(k) <= grid%hgt(i,j,grid%npth)) then
v = grid%var3dt(i, j, grid%npth)
else if(grid%alt(k) >= grid%hgt(i,j,1)) then
v = grid%var3dt(i, j, 1)
else
do while (n >= 1)
if(grid%alt(k) <= grid%hgt(i,j,n)) then
d = (grid%alt(k) - grid%hgt(i,j,n+1)) &
/ (grid%hgt(i,j,n) - grid%hgt(i,j,n+1))
v = d*grid%var3dt(i, j, n) &
+ (1.0-d)*grid%var3dt(i, j, n+1)
exit
end if
n = n - 1
end do
end if
var3d(i, jj, k) = v
end do
end do
end do
end subroutine t2z
!----------------------------------------------------------------------
subroutine u2z(grid, var3d)
use module_grid
implicit none
type(gfsgrid), intent(in) :: grid
real, dimension(grid%nlon, grid%nlat, grid%nalt), intent(out) :: var3d
integer :: i, j, k, n, jj, nn, nd
real :: d, v
nd = grid%npth - grid%npruv
do j = 1, grid%nlat
jj = grid%nlat + 1 - j
do i = 1, grid%nlon
n = grid%npth - 1
do k = 1, grid%nalt
if(grid%alt(k) <= grid%hgt(i,j,grid%npth)) then
v = grid%var3du(i, j, grid%npruv)
else if(grid%alt(k) >= grid%hgt(i,j,nd+1)) then
v = grid%var3du(i, j, 1)
else
do while (n >= 1)
if(grid%alt(k) <= grid%hgt(i,j,n)) then
nn = n - nd
d = (grid%alt(k) - grid%hgt(i,j,n+1)) &
/ (grid%hgt(i,j,n) - grid%hgt(i,j,n+1))
v = d*grid%var3du(i, j, nn) &
+ (1.0-d)*grid%var3du(i, j, nn+1)
exit
end if
n = n - 1
end do
end if
var3d(i, jj, k) = v
end do
end do
end do
end subroutine u2z
!----------------------------------------------------------------------
subroutine flip(grid, var2d)
use module_grid
implicit none
type(gfsgrid), intent(in) :: grid
real, dimension(grid%nlon, grid%nlat), intent(inout) :: var2d
integer :: i, j, jj, n
real :: v
n = grid%nlat/2
do j = 1, n
jj = grid%nlat + 1 - j
do i = 1, grid%nlon
v = var2d(i, j)
var2d(i, j) = var2d(i, jj)
var2d(i, jj) = v
end do
end do
end subroutine flip