lib/Geo/Format/Landsat/MTL.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Geo/Format/Landsat.pm ↑ Summary
1 # Copyrights 2009-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-Landsat. 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::Landsat::MTL;
10 1 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 $VERSION = '0.90';
12  
13 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 our @EXPORT = qw/
19 landsat_mtl_from_file
20 landsat_meta_from_filename
21 /;
22  
23 1 ▶use constant METADATA_RECORD => 65536;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant METERS2FEET => 3.2808399;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 0 ▶use Geo::Point ();
Subroutine BEGIN: called CC=1 SCAR=2.6
27 use File::Basename qw/basename/;
28  
29 use POSIX qw/mktime strftime tzset/;
30 $ENV{TZ} = 'UTC'; tzset;
31  
32 sub _process_group($);
33  
34 sub _cleanup_mtl($);
35 sub _cleanup_product_parameters($);
36 sub _cleanup_product_metadata($$);
37 sub _cleanup_metadata_file_info($);
38 sub _cleanup_min_max_radiance($);
39 sub _cleanup_min_max_pixel_value($);
40 sub _cleanup_projection_parameters($);
41 sub _cleanup_corrections_applied($);
42 sub _get_map_proj($);
43  
44  
45 sub landsat_meta_from_filename($)
46 { my $filename = basename shift;
47 $filename =~ m/^L([57])(\d\d\d)(\d\d\d)_(\d\d\d)(\d\d\d\d)(\d\d)(\d\d)/
48 or return;
49  
50 +{ SPACECRAFT_ID => "Landsat$1"
51 , WRS_PATH => $2
52 , STARTING_ROW => $3
53 , ENDING_ROW => 4
54 , ACQUISITION_DATE => "$5-$6-$7"
55 };
56 }
57  
58  
59 sub landsat_mtl_from_file($)
60 { my $f = shift;
61  
62 my $text;
63 if(UNIVERSAL::isa($f, 'GLOB') || UNIVERSAL::isa($f, 'IO::Handle'))
64 { sysread $f, $text, METADATA_RECORD;
65 }
66 else
67 { open F, '<', $f
68 or die "ERROR: cannot read from $f: $!\n";
69 sysread F, $text, METADATA_RECORD;
70 close F;
71 }
72  
73 $text =~ s/\0+\z//; # record padded with \0 bytes
74  
75 $text =~ s/^\s*END\s*\z//m
76 or die "ERROR: did not get 'END' tag\n";
77  
78 my $wrapper = _process_group $text;
79 my ($type, $data) = %$wrapper; # only one key
80  
81 ($type, _cleanup_mtl $data);
82 }
83  
84 sub _process_group($)
85 { my $text = shift;
86 my $data;
87  
88 while($text =~ s/\A\s*GROUP\s*\=\s*(\w+)\s*
89 (.*?)
90 \s*END_GROUP\s*\=\s*\1\s*//xsm)
91 { my $name = $1;
92 $data->{$name} = _process_group($2);
93 }
94  
95 foreach my $line (split /\n/, $text)
96 { if($line =~ m/^\s*(\w+)\s*\=\s+\"(.*?)\"\s*$/ )
97 { $data->{$1} = $2;
98 }
99 elsif($line =~ m/^\s*(\w+)\s*\=\s+(.*?)\s*$/ )
100 { $data->{$1} = $2;
101 }
102 else
103 { warn "Do not understand line:\n $line\n";
104 }
105 }
106  
107 $data;
108 }
109  
110 sub _cleanup_mtl($)
111 { my $data = shift;
112  
113 _cleanup_metadata_file_info $data->{METADATA_FILE_INFO};
114 _cleanup_min_max_radiance $data->{MIN_MAX_RADIANCE};
115 _cleanup_min_max_pixel_value $data->{MIN_MAX_PIXEL_VALUE};
116 _cleanup_product_parameters $data->{PRODUCT_PARAMETERS};
117 _cleanup_corrections_applied $data->{CORRECTIONS_APPLIED};
118 _cleanup_projection_parameters $data->{PROJECTION_PARAMETERS};
119  
120 my $mapproj = $data->{map_projection} = _get_map_proj $data;
121  
122 _cleanup_product_metadata $data->{PRODUCT_METADATA}, $mapproj;
123 $data;
124 }
125  
126 sub _cleanup_metadata_file_info($)
127 { my $d = shift or return;
128  
129 if($d->{REQUEST_ID} =~ m/(...)(\d\d)(\d\d)(\d\d)(\d{4})_(\d{5})/)
130 { my $date = sprintf "%04d-%02d-%02dZ"
131 , ($2 < 70 ? 2000+$2 : 1900+$2), $3, $4;
132 $d->{request_id} = { node => $1+0, date_iso => $date, seqnr => $5+0
133 , dorran_unit => $6+0 };
134 }
135  
136 # 0 = unknown, becomes undef.
137 $d->{landsat_xband} = $d->{LANDSAT5_XBAND} || $d->{LANDSAT7_XBAND} || undef;
138  
139 # no simple way to translate DOY -> month/day
140 if($d->{DATEHOUR_CONTACT_PERIOD} =~ m/^(\d\d)(\d\d\d)(\d\d)$/ )
141 { my ($year, $yday, $hour) = ($1, $2, $3);
142 $year += $year < 70 ? 2000 : 1900;
143 # (Date::Calc::Add_Delta_Days($_[0],1,1,$_[1]-1))[1,2];
144  
145 my @monthdays = (undef, 31,28,31,30,31,30,31,31,30,31,30,31);
146 $monthdays[2] = 29 if $year%400==0 || ($year%4==0 && $year%100!=0);
147 my ($month, $day) = (1, $yday);
148 while($day > $monthdays[$month])
149 { $day -= $monthdays[$month];
150 $month++;
151 }
152 $d->{received} = sprintf "%04d-%02d-%02dT%02d:00:00Z"
153 , $year, $month, $day, $hour;
154 }
155 }
156  
157 sub _cleanup_product_metadata($$)
158 { my ($d, $proj) = @_;
159 $d or return;
160 my $mapproj = $proj->nick;
161  
162 if($d->{PROCESSING_SOFTWARE} =~ m/([A-Z]+)_(.*)/)
163 { $d->{software_system} = $1;
164 $d->{software_version} = $2;
165 }
166 $d->{EPHEMERIS_TYPE} ||= 'PREDICTIVE';
167  
168 foreach my $band ( '', '_PAN', '_THM')
169 { $d->{"PRODUCT_UL_CORNER_LAT$band"} or next;
170 my (@bbox, @bbox_map);
171 foreach my $c (qw/UL UR LR LL/)
172 { my $lat = $d->{"PRODUCT_${c}_CORNER_LAT${band}"};
173 my $lon = $d->{"PRODUCT_${c}_CORNER_LON${band}"};
174 my $corner = Geo::Point->latlong($lat, $lon, 'wgs84');
175 $d->{"product_".(lc $c)."_wgs84".lc $band} = $corner;
176 push @bbox, $corner;
177  
178 my $x = $d->{"PRODUCT_${c}_CORNER_MAPX${band}"};
179 my $y = $d->{"PRODUCT_${c}_CORNER_MAPY${band}"};
180 my $map = Geo::Point->xy($x, $y, $mapproj);
181 $d->{"product_".(lc $c)."_map".lc $band} = $map;
182 push @bbox_map, $map;
183 }
184 my $bbox = Geo::Line->filled(points => \@bbox, clockwise => 1);
185 $d->{"footprint".lc $band} = Geo::Surface->new($bbox);
186  
187 my $bbox_map = Geo::Line->filled(points => \@bbox_map, clockwise => 1);
188 $d->{"footprint_map".lc $band} = Geo::Surface->new($bbox_map);
189 }
190 }
191  
192 sub _cleanup_min_max_radiance($)
193 { my $d = shift or return;
194 # strings can be used as numbers... nothing to do
195 }
196  
197 sub _cleanup_min_max_pixel_value($)
198 { my $d = shift or return;
199 # strings can be used as numbers... nothing to do
200 }
201  
202 sub _cleanup_product_parameters($)
203 { my $d = shift or return;
204 # too specific
205 }
206  
207 sub _cleanup_corrections_applied($)
208 { my $d = shift or return;
209  
210 foreach my $key (qw/BANDING COHERENT_NOICE MEMORY_EFFECT
211 SCAN_CORRELATED_SHIFT INOPERABLE_DETECTORS DROPPED_LINES/)
212 { defined $d->{$key} or next;
213 $d->{lc $key} = $d->{$key} eq 'Y' ? 1 : 0;
214 }
215 }
216  
217 sub _cleanup_projection_parameters($)
218 { my $d = shift or return;
219  
220 $d->{REFERENCE_DATUM} eq 'WGS84' # hard-coded in spec
221 && $d->{REFERENCE_ELLIPSOID} eq 'WGS84'
222 or die "ERROR: WGS84 expected\n";
223  
224 if(my $o = $d->{ORIENTATION})
225 { $d->{orientation}
226 = $o eq 'NOM' ? 'Nominal Path'
227 : $o eq 'NUP' ? 'North Up'
228 : $o eq 'TN' ? 'True North'
229 : $o eq 'USR' ? 'User'
230 : 'UNKNOWN';
231 }
232  
233 if(my $r = $d->{RESAMPLING_OPTION})
234 { $d->{resampling_option}
235 = $r eq 'NN' ? 'Nearest Neighbor'
236 : $r eq 'CC' ? 'Cubic Convolution'
237 : $r eq 'MTF' ? 'Modulation Transfer Function'
238 : $r eq 'BI' ? 'Bilinear'
239 : $r eq 'KD' ? 'Kaiser Damped'
240 : $r eq '16' ? '16 Point Sinc'
241 : $r eq '8' ? '8 Point Sinc'
242 : $r eq 'DW' ? 'Damped Window'
243 : 'UNKNOWN';
244 }
245 }
246  
247  
248 # See http://www.remotesensing.org/geotiff/proj_list
249 sub _get_map_proj($)
250 { my $data = shift;
251 my $code = $data->{PROJECTION_PARAMETERS}{MAP_PROJECTION};
252 my $details = $data->{"${code}_PARAMETERS"};
253
254 my $nick = lc $code;
255 my ($proj, $name, @params);
256  
257 my $units = $details->{FALSE_EASTING_NORTHING_UNITS};
258 push @params, '-M'
259 if defined $units && $units eq 'meters';
260  
261 my @common = qw/
262 FALSE_EASTING x_0
263 FALSE_NORTHING y_0
264 LATITUDE_OF_PROJECTION_ORIGIN lat_0
265 LATITUDE_OF_CENTER lat_0
266 LONGITUDE_OF_CENTRAL_MERIDIAN lon_0
267 LONGITUDE_OF_CENTER lon_0
268 VERTICAL_LONGITUDE_FROM_POLE lon_0
269 LATITUDE_OF_FIRST_STANDARD_PARALLEL lat_1
270 LATITUDE_FIRST_POINT_GEODETIC lat_1
271 LONGITUDE_OF_FIRST_STANDARD_PARALLEL lon_1
272 LONGITUDE_FIRST_POINT_GEODETIC lon_1
273 LATITUDE_OF_SECOND_STANDARD_PARALLEL lat_2
274 LATITUDE_SECOND_POINT_GEODETIC lat_2
275 LONGITUDE_SECOND_POINT_GEODETIC lon_2
276 LATITUDE_OF_TRUE_SCALE lat_ts
277 ANGLE_OF_AZIMUTH alpha
278 LONGITUDE_ALONG_PROJECTION lonc
279 SCALE_FACTOR_AT_CENTRAL_MERIDIAN k
280 /;
281  
282 while(@common)
283 { my ($key, $label) = (shift @common, shift @common);
284 push @params, $label => $details->{$key}
285 if $details->{$key};
286 }
287  
288 my $h = $details->{HEIGHT}; # always in meters, according to doc
289 if(defined $h)
290 { push @params, h => ($units eq 'meters' ? $h : $h * METERS2FEET);
291 }
292  
293 # convert to NLAPS type
294 $code .= $details->{EQC_TYPE} if $code eq 'EQC';
295 $code .= $details->{OM_TYPE} if $code eq 'OM';
296 $code .= $details->{SOM_TYPE} if $code eq 'SOM';
297  
298 if($code eq 'AKC')
299 { $name = 'Alaska Conformal';
300 $proj = 'UNKNOWN'; #???
301 }
302 elsif($code eq 'AEA') # epsg:9822
303 { $name = 'Albers Equal-Area Conic';
304 $proj = 'eae';
305 }
306 elsif($code eq 'AZIM')
307 { $name = 'Azimuthal Equidistant';
308 $proj = 'aeqd';
309 }
310 elsif($code =~ m/^EQC([AB])$/)
311 { $name = "Equidistant Conic type $1";
312 $proj = 'eqdc';
313 }
314 elsif($code eq 'EQUI') # epsg:9823 (spherical), 9842 (elliptical)
315 { $name = 'Equirectangular';
316 $proj = 'UNKNOWN'; #???
317 }
318 elsif($code eq 'GNOM')
319 { $name = 'Gnomonic';
320 $proj = 'gnom';
321 }
322 elsif($code eq 'GVNP')
323 { $name = 'General Vertical Near Side Perspective';
324 $proj = 'nsper';
325 }
326 elsif($code eq 'HAMM')
327 { $name = 'Hammer';
328 $proj = 'hammer';
329 }
330 elsif($code eq 'LAEA') # epsg:9820
331 { $name = 'Lambert Azimuthal Equal Area';
332 $proj = 'laea';
333 }
334 elsif($code eq 'LCC') # epsg:9802 (?)
335 { $name = 'Lambert Conformal Conic (2SP)';
336 $proj = 'lcc';
337 }
338 elsif($code eq 'MERC')
339 { $name = 'Mercator (2SP)';
340 $proj = 'merc';
341 }
342 elsif($code eq 'MCYL')
343 { $name = 'Miller Cylindrical';
344 $proj = 'mill';
345 }
346 elsif($code eq 'MOLL')
347 { $name = 'Mollweide';
348 $proj = 'moll';
349 }
350 elsif($code eq 'OEA')
351 { $name = 'Oblated Equal Area';
352 $proj = 'oea';
353 push @params, theta => $details->{ANGLE};
354 }
355 elsif($code =~ m/^OM([AB])$/) # epsg:9815
356 { $name = "Oblique Mercator type $1";
357 $proj = 'omerc';
358 #??? SCALE_FACTOR_AT_CENTER_OF_PROJECTION
359 }
360 elsif($code eq 'ORTH')
361 { $name = 'Orthographic';
362 $proj = 'ortho';
363 }
364 elsif($code eq 'PC')
365 { $name = 'Polyconic'; # (American?)
366 $proj = 'poly'; # ???
367 }
368 elsif($code eq 'PS') # epsg:9810
369 { $name = 'Polar Stereographic';
370 $proj = 'stere';
371 push @params, lat_0 => '90'; #???
372 }
373 elsif($code eq 'ROBN')
374 { $name = 'Robinson';
375 $proj = 'robin';
376 }
377 elsif($code eq 'SINU')
378 { $name = 'Sinusoidal (Sanson-Flamsteed)';
379 $proj = 'sinu';
380 }
381 elsif($code eq 'SOMA')
382 { $name = 'Space Oblique Mercator type A';
383 $proj = "UNKNOWN"; # too complex
384 }
385 elsif($code eq 'SOMB')
386 { $name = 'Space Oblique for Landsat';
387 $proj = 'lsat';
388 push @params, lsat => $details->{LANDSAT_NUMBER}
389 , path => $details->{PATH};
390 }
391 elsif($code eq 'STRG')
392 { $name = 'Stereographic';
393 $proj = 'stere';
394 }
395 elsif($code eq 'TM')
396 { $name = 'Traverse Mercator (Gauss-Krueger)';
397 $proj = 'tmerc';
398 }
399 elsif($code eq 'UTM')
400 { $name = 'Universal Transverse Mercator';
401 my $zone = $details->{ZONE_NUMBER};
402 $nick = "utm$zone-wgs84";
403 $proj = 'utm';
404 push @params, datum => 'WGS84', zone => $zone;
405 }
406 elsif($code eq 'VDGR')
407 { $name = 'van der Grinten';
408 $proj = 'vandg';
409 }
410 elsif($code eq 'WIV')
411 { $name = 'Wagner IV';
412 $proj = 'wag4';
413 }
414 elsif($code eq 'WVII')
415 { $name = 'Wagner VII';
416 $proj = 'wag7';
417 }
418 else
419 { die "ERROR: unknown projection code $code\n";
420 }
421  
422 unshift @params, proj => $proj;
423  
424 Geo::Proj->new
425 ( nick => $nick
426 , name => $name
427 , proj4 => \@params
428 );
429 }
430  
431 1;
« blib/lib/Geo/Format/Landsat.pm ↑ Summary