-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathg2getgb2.F90
1452 lines (1340 loc) · 53.4 KB
/
g2getgb2.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
!> @file
!> @brief The getgb2 subroutines find and unpack a GRIB2 message in a
!> file.
!> @author Ed Hartnett @date Mar 6, 2024
!> This is a legacy version of getgb2i2(). It finds and unpacks a
!> GRIB2 message in a file, using an version 1 index record which is
!> either found in memory, read from an index file, or generated.
!>
!> Users of this routine must:
!> 1. include a use grib_mod
!> 2. call gf_free() on the @ref grib_mod::gribfield parameter
!> 3. free library memory with gf_finalize()
!>
!> For more details, see getgb2i2().
!>
!> @param[in] lugb integer unit of the unblocked grib data file.
!> File must be opened with [baopen() or baopenr()]
!> (https://noaa-emc.github.io/NCEPLIBS-bacio/) before calling
!> this routine.
!> @param[in] lugi integer unit of the unblocked grib index file.
!> If nonzero, file must be opened with [baopen() or baopenr()]
!> (https://noaa-emc.github.io/NCEPLIBS-bacio/) before
!> calling this routine. lugi may be:
!> - > 0 read index from index file lugi, if index doesn"t already exist.
!> - = 0 to get index buffer from the grib file, if index
!> doesn"t already exist.
!> - < 0 force reread of index from index file abs(lugi).
!> - = lugb force regeneration of index from GRIB2 file lugb.
!> @param[in] j integer number of fields to skip (0 to search from
!> beginning).
!> @param[in] jdisc GRIB2 discipline number of requested field:
!> --1 accept any discipline
!> - 0 meteorological products
!> - 1 hydrological products
!> - 2 land surface products
!> - 3 space products
!> - 10 oceanographic products
!> @param[in] JIDS integer array of values in the identification section
!> (=-9999 for wildcard).
!> @param[in] jpdtn integer product definition template number (n)
!> (if = -1, don't bother matching pdt - accept any)
!> @param[in] JPDT integer array of values defining the product definition
!> template 4.n of the field for which to search (=-9999 for wildcard)
!> @param[in] jgdtn integer grid definition template number (m)
!> (if = -1, don't bother matching gdt - accept any )
!> @param[in] JGDT integer array of values defining the grid definition
!> template 3.m of the field for which to search (=-9999 for wildcard)
!> @param[in] unpack logical value indicating whether to unpack bitmap/data
!> - .TRUE. unpack bitmap and data values
!> - .FALSE. do not unpack bitmap and data values
!> @param[out] k integer field number unpacked
!> @param[out] gfld derived type @ref grib_mod::gribfield.
!> @param[out] iret integer return code
!> - 0 all ok
!> - 96 error reading index
!> - 97 error reading grib file
!> - 99 request not found
!> - other gf_getfld grib2 unpacker return code
!>
!> @author Mark Iredell, Ed Hartnett @date 1994-04-01
subroutine getgb2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
unpack, k, gfld, iret)
use grib_mod
implicit none
integer, intent(in) :: lugb, lugi, j, jdisc
integer, dimension(:) :: jids(*)
integer, intent(in) :: jpdtn
integer, dimension(:) :: jpdt(*)
integer, intent(in) :: jgdtn
integer, dimension(:) :: jgdt(*)
logical, intent(in) :: unpack
integer, intent(out) :: k
type(gribfield), intent(out) :: gfld
integer, intent(out) ::iret
integer :: idxver = 1
call getgb2i2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
unpack, idxver, k, gfld, iret)
end subroutine getgb2
!> Find and unpack a GRIB2 message in a file, using an version 1 or 2
!> index record which is either found in memory, read from an index
!> file, or generated.
!>
!> This subroutine is similar to getgb2(), but allows the caller to
!> specify the index version of the generated index (if one is
!> generated). Use index version 2 for all new code, as it handles all
!> GRIB2 files, including files > 2 GB. Use index version 1 for
!> backward compatibility, but it does not work past the first 2 GB of
!> the file.
!>
!> The GRIB field request specifies the number of fields to skip and
!> the unpacked identification section, grid definition template and
!> product defintion section parameters. (A requested parameter of
!> -9999 means to allow any value of this parameter to be found.)
!>
!> If the requested GRIB field is found, then it is read from the GRIB
!> file and unpacked. Its number is returned along with the associated
!> unpacked parameters. the bitmap (if any); the data values are
!> unpacked only if argument unpack is set to true. If the GRIB
!> field is not found, then the return code will be nonzero.
!>
!> The decoded information for the selected GRIB field is returned in
!> a derived type variable, gfld, which is of type @ref
!> grib_mod::gribfield. Users of this routine will need to include the
!> line "use grib_mod" in their calling routine.
!>
!> Derived type @ref grib_mod::gribfield contains pointers to many
!> arrays of data. Users must free this memory by calling gf_free().
!>
!> This subroutine calls getidx2(), which allocates memory and stores
!> the resulting pointers in an array that is a Fortran "save"
!> variable. The result is that the memory will not be freed by the
!> library and cannot be reached by the caller. To free this memory
!> call gf_finalize() after all library operations are complete.
!>
!> @note Specify an index file if feasible to increase speed. Do not
!> engage the same logical unit from more than one processor.
!>
!> @param[in] lugb integer unit of the unblocked grib data file.
!> File must be opened with [baopen() or baopenr()]
!> (https://noaa-emc.github.io/NCEPLIBS-bacio/) before calling
!> this routine.
!> @param[in] lugi integer unit of the unblocked grib index file.
!> If nonzero, file must be opened with [baopen() or baopenr()]
!> (https://noaa-emc.github.io/NCEPLIBS-bacio/) before
!> calling this routine. lugi may be:
!> - > 0 read index from index file lugi, if index doesn"t already exist.
!> - = 0 to get index buffer from the grib file, if index
!> doesn"t already exist.
!> - < 0 force reread of index from index file abs(lugi).
!> - = lugb force regeneration of index from GRIB2 file lugb.
!> @param[in] j integer number of fields to skip (0 to search from
!> beginning).
!> @param[in] jdisc GRIB2 discipline number of requested field:
!> --1 accept any discipline
!> - 0 meteorological products
!> - 1 hydrological products
!> - 2 land surface products
!> - 3 space products
!> - 10 oceanographic products
!> @param[in] JIDS integer array of values in the identification section
!> (=-9999 for wildcard)
!> - JIDS(1) identification of originating centre
!> (see common code table c-1)
!> - JIDS(2) identification of originating sub-centre
!> - JIDS(3) grib master tables version number
!> (see code table 1.0) 0 experimental;1 initial operational version number.
!> - JIDS(4) grib local tables version number (see code table 1.1)
!> 0 local tables not used; 1-254 number of local tables version used.
!> - JIDS(5) significance of reference time (code table 1.2)
!> 0 analysis; 1 start of forecast; 2 verifying time of forecast; 3 observation time
!> - JIDS(6) year (4 digits)
!> - JIDS(7) month
!> - JIDS(8) day
!> - JIDS(9) hour
!> - JIDS(10) minute
!> - JIDS(11) second
!> - JIDS(12) production status of processed data (see code table 1.3)
!> 0 operational products; 1 operational test products;
!> 2 research products; 3 re-analysis products.
!> - JIDS(13) type of processed data (see code table 1.4)
!> 0 analysis products; 1 forecast products; 2 analysis and forecast
!> products; 3 control forecast products; 4 perturbed forecast products;
!> 5 control and perturbed forecast products; 6 processed satellite
!> observations; 7 processed radar observations.
!> @param[in] jpdtn integer product definition template number (n)
!> (if = -1, don't bother matching pdt - accept any)
!> @param[in] JPDT integer array of values defining the product definition
!> template 4.n of the field for which to search (=-9999 for wildcard)
!> @param[in] jgdtn integer grid definition template number (m)
!> (if = -1, don't bother matching gdt - accept any )
!> @param[in] JGDT integer array of values defining the grid definition
!> template 3.m of the field for which to search (=-9999 for wildcard)
!> @param[in] unpack logical value indicating whether to unpack bitmap/data
!> - .TRUE. unpack bitmap and data values
!> - .FALSE. do not unpack bitmap and data values
!> @param[in] idxver Index version of the cindex buffer.
!> @param[out] k integer field number unpacked
!> @param[out] gfld derived type @ref grib_mod::gribfield.
!> @param[out] iret integer return code
!> - 0 all ok
!> - 96 error reading index
!> - 97 error reading grib file
!> - 99 request not found
!> - other gf_getfld grib2 unpacker return code
!>
!> @author Ed Hartnett, Mark Iredell @date 2024-03-19
subroutine getgb2i2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
unpack, idxver, k, gfld, iret)
use g2logging
use grib_mod
implicit none
integer, intent(in) :: lugb, lugi, j, jdisc
integer, dimension(:) :: jids(*)
integer, intent(in) :: jpdtn
integer, dimension(:) :: jpdt(*)
integer, intent(in) :: jgdtn
integer, dimension(:) :: jgdt(*)
logical, intent(in) :: unpack
integer, intent(in) :: idxver
integer, intent(out) :: k
type(gribfield), intent(out) :: gfld
integer, intent(out) ::iret
character(len = 1), pointer, dimension(:) :: cbuf
integer :: nnum, nlen, lpos, jk, irgi, irgs
! Declare interfaces (required for cbuf pointer).
interface
subroutine getidx2(lugb, lugi, idxver, cindex, nlen, nnum, iret)
integer, intent(in) :: lugb, lugi, idxver
character(len = 1), pointer, dimension(:) :: cindex
integer, intent(out) :: nlen, nnum, iret
end subroutine getidx2
subroutine getgb2s2(cbuf, idxver, nlen, nnum, j, jdisc, jids, jpdtn, jpdt, jgdtn, &
jgdt, k, gfld, lpos, iret)
import gribfield
character(len = 1), intent(in) :: cbuf(nlen)
integer, intent(in) :: idxver, nlen, nnum, j, jdisc
integer, dimension(:) :: jids(*)
integer, intent(in) :: jpdtn
integer, dimension(:) :: jpdt(*)
integer, intent(in) :: jgdtn
integer, dimension(:) :: jgdt(*)
integer, intent(out) :: k
type(gribfield), intent(out) :: gfld
integer, intent(out) :: lpos, iret
end subroutine getgb2s2
subroutine getgb2l2(lugb, idxver, cindex, gfld, iret)
use grib_mod
integer, intent(in) :: lugb, idxver
character(len = 1), intent(in) :: cindex(*)
type(gribfield) :: gfld
integer, intent(out) :: iret
end subroutine getgb2l2
subroutine getgb2r2(lugb, idxver, cindex, gfld, iret)
use grib_mod
implicit none
integer, intent(in) :: lugb, idxver
character(len=1), intent(in) :: cindex(*)
type(gribfield) :: gfld
integer, intent(out) :: iret
end subroutine getgb2r2
end interface
#ifdef LOGGING
write(g2_log_msg, '(a, i2, a, i2, a, i5, a, i5, a, l, a, i1)') 'getgb2i2: lugb ', lugb, ' lugi ', lugi, &
' j ', j, ' jdisc ', jdisc, ' unpack ', unpack, ' idxver ', idxver
call g2_log(1)
#endif
! Fill cbuf with the index records of this file, by recalling them
! from memory, reading them from the index file, or generating them
! from the data file.
irgi = 0
call getidx2(lugb, lugi, idxver, cbuf, nlen, nnum, irgi)
if (irgi .gt. 1) then
iret = 96
return
endif
! Search the index in cbuf for the first message which meets our
! search criteria.
call getgb2s2(cbuf, idxver, nlen, nnum, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, jk, &
gfld, lpos, irgs)
if (irgs .ne. 0) then
iret = 99
call gf_free(gfld)
return
endif
! Read local use section for the selected GRIB2 message, if available.
call getgb2l2(lugb, idxver, cbuf(lpos), gfld, iret)
! Read and unpack grib record for the selected GRIB2 message.
if (unpack) then
call getgb2r2(lugb, idxver, cbuf(lpos), gfld, iret)
endif
! Return the number of the unpacked field to the caller.
k = jk
end subroutine getgb2i2
!> Read and unpack a local use section from a GRIB2 index record
!> (index format 1) and GRIB2 file.
!>
!> This subprogram is intended for private use by getgb2() routine
!> only.
!>
!> Note that derived type gribfield contains pointers to many arrays
!> of data. Users must free this memory with gf_free().
!>
!> This subroutine supports only index format 1, which will not work
!> for files > 2 GB. New code should use getgb2l2().
!>
!> @param[in] lugb integer unit of the unblocked grib data file.
!> @param[in] cindex index record of the grib field (see ix2gb2() for
!> description of an index record.)
!> @param[out] gfld derived type gribfield @ref grib_mod::gribfield.
!> @param[out] iret integer return code
!> - 0 all ok
!> - 97 error reading grib file
!> - other gf_getfld grib2 unpacker return code
!>
!> @author Stephen Gilbert @date 2002-05-07
subroutine getgb2l(lugb, cindex, gfld, iret)
use grib_mod
implicit none
integer, intent(in) :: lugb
character(len = 1), intent(in) :: cindex(*)
type(gribfield) :: gfld
integer, intent(out) :: iret
interface
subroutine getgb2l2(lugb, idxver, cindex, gfld, iret)
use grib_mod
integer, intent(in) :: lugb, idxver
character(len = 1), intent(in) :: cindex(*)
type(gribfield) :: gfld
integer, intent(out) :: iret
end subroutine getgb2l2
end interface
call getgb2l2(lugb, 1, cindex, gfld, iret)
end subroutine getgb2l
!> Read and unpack a local use section from a GRIB2 index record
!> (index format 1 or 2) and GRIB2 file.
!>
!> This subroutine decodes information for the selected GRIB2 field and
!> returns it in a derived type variable, gfld. gfld is of type @ref
!> grib_mod::gribfield. Users of this routine will need to include the
!> line "use grib_mod" in their calling routine.
!>
!> This subprogram is intended for private use by getgb2() routine
!> only.
!>
!> Note that derived type gribfield contains pointers to many arrays
!> of data. Users must free this memory with gf_free().
!>
!> @param[in] lugb integer unit of the unblocked grib data file.
!> @param[in] idxver Index version of the cindex buffer.
!> @param[in] cindex index record of the grib field (see ix2gb2() for
!> description of an index record.)
!> @param[out] gfld derived type gribfield @ref grib_mod::gribfield.
!> @param[out] iret integer return code
!> - 0 all ok
!> - 97 error reading grib file
!> - other gf_getfld grib2 unpacker return code
!>
!> @author Stephen Gilbert @date 2002-05-07
subroutine getgb2l2(lugb, idxver, cindex, gfld, iret)
use g2logging
use grib_mod
implicit none
integer, intent(in) :: lugb, idxver
character(len = 1), intent(in) :: cindex(*)
type(gribfield) :: gfld
integer, intent(out) :: iret
integer :: lskip, skip2
integer (kind = 8) :: lskip8, iskip8, lread8, ilen8, skip28
character(len = 1):: csize(4)
character(len = 1), allocatable :: ctemp(:)
integer :: ilen, iofst, ierr
integer :: INT1_BITS, INT2_BITS, INT4_BITS, INT8_BITS
parameter(INT1_BITS = 8, INT2_BITS = 16, INT4_BITS = 32, INT8_BITS = 64)
integer :: mypos
interface
subroutine g2_gbytec1(in, siout, iskip, nbits)
character*1, intent(in) :: in(*)
integer, intent(inout) :: siout
integer, intent(in) :: iskip, nbits
end subroutine g2_gbytec1
subroutine g2_gbytec81(in, siout, iskip, nbits)
character*1, intent(in) :: in(*)
integer (kind = 8), intent(inout) :: siout
integer, intent(in) :: iskip, nbits
integer (kind = 8) :: iout(1)
end subroutine g2_gbytec81
subroutine gf_unpack2(cgrib, lcgrib, iofst, lencsec2, csec2, ierr)
character(len = 1), intent(in) :: cgrib(lcgrib)
integer, intent(in) :: lcgrib
integer, intent(inout) :: iofst
integer, intent(out) :: lencsec2
integer, intent(out) :: ierr
character(len = 1), pointer, dimension(:) :: csec2
end subroutine gf_unpack2
end interface
#ifdef LOGGING
write(g2_log_msg, '(a, i2, a, i1)') 'getgb2l2: lugb ', lugb, ' idxver ', idxver
call g2_log(1)
#endif
! Get info.
nullify(gfld%local)
iret = 0
mypos = INT4_BITS ! Skip length of index record.
! These are all 4-byte ints in index format 1, and 8-byte ints in
! index version 2.
if (idxver .eq. 1) then
! Read bytes to skip in file before message.
call g2_gbytec1(cindex, lskip, mypos, INT4_BITS)
mypos = mypos + INT4_BITS
lskip8 = lskip
! Read bytes to skip in msg before local use.
call g2_gbytec1(cindex, skip2, mypos, INT4_BITS)
skip28 = skip2
else
! Read bytes to skip in file before message.
call g2_gbytec81(cindex, lskip8, mypos, INT8_BITS)
mypos = mypos + INT8_BITS
! Read bytes to skip in msg before local use.
call g2_gbytec81(cindex, skip28, mypos, INT8_BITS)
mypos = mypos + INT8_BITS
endif
! Read and unpack local use section, if present.
if (skip28 .ne. 0) then
iskip8 = lskip8 + skip28
! Get length of section.
call bareadl(lugb, iskip8, 4_8, lread8, csize)
call g2_gbytec1(csize, ilen, 0, 32)
allocate(ctemp(ilen))
ilen8 = ilen
! Read in section.
call bareadl(lugb, iskip8, ilen8, lread8, ctemp)
if (ilen8 .ne. lread8) then
iret = 97
deallocate(ctemp)
return
endif
iofst = 0
call gf_unpack2(ctemp, ilen, iofst, gfld%locallen, gfld%local, ierr)
if (ierr .ne. 0) then
iret = 98
deallocate(ctemp)
return
endif
deallocate(ctemp)
else
gfld%locallen = 0
endif
end subroutine getgb2l2
!> Legacy subroutine to find and extract a GRIB2 message from a
!> file. Use getgb2p2() for new code.
!>
!> This subroutine reads a GRIB index file (or optionally the GRIB
!> file itself) to get the index buffer (i.e. table of contents) for
!> the GRIB file. It finds in the index buffer a reference to the
!> GRIB field requested.
!>
!> The GRIB field request specifies the number of fields to skip and
!> the unpacked identification section, grid definition template and
!> product defintion section parameters. (A requested parameter of
!> -9999 means to allow any value of this parameter to be found.)
!>
!> If the requested GRIB field is found, then it is read from the GRIB
!> file and unpacked. If the GRIB field is not found, then the return
!> code will be nonzero.
!>
!> The derived type @ref grib_mod::gribfield contains allocated memory
!> that must be freed by the caller with subroutine gf_free().
!>
!> @note Specifing an index file may increase speed.
!> Do not engage the same logical unit from more than one processor.
!>
!> @param[in] lugb Unit of the unblocked GRIB data file. The
!> file must have been opened with [baopen() or baopenr()]
!> (https://noaa-emc.github.io/NCEPLIBS-bacio/) before calling this
!> routine.
!> @param[in] lugi Unit of the unblocked GRIB index file. If
!> nonzero, file must have been opened with [baopen() or baopenr()]
!> (https://noaa-emc.github.io/NCEPLIBS-bacio/) before calling this
!> subroutine. Set to 0 to get index buffer from the GRIB file.
!> @param[in] j Number of fields to skip (set to 0 to search
!> from beginning).
!> @param[in] jdisc GRIB2 discipline number of requested field. See
!> [GRIB2 - TABLE 0.0 -
!> DISCIPLINE](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_table0-0.shtml).
!> Use -1 to accept any discipline.
!> @param[in] jids Array of values in the identification
!> section. (Set to -9999 for wildcard.)
!> @param[in] jpdtn Product Definition Template (PDT) number (n)
!> (if = -1, don't bother matching PDT - accept any)
!> @param[in] jpdt Array of values defining the Product Definition
!> Template of the field for which to search (=-9999 for wildcard).
!> @param[in] jgdtn Grid Definition Template (GDT) number (if = -1,
!> don't bother matching GDT - accept any).
!> @param[in] jgdt array of values defining the Grid Definition
!> Template of the field for which to search (=-9999 for wildcard).
!> @param[in] extract value indicating whether to return a
!> GRIB2 message with just the requested field, or the entire
!> GRIB2 message containing the requested field.
!> - .true. return GRIB2 message containing only the requested field.
!> - .false. return entire GRIB2 message containing the requested field.
!> @param[out] k field number unpacked.
!> @param[out] gribm returned GRIB message.
!> @param[out] leng length of returned GRIB message in bytes.
!> @param[out] iret integer return code
!> - 0 No error.
!> - 96 Error reading index.
!> - 97 Error reading GRIB file.
!> - 99 Request not found.
!>
!> @author Mark Iredell @date 1994-04-01
subroutine getgb2p(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
extract, k, gribm, leng, iret)
use grib_mod
implicit none
integer, intent(in) :: lugb, lugi, j, jdisc
integer, dimension(:) :: jids(*)
integer, intent(in) :: jpdtn
integer, dimension(:) :: jpdt(*)
integer, intent(in) :: jgdtn
integer, dimension(:) :: jgdt(*)
logical, intent(in) :: extract
integer, intent(out) :: k
character(len = 1), pointer, dimension(:) :: gribm
integer, intent(out) :: leng, iret
integer :: idxver
integer (kind = 8) :: leng8
interface
subroutine getgb2p2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
extract, idxver, k, gribm, leng8, iret)
integer, intent(in) :: lugb, lugi, j, jdisc
integer, dimension(:) :: jids(*)
integer, intent(in) :: jpdtn
integer, dimension(:) :: jpdt(*)
integer, intent(in) :: jgdtn
integer, dimension(:) :: jgdt(*)
logical, intent(in) :: extract
integer, intent(inout) :: idxver
integer, intent(out) :: k
character(len = 1), pointer, dimension(:) :: gribm
integer (kind = 8), intent(out) :: leng8
integer, intent(out) :: iret
end subroutine getgb2p2
end interface
! Call the new version of this subroutine, which handles messages > 2 GB.
idxver = 1
call getgb2p2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
extract, idxver, k, gribm, leng8, iret)
leng = int(leng8, kind(4))
end subroutine getgb2p
!> Find and extract a GRIB2 message from a file.
!>
!> This subroutine reads a GRIB index file (or optionally the GRIB
!> file itself) to get the index buffer (i.e. table of contents) for
!> the GRIB file. It finds in the index buffer a reference to the
!> GRIB field requested.
!>
!> The GRIB field request specifies the number of fields to skip and
!> the unpacked identification section, grid definition template and
!> product defintion section parameters. (A requested parameter of
!> -9999 means to allow any value of this parameter to be found.)
!>
!> If the requested GRIB field is found, then it is read from the GRIB
!> file and unpacked. If the GRIB field is not found, then the return
!> code will be nonzero.
!>
!> The derived type @ref grib_mod::gribfield contains allocated memory
!> that must be freed by the caller with subroutine gf_free().
!>
!> @note Specifing an index file may increase speed.
!> Do not engage the same logical unit from more than one processor.
!>
!> @param[in] lugb Unit of the unblocked GRIB data file. The
!> file must have been opened with [baopen() or baopenr()]
!> (https://noaa-emc.github.io/NCEPLIBS-bacio/) before calling this
!> routine.
!> @param[in] lugi Unit of the unblocked GRIB index file. If
!> nonzero, file must have been opened with [baopen() or baopenr()]
!> (https://noaa-emc.github.io/NCEPLIBS-bacio/) before calling this
!> subroutine. Set to 0 to get index buffer from the GRIB file.
!> @param[in] j Number of fields to skip (set to 0 to search
!> from beginning).
!> @param[in] jdisc GRIB2 discipline number of requested field. See
!> [GRIB2 - TABLE 0.0 -
!> DISCIPLINE](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_table0-0.shtml).
!> Use -1 to accept any discipline.
!> @param[in] jids Array of values in the identification
!> section. (Set to -9999 for wildcard.)
!> - jids(1) Identification of originating centre. See [TABLE 0 -
!> NATIONAL/INTERNATIONAL ORIGINATING
!> CENTERS](https://www.nco.ncep.noaa.gov/pmb/docs/on388/table0.html).
!> - jids(2) Identification of originating sub-centre. See [TABLE C -
!> NATIONAL
!> SUB-CENTERS](https://www.nco.ncep.noaa.gov/pmb/docs/on388/tablec.html).
!> - jids(3) GRIB master tables version number. See [GRIB2 - TABLE 1.0
!> - GRIB Master Tables Version
!> Number](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_table1-0.shtml).
!> - jids(4) GRIB local tables version number. See [GRIB2 - TABLE 1.1
!> - GRIB Local Tables Version
!> Number](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_table1-1.shtml).
!> - jids(5) Significance of reference time. See [GRIB2 - TABLE 1.2 -
!> Significance of Reference
!> Time](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_table1-2.shtml).
!> - jids(6) year (4 digits)
!> - jids(7) month
!> - jids(8) day
!> - jids(9) hour
!> - jids(10) minute
!> - jids(11) second
!> - jids(12) Production status of processed data. See [GRIB2 - TABLE
!> 1.3 - Production Status of
!> Data](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_table1-3.shtml).
!> - jids(13) Type of processed data. See [GRIB2 - TABLE 1.4 - TYPE OF
!> DATA](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_table1-4.shtml).
!> @param[in] jpdtn Product Definition Template (PDT) number (n)
!> (if = -1, don't bother matching PDT - accept any)
!> @param[in] jpdt Array of values defining the Product Definition
!> Template of the field for which to search (=-9999 for wildcard).
!> @param[in] jgdtn Grid Definition Template (GDT) number (if = -1,
!> don't bother matching GDT - accept any).
!> @param[in] jgdt array of values defining the Grid Definition
!> Template of the field for which to search (=-9999 for wildcard).
!> @param[in] extract value indicating whether to return a
!> GRIB2 message with just the requested field, or the entire
!> GRIB2 message containing the requested field.
!> - .true. return GRIB2 message containing only the requested field.
!> - .false. return entire GRIB2 message containing the requested field.
!> @param[in] idxver The index version, use 2 for new code, 1 for
!> legacy index files.
!> @param[out] k field number unpacked.
!> @param[out] gribm returned GRIB message.
!> @param[out] leng8 length of returned GRIB message in bytes.
!> @param[out] iret integer return code
!> - 0 No error.
!> - 96 Error reading index.
!> - 97 Error reading GRIB file.
!> - 99 Request not found.
!>
!> @author Alex Richert, Edward Hartnett @date 2024-05-21
subroutine getgb2p2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
extract, idxver, k, gribm, leng8, iret)
use grib_mod
implicit none
integer, intent(in) :: lugb, lugi, j, jdisc
integer, dimension(:) :: jids(*)
integer, intent(in) :: jpdtn
integer, dimension(:) :: jpdt(*)
integer, intent(in) :: jgdtn
integer, dimension(:) :: jgdt(*)
logical, intent(in) :: extract
integer, intent(inout) :: idxver
integer, intent(out) :: k
character(len = 1), pointer, dimension(:) :: gribm
integer (kind = 8), intent(out) :: leng8
integer, intent(out) :: iret
type(gribfield) :: gfld
integer :: irgi, irgs, jk, lpos, mskp, nlen, nmess, nnum
character(len = 1), pointer, dimension(:) :: cbuf
integer (kind = 8) :: msk1, msk2
parameter(msk1 = 32000, msk2 = 4000)
interface
subroutine getg2i(lugi, cbuf, nlen, nnum, iret)
character(len = 1), pointer, dimension(:) :: cbuf
integer, intent(in) :: lugi
integer, intent(out) :: nlen, nnum, iret
end subroutine getg2i
subroutine getg2ir(lugb, msk1, msk2, mnum, cbuf, nlen, nnum, &
nmess, iret)
character(len = 1), pointer, dimension(:) :: cbuf
integer, intent(in) :: lugb, msk1, msk2, mnum
integer, intent(out) :: nlen, nnum, nmess, iret
end subroutine getg2ir
subroutine getg2i2(lugi, cbuf, idxver, nlen, nnum, iret)
integer, intent(in) :: lugi
character(len=1), pointer, dimension(:) :: cbuf
integer, intent(out) :: idxver, nlen, nnum, iret
end subroutine getg2i2
subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, &
nlen, nnum, nmess, iret)
integer, intent(in) :: lugb
integer (kind = 8), intent(in) :: msk1, msk2
integer, intent(in) :: mnum, idxver
character(len = 1), pointer, dimension(:) :: cbuf
integer, intent(out) :: nlen, nnum, nmess, iret
end subroutine getg2i2r
subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng8, iret)
integer, intent(in) :: lugb
integer, intent(inout) :: idxver
character(len = 1), intent(in) :: cindex(*)
logical, intent(in) :: extract
character(len = 1), pointer, dimension(:) :: gribm
integer (kind = 8), intent(out) :: leng8
integer, intent(out) :: iret
end subroutine getgb2rp2
subroutine getgb2s2(cbuf, idxver, nlen, nnum, j, jdisc, jids, jpdtn, jpdt, jgdtn, &
jgdt, k, gfld, lpos, iret)
import gribfield
character(len = 1), intent(in) :: cbuf(nlen)
integer, intent(in) :: idxver, nlen, nnum, j, jdisc
integer, dimension(:) :: jids(*)
integer, intent(in) :: jpdtn
integer, dimension(:) :: jpdt(*)
integer, intent(in) :: jgdtn
integer, dimension(:) :: jgdt(*)
integer, intent(out) :: k
type(gribfield), intent(out) :: gfld
integer, intent(out) :: lpos, iret
end subroutine getgb2s2
end interface
nullify(gribm)
! Initialize the index information in cbuf.
irgi = 0
if (lugi .gt. 0) then
call getg2i2(lugi, cbuf, idxver, nlen, nnum, irgi)
elseif (lugi .le. 0) then
mskp = 0
call getg2i2r(lugb, msk1, msk2, mskp, idxver, cbuf, nlen, nnum, nmess, irgi)
endif
if (irgi .gt. 1) then
iret = 96
return
endif
! Find info from index and fill a grib_mod::gribfield variable.
call getgb2s2(cbuf, idxver, nlen, nnum, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
jk, gfld, lpos, irgs)
if (irgs .ne. 0) then
iret = 99
call gf_free(gfld)
return
endif
! Extract grib message from file.
call getgb2rp2(lugb, idxver, cbuf(lpos:), extract, gribm, leng8, iret)
k = jk
! Free cbuf memory allocated in getg2i/getg2ir().
if (associated(cbuf)) deallocate(cbuf)
call gf_free(gfld)
end subroutine getgb2p2
!> Read and unpack sections 6 and 7 from a GRIB2 message using a
!> version 1 index record.
!>
!> This function is maintained for backward compatibility. New code
!> should use getgb2r2(), which can handle both version 1 and version
!> 2 index records.
!>
!> Metadata for the field must already exists in derived type @ref
!> grib_mod::gribfield. Specifically, it requires gfld\%ibmap,
!> gfld\%ngrdpts, gfld\%idrtnum, gfld\%idrtmpl, and gfld\%ndpts.
!>
!> The field data is returned in derived type variable, gfld, of type
!> @ref grib_mod::gribfield. Users of this routine will need to
!> include the line "use grib_mod" in their calling routine.
!>
!> This subprogram is intended for private use by getgb2()
!> routines only.
!>
!> Derived type gribfield contains pointers to many arrays of
!> data. Users must free this memory by calling gf_free().
!>
!> @param[in] lugb integer unit of the unblocked grib data file.
!> File must be opened with [baopen() or baopenr()]
!> (https://noaa-emc.github.io/NCEPLIBS-bacio/) before calling
!> this routine.
!> @param[in] cindex version 1 index record of the field (see
!> subroutine ix2gb2() for description of an index record.)
!> @param[out] gfld derived type @ref grib_mod::gribfield.
!> @param[out] iret Return code:
!> - 0 all ok
!> - 97 error reading grib file
!> - other gf_getfld grib2 unpacker return code
!>
!> @author Stephen Gilbert, Ed Hartnett @date 2002-01-11
subroutine getgb2r(lugb, cindex, gfld, iret)
use grib_mod
implicit none
integer, intent(in) :: lugb
character(len=1), intent(in) :: cindex(*)
type(gribfield) :: gfld
integer, intent(out) :: iret
interface
subroutine getgb2r2(lugb, idxver, cindex, gfld, iret)
use grib_mod
implicit none
integer, intent(in) :: lugb, idxver
character(len=1), intent(in) :: cindex(*)
type(gribfield) :: gfld
integer, intent(out) :: iret
end subroutine getgb2r2
end interface
call getgb2r2(lugb, 1, cindex, gfld, iret)
end subroutine getgb2r
!> Read and unpack sections 6 and 7 from a GRIB2 message using a
!> version 1 or version 2 index record.
!>
!> Metadata for the field must already exists in derived type @ref
!> grib_mod::gribfield. Specifically, it requires gfld\%ibmap,
!> gfld\%ngrdpts, gfld\%idrtnum, gfld\%idrtmpl, and gfld\%ndpts.
!>
!> The field data is returned in derived type variable, gfld, of type
!> @ref grib_mod::gribfield. Users of this routine will need to
!> include the line "use grib_mod" in their calling routine.
!>
!> This subprogram is intended for private use by getgb2()
!> routines only.
!>
!> Derived type gribfield contains pointers to many arrays of
!> data. Users must free this memory by calling gf_free().
!>
!> @note Do not engage the same logical unit from more than one
!> processor.
!>
!> @param[in] lugb integer unit of the unblocked grib data file.
!> File must be opened with [baopen() or baopenr()]
!> (https://noaa-emc.github.io/NCEPLIBS-bacio/) before calling
!> this routine.
!> @param[in] idxver Index version, 1 for legacy, 2 if file may be >
!> 2 GB.
!> @param[in] cindex version 1 or 2 index record of the grib field
!> (see subroutine ixgb2() for description of an index record.)
!> @param[out] gfld derived type @ref grib_mod::gribfield.
!> @param[out] iret Return code:
!> - 0 all ok
!> - 97 error reading grib file
!> - other gf_getfld grib2 unpacker return code
!>
!> @author Ed Hartnett, Stephen Gilbert @date Feb 14, 2024
subroutine getgb2r2(lugb, idxver, cindex, gfld, iret)
use g2logging
use grib_mod
implicit none
integer, intent(in) :: lugb, idxver
character(len=1), intent(in) :: cindex(*)
type(gribfield) :: gfld
integer, intent(out) :: iret
integer :: lskip, skip6, skip7
integer (kind = 8) :: skip68, skip78
character(len=1):: csize(4)
character(len=1), allocatable :: ctemp(:)
real, pointer, dimension(:) :: newfld
integer :: n, j, iofst, ilen, ierr, idum
integer (kind = 8) :: lskip8, lread8, ilen8, iskip8
! Bytes to skip in (version 1 and 2) index record to get to bms.
integer :: IXBMS1, IXBMS2
parameter(IXBMS1 = 24, IXBMS2 = 44)
! Bytes to skip in (version 1 and 2) index record to get to data section.
integer :: IXDS1, IXDS2
parameter(IXDS1 = 28, IXDS2 = 52)
integer :: INT1_BITS, INT2_BITS, INT4_BITS, INT8_BITS
parameter(INT1_BITS = 8, INT2_BITS = 16, INT4_BITS = 32, INT8_BITS = 64)
interface
subroutine gf_unpack6(cgrib, lcgrib, iofst, ngpts, ibmap, bmap, ierr)
character(len=1), intent(in) :: cgrib(lcgrib)
integer, intent(in) :: lcgrib, ngpts
integer, intent(inout) :: iofst
integer, intent(out) :: ibmap
integer, intent(out) :: ierr
logical*1, pointer, dimension(:) :: bmap
end subroutine gf_unpack6
subroutine gf_unpack7(cgrib, lcgrib, iofst, igdsnum, igdstmpl, &
idrsnum, idrstmpl, ndpts, fld, ierr)
character(len=1), intent(in) :: cgrib(lcgrib)
integer, intent(in) :: lcgrib, ndpts, idrsnum, igdsnum
integer, intent(inout) :: iofst
integer, pointer, dimension(:) :: idrstmpl, igdstmpl
integer, intent(out) :: ierr
real, pointer, dimension(:) :: fld
end subroutine gf_unpack7
subroutine g2_gbytec(in, iout, iskip, nbits)
character*1, intent(in) :: in(*)
integer, intent(inout) :: iout(*)
integer, intent(in) :: iskip, nbits
end subroutine g2_gbytec
subroutine g2_gbytec1(in, siout, iskip, nbits)
character*1, intent(in) :: in(*)
integer, intent(inout) :: siout
integer, intent(in) :: iskip, nbits
integer (kind = 4) :: iout(1)
end subroutine g2_gbytec1
subroutine g2_gbytec81(in, siout, iskip, nbits)
character*1, intent(in) :: in(*)
integer (kind = 8), intent(inout) :: siout
integer, intent(in) :: iskip, nbits
integer (kind = 8) :: iout(1)
end subroutine g2_gbytec81
end interface
#ifdef LOGGING
write(g2_log_msg, *) 'getgb2r2: lugb ', lugb, ' idxver ', idxver
call g2_log(1)
#endif
! Initialize.
nullify(gfld%bmap, gfld%fld)
iret = 0
! Get the bytes to skip to reach the local use section. In index
! version 1 this is a 4-byte value, in index version 2 it is an
! 8-byte value. (To reach the local use offset in the index record,
! we skip the first 4 bytes, which is the length of the index
! record.)
if (idxver .eq. 1) then
call g2_gbytec1(cindex, lskip, INT4_BITS, INT4_BITS)
lskip8 = lskip
else
call g2_gbytec81(cindex, lskip8, INT4_BITS, INT8_BITS)
lskip = int(lskip8, kind(4))
endif
! Read the offset to section 6, the BMS section.
if (idxver .eq. 1) then
call g2_gbytec1(cindex, skip6, IXBMS1 * INT1_BITS, INT4_BITS)
skip68 = skip6
else
call g2_gbytec81(cindex, skip68, IXBMS2 * INT1_BITS, INT8_BITS)
endif
#ifdef LOGGING
write(g2_log_msg, *) ' getgb2r2: skip68', skip68
call g2_log(1)
#endif
! Read the offset to section 7, the data section.
if (idxver .eq. 1) then
call g2_gbytec1(cindex, skip7, IXDS1 * INT1_BITS, INT4_BITS)
else
call g2_gbytec81(cindex, skip78, IXDS2 * INT1_BITS, INT8_BITS)
skip7 = int(skip78, kind(4))
endif
#ifdef LOGGING
write(g2_log_msg, *) ' getgb2r2: skip7', skip7
call g2_log(1)
#endif
! Read and unpack bit_map, if present.
if (gfld%ibmap .eq. 0 .or. gfld%ibmap .eq. 254) then
iskip8 = lskip8 + skip68
! Get length of bitmap section.
call bareadl(lugb, iskip8, 4_8, lread8, csize)
call g2_gbytec1(csize, ilen, 0, INT4_BITS)
allocate(ctemp(ilen))
ilen8 = ilen
! Read in bitmap section.
call bareadl(lugb, iskip8, ilen8, lread8, ctemp)
if (ilen8 .ne. lread8) then
iret = 97
deallocate(ctemp)
return
endif
! Unpack bitmap section.
iofst = 0
call gf_unpack6(ctemp, ilen, iofst, gfld%ngrdpts, idum, gfld%bmap, ierr)
if (ierr .ne. 0) then
iret = 98
deallocate(ctemp)
return
endif
deallocate(ctemp)
endif
! Read and unpack data field.
iskip8 = lskip8 + skip7
! Get length of data section.
call bareadl(lugb, iskip8, 4_8, lread8, csize)