-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIso.pm
More file actions
156 lines (133 loc) · 4.49 KB
/
Iso.pm
File metadata and controls
156 lines (133 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package Iso;
use v5.18.2;
use Mojo::UserAgent;
use Data::Dumper;
my $m_id = 'gmd\:identificationInfo > gmd\:MD_DataIdentification > ';
my $m_di = 'gmd\:distributionInfo > gmd\:MD_Distribution >
gmd\:distributor > gmd\:MD_Distributor > ';
my $m_cs = 'gco\:CharacterString';
my $m_ci = $m_id.'gmd\:citation > gmd\:CI_Citation > ';
my $m_ex = $m_id.'gmd\:extent > gmd\:EX_Extent > ';
my $m_gx = $m_ex.'gmd\:geographicElement > gmd\:EX_GeographicBoundingBox > ';
my $m_dc = 'gco\:Decimal';
my $m_tx = $m_ex.'gmd\:temporalElement > gmd\:EX_TemporalExtent >
gmd\:extent > gml\:TimePeriod > ';
my $m_ai = 'gmi\:acquisitionInformation > gmi\:MI_AcquisitionInformation > ';
my $m_hi = 'gmi\:identifier > gmd\:MD_Identifier >%
gmd\:code > '.$m_cs.' >% gmd\:description > '.$m_cs;
my $map_iso = {
_id => $m_ci.'gmd\:identifier >@ gmd\:MD_Identifier >%
gmd\:description > '.$m_cs.' >%
gmd\:code > '.$m_cs,
native_id => 'gmd\:fileIdentifier > '.$m_cs,
name => $m_ci.'gmd\:title > '.$m_cs,
description => $m_id.'gmd\:abstract > '.$m_cs,
_url1 => $m_ci.'gmd\:citedResponsibleParty >
gmd\:CI_ResponsibleParty > gmd\:contactInfo >
gmd\:CI_Contact > gmd\:onlineResource >
gmd\:CI_OnlineResource > gmd\:linkage > gmd\:URL',
_url2 => $m_di.'gmd\:distributorTransferOptions >
gmd\:MD_DigitalTransferOptions > gmd\:onLine >
gmd\:CI_OnlineResource > gmd\:linkage > gmd\:URL',
lat_min => $m_gx.'gmd\:southBoundLatitude > '.$m_dc,
lat_max => $m_gx.'gmd\:northBoundLatitude > '.$m_dc,
lon_min => $m_gx.'gmd\:westBoundLongitude > '.$m_dc,
lon_max => $m_gx.'gmd\:eastBoundLongitude > '.$m_dc,
start_time => $m_tx.'gml\:beginPosition',
end_time => $m_tx.'gml\:endPosition',
release_dt => $m_ci.'gmd\:date > gmd\:CI_Date > gmd\:date > gco\:Date',
_poc_org => $m_id.'gmd\:pointOfContact > gmd\:CI_ResponsibleParty >
gmd\:organisationName > '.$m_cs,
_instrument => $m_ai.'gmi\:instrument >@ gmi\:MI_Instrument > '.$m_hi,
_platform => $m_ai.'gmi\:platform >@ gmi\:MI_Platform > '.$m_hi,
_instrument_eos => $m_ai.'gmi\:instrument >@ eos\:EOS_Instrument > '.$m_hi,
_platform_eos => $m_ai.'gmi\:platform >@ eos\:EOS_Platform > '.$m_hi,
};
sub new {
my $class = shift;
my $id = shift;
my $ua = Mojo::UserAgent->new;
$ua->max_redirects(3);
my $u = "http://reverb.echo.nasa.gov/reverb/query.iso19115?catalog_id=".$id."&id=".$id;
my $s;
$s->{id} = $id;
$s->{dom} = $ua->get($u)->res->dom or do {
say " error - getting iso metadata for : $id";
return undef;
};
bless $s, $class;
return $s;
};
sub set_dom {
my $class = shift;
my $dom = shift;
my $id = shift;
my $s;
$s->{id} = $id;
$s->{dom} = $dom;
bless $s, $class;
return $s;
};
sub get_meta {
my $s = shift;
my %m;
for (keys %$map_iso) {
my $f = $map_iso->{$_};
if ($f =~ / +>@ +/) {
my $a = $s->_get_array($s->{dom}, $f) or next;
$m{$_} = $a;
next;
}
if ($f =~ / +>% +/) {
my $h = $s->_get_hash($s->{dom}, $f) or next;
$m{$_} = $h;
next;
}
my $v = $s->{dom}->at($f) or next;
next unless $v->text;
$m{$_} = $v->text;
}
$m{_identifier} = $s->{id};
if ($m{_url1}) {
$m{url} = $m{_url1};
} elsif ($m{_url2}) {
$m{url} = $m{_url2};
}
return \%m;
}
sub _get_array {
my $s = shift;
my $d = shift;
my $f = shift;
my ($a, $l) = split ' >@ ', $f;
my @v = $d->find($a)->each or return undef;
if ($l =~ / +>% +/) {
my %z;
for (@v) {
my $h = $s->_get_hash($_, $l) or next;
$z{$_} = $h->{$_} for keys %$h;;
}
return %z ? \%z : undef;
}
my @z;
for (@v) {
my $i = $_->at($l) or next;
push @z, $i->text or next;
}
return @z > 0 ? \@z : undef;
}
sub _get_hash {
my $s = shift;
my $d = shift;
my $f = shift;
my ($a, @l) = split ' >% ', $f;
return undef unless @l == 2;
my $b = "$a > $l[0]";
my $t = $d->at($b) or return undef;
my $k = $t->text or return undef;
my %h = ($k => undef);
$b = "$a > $l[1]";
$t = $d->at($b) or return \%h;
$h{$k} = $t->text;
return \%h;
}