Commit 57493104abc20ff712b259f73375d1a6ed1e5702
1 parent
bc18b96c
Exists in
master
and in
2 other branches
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,7 +16,7 @@ from flask import Flask | ||
16 | from flask import request | 16 | from flask import request |
17 | from flask import url_for, send_from_directory, abort as abort_flask | 17 | from flask import url_for, send_from_directory, abort as abort_flask |
18 | from jinja2 import Environment, FileSystemLoader | 18 | from jinja2 import Environment, FileSystemLoader |
19 | -from netCDF4 import Dataset | 19 | +from netCDF4 import Dataset, date2num |
20 | from yaml import load as yaml_load | 20 | from yaml import load as yaml_load |
21 | 21 | ||
22 | # PATH RELATIVITY ############################################################# | 22 | # PATH RELATIVITY ############################################################# |
@@ -547,7 +547,7 @@ def download_targets_tarball(targets, started_at, stopped_at): | @@ -547,7 +547,7 @@ def download_targets_tarball(targets, started_at, stopped_at): | ||
547 | if 0 == len(targets_configs): | 547 | if 0 == len(targets_configs): |
548 | abort(400, "No valid targets specified. What are you doing?") | 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 | try: | 551 | try: |
552 | started_at = datetime.datetime.strptime(started_at, date_fmt) | 552 | started_at = datetime.datetime.strptime(started_at, date_fmt) |
553 | except: | 553 | except: |
@@ -613,7 +613,7 @@ def download_targets_netcdf(targets, params, started_at, stopped_at): | @@ -613,7 +613,7 @@ def download_targets_netcdf(targets, params, started_at, stopped_at): | ||
613 | if not is_list_in_list(params, PARAMETERS.keys()): | 613 | if not is_list_in_list(params, PARAMETERS.keys()): |
614 | abort(400, "Some parameters are not recognized in '%s'." % str(params)) | 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 | try: | 617 | try: |
618 | started_at = datetime.datetime.strptime(started_at, date_fmt) | 618 | started_at = datetime.datetime.strptime(started_at, date_fmt) |
619 | except: | 619 | except: |
@@ -644,7 +644,23 @@ def download_targets_netcdf(targets, params, started_at, stopped_at): | @@ -644,7 +644,23 @@ def download_targets_netcdf(targets, params, started_at, stopped_at): | ||
644 | data = get_data_for_target(target, started_at, stopped_at) | 644 | data = get_data_for_target(target, started_at, stopped_at) |
645 | dkeys = sorted(data) | 645 | dkeys = sorted(data) |
646 | nc_handle.createDimension('dim_'+target_slug, len(dkeys)) | 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 | nc_vars = [] | 664 | nc_vars = [] |
649 | indices = [] | 665 | indices = [] |
650 | for param in params: | 666 | for param in params: |
@@ -663,6 +679,7 @@ def download_targets_netcdf(targets, params, started_at, stopped_at): | @@ -663,6 +679,7 @@ def download_targets_netcdf(targets, params, started_at, stopped_at): | ||
663 | nc_var[:] = values | 679 | nc_var[:] = values |
664 | # todo: add orbit x and y | 680 | # todo: add orbit x and y |
665 | except Exception as e: | 681 | except Exception as e: |
682 | + log.error("Failed to generate NetCDF '%s'." % nc_filename) | ||
666 | raise e | 683 | raise e |
667 | finally: | 684 | finally: |
668 | nc_handle.close() | 685 | nc_handle.close() |