-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication.py
More file actions
210 lines (174 loc) · 6.95 KB
/
application.py
File metadata and controls
210 lines (174 loc) · 6.95 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from datetime import datetime
import logging
import sys
from flask import Flask, render_template, send_from_directory
from flask_cors import CORS
from config import SITE_OFFLINE, geojson_names
from marshmallow import Schema, fields, validate, ValidationError
import re
import pyproj
from luts import (
fire_weather_ops,
all_cmip6_downscaled_models,
all_cmip6_downscaled_scenarios,
)
from routes import routes, request
# Configure logging to emit to stdout
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
# Elastic Beanstalk wants `application` to be present.
application = app = Flask(__name__)
CORS(app)
app.register_blueprint(routes)
# Disable PROJ network to fix intermittent bugs converting between EPSGs.
# This will force pyproj to use its local database instead.
pyproj.network.set_network_enabled(False)
def get_service_categories():
"""
This is the location for the service_categories on the main page.
This will function will be called on the default route and the list
will be passed to the index.html template.
"""
return [
("CMIP6", "/cmip6"),
("CMIP6, Downscaled", "/cmip6_downscaled"),
("Climate Indicators", "/indicators"),
("Climate Protection from Spruce Beetles", "/beetles"),
("Degree Days", "/degree_days"),
("Digital Elevation Models (DEMs)", "/elevation"),
("Flammability and Vegetation Type (ALFRESCO)", "/alfresco"),
("Hydrology", "/hydrology"),
("Landfast Sea Ice", "/landfastice"),
("Permafrost", "/permafrost"),
# ("Physical and Administrative Boundary Polygons", "/boundary"),
# ("Ecoregions", "/ecoregions"),
("Sea Ice Concentration", "/seaice"),
("Snowfall Equivalent", "/snow"),
("Temperature Anomalies", "/temperature_anomalies"),
("Temperature and Precipitation", "/taspr"),
("Wet Days Per Year", "/wet_days_per_year"),
("Wildfire", "/fire"),
("WRF Dynamically Downscaled ERA5 Reanalysis", "/era5wrf"),
("Demographics", "/demographics"),
("CONUS Hydrology", "/conus_hydrology"),
("CMIP6 Fire Weather Indices", "/fire_weather"),
("Arctic Hydrology", "/arctic_hydrology"),
]
def get_geospatial_categories():
"""
This is the location for the geospatial_categories on the main page.
This will function will be called on the default route and the list
will be passed to the index.html template.
"""
return [
("Communities, Places, and Areas of Interest", "/places"),
("GeoJSON Polygon Data", "/boundary"),
# ("Ecoregions", "/ecoregions"),
]
@app.context_processor
def inject_date():
"""
Inject date so it can be used in the footer easily.
"""
year = datetime.now().year
return dict(year=year)
@app.before_request
def validate_get_params():
class QueryParamsSchema(Schema):
format = fields.Str(validate=validate.OneOf(["csv"]), required=False)
summarize = fields.Str(validate=validate.OneOf(["mmm"]), required=False)
# Make sure "community" parameter is only uppercase letters and
# numbers, and less than or equal to 10 characters long.
community = fields.Str(
validate=lambda str: bool(re.match(r"^[A-Z0-9]{0,10}$", str)),
required=False,
)
# Make sure "tags" parameter is only letters and commas, and less than
# or equal to 50 characters long.
tags = fields.Str(
validate=lambda str: bool(re.match(r"^[A-Za-z,]{0,50}$", str)),
required=False,
)
# Make sure "extent" parameter is one of the predefined extents
extent = fields.Str(
validate=validate.OneOf(geojson_names),
required=False,
)
# Make sure "substring" parameter is less than or equal to 50 characters long, allow all UTF-8 characteres
substring = fields.Str(
validate=lambda str: len(str) <= 50,
required=False,
)
def validate_vars(value):
"""
Validate a comma-separated list of variable names.
The regex is constructed such that it validates that the input string:
- Is between 1 and 200 characters long.
- Each variable name is alphanumeric, and may include underscores
Args:
value (str): raw `vars` query parameter to validate
Returns:
bool: True if validation succeeds.
Raises: ValidationError: when `value` not a valid vars string
"""
# 200 is arbitrary, but endpoints (e.g., era5wrf) have many vars
climate_var_regex = re.compile(r"^(?=.{1,200}$)[A-Za-z0-9,_]+$")
if not climate_var_regex.match(value):
raise ValidationError("Invalid var(s) provided.")
return True
vars = fields.Str(
validate=validate_vars,
required=False,
)
# Make sure "models" parameter contains only valid model names.
def validate_models(value):
items = value.split(",")
if not all(item in all_cmip6_downscaled_models for item in items):
raise ValidationError("Invalid model(s) provided.")
return True
models = fields.Str(
validate=validate_models,
required=False,
)
# Make sure "scenarios" parameter contains only valid scenario names.
def validate_scenarios(value):
items = value.split(",")
if not all(item in all_cmip6_downscaled_scenarios for item in items):
raise ValidationError("Invalid scenario(s) provided.")
return True
scenarios = fields.Str(
validate=validate_scenarios,
required=False,
)
# Make sure "op" parameter is one of the predefined fire weather operations
op = fields.Str(
validate=validate.OneOf(fire_weather_ops),
required=False,
)
schema = QueryParamsSchema()
errors = schema.validate(request.args)
if errors:
return render_template("422/invalid_get_parameter.html"), 422
@app.after_request
def add_cache_control(response):
# Set cache control headers here
response.cache_control.max_age = 7776000
return response
@app.route("/")
def index():
"""Render index page"""
# Sort the service categories by category name
service_categories = sorted(get_service_categories(), key=lambda x: x[0])
geospatial_categories = sorted(get_geospatial_categories(), key=lambda x: x[0])
return render_template(
"index.html",
service_categories=service_categories,
geospatial_categories=geospatial_categories,
SITE_OFFLINE=SITE_OFFLINE,
)
@app.route("/robots.txt")
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])