Commit 57493104abc20ff712b259f73375d1a6ed1e5702

Authored by Goutte
1 parent bc18b96c

Add the time to the generated NetCDF.

Showing 1 changed file with 21 additions and 4 deletions   Show diff stats
web/run.py
... ... @@ -16,7 +16,7 @@ from flask import Flask
16 16 from flask import request
17 17 from flask import url_for, send_from_directory, abort as abort_flask
18 18 from jinja2 import Environment, FileSystemLoader
19   -from netCDF4 import Dataset
  19 +from netCDF4 import Dataset, date2num
20 20 from yaml import load as yaml_load
21 21  
22 22 # PATH RELATIVITY #############################################################
... ... @@ -547,7 +547,7 @@ def download_targets_tarball(targets, started_at, stopped_at):
547 547 if 0 == len(targets_configs):
548 548 abort(400, "No valid targets specified. What are you doing?")
549 549  
550   - date_fmt = "%Y-%m-%dT%H:%M:%S"
  550 + date_fmt = FILE_DATE_FMT
551 551 try:
552 552 started_at = datetime.datetime.strptime(started_at, date_fmt)
553 553 except:
... ... @@ -613,7 +613,7 @@ def download_targets_netcdf(targets, params, started_at, stopped_at):
613 613 if not is_list_in_list(params, PARAMETERS.keys()):
614 614 abort(400, "Some parameters are not recognized in '%s'." % str(params))
615 615  
616   - date_fmt = "%Y-%m-%dT%H:%M:%S"
  616 + date_fmt = FILE_DATE_FMT
617 617 try:
618 618 started_at = datetime.datetime.strptime(started_at, date_fmt)
619 619 except:
... ... @@ -644,7 +644,23 @@ def download_targets_netcdf(targets, params, started_at, stopped_at):
644 644 data = get_data_for_target(target, started_at, stopped_at)
645 645 dkeys = sorted(data)
646 646 nc_handle.createDimension('dim_'+target_slug, len(dkeys))
647   - # todo: add time
  647 +
  648 + # TIME #
  649 + nc_time = nc_group.createVariable(
  650 + 'time', 'i8', ('dim_' + target_slug,)
  651 + )
  652 + nc_time.units = "hours since 1970-01-01 00:00:00"
  653 + nc_time.calendar = "standard"
  654 + times = []
  655 + for dkey in dkeys:
  656 + time_as_string = data[dkey][0][:-6] # remove +00:00 tail
  657 + date = datetime.datetime.strptime(time_as_string, date_fmt)
  658 + times.append(date2num(
  659 + date, units=nc_time.units, calendar=nc_time.calendar
  660 + ))
  661 + nc_time[:] = times
  662 +
  663 + # SELECTED PARAMETERS #
648 664 nc_vars = []
649 665 indices = []
650 666 for param in params:
... ... @@ -663,6 +679,7 @@ def download_targets_netcdf(targets, params, started_at, stopped_at):
663 679 nc_var[:] = values
664 680 # todo: add orbit x and y
665 681 except Exception as e:
  682 + log.error("Failed to generate NetCDF '%s'." % nc_filename)
666 683 raise e
667 684 finally:
668 685 nc_handle.close()
... ...