zeroclickinfo-goodies/lib/DDG/Goodie/Wavelength.pm

80 lines
2.2 KiB
Perl
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package DDG::Goodie::Wavelength;
# ABSTRACT: Frequency to wavelength
use utf8;
use DDG::Goodie;
use constant SPEED_OF_LIGHT => 299792458; #meters/second
use constant MULTIPLIER => {
hz => 1,
khz => 10**3,
mhz => 10**6,
ghz => 10**9,
thz => 10**12
};
use constant FORMAT_UNITS => {
hz => 'Hz',
khz => 'kHz',
mhz => 'MHz',
ghz => 'GHz',
thz => 'THz'
};
zci answer_type => "wavelength";
zci is_cached => 1;
# Metadata. See https://duck.co/duckduckhack/metadata for help in filling out this section.
name "Wavelength";
description "Frequency to Wavelength translation";
primary_example_queries "λ 2.4GHz", "144.39 MHz wavelength","lambda 1500kHz", "1Mhz lambda VF=0.85";
category "physical_properties";
topics "math", "science", "special_interest";
code_url "https://github.com/nebulous/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Wavelength.pm";
attribution github => ["nebulous", "John Lifsey"],
cpan => "nebulous",
twitter => "aresweet";
# Triggers
triggers any => "λ", "wavelength", "lambda";
# Handle statement
handle remainder => sub {
my ($query) = @_;
my ($freq,$units) = $query =~ m/([\d\.]+)\s*((k|M|G|T)?hz)/i;
return unless $freq and $units;
my ($vf) = $query =~ m/\bvf[ =]([\d\.]+)/i;
$vf = 1 if (!$vf or $vf>1 or $vf<0);
my $velocity_text = ($vf == 1 ? '' : "$vf × ").'Speed of light in a vacuum';
my $mul = MULTIPLIER->{lc($units)};
my $hz_freq = $freq * $mul * (1/$vf);
my $output_value = (SPEED_OF_LIGHT / $hz_freq);
my $output_units = 'Meters';
# Express higher freqs in cm/mm.
# eg UHF 70cm band, microwave 3mm, etc
if ($output_value<1) {
$output_units = 'Centimeters';
$output_value *= 100;
if ($output_value<1) {
$output_units = 'Millimeters';
$output_value *= 10;
}
}
my $result_text = "λ = $output_value $output_units";
my $operation_text = "Wavelength of $freq ".FORMAT_UNITS->{lc($units)}." ($velocity_text)";
return $result_text,
structured_answer => {
input => [],
operation => $operation_text,
result => $result_text,
};
};
1;