diff --git a/debian/control b/debian/control index 3152930..789b5d1 100644 --- a/debian/control +++ b/debian/control @@ -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 diff --git a/geomet_climate/wsgi.py b/geomet_climate/wsgi.py index d4d6bf1..02173b4 100755 --- a/geomet_climate/wsgi.py +++ b/geomet_climate/wsgi.py @@ -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"> {text} - '''.format(code=code, locator=locator, text=text), encoding='utf-8') + '''.format(code=code, locator=locator, text=text), encoding='utf-8') # noqa def application(env, start_response): @@ -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] diff --git a/requirements.txt b/requirements.txt index 1d2b9f4..2896d06 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ click mappyfile matplotlib numpy +python-dateutil pyyaml