| 1 | # Copyrights 2008-2018 by [Mark Overmeer]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||
| 5 | # This code is part of distribution Geo-Format-Envisat. Meta-POD processed | ||
| 6 | # with OODoc into POD and HTML manual-pages. See README.md | ||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||
| 8 | |||
| 9 | package Geo::Format::Envisat; | ||
| 10 | 2 | ▶ | use vars '$VERSION'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | $VERSION = '0.90'; | ||
| 12 | |||
| 13 | 2 | ▶ | use base 'Exporter'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | |||
| 18 | our @EXPORT = qw/envisat_mph_from_name envisat_meta_from_file/; | ||
| 19 | |||
| 20 | 2 | ▶ | use File::Basename qw/basename/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 2 | ▶ | use POSIX qw/tzset mktime strftime/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 2 | ▶ | use IO::Uncompress::AnyUncompress qw/$AnyUncompressError/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 0 | ▶ | use Geo::Point (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 24 | |||
| 25 | my @month = qw/XXX JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC/; | ||
| 26 | my %month; | ||
| 27 | $month{$_} = keys %month for @month; | ||
| 28 | delete $month{XXX}; | ||
| 29 | |||
| 30 | $ENV{TZ} = 'UTC'; | ||
| 31 | tzset; # needed for mktime() | ||
| 32 | |||
| 33 | use constant MPH_LENGTH => 1247; | ||
| 34 | |||
| 35 | sub _decode_name($); | ||
| 36 | sub _cleanup_mph($); | ||
| 37 | sub _cleanup_sph($); | ||
| 38 | sub _cleanup_dsd($); | ||
| 39 | sub _read_block($$); | ||
| 40 | sub _decompose($); | ||
| 41 | sub _timestamp_to_time($); | ||
| 42 | sub _timestamp_iso($); | ||
| 43 | sub _add_missing_stripped($); | ||
| 44 | sub _strip_unit($); | ||
| 45 | |||
| 46 | |||
| 47 | sub envisat_mph_from_name($) | ||
| 48 | { my $filename = shift; | ||
| 49 | my $mph = _decode_name $filename; | ||
| 50 | |||
| 51 | if(my $size = -s $filename) | ||
| 52 | { $mph->{TOT_SIZE} = "$size<bytes>"; | ||
| 53 | } | ||
| 54 | |||
| 55 | my ($year, $month, $day) = $mph->{start_day} =~ m/(\d{4})(\d\d)(\d\d)/; | ||
| 56 | my ($hour, $min, $sec ) = $mph->{start_time} =~ m/(\d\d)(\d\d)(\d\d)/; | ||
| 57 | $mph->{SENSING_START} = sprintf "%02d-%s-%04d %02d:%02d:%02d.%06d" | ||
| 58 | , $day, $month[$month], $year, $hour, $min, $sec, 0; | ||
| 59 | |||
| 60 | _cleanup_mph($mph); | ||
| 61 | $mph; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub envisat_meta_from_file($@) | ||
| 66 | { my ($fn, %args) = @_; | ||
| 67 | my $mph_from_filename = _decode_name $fn; | ||
| 68 | |||
| 69 | my $meta; | ||
| 70 | my $file = IO::Uncompress::AnyUncompress->new($fn) | ||
| 71 | or die "ERROR: cannot read from $fn: $AnyUncompressError\n"; | ||
| 72 | |||
| 73 | my $mph = _decompose(_read_block $file, MPH_LENGTH); | ||
| 74 | |||
| 75 | foreach my $k (keys %$mph_from_filename) | ||
| 76 | { if(! exists $mph->{$k} ) | ||
| 77 | { $mph->{$k} = $mph_from_filename->{$k}; | ||
| 78 | } | ||
| 79 | elsif($mph->{$k} ne $mph_from_filename->{$k}) | ||
| 80 | { warn "field $k differs: \n in file : `$mph->{$k}'\n" | ||
| 81 | , " in filename: `$mph_from_filename->{$k}'\n"; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | _cleanup_mph $mph; | ||
| 86 | |||
| 87 | my ($sph, %dsd); | ||
| 88 | if(my $sphsize = $mph->{sph_size}) | ||
| 89 | { my $dsd_size = $mph->{dsd_size}; | ||
| 90 | my $dsd_num = $mph->{num_dsd}; | ||
| 91 | my $sph_length = $sphsize - $dsd_size * $dsd_num; | ||
| 92 | $sph = _decompose(_read_block $file, $sph_length); | ||
| 93 | _cleanup_sph $sph; | ||
| 94 | |||
| 95 | for(1..$dsd_num) | ||
| 96 | { my $dsd = _decompose(_read_block $file, $dsd_size); | ||
| 97 | _cleanup_dsd $dsd; | ||
| 98 | $dsd->{num_dsr} > 0 | ||
| 99 | or next; | ||
| 100 | (my $name = $dsd->{ds_name}) =~ s/\s+/_/g; | ||
| 101 | $dsd{$name} = $dsd; | ||
| 102 | } | ||
| 103 | |||
| 104 | # only forward seeks permitted. | ||
| 105 | my $take = exists $args{take_dsd_content} ? $args{take_dsd_content} : 0; | ||
| 106 | if($take) | ||
| 107 | { my @sorted = sort {$a->{ds_offset} <=> $b->{ds_offset}} values %dsd; | ||
| 108 | foreach my $dsd (@sorted) | ||
| 109 | { next if $dsd->{DS_TYPE} eq 'M'; | ||
| 110 | $file->seek($dsd->{ds_offset}, 0); | ||
| 111 | $dsd->{content} = _read_block $file, $dsd->{ds_size}; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | $file->close; | ||
| 117 | |||
| 118 | +{ mph => $mph, sph => $sph, dsd => \%dsd }; | ||
| 119 | } | ||
| 120 | |||
| 121 | ##### some helpers | ||
| 122 | |||
| 123 | sub _decode_name($) | ||
| 124 | { my $name = uc basename shift; | ||
| 125 | $name =~ | ||
| 126 | m/^ (\w{10}) # product ID | ||
| 127 | (\w) # processing stage flag | ||
| 128 | (\w{3}) # originator ID | ||
| 129 | (\d{8}) _ # start_day | ||
| 130 | (\d{6}) _ # start_time | ||
| 131 | (\d{8}) # duration | ||
| 132 | (\w) # phase id | ||
| 133 | (\d{3}) _ # cycle number within phase | ||
| 134 | (\d{5}) _ # relative orbit number within cycle | ||
| 135 | (\d{5}) _ # absolute orbit number | ||
| 136 | (\d{4}) # product file counter | ||
| 137 | \. (\w\w) # satellite ID | ||
| 138 | /x | ||
| 139 | or return (); | ||
| 140 | |||
| 141 | { PRODUCT_ID => $1 | ||
| 142 | , PROC_STAGE => $2 | ||
| 143 | , originator_id => $3 # proc center abbreviated into 3 chars | ||
| 144 | , start_day => $4 | ||
| 145 | , start_time => $5 | ||
| 146 | , duration => $6+0 | ||
| 147 | , PHASE => $7 | ||
| 148 | , CYCLE => "+$8" | ||
| 149 | , REL_ORBIT => "+$9" | ||
| 150 | , ABS_ORBIT => "+$10" | ||
| 151 | , product_file_counter => $11 | ||
| 152 | , satellite_id => $12 | ||
| 153 | }; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub _cleanup_mph($) | ||
| 157 | { my $mph = shift; | ||
| 158 | |||
| 159 | if(my $s = $mph->{PROC_STAGE}) | ||
| 160 | { $mph->{proc_stage} | ||
| 161 | = $s eq 'N' ? 'Near Real Time' | ||
| 162 | : $s eq 'T' ? 'test product' | ||
| 163 | : $s eq 'V' ? 'fully validated' | ||
| 164 | : $s eq 'S' ? 'special product' | ||
| 165 | : $s eq 'X' ? 'not used' | ||
| 166 | : $s gt 'N' && $s lt 'V' ? "consolidation level $s" | ||
| 167 | : 'ERROR'; | ||
| 168 | } | ||
| 169 | |||
| 170 | if(my $s = $mph->{satellite_id}) | ||
| 171 | { $mph->{satellite} | ||
| 172 | = $s eq 'N1' ? 'Envisat' | ||
| 173 | : $s eq 'E1' ? 'ERS1' | ||
| 174 | : $s eq 'E2' ? 'ERS2' | ||
| 175 | : 'ERROR'; | ||
| 176 | } | ||
| 177 | |||
| 178 | if(my $s = $mph->{VECTOR_SOURCE}) | ||
| 179 | { $mph->{vector_source} | ||
| 180 | = $s eq 'FP' ? 'FOS predicted orbit state vectors' | ||
| 181 | : $s eq 'DN' ? 'DORIS Level 0 navigator product acquired at PDHS' | ||
| 182 | : $s eq 'FR' ? 'FOS restituted orbit state vectors' | ||
| 183 | : $s eq 'DI' ? 'DORIS initial (prelimary) orbit' | ||
| 184 | : $s eq 'DP' ? 'DORIS precise orbit' | ||
| 185 | : $s eq '' ? 'not used' | ||
| 186 | : 'ERROR'; | ||
| 187 | } | ||
| 188 | |||
| 189 | # add lower-cased field versions without unit specification | ||
| 190 | foreach my $field (qw/TOT_SIZE SPH_SIZE DSD_SIZE DELTA_UT1 CLOCK_STEP | ||
| 191 | X_POSITION Y_POSITION Z_POSITION X_VELOCITY Y_VELOCITY Z_VELOCITY/) | ||
| 192 | { $mph->{lc $field} = _strip_unit $mph->{$field} | ||
| 193 | if $mph->{$field}; | ||
| 194 | } | ||
| 195 | |||
| 196 | # make numeric | ||
| 197 | foreach my $field (qw/CYCLE REL_ORBIT ABS_ORBIT LEAP_SIGN | ||
| 198 | NUM_DATA_SETS NUM_DSD SAT_BINARY_TIME PRODUCT_ERR/) | ||
| 199 | { defined $mph->{$field} or next; | ||
| 200 | $mph->{lc $field} = $mph->{$field} + 0; | ||
| 201 | } | ||
| 202 | |||
| 203 | # convert to OS time | ||
| 204 | foreach my $field (qw/SENSING_START SENSING_STOP PROC_TIME | ||
| 205 | STATE_VECTOR_TIME UTC_SBT_TIME LEAP_UTC/) | ||
| 206 | { defined $mph->{$field} or next; | ||
| 207 | $mph->{lc $field} = _timestamp_to_time $mph->{$field}; | ||
| 208 | $mph->{lc($field).'_iso'} = _timestamp_iso $mph->{lc $field}; | ||
| 209 | } | ||
| 210 | |||
| 211 | if(my $station = $mph->{ACQUISITION_STATION}) | ||
| 212 | { $station =~ s/\s//g; | ||
| 213 | $mph->{acquisition_station} = [ split /\,/, $station ]; | ||
| 214 | } | ||
| 215 | |||
| 216 | _add_missing_stripped $mph; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub _cleanup_sph($) | ||
| 220 | { my $sph = shift; | ||
| 221 | |||
| 222 | # add field versions without unit specification | ||
| 223 | $sph->{lc $_} = _strip_unit $sph->{$_} | ||
| 224 | for qw/AZIMUTH_SPACING LINE_LENGTH LINE_TIME_INTERVAL RANGE_SPACING/; | ||
| 225 | |||
| 226 | # convert to OS time | ||
| 227 | foreach my $field (qw/FIRST_LINE_TIME LAST_LINE_TIME/) | ||
| 228 | { $sph->{lc $field} = _timestamp_to_time $sph->{$field}; | ||
| 229 | $sph->{lc($field).'_iso'} = _timestamp_iso $sph->{lc $field}; | ||
| 230 | } | ||
| 231 | |||
| 232 | # make numeric | ||
| 233 | foreach my $field (qw/AZIMUTH_LOOKS NUM_SLICES RANGE_LOOKS | ||
| 234 | SLICE_POSITION STRIPLINE_CONTINUITY_INDICATOR/) | ||
| 235 | { $sph->{lc $field} = $sph->{$field} + 0; | ||
| 236 | } | ||
| 237 | |||
| 238 | if(my $t = $sph->{DATA_TYPE}) | ||
| 239 | { $sph->{pixel_octets} | ||
| 240 | = $t eq 'UWORD' ? 2 # unsigned | ||
| 241 | : $t eq 'SWORD' ? 2 # signed | ||
| 242 | : $t eq 'UBYTE' ? 1 # unsigned | ||
| 243 | : 'ERROR'; | ||
| 244 | } | ||
| 245 | |||
| 246 | # Simplify coordinates | ||
| 247 | my %bounds; | ||
| 248 | foreach my $azimuth ('FIRST', 'LAST') | ||
| 249 | { foreach my $range ('NEAR', 'MID', 'FAR') | ||
| 250 | { (my $lat = $sph->{"${azimuth}_${range}_LAT"}) =~ s/\<.*//; | ||
| 251 | $lat *= 1e-6; | ||
| 252 | $sph->{lc "${azimuth}_${range}_lat"} = $lat; | ||
| 253 | (my $long = $sph->{"${azimuth}_${range}_LONG"}) =~ s/\<.*//; | ||
| 254 | $long *= 1e-6; | ||
| 255 | $sph->{lc "${azimuth}_${range}_long"} = $long; | ||
| 256 | |||
| 257 | my $point = Geo::Point->latlong($lat, $long, 'wgs84'); | ||
| 258 | push @{$bounds{$azimuth}}, $point; | ||
| 259 | $sph->{lc "${azimuth}_${range}_point"} = $point; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | # I don't know how multiple polys should be calculated (when there | ||
| 264 | # is strip spacing. Polygon clockwise starting left-top | ||
| 265 | my @poly | ||
| 266 | = $sph->{PASS} eq 'DESCENDING' | ||
| 267 | ? (@{$bounds{FIRST}}, reverse @{$bounds{LAST}} ) | ||
| 268 | : (@{$bounds{LAST}}, reverse @{$bounds{FIRST}}); | ||
| 269 | |||
| 270 | my $footprint = Geo::Line->filled(points => \@poly, clockwise => 1); | ||
| 271 | $sph->{target_polys} = Geo::Surface->new($footprint); | ||
| 272 | |||
| 273 | _add_missing_stripped $sph; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub _cleanup_dsd($) | ||
| 277 | { my $dsd = shift; | ||
| 278 | |||
| 279 | $dsd->{lc $_} = _strip_unit $dsd->{$_} | ||
| 280 | for qw/DS_OFFSET DS_SIZE DSR_SIZE/; | ||
| 281 | |||
| 282 | $dsd->{num_dsr} = $dsd->{NUM_DSR} + 0; | ||
| 283 | |||
| 284 | my $t = $dsd->{DS_TYPE}; | ||
| 285 | $dsd->{ds_type} | ||
| 286 | = $t eq 'M' ? 'Measurement DS' | ||
| 287 | : $t eq 'A' ? 'Annotation DS' | ||
| 288 | : $t eq 'G' ? 'Global ADS' | ||
| 289 | : $t eq 'R' ? 'Reference only' | ||
| 290 | : 'ERROR'; | ||
| 291 | |||
| 292 | # field can also contain 'MISSING' and 'NOT USED' :-( | ||
| 293 | $dsd->{filename} = $t eq 'R' ? $dsd->{FILENAME} : undef; | ||
| 294 | |||
| 295 | _add_missing_stripped $dsd; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub _read_block($$) | ||
| 299 | { my ($fh, $size) = @_; | ||
| 300 | my $buffer = ''; | ||
| 301 | $fh->read($buffer, $size - length $buffer, length $buffer) | ||
| 302 | while length $buffer < $size; | ||
| 303 | $buffer; | ||
| 304 | } | ||
| 305 | |||
| 306 | sub _decompose($) | ||
| 307 | { my @lines = split /\n/, shift; | ||
| 308 | my %h; | ||
| 309 | |||
| 310 | foreach my $line (@lines) | ||
| 311 | { if ($line =~ m/^(\w+)\=\"(.*?)"/) { $h{$1} = $2 } | ||
| 312 | elsif($line =~ m/^(\w+)\=(.*)/) { $h{$1} = $2 } | ||
| 313 | } | ||
| 314 | \%h; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub _timestamp_to_time($) | ||
| 318 | { my $stamp = shift; | ||
| 319 | $stamp =~ m/^(\d\d)\-([A-Z]{3})\-(\d{4}) (\d\d):(\d\d):(\d\d)\.(\d+)/ | ||
| 320 | or return 'ERROR'; | ||
| 321 | my $secs = mktime($6, $5, $4, $1, $month{$2}-1, $3-1900); | ||
| 322 | 0.0 + "$secs.$7"; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub _timestamp_iso($) | ||
| 326 | { my $time = shift; | ||
| 327 | my $frag = $time =~ s/\.(\d+)$// ? $1 : 0; | ||
| 328 | (strftime "%FT%T", gmtime $time). ($frag ? ".$frag" : '') . 'Z'; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub _add_missing_stripped($) | ||
| 332 | { my $h = shift; | ||
| 333 | |||
| 334 | # for all fields without a lower-cased value, we generate one | ||
| 335 | # where the blanks are stripped off. | ||
| 336 | foreach my $f (keys %$h) | ||
| 337 | { next if exists $h->{lc $f}; # skips lc keys as well | ||
| 338 | ($h->{lc $f} = $h->{$f}) =~ s/\s+$//; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | sub _strip_unit($) | ||
| 343 | { (my $value = $_[0]) =~ s/\<[^<>]*\>\s*$//; | ||
| 344 | $value + 0; | ||
| 345 | } | ||
| 346 | |||
| 347 | 1; | ||