source: exist/trunk/xquery/moles2mdip.xq @ 4124

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/xquery/moles2mdip.xq@4326
Revision 4124, 11.6 KB checked in by sdonegan, 11 years ago (diff)

Updated xquery handles multiple MOLES spatial and temporal elements as well as empty elements.

Line 
1(: Create an MDIP record for moles documents in /misc/humid1/cbyrom/eclipseWorkspace/ingestAutomation-upgrade2/OAIBatch/data/badc/discovery/moles/ that have badc.nerc.ac.uk and test_file2 :)
2import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'vocab_xquery_lib.xquery';
3import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'utility_xquery_lib.xquery';
4
5declare default element namespace 'http://www.oceannet.org/mdip/xml';
6declare namespace moles='http://ndg.nerc.ac.uk/moles';
7declare namespace gco='http://www.isotc211.org/2005/gco';
8declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
9
10(:declare variable $targetCollection as xs:string := '.';:)
11declare variable $targetCollection as xs:string := 'TargetCollection';
12declare variable $repositoryIdentifier as xs:string := 'RepositoryID';
13declare variable $localIdentifier as xs:string := 'LocalID';
14declare variable $ISO_639-2_ns as xs:string := $voclib:ISO_639-2_ns;
15declare variable $ISO_3166_ns as xs:string := $voclib:ISO_3166_ns;
16
17for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
18    exists('moles:dgDataEntity')
19    and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
20    and moles:dgMetadataID/moles:repositoryIdentifier=$repositoryIdentifier
21    and moles:dgMetadataID/moles:localIdentifier=$localIdentifier]
22return
23element Metadata {
24    attribute xsi:schemaLocation {'http://www.oceannet.org/mdip/xml N:\DATAMA~1\METADA~2\MDIP\MDIP_Metadata_v1_3.xsd'}, (:remove for production :)
25    element Title {data($DE/moles:name)},
26
27if (exists($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDatasetLanguage)) then
28    for $lang in $DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDatasetLanguage
29return
30    element Language{
31        element LanguageName {data($lang/moles:dgValidTerm)},
32        element LanguageVocab {data($lang/moles:dgValidTermID/moles:ParentListID)},
33        element LanguageCode {data($lang/moles:dgValidTermID/moles:TermID)}
34}
35(: put default in to keep schema happy.. :)
36else element Language{
37        element LanguageName {data("English")},
38        element LanguageVocab {data("UKGemini_LanguageCategories")},
39        element LanguageCode {data("eng")}
40},
41
42(:check for abstract text:)
43if (string-length($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)  < 16)  then
44    element Abstract {data("There was either no or insufficient info in MOLES abstract to produce an MDIP abstract!")}
45else
46    element Abstract {string($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)},
47
48(:check for topicCategory stuff in moles :)
49if (count($DE/moles:dgStructuredKeyword[voclib:spot-vocab($voclib:iso_topic_list, moles:dgValidTermID/moles:ParentListID)] ) < 1 ) then
50    (: put padding in to produce valid mdip :)
51     element TopicCategory {
52        element TopicCategoryName {data("geoscientificInformation")},
53        element TopicCategoryVocab {data("ISO_19115:2003_MD_TopicCategoryCodes")},
54        element TopicCategoryCode {data("001")}
55    }
56else
57    for $isoTopic in $DE/moles:dgStructuredKeyword[voclib:spot-vocab($voclib:iso_topic_list, moles:dgValidTermID/moles:ParentListID)]
58    return element TopicCategory {
59        element TopicCategoryName {data($isoTopic/moles:dgValidTerm)},
60        element TopicCategoryVocab {data($isoTopic/moles:dgValidTermID/moles:ParentListID)},
61        element TopicCategoryCode {data($isoTopic/moles:dgValidTermID/moles:TermID)}
62    },
63   
64(: Subject :)
65    (: standard parameter terms :)
66if (count($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgParameterSummary/moles:dgStdParameterMeasured[
67    voclib:spot-vocab($voclib:gcmd_science_valids, moles:dgValidTermID/moles:ParentListID)
68    or voclib:spot-vocab($voclib:gcmd_project_valids, moles:dgValidTermID/moles:ParentListID)
69    or voclib:spot-vocab($voclib:gcmd_science_valids_categories, moles:dgValidTermID/moles:ParentListID)
70    or voclib:spot-vocab($voclib:bodc_parameter_usage_vocab, moles:dgValidTermID/moles:ParentListID)
71    or voclib:spot-vocab($voclib:bodc_parameter_discovery_vocab, moles:dgValidTermID/moles:ParentListID)
72    ]) < 1)  then
73    element Subject {
74        element SubjectName {data("No subject identiified")},
75        element SubjectVocab {data("Other")},
76        element SubjectCode {data("00")}
77}
78else
79    for $subject in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgParameterSummary/moles:dgStdParameterMeasured[
80    voclib:spot-vocab($voclib:gcmd_science_valids, moles:dgValidTermID/moles:ParentListID)
81    or voclib:spot-vocab($voclib:gcmd_project_valids, moles:dgValidTermID/moles:ParentListID)
82    or voclib:spot-vocab($voclib:gcmd_science_valids_categories, moles:dgValidTermID/moles:ParentListID)
83    or voclib:spot-vocab($voclib:bodc_parameter_usage_vocab, moles:dgValidTermID/moles:ParentListID)
84    or voclib:spot-vocab($voclib:bodc_parameter_discovery_vocab, moles:dgValidTermID/moles:ParentListID)
85    ])
86    return (element Subject {
87        element SubjectName {data($subject/moles:dgValidTerm)},
88        element SubjectVocab {data($subject/moles:dgValidTermID/moles:ParentListID)},
89        element SubjectCode {data($subject/moles:dgValidTermID/moles:TermID)}
90},
91
92    (: standard parameter SUB terms :)
93for $subterm in $subject//moles:dgValidSubterm
94return (element Subject {
95    element SubjectName {data($subterm/moles:dgValidTerm)},
96    element SubjectVocab {data($subterm/moles:dgValidTermID/moles:ParentListID)},
97    element SubjectCode {data($subterm/moles:dgValidTermID/moles:TermID)}
98    }
99)
100),
101
102(:Date :)
103
104(: check empty stuff :)
105
106if (exists($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange))) then
107
108    (:Note MOLES can have any number of dateRanges but MDIP only 1.. :)
109    if (count($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange)) = 1) then
110        element Date {
111       
112             if ($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange/moles:DateRangeStart) != '') then
113                element DatasetStartDate {
114                    let $dateStart:=min($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange/moles:DateRangeStart) cast as xs:date)
115                    return
116                        if (empty($dateStart)) then ()
117                            else data($dateStart)
118                           
119                 }
120             else element DatasetStartDate {data('')},
121             
122            if ($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange/moles:DateRangeEnd) != '') then
123                element DatasetEndDate{
124                  let $dateEnd:=max($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange/moles:DateRangeEnd) cast as xs:date)
125                  return
126                    if (empty($dateEnd)) then ()
127                        else data($dateEnd)
128                }
129            else element DatasetEndDate {data('')}
130    }
131   
132    else
133 
134    (: take first start date element and last end date element from the moles to bound :)
135        element Date {
136       
137         if ($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange[1]/moles:DateRangeStart) != '') then
138            element DatasetStartDate {
139                    let $dateStart:=$DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange[1]/moles:DateRangeStart) cast as xs:date
140                    return
141                        if (empty($dateStart)) then ()
142                            else data($dateStart)
143                }
144             else element DatasetStartDate {data('')},
145             
146             if ($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange[count($DE//moles:dgTemporalCoverage/moles:DateRange)]/moles:DateRangeEnd) != '') then
147                element DatasetEndDate {
148                    let $dateEnd:=$DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange[count($DE//moles:dgTemporalCoverage/moles:DateRange)]/moles:DateRangeEnd) cast as xs:date
149                    return
150                        if (empty($dateEnd)) then ()
151                            else data($dateEnd)
152                }
153                  else element DatasetEndDate {data('')}                               
154            (:
155            element DatasetStartDate {data($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange[1]/moles:DateRangeStart))},
156            element DatasetEndDate {data($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange[count($DE//moles:dgTemporalCoverage/moles:DateRange)]/moles:DateRangeEnd))}
157            :)
158        }
159else (), (: if no date not a lot can do about it :)
160
161(:Coordinates, boundingBoxes etc :)
162(: MOLES, dif can have any number of boundingBoxes but MDIP only 1.. so check how many and produce an agregate box -wow -XPATH2 works here!:)
163if ( (count($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox) ) > 1)  then
164    let $minLon := min(for $bbox in ($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox) return $bbox/moles:LimitWest) return element WestCoOrdinate {data ($minLon) }
165else
166    element WestCoOrdinate {data($DE/moles:dgDataEntity/moles:dgDataSummary/moles:BoundingBox/moles:LimitWest)},
167   
168if ( (count($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox) ) > 1)  then
169    let $maxLon := max(for $bbox in ($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox) return $bbox/moles:LimitEast) return element EastCoOrdinate {data ($maxLon) }
170else
171    element EastCoOrdinate {data($DE/moles:dgDataEntity/moles:dgDataSummary/moles:BoundingBox/moles:LimitEast)},
172   
173if ( (count($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox) ) > 1)  then
174    let $maxLat := max(for $bbox in ($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox) return $bbox/moles:LimitNorth) return element NorthCoOrdinate {data ($maxLat) }
175else
176    element NorthCoOrdinate {data($DE/moles:dgDataEntity/moles:dgDataSummary/moles:BoundingBox/moles:LimitNorth)},
177   
178if ( (count($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox) ) > 1)  then
179    let $minLat := min(for $bbox in ($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox) return $bbox/moles:LimitSouth) return element SouthCoOrdinate {data ($minLat) }
180else
181    element SouthCoOrdinate {data($DE/moles:dgDataEntity/moles:dgDataSummary/moles:BoundingBox/moles:LimitSouth)},
182   
183
184(:
185let $west := min($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitWest cast as xs:decimal)
186return element WestCoOrdinate {data($west)},
187let $east := max($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitEast cast as xs:decimal)
188return element EastCoOrdinate {data($east)},
189let $north := max($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitNorth cast as xs:decimal)
190return element NorthCoOrdinate {data($north)},
191let $south := min($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitSouth cast as xs:decimal)
192return element SouthCoOrdinate {data($south)}
193:)
194(:metadata language - hardcode this.. :)
195element MetadataLanguage {
196    element LanguageName {"English"},
197    element LanguageVocab {"UKGemini_LanguageCategories"},
198    element LanguageCode {"eng"}
199},
200
201(:DatasetIdentifier:)
202element DatasetIdentifier {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, $utillib:moles_id_separator, 'MDIP', $utillib:moles_id_separator, $DE/moles:dgMetadataID/moles:localIdentifier)},
203
204(:Target:)
205for $target in $DE/moles:dgStructuredKeyword[voclib:spot-vocab($voclib:ndg_data_provider_vocab, moles:dgValidTermID/moles:ParentListID) ]
206return element Target {
207element TargetName {data($target/moles:dgValidTerm)},
208element TargetVocab {data($target/moles:dgValidTermID/moles:ParentListID)},
209element TargetCode {data($target/moles:dgValidTermID/moles:TermID)}
210}
211
212}
Note: See TracBrowser for help on using the repository browser.