This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_metadata.py
100 lines (81 loc) · 2.79 KB
/
get_metadata.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 4 13:47:42 2022
@author: erlend
"""
""" Idea: Download metadata on the form (dictionary)
- {"Ås (NMBU)": {
"shortname": "Ås (NMBU)",
"id": "SN17850",
"name": "ÅS",
"maxResolution": "PT1H", #"PT10M",
"lat": "59.6605",
"lon": "10.7818"}}
for given
- bbox coordinates (see other scripts) (f.eks hele sørnorge)
- tilgjengelig måling, 1m eller 1 h osv.
"""
import requests
import pickle
import datetime
# get client ID
with open('/home/erlend/frost_id', 'r') as file:
clientID = file.read().rstrip('\n')
def get_available_gauges_in_area(geometry=None, elements=None,
validtime = None, country = None):
url = "https://frost.met.no/sources/v0.jsonld"
headers = {"Accept": "application/json", "Authorization": f"Basic {clientID}"}
params = {
"types": "SensorSystem",
"geometry": geometry,
"elements": elements,
"validtime": validtime,
"country": country,
}
r = requests.get(url, params, headers=headers, auth=(clientID, ""))
return r.json()
# Geometry specifications:
# https://frost.met.no/concepts2.html#geometryspecification
# Get available stations in box:
# http://bboxfinder.com/#59.570506,10.517349,59.790616,10.962982
bbox = [2.856445, 57.727619, 13.579102, 64.377941] #norge sør for Steinkjær
country = 'Norge' # set to none to include ie sweden
# Type element
elements = "sum(precipitation_amount PT1H)" #timesnedbør
#elements = "sum(precipitation_amount PT1M)" #minuttnedbør
# Valid timeinterval
#start = '2018-08-07'
#end = '2018-08-09'
start = '2018-06-07'
end = '2019-09-09'
if (start is not None) and (end is not None):
validtime = f"{start}/{end}"
elif (end is None) and (start is not None):
validtime = f"{start}"
else: # all times included
validtime = None
# bbox to polygon
a = str(bbox[0]) + " " + str(bbox[1])
b = str(bbox[2]) + " " + str(bbox[1])
c = str(bbox[2]) + " " + str(bbox[3])
d = str(bbox[0]) + " " + str(bbox[3])
geomet = "POLYGON((" + a + ", " + b + ", " + c + ", " + d + "))"
s = get_available_gauges_in_area(geometry=geomet,
elements = elements,
validtime = validtime,
country = country,
)
metadata = {}
for station in s['data']:
metadata[station['name']] = {
"shortname": station['shortName'],
"id": station['id'],
"name": station['name'],
"resolution": str(elements),
"lat": station['geometry']['coordinates'][1],
"lon": station['geometry']['coordinates'][0]
}
a_file = open("./stations/meta_data.pkl", "wb")
pickle.dump(metadata, a_file)
a_file.close()