forked from DOI-USGS/volcano-ash3d-metreader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
MetReader_GRIB.f90
2242 lines (2050 loc) · 105 KB
/
MetReader_GRIB.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
!##############################################################################
!##############################################################################
!##############################################################################
! Note: you can inspect the GRIB header using grib_dump tmp.grib2 > header.txt
! MR_Read_Met_DimVars_GRIB
! MR_Read_Met_Times_GRIB
! MR_Read_MetP_Variable_GRIB
! MR_GRIB_check_status
!##############################################################################
!
! MR_Read_Met_DimVars_GRIB
!
! Called once from MR_Read_Met_DimVars
!
! This subroutine reads the variable and dimension IDs, and fills the
! coordinate dimension variables
!
! After this subroutine completes, the following variables will be set:
! All the projection parameters of NWP grid
! Met_dim_names, Met_var_GRIB_names, Met_var_conversion_factor, Met_var_IsAvailable
! The lengths of all the dimensions of the file
! p_fullmet_sp (converted to Pa)
! x_fullmet_sp, y_fullmet_sp
! IsLatLon_MetGrid, IsGlobal_MetGrid, IsRegular_MetGrid
!
!##############################################################################
subroutine MR_Read_Met_DimVars_GRIB
use MetReader, only : &
MR_nio,VB,outlog,errlog,verbosity_error,verbosity_info,verbosity_production,&
MR_MAXVARS,x_fullmet_sp,y_fullmet_sp,nx_fullmet,ny_fullmet,&
Met_var_zdim_idx,nlevs_fullmet,levs_code,levs_fullmet_sp,&
p_fullmet_sp,np_fullmet,z_approx,&
MR_dx_met,MR_dy_met,dx_met_const,dy_met_const,IsLatLon_MetGrid,IsRegular_MetGrid,&
Met_iprojflag,Met_k0,Met_lam0,Met_phi0,Met_phi1,Met_phi2,Met_Re,MR_EPS_SMALL,&
MR_GRIB_Version,MR_iwind,MR_iwindformat,MR_Max_geoH_metP_predicted,&
x_inverted,y_inverted,z_inverted,MR_windfiles,Met_var_IsAvailable,&
nlev_coords_detected,nt_fullmet,Met_var_GRIB1_Param,Met_var_GRIB1_St,MR_GRIB_Version,&
Met_var_GRIB2_DPcPnSt,Met_var_GRIB_names,&
MR_Z_US_StdAtm
use projection, only : &
PJ_Set_Proj_Params,&
PJ_proj_for
use eccodes
implicit none
integer, parameter :: sp = 4 ! single precision
integer, parameter :: dp = 8 ! double precision
integer, parameter :: MAXGRIBREC = 10000
integer :: i, j, k
integer :: ir
real(kind=sp) :: xLL_fullmet
real(kind=sp) :: yLL_fullmet
real(kind=sp) :: xUR_fullmet
real(kind=sp) :: yUR_fullmet
integer :: ifile
integer :: igrib
integer :: iw
integer,dimension(MAXGRIBREC) :: igribv
integer :: nSTAT
integer :: ivar,iivar
integer :: idx
integer :: maxdimlen
character(len=130) :: grib_file_path
integer(kind=4) :: iv_discpl
integer(kind=4) :: iv_paramC
integer(kind=4) :: iv_paramN
integer(kind=4) :: iv_typeSf
!integer(kind=4) :: iv_Table
character(len=3) :: iv_typeSfc
integer(kind=4) :: numberOfPoints
integer(kind=4) :: dum_int
real(kind=8) :: dum_dp
character(len=20) :: dum_str
integer :: gg_order
real(kind=dp) :: x_start,y_start
real(kind=dp) :: Lon_start,Lat_start
real(kind=dp) :: Lon_end,Lat_end
!real(kind=dp),dimension(:),allocatable :: lats,lons,values
real(kind=dp),dimension(:),allocatable :: values
real(kind=dp), parameter :: tol = 1.0e-3_dp
integer(kind=4) :: typeOfFirstFixedSurface
integer :: count1=0
! Stores values of keys read from GRIB file
character(len=6) :: grb_shortName
character(len=36):: grb_longName
character(len=4) :: grb_typeSfc
integer(kind=4) :: grb_discipline
integer(kind=4) :: grb_parameterCategory
integer(kind=4) :: grb_parameterNumber
integer(kind=4) :: grb_Table
integer(kind=4) :: grb_level
integer(kind=4) :: grb_scaledValueOfFirstFixedSurface
integer(kind=4),dimension(MR_MAXVARS,100) :: zlev_dum ! This will hold the z-levels, up to 100
integer(kind=4),dimension(MR_MAXVARS) :: zcount ! This will hold the length of the z-coord
logical :: Check
logical :: FoundOldDim
logical :: IsTruncatedDim
logical :: ReadGrid
integer(kind=4) :: kk,tmp1
!integer :: stat
logical :: IsNewLevel
integer :: iz
integer :: io ! Index for output streams
INTERFACE
subroutine MR_GRIB_check_status(nSTAT, errcode, operation)
integer, intent(in) :: nSTAT
integer, intent(in) :: errcode
character(len=*), intent(in) :: operation
end subroutine MR_GRIB_check_status
END INTERFACE
do io=1,MR_nio;if(VB(io).le.verbosity_production)then
write(outlog(io),*)"-----------------------------------------------------------------------"
write(outlog(io),*)"---------- MR_Read_Met_DimVars_GRIB ----------"
write(outlog(io),*)"-----------------------------------------------------------------------"
endif;enddo
if(MR_iwind.eq.5)then
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)"MR ERROR : ",&
"GRIB reader not implemented for multi-timestep files."
write(errlog(io),*)" iwind=5 files are all multi-step"
endif;enddo
stop 1
else
!------------------------------------------------------------------------
! Start of block for all non-iwind=5 and non-iwf=50
! This is where the Netcdf and Grib subroutines can be compared
!
! Checking for dimension length and values for x,y,t,p
! Assume all files have the same format
! Note: you can inspect the GRIB header using grib_dump tmp.grib2 > header.txt
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Opening GRIB file to find version number"
endif;enddo
iw = 1
call codes_open_file(ifile,trim(adjustl(MR_windfiles(iw))),'R',nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_open_file ")
call codes_new_from_file(ifile,igrib,CODES_PRODUCT_GRIB,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_new_from_file ")
call codes_get(igrib,'editionNumber',MR_GRIB_Version,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get editionNumber ")
call codes_release(igrib,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_release ")
call codes_close_file(ifile,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_close_file ")
endif
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Grib version = ",MR_GRIB_Version
endif;enddo
!------------------------------------------------------------------------
! Checking for dimension length and values for x,y,t,p
! Assume all files have the same format
maxdimlen = 0
! Loop through all the GRIB messages,
! If we find a message that matches a variable criterion, then log the level to
! a dummy array.
! Finally sort the pressure values and evaluate the distinct pressure coordinates
grib_file_path = trim(adjustl(MR_windfiles(1)))
call codes_open_file(ifile,grib_file_path,'R',nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_open_file ")
count1=1
call codes_new_from_file(ifile,igribv(count1),CODES_PRODUCT_GRIB,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_new_from_file ")
do while (nSTAT.eq.CODES_SUCCESS)
count1=count1+1
if (count1.gt.MAXGRIBREC) then
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)"MR ERROR: too many GRIB messages"
write(errlog(io),*)" current limit set to ",MAXGRIBREC
endif;enddo
stop 1
endif
call codes_new_from_file(ifile,igribv(count1),CODES_PRODUCT_GRIB,nSTAT)
enddo
count1=count1-1
zcount(:) = 0
zlev_dum(:,:) = 0
do ir = 1,count1
if(ir.eq.1)then
! For the first record, get the x,y grid info
ReadGrid = .false.
call codes_get(igribv(ir),'Ni',nx_fullmet,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get Ni ")
call codes_get(igribv(ir),'Nj',ny_fullmet,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get Nj ")
allocate(x_fullmet_sp(0:nx_fullmet+1))
allocate(y_fullmet_sp(ny_fullmet))
allocate(MR_dx_met(nx_fullmet))
allocate(MR_dy_met(ny_fullmet))
call codes_get(igribv(ir),'gridType',dum_str,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get gridType ")
call codes_get(igribv(ir),'latitudeOfFirstGridPointInDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get latitudeOfFirstGridPointInDegrees ")
Lat_start = dum_dp
call codes_get(igribv(ir),'longitudeOfFirstGridPointInDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get longitudeOfFirstGridPointInDegrees ")
Lon_start = dum_dp
! Check for end lat/lon in order to determine if either coordinate is
! inverted
call codes_get(igribv(ir),'latitudeOfLastGridPointInDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)then
!call MR_GRIB_check_status(nSTAT,0,"codes_get latitudeOfLastGridPointInDegrees ")
! assume it is not inverted
y_inverted = .false.
else
Lat_end = dum_dp
if(Lat_start.gt.Lat_end)then
y_inverted = .true.
else
y_inverted = .false.
endif
endif
call codes_get(igribv(ir),'longitudeOfLastGridPointInDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)then
!call MR_GRIB_check_status(nSTAT,0,"codes_get longitudeOfLastGridPointInDegrees ")
! assume it is not inverted
x_inverted = .false.
else
Lon_end = dum_dp
if(Lon_start.gt.Lon_end)then
x_inverted = .true.
else
x_inverted = .false.
endif
endif
dum_int = 0
Met_Re = 6371.229_8
call codes_get(igribv(ir),'shapeOfTheEarth',dum_int,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get shapeOfTheEarth ")
if (dum_int.eq.0)then
! 0 Earth assumed spherical with radius = 6,367,470.0 m
Met_Re = 6367.470_8
elseif(dum_int.eq.1)then
! 1 Earth assumed spherical with radius specified by data producer
! Try to read the radius of earth
! For now, just assign the default
Met_Re = 6371.229_8
elseif(dum_int.eq.2)then
! 2 Earth assumed oblate spheroid with size as determined by IAU in 1965
! (major axis = 6,378,160.0 m, minor axis = 6,356,775.0 m, f = 1/297.0)
Met_Re = 6371.229_8
elseif(dum_int.eq.3)then
! 3 Earth assumed oblate spheroid with major and minor axes specified by data producer
Met_Re = 6371.229_8
elseif(dum_int.eq.4)then
! 4 Earth assumed oblate spheroid as defined in IAG-GRS80 model
! (major axis = 6,378,137.0 m, minor axis = 6,356,752.314 m, f = 1/298.257222101)
Met_Re = 6371.229_8
elseif(dum_int.eq.5)then
! 5 Earth assumed represented by WGS84 (as used by ICAO since 1998)
Met_Re = 6371.229_8
elseif(dum_int.eq.6)then
! 6 Earth assumed spherical with radius of 6,371,229.0 m
Met_Re = 6371.229_8
else
! 7-191 Reserved
! 192- 254 Reserved for local use
Met_Re = 6371.229_8
endif
if(index(dum_str,'regular_ll').ne.0)then
IsLatLon_MetGrid = .true.
!Lat_start = y_start
!Lon_start = x_start
y_start = Lat_start
x_start = Lon_start
call codes_get(igribv(ir),'numberOfPoints',numberOfPoints,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get numberOfPoints ")
allocate(values(numberOfPoints))
call codes_get(igribv(ir),'values',values,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get values ")
ReadGrid = .true.
deallocate(values)
!call codes_get(igribv(ir),'latitudeOfLastGridPointInDegrees',dum_dp,nSTAT)
!if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get latitudeOfLastGridPointInDegrees ")
!if(Lat_start.gt.dum_dp)then
! y_inverted = .true.
!else
! y_inverted = .false.
!endif
!call codes_get(igribv(ir),&
! 'longitudeOfLastGridPointInDegrees',dum_dp,nSTAT)
!if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get longitudeOfLastGridPointInDegrees ")
!if(Lon_start.gt.dum_dp)then
! x_inverted = .true.
!else
! x_inverted = .false.
!endif
call codes_get(igribv(ir),'iDirectionIncrementInDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get iDirectionIncrementInDegrees ")
dx_met_const = real(dum_dp,kind=4)
call codes_get(igribv(ir),'jDirectionIncrementInDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get jDirectionIncrementInDegrees ")
dy_met_const = real(dum_dp,kind=4)
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Setting x grid starting at",Lon_start
write(outlog(io),*)"with a spacing of ",dx_met_const
endif;enddo
do i=1,nx_fullmet
x_fullmet_sp(i) = real(Lon_start,kind=sp)+ &
(i-1)* dx_met_const
enddo
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Setting y grid starting at",Lat_start
write(outlog(io),*)"with a spacing of ",dy_met_const
endif;enddo
if(y_inverted)then
do j = 1,ny_fullmet
y_fullmet_sp(j) = real(y_start - (j-1)*dy_met_const,kind=sp)
enddo
else
do j = 1,ny_fullmet
y_fullmet_sp(j) = real(y_start + (j-1)*dy_met_const,kind=sp)
enddo
endif
!do j=1,ny_fullmet
! y_fullmet_sp(j) = real(Lat_start,kind=sp)+ &
! (j-1)* dy_met_const
!enddo
x_fullmet_sp(0) = x_fullmet_sp(1)-dx_met_const
x_fullmet_sp(nx_fullmet+1) = x_fullmet_sp(nx_fullmet)+dx_met_const
elseif(index(dum_str,'regular_gg').ne.0)then
IsLatLon_MetGrid = .true.
! For Gaussian grid, get the order
call codes_get(igribv(ir),'N',gg_order,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get N ")
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Detected regular_gg grid of order N=",gg_order
endif;enddo
if(gg_order.eq.128)then
! this is the N128 grid (512x256) used by ERA Interim
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Assuming this is ERA Interim; filling lon and lat"
endif;enddo
dx_met_const = 0.7031252_sp
do i=1,nx_fullmet
x_fullmet_sp(i) = 0.0_sp+(i-1)* dx_met_const
enddo
y_fullmet_sp(1:ny_fullmet) = &
(/89.46282_sp, 88.76695_sp, 88.06697_sp, 87.36607_sp, 86.66480_sp, 85.96337_sp, &
85.26185_sp, 84.56026_sp, 83.85863_sp, 83.15699_sp, 82.45532_sp, 81.75363_sp, 81.05194_sp, &
80.35023_sp, 79.64853_sp, 78.94681_sp, 78.24509_sp, 77.54337_sp, 76.84164_sp, 76.13991_sp, &
75.43818_sp, 74.73644_sp, 74.03471_sp, 73.33297_sp, 72.63123_sp, 71.92949_sp, 71.22775_sp, &
70.52601_sp, 69.82426_sp, 69.12252_sp, 68.42078_sp, 67.71903_sp, 67.01729_sp, 66.31554_sp, &
65.61379_sp, 64.91205_sp, 64.21030_sp, 63.50855_sp, 62.80680_sp, 62.10506_sp, 61.40331_sp, &
60.70156_sp, 59.99981_sp, 59.29806_sp, 58.59631_sp, 57.89456_sp, 57.19281_sp, 56.49106_sp, &
55.78931_sp, 55.08756_sp, 54.38581_sp, 53.68406_sp, 52.98231_sp, 52.28056_sp, 51.57881_sp, &
50.87706_sp, 50.17531_sp, 49.47356_sp, 48.77180_sp, 48.07005_sp, 47.36830_sp, 46.66655_sp, &
45.96480_sp, 45.26305_sp, 44.56129_sp, 43.85954_sp, 43.15779_sp, 42.45604_sp, 41.75429_sp, &
41.05254_sp, 40.35078_sp, 39.64903_sp, 38.94728_sp, 38.24553_sp, 37.54378_sp, 36.84202_sp, &
36.14027_sp, 35.43852_sp, 34.73677_sp, 34.03502_sp, 33.33326_sp, 32.63151_sp, 31.92976_sp, &
31.22800_sp, 30.52625_sp, 29.82450_sp, 29.12275_sp, 28.42099_sp, 27.71924_sp, 27.01749_sp, &
26.31573_sp, 25.61398_sp, 24.91223_sp, 24.21048_sp, 23.50872_sp, 22.80697_sp, 22.10522_sp, &
21.40347_sp, 20.70171_sp, 19.99996_sp, 19.29821_sp, 18.59645_sp, 17.89470_sp, 17.19295_sp, &
16.49120_sp, 15.78944_sp, 15.08769_sp, 14.38594_sp, 13.68418_sp, 12.98243_sp, 12.28068_sp, &
11.57893_sp, 10.87717_sp, 10.17542_sp, 9.473666_sp, 8.771913_sp, 8.07016_sp, 7.368407_sp, &
6.666654_sp, 5.964901_sp, 5.263148_sp, 4.561395_sp, 3.859642_sp, 3.157889_sp, 2.456136_sp, &
1.754383_sp, 1.05263_sp,0.3508765_sp,-0.350877_sp, -1.05263_sp,-1.754383_sp,-2.456136_sp, &
-3.157889_sp,-3.859642_sp,-4.561395_sp,-5.263148_sp,-5.964901_sp,-6.666654_sp, &
-7.368407_sp, -8.07016_sp,-8.771913_sp,-9.473666_sp,-10.17542_sp,-10.87717_sp, &
-11.57893_sp,-12.28068_sp,-12.98243_sp,-13.68418_sp,-14.38594_sp,-15.08769_sp, &
-15.78944_sp,-16.49120_sp,-17.19295_sp, -17.8947_sp,-18.59645_sp,-19.29821_sp, &
-19.99996_sp,-20.70171_sp,-21.40347_sp,-22.10522_sp,-22.80697_sp,-23.50872_sp, &
-24.21048_sp,-24.91223_sp,-25.61398_sp,-26.31573_sp,-27.01749_sp,-27.71924_sp, &
-28.42099_sp,-29.12275_sp,-29.82450_sp,-30.52625_sp,-31.22800_sp,-31.92976_sp, -32.63151_sp,&
-33.33326_sp,-34.03502_sp,-34.73677_sp,-35.43852_sp,-36.14027_sp,-36.84202_sp, &
-37.54378_sp,-38.24553_sp,-38.94728_sp,-39.64903_sp,-40.35078_sp,-41.05254_sp, &
-41.75429_sp,-42.45604_sp,-43.15779_sp,-43.85954_sp,-44.56129_sp,-45.26305_sp, &
-45.96480_sp,-46.66655_sp,-47.36830_sp,-48.07005_sp,-48.77180_sp,-49.47356_sp, -50.17531_sp,&
-50.87706_sp,-51.57881_sp,-52.28056_sp,-52.98231_sp,-53.68406_sp,-54.38581_sp, &
-55.08756_sp,-55.78931_sp,-56.49106_sp,-57.19281_sp,-57.89456_sp,-58.59631_sp, &
-59.29806_sp,-59.99981_sp,-60.70156_sp,-61.40331_sp,-62.10506_sp,-62.80680_sp, &
-63.50855_sp,-64.21030_sp,-64.91205_sp,-65.61379_sp,-66.31554_sp,-67.01729_sp, &
-67.71903_sp,-68.42078_sp,-69.12252_sp,-69.82426_sp,-70.52601_sp,-71.22775_sp, &
-71.92949_sp,-72.63123_sp,-73.33297_sp,-74.03471_sp,-74.73644_sp,-75.43818_sp, &
-76.13991_sp,-76.84164_sp,-77.54337_sp,-78.24509_sp,-78.94681_sp,-79.64853_sp, &
-80.35023_sp,-81.05194_sp,-81.75363_sp,-82.45532_sp,-83.15699_sp,-83.85863_sp, &
-84.56026_sp,-85.26185_sp,-85.96337_sp, -86.6648_sp,-87.36607_sp,-88.06697_sp, &
-88.76695_sp,-89.46282_sp /)
elseif(gg_order.eq.80)then
! this is the N80 grid (320x160) used by ERA 20C
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Assuming this is ERA 20C; filling lon and lat"
endif;enddo
dx_met_const = 1.125_sp
do i=1,nx_fullmet
x_fullmet_sp(i) = 0.0_sp+(i-1)* dx_met_const
enddo
y_fullmet_sp(1:ny_fullmet) = &
(/89.14152_sp, 88.02943_sp, 86.91077_sp, 85.79063_sp, 84.66992_sp, 83.54895_sp, &
82.42782_sp, 81.30659_sp, 80.18531_sp, 79.06398_sp, 77.94263_sp, 76.82124_sp, 75.69984_sp, &
74.57843_sp, 73.45701_sp, 72.33558_sp, 71.21413_sp, 70.09269_sp, 68.97124_sp, 67.84978_sp, &
66.72832_sp, 65.60686_sp, 64.48540_sp, 63.36393_sp, 62.24246_sp, 61.12099_sp, 59.99952_sp, &
58.87804_sp, 57.75657_sp, 56.63509_sp, 55.51361_sp, 54.39214_sp, 53.27066_sp, 52.14917_sp, &
51.02769_sp, 49.90621_sp, 48.78473_sp, 47.66325_sp, 46.54176_sp, 45.42028_sp, 44.29879_sp, &
43.17731_sp, 42.05582_sp, 40.93434_sp, 39.81285_sp, 38.69136_sp, 37.56988_sp, 36.44839_sp, &
35.32690_sp, 34.20542_sp, 33.08393_sp, 31.96244_sp, 30.84096_sp, 29.71947_sp, 28.59798_sp, &
27.47649_sp, 26.35500_sp, 25.23351_sp, 24.11202_sp, 22.99054_sp, 21.86905_sp, 20.74756_sp, &
19.62607_sp, 18.50458_sp, 17.38309_sp, 16.26160_sp, 15.14011_sp, 14.01862_sp, 12.89713_sp, &
11.77564_sp, 10.65415_sp, 9.53266_sp, 8.41117_sp, 7.28968_sp, 6.168194_sp, 5.046704_sp, &
3.92522_sp, 2.80373_sp, 1.68224_sp, 0.56074_sp, -0.56074_sp,-1.682235_sp, &
-2.80373_sp, -3.92522_sp, -5.04670_sp, -6.16819_sp, -7.28968_sp,-8.411174_sp, &
-9.53266_sp,-10.65415_sp,-11.77564_sp,-12.89713_sp,-14.01862_sp,-15.14011_sp, &
-16.26160_sp,-17.38309_sp,-18.50458_sp,-19.62607_sp,-20.74756_sp,-21.86905_sp, &
-22.99054_sp,-24.11202_sp,-25.23351_sp,-26.35500_sp,-27.47649_sp,-28.59798_sp, &
-29.71947_sp,-30.84096_sp,-31.96244_sp,-33.08393_sp,-34.20542_sp,-35.32690_sp, &
-36.44839_sp,-37.56988_sp,-38.69136_sp,-39.81285_sp,-40.93434_sp,-42.05582_sp, &
-43.17731_sp,-44.29879_sp,-45.42028_sp,-46.54176_sp,-47.66325_sp,-48.78473_sp, &
-49.90621_sp,-51.02769_sp,-52.14917_sp,-53.27066_sp,-54.39214_sp,-55.51361_sp, &
-56.63509_sp,-57.75657_sp,-58.87804_sp,-59.99952_sp,-61.12099_sp,-62.24246_sp, &
-63.36393_sp,-64.48540_sp,-65.60686_sp,-66.72832_sp,-67.84978_sp,-68.97124_sp, &
-70.09269_sp,-71.21413_sp,-72.33558_sp,-73.45701_sp,-74.57843_sp,-75.69984_sp, &
-76.82124_sp,-77.94263_sp,-79.06398_sp,-80.18531_sp,-81.30659_sp,-82.42782_sp, &
-83.54895_sp,-84.66992_sp,-85.79063_sp,-86.91077_sp,-88.02943_sp,-89.14152_sp /)
else
! We currently do not have a general gaussian grid calculator.
! Might incorporate this file at some point.
! https://github.com/NCAR/ncl/blob/develop/ni/src/lib/nfpfort/gaus.f
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)"MR ERROR: Need to fix grid reading for gg grids."
endif;enddo
stop 1
endif
!Lat_start = y_start
!Lon_start = x_start
y_start = Lat_start
x_start = Lon_start
call codes_get(igribv(ir),'numberOfPoints',numberOfPoints,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get numberOfPoints ")
allocate(values(numberOfPoints))
call codes_get(igribv(ir),'values',values,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get values ")
ReadGrid = .true.
deallocate(values)
!call codes_get(igribv(ir),'latitudeOfLastGridPointInDegrees',dum_dp,nSTAT)
!if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get latitudeOfLastGridPointInDegrees ")
!if(Lat_start.gt.dum_dp)then
! y_inverted = .true.
!else
! y_inverted = .false.
!endif
!call codes_get(igribv(ir),&
! 'longitudeOfLastGridPointInDegrees',dum_dp,nSTAT)
!if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get longitudeOfLastGridPointInDegrees ")
!if(Lon_start.gt.dum_dp)then
! x_inverted = .true.
!else
! x_inverted = .false.
!endif
x_fullmet_sp(0) = x_fullmet_sp(nx_fullmet)-360.0_sp
x_fullmet_sp(nx_fullmet+1) = x_fullmet_sp(1)+360.0_sp
elseif(index(dum_str,'polar_stereographic').ne.0)then
IsLatLon_MetGrid = .false.
Met_iprojflag = 1
call codes_get(igribv(ir),'orientationOfTheGridInDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get orientationOfTheGridInDegrees ")
Met_lam0 = dum_dp
Met_phi0 = 90.0_8
Met_phi1 = 90.0_8
Met_k0 = 0.933_8
Met_Re = 6371.229_8
elseif(index(dum_str,'albers').ne.0)then
IsLatLon_MetGrid = .false.
Met_iprojflag = 2
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)"MR ERROR: Alber Equal Area not implemented"
endif;enddo
stop 1
elseif(index(dum_str,'UTM').ne.0)then
IsLatLon_MetGrid = .false.
Met_iprojflag = 3
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)"MR ERROR: UTM not implemented"
endif;enddo
stop 1
elseif(index(dum_str,'lambert').ne.0)then
IsLatLon_MetGrid = .false.
Met_iprojflag = 4
call codes_get(igribv(ir),'LoVInDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get LoVInDegrees ")
Met_lam0 = dum_dp
call codes_get(igribv(ir),'LaDInDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get LaDInDegrees ")
Met_phi0 = dum_dp
call codes_get(igribv(ir),'Latin1InDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get Latin1InDegrees ")
Met_phi1 = dum_dp
call codes_get(igribv(ir),'Latin2InDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get Latin2InDegrees ")
Met_phi2 = dum_dp
Met_k0 = 0.933_8
Met_Re = 6371.229_8
elseif(index(dum_str,'mercator').ne.0)then
IsLatLon_MetGrid = .false.
Met_iprojflag = 5
Met_lam0 = Lon_start
call codes_get(igribv(ir),'LaDInDegrees',dum_dp,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get LaDInDegrees ")
Met_phi0 = dum_dp
Met_k0 = 0.933_8
Met_Re = 6371.229_8
else
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR ERROR: Cannot determine the projection from the GRIB file.'
endif;enddo
stop 1
endif
! Override for the case of NARR
if (MR_iwindformat.eq.3)Met_Re = 6367.470_8
if(.not.IsLatLon_MetGrid)then
call codes_get(igribv(ir),'DxInMetres',dum_int,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,0,"codes_get DxInMetres ")
if(nSTAT.ne.0)then
call codes_get(igribv(ir),'DiInMetres',dum_int,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get DiInMetres ")
endif
dx_met_const = real(dum_int,kind=4)/1000.0_sp
call codes_get(igribv(ir),'DyInMetres',dum_int,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,0,"codes_get DyInMetres ")
if(nSTAT.ne.0)then
call codes_get(igribv(ir),'DjInMetres',dum_int,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get DjInMetres ")
endif
dy_met_const = real(dum_int,kind=4)/1000.0_sp
call PJ_proj_for(Lon_start,Lat_start, Met_iprojflag, &
Met_lam0,Met_phi0,Met_phi1,Met_phi2,Met_k0,Met_Re, &
x_start,y_start)
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Getting start coordinate for ",Lon_start,Lat_start
write(outlog(io),*)" Projected coordinate = ",x_start,y_start
endif;enddo
endif
if(.not.ReadGrid)then
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Setting x grid starting at",x_start
write(outlog(io),*)"with a spacing of ",dx_met_const
endif;enddo
do i = 0,nx_fullmet+1
x_fullmet_sp(i) = real(x_start + (i-1)*dx_met_const,kind=sp)
enddo
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Setting y grid starting at",y_start
write(outlog(io),*)"with a spacing of ",dy_met_const
endif;enddo
if(y_inverted)then
do i = 1,ny_fullmet
y_fullmet_sp(i) = real(y_start - (i-1)*dy_met_const,kind=sp)
enddo
else
do i = 1,ny_fullmet
y_fullmet_sp(i) = real(y_start + (i-1)*dy_met_const,kind=sp)
enddo
endif
ReadGrid = .true.
endif
do i = 1,nx_fullmet
MR_dx_met(i) = x_fullmet_sp(i+1)-x_fullmet_sp(i)
enddo
do i = 1,ny_fullmet-1
MR_dy_met(i) = y_fullmet_sp(i+1)-y_fullmet_sp(i)
enddo
MR_dy_met(ny_fullmet) = MR_dy_met(ny_fullmet-1)
! We need to check if this is a regular grid
IsRegular_MetGrid = .true.
do i = 1,nx_fullmet-1
if(abs(MR_dx_met(i+1)-MR_dx_met(i)).gt.tol*MR_dx_met(i))then
IsRegular_MetGrid = .false.
endif
enddo
do i = 1,ny_fullmet-1
if(abs(MR_dy_met(i+1)-MR_dy_met(i)).gt.tol*MR_dy_met(i))then
IsRegular_MetGrid = .false.
endif
enddo
endif ! count1.eq.1
! End of block reading the first record for grid info
if(MR_GRIB_Version.eq.1)then
call codes_get(igribv(ir),'indicatorOfTypeOfLevel',grb_typeSfc,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get indicatorOfTypeOfLevel ")
! for populating z-levels, we are only concerned with specific level types
if(index(grb_typeSfc,'pl').ne.0.or. & ! Isobaric surface (Pa)
index(grb_typeSfc,'105').ne.0)then ! Specified height level above ground (m)
call codes_get(igribv(ir),'indicatorOfParameter',grb_parameterNumber,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get indicatorOfParameter ")
call codes_get(igribv(ir),'table2Version',grb_Table,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get table2Version ")
! Loop through all the variables and see if we have a match with this GRIB record
do ivar = 1,MR_MAXVARS
if (.not.Met_var_IsAvailable(ivar)) cycle
iv_ParamN = Met_var_GRIB1_Param(ivar)
iv_typeSfc = Met_var_GRIB1_St(ivar)
if(iv_ParamN.eq.grb_parameterNumber.and. &
iv_typeSfc.eq.grb_typeSfc)then
! This is one we are tracking, log the level
call codes_get(igribv(ir),'level',grb_level,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get level ")
zcount(ivar) = zcount(ivar) + 1
zlev_dum(ivar,zcount(ivar)) = grb_level * 100
endif
enddo
endif
elseif(MR_GRIB_Version.eq.2)then
typeOfFirstFixedSurface = -1
grb_discipline = -1
grb_parameterCategory = -1
grb_parameterNumber = -1
grb_scaledValueOfFirstFixedSurface = -1
call codes_get(igribv(ir),'typeOfFirstFixedSurface', typeOfFirstFixedSurface,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get typeOfFirstFixedSurface ")
! for populating z-levels, we are only concerned with specific level types
if(typeOfFirstFixedSurface.eq.100.or. & ! Isobaric surface (Pa)
typeOfFirstFixedSurface.eq.103.or. & ! Specified height level above ground (m)
typeOfFirstFixedSurface.eq.106)then ! Depth below land surface (m)
call codes_get(igribv(ir),'discipline', grb_discipline,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get discipline ")
call codes_get(igribv(ir),'parameterCategory', grb_parameterCategory,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get parameterCategory ")
call codes_get(igribv(ir),'parameterNumber', grb_parameterNumber,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get parameterNumber ")
call codes_get(igribv(ir),'shortName', grb_shortName,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get shortName ")
call codes_get(igribv(ir),'cfNameECMF', grb_longName,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get cfNameECMF ")
call codes_get(igribv(ir),&
'scaledValueOfFirstFixedSurface',&
grb_scaledValueOfFirstFixedSurface,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get scaledValueOfFirstFixedSurface ")
! Loop through all the variables and see if we have a match with this GRIB record
do ivar = 1,MR_MAXVARS
if (.not.Met_var_IsAvailable(ivar)) cycle
iv_discpl = Met_var_GRIB2_DPcPnSt(ivar,1)
iv_paramC = Met_var_GRIB2_DPcPnSt(ivar,2)
iv_paramN = Met_var_GRIB2_DPcPnSt(ivar,3)
iv_typeSf = Met_var_GRIB2_DPcPnSt(ivar,4)
if(iv_discpl.eq.grb_discipline.and. &
iv_paramC.eq.grb_parameterCategory.and.&
iv_paramN.eq.grb_parameterNumber.and. &
iv_typeSf.eq.typeOfFirstFixedSurface)then
! This is one we are tracking, log the level
call codes_get(igribv(ir),&
'scaledValueOfFirstFixedSurface',&
grb_scaledValueOfFirstFixedSurface,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_get scaledValueOfFirstFixedSurface ")
! Double-check that this isn't a duplicate record
IsNewLevel = .true.
do iz = 1,zcount(ivar)
if (zlev_dum(ivar,iz).eq.grb_scaledValueOfFirstFixedSurface) then
IsNewLevel = .false.
exit
endif
enddo
if (IsNewLevel) then
zcount(ivar) = zcount(ivar) + 1
zlev_dum(ivar,zcount(ivar)) = grb_scaledValueOfFirstFixedSurface
endif
endif
enddo
endif
endif !MR_GRIB_Version.eq.1 or .eq.2
enddo
do ir = 1,count1
call codes_release(igribv(ir),nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_release ")
enddo
call codes_close_file(ifile,nSTAT)
if(nSTAT.ne.CODES_SUCCESS)call MR_GRIB_check_status(nSTAT,1,"codes_close_file ")
maxdimlen = maxval(zcount(:))
nlev_coords_detected = 1
Met_var_zdim_idx(:) = 0
Met_var_zdim_idx(1) = 1
do ivar = 2,MR_MAXVARS
if (.not.Met_var_IsAvailable(ivar)) cycle
FoundOldDim = .false.
do iivar = 1,ivar-1
if (zcount(iivar).eq.zcount(ivar))then ! This check for a different coordinate is
! solely on the size of the dimension.
FoundOldDim = .true.
Met_var_zdim_idx(ivar) = Met_var_zdim_idx(iivar)
exit
endif
enddo
if(.not.FoundOldDim)then
nlev_coords_detected = nlev_coords_detected + 1
Met_var_zdim_idx(ivar) = nlev_coords_detected
endif
enddo
! The V part of velocity is typically the second of a multi-component record and the above code
! does not catch it. Assume V has the same characteristics as U and copy U
! V @ isobaric
zcount(3) = zcount(2)
zlev_dum(3,1:zcount(3)) = zlev_dum(2,1:zcount(2))
Met_var_zdim_idx(3) = Met_var_zdim_idx(2)
! V @ 10 m
zcount(12) = zcount(11)
zlev_dum(12,1:zcount(12)) = zlev_dum(11,1:zcount(11))
Met_var_zdim_idx(12) = Met_var_zdim_idx(11)
! We have all the level dimension names and dim_ids; now we need to get the sizes
allocate(nlevs_fullmet(nlev_coords_detected))
allocate(levs_code(nlev_coords_detected))
allocate(levs_fullmet_sp(nlev_coords_detected,maxdimlen))
do ivar = 1,MR_MAXVARS
if (.not.Met_var_IsAvailable(ivar)) cycle
! Check if this variable has a z-dimension (pressure, height, depth, etc.)
if(Met_var_zdim_idx(ivar).gt.0)then
idx = Met_var_zdim_idx(ivar)
nlevs_fullmet(idx) = zcount(ivar)
! Check that the pressure level is monotonically increasing
Check=.true.
do k=1,zcount(ivar)-1
if(zlev_dum(ivar,k).gt.zlev_dum(ivar,k+1)) Check=.false.
enddo
if (Check)then
levs_fullmet_sp(idx,1:nlevs_fullmet(idx)) = &
real(zlev_dum(ivar,1:nlevs_fullmet(idx)),kind=sp)
z_inverted = .true.
! Pressure is expected to be from the bottom up so invert
allocate(p_fullmet_sp(nlevs_fullmet(idx)))
do k=1,zcount(ivar)
p_fullmet_sp(k) = levs_fullmet_sp(idx,nlevs_fullmet(idx)+1-k)
enddo
levs_fullmet_sp(idx,1:nlevs_fullmet(idx)) = p_fullmet_sp(1:nlevs_fullmet(idx))
deallocate(p_fullmet_sp)
else
! Need to first sort pressure variable here
! Using insertion sort on p321 of Numerical Recipes
do k=2,nlevs_fullmet(idx)
tmp1 = zlev_dum(ivar,k)
do kk=k-1,1,-1
if (zlev_dum(ivar,kk).ge.tmp1) goto 101
zlev_dum(ivar,kk+1) = zlev_dum(ivar,kk)
enddo
kk=0
101 zlev_dum(ivar,kk+1) = tmp1
enddo
levs_fullmet_sp(idx,1:nlevs_fullmet(idx)) = &
real(zlev_dum(ivar,1:nlevs_fullmet(idx)),kind=sp)
z_inverted = .false.
endif
endif
enddo
! Now log all pressure coordinates as one-to-one, truncated, or interrupted
levs_code(1:nlev_coords_detected) = 0
levs_code(1) = 1 ! The first var checked (GPH) should have a one-to-one mapping
! Check how each of the pressure coordinates map onto the GPH grid
if (nlev_coords_detected.gt.1)then
! Only bother if there are multiple pressure coordinates
do idx = 2,nlev_coords_detected
if (nlevs_fullmet(idx).gt.nlevs_fullmet(1))then
! This coordinate has more values than the GPH pressure coordinate
levs_code(idx) = 4
elseif (nlevs_fullmet(idx).lt.nlevs_fullmet(1))then
! It there are fewer levels, check if this is a truncated coordinate (code = 2)
! or one with missing levels that requires interpolation (code = 3)
IsTruncatedDim = .true.
do i=1,nlevs_fullmet(idx)
if(abs(levs_fullmet_sp(idx,i)-levs_fullmet_sp(1,i)).gt.MR_EPS_SMALL)then
IsTruncatedDim = .false.
exit
endif
enddo
if(IsTruncatedDim)then
levs_code(idx) = 2
else
levs_code(idx) = 3
endif
else
! This coordinate has the same dimension as the GPH pressure coordinate.
! They are probably the same
levs_code(idx) = 1
endif
enddo
endif
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)" Found these levels"
write(outlog(io),*)&
" VaribleID LevelIdx dimID length"
do ivar = 1,MR_MAXVARS
if (Met_var_IsAvailable(ivar))then
if(Met_var_zdim_idx(ivar).eq.0)then
write(outlog(io),*)ivar,Met_var_zdim_idx(ivar),0,0,&
trim(adjustl(Met_var_GRIB_names(ivar)))
else
write(outlog(io),*)ivar,Met_var_zdim_idx(ivar),0,&
nlevs_fullmet(Met_var_zdim_idx(ivar)),&
trim(adjustl(Met_var_GRIB_names(ivar)))
endif
endif
enddo
endif;enddo
! Now assign these levels to the working arrays
! Geopotential is the first variable checked, use this for np_fullmet
nt_fullmet = 1
np_fullmet = nlevs_fullmet(Met_var_zdim_idx(1)) ! Assign fullmet the length of H,U,V
allocate(p_fullmet_sp(np_fullmet))
idx = Met_var_zdim_idx(1)
p_fullmet_sp(1:nlevs_fullmet(idx)) = levs_fullmet_sp(idx,1:nlevs_fullmet(idx))
MR_Max_geoH_metP_predicted = MR_Z_US_StdAtm(p_fullmet_sp(np_fullmet)/100.0_sp)
allocate(z_approx(np_fullmet))
do k=1,np_fullmet
! Calculate heights for US Std Atmos while pressures are still in mbars
! or hPa
z_approx(k) = MR_Z_US_StdAtm(p_fullmet_sp(k))
enddo
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"Dimension info:"
write(outlog(io),*)" record (time): ",nt_fullmet
write(outlog(io),*)" level (z) : ",np_fullmet
write(outlog(io),*)" y : ",ny_fullmet
write(outlog(io),*)" x : ",nx_fullmet
endif;enddo
!************************************************************************
! assign boundaries of mesoscale model
if(x_inverted)then
! I know of no windfiles with x-coordinate reversed
xLL_fullmet = x_fullmet_sp(nx_fullmet)
xUR_fullmet = x_fullmet_sp(1)
else
xLL_fullmet = x_fullmet_sp(1)
xUR_fullmet = x_fullmet_sp(nx_fullmet)
endif
if(y_inverted)then
! Most lon/lat grids have y reversed
yLL_fullmet = y_fullmet_sp(ny_fullmet)
yUR_fullmet = y_fullmet_sp(1)
else
! Projected grids have y not reversed
yLL_fullmet = y_fullmet_sp(1)
yUR_fullmet = y_fullmet_sp(ny_fullmet)
endif
do io=1,MR_nio;if(VB(io).le.verbosity_production)then
write(outlog(io),*)"-----------------------------------------------------------------------"
endif;enddo
end subroutine MR_Read_Met_DimVars_GRIB
!##############################################################################
!##############################################################################
!
! MR_Read_Met_Times_GRIB
!
! Called once from MR_Read_Met_DimVars
!
! This subroutine opens each GRIB file and determine the time of each
! time step of each file in the number of hours since MR_BaseYear.
! In most cases, the length of the time variable (nt_fullmet) will be
! read directly from the file and overwritten (is was set in MR_Read_Met_DimVars_GRIB
! above).
!
! After this subroutine completes, the following variables will be set:
! MR_windfile_starthour(MR_iwindfiles)
! MR_windfile_stephour(MR_iwindfiles,nt_fullmet)
!
!##############################################################################
subroutine MR_Read_Met_Times_GRIB
use MetReader, only : &
MR_nio,VB,outlog,errlog,verbosity_error,verbosity_info,verbosity_production,&
MR_windfile_starthour,MR_iwindfiles,MR_windfile_stephour,&
MR_BaseYear,MR_useLeap,MR_Comp_StartYear,MR_iwindformat,nt_fullmet,&
Met_dim_IsAvailable,MR_windfiles,MR_windfiles_nt_fullmet,MR_GRIB_Version
use eccodes
implicit none
integer, parameter :: sp = 4 ! single precision
!integer, parameter :: dp = 8 ! double precision
integer :: iw,iws
integer :: itstart_year,itstart_month
integer :: itstart_day
real(kind=sp) :: filestart_hour
integer :: itstart_hour,itstart_min,itstart_sec
!real(kind=8) :: HS_hours_since_baseyear
character(len=130) :: dumstr
integer :: iwstep
integer :: dataDate
integer :: dataTime
integer :: forecastTime
integer :: ifile
! integer :: iret
integer :: igrib
integer :: nSTAT
integer :: io ! Index for output streams
INTERFACE
real(kind=8) function HS_hours_since_baseyear(iyear,imonth,iday,hours,byear,useLeaps)
integer :: iyear
integer :: imonth
integer :: iday
real(kind=8) :: hours
integer :: byear
logical :: useLeaps
end function HS_hours_since_baseyear
subroutine MR_GRIB_check_status(nSTAT, errcode, operation)
integer, intent(in) :: nSTAT
integer, intent(in) :: errcode
character(len=*), intent(in) :: operation
end subroutine MR_GRIB_check_status
END INTERFACE
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)"-----------------------------------------------------------------------"
write(outlog(io),*)"---------- MR_Read_Met_Times_GRIB ----------"
write(outlog(io),*)"-----------------------------------------------------------------------"
endif;enddo
if(.not.Met_dim_IsAvailable(1))then
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)"MR ERROR: Time dimension is required and not listed"
write(errlog(io),*)" in custom windfile specification file."
endif;enddo
stop 1
endif
allocate(MR_windfile_starthour(MR_iwindfiles))
if(MR_iwindformat.eq.27)then
! GRIB1 reader not yet working!!
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)"MR ERROR: iwf=27 is a GRIB1 format."
write(errlog(io),*)" The GRIB1 reader is not yet working"
endif;enddo
stop 1
! Here the branch for when MR_iwindformat = 27
! First copy path read in to slot 2
!if(MR_runAsForecast)then
! do io=1,MR_nio;if(VB(io).le.verbosity_error)then
! write(errlog(io),*)"MR ERROR: iwf=27 cannot be used for forecast runs."
! write(errlog(io),*)" These are reanalysis files."
! endif;enddo
! stop 1
!endif
dumstr = MR_windfiles(1)
110 format(a50,a1,i4,a1)
write(MR_windfiles(1),110)trim(adjustl(dumstr)),'/', &
MR_Comp_StartYear,'/'
write(MR_windfiles(2),110)trim(adjustl(dumstr)),'/', &
MR_Comp_StartYear+1,'/'