PDL-IO-ENVI-2.098/0000755000175000017500000000000014736677227013172 5ustar osboxesosboxesPDL-IO-ENVI-2.098/META.json0000644000175000017500000000250414736677227014614 0ustar osboxesosboxes{ "abstract" : "unknown", "author" : [ "PerlDL Developers " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.44, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "PDL-IO-ENVI", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0", "PDL" : "2.094" } }, "runtime" : { "requires" : { "PDL" : "2.094" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/PDLPorters/PDL-IO-ENVI/issues" }, "homepage" : "http://pdl.perl.org/", "repository" : { "type" : "git", "url" : "git://github.com/PDLPorters/PDL-IO-ENVI.git", "web" : "https://github.com/PDLPorters/PDL-IO-ENVI" }, "x_IRC" : "irc://irc.perl.org/#pdl" }, "version" : "2.098", "x_serialization_backend" : "JSON::PP version 4.04" } PDL-IO-ENVI-2.098/ENVI.pm0000755000175000017500000003266114736675102014272 0ustar osboxesosboxespackage PDL::IO::ENVI; use strict; use warnings; use PDL::LiteF; use PDL::IO::FlexRaw; use PDL::Exporter; use Config; our @ISA = qw( PDL::Exporter ); our @EXPORT_OK = qw( readenvi readenvi_hdr ); our @EXPORT = @EXPORT_OK; our %EXPORT_TAGS = ( Func=>[@EXPORT_OK] ); our $VERSION = "2.098"; $VERSION = eval $VERSION; our $verbose = 0; # for diagnostics =head1 NAME PDL::IO::ENVI - read ENVI data files into PDL =head1 SYNOPSIS use PDL::IO::ENVI; $pdl = readenvi("file.dat"); # implies there's a file.hdr next to it $hdr = readenvi_hdr("file.hdr"); # available separately, used for testing =head1 DESCRIPTION Allows you to read ENVI data into an ndarray. =head1 FUNCTIONS =head2 readenvi_hdr =for ref Given the name of an ENVI file header, parses the header and returns a hash-ref. TODO (1) verify that all required fields are present (2) parse map_info for pixel geolocation - handle keyword=value inside list (3) check that all sensor keywords are parsed (4) add support for offset/stride/count/reshape (5) implement writeenvi/wenvi routine (6) LATER: add support for complex data input, e.g. [2,S,L,B] (7) LATER: support unsigned long long =cut # This is a hash ref of the known/allowed keywords # in an ENVI header file. While these are the current # values, this implementation allows for new keywords # by parsing according to the following rules: # # (1) keywords are between the start of line and the = # (2) keywords are case insensitive # (3) white space is significant but amount and type is not # (4) string values will have leading and trailing whitespace removed # (5) canonical whitespace is a single ASCII space char # (6) single spaces in hash keywords will be replace by underscore # (7) canonical case for normalized keywords is lowercase # (8) required key-value pairs are always on a single line # (9) brace starting lists must be on same line as keyword = # (10) comment lines begin with ; in the first column # # Initially, we will parse all keyword = values but only fully # process for the required and optional entries needed for the # scissor data files. A hash value of 1 indicates required.. # my $envi_keywords = { 'band_names' => 0, # optional, CSV str of band names 'bands' => 1, # required, num of bands in image file 'bbl' => 0, # optional, (tbd) 'byte_order' => 1, # required, num 0 or 1 for LSF or MSF order 'class_lookup' => 0, # optional, (tbd) 'class_names' => 0, # optional, (tbd) 'classes' => 0, # optional, num of classes, including unclassified 'complex_function' => 0, # optional, (tbd) 'coordinate_system string' => 0, # optional, (tbd, for georeferencing) 'data_gain_values' => 0, # optional, CSV of gain vals for each band 'data_ignore_value' => 0, # optional, value of bad/missing element in data 'data_offset_values' => 0, # optional, CSV of offset vals for each band 'data_type' => 1, # required, id number in 1-6,9,12-15 'default_bands' => 0, # optional, CSV of 1 or 3 band numbers to display 'default_stretch' => 0, # optional, str of stretch to use for image display 'dem_band' => 0, # optional, (tbd) 'dem_file' => 0, # optional, (tbd) 'description' => 0, # optional, str describing the image or processing 'file_type' => 1, # required, ENVI Standard or from filetype.txt 'fwhm' => 0, # optional, CSV of band widths in wavelength units 'geo_points' => 0, # optional, CSV of x,y,lat,long of 1-4 image pts 'header_offset' => 1, # required, num bytes imbedded hdr in image file 'interleave' => 1, # required, str/num of BSQ/0, BIL/1, or BIP/2 'lines' => 1, # required, num lines in image 'map_info' => 0, # optional, CSV of values, as in # UTM, x0, y0, east0, north0, xpixsize, ypixsize, # UTM zone #, N or S (UTM only), datum, # units=str, rotation=val 'pixel_size' => 0, # optional, CSV of x and y pixel size in meters 'major_frame_offsets' => 0, # optional, (tbd) 'minor_frame_offsets' => 0, # optional, (tbd) 'projection_info' => 0, # optional, (tbd) 'reflectance_scale_factor' => 0, # optional, (tbd) 'rpc_info' => 0, # optional, (tbd) 'samples' => 1, # required, num samples per image line each band 'sensor_type' => 0, # optional, str Unknown or exact match in sensor.txt 'spectra_names' => 0, # optional, (tbd) 'wavelength' => 0, # optional, CSV of band center value in image 'wavelength_units' => 0, # optional, str with units for wavelength and fwhm 'x_start' => 0, # optional, (tbd) 'y_start' => 0, # optional, (tbd) 'z_plot_average' => 0, # optional, (tbd) 'z_plot_range' => 0, # optional, (tbd) 'z_plot_titles' => 0, # optional, (tbd) }; my $envi_required_keywords = []; foreach (sort keys %$envi_keywords) { push @$envi_required_keywords, $_ if $envi_keywords->{$_}; } my $interleave = { 'bsq' => [ qw( samples lines bands ) ], 'bil' => [ qw( samples bands lines ) ], 'bip' => [ qw( bands samples lines ) ], }; my $envi_data_types = []; $envi_data_types->[1] = 'byte'; $envi_data_types->[2] = 'short'; $envi_data_types->[3] = 'long'; $envi_data_types->[4] = 'float'; $envi_data_types->[5] = 'double'; $envi_data_types->[6] = undef; # complex, not supported, [2,shape] $envi_data_types->[9] = undef; # double complex, not supported, [2,shape] $envi_data_types->[12] = 'ushort'; $envi_data_types->[13] = 'ulong'; $envi_data_types->[14] = 'longlong'; $envi_data_types->[15] = undef; # unsigned long64, not supported, longlong? # Takes one arg, an ENVI hdr filename and # returns a hash reference of the header data # sub readenvi_hdr { my $hdrname = $_[0]; my $hdr = {}; # an easy progress message if ($verbose>1) { print STDERR "readenvi_hdr: reading ENVI hdr data from '@_'\n"; print STDERR "readenvi_hdr: required ENVI keywords are:\n"; print STDERR " @{ [sort @$envi_required_keywords] }\n"; } # open hdr file open my $hdrfile, '<', $hdrname or barf "readenvi_hdr: couldn't open '$hdrname' for reading: $!"; binmode $hdrfile; if ( eof($hdrfile) ) { barf "readenvi_hdr: WARNING '$hdrname' is empty, invalid ENVI format" } ITEM: while (!eof($hdrfile)) { # check for ENVI hdr start word on first line my $line = <$hdrfile>; if ($line !~ /^ENVI\r?$/) { barf "readenvi_hdr: '$hdrname' is not in ENVI hdr format" } $hdr->{ENVI} = 1; # this marks this header as ENVI # collect key=values into a hash my ($keyword,$val); my $in_list = 0; # used to track when we re reading a { } list LINE: while (defined($line = <$hdrfile>)) { next LINE if $line =~ /^;/; # skip comment line (maybe print?) $line =~ s/\s+$//; $line =~ s/^\s+//; next LINE if $line =~ /^$/; chomp $line; if ($in_list>0) { # append to value string $val .= " $line"; # need to keep whitespace for separation if ($line =~ /{/) { barf "readenvi_hdr: warning, found nested braces for line '$line'\n"; } if ( $val =~ /}$/ ) { # got to end of list # parse $val list print STDERR "readenvi_hdr: got list value = $val\n" if $verbose>1; # clear list parse flag $in_list--; } } else { # look for next keyword = line ($keyword,$val) = (undef, undef); ($keyword,$val) = $line =~ /^\s*([^=]+)=\s*(.*)$/; if (defined $keyword) { # warning exit in case underscores are used in keywords if ($keyword =~ /_/) { barf "readenvi_hdr: WARNING keyword '$keyword' contains underscore!" } # normalize to lc and single underscore for whitespace $keyword =~ s/\s+$//; $keyword =~ s/\s+/_/g; $keyword = lc $keyword; $val =~ s/^\s+//; $val =~ s/\s+$//; $in_list++ if $val =~ /^{/ and not $in_list; $in_list-- if $val =~ /}$/ and $in_list; next LINE if $in_list>0; # parse ENVI hdr lists and convert to perl array ref if ($val =~ /^{/) { # } vim gets confused # strip off braces $val =~ s/^{\s*//; $val =~ s/\s*}$//; my @listval = split ',\s*', $val; print STDERR "readenvi_hdr: expanded $keyword list value to (@listval)\n" if $verbose; $val = [@listval]; } my $reqoropt = $envi_keywords->{$keyword} ? 'required' : 'optional'; print STDERR " got $reqoropt $keyword = $val\n" if $verbose; # replace ignore_value by data_ignore_value $keyword =~ s/^ignore_value$/data_ignore_value/; $hdr->{$keyword} = $val; } else { print STDERR " NOT a 'keyword =' line: '$line'\n" if $verbose; } } } } # close hdr file close $hdrfile; return $hdr; } =head2 readenvi =for ref reads ENVI standard format image files =for usage $im = readenvi( filename ); # read image data ($im, $hdr) = readenvi( filename ); # read image data and hdr data hashref readenvi will look for an ENVI header file named filename.hdr If that file is not found, it will try with the windows convention of replacing the suffix of the filename by .hdr If valid header data is found, the image will be read and returned, with a ref to a hash of the hdr data in list context. NOTE: This routine only supports raw binary data at this time. =cut sub readenvi { barf 'Usage ($x [,$hdr]) = readenvi("filename")' if $#_ > 0; my $enviname = $_[0]; my $envi; # image data to return my $filehdr; # image file header (before ENVI image data) my $envihdr; # image hdr to return my $flexhdr = []; # an easy progress message print STDERR "readenvi: reading ENVI data from '@_'\n" if $verbose; # read ENVI header my $envihdrname; $envihdrname = $enviname . '.hdr'; if (! -f $envihdrname ) { $envihdrname = $enviname; $envihdrname =~ s/\.\w+$/.hdr/; } print STDERR "readenvi: ERROR could not find ENVI hdr file\n" unless -r $envihdrname; $envihdr = readenvi_hdr($envihdrname); # add read of imbedded_header data if have header_offset non-zero if ($envihdr->{header_offset}) { push @$flexhdr, { Type => 'byte', NDims => 1, Dims=>$envihdr->{header_offset} } } # see if we need to swap my $byteorder = ($Config{byteorder} =~ /4321$/) ? 1 : 0; print STDERR "readenvi: Config{byteorder} is $Config{byteorder}\n" if $verbose>1; if ($byteorder != $envihdr->{byte_order}) { print STDERR "readenvi: got byteorder of $byteorder, ENVI file has $envihdr->{byte_order}\n" if $verbose; print STDERR "readenvi: adding { Type => 'swap' } to \$flexhdr\n" if $verbose; push @$flexhdr, { Type => 'swap' } if $byteorder != $envihdr->{byte_order}; } # determine data type for readflex from interleave header value my $imagespec = { }; my $imagetype = $envi_data_types->[$envihdr->{data_type}]; print STDERR "readenvi: setting image { Type => $imagetype }\n" if $verbose; $imagespec->{Type} = $imagetype; # construct Dims for readflex my @imagedims = (); @imagedims = @{$interleave->{lc($envihdr->{interleave})}}; print STDERR "readenvi: Need Dims => @imagedims\n" if $verbose; my $imagedims = [ map { $envihdr->{$_} } @imagedims ]; print STDERR "readenvi: computed Dims => [", join( ', ', @{$imagedims} ), "]\n" if $verbose; $imagespec->{Dims} = $imagedims; $imagespec->{Ndims} = scalar(@$imagedims); push @$flexhdr, $imagespec; # read file using readflex my (@envidata) = readflex( $enviname, $flexhdr ); if (2==@envidata) { ($filehdr,$envi) = @envidata; $envihdr->{imbedded_header} = $filehdr; } else { ($envi) = @envidata; } # attach ENVI hdr to ndarray $envi->sethdr($envihdr); # handle ignore values by mapping to BAD if ( exists $envihdr->{data_ignore_value} ) { $envi->inplace->badflag; # set badflag for image $envi->inplace->setvaltobad($envihdr->{data_ignore_value}); } # return data and optionally header if requested return wantarray ? ($envi, $envihdr) : $envi; } =head1 SEE ALSO Sample data: L Header description: L Raster description: L =cut 1; PDL-IO-ENVI-2.098/t/0000755000175000017500000000000014736677227013435 5ustar osboxesosboxesPDL-IO-ENVI-2.098/t/t.hdr0000644000175000017500000000166114723753224014367 0ustar osboxesosboxesENVI description = { ENVI Spectral Index Result [Tue Dec 19 10:12:05 2023]} samples = 3409 lines = 2370 bands = 1 data type = 4 interleave = bsq file type = ENVI Standard header offset = 0 byte order = 0 map info = {UTM, 1.000, 1.000, 466860.000, 3353870.000, 1.000000e+01, 1.000000e+01, 15, North, WGS-84, units=Meters} coordinate system string = {PROJCS["WGS_1984_UTM_Zone_15N",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-93.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]} x start = 6691 y start = 4616 band names = { Modified Normalized Difference Water Index} data ignore value = -1.000000e+34 acquisition time = 2020-09-30T16:50:19.024Z PDL-IO-ENVI-2.098/t/readhdr.t0000644000175000017500000000247514723753224015227 0ustar osboxesosboxesuse strict; use warnings; use Test::More; use PDL::IO::ENVI; use File::Spec::Functions; my $hdr = readenvi_hdr(catfile qw(t t.hdr)); is_deeply $hdr, { 'ENVI' => 1, 'acquisition_time' => '2020-09-30T16:50:19.024Z', 'bands' => '1', 'byte_order' => '0', 'coordinate_system_string' => [ 'PROJCS["WGS_1984_UTM_Zone_15N"', 'GEOGCS["GCS_WGS_1984"', 'DATUM["D_WGS_1984"', 'SPHEROID["WGS_1984"', '6378137.0', '298.257223563]]', 'PRIMEM["Greenwich"', '0.0]', 'UNIT["Degree"', '0.0174532925199433]]', 'PROJECTION["Transverse_Mercator"]', 'PARAMETER["False_Easting"', '500000.0]', 'PARAMETER["False_Northing"', '0.0]', 'PARAMETER["Central_Meridian"', '-93.0]', 'PARAMETER["Scale_Factor"', '0.9996]', 'PARAMETER["Latitude_Of_Origin"', '0.0]', 'UNIT["Meter"', '1.0]]' ], 'data_ignore_value' => '-1.000000e+34', 'data_type' => '4', 'file_type' => 'ENVI Standard', 'header_offset' => '0', 'interleave' => 'bsq', 'lines' => '2370', 'map_info' => [ 'UTM', '1.000', '1.000', '466860.000', '3353870.000', '1.000000e+01', '1.000000e+01', '15', 'North', 'WGS-84', 'units=Meters' ], 'samples' => '3409', 'x_start' => '6691', 'y_start' => '4616', } or diag "got=", explain $hdr; done_testing; PDL-IO-ENVI-2.098/META.yml0000644000175000017500000000137214736677227014446 0ustar osboxesosboxes--- abstract: unknown author: - 'PerlDL Developers ' build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' PDL: '2.094' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.44, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: PDL-IO-ENVI no_index: directory: - t - inc requires: PDL: '2.094' resources: IRC: irc://irc.perl.org/#pdl bugtracker: https://github.com/PDLPorters/PDL-IO-ENVI/issues homepage: http://pdl.perl.org/ repository: git://github.com/PDLPorters/PDL-IO-ENVI.git version: '2.098' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' PDL-IO-ENVI-2.098/Changes0000644000175000017500000000021614736675751014464 0ustar osboxesosboxes2.098 2025-01-06 - add licence information 2.097 2024-12-09 - install pdldoc, add repo metadata 2.096 2024-12-04 - split out from PDL 2.095 PDL-IO-ENVI-2.098/MANIFEST0000644000175000017500000000036614736677227014330 0ustar osboxesosboxesChanges ENVI.pm Makefile.PL MANIFEST This list of files t/readhdr.t t/t.hdr META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) PDL-IO-ENVI-2.098/Makefile.PL0000644000175000017500000000202714736671344015137 0ustar osboxesosboxesuse ExtUtils::MakeMaker; use strict; use warnings; use PDL::Core::Dev; my $package_name = "PDL::IO::ENVI"; (my $repo = $package_name) =~ s#::#-#g; $repo = "PDLPorters/$repo"; WriteMakefile( NAME => $package_name, VERSION_FROM => 'ENVI.pm', AUTHOR => 'PerlDL Developers ', LICENSE=> "perl", CONFIGURE_REQUIRES => { 'ExtUtils::MakeMaker' => 0, 'PDL' => '2.094', }, PREREQ_PM => { 'PDL' => '2.094', }, META_MERGE => { "meta-spec" => { version => 2 }, resources => { homepage => 'http://pdl.perl.org/', bugtracker => {web=>"https://github.com/$repo/issues"}, repository => { url => "git://github.com/$repo.git", type => 'git', web => "https://github.com/$repo", }, x_IRC => 'irc://irc.perl.org/#pdl', }, }, ); sub MY::postamble { my $oneliner = PDL::Core::Dev::_oneliner(qq{exit if \$ENV{DESTDIR}; use PDL::Doc; eval { PDL::Doc::add_module(shift); }}); qq|\ninstall :: pure_install\n\t$oneliner \$(NAME)\n|; }