Weather-GHCN-Fetch
view release on metacpan or search on metacpan
lib/Weather/GHCN/App/CacheUtil.pm view on Meta::CPAN
}
=head2 get_ghcn ($profile, $cachedir)
Returns a Weather::GHCN::StationTable object initialized with a cache
location obtained from $cachedir or, if $cachdir is undefined, from
the cachedir option defined in the user profile specified by
$profile. If errors are encounterd, it dies and produces a list.
=cut
sub get_ghcn ($profile, $cachedir) {
my $ghcn = Weather::GHCN::StationTable->new;
$profile //= $PROFILE_FILE;
my ($opt, @errors) = $ghcn->set_options(
cachedir => $cachedir,
profile => $profile,
);
die @errors if @errors;
return $ghcn;
}
=head2 get_options ( \@ARGV )
B<get_options> encapsulates everything we need to process command line
options, or to set options when invoking this script from a test script.
Normally it's called by passing a reference to @ARGV; from a test script
you'd set up a local array variable to specify the options.
By convention, you should set up a file-scoped lexical variable named
$Opt and set it in the mainline using the return value from this function.
Then all options can be accessed used $Opt->option notation.
=cut
sub get_options ($argv_aref) {
my @options = (
'country:s', # filter by country
'state|prov:s', # filter by state or province
'location:s', # filter by localtime
'remove', # remove cached daily files (except aliases)
'clean', # remove all files from the cache
'invert|v', # invert -location selection criteria
'size|kb:i', # select files by size in Kb
'age:i', # select file if >= age
'type:s', # select based on type
'cachedir:s', # cache location
'profile:s', # profile file
'outclip', # output data to the Windows clipboard
'help','usage|?', # help
);
my %opt;
# create a list of option key names by stripping the various adornments
my @keys = map { (split m{ [!+=:|] }xms)[0] } grep { !ref } @options;
# initialize all possible options to undef
@opt{ @keys } = ( undef ) x @keys;
GetOptionsFromArray($argv_aref, \%opt, @options)
or pod2usage(2);
# Make %opt into an object and name it the same as what we usually
# call the global options object. Note that this doesn't set the
# global -- the script will have to do that using the return value
# from this function. But, what this does is allow us to call
# $Opt->help and other option within this function using the same
# syntax as what we use in the script. This is handy if you need
# to rename option '-foo' to '-bar' because you can do a find/replace
# on '$Opt->foo' and you'll get any instances of it here as well as
# in the script.
## no critic [Capitalization]
## no critic [ProhibitReusedNames]
my $Opt = _wrap_hash \%opt;
pod2usage(1) if $Opt->usage;
pod2usage(-verbose => 2) if $Opt->help;
return $Opt;
}
=head2 get_alias_stnids ( \%profile )
Read the hash obtained from the user profile file and find the alias
definitions. Return a hash of station id's that have been aliased.
=cut
sub get_alias_stnids ($profile_href) {
return {} if not $profile_href;
my $aliases_href = $profile_href->{aliases};
return {} if not $aliases_href;
my %aliases;
foreach my $stn_str (values $aliases_href->%*) {
my @stns = split $COMMA, $stn_str;
foreach my $stn (@stns) {
$aliases{$stn} = 1;
}
}
return \%aliases;
}
=head2 load_cached_files ($ghcn, $cache_pto, \%alias )
Given a Weather::GHCN::StationTable object and a cache Path::Tiny
object, and a hash of which files correspond to aliased stations,
return a hash which combines the file information and the station
information (where applicable) and categorizes each entry by type:
D for daily data file, A for aliases station, and C for catalog files.
=cut
sub load_cached_files ($ghcn, $cache_pto, $alias_href) {
my @files = $cache_pto->children;
( run in 2.055 seconds using v1.01-cache-2.11-cpan-71847e10f99 )