Astro-WaveBand

 view release on metacpan or  search on metacpan

lib/Astro/WaveBand.pm  view on Meta::CPAN


    $w = Astro::WaveBand->new(
        Wavelength => $wavelength,
        Instrument => 'CGS4');

    $filter = $w->filter;
    $wave = $w->wavelength;
    $band = $w->waveband;  # radio, xray, submm
    $freq = $w->frequency;
    $wnum = $w->wavenumber;

    $natural = $w->natural;
    $natural = "$w";

    $w->natural_unit("wavelength");

    if ($w1 > $w2) {...}
    if ($w1 == $w2) {...}

=head1 DESCRIPTION

Class to transparently deal with the conversion between filters,
wavelength, frequency and other methods of specifying a location
in the electro-magentic spectrum.

The class tries to determine the natural form of the numbers such that
a request for a summary of the object when it contains 2.2 microns
would return the filter name but would return the wavelength if it was
not a standard filter. In ambiguous cases an instrument name is
required to decide what to return. In really ambiguous cases the user
can specify the unit in which to display the numbers on
stringification.

Used mainly as a way of storing a single number in a database table
but using logic to determine the number that an observer is most likely
to understand.

Numerical comparison operators can be used to compare two C<Astro::WaveBand>
objects. When checking equality, the "natural" and "instrument" methods are
used, so if two C<Astro::WaveBand> objects return the same value from those
methods, they are considered to be equal. When checking other comparisons
such as greater than, the wavelength is used.

=cut

use 5.006;
use strict;
use warnings;
use Carp;

# Register an Astro::WaveBand warning category
use warnings::register;

# CVS version: $Revision$
our $VERSION = '0.12';

# Overloading
use overload
    '""' => "natural",
    '==' => "equals",
    '!=' => "not_equals",
    '<=>' => "compare",
    'fallback' => 1;

# Constants

# Speed of light in m/s
use constant CLIGHT => 299792458;

# list of instruments specific to a telescope
my %TELESCOPE = (
    UKIRT => [
        qw/CGS4 IRCAM UFTI UIST MICHELLE WFCAM/],
    JCMT => [
        qw/SCUBA SCUBA-2 RXA3 RXA3M RXB3 RXW RXH3
            ACSIS DAS HARP ALAIHI UU AWEOWEO
            KUNTUR/
    ],
);

# Continuum Filters are keyed by instrument
# although if an instrument is not specified the filters
# hash will be searched for a match if none is available in
# GENERIC
my %FILTERS = (
    GENERIC => {
        U => 0.365,
        B => 0.44,
        V => 0.55,
        R => 0.70,
        I => 0.90,
        J => 1.25,
        H => 1.65,
        K => 2.2,
        L => 3.45,
        M => 4.7,
        N => 10.2,
        Q => 20.0,
        up => 0.355,
        gp => 0.470,
        rp => 0.620,
        ip => 0.750,
        zp => 0.880,
        Pu => 0.355,
        Pg => 0.470,
        Pr => 0.620,
        Pi => 0.750,
        Pz => 0.880,
        Y => 1.020,  # this will get incorrectly classed as infrared
        w => 0.608,
        SO => 0.600,
    },
    WFCAM => {
        "Z" => 0.83,
        "Y" => 0.97,
        "J" => 1.17,
        "H" => 1.49,
        "K" => 2.03,
        "1-0S1" => 2.111,
        "BGamma" => 2.155,
        "1.205nbJ" => 1.205,

lib/Astro/WaveBand.pm  view on Meta::CPAN


    if ($wb1->compare($wb2)) {...}

This method will return -1 if, in the above example, $wb1 is of
a shorter wavelength than $wb2, 0 if the wavelengths are equal,
and +1 if $wb1 is of a longer wavelength than $wb2. Please note
that for strict waveband equality the C<equals> method should be
used, as that method uses the C<natural> method to check if two
wavebands are identical.

This method is overloaded with the standard numerical comparison
operators, so to check if one waveband is shorter than another
you would do

    if ($wb1 < $wb2) {...}

and it will work as you expect. This method does not overload
the == operator; see the C<equals> method for that.

=cut

sub compare {
    my ($object1, $object2, $was_reversed) = @_;
    ($object1, $object2) = ($object2, $object1) if $was_reversed;

    return $object1->wavelength <=> $object2->wavelength;
}

=item B<equals>

Compares two C<Astro::WaveBand> objects for equality.

    if ($wb1->equals($wb2)) {...}

This method will return 1 if, in the above example, both
C<Astro::WaveBand> objects return the same value from the
C<natural> method AND for the C<instrument> method (if it
is defined for both objects) , and 0 of they return different values.

This method is overloaded using the == operator, so

    if ($wb1 == $wb2) {...}

is functionally the same as the first example.

=cut

sub equals {
    my $self = shift;
    my $comp = shift;

    if (defined($self->instrument) && defined($comp->instrument)) {
        return (($self->natural eq $comp->natural)
            && ($self->instrument eq $comp->instrument));
    }
    else {
        return ($self->natural eq $comp->natural);
    }
}

=item B<not_equals>

Compares two C<Astro::WaveBand> objects for inequality.

    if ($wb1->not_equals($wb2)) {...}

This method will return 1 if, in the above example, either the
C<natural> method or the C<instrument> method return different
values. If the instrument is undefined for either object, then
the C<natural> method will be used.

This method is overloaded using the != operator, so

    if ($wb1 != $wb2) {...}

is functionally the same as the first example.

=cut

sub not_equals {
    my $self = shift;
    my $comp = shift;

    if (!defined($self->instrument) || !defined($comp->instrument)) {
        return ($self->natural ne $comp->natural);
    }
    else {
        return (($self->natural ne $comp->natural)
            || ($self->instrument ne $comp->instrument));
    }
}

=back

=begin __PRIVATE_METHODS__

=head2 Private Methods

=over 4

=item B<_cache>

Retrieve the hash reference associated with the cache (in a scalar
context) or the contents of the hash (in a list context).

    $ref = $w->cache;
    %cache = $w->cache;

=cut

sub _cache {
    my $self = shift;
    if (wantarray) {
        return %{$self->{Cache}};
    }
    else {
        return $self->{Cache};
    }
}

=item B<_store_in_cache>

Store values in the cache associated with particular types.

    $w->_store_in_cache(
        "filter" => "K",
        "frequency" => 1.4E14);

If the cache already contains a value for this entry the cache
is cleared prior to storing it (unless it contains the same value)
on the assumption that the cache is no longer consistent.

More than one key can be supplied. All keys are tested for prior
existence before inserting the new ones.

=cut

sub _store_in_cache {
    my $self = shift;
    my %entries = @_;



( run in 1.556 second using v1.01-cache-2.11-cpan-39bf76dae61 )