PET/Thornthwaite.pm

filter: Statementstmt 90.9% 150 / 165 Branchbran 62.7% 59 / 94 Conditioncond 57.1% 24 / 42 MC/DCmcdc 36.0% 9 / 25 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 75.0% 253 / 337 CC 62 SCAR 37.3
CC
62
Cov
77%
pet
50.4
new
41.6
BEGIN
1.0
CRAP 41.7
↑ Summary PET.pm »
1 package Meteorology::PET::Thornthwaite;
2  
3 5 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Array::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Math::Trig qw(pi acos tan);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Scalar::Util qw(blessed looks_like_number reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 our $VERSION = "0.01";
13  
14 =pod
15  
16 =head1 NAME
17  
18 Meteorology::PET::Thornthwaite - Calculate potential evapotranspiration (PET) using the Thornthwaite Method
19  
20 =head1 SYNOPSIS
21  
22 use Meteorology::PET::Thornthwaite;
23
24 my $pet_calc = Meteorology::PET::Thornthwaite->new({
25 LATITUDE => $lat,
26 TEMPERATURE_CLIMO => \@temperature_climo,
27 TEMPERATURE_UNITS => 'F',
28 PET_UNITS => 'mm',
29 });
30
31 my $pet = $pet_calc->pet({
32 YDAY => 180,
33 NDAYS => 31,
34 TEMPERATURE => $temperature,
35 TEMPERATURE_UNITS => 'C',
36 });
37  
38 =head1 DESCRIPTION
39  
40 The C<Meteorology::PET::Thornthwaite> module provides an object-oriented interface with a dedicated method to
41 calculate potential evapotranspiration (PET) using equations developed by Charles Warren Thornthwaite and published
42 in I<Geographical Review> in 1948. Relying on temperature data and daylight hours as a function of latitude and the
43 time of year, the Thornthwaite method is straightforward to implement, and has been utilized in many weather and
44 climate applications, such as the two-layer soil water balance model used in the Palmer Drought Index (Palmer 1965),
45 and the one-layer "Leaky Bucket" soil moisture model (e.g., Huang et al. 1996) used for seasonal temperature forecasts.
46  
47 In addition to a location's temperature observations, the time of year, and its latitude, the Thornthwaite Equation
48 also requires a monthly temperature climatology for the location, used to compute a term called the Temperature
49 Efficiency Index (TEI). To allow for faster processing, therefore, information about the location(s) to be used, such
50 as the monthly temperature climatology and latitude, are supplied at the time of object construction (C<new>), allowing
51 the TEI to be pre-computed.
52  
53 The module is designed to work with two data input types:
54  
55 =over 4
56  
57 =item * Scalar values for a single location
58  
59 =item * L<PDL> ndarray objects for multiple locations
60  
61 =back
62  
63 The module automatically detects which input type the caller is using by analyzing the latitude and temperature
64 climatology arguments passed to the constructor. All PDL arguments must have the exact same spatial dimensions. The
65 PDL module is not required to be installed on your system if you are only using the scalar argument mode.
66  
67 All input and output data units must be specified. The module has a limited ability to convert between units: Kelvin/
68 Celsius/Fahrenheit (K, C, F) for temperatures, and millimeters/inches (mm/in) for PET.
69  
70 =head1 METHODS
71  
72 =head2 new
73  
74 my $pet_calc = Meteorology::PET::Thornthwaite->new({
75 LATITUDE => $lat,
76 TEMPERATURE_CLIMO => \@temperature_climo,
77 TEMPERATURE_UNITS => 'F',
78 PET_UNITS => 'mm',
79 });
80  
81 Returns a C<Meteorology::PET::Thornthwaite> object to the calling program, defining the location(s) to be used in
82 PET calculations by latitude and 12-month temperature climatology. The constructor accepts a L<hash ref|https://perldoc.perl.org/perlref>
83 argument with four required parameters:
84  
85 =over 4
86  
87 =item * C<LATITUDE> - location latitude(s) ranging from -90 to 90. Can be a numeric scalar value for a single
88 location, or a PDL ndarray object for multiple locations.
89  
90 =item * C<TEMPERATURE_CLIMO> - an array ref containing 12 monthly temperature climatological values for the location(s).
91 The array elements can be numeric scalars for a single location, or PDL ndarray objects for multiple locations. The
92 dimensions of each PDL object must match those of C<LATITUDE> exactly.
93  
94 =item * C<TEMPERATURE_UNITS> - The units of the C<TEMPERATURE_CLIMO> values. Can be C<C> (Celsius), C<K> (Kelvin), or
95 C<F> (Fahrenheit).
96  
97 =item * C<PET_UNITS> - The units of the PET values that this object will return via calls to the C<pet> method. Can be
98 C<mm> (millimeters) or C<in> (inches).
99  
100 =back
101  
102 =cut
103  
104 9 ▶sub new {
Subroutine new: called CC=30 SCAR=37.3
Pod documented
105 9 my $class = shift;
106 9 my $method = "${class} (new)";
107 9 my $args = shift;
108 9 ▶ croak("$method - Argument required and must be a hash ref") unless(ref($args) && reftype($args) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args and builtin::reftype($args) eq "HASH" 1 8 9
Condition66%2/3
ref $args and builtin::reftype($args) eq "HASH"
!ll&&!rl&&r
108
MC/DC50%1/2
ref $args and builtin::reftype($args) eq "HASH"
ref $args builtin::reftype($args) eq "HASH"
Truth table66%2/3
A and B
A= ref $argsB= builtin::reftype($args) eq "HASH"
ABresult
0X0
100
111
109  
110 # Validate that all of the required parameters are defined
111 8 ▶ croak("$method - LATITUDE parameter is undefined") unless(defined($args->{LATITUDE}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"LATITUDE"} 0 8 8
112 8 ▶ croak("$method - TEMPERATURE_CLIMO parameter is undefined") unless(defined($args->{TEMPERATURE_CLIMO}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"TEMPERATURE_CLIMO"} 0 8 8
113 8 ▶ croak("$method - TEMPERATURE_UNITS parameter is undefined") unless(defined($args->{TEMPERATURE_UNITS}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"TEMPERATURE_UNITS"} 1 7 8
114 7 ▶ croak("$method - PET_UNITS parameter is undefined") unless(defined($args->{PET_UNITS}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"PET_UNITS"} 0 7 7
115  
116 # Validate that TEMPERATURE_CLIMO has 12 values
117 7 ▶ croak("$method - TEMPERATURE_CLIMO parameter must be an array ref with 12 elements") unless(ref($args->{TEMPERATURE_CLIMO}) eq 'ARRAY' && @{$args->{TEMPERATURE_CLIMO}} == 12);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{"TEMPERATURE_CLIMO"} eq "ARRAY" and @{$args->{"TEMPERATURE_CLIMO"};} == 12 1 6 7
Condition66%2/3
ref $args->{"TEMPERATURE_CLIMO"} eq "ARRAY" and @{$args->{"TEMPERATURE_CLIMO"};} == 12
!ll&&!rl&&r
016
MC/DC50%1/2
ref $args->{"TEMPERATURE_CLIMO"} eq "ARRAY" and @{$args->{"TEMPERATURE_CLIMO"};} == 12
ref $args->{"TEMPERATURE_CLIMO"} eq "ARRAY" @{$args->{"TEMPERATURE_CLIMO"};} == 12
Truth table66%2/3
A and B
A= ref $args->{"TEMPERATURE_CLIMO"} eq "ARRAY"B= @{$args->{"TEMPERATURE_CLIMO"};} == 12
ABresult
0X0
100
111
118  
119 # Validate units parameters
120 6 ▶ croak("$method - TEMPERATURE_UNITS parameter is invalid") unless($args->{TEMPERATURE_UNITS} =~ /^[CFK]$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"TEMPERATURE_UNITS"} =~ /^[CFK]$/ 0 6 6
121 6 ▶ croak("$method - PET_UNITS parameter is invalid") unless($args->{PET_UNITS} =~ /^(mm|in)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"PET_UNITS"} =~ /^(mm|in)$/ 0 6 6
122  
123 # Add units params to the object under construction
124 6 my $self = {};
125 6 $self->{TEMPERATURE_UNITS} = $args->{TEMPERATURE_UNITS};
126 6 $self->{PET_UNITS} = $args->{PET_UNITS};
127  
128 # Initialize the rest of the object params, then determine object mode (scalar or PDL)
129 6 my $pdl_mode = 0;
130 6 my $lat = undef;
131 6 my $tei = undef;
132  
133 # --- PDL Mode ---
134  
135 6 ▶ if(blessed($args->{LATITUDE}) && $args->{LATITUDE}->isa('PDL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($args->{'LATITUDE'}) and $args->{'LATITUDE'}->isa('PDL')) { } 1 5 6
Condition66%2/3
builtin::blessed($args->{"LATITUDE"}) and $args->{"LATITUDE"}->isa("PDL")
!ll&&!rl&&r
501
MC/DC50%1/2
builtin::blessed($args->{"LATITUDE"}) and $args->{"LATITUDE"}->isa("PDL")
builtin::blessed($args->{"LATITUDE"}) $args->{"LATITUDE"}->isa("PDL")
Truth table66%2/3
A and B
A= builtin::blessed($args->{"LATITUDE"})B= $args->{"LATITUDE"}->isa("PDL")
ABresult
0X0
100
111
136 1 $pdl_mode = 1;
137  
138 1 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require PDL; 1 } } or croak("$method - In PDL mode, but the PDL module cannot be loaded") 1 0 1
139 1 require PDL;
140 1 1;
141 } or croak("$method - In PDL mode, but the PDL module cannot be loaded");
142  
143 # Validate PDL args and calculate TEI
144 1 $lat = $args->{LATITUDE}->copy;
145 0 ▶ unless($lat->min >= -90 && $lat->max <= 90) { croak("$method - LATITUDE values must be within [-90, 90]"); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($lat->min >= -90 and $lat->max <= 90) 0 1 1
Condition33%1/3
$lat->min >= -90 and $lat->max <= 90
!ll&&!rl&&r
010
MC/DC0%0/2
$lat->min >= -90 and $lat->max <= 90
$lat->min >= -90 $lat->max <= 90
Truth table33%1/3
A and B
A= $lat->min >= -90B= $lat->max <= 90
ABresult
0X0
100
111
146 1 $tei = $lat->zeros;
147 1 my $array_comp = Array::Compare->new();
148 1 my @lat_dims = $lat->dims;
149  
150 1 ▶ for(my $i=0; $i<@{$args->{TEMPERATURE_CLIMO}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$args->{"TEMPERATURE_CLIMO"};}) 12 1 13
151 12 ▶ croak("$method - In PDL mode, but element $i in the TEMPERATURE_CLIMO parameter is not a PDL object") unless(blessed($args->{TEMPERATURE_CLIMO}[$i]) && $args->{TEMPERATURE_CLIMO}[$i]->isa('PDL'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($args->{"TEMPERATURE_CLIMO"}[$i]) and $args->{"TEMPERATURE_CLIMO"}[$i]->isa("PDL") 0 12 12
Condition33%1/3
builtin::blessed($args->{"TEMPERATURE_CLIMO"}[$i]) and $args->{"TEMPERATURE_CLIMO"}[$i]->isa("PDL")
!ll&&!rl&&r
0012
MC/DC0%0/2
builtin::blessed($args->{"TEMPERATURE_CLIMO"}[$i]) and $args->{"TEMPERATURE_CLIMO"}[$i]->isa("PDL")
builtin::blessed($args->{"TEMPERATURE_CLIMO"}[$i]) $args->{"TEMPERATURE_CLIMO"}[$i]->isa("PDL")
Truth table33%1/3
A and B
A= builtin::blessed($args->{"TEMPERATURE_CLIMO"}[$i])B= $args->{"TEMPERATURE_CLIMO"}[$i]->isa("PDL")
ABresult
0X0
100
111
152 12 my $mtemp_in = $args->{TEMPERATURE_CLIMO}[$i]->copy;
153 12 my @pdl_dims = $mtemp_in->dims;
154 12 ▶ croak("$method - The dimensions of element $i in the TEMPERATURE_CLIMO parameter do not match the LATITUDE parameter's dimensions") unless($array_comp->compare(\@lat_dims,\@pdl_dims));
Branch: 1 branch
BranchTrueFalseTotal
unless $array_comp->compare(\@lat_dims, \@pdl_dims) 0 12 12
155 12 my $mtemp = undef;
156 0 ▶ if($args->{TEMPERATURE_UNITS} eq 'F') { $mtemp = ($mtemp_in - 32.0) / 1.8; }
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'TEMPERATURE_UNITS'} eq 'F') { } 0 12 12
elsif ($args->{'TEMPERATURE_UNITS'} eq 'K') { } 0 12 12
157 0 elsif($args->{TEMPERATURE_UNITS} eq 'K') { $mtemp = $mtemp_in - 273.15; }
158 12 else { $mtemp = $mtemp_in->copy; }
159 12 my $ptemps = $mtemp->lclip(0);
160 12 $tei += ($ptemps / 5) ** 1.514;
161 }
162  
163 }
164  
165 # --- Scalar Mode ---
166  
167 else {
168 5 $lat = $args->{LATITUDE};
169 5 ▶ unless(looks_like_number($lat) && $lat >= -90 && $lat <= 90) { croak("$method - LATITUDE value must be numeric and within [-90, 90]"); }
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($lat) and $lat >= -90 and $lat <= 90) 1 4 5
Condition50%3/6
&looks_like_number($lat) and $lat >= -90 and $lat <= 90
!ll&&!rl&&r
014
&looks_like_number($lat) and $lat >= -90
!ll&&!rl&&r
005
MC/DC33%1/3
&looks_like_number($lat) and $lat >= -90 and $lat <= 90
&looks_like_number($lat) $lat >= -90 $lat <= 90
Truth table50%2/4
A and B and C
A= &looks_like_number($lat)B= $lat >= -90C= $lat <= 90
ABCresult
0XX0
10X0
1100
1111
170 4 $tei = 0;
171  
172 4 ▶ for(my $i=0; $i<@{$args->{TEMPERATURE_CLIMO}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$args->{"TEMPERATURE_CLIMO"};}) 48 4 52
173 0 ▶ unless(looks_like_number($args->{TEMPERATURE_CLIMO}[$i])) { croak("$method - Element $i in the TEMPERATURE_CLIMO parameter must be numeric"); }
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($args->{"TEMPERATURE_CLIMO"}[$i])) 0 48 48
174 48 my $mtemp_in = $args->{TEMPERATURE_CLIMO}[$i];
175 48 my $mtemp = undef;
176 0 ▶ if($args->{TEMPERATURE_UNITS} eq 'F') { $mtemp = ($mtemp_in - 32.0) / 1.8; }
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'TEMPERATURE_UNITS'} eq 'F') { } 0 48 48
elsif ($args->{'TEMPERATURE_UNITS'} eq 'K') { } 0 48 48
177 0 elsif($args->{TEMPERATURE_UNITS} eq 'K') { $mtemp = $mtemp_in - 273.15; }
178 48 else { $mtemp = $mtemp_in; }
179 48 ▶ my $ptemp = $mtemp > 0 ? $mtemp : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mtemp > 0 ? : 33 15 48
180 48 $tei += ($ptemp / 5) ** 1.514;
181 }
182  
183 }
184  
185 5 $self->{LATITUDE} = $lat;
186 5 $self->{TEI} = $tei;
187 5 $self->{PDL_MODE} = $pdl_mode;
188  
189 5 bless($self,$class);
190 5 return $self;
191 }
192  
193 =pod
194  
195 =head2 pet
196  
197 my $pet = $pet_calc->pet({
198 YDAY => 180,
199 NDAYS => 31,
200 TEMPERATURE => $temperature,
201 TEMPERATURE_UNITS => 'C',
202 });
203  
204 Returns a scalar PET value to the calling program in scalar mode, or a PDL object containing PET values in PDL
205 mode for the locations defined in the constructor. Accepts a L<hash ref|https://perldoc.perl.org/perlref> argument
206 with four required parameters:
207  
208 =over 4
209  
210 =item * C<YDAY> - the day of the year of the midpoint of the time period to calculate PET, ranging from 1 (Jan 1) to
211 366 (Dec 31 of a leap year).
212  
213 =item * C<NDAYS> - the number of days in the time period for which to calculate PET. The original Thornthwaite
214 formulation assumed monthly time scales, but the equation can be scaled for daily, weekly, bi-weekly, or other periods.
215  
216 =item * C<TEMPERATURE> - the mean temperature observed at the location(s) during the time period defined by C<YDAY> and
217 C<NDAYS>. Can be a numeric scalar value or a PDL object with dimensions matching the objects passed to the constructor.
218  
219 =item * C<TEMPERATURE_UNITS> - The units of the C<TEMPERATURE> value(s). Can be C<C> (Celsius), C<K> (Kelvin), or
220 C<F> (Fahrenheit).
221  
222 =back
223  
224 =cut
225  
226 8 ▶sub pet {
Subroutine pet: called CC=33 SCAR=39.2
Pod documented
227 8 my $self = shift;
228 8 my $class = ref($self);
229 8 my $method = "${class} (pet)";
230 8 my $args = shift;
231 8 ▶ croak("$method - Argument required and must be a hash ref") unless(ref($args) && reftype($args) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args and builtin::reftype($args) eq "HASH" 0 8 8
Condition33%1/3
ref $args and builtin::reftype($args) eq "HASH"
!ll&&!rl&&r
008
MC/DC0%0/2
ref $args and builtin::reftype($args) eq "HASH"
ref $args builtin::reftype($args) eq "HASH"
Truth table33%1/3
A and B
A= ref $argsB= builtin::reftype($args) eq "HASH"
ABresult
0X0
100
111
232  
233 # Validate that all of the required parameters are defined
234 8 ▶ croak("$method - YDAY parameter is undefined") unless(defined($args->{YDAY}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"YDAY"} 0 8 8
235 8 ▶ croak("$method - NDAYS parameter is undefined") unless(defined($args->{NDAYS}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"NDAYS"} 0 8 8
236 8 ▶ croak("$method - TEMPERATURE parameter is undefined") unless(defined($args->{TEMPERATURE}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"TEMPERATURE"} 0 8 8
237 8 ▶ croak("$method - TEMPERATURE_UNITS parameter is undefined") unless(defined($args->{TEMPERATURE_UNITS}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"TEMPERATURE_UNITS"} 0 8 8
238  
239 # Validate YDAY
240 8 my $yday = $args->{YDAY};
241 8 ▶ croak("$method - YDAY parameter must be an integer between 1 and 366") unless(looks_like_number($yday) && $yday =~ /^[1-9]\d*$/ && $yday <= 366);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($yday) and $yday =~ /^[1-9]\d*$/ and $yday <= 366 1 7 8
Condition50%3/6
&looks_like_number($yday) and $yday =~ /^[1-9]\d*$/ and $yday <= 366
!ll&&!rl&&r
017
&looks_like_number($yday) and $yday =~ /^[1-9]\d*$/
!ll&&!rl&&r
008
MC/DC33%1/3
&looks_like_number($yday) and $yday =~ /^[1-9]\d*$/ and $yday <= 366
&looks_like_number($yday) $yday =~ /^[1-9]\d*$/ $yday <= 366
Truth table50%2/4
A and B and C
A= &looks_like_number($yday)B= $yday =~ /^[1-9]\d*$/C= $yday <= 366
ABCresult
0XX0
10X0
1100
1111
242  
243 # Validate NDAYS
244 7 my $ndays = $args->{NDAYS};
245 7 ▶ croak("$method - NDAYS parameter must be a positive integer") unless(looks_like_number($ndays) && $ndays =~ /^[1-9]\d*$/);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($ndays) and $ndays =~ /^[1-9]\d*$/ 1 6 7
Condition66%2/3
&looks_like_number($ndays) and $ndays =~ /^[1-9]\d*$/
!ll&&!rl&&r
016
MC/DC50%1/2
&looks_like_number($ndays) and $ndays =~ /^[1-9]\d*$/
&looks_like_number($ndays) $ndays =~ /^[1-9]\d*$/
Truth table66%2/3
A and B
A= &looks_like_number($ndays)B= $ndays =~ /^[1-9]\d*$/
ABresult
0X0
100
111
246  
247 # Validate TEMPERATURE_UNITS
248 6 ▶ croak("$method - TEMPERATURE_UNITS parameter is invalid") unless($args->{TEMPERATURE_UNITS} =~ /^[CFK]$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"TEMPERATURE_UNITS"} =~ /^[CFK]$/ 0 6 6
249  
250 6 my $pet_mm = undef;
251  
252 # --- PDL Mode ---
253  
254 6 ▶ if($self->{PDL_MODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'PDL_MODE'}) { } 1 5 6
255 1 ▶ croak("$method - In PDL mode, but the TEMPERATURE parameter is not a PDL object") unless(blessed($args->{TEMPERATURE}) && $args->{TEMPERATURE}->isa('PDL'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($args->{"TEMPERATURE"}) and $args->{"TEMPERATURE"}->isa("PDL") 0 1 1
Condition33%1/3
builtin::blessed($args->{"TEMPERATURE"}) and $args->{"TEMPERATURE"}->isa("PDL")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($args->{"TEMPERATURE"}) and $args->{"TEMPERATURE"}->isa("PDL")
builtin::blessed($args->{"TEMPERATURE"}) $args->{"TEMPERATURE"}->isa("PDL")
Truth table33%1/3
A and B
A= builtin::blessed($args->{"TEMPERATURE"})B= $args->{"TEMPERATURE"}->isa("PDL")
ABresult
0X0
100
111
256 # Validate temperature PDL dimensions
257 1 my @lat_dims = $self->{LATITUDE}->dims;
258 1 my @arg_dims = $args->{TEMPERATURE}->dims;
259 1 my $array_comp = Array::Compare->new();
260 1 ▶ croak("$method - TEMPERATURE parameter dimensions do not match the object data (e.g., LATITUDE)") unless($array_comp->compare(\@lat_dims,\@arg_dims));
Branch: 1 branch
BranchTrueFalseTotal
unless $array_comp->compare(\@lat_dims, \@arg_dims) 0 1 1
261 # Convert temperature to degC if needed
262 1 my $temp = undef;
263 0 ▶ if($args->{TEMPERATURE_UNITS} eq 'F') { $temp = ($args->{TEMPERATURE} - 32.0) / 1.8; }
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'TEMPERATURE_UNITS'} eq 'F') { } 0 1 1
elsif ($args->{'TEMPERATURE_UNITS'} eq 'K') { } 0 1 1
264 0 elsif($args->{TEMPERATURE_UNITS} eq 'K') { $temp = $args->{TEMPERATURE} - 273.15; }
265 1 else { $temp = $args->{TEMPERATURE}->copy; }
266 # Flip YDAY by half a year for any southern hemisphere latitudes
267 1 my $is_south = $self->{LATITUDE} < 0;
268 1 my $sh_yday = $yday + 182.5;
269 1 ▶ $sh_yday = $sh_yday > 365 ? $sh_yday - 365 : $sh_yday;
Branch: 1 branch
BranchTrueFalseTotal
$sh_yday > 365 ? : 0 1 1
270 1 $sh_yday = int($sh_yday + 0.5);
271 1 my $yday_pdl = $temp->zeros + $yday;
272 1 ▶ if($is_south->any) { $yday_pdl->where($is_south) .= $sh_yday; }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_south->any) 1 0 1
273 # And now with southern hemisphere YDAYs flipped, make all latitudes positive for the calculation
274 1 my $lat = abs($self->{LATITUDE});
275 # Now crunch out the PET!
276 1 my $tei = $self->{TEI}->copy;
277 1 $tei->where($tei <= 0) .= 1; # Avoid div0 errors
278 1 my $lat_rad = ($lat * pi) / 180;
279 1 my $solar_dec = 0.409 * (((2 * pi) / 365) * $yday_pdl - 1.39)->sin;
280 1 my $cos_arg = -1 * $lat_rad->tan * $solar_dec->tan;
281 1 $cos_arg->clip(-1, 1, $cos_arg); # Make sure the results are bounded by [-1, 1]
282 1 my $daylength = (24 / pi) * $cos_arg->acos;
283 1 my $alpha = (6.75e-7) * ($tei**3) - (7.71e-5) * ($tei**2) + 0.01792 * $tei + 0.49239;
284 1 $pet_mm = $temp->zeros;
285 1 my $thornth = ($temp > 0) & ($temp < 26.5) & ($self->{TEI} > 0); # These values will use the Thornthwaite equation
286 1 my $hotadj = ($temp >= 26.5); # These hot values will use the Huang et al. equation
287 1 my $pos_temp = $temp->lclip(1e-20); # Prevents errors with negative temps (and zero)
288 1 my $pet_gross = 16 * ((10 * $pos_temp) / $tei)**$alpha;
289 1 my $pet_nml = $pet_gross * ($daylength / 12) * ($ndays / 30);
290 1 $pet_mm->where($thornth) .= $pet_nml->where($thornth);
291 1 my $pet_hot = (-415.85 + 32.25 * $temp - 0.43 * ($temp**2)) * ($ndays / 30);
292 1 $pet_mm->where($hotadj) .= $pet_hot->where($hotadj);
293 }
294  
295 # --- Scalar Mode ---
296  
297 else {
298 5 ▶ croak("$method - TEMPERATURE parameter must be numeric") unless(looks_like_number($args->{TEMPERATURE}));
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($args->{"TEMPERATURE"}) 0 5 5
299 # Convert temperature to degC if needed
300 5 my $temp = undef;
301 0 ▶ if($args->{TEMPERATURE_UNITS} eq 'F') { $temp = ($args->{TEMPERATURE} - 32.0) / 1.8; }
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'TEMPERATURE_UNITS'} eq 'F') { } 0 5 5
elsif ($args->{'TEMPERATURE_UNITS'} eq 'K') { } 0 5 5
302 0 elsif($args->{TEMPERATURE_UNITS} eq 'K') { $temp = $args->{TEMPERATURE} - 273.15; }
303 5 else { $temp = $args->{TEMPERATURE}; }
304 # Flip YDAY by half a year if latitude is southern hemisphere
305 5 my $lat = $self->{LATITUDE};
306 5 ▶ if($lat < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lat < 0) 0 5 5
307 0 ▶ $yday = $yday + 182.5 > 365 ? $yday + 182.5 - 365 : $yday + 182.5;
Branch: 1 branch
BranchTrueFalseTotal
$yday + 182.5 > 365 ? : 0 0 0
308 0 $yday = int($yday + 0.5);
309 }
310 5 $lat = abs($lat);
311 # Now crunch out the PET!
312 5 my $tei = $self->{TEI};
313 5 ▶ if($temp <= 0 || ($temp < 26.5 && $tei <= 0)) { $pet_mm = 0; } # Frozen ground
Branch: 2 branches
BranchTrueFalseTotal
if ($temp <= 0 or $temp < 26.5 and $tei <= 0) { } 2 3 5
elsif ($temp < 26.5) { } 1 2 3
Condition100%6/6
$temp <= 0 or $temp < "26.5" and $tei <= 0
l!l&&r!l&&!r
113
$temp < "26.5" and $tei <= 0
!ll&&!rl&&r
211
MC/DC100%3/3
$temp <= 0 or $temp < "26.5" and $tei <= 0
$temp <= 0 $temp < "26.5" $tei <= 0
Truth table100%4/4
A or B and C
A= $temp <= 0B= $temp < "26.5"C= $tei <= 0
ABCresult
1XX1
0111
00X0
0100
314 elsif($temp < 26.5) { # These values will use the Thornthwaite equation
315 1 my $lat_rad = ($lat * pi) / 180;
316 1 my $solar_dec = 0.409 * sin(((2 * pi) / 365) * $yday - 1.39);
317 1 my $cos_arg = -tan($lat_rad) * tan($solar_dec);
318 # Make sure the result is bounded by [-1, 1]
319 0 ▶ if($cos_arg > 1) { $cos_arg = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cos_arg > 1) 0 1 1
320 0 ▶ if($cos_arg < -1) { $cos_arg = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cos_arg < -1) 0 1 1
321 1 my $daylength = (24 / pi) * acos($cos_arg);
322 1 my $alpha = (6.75e-7) * ($tei**3) - (7.71e-5) * ($tei**2) + 0.01792 * $tei + 0.49239;
323 1 my $pet_gross = 16 * ((10 * $temp) / $tei)**$alpha;
324 1 $pet_mm = $pet_gross * ($daylength / 12) * ($ndays / 30);
325 }
326 else { # This hot value will use the Huang et al. equation
327 2 $pet_mm = (-415.85 + 32.25 * $temp - 0.43 * ($temp**2)) * ($ndays / 30);
328 }
329 }
330  
331 0 ▶ if($self->{PET_UNITS} eq 'in') { return $pet_mm / 25.4; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'PET_UNITS'} eq 'in') { } 0 6 6
332 6 else { return $pet_mm; }
333 }
334  
335 =head1 SEE ALSO
336  
337 =over 4
338  
339 =item * L<Meteorology::PET>
340  
341 =item * L<Array::Compare>
342  
343 =item * L<PDL>
344  
345 =back
346  
347 =head1 REFERENCES
348  
349 =over 4
350  
351 =item * Thornthwaite, C. W., 1948: An approach toward a rational classification of climate. I<Geographical Review>, 38, 55-94.
352  
353 =item * Palmer, Wayne C., 1965: Meteorological Drought. U.S. Weather Bureau, Research Paper No. 45.
354  
355 =item * Huang, J., H. M. van den Dool, and K. E. Georgakakos, 1996: Analysis of model-calculated soil moisture over the United
356 States (1931-1993) and applications to long-range temperature forecasts. I<J. Climate>, 9, 1350-1362.
357  
358 =back
359  
360 =head1 AUTHOR
361  
362 Adam Allgood, NOAA/NWS/NCEP/Weather Prediction Center
363  
364 =head1 LICENSE
365  
366 As a work of the United States Government, this project is in the
367 public domain within the United States.
368  
369 Additionally, we waive copyright and related rights in the work
370 worldwide through the CC0 1.0 Universal public domain dedication.
371  
372 =head2 CC0 1.0 Universal Summary
373  
374 This is a human-readable summary of the L<Legal Code (read the full text)|https://creativecommons.org/publicdomain/zero/1.0/legalcode>.
375  
376 =head3 No Copyright
377  
378 The person who associated a work with this deed has dedicated the work to
379 the public domain by waiving all of his or her rights to the work worldwide
380 under copyright law, including all related and neighboring rights, to the
381 extent allowed by law.
382  
383 You can copy, modify, distribute and perform the work, even for commercial
384 purposes, all without asking permission.
385  
386 =head3 Other Information
387  
388 In no way are the patent or trademark rights of any person affected by CC0,
389 nor are the rights that other persons may have in the work or in how the
390 work is used, such as publicity or privacy rights.
391  
392 Unless expressly stated otherwise, the person who associated a work with
393 this deed makes no warranties about the work, and disclaims liability for
394 all uses of the work, to the fullest extent permitted by applicable law.
395 When using or citing the work, you should not imply endorsement by the
396 author or the affirmer.
397  
398 =cut
399  
400 5 1;
401  
402 __END__
↑ Summary PET.pm »