Commit 8b1f082a83ee7817e4eaeebc1ae74ae3cd1459db
1 parent
3b520446
Exists in
rhitier-dev
and in
1 other branch
Change read_var to return array of numpy type
Showing
2 changed files
with
15 additions
and
24 deletions
Show diff stats
tests/BackendTests.py
... | ... | @@ -5,9 +5,10 @@ import unittest |
5 | 5 | from datetime import datetime as ddatetime, datetime |
6 | 6 | from pprint import pprint |
7 | 7 | |
8 | +import numpy.ma.core | |
8 | 9 | from netCDF4 import Dataset |
9 | 10 | |
10 | -from web.run import retrieve_amda_netcdf, get_data_for_target, _sta_sto, _read_var | |
11 | +from web.run import retrieve_amda_netcdf, get_data_for_target, _sta_sto, _read_var, default_nc_keys | |
11 | 12 | |
12 | 13 | FILE_DATE_FMT = "%Y-%m-%d %H:%M:%S" |
13 | 14 | |
... | ... | @@ -72,21 +73,11 @@ class AmdaTestCase(BaseTestCase): |
72 | 73 | self.assertIsInstance(s0, datetime) |
73 | 74 | self.assertIsInstance(s1, datetime) |
74 | 75 | |
75 | - def test_read_nc(self): | |
76 | + def test_var_read_nc_float32(self): | |
76 | 77 | SCRIPT_PATH = os.path.dirname(__file__) |
77 | 78 | PROJECT_DIR = os.path.abspath(os.path.join(SCRIPT_PATH, os.pardir)) |
78 | 79 | local_netc_file = os.path.join(PROJECT_DIR,'tests-resources', 'amda-irap-omp-eu-ddservice-base-data-omni-hour-omni202107010-nc') |
79 | 80 | cdf_handle = Dataset(local_netc_file, "r", format="NETCDF4") |
80 | - default_nc_keys = { | |
81 | - 'hee': 'HEE', | |
82 | - 'vtot': 'V', | |
83 | - 'magn': 'B', | |
84 | - 'temp': 'T', | |
85 | - 'dens': 'N', | |
86 | - 'pdyn': 'P_dyn', | |
87 | - 'atse': 'Delta_angle', | |
88 | - } | |
89 | 81 | nc_keys = default_nc_keys.copy() |
90 | 82 | data_v = _read_var(cdf_handle, nc_keys, 'vtot') |
91 | - pprint(data_v) | |
92 | - pprint(data_v[0]) | |
83 | + self.assertIsInstance(data_v[0], numpy.float32) | ... | ... |
web/run.py
... | ... | @@ -332,6 +332,16 @@ def datetime_from_list(time_list): |
332 | 332 | "%Y%j%H%M%S%f" |
333 | 333 | ) |
334 | 334 | |
335 | +# Override these using the model configuration in config.yml | |
336 | +default_nc_keys = { | |
337 | + 'hee': 'HEE', | |
338 | + 'vtot': 'V', | |
339 | + 'magn': 'B', | |
340 | + 'temp': 'T', | |
341 | + 'dens': 'N', | |
342 | + 'pdyn': 'P_dyn', | |
343 | + 'atse': 'Delta_angle', | |
344 | +} | |
335 | 345 | |
336 | 346 | def _sta_sto(_cnf, _sta, _sto): |
337 | 347 | if 'started_at' in _cnf: |
... | ... | @@ -349,7 +359,7 @@ def _sta_sto(_cnf, _sta, _sto): |
349 | 359 | |
350 | 360 | def _read_var(_nc, _keys, _key, mandatory=False): |
351 | 361 | try: |
352 | - return _nc.variables[_keys[_key]] | |
362 | + return _nc.variables[_keys[_key]][:] | |
353 | 363 | except KeyError: |
354 | 364 | pass |
355 | 365 | if mandatory: |
... | ... | @@ -673,16 +683,6 @@ def get_data_for_target(target_config, input_slug, |
673 | 683 | # abort(500, "Invalid orbit configuration for '%s' : %s" |
674 | 684 | # % (target_config['slug'], str(e))) |
675 | 685 | |
676 | - # Override these using the model configuration in config.yml | |
677 | - default_nc_keys = { | |
678 | - 'hee': 'HEE', | |
679 | - 'vtot': 'V', | |
680 | - 'magn': 'B', | |
681 | - 'temp': 'T', | |
682 | - 'dens': 'N', | |
683 | - 'pdyn': 'P_dyn', | |
684 | - 'atse': 'Delta_angle', | |
685 | - } | |
686 | 686 | |
687 | 687 | precision = "%Y-%m-%dT%H" # model and orbits times are only equal-ish |
688 | 688 | orbit_data = {} # keys are datetime as str, values arrays of XY | ... | ... |