-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
98 lines (80 loc) · 4.46 KB
/
app.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
from flask import Flask, request, jsonify
app = Flask(__name__)
from get_model_data import extract_annual_data_UM, extract_ts_data_cmip
#########################################################################################
# BRIDGE annual mean climatology
#########################################################################################
@app.route('/get_mean_data_bridge', methods=['POST'])
def get_mean_data_bridge():
data = request.json
# check that the request contains the expected keys
if not all(key in data for key in ['model_ids', 'locations', 'variable']):
return jsonify({'error': 'Missing data. Expected keys are: model_ids, locations and variable'}), 400
model_ids = data['model_ids']
locations = data['locations']
variable = data['variable']
# check for correct API call
# validate model_ids and locations
if not isinstance(model_ids, list) or not all(isinstance(id, str) for id in model_ids):
return jsonify({'error': 'model_ids should be a list of strings'}), 400
if not isinstance(locations, list) or not all(isinstance(loc, (list, tuple)) and len(loc) == 2 for loc in locations):
return jsonify({'error': 'locations should be a list of tuples'}), 400
# check if the length of model_ids and locations is the same
if len(model_ids) != len(locations):
return jsonify({'error': 'model_ids and locations must have the same length'}), 400
# validate variable: should be a string
if not isinstance(variable, str):
return jsonify({'error': 'variable should be a string'}), 400
# validate that known variable was requested
if variable not in ['tas', 'pr']:
return jsonify({'error': f'Unknown variable: {variable}. Currently supported variables are: tas and pr'}), 400
# try to extract the data
try:
results = extract_annual_data_UM(model_ids, locations, variable)
return jsonify(results)
except ValueError as ve:
return jsonify({'error': str(ve)}), 400 # Bad request
except RuntimeError as re:
return jsonify({'error': str(re)}), 500 # Internal server error
#########################################################################################
# CMIP6 monthly or annual mean scenario timeseries
#########################################################################################
@app.route('/get_ts_data_cmip', methods=['POST'])
def get_ts_data_cmip():
data = request.json
# check that the request contains the expected keys
if not all(key in data for key in ['model_id', 'location', 'variable', 'frequency']):
return jsonify({'error': 'Missing data. Expected keys are: model_id, location, variable and frequency/'}), 400
model_id = data['model_id']
location = data['location']
variable = data['variable']
frequency = data['frequency']
# check for correct API call
# validate model_ids, locations and variable
if not isinstance(model_id, str):
return jsonify({'error': 'model_id should be a single string'}), 400
if not isinstance(location, list) or not len(location) == 2:
return jsonify({'error': 'location should be a list of length 2'}), 400
if not isinstance(variable, str):
return jsonify({'error': 'variable should be a string'}), 400
if not isinstance(frequency, str):
return jsonify({'error': 'frequency should be a string'}), 400
# validate that known model ID was requested
if model_id not in ['ssp126', 'ssp245', 'ssp370', 'ssp585', 'PI']:
return jsonify({'error': f'Unknown model_id: {model_id}. Currently supported model_ids are: ssp126, ssp245, ssp370 and ssp585'}), 400
# validate that known variable was requested
if variable not in ['tas', 'pr']:
return jsonify({'error': f'Unknown variable: {variable}. Currently supported variables are: tas and pr'}), 400
# validate that known time frequency was requested
if frequency not in ['mm', 'ym']:
return jsonify({'error': f'Unknown time frequency: {frequency}. Currently supported samplings are: mm and ym'}), 400
# try to extract the data
try:
results = extract_ts_data_cmip(model_id, location, variable, frequency)
return jsonify(results)
except ValueError as ve:
return jsonify({'error': str(ve)}), 400 # Bad request
except RuntimeError as re:
return jsonify({'error': str(re)}), 500 # Internal server error
if __name__ == '__main__':
app.run(host='0.0.0.0', port=4000, debug=False)