forked from NOAA-EMC/gfs-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregdiag.f
1432 lines (1302 loc) · 45.2 KB
/
regdiag.f
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
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
! !
! This program reads ocean and ice data on lat/lon grid !
! It contains 4 subroutines: diag, read_ice, read_ocn !
! !
! Xingren Wu (Xingren.Wu@noaa.gov) !
! May 18, 2007 !
! Xingren Wu !
! Nov 16, 2007 - modified !
! fix kpds(13) to kpds(15) for handle different forecast time unit !
! Xingren Wu !
! Dec 4, 2007 - modified !
! add extra fields to match production output !
! Xingren Wu !
! Sep 7, 2016 - modified !
! add cice !
! Xingren Wu !
! Feb 10, 2017 - modified !
! writing grib2 !
! Christopher Melhauser !
! Sep 8, 2017 - overhauled code for latlon --> grb2 only !
! Xingren Wu !
! Oct 23, 2017 - Bug/fix !
! Suranjana Saha !
! Nov 8, 2017 - Added new variables in the MOM6 NetCDF file to grib2 !
! !
! Xingren Wu !
! Nov 22, 2017 - Fixed the following !
! 1. Ice concentration !
! 2. Ice thickness !
! 3. Snow depth !
! 4. Surface Temperature over Water and Ice !
! 5. u-component of ice drift !
! 6. v-component of ice drift !
! Mar 30, 2018 - Bug fix !
! !
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
! Notes: !
! 1. uflx, vflx, lhtfl and shtfl are opposite in sign to data in the flx file !
! 2. nlwrs=net lw radiation at sfc i.e dlwrfavesfc-ulwrfavesfc flx file !
! 3. nswrs=net sw radiation at sfc i.e. dswrfavesfc-uswrfavesfc in flx file !
! 4. sfc_hflux (THFLX) = Total net heat, i.e. !
! dswrfavesfc-uswrfavesfc+dlwrfavesfc-ulwrfavesfc-lhtflsfc-shtflsfc in flx !
! 5. evp=evaporation i.e -lhtflsfc*0.03456 in flx file !
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
! Note that the input NetCDF and output grib2 files have the following: !
! z=1 is topmost level; z=40 is bottom most level !
! even though grads control file puts z=1 at the bottom and z=40 at the top !
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
module regdiag_mod
use wgrib2api
implicit none
contains
subroutine read_ocn(im,jm,km,lon,lat, &
t,s,u,v,eta,sfcflx,pme,mld,taux,tauy,ocean_file, &
sss,ssu,ssv,speed,sensible,latent,sw,lw,lprec,evap)
include "netcdf.inc"
integer im,jm,km,i,j,k
integer status, ncid
integer :: id_lat,id_lon, &
id_temp,id_salt,id_u,id_v,id_wt, &
id_eta_t,id_sfc_hflux,id_pme,id_mld,id_tau_x,id_tau_y, &
id_sss,id_ssu,id_ssv,id_speed, &
id_sensible,id_latent,id_sw,id_lw,id_lprec,id_evap
real, dimension(im,jm,km) :: t,s,u,v
real, dimension(im,jm) :: eta,sfcflx,pme,mld,taux,tauy,LwLatSens
real, dimension(im,jm) :: sss,ssu,ssv,speed,sensible,latent,sw,lw
real, dimension(im,jm) :: lprec,evap
real, dimension(im) :: lon
real, dimension(jm) :: lat
real, dimension(km,jm,im) :: tmp3D
real, dimension(jm,im) :: tmp2D
character(len=300) :: ocean_file
real, parameter :: c2k=273.15
real :: undef
undef=-1.0E+34
status = nf_open(ocean_file, NF_NOWRITE, ncid)
if(status.ne.NF_NOERR) stop 'cannot open ocean_file'
status = nf_inq_varid(ncid, 'temp' , id_temp)
status = nf_inq_varid(ncid, 'so' , id_salt)
status = nf_inq_varid(ncid, 'uo' , id_u )
status = nf_inq_varid(ncid, 'vo' , id_v )
status = nf_inq_varid(ncid, 'SSH' , id_eta_t)
status = nf_inq_varid(ncid, 'LwLatSens',id_sfc_hflux)
status = nf_inq_varid(ncid, 'Heat_PmE', id_pme)
status = nf_inq_varid(ncid, 'mld' , id_mld)
status = nf_inq_varid(ncid, 'taux' , id_tau_x)
status = nf_inq_varid(ncid, 'tauy' , id_tau_y)
status = nf_inq_varid(ncid, 'lon' , id_lon)
status = nf_inq_varid(ncid, 'lat' , id_lat)
status = nf_inq_varid(ncid, 'SSS' , id_sss)
status = nf_inq_varid(ncid, 'SSU' , id_ssu)
status = nf_inq_varid(ncid, 'SSV' , id_ssv)
status = nf_inq_varid(ncid, 'speed' , id_speed)
status = nf_inq_varid(ncid, 'sensible', id_sensible)
status = nf_inq_varid(ncid, 'latent' , id_latent)
status = nf_inq_varid(ncid, 'SW' , id_sw)
status = nf_inq_varid(ncid, 'LW' , id_lw)
status = nf_inq_varid(ncid, 'lprec' , id_lprec)
status = nf_inq_varid(ncid, 'evap' , id_evap)
! read variable
status = nf_get_var_real(ncid, id_lon, lon)
status = nf_get_var_real(ncid, id_lat, lat)
status = nf_get_var_real(ncid, id_temp, t) !temp
status = nf_get_var_real(ncid, id_salt, s) !salt
status = nf_get_var_real(ncid, id_u, u) !u
status = nf_get_var_real(ncid, id_v, v) !v
status = nf_get_var_real(ncid, id_eta_t, eta) !eta_t
status = nf_get_var_real(ncid, id_sfc_hflux, LwLatSens) !sfc_hflux
status = nf_get_var_real(ncid, id_pme, pme) !pme
status = nf_get_var_real(ncid, id_mld, mld) !mld
status = nf_get_var_real(ncid, id_tau_x, taux) !tau_x
status = nf_get_var_real(ncid, id_tau_y, tauy) !tau_y
status = nf_get_var_real(ncid, id_sss, sss) !sss
status = nf_get_var_real(ncid, id_ssu, ssu) !ssu
status = nf_get_var_real(ncid, id_ssv, ssv) !ssv
status = nf_get_var_real(ncid, id_speed, speed) !speed
status = nf_get_var_real(ncid, id_sensible, sensible) !sensible
status = nf_get_var_real(ncid, id_latent, latent) !latent
status = nf_get_var_real(ncid, id_sw, sw) !sw
status = nf_get_var_real(ncid, id_lw, lw) !lw
status = nf_get_var_real(ncid, id_lprec, lprec) !lprec
status = nf_get_var_real(ncid, id_evap, evap) !evap
! status = nf_get_var_real(ncid, id_fprec, fprec) !fprec
! status = nf_get_var_real(ncid, id_lrunoff, lrunoff) !lrunoff
! status = nf_get_var_real(ncid, id_frunoff, frunoff) !frunoff
sfcflx=SW+LwLatSens
! convert all temps in Celcius that are read in to Kelvin below..
t=t+c2k
return
end subroutine read_ocn
subroutine flip_ijk(im,jm,km,datain,dataout)
implicit none
integer :: im,jm,km,i,j,k
real, intent(out), dimension(im,jm,km) :: dataout
real, intent(in), dimension(km,jm,im) :: datain
do k=1,km
do j=1,jm
do i=1,im
dataout(i,j,k)=datain(k,j,i)
enddo
enddo
enddo
return
end subroutine flip_ijk
subroutine flip_ij(im,jm,datain,dataout)
implicit none
integer :: im,jm,i,j
real, intent(out), dimension(im,jm) :: dataout
real, intent(in), dimension(jm,im) :: datain
do j=1,jm
do i=1,im
dataout(i,j)=datain(j,i)
enddo
enddo
return
end subroutine flip_ij
subroutine set_undef(im,jm,spv,undef,data)
implicit none
integer :: im,jm,i,j
real :: spv,undef
real, intent(inout), dimension(im,jm) :: data
do j=1,jm
do i=1,im
if (data(i,j) .GE. spv) data(i,j)=undef
enddo
enddo
return
end subroutine set_undef
subroutine read_ice(im,jm,hi_h,hs_h,aice_h,ts_h,uvel_h,vvel_h,icefile)
include "netcdf.inc"
character(len=300) :: icefile
integer :: status,ncid
integer :: i,j
integer :: im,jm
integer :: id_hi_h,id_hs_h,id_tsfc_h,id_aice_h,id_sst_h,id_uvel_h,id_vvel_h
real :: spv_ci,undef
real, dimension(im,jm) :: hi_h,hs_h,Tsfc_h,aice_h,sst_h,ts_h,uvel_h,vvel_h,fi,fw
spv_ci=1.0e+30
undef=-1.0E+34
print *, 'icefile:', icefile
status = nf_open(icefile, NF_NOWRITE, ncid)
print *, 'status:', status
if(status.ne.NF_NOERR) stop 'cannot open ice_file'
status = nf_inq_varid(ncid, 'hi_h ', id_hi_h)
status = nf_inq_varid(ncid, 'hs_h ', id_hs_h)
status = nf_inq_varid(ncid, 'Tsfc_h ', id_tsfc_h)
status = nf_inq_varid(ncid, 'aice_h ', id_aice_h)
status = nf_inq_varid(ncid, 'sst_h ', id_sst_h)
status = nf_inq_varid(ncid, 'uvel_h ', id_uvel_h)
status = nf_inq_varid(ncid, 'vvel_h ', id_vvel_h)
status = nf_get_var_real(ncid, id_hi_h ,hi_h)
status = nf_get_var_real(ncid, id_hs_h ,hs_h)
status = nf_get_var_real(ncid, id_tsfc_h ,Tsfc_h)
status = nf_get_var_real(ncid, id_aice_h ,aice_h)
status = nf_get_var_real(ncid, id_sst_h ,sst_h)
status = nf_get_var_real(ncid, id_uvel_h ,uvel_h)
status = nf_get_var_real(ncid, id_vvel_h ,vvel_h)
do j=1,jm
do i=1,im
if (aice_h(i,j) < spv_ci) then
if (aice_h(i,j) > 1.0) then
print *,'Warning: aice_h>1:',aice_h(i,j)
aice_h(i,j)=1.0
endif
fi(i,j)=aice_h(i,j)
else
fi(i,j)=0.0
endif
fw=1.0-fi(i,j)
ts_h(i,j)=fw(i,j)*sst_h(i,j)+fi(i,j)*Tsfc_h(i,j)
enddo
enddo
return
end subroutine read_ice
subroutine diag(im,jm,km,icefile,ocnfile, &
isyr,ismth,isday,ishr,iyr,imth,iday,ihr,mfh,mfhout, &
flonw,flone,dlon,flatn,flats,dlat,imo,jmo, &
outfile,mfcstcpl,igenocnp)
integer nvar,ko,ki,kk,ndtc,mfcstcpl,igenocnp
! integer mkmoc,nreg
real hfc
parameter(nvar=37,ko=40,ki=5)
parameter(ndtc=7)
!
character*300 cicefile,icefile,ocnfile,outfile
! character*120 mocfile
character*120 template_file,template_inv,metadata,template_var
character*10 datecode
character*4 level4
character*8 hr8
character*80 levelm(nvar)
character*80 levelcode,ftime
character*5 varcode(nvar)
integer im,jm,km
integer imo,jmo
integer iyr,imth,iday,ihr,mfh,mfhout
integer isyr,ismth,isday,ishr
integer hrdif
integer isdate(8),iedate(8)
real dlat,dlon,flats,flatn,flonw,flone
! real tripolat,dtripolat
! integer jtripolat
real factor,undef,spv_ci,spv_pme,spv_tau,val
integer i,j,k,ierr,ilev,ind,iret,nv,ndata,nr,nundef,kreg
integer nx,ny
real datedif(5)
!
real*8, dimension(im,jm) :: hi,hs,ts,t1,t2,fi,alb,ui,vi,sst,saltf
!
real, dimension(im,jm) :: hi_ci,hs_ci,ts_ci,fi_ci,ui_ci,vi_ci
!
real, dimension(im,jm,km) :: t,s,u,v,w,vv
! real, dimension(im,jm,km) :: dckt,dcks,vfc
real, dimension(im,jm) :: eta,sfcflx,pme,mld,taux,tauy,uice,vice
real, dimension(im,jm) :: sss,ssu,ssv,speed,sensible,latent,sw,lw
real, dimension(im,jm) :: lprec,evap
real, dimension(im,jm) :: varice
real, dimension(im) :: lon
real, dimension(jm) :: lat
!
real, dimension(imo,jmo) :: grid,grdtmp,varsfc
real, dimension(imo,jmo,km) :: varocn,tocn,socn
real zt(ko)
real zw(ko)
real dtc(ndtc)
real grid2(imo*jmo)
!
real, dimension(nvar) :: fac
integer, dimension(nvar) :: kpds5,kpds6,kpds7,kpds22
integer, dimension(ko) :: levs
integer, parameter :: kpds_dim=200
integer, dimension(kpds_dim) :: KPDS,KGDS,JPDS,JGDS
logical*1 lbms(imo,jmo)
logical :: climate = .false.
! new wgrib2api requires this
integer, parameter :: regex=1
!
data dtc/2.5,5.,10.,15.,20.,25.,28./
!
! NV kpds5=Variable (Parameter Table)
! http://www.nco.ncep.noaa.gov/pmb/docs/on388/table2.html#TABLE128
!
! 1. 13=Potential temperature (2)
! 2. 88=Salinity (2)
! 3. 49=u-component of current (2)
! 4. 50=v-component of current (2)
! 5. 40=Geometric Vertical velocity (2)
! 6. 124=Momentum flux, u component (2)
! 7. 125=Momentum flux, v component (2)
! 8. 198=Sea Surface Height Relative to Geoid (129)
! 9. 91=Ice concentration (2)
! 10. 92=Ice thickness (2)
! 11. 66=Snow depth (2)
! 12. 11=Surface Temperature over Water and Ice(2)
! 13. 95=u-component of ice drift (2)
! 14. 96=v-component of ice drift (2)
! 15. 188=Evaporation - Precipitation (2)
! 16. 202=Total downward heat flux at surface (downward is positive) (129)
! 17. 195=Geometric Depth Below Sea Surface (129)
! 18. 195=Geometric Depth Below Sea Surface (129)
! 19. 197=Ocean Heat Content (129)
! 20. 194=Tropical Cyclone Heat Potential (129)
! 21. 195=Geometric Depth Below Sea Surface for the 2.5C isotherm (129)
! 22. 195=Geometric Depth Below Sea Surface for the 5C isotherm (129)
! 23. 195=Geometric Depth Below Sea Surface for the 10C isotherm (129)
! 24. 195=Geometric Depth Below Sea Surface for the 15C isotherm (129)
! 25. 195=Geometric Depth Below Sea Surface for the 20C isotherm (129)
! 26. 195=Geometric Depth Below Sea Surface for the 25C isotherm (129)
! 27. 195=Geometric Depth Below Sea Surface for the 28C isotherm (129)
! 28. 88=Sea Surface Salinity (2)
! 29. 49=Sea Surface u-current (2)
! 30. 50=Sea Surface v-current (2)
! 31. 32=Sea Surface speed (2)
! 32. 122=Sensible Heat (2)
! 33. 121=Latent Heat (2)
! 34. 111=Net surface Downward Short Wave flux (2)
! 35. 112=Net surface Downward Long Wave flux (2)
! 36. 59=Precipitation (2)
! 37. 57=Evaporation (2)
!
data kpds5/ 13, 88, 49, 50, 40,124,125,198, 91, 92, &
66, 11, 95, 96,188,202,195,195,197,194, &
195,195,195,195,195,195,195, 88, 49, 50, &
32,122,121,111,112, 59, 57/
data kpds6/ 160,160,160,160,160, 1, 1, 1, 1, 1, &
1, 1, 1, 1, 1, 1,237,238,236,239, &
235,235,235,235,235,235,235, 1, 1, 1, &
1, 1, 1, 1, 1, 1, 1/
data kpds7/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &
0, 0, 0, 0, 0, 0, 0, 0, 30,260, &
25, 50,100,150,200,250,280, 0, 0, 0, &
0, 0, 0, 0, 0, 0, 0/
!-------------------------------------------------------------------
! Xingren: check why fac=273.15 for Ice temperature
! check why fac=-8.64e6 for evap minus precip
!-------------------------------------------------------------------
data fac/ 0.0, 0.001,1.0,1.0, 1.0, 1.0, 1.0,1.0,1.0,1.0, &
1.0,273.15,1.0,1.0,-8.64e3, 1.0, 1.0,1.0,1.0,1.0, &
1.0, 1.0,1.0,1.0, 1.0, 1.0, 1.0,1.0,1.0,1.0, &
1.0, 1.0,1.0,1.0, 1.0, 1.0, 1.0/
data kpds22/ 2, 5, 3, 3, 9, 3, 3, 3, 3, 2, &
3, 2, 3, 3, 3, 3, 0, 0, -5, -4, &
0, 0, 0, 0, 0, 0, 0, 5, 3, 3, &
0, 0, 0, 0, 0, 3, 3/
!
data levs/ 5, 15, 25, 35, 45, 55, 65, 75, &
85, 95, 105, 115, 125, 135, 145, 155, &
165, 175, 185, 195, 205, 215, 225, 238, &
262, 303, 366, 459, 584, 747, 949,1193, &
1479,1807,2174,2579,3016,3483,3972,4478/
!
data spv_tau/-1.0E+5/
data spv_ci/1.0E+29/
data spv_pme/-1.0E+10/
data undef/-1.0E+34/
!
data zt/ 5., 15., 25., 35., 45., 55., 65., 75., &
85., 95., 105., 115., 125., 135., 145., 155., &
165., 175., 185., 195., 205., 215., 225., 238., &
262., 303., 366., 459., 584., 747., 949.,1193., &
1479.,1807.,2174.,2579.,3016.,3483.,3972.,4478./
!
data zw/ 10., 20., 30., 40., 50., 60., 70., 80., &
90., 100., 110., 120., 130., 140., 150., 160., &
170., 180., 190., 200., 210., 220., 232., 250., &
283., 335., 413., 522., 666., 848.,1072.,1337., &
1643.,1991.,2377.,2798.,3250.,3728.,4225.,4737./
!
data levelm/' m below sea level', ' m below sea level', &
' m below sea level', ' m below sea level', &
' m below sea level', &
'surface', 'surface', 'surface', 'surface', &
'surface', 'surface', 'surface', 'surface', &
'surface', 'surface', 'surface', &
'bottom of ocean mixed layer', &
'bottom of ocean isothermal layer', &
'0-300 m ocean layer', &
'layer ocean surface and 26C ocean isothermal level', &
'2.5C ocean isotherm', &
'5C ocean isotherm', &
'10C ocean isotherm', &
'15C ocean isotherm', &
'20C ocean isotherm', &
'25C ocean isotherm', &
'28C ocean isotherm', &
'surface', &
'surface', &
'surface', &
'surface', &
'surface', &
'surface', &
'surface', &
'surface', &
'surface', &
'surface'/
data varcode/'POT', 'SALTY', 'UOGRD', 'VOGRD', 'DZDT', &
'UFLX', 'VFLX', 'SSHG', 'ICEC', 'ICETK', &
'SNOD', 'TMP', 'UICE', 'VICE', 'EMNP', &
'THFLX', 'DBSS', 'DBSS', 'OHC', 'TCHP', &
'DBSS', 'DBSS', 'DBSS', 'DBSS', 'DBSS', &
'DBSS', 'DBSS', &
'SALTY', 'UOGRD', 'VOGRD','SPEED','SHTFL', &
'LHTFL', 'NSWRS', 'NLWRS','PRATE','EVP'/
!
template_file = 'iceocnpost.g2'
template_inv = '@mem:0'
! find grid size to make sure
iret = grb2_mk_inv(template_file, template_inv)
if (iret.ne.0) stop 1
! search using variable and regular date YYYYMMDDHH
template_var = '^1:'
! Based on Wesley's suggestion
! iret = grb2_inq(template_file,template_inv,template_var,nx=nx,ny=ny)
iret = grb2_inq(template_file,template_inv,template_var,nx=nx,ny=ny,regex=regex)
if (iret.ne.1) then
if (iret.eq.0) write(*,*) 'could not find message'
if (iret.gt.1) write(*,*) 'found multiple messages ', iret
stop 2
endif
!
if (mfcstcpl.eq.1) climate = .true.
!
print *,'im = ',im
print *,'jm = ',jm
print *,'km = ',km
print *,'imo = ',imo
print *,'jmo = ',jmo
print *,'IGEN_OCNP = ',igenocnp
print *,'mfcstcpl = ',mfcstcpl
print *,'climate = ',climate
isdate=0
isdate(1)=isyr
isdate(2)=ismth
isdate(3)=isday
isdate(5)=ishr
print *,'isdate ',isdate
iedate=0
iedate(1)=iyr
iedate(2)=imth
iedate(3)=iday
iedate(5)=ihr
call w3difdat(iedate,isdate,2,datedif)
hrdif=datedif(2)
write(hr8,'(i8)') hrdif
! print *,'datedif',datedif
print *,'hrdif',hrdif
write(datecode,'(i4.4,i2.2,i2.2,i2.2)') isyr, ismth, isday, ishr
print*, 'datecode=',datecode
call read_ocn(im,jm,km,lon,lat, &
t,s,u,v,eta,sfcflx,pme,mld,taux,tauy,ocnfile, &
sss,ssu,ssv,speed,sensible,latent,sw,lw,lprec,evap)
print *,'after call read_ocn'
call read_ice(im,jm,hi_ci,hs_ci,fi_ci,ts_ci,ui_ci,vi_ci,icefile)
print *,'after call read_ice'
uice(:,:)=ui_ci(:,:)
vice(:,:)=vi_ci(:,:)
do k=1,80
ftime(k:k)=' '
enddo
! ftime='6 hour fcst'
kpds(1)=7
if (igenocnp.GT.0) then
kpds(2)=igenocnp
else
kpds(2)=98
endif
kpds(3)=10
kpds(4)=192
kpds(8)=mod(isyr-1,100)+1
kpds(9)=ismth
kpds(10)=isday
kpds(11)=ishr
kpds(12)=0
if (mfhout == 1) then
kpds(13)=1
else if (mfhout == 3) then
kpds(13)=10
else if (mfhout == 6) then
kpds(13)=11
else if (mfhout == 12) then
kpds(13)=12
else if (mfhout == 24) then
kpds(13)=2
else
print *,'invalid mhout, must be one of (1 3 6 12 24).'
stop
endif
if (climate) then
kpds(13)=1
kpds(14)=mfh
print *,'kpds(14)=',kpds(14)
print *,'mfhout=',mfhout
kpds(15)=0
kpds(16)=10
ftime=hr8 // ' hour fcst'
print *, 'ftime:', ftime
else
if (mfh > 1530) then
kpds(13)=1
kpds(14)=mfh
kpds(15)=0
kpds(16)=10
else
kpds(14)=mfh/mfhout-1
kpds(15)=kpds(14)+1
kpds(16)=3
endif
endif
kpds(17)=0
kpds(18)=1
kpds(19)=2
kpds(20)=0
kpds(21)=((iyr-1)/100)+1
kpds(23)=4
kpds(24)=0
kpds(25)=32
print*,'kpds:',kpds(1:25)
!
kgds(1)=0
kgds(2)=imo
kgds(3)=jmo
kgds(4)=nint(flatn*1000.)
kgds(5)=nint(flonw*1000.)
kgds(6)=128
kgds(7)=nint(flats*1000.)
kgds(8)=nint(flone*1000.)
kgds(9)=nint(dlon*1000.)
kgds(10)=nint(dlat*1000.)
kgds(11)=0
kgds(12)=0
kgds(13)=0
kgds(14)=0
kgds(15)=0
kgds(16)=0
kgds(17)=0
kgds(18)=0
kgds(19)=0
kgds(20)=255
kgds(21)=0
kgds(22)=0
!
print*,'kgds:',kgds(1:22)
!
ndata=imo*jmo
!
ind=0
do nv=1,5
factor=fac(nv)
kpds(22)=kpds22(nv)
print *,nv,' factor ',factor,' kpds22 ',kpds(22)
!temp/potdsl...
if (nv.eq.1) then
varocn=t
do k=1,km
do j=1,jmo
do i=1,imo
if (varocn(i,j,k).LE.undef) then
tocn(i,j,k)=undef
else
tocn(i,j,k)=varocn(i,j,k)
endif
enddo
enddo
enddo
endif
!salinity
if (nv.eq.2) then
varocn=s
do k=1,km
do j=1,jmo
do i=1,imo
socn(i,j,k)=varocn(i,j,k)
enddo
enddo
enddo
endif
!u-current
if (nv.eq.3) then
varocn=u
endif
!v-current
if (nv.eq.4) then
varocn=v
endif
!-------------------------------------------------------------------
! Xingren:
! w vertical velocity (not present in raw NetCDF file)
! so how can you compute the vertical velocity with the program below?
!-------------------------------------------------------------------
if (nv.eq.5) then
varocn=w
do k=1,km-1
kk=km-k+1
do j=1,jmo
do i=1,imo
if (varocn(i,j,k).LE.undef) then
varocn(i,j,k)=varocn(i,j,k+1)
else
varocn(i,j,k)=(varocn(i,j,k+1)*(zw(kk)-zt(kk)) &
+varocn(i,j,k)*(zt(kk)-zw(kk-1)))/(zw(kk)-zw(kk-1))
endif
enddo
enddo
enddo
continue
endif
do k=1,km
ind=ind+1
ilev=levs(k)
! flip N-S
do j=1,jmo
grdtmp(:,j)=varocn(:,jmo-j+1,k)
enddo
! make bit-map....
do j=1,jmo
do i=1,imo
val=grdtmp(i,j)
if (val.eq.undef) then
lbms(i,j) = .false.
grid2(i+(j-1)*nx)=9.999E+20
else
if (nv.eq.1) then
grid(i,j)=val+factor
else
grid(i,j)=val*factor
endif
lbms(i,j) = .true.
grid2(i+(j-1)*nx)=grid(i,j)
endif
enddo
enddo
print *,' record written ',ind,nv,k,grid(92,125),lbms(92,125)
kpds(5)=kpds5(nv)
kpds(6)=kpds6(nv)
kpds(7)=ilev
write(level4,'(i4)') ilev
levelcode=level4 // levelm(nv)
print *, 'levelcode= ', levelcode
print *, 'trim(ftime):', trim(ftime)
metadata='d=' // datecode // ':' // trim(varcode(nv)) // ':' // trim(levelcode) // ':' // trim(ftime) // ':'
iret = grb2_wrt(outfile,template_file,1,data1=grid2,meta=metadata)
write(*,*) iret
!
!.. end level-loop
enddo
!.. end variable-loop
enddo
!... now read and grib 5 surface records...
!
do nv=6,nvar
levelcode=levelm(nv)
print *, 'levelcode= ', levelcode
print *, 'process data for nv=',nv
factor=fac(nv)
kpds(22)=kpds22(nv)
print *,nv,' factor ',factor,' kpds22 ',kpds(22)
ind=ind+1
kpds(5)=kpds5(nv)
kpds(6)=kpds6(nv)
kpds(7)=kpds7(nv)
if (nv .EQ. 8 .or. (nv.GE.16 .AND. nv.LE.27)) then
kpds(19)=129
else if (nv .EQ. 15) then
kpds(19)=128
else
kpds(19)=2
endif
! taux
if (nv.eq.6) then
varsfc=taux
do j=1,jmo
do i=1,imo
if (varsfc(i,j) .LE. spv_tau) varsfc(i,j)=undef
enddo
enddo
endif
! tauy
if (nv.eq.7) then
varsfc=tauy
do j=1,jmo
do i=1,imo
if (varsfc(i,j) .LE. spv_tau) varsfc(i,j)=undef
enddo
enddo
endif
! eta
if (nv.eq.8) then
varsfc=eta
endif
! fi
if (nv.eq.9) then
varsfc=fi_ci
call set_undef(im,jm,spv_ci,undef,varsfc)
endif
! hi
if (nv.eq.10) then
varsfc=hi_ci
call set_undef(im,jm,spv_ci,undef,varsfc)
endif
! hs
if (nv.eq.11) then
varsfc=hs_ci
call set_undef(im,jm,spv_ci,undef,varsfc)
endif
! ts
if (nv.eq.12) then
varsfc=ts_ci
call set_undef(im,jm,spv_ci,undef,varsfc)
endif
! uice
if (nv.eq.13) then
varsfc=uice
call set_undef(im,jm,spv_ci,undef,varsfc)
endif
! vice
if (nv.eq.14) then
varsfc=vice
call set_undef(im,jm,spv_ci,undef,varsfc)
endif
! pme
if (nv.eq.15) then
varsfc=lprec-evap !pme
do j=1,jm
do i=1,im
! if (varsfc(i,j) .LE. spv_pme) varsfc(i,j)=undef
if (lprec(i,j) .LE. spv_pme) varsfc(i,j)=undef
enddo
enddo
endif
! sfc_flx
if (nv.eq.16) then
varsfc=sfcflx
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! mld
if (nv.eq.17) then
! call mixed_layer(imo,jmo,km,tocn,socn,zt,varsfc,undef)
varsfc=mld
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! sfc isothm layer depth
if (nv.eq.18) then
call sfc_isothm_layer(imo,jmo,km,tocn,zt,varsfc,undef)
endif
! ocean heat content
if (nv.eq.19) then
call ocean_heat(imo,jmo,km,tocn,socn,zw,zt,varsfc,undef)
endif
! tropical cyc heat potential
if (nv.eq.20) then
call tchp26(imo,jmo,km,tocn,socn,zw,zt,varsfc,undef)
endif
! depth of 7 different isotherms...
if (nv.ge.21 .AND. nv.le.27) then
i=nv-20
call isothm_layer(imo,jmo,km,dtc(i),tocn,zt,varsfc,undef)
endif
! sea surface salinity
if (nv.eq.28) then
varsfc=sss
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! sea surface u-current
if (nv.eq.29) then
varsfc=ssu
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! sea surface v-current
if (nv.eq.30) then
varsfc=ssv
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! sea surface speed
if (nv.eq.31) then
varsfc=speed
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! sensible heat
if (nv.eq.32) then
varsfc=sensible
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! latent heat
if (nv.eq.33) then
varsfc=latent
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! net downward shortwave radiation at the surface
if (nv.eq.34) then
varsfc=sw
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! net downward longwave radiation at the surface
if (nv.eq.35) then
varsfc=lw
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! precipitation rate
if (nv.eq.36) then
varsfc=lprec
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
! evaporation
if (nv.eq.37) then
varsfc=evap
do j=1,jm
do i=1,im
if (varsfc(i,j) .LE. undef) varsfc(i,j)=undef
enddo
enddo
endif
nundef=0
do j=1,jmo
grdtmp(:,j)=varsfc(:,jmo-j+1)
enddo
do j=1,jmo
do i=1,imo
val=grdtmp(i,j)
if (val.eq.undef) then
lbms(i,j) = .false.
nundef=nundef+1
grid2(i+(j-1)*nx)=9.999E+20
else
if (nv.EQ.12) then
grid(i,j)=val+factor
else
grid(i,j)=val*factor
endif
lbms(i,j) = .true.
grid2(i+(j-1)*nx)=grid(i,j)
endif
enddo
enddo
if(nundef.eq.imo*jmo) then
print *,' record not written because of all undef ', &
ind,kpds(5),kpds(6),kpds(7)
else
print *,' record written ',ind,kpds(5),kpds(6),kpds(7), &
grid(92,125),lbms(92,125),factor
endif
print *,'nv= ', nv
metadata='d=' // datecode // ':' // trim(varcode(nv)) // ':' // trim(levelcode) // ':' // trim(ftime) // ':'
iret = grb2_wrt(outfile,template_file,1,data1=grid2,meta=metadata)
write(*,*) iret
enddo
return
end subroutine diag
subroutine isothm_layer(im,jm,km,dtc,temp,zlev,zisothm,undef)
real, parameter :: c2k=273.15
integer inumc,im,jm,km
integer i,j,k
real dtc
real, dimension(km) :: tz,zlev
real, dimension(im,jm) :: zisothm
real, dimension(im,jm,km) :: temp
real a,b,tc,undef
tc=dtc+c2k
do j=1,jm
do i=1,im
zisothm(i,j)=undef
if (temp(i,j,1) .GE. tc) then
do k=1,km
tz(k)=temp(i,j,k)
enddo
do k=2,km
if (tz(k) .LT. -3.0) go to 111
if (tz(k) .LT. tc) then
a = (tz(k)-tc) / (tz(k)-tz(k-1))
b = (tc-tz(k-1)) / (tz(k)-tz(k-1))
zisothm(i,j)=a*zlev(k-1)+b*zlev(k)
go to 111
endif
enddo
endif
111 continue
enddo
enddo
return
end subroutine isothm_layer
subroutine mixed_layer(im,jm,km,temp,salt,zlev,mld,undef)