File: grib_split.pl

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (155 lines) | stat: -rwxr-xr-x 4,247 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl
use strict;

# ----------------------------------------------------------------------
# Convert GRIB edition 2 multi-field (several fields per message) files
# to single-field (one field per message)
# ----------------------------------------------------------------------

my $debug = 0;
my $total = 0;
my $previous_bitmap;

if ($ARGV[0] eq "-d")
{
    $debug = 1;
    shift @ARGV;
}

my $out = pop @ARGV;

unless(@ARGV >= 1 && defined $out)
{
    print STDERR "Usage: $0 [-d] in1 [in2 ...]  out\n";
    exit(1);
}

open(OUT,">$out") || die "$out: $!";

foreach my $in ( @ARGV )
{
    my $gribs = 0;
    my $cnt   = 0;
    open(IN,"<$in")   || die "$in: $!";

    while(1)
    {
        my @sections = ();
        # Find header

        print "Looking for GRIB\n" if ($debug);
        my $grib;
        while((read(IN,$grib,4) == 4) && ($grib ne "GRIB"))
        {
            seek(IN,-3,1);
        }

        unless($grib eq "GRIB")
        {
            print "No more GRIBs\n" if ($debug);
            last;
        }

        my $here = tell(IN);
        my $start = $here - 4;
        $gribs++;

        print "$grib found at offset $start\n" if ($debug);

        my ($reserved,$discipline,$edition);

        die "$in: $!" unless(read(IN,$reserved,2) == 2);
        die "$in: $!" unless(read(IN,$discipline,1) == 1);
        die "$in: $!" unless(read(IN,$edition,1) == 1);

        my $grib_edition = ord($edition);
        die "Un-supported edition $grib_edition" unless($grib_edition == 2);

        my ($len1,$len2);
        die "$in: $!" unless(read(IN,$len1,4) == 4);
        die "$in: $!" unless(read(IN,$len2,4) == 4);

        my $msglen1 = unpack("N",$len1); die "GRIB too large" if ($msglen1);
        my $msglen2 = unpack("N",$len2);
        my $msglen = $msglen2;
        print "Message length $msglen\n" if ($debug);

        while(1)
        {
            $here = tell(IN);
            my ($seclen,$section);

            die "$in: $!" unless(read(IN,$seclen,4) == 4);

            if ($seclen eq "7777")
            {
                print "7777 found at offset $here\n" if ($debug);
                die "Invalid message lenth $msglen"
                    unless($here+4-$start == $msglen);
                last;
            }

            $seclen = unpack("N",$seclen);
            die "$in: $!" unless(read(IN,$section,1) == 1);
            $section = ord($section);
            die "Invalid section number $section" unless ($section >= 1 && $section <=7);
            print "Section $section found at offset $here length $seclen\n" if ($debug);
            seek(IN,-5,1) or die "$in: $!";

            my $data;
            die "$in: $!" unless(read(IN,$data,$seclen) == $seclen);

            if ($section == 6)
            {
                # Special case for inherited bitmaps
                if (unpack('C',substr($data,5,1)) == 254)
                {
                    die "No previous bitmap" unless(defined $previous_bitmap);
                    $data = $previous_bitmap;
                }
                else
                {
                    $previous_bitmap = $data;
                }
            }

            splice(@sections,$section+1);
            $sections[$section] = $data;

            if ($section == 7)
            {
                print OUT "GRIB";
                print OUT $reserved;
                print OUT $discipline;
                print OUT $edition;
                print OUT $len1;

                $len2 = 16 + 4;
                for(my $i = 1; $i <= 7 ; $i++)
                {
                    $len2 += length($sections[$i])
                        if (defined $sections[$i]);
                }

                $cnt++;
                print "GRIB $cnt is $len2\n" if ($debug);
                $len2 = pack("N",$len2);
                print OUT $len2;

                for(my $i = 1; $i <= 7 ; $i++)
                {
                    next unless(defined $sections[$i]);
                    print OUT $sections[$i];
                }
                print OUT "7777";
            }
        }
    }

    print "$in: $cnt field(s) found in $gribs GRIB(s)\n";
    $total += $cnt;
}

close(OUT) or die "$out: $!";

print "$out: $total field(s) written\n";