diff --git a/web/run.py b/web/run.py index 37dc936..43b1d7f 100755 --- a/web/run.py +++ b/web/run.py @@ -16,7 +16,7 @@ from flask import Flask from flask import request from flask import url_for, send_from_directory, abort as abort_flask from jinja2 import Environment, FileSystemLoader -from netCDF4 import Dataset +from netCDF4 import Dataset, date2num from yaml import load as yaml_load # PATH RELATIVITY ############################################################# @@ -547,7 +547,7 @@ def download_targets_tarball(targets, started_at, stopped_at): if 0 == len(targets_configs): abort(400, "No valid targets specified. What are you doing?") - date_fmt = "%Y-%m-%dT%H:%M:%S" + date_fmt = FILE_DATE_FMT try: started_at = datetime.datetime.strptime(started_at, date_fmt) except: @@ -613,7 +613,7 @@ def download_targets_netcdf(targets, params, started_at, stopped_at): if not is_list_in_list(params, PARAMETERS.keys()): abort(400, "Some parameters are not recognized in '%s'." % str(params)) - date_fmt = "%Y-%m-%dT%H:%M:%S" + date_fmt = FILE_DATE_FMT try: started_at = datetime.datetime.strptime(started_at, date_fmt) except: @@ -644,7 +644,23 @@ def download_targets_netcdf(targets, params, started_at, stopped_at): data = get_data_for_target(target, started_at, stopped_at) dkeys = sorted(data) nc_handle.createDimension('dim_'+target_slug, len(dkeys)) - # todo: add time + + # TIME # + nc_time = nc_group.createVariable( + 'time', 'i8', ('dim_' + target_slug,) + ) + nc_time.units = "hours since 1970-01-01 00:00:00" + nc_time.calendar = "standard" + times = [] + for dkey in dkeys: + time_as_string = data[dkey][0][:-6] # remove +00:00 tail + date = datetime.datetime.strptime(time_as_string, date_fmt) + times.append(date2num( + date, units=nc_time.units, calendar=nc_time.calendar + )) + nc_time[:] = times + + # SELECTED PARAMETERS # nc_vars = [] indices = [] for param in params: @@ -663,6 +679,7 @@ def download_targets_netcdf(targets, params, started_at, stopped_at): nc_var[:] = values # todo: add orbit x and y except Exception as e: + log.error("Failed to generate NetCDF '%s'." % nc_filename) raise e finally: nc_handle.close() -- libgit2 0.21.2