Skip to content

Commit

Permalink
update wsgi for better time and error handling
Browse files Browse the repository at this point in the history
update to code
  • Loading branch information
Louis-Philippe Rousseau Lambert committed Feb 20, 2024
1 parent c5ee75a commit 7203ce5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Vcs-Git: https://github.com/ECCC-CCCS/geomet-climate.git

Package: geomet-climate
Architecture: all
Depends: ${python3:Depends}, mapserver-bin, python3-all, python3-click, python3-gdal, python3-mappyfile, python3-mapscript, python3-matplotlib, python3-numpy, python3-pyproj, python3-yaml, proj-bin, proj-data, ${misc:Depends}
Depends: ${python3:Depends}, mapserver-bin, python3-all, python3-click, python3-dateutil, python3-gdal, python3-mappyfile, python3-mapscript, python3-matplotlib, python3-numpy, python3-pyproj, python3-yaml, proj-bin, proj-data, ${misc:Depends}
Suggests: gdal-bin
Homepage: https://github.com/ECCC-CCCS/geomet-climate
Description: MSC GeoMet climate services
Expand Down
81 changes: 53 additions & 28 deletions geomet_climate/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def get_custom_service_exception(code, locator, text):
xsi:schemaLocation="http://www.opengis.net/ogc
http://schemas.opengis.net/wms/1.3.0/exceptions_1_3_0.xsd">
<ogc:ServiceException code="{code}" locator="{locator}">{text}</ogc:ServiceException>
</ogc:ServiceExceptionReport>'''.format(code=code, locator=locator, text=text), encoding='utf-8')
</ogc:ServiceExceptionReport>'''.format(code=code, locator=locator, text=text), encoding='utf-8') # noqa


def application(env, start_response):
Expand Down Expand Up @@ -215,52 +215,77 @@ def application(env, start_response):
else:
LOGGER.debug('Loading mapfile: {}'.format(mapfile_))
mapfile = mapscript.mapObj(mapfile_)

layerobj = mapfile.getLayerByName(layer)
if request_ == 'GetCapabilities' and lang == 'fr':
metadata_lang(mapfile, lang)
layerobj = mapfile.getLayerByName(layer)
layerobj.setMetaData('ows_title',
layerobj.getMetaData('ows_title_{}'.format(lang))) # noqa
layerobj.setMetaData('ows_layer_group',
layerobj.getMetaData('ows_layer_group_{}'.format(lang))) # noqa

if time_:
if time_ and 'ows_timeextent' in layerobj.metadata.keys():
try:
layerobj = mapfile.getLayerByName(layer)

dates = []
timeextent = layerobj.getMetaData('ows_timeextent')

start_date, end_date, duration = timeextent.split('/')
start_date = isoparse(start_date)
end_date = isoparse(end_date)
time_ = isoparse(time_)
time_iso = isoparse(time_)

end_year_month = (end_date.year - start_date.year)*12
end_month = end_date.month - start_date.month
end_date = end_year_month + end_month

if duration == 'P1Y':
dates = [f'{(start_date + relativedelta(months=+i)).year}'
for i in range(0, (end_date.year - start_date.year)*12
+ end_date.month - start_date.month + 1,
12)]
time_upt = f'{time_.year}'
else:
dates = [start_date + relativedelta(months=+i)
for i in range(0, (end_date.year - start_date.year)*12
+ end_date.month - start_date.month + 1,
1)]
time_upt = f'{time_.year}-{time_.month}'

print(time_)
print(time_upt)
print(dates[0])
if time_upt in dates:
print(env['QUERY_STRING'])
if time_ != time_iso.strftime('%Y'):
time_error = 'Format de temps invalide, ' \
'format attendu : YYYY / ' \
'Invalid time format, ' \
'expected format: YYYY'
response = get_custom_service_exception('InvalidDimensionValue', # noqa
'time',
time_error)
start_response('200 OK', [('Content-type',
'text/xml')])
return [response]

for i in range(0, end_date + 1, 12):
date_ = start_date + relativedelta(months=+i)
dates.append(date_.strftime('%Y'))

else:
time_error = 'temps en dehors des heures valides / time outside valid hours'
response = get_custom_service_exception('NoMatch', 'time', time_error)
if time_ != time_iso.strftime('%Y-%m'):
time_error = 'Format de temps invalide, ' \
'format attendu' \
' YYYY-MM / Invalid time format, ' \
'expected format: YYYY-MM'
response = get_custom_service_exception('InvalidDimensionValue', # noqa
'time',
time_error)
start_response('200 OK', [('Content-type',
'text/xml')])
return [response]

for i in range(0, end_date + 1, 1):
date_ = start_date + relativedelta(months=+i)
dates.append(date_.strftime('%Y-%m'))

if time_ not in dates:
time_error = 'Temps en dehors des heures valides /' \
' Time outside valid hours'
response = get_custom_service_exception('NoMatch',
'time',
time_error)
start_response('200 OK', [('Content-type', 'text/xml')])
return [response]

except ValueError:
time_error = 'Format de temps invalide / time format is invalid'
response = get_custom_service_exception('NoMatch', 'time', time_error)
time_error = 'Valeur de temps invalide /' \
' Time value is invalid'
response = get_custom_service_exception('InvalidDimensionValue', # noqa
'time',
time_error)
start_response('200 OK', [('Content-type', 'text/xml')])
return [response]

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ click
mappyfile
matplotlib
numpy
python-dateutil
pyyaml

0 comments on commit 7203ce5

Please sign in to comment.