blib/lib/Parse/SpectrumDirect/RadioFrequency.pm

filter: Statementstmt 96.3% 79 / 82 Branchbran 81.8% 18 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 94.1% 112 / 119 CC 16 SCAR 13.2
CC
16
Cov
93%
_fixup_station_data
6.1
_extract_legend
5.0
parse
4.2
CRAP 3.7
↑ Summary
1 package Parse::SpectrumDirect::RadioFrequency;
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Parse::SpectrumDirect::RadioFrequency - Parse Industry Canada "Spectrum Direct" radio frequency search output
9  
10 =head1 VERSION
11  
12 Version 0.100
13  
14 =cut
15  
16 3 our $VERSION = '0.100';
17  
18 =head1 DESCRIPTION
19  
20 This module provides a parser for the "Radio Frequency Search" text-format
21 output from Industry Canada's Spectrum Direct service. This service provides
22 information on the location of RF spectrum licensing, transmitter locations,
23 etc.
24  
25 The service is available at http://www.ic.gc.ca/eic/site/sd-sd.nsf/eng/home
26  
27 The text export is a series of fixed-width fields, with field locations and
28 descriptions present in a legend at the end of the data file.
29  
30 =head1 SYNOPSIS
31  
32 my $parser = Parse::SpectrumDirect::RadioFrequency->new();
33  
34 $parser->parse( $prefetched_output );
35  
36 my $legend_hash = $parser->get_legend();
37  
38 my $stations = $parser->get_stations();
39  
40 =head1 METHODS
41  
42 =over 4
43  
44 =item new ( )
45  
46 Creates a new parser.
47  
48 =cut
49  
50 sub new
51 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
52 1 my ($class) = @_;
53 1 return bless({},$class);
54 }
55  
56 =item parse ( $raw )
57  
58 Parses the raw data provided. Returns a true value if successful, a false if
59 not.
60  
61 Parsed data can be obtained with get_legend() and get_stations() (see below).
62  
63 =cut
64  
65 sub parse
66 4 ▶{
Subroutine parse: called CC=4 SCAR=14.3
Pod documented
67 4 my ($self, $raw) = @_;
68 4 delete $self->{legend};
69 4 delete $self->{stations};
70  
71 4 ▶ return undef unless $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw 2 2 4
72  
73 2 ▶ if( ! $self->_extract_legend( $raw ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_extract_legend($raw)) 1 1 2
74 1 delete $self->{legend};
75 1 return undef;
76 }
77 1 ▶ if( ! $self->_extract_stations( $raw ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_extract_stations($raw)) 0 1 1
78 0 delete $self->{legend};
79 0 delete $self->{stations};
80 0 return undef;
81 }
82  
83 1 return 1;
84 }
85  
86 =item get_legend ()
87  
88 Returns the description of fields as parsed from the input data.
89  
90 Return value is an array reference containing one hash reference per field.
91  
92 Each hashref contains:
93  
94 =over 4
95  
96 =item name
97  
98 As in source legend, stripped of trailing spaces
99  
100 =item units
101  
102 Units for data value, if determinable from legend.
103  
104 =item key
105  
106 Key used in station hashes. Generated from name stripped of unit information, and whitespaces converted to _.
107  
108 =item start
109  
110 Column index to start extracting data value
111  
112 =item len
113  
114 Column width, used for data extraction.
115  
116 =back
117  
118 =cut
119  
120 sub get_legend
121 1 ▶{
Subroutine get_legend: called CC=1 SCAR=0.0
Pod documented
122 1 my ($self) = @_;
123 1 return $self->{legend};
124 }
125  
126 =item get_stations ()
127  
128 Returns station information as parsed from the input data.
129  
130 =back
131  
132 =cut
133  
134 sub get_stations
135 3 ▶{
Subroutine get_stations: called CC=1 SCAR=0.0
Pod documented
136 3 my ($self) = @_;
137 3 return $self->{stations};
138 }
139  
140 # Extract legend as an arrayref of hashrefs.
141 sub _extract_legend
142 2 ▶{
Subroutine _extract_legend: called CC=5 SCAR=16.1
143 2 my ($self, $raw) = @_;
144  
145 2 my ($raw_legend) = $raw =~ m/Field Position Legend(.*)/sm;
146 2 ▶ return undef unless $raw_legend;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw_legend 1 1 2
147  
148 1 $self->{legend} = [];
149 1 foreach my $line (split(/\n/, $raw_legend)) {
150  
151 # Lines are in the format of:
152 # name start - end
153 # with the columns starting at 1.
154  
155 73 my ($name, $start, $end) = $line =~ m/(.*?)\s+(\d+) - (\d+)/;
156 73 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 4 69 73
157 69 $name =~ s/\s+$//;
158  
159 # Pull off units
160 69 my $units = undef;
161 69 ▶ if( $name =~ m/\((.*?)\)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\((.*?)\)$/) 27 42 69
162 27 $units = $1;
163 }
164  
165 69 my $key = $name;
166 69 $key =~ s/\(.*?\)//g;
167 69 $key =~ s/\s+$//;
168 69 $key =~ s/\s+/_/g;
169  
170 69 my $col = {
171 key => $key,
172 units => $units,
173 name => $name,
174 start => $start - 1,
175 len => $end - $start + 1,
176 };
177 69 push @{$self->{legend}},$col;
178  
179 }
180  
181 1 return $self->{legend};
182 }
183  
184 # Return station data as an arrayref of hashrefs, one per row.
185 sub _extract_stations
186 1 ▶{
Subroutine _extract_stations: called CC=3 SCAR=11.0
187 1 my ($self, $raw) = @_;
188  
189 1 my ($data) = $raw =~ m/\[DATA\](.*)\[\/DATA\]/sm;
190 1 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 1 1
191  
192 1 my $regex = join('\s', map { "(.{$_->{len},$_->{len}})" } @{$self->{legend}} );
193 1 my @key_ary = map { $_->{key} } @{$self->{legend}};
194  
195 1 $self->{stations} = [];
196 1 foreach my $line (split(/\n/,$data)) {
197 415 my (@tmprow) = $line =~ /$regex/o;
198  
199 415 my %row;
200 415 @row{@key_ary} = map { s/^\s+|\s+$//g; $_ } @tmprow;
201 415 push @{$self->{stations}}, \%row;
202 }
203 1 $self->_fixup_station_data();
204  
205 1 return $self->{stations};
206 }
207  
208 # Fix some common stupidity with station data
209 sub _fixup_station_data
210 1 ▶{
Subroutine _fixup_station_data: called CC=6 SCAR=18.1
211 1 my ($self) = @_;
212  
213  
214 # Convert to decimal degrees from ddmmss. Also, force
215 # longitude to west (negative), since this is Canada we're
216 # dealing with.
217 1 foreach my $s (@{$self->{stations}}) {
218 415 ▶ $s->{Latitude} = _dd_from_dms( $s->{Latitude} ) if exists $s->{Latitude};
Branch: 1 branch
BranchTrueFalseTotal
if exists $s->{"Latitude"} 415 0 415
219 415 ▶ $s->{Longitude} = 0 - _dd_from_dms( $s->{Longitude} ) if exists $s->{Longitude};
Branch: 1 branch
BranchTrueFalseTotal
if exists $s->{"Longitude"} 415 0 415
220 }
221  
222 # Change units in legend, too
223 1 foreach my $l (@{$self->{legend}}) {
224 69 ▶ if( $l->{key} =~ /^(?:Latitude|Longitude)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->{"key"} =~ /^(?:Latitude|Longitude)$/) 2 67 69
225 2 $l->{units} = 'decimal degrees';
226 }
227 }
228 }
229  
230 sub _dd_from_dms
231 830 ▶{
Subroutine _dd_from_dms: called CC=2 SCAR=6.9
232 830 my ($dms) = @_;
233  
234 830 ▶ return 0.0 unless $dms;
Branch: 1 branch
BranchTrueFalseTotal
unless $dms 14 816 830
235  
236 816 my $ss = substr( $dms, -2, 2, '');
237 816 my $mm = substr( $dms, -2, 2, '');
238 816 my $dd = $dms;
239  
240 816 return sprintf('%.6f', $dd + ($mm * 60 + $ss)/3600);
241 }
242  
243  
244 3 1;
245 __END__
↑ Summary