77import requests
88from elasticsearch import Elasticsearch
99
10+
1011def get_uuid (path ):
1112
12- cli = Elasticsearch (hosts = [' https://elasticsearch.ceda.ac.uk' ])
13+ cli = Elasticsearch (hosts = [" https://elasticsearch.ceda.ac.uk" ])
1314 opensearch_resp = cli .search (
14- index = ' opensearch-files' ,
15- body = {
15+ index = " opensearch-files" ,
16+ body = {
1617 "query" : {
1718 "bool" : {
1819 "must" : [
19- {
20- "prefix" : {
21- "info.directory" : path
22- }
23- },
24- {
25- "exists" : {
26- "field" : "projects.opensearch.datasetId"
27- }
28- }
20+ {"prefix" : {"info.directory" : path }},
21+ {"exists" : {"field" : "projects.opensearch.datasetId" }},
2922 ]
3023 }
3124 }
32- }
25+ },
26+ )
27+ return opensearch_resp ["hits" ]["hits" ][0 ]["_source" ]["projects" ]["opensearch" ].get (
28+ "datasetId"
3329 )
34- return opensearch_resp [ 'hits' ][ 'hits' ][ 0 ][ '_source' ][ 'projects' ][ 'opensearch' ]. get ( 'datasetId' )
30+
3531
3632def get_moles (uuid ):
3733
38- resp = requests .get (f'https://catalogue.ceda.ac.uk/api/v2/observations.json?uuid={ uuid } ' )
34+ resp = requests .get (
35+ f"https://catalogue.ceda.ac.uk/api/v2/observations.json?uuid={ uuid } "
36+ )
3937 try :
40- moles_resp = resp .json ()[' results' ][0 ]
38+ moles_resp = resp .json ()[" results" ][0 ]
4139 except IndexError :
4240 moles_resp = {}
4341
4442 return moles_resp
4543
44+
4645def get_opensearch_hit (uuid ) -> dict :
4746 """
4847 Query elasticsearch for the opensearch collections metadata
4948 """
50- cli = Elasticsearch (hosts = [' https://elasticsearch.ceda.ac.uk' ])
49+ cli = Elasticsearch (hosts = [" https://elasticsearch.ceda.ac.uk" ])
5150
5251 opensearch_resp = cli .search (
53- index = 'opensearch-collections' ,
54- body = {
55- "query" :{
56- "bool" : {
57- "must" :[
58- {
59- "match" :{
60- "collection_id" :uuid
61- }
62- }
63- ]
64- }
65- }
66- })
67-
52+ index = "opensearch-collections" ,
53+ body = {"query" : {"bool" : {"must" : [{"match" : {"collection_id" : uuid }}]}}},
54+ )
55+
6856 try :
69- opensearch_hit = opensearch_resp [' hits' ][ ' hits' ][0 ]
57+ opensearch_hit = opensearch_resp [" hits" ][ " hits" ][0 ]
7058 except IndexError or KeyError :
7159 opensearch_hit = {}
72-
73- return opensearch_hit .get ('_source' ,{})
60+
61+ return opensearch_hit .get ("_source" , {})
62+
7463
7564# Parse command line arguments using click
7665@click .command ()
77- @click .argument ('drs' )
78- @click .option ('--path' , required = False )
79-
80- @click .option ('--uuid' , required = False )
81- @click .option ('--formats' , required = False )
66+ @click .argument ("drs" )
67+ @click .option ("--path" , required = False )
68+ @click .option ("--uuid" , required = False )
69+ @click .option ("--formats" , required = False )
8270def main (drs , path = None , uuid = None , formats = None ):
8371
84- STAC_API = ' https://api.stac.164.30.69.113.nip.io'
85- DEFAULTS = [' openeo' ]
72+ STAC_API = " https://api.stac.164.30.69.113.nip.io"
73+ DEFAULTS = [" openeo" ]
8674
8775 if path is not None :
8876 uuid = get_uuid (path )
8977
9078 title = drs
9179 drs = drs .lower ()
92- if ' .openeo' not in drs :
93- drs = drs + ' .openeo'
80+ if " .openeo" not in drs :
81+ drs = drs + " .openeo"
9482
9583 metadata = get_moles (uuid )
9684 opensearch_hit = get_opensearch_hit (uuid )
9785
98- with open (' stac_collections/openeo_collection_template.json' ) as f :
99- template = '' .join ([r .strip () for r in f .readlines ()])
86+ with open (" stac_collections/openeo_collection_template.json" ) as f :
87+ template = "" .join ([r .strip () for r in f .readlines ()])
10088
101- desc = str (metadata [' abstract' ])
102- with open (' desc.txt' , 'w' ) as f :
103- f .write (repr (desc ))
89+ desc = str (metadata [" abstract" ])
90+ with open (" desc.txt" , "w" ) as f :
91+ f .write (repr (desc ))
10492
105- template = template .replace (' STAC_API' , STAC_API )
106- template = template .replace (' SELF' , drs )
107- template = template .replace (' TITLE' , title )
108- template = template .replace (' UUID' , uuid )
93+ template = template .replace (" STAC_API" , STAC_API )
94+ template = template .replace (" SELF" , drs )
95+ template = template .replace (" TITLE" , title )
96+ template = template .replace (" UUID" , uuid )
10997 try :
11098 template_json = json .loads (template )
11199 except :
112- raise ValueError (' Failed JSON serialisation' )
100+ raise ValueError (" Failed JSON serialisation" )
113101
114102 defaults = DEFAULTS
115103 if formats is not None :
116- defaults += formats .split (',' )
117-
118- keywords = defaults + drs .split ('.' ) + [k .strip () for k in metadata .get ('keywords' ,None ).split (',' )]
119- template_json ['keywords' ] = list (set (keywords )) + [uuid ]
120- template_json ['summaries' ] = None
121- template_json ['description' ] = desc
122- template_json ['providers' ] = [
104+ defaults += formats .split ("," )
105+
106+ keywords = (
107+ defaults
108+ + drs .split ("." )
109+ + [k .strip () for k in metadata .get ("keywords" , None ).split ("," )]
110+ )
111+ template_json ["keywords" ] = list (set (keywords )) + [uuid ]
112+ template_json ["summaries" ] = None
113+ template_json ["description" ] = desc
114+ template_json ["providers" ] = [
123115 {
124- ' roles' : ["host" ],
125- ' name' : ' Centre for Environmental Data Analysis (CEDA)' ,
126- ' url' : ' https://catalogue.ceda.ac.uk'
116+ " roles" : ["host" ],
117+ " name" : " Centre for Environmental Data Analysis (CEDA)" ,
118+ " url" : " https://catalogue.ceda.ac.uk" ,
127119 }
128120 ]
129121
130- start_datetime = opensearch_hit .get (' start_date' , ' 2025-09-19T00:00:00Z' )
131- end_datetime = opensearch_hit .get (' end_date' , ' 2025-09-19T00:00:00Z' )
122+ start_datetime = opensearch_hit .get (" start_date" , " 2025-09-19T00:00:00Z" )
123+ end_datetime = opensearch_hit .get (" end_date" , " 2025-09-19T00:00:00Z" )
132124
133125 bbox = [[- 180 , - 90 , 180 , 90 ]]
134126
135- template_json [' extent' ] = {
127+ template_json [" extent" ] = {
136128 "spatial" : {
137129 "bbox" : bbox ,
138130 },
139- "temporal" : {
140- "interval" : [
141- [
142- start_datetime ,
143- end_datetime
144- ]
145- ]
146- }
131+ "temporal" : {"interval" : [[start_datetime , end_datetime ]]},
147132 }
148133
149134 # Fill in template
150135
151- with open (f' openeo/collections/{ drs } .json' , 'w' ) as f :
136+ with open (f" openeo/collections/{ drs } .json" , "w" ) as f :
152137 f .write (json .dumps (template_json ))
153138
154- if __name__ == '__main__' :
155- main ()
139+
140+ if __name__ == "__main__" :
141+ main ()
0 commit comments