forked from hschwaiger-usgs/volcano-ash3d-metreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetReader_GRIB.f90
1663 lines (1502 loc) · 70.1 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
!##############################################################################
!##############################################################################
!##############################################################################
!##############################################################################
!
! 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
use grib_api
use projection
implicit none
integer, parameter :: sp = 4 ! single precision
integer, parameter :: dp = 8 ! double precision
integer :: i, j, k
real(kind=sp) :: xLL_fullmet
real(kind=sp) :: yLL_fullmet
real(kind=sp) :: xUR_fullmet
real(kind=sp) :: yUR_fullmet
integer :: ifile
integer :: iret
integer :: igrib
integer :: iw
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=19) :: dum_str
character(len=20) :: dum_str
real(kind=dp) :: x_start,y_start
real(kind=dp) :: Lon_start,Lat_start
real(kind=dp),dimension(:),allocatable :: lats,lons,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=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
write(MR_global_production,*)"--------------------------------------------------------------------------------"
write(MR_global_production,*)"---------- MR_Read_Met_DimVars_GRIB ----------"
write(MR_global_production,*)"--------------------------------------------------------------------------------"
!---------------------------------------------------------------------------------
! Checking for dimension length and values for x,y,t,p
! Assume all files have the same format
if(MR_iwind.eq.5)then
write(MR_global_error,*)"MR ERROR : GRIB reader not implemented for multi-timestep files."
write(MR_global_error,*)" iwind=5 files are all multi-step"
stop 1
else
write(MR_global_production,*)"Opening grib file to find version number"
iw = 1
call grib_open_file(ifile,trim(ADJUSTL(MR_windfiles(iw))),'R')
call grib_new_from_file(ifile,igrib,iret)
call grib_get(igrib,'editionNumber',MR_GRIB_Version)
call grib_release(igrib)
call grib_close_file(ifile)
endif
write(MR_global_production,*)"Grib version = ",MR_GRIB_Version
!---------------------------------------------------------------------------------
! 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 criteria, then log the level to
! a dummy array.
! Finally sort the pressure values and evaluate the distinct pressure coordinates
grib_file_path = adjustl(trim(MR_windfiles(1)))
call grib_open_file(ifile,grib_file_path,'R')
! Loop on all the messages in a file.
call grib_new_from_file(ifile,igrib,iret)
count1=0
zcount(:) = 0
zlev_dum(:,:) = 0
do while (iret/=GRIB_END_OF_FILE)
count1=count1+1
if(count1.eq.1)then
! For the first record, get the x,y grid info
ReadGrid = .false.
call grib_get(igrib,'Ni',nx_fullmet)
call grib_get(igrib,'Nj',ny_fullmet)
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 grib_get(igrib,'gridType',dum_str)
call grib_get(igrib,'latitudeOfFirstGridPointInDegrees',dum_dp)
Lat_start = dum_dp
call grib_get(igrib,'longitudeOfFirstGridPointInDegrees',dum_dp)
Lon_start = dum_dp
dum_int = 0
Met_Re = 6371.229_8
call grib_get(igrib,'shapeOfTheEarth',dum_int)
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
call grib_get(igrib,'numberOfPoints',numberOfPoints)
allocate(lats(numberOfPoints))
allocate(lons(numberOfPoints))
allocate(values(numberOfPoints))
call grib_get_data(igrib,lats,lons,values)
do j=1,ny_fullmet
do i=1,nx_fullmet
idx = (j-1)*nx_fullmet + i
x_fullmet_sp(i) = real(lons(idx),kind=sp)
y_fullmet_sp(j) = real(lats(idx),kind=sp)
enddo
enddo
ReadGrid = .true.
deallocate(lats)
deallocate(lons)
deallocate(values)
call grib_get(igrib,'latitudeOfLastGridPointInDegrees',dum_dp)
if(Lat_start.gt.dum_dp)then
y_inverted = .true.
else
y_inverted = .false.
endif
call grib_get(igrib,'longitudeOfLastGridPointInDegrees',dum_dp)
if(Lon_start.gt.dum_dp)then
x_inverted = .true.
else
x_inverted = .false.
endif
call grib_get(igrib,'iDirectionIncrementInDegrees',dum_dp)
dx_met_const = real(dum_dp,kind=4)
call grib_get(igrib,'jDirectionIncrementInDegrees',dum_dp)
dy_met_const = real(dum_dp,kind=4)
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.
Lat_start = y_start
Lon_start = x_start
call grib_get(igrib,'numberOfPoints',numberOfPoints)
allocate(lats(numberOfPoints))
allocate(lons(numberOfPoints))
allocate(values(numberOfPoints))
call grib_get_data(igrib,lats,lons,values)
do j=1,ny_fullmet
do i=1,nx_fullmet
idx = (j-1)*nx_fullmet + i
x_fullmet_sp(i) = real(lons(idx),kind=sp)
y_fullmet_sp(j) = real(lats(idx),kind=sp)
enddo
enddo
ReadGrid = .true.
deallocate(lats)
deallocate(lons)
deallocate(values)
call grib_get(igrib,'latitudeOfLastGridPointInDegrees',dum_dp)
if(Lat_start.gt.dum_dp)then
y_inverted = .true.
else
y_inverted = .false.
endif
call grib_get(igrib,'longitudeOfLastGridPointInDegrees',dum_dp)
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 grib_get(igrib,'orientationOfTheGridInDegrees',dum_dp)
Met_lam0 = dum_dp
Met_phi0 = 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
write(MR_global_error,*)"MR ERROR: Alber Equal Area not implemented"
stop 1
elseif(index(dum_str,'UTM').ne.0)then
IsLatLon_MetGrid = .false.
Met_iprojflag = 3
write(MR_global_error,*)"MR ERROR: UTM not implemented"
stop 1
elseif(index(dum_str,'lambert').ne.0)then
IsLatLon_MetGrid = .false.
Met_iprojflag = 4
call grib_get(igrib,'LoVInDegrees',dum_dp)
Met_lam0 = dum_dp
call grib_get(igrib,'LaDInDegrees',dum_dp)
Met_phi0 = dum_dp
call grib_get(igrib,'Latin1InDegrees',dum_dp)
Met_phi1 = dum_dp
call grib_get(igrib,'Latin2InDegrees',dum_dp)
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 grib_get(igrib,'LaDInDegrees',dum_dp)
Met_phi0 = dum_dp
Met_k0 = 0.933_8
Met_Re = 6371.229_8
else
write(MR_global_error,*)'MR ERROR: Cannot determine the projection from the GRIB file.'
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 grib_get(igrib,'DxInMetres',dum_int,stat)
if(stat.ne.0)then
call grib_get(igrib,'DiInMetres',dum_int,stat)
endif
dx_met_const = real(dum_int,kind=4)/1000.0_sp
call grib_get(igrib,'DyInMetres',dum_int,stat)
if(stat.ne.0)then
call grib_get(igrib,'DjInMetres',dum_int,stat)
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)
endif
if(.not.ReadGrid)then
do i = 0,nx_fullmet+1
x_fullmet_sp(i) = real(x_start + (i-1)*dx_met_const,kind=sp)
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
if(MR_GRIB_Version.eq.1)then
call grib_get(igrib,'indicatorOfTypeOfLevel',grb_typeSfc)
! 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 grib_get(igrib,'indicatorOfParameter',grb_parameterNumber)
call grib_get(igrib,'table2Version',grb_Table)
! 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 grib_get(igrib,'level',grb_level)
!call grib_get(igrib,'scaledValueOfFirstFixedSurface',grb_scaledValueOfFirstFixedSurface)
zcount(ivar) = zcount(ivar) + 1
zlev_dum(ivar,zcount(ivar)) = grb_level * 100
endif
enddo
endif
elseif(MR_GRIB_Version.eq.2)then
call grib_get(igrib,'typeOfFirstFixedSurface', 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 grib_get(igrib,'discipline', grb_discipline)
call grib_get(igrib,'parameterCategory', grb_parameterCategory)
call grib_get(igrib,'parameterNumber', grb_parameterNumber)
! 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 grib_get(igrib,'scaledValueOfFirstFixedSurface',grb_scaledValueOfFirstFixedSurface)
zcount(ivar) = zcount(ivar) + 1
zlev_dum(ivar,zcount(ivar)) = grb_scaledValueOfFirstFixedSurface
endif
enddo
endif
endif !MR_GRIB_Version.eq.1
call grib_release(igrib)
call grib_new_from_file(ifile,igrib,iret)
enddo
call grib_release(igrib)
call grib_close_file(ifile)
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
! soley 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
write(MR_global_production,*)" Found these levels"
write(MR_global_production,*)" 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(MR_global_production,*)ivar,Met_var_zdim_idx(ivar),0,0
else
write(MR_global_production,*)ivar,Met_var_zdim_idx(ivar),0,&
nlevs_fullmet(Met_var_zdim_idx(ivar))
endif
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
write(MR_global_info,*)"Dimension info:"
write(MR_global_info,*)" record (time): ",nt_fullmet
write(MR_global_info,*)" level (z) : ",np_fullmet
write(MR_global_info,*)" y : ",ny_fullmet
write(MR_global_info,*)" x : ",nx_fullmet
!************************************************************************
! 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
write(MR_global_production,*)"--------------------------------------------------------------------------------"
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
use grib_api
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
write(MR_global_production,*)"--------------------------------------------------------------------------------"
write(MR_global_production,*)"---------- MR_Read_Met_Times_GRIB ----------"
write(MR_global_production,*)"--------------------------------------------------------------------------------"
if(.not.Met_dim_IsAvailable(1))then
write(MR_global_error,*)"MR ERROR: Time dimension is required and not listed"
write(MR_global_error,*)" in custom windfile specification file."
stop 1
endif
allocate(MR_windfile_starthour(MR_iwindfiles))
if(MR_iwindformat.eq.27)then
! GRIB1 reader not yet working!!
write(MR_global_error,*)"MR ERROR: iwf=27 is a GRIB1 format."
write(MR_global_error,*)" The GRIB1 reader is not yet working"
stop 1
! Here the branch for when MR_iwindformat = 27
! First copy path read in to slot 2
!if(MR_runAsForecast)then
! write(MR_global_error,*)"MR ERROR: iwf=27 cannot be used for forecast runs."
! write(MR_global_error,*)" These are reanalysis files."
! 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,'/'
MR_windfile_starthour(1) = real(HS_hours_since_baseyear( &
MR_Comp_StartYear,1,1,0.0_8,MR_BaseYear,MR_useLeap),kind=sp)
MR_windfile_starthour(2) = real(HS_hours_since_baseyear( &
MR_Comp_StartYear+1,1,1,0.0_8,MR_BaseYear,MR_useLeap),kind=sp)
if ((mod(MR_Comp_StartYear,4).eq.0) .and. &
(mod(MR_Comp_StartYear,100).ne.0).or.(mod(MR_Comp_StartYear,400).eq.0))then
nt_fullmet = 1464 ! Leap year
else
nt_fullmet = 1460 ! Not a leap year
endif
MR_windfiles_nt_fullmet(1)=nt_fullmet
MR_windfiles_nt_fullmet(2)=nt_fullmet ! Note: we don't care if the next
! year is a leap year since
! the simulation will never
! be long enough to matter.
allocate(MR_windfile_stephour(MR_iwindfiles,nt_fullmet))
! the interval for iwf27 is 6 hours
do iwstep = 1,nt_fullmet
MR_windfile_stephour(:,iwstep) = (iwstep-1)*6.0_4
enddo
else
! For all other formats, try to read the first grib message and get
! dataDate, dataTime and forecastTime
! Loop through all the windfiles
do iw = 1,MR_iwindfiles
! Each wind file needs a ref-time which in almost all cases is given
! in the 'units' attribute of the time variable
write(MR_global_info,*)iw,trim(ADJUSTL(MR_windfiles(iw)))
if(iw.eq.1)then
! For now, assume one time step per file
nt_fullmet = 1
write(MR_global_info,*)" Assuming all NWP files have the same number of steps."
write(MR_global_info,*)" For grib, assume one time step per file."
write(MR_global_info,*)" Allocating time arrays for ",MR_iwindfiles,"files"
write(MR_global_info,*)" ",nt_fullmet,"step(s) each"
allocate(MR_windfile_stephour(MR_iwindfiles,nt_fullmet))
endif
call grib_open_file(ifile,trim(ADJUSTL(MR_windfiles(iw))),'R')
call grib_new_from_file(ifile,igrib,iret)
if(iw.eq.1)call grib_get(igrib,'editionNumber',MR_GRIB_Version)
call grib_get(igrib,'dataDate',dataDate)
call grib_get(igrib,'dataTime',dataTime)
if(MR_GRIB_Version.eq.1)then
! The only grib1 files we deal with are reanalysis files with no FC time
forecastTime = 0
else
call grib_get(igrib,'forecastTime',forecastTime)
endif
itstart_year = int(dataDate/10000)
itstart_month = int((dataDate-10000*itstart_year)/100)
itstart_day = mod(dataDate,100)
itstart_hour = int(dataTime/100)
itstart_min = mod(dataTime,100)
itstart_sec = 0
write(MR_global_info,2100)"Ref time = ",itstart_year,itstart_month,itstart_day, &
itstart_hour,itstart_min,itstart_sec
call grib_release(igrib)
call grib_close_file(ifile)
filestart_hour = real(itstart_hour,kind=sp) + &
real(itstart_min,kind=sp)/60.0_sp + &
real(itstart_sec,kind=sp)/3600.0_sp
MR_windfiles_nt_fullmet(iw)=nt_fullmet
MR_windfile_starthour(iw) = real(HS_hours_since_baseyear(itstart_year,itstart_month, &
itstart_day,real(filestart_hour,kind=8),MR_BaseYear,MR_useLeap),kind=4)
MR_windfile_stephour(iw,1) = real(forecastTime,kind=4)
enddo
endif
2100 format(20x,a11,i4,1x,i2,1x,i2,1x,i2,1x,i2,1x,i2)
! Finished setting up the start time of each wind file in HoursSince : MR_windfile_starthour(iw)
! and the forecast (offset from start of file) for each step : MR_windfile_stephour(iw,iwstep)
write(MR_global_info,*)"File, step, Ref, Offset, HoursSince"
do iw = 1,MR_iwindfiles
do iws = 1,nt_fullmet
write(MR_global_info,*)iw,iws,real(MR_windfile_starthour(iw),kind=4),&
real(MR_windfile_stephour(iw,iws),kind=4),&
real(MR_windfile_starthour(iw)+MR_windfile_stephour(iw,iws),kind=4)
enddo
enddo
write(MR_global_production,*)"--------------------------------------------------------------------------------"
end subroutine MR_Read_Met_Times_GRIB
!##############################################################################
!##############################################################################
!
! MR_Read_MetP_Variable_GRIB
!
! Called from Read_HGT_arrays and once from Read_3d_MetP_Variable.
!
! Sets MR_dum3d_metP, MR_dum2d_met, or MR_dum2d_met_int as appropriate
!
!##############################################################################
subroutine MR_Read_MetP_Variable_GRIB(ivar,istep)
use MetReader
use grib_api
implicit none
integer, parameter :: sp = 4 ! single precision
integer, parameter :: dp = 8 ! double precision
integer,intent(in) :: ivar
integer,intent(in) :: istep
integer :: iw,iwstep
integer :: np_met_loc
character(len=71) :: invar
character(len=130) :: index_file
character(len=130) :: grib_file_path
character(len=130) :: grib_file
real(kind=sp) :: del_H,del_P,dpdz
integer :: i,j,k
integer :: kk
integer :: kkk,itmp
integer :: ict, ileft(2),iright(2) !if wrapgrid=.true. ict=2 and left & iright have 2 values, otherwise 1
integer :: iistart(2),iicount(2) !if (wrapgrid), iistart(1)=istart, iistart(2)=1
integer :: Dimension_of_Variable
logical :: IsCategorical
integer,dimension(np_fullmet) :: p_met_loc
integer :: ifile
integer :: igrib
integer :: idx
integer :: iret
integer :: l,m,t
integer :: count1=0
integer :: rstrt, rend
real(kind=8),dimension(:),allocatable :: values
real(kind=8),dimension(:,:),allocatable :: slice
integer(kind=4) :: numberOfPoints
integer(kind=4) :: Ni
integer(kind=4) :: Nj
integer(kind=4) :: typeOfFirstFixedSurface
! Stores values of keys read from grib file
!character(len=7) :: grb_marsParam
character(len=4) :: grb_typeSfc
integer(kind=4) :: grb_discipline
integer(kind=4) :: grb_parameterCategory
integer(kind=4) :: grb_parameterNumber
integer(kind=4) :: grb_level
integer(kind=4) :: grb_scaledValueOfFirstFixedSurface
!character(len=9) :: sName
!integer(kind=4) :: forecastTime
!integer(kind=4),dimension(:),allocatable :: marsParam_idx
integer(kind=4),dimension(:),allocatable :: discipline_idx
integer(kind=4),dimension(:),allocatable :: parameterCategory_idx
integer(kind=4),dimension(:),allocatable :: parameterNumber_idx
integer(kind=4),dimension(:),allocatable :: level_idx
integer(kind=4),dimension(:),allocatable :: forecastTime_idx
!integer(kind=4) :: marsParamSize
integer(kind=4) :: disciplineSize
integer(kind=4) :: parameterCategorySize
integer(kind=4) :: parameterNumberSize
integer(kind=4) :: levelSize
integer(kind=4) :: forecastTimeSize
! temporary versions of those stored in Met_var_GRIB[...](ivar)
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=7) :: iv_marsParam
character(len=3) :: iv_typeSfc
real(kind=sp) :: Z_top, T_top
real(kind=sp),dimension(:,:,:),allocatable :: full_values
logical :: Use_GRIB_Index = .false.
integer :: fn_idx
if(.not.Met_var_IsAvailable(ivar))then
write(MR_global_error,*)"MR ERROR: Variable not available for this windfile"
write(MR_global_error,*)" ivar = ",ivar
write(MR_global_error,*)" vname = ",Met_var_GRIB_names(ivar)
write(MR_global_error,*)" iwf = ",MR_iwindformat
stop 1
endif
if(ivar.eq.3 .or. & ! V_isobaric
ivar.eq.12)then ! V_height_above_ground
! The v components might be not reachable with the indexing if they are the second
! component of a multi-variable message
Use_GRIB_Index = .false.
! else
! Use_GRIB_Index = .true.
endif
if(MR_GRIB_Version.eq.1)then
! grib1 uses an index based on Param
iv_paramN = Met_var_GRIB1_Param(ivar)
iv_Table = Met_var_GRIB1_Table(ivar)
iv_typeSfc = Met_var_GRIB1_St(ivar)
elseif(MR_GRIB_Version.eq.2)then
! Get the variable discipline, Parameter Category, Parameter Number, and
! level type for this variable
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)
else
write(MR_global_error,*)"MR ERROR: GRIB type not determined"
stop 1
endif
iw = MR_MetStep_findex(istep)
iwstep = MR_MetStep_tindex(istep)
if(Met_var_GRIB_names(ivar).eq."")then
write(MR_global_error,*)"Variable ",ivar," not available for MR_iwindformat = ",&
MR_iwindformat
stop 1
endif
! Get the dimension of the variable requested (either 2 or 3-D)
if(ivar.eq.1 ) Dimension_of_Variable = 3 ! Geopotential Height
if(ivar.eq.2 ) Dimension_of_Variable = 3 ! Vx
if(ivar.eq.3 ) Dimension_of_Variable = 3 ! Vy
if(ivar.eq.4 ) Dimension_of_Variable = 3 ! Vz
if(ivar.eq.5 ) Dimension_of_Variable = 3 ! Temperature
if(ivar.eq.6 ) Dimension_of_Variable = 3 ! Pressure (only for WRF or other eta-level files)
if(ivar.eq.10) Dimension_of_Variable = 2 ! Planetary Boundary Layer Height
if(ivar.eq.11) Dimension_of_Variable = 2 ! U @ 10m
if(ivar.eq.12) Dimension_of_Variable = 2 ! V @ 10m
if(ivar.eq.13) Dimension_of_Variable = 2 ! Friction velocity
if(ivar.eq.14) Dimension_of_Variable = 2 ! Displacement Height
if(ivar.eq.15) Dimension_of_Variable = 2 ! Snow cover
if(ivar.eq.16) Dimension_of_Variable = 2 ! Soil moisture
if(ivar.eq.17) Dimension_of_Variable = 2 ! Surface roughness
if(ivar.eq.18) Dimension_of_Variable = 2 ! Wind_speed_gust_surface
if(ivar.eq.20) Dimension_of_Variable = 2 ! pressure at lower cloud base
if(ivar.eq.21) Dimension_of_Variable = 2 ! pressure at lower cloud top
if(ivar.eq.22) Dimension_of_Variable = 2 ! temperature at lower cloud top
if(ivar.eq.23) Dimension_of_Variable = 2 ! Total Cloud cover
if(ivar.eq.24) Dimension_of_Variable = 2 ! Cloud cover (low)
if(ivar.eq.25) Dimension_of_Variable = 2 ! Cloud cover (convective)
if(ivar.eq.30) Dimension_of_Variable = 3 ! Rel. Hum
if(ivar.eq.31) Dimension_of_Variable = 3 ! QV (specific humidity)
if(ivar.eq.32) Dimension_of_Variable = 3 ! QL (liquid)
if(ivar.eq.33) Dimension_of_Variable = 3 ! QI (ice)
if(ivar.eq.40) Dimension_of_Variable = 2 ! Categorical rain
if(ivar.eq.41) Dimension_of_Variable = 2 ! Categorical snow
if(ivar.eq.42) Dimension_of_Variable = 2 ! Categorical frozen rain
if(ivar.eq.43) Dimension_of_Variable = 2 ! Categorical ice
if(ivar.eq.44) Dimension_of_Variable = 2 ! Precipitation rate large-scale (liquid)
if(ivar.eq.45) Dimension_of_Variable = 2 ! Precipitation rate convective (liquid)
if(ivar.eq.46) Dimension_of_Variable = 3 ! Precipitation rate large-scale (ice)
if(ivar.eq.47) Dimension_of_Variable = 3 ! Precipitation rate convective (ice)
if(ivar.eq.40.or.&
ivar.eq.41.or.&
ivar.eq.42.or.&
ivar.eq.43)then
! Categorical variables are integers and need special interpolation
IsCategorical = .true.
else
! The default is to read floating point values
IsCategorical = .false.
endif
if(MR_iwindformat.eq.27)then
! Get correct GRIB1 file
write(MR_global_error,*)"MR ERROR: iwf27 not working for GRIB1"
stop 1
if(ivar.eq.1)then
write(index_file,125)trim(adjustl(MR_MetStep_File(istep))), &
"pgrbanl_mean_",MR_iwind5_year(istep), &
"_HGT_pres.nc"
np_met_loc = np_fullmet
elseif(ivar.eq.2)then
write(index_file,126)trim(adjustl(MR_MetStep_File(istep))), &
"pgrbanl_mean_",MR_iwind5_year(istep), &
"_UGRD_pres.nc"
np_met_loc = np_fullmet
elseif(ivar.eq.3)then
write(index_file,126)trim(adjustl(MR_MetStep_File(istep))), &
"pgrbanl_mean_",MR_iwind5_year(istep), &
"_VGRD_pres.nc"
np_met_loc = np_fullmet
elseif(ivar.eq.4)then
write(index_file,126)trim(adjustl(MR_MetStep_File(istep))), &
"pgrbanl_mean_",MR_iwind5_year(istep), &
"_VVEL_pres.nc"
write(*,*)"+++++++++++++++++++++++++++++++++++++++++++"
write(*,*)" NEED TO FIX THIS Vz"
np_met_loc = np_fullmet
elseif(ivar.eq.5)then
write(index_file,125)trim(adjustl(MR_MetStep_File(istep))), &
"pgrbanl_mean_",MR_iwind5_year(istep), &
"_TMP_pres.nc"
np_met_loc = np_fullmet
elseif(ivar.eq.10)then
write(index_file,128)trim(adjustl(MR_MetStep_File(istep))), &
"sflxgrbfg_mean_",MR_iwind5_year(istep), &
"_HPBL_sfc.nc"
elseif(ivar.eq.22)then
write(index_file,130)trim(adjustl(MR_MetStep_File(istep))), &
"sflxgrbfg_mean_",MR_iwind5_year(istep), &
"_TMP_low-cldtop.nc"
elseif(ivar.eq.23)then
write(index_file,131)trim(adjustl(MR_MetStep_File(istep))), &
"sflxgrbfg_mean_",MR_iwind5_year(istep), &
"_TCDC_low-cldlay.nc"
elseif(ivar.eq.30)then
write(index_file,127)trim(adjustl(MR_MetStep_File(istep))), &
"pgrbanl_mean_",MR_iwind5_year(istep), &
"_RH_pres.nc"
write(*,*)"+++++++++++++++++++++++++++++++++++++++++++"
write(*,*)" NEED TO FIX THIS : RH"
np_met_loc = np_fullmet