Commit 58bfe2813b911f5523ba5fdfa4316eb2f7280501
1 parent
60b73eb1
Exists in
master
and in
2 other branches
Handle start and stop dates for the models as well.
Showing
1 changed file
with
55 additions
and
70 deletions
Show diff stats
web/run.py
... | ... | @@ -427,44 +427,24 @@ def get_data_for_target(target_config, started_at, stopped_at): |
427 | 427 | abort(500, "Invalid orbit configuration for '%s' : %s" |
428 | 428 | % (target_config['slug'], str(e))) |
429 | 429 | |
430 | - # try: | |
431 | - # started_at = datetime.datetime.strptime(started_at, FILE_DATE_FMT) | |
432 | - # except: | |
433 | - # abort(400, "Invalid started_at config : '%s'." % started_at) | |
434 | - # try: | |
435 | - # stopped_at = datetime.datetime.strptime(stopped_at, FILE_DATE_FMT) | |
436 | - # except: | |
437 | - # abort(400, "Invalid stopped_at config : '%s'." % stopped_at) | |
438 | - | |
439 | - # Grab the list of netCDF files from Myriam's API | |
440 | - model_files = [] | |
441 | - for model in models: | |
442 | - model_files = model_files + retrieve_amda_netcdf( | |
443 | - target_config['slug'], model['slug'], started_at, stopped_at | |
444 | - ) | |
445 | - # Remove possible duplicates | |
446 | - model_files = set(model_files) | |
447 | - | |
448 | - # Let's return an empty dict when there's no data instead of crashing | |
449 | - # if not len(model_files): | |
450 | - # abort(500, "No model files found for '%s'." % target_config['slug']) | |
451 | - # if not len(orbit_files): | |
452 | - # abort(500, "No orbit files found for '%s'." % target_config['slug']) | |
430 | + def _sta_sto(_cnf, _sta, _sto): | |
431 | + if 'started_at' in _cnf: | |
432 | + _s0 = datetime.datetime.strptime(_cnf['started_at'], FILE_DATE_FMT) | |
433 | + _s0 = max(_s0, _sta) | |
434 | + else: | |
435 | + _s0 = _sta | |
436 | + if 'stopped_at' in _cnf: | |
437 | + _s1 = datetime.datetime.strptime(_cnf['stopped_at'], FILE_DATE_FMT) | |
438 | + _s1 = min(_s1, _sto) | |
439 | + else: | |
440 | + _s1 = _sto | |
441 | + return _s1, _s0 | |
453 | 442 | |
454 | 443 | precision = "%Y-%m-%dT%H" # model and orbits times are only equal-ish |
455 | 444 | orbit_data = {} # keys are datetime as str, values arrays of XY |
456 | 445 | |
457 | 446 | for orbit in orbits: |
458 | - if 'started_at' in orbit: | |
459 | - s0 = datetime.datetime.strptime(orbit['started_at'], FILE_DATE_FMT) | |
460 | - s0 = max(s0, started_at) | |
461 | - else: | |
462 | - s0 = started_at | |
463 | - if 'stopped_at' in orbit: | |
464 | - s1 = datetime.datetime.strptime(orbit['stopped_at'], FILE_DATE_FMT) | |
465 | - s1 = min(s1, stopped_at) | |
466 | - else: | |
467 | - s1 = stopped_at | |
447 | + s0, s1 = _sta_sto(orbit, started_at, stopped_at) | |
468 | 448 | |
469 | 449 | orbit_files = retrieve_amda_netcdf( |
470 | 450 | target_config['slug'], orbit['slug'], s0, s1 |
... | ... | @@ -493,43 +473,48 @@ def get_data_for_target(target_config, started_at, stopped_at): |
493 | 473 | cdf_handle.close() |
494 | 474 | |
495 | 475 | all_data = {} # keys are datetime as str, values tuples of data |
496 | - for model_file in model_files: | |
497 | - # Time, StartTime, StopTime, V, B, N, T, Delta_angle, P_dyn | |
498 | - log.debug("%s: opening model NETCDF4 '%s'..." % | |
499 | - (target_config['name'], model_file)) | |
500 | - cdf_handle = Dataset(model_file, "r", format="NETCDF4") | |
501 | - times = cdf_handle.variables['Time'] # YYYY DOY HH MM SS .ms | |
502 | - data_v = cdf_handle.variables['V'] | |
503 | - data_b = cdf_handle.variables['B'] | |
504 | - data_t = cdf_handle.variables['T'] | |
505 | - data_n = cdf_handle.variables['N'] | |
506 | - data_p = cdf_handle.variables['P_dyn'] | |
507 | - data_d = cdf_handle.variables['Delta_angle'] | |
508 | - log.debug("%s: aggregating data from '%s'..." % | |
509 | - (target_config['name'], model_file)) | |
510 | - for time, datum_v, datum_b, datum_t, datum_n, datum_p, datum_d \ | |
511 | - in zip(times, data_v, data_b, data_t, data_n, data_p, data_d): | |
512 | - vrad = datum_v[0] | |
513 | - vtan = datum_v[1] | |
514 | - try: | |
515 | - dtime = datetime_from_list(time) | |
516 | - except Exception as e: | |
517 | - log.error("Failed to parse time from %s." % time) | |
518 | - raise e | |
519 | - if started_at <= dtime <= stopped_at: | |
520 | - dkey = round_time(dtime, 60*60).strftime(precision) | |
521 | - x_hee = None | |
522 | - y_hee = None | |
523 | - if dkey in orbit_data: | |
524 | - x_hee = orbit_data[dkey][0] | |
525 | - y_hee = orbit_data[dkey][1] | |
526 | - all_data[dkey] = ( | |
527 | - dtime.strftime("%Y-%m-%dT%H:%M:%S+00:00"), | |
528 | - vrad, vtan, sqrt(vrad * vrad + vtan * vtan), | |
529 | - datum_b, datum_t, datum_n, datum_p, datum_d, | |
530 | - x_hee, y_hee | |
531 | - ) | |
532 | - cdf_handle.close() | |
476 | + for model in models: | |
477 | + s0, s1 = _sta_sto(model, started_at, stopped_at) | |
478 | + model_files = retrieve_amda_netcdf( | |
479 | + target_config['slug'], model['slug'], s0, s1 | |
480 | + ) | |
481 | + for model_file in model_files: | |
482 | + # Time, StartTime, StopTime, V, B, N, T, Delta_angle, P_dyn | |
483 | + log.debug("%s: opening model NETCDF4 '%s'..." % | |
484 | + (target_config['name'], model_file)) | |
485 | + cdf_handle = Dataset(model_file, "r", format="NETCDF4") | |
486 | + times = cdf_handle.variables['Time'] # YYYY DOY HH MM SS .ms | |
487 | + data_v = cdf_handle.variables['V'] | |
488 | + data_b = cdf_handle.variables['B'] | |
489 | + data_t = cdf_handle.variables['T'] | |
490 | + data_n = cdf_handle.variables['N'] | |
491 | + data_p = cdf_handle.variables['P_dyn'] | |
492 | + data_d = cdf_handle.variables['Delta_angle'] | |
493 | + log.debug("%s: aggregating data from '%s'..." % | |
494 | + (target_config['name'], model_file)) | |
495 | + for time, datum_v, datum_b, datum_t, datum_n, datum_p, datum_d \ | |
496 | + in zip(times, data_v, data_b, data_t, data_n, data_p, data_d): | |
497 | + vrad = datum_v[0] | |
498 | + vtan = datum_v[1] | |
499 | + try: | |
500 | + dtime = datetime_from_list(time) | |
501 | + except Exception as e: | |
502 | + log.error("Failed to parse time from %s." % time) | |
503 | + raise e | |
504 | + if s0 <= dtime <= s1: | |
505 | + dkey = round_time(dtime, 60*60).strftime(precision) | |
506 | + x_hee = None | |
507 | + y_hee = None | |
508 | + if dkey in orbit_data: | |
509 | + x_hee = orbit_data[dkey][0] | |
510 | + y_hee = orbit_data[dkey][1] | |
511 | + all_data[dkey] = ( | |
512 | + dtime.strftime("%Y-%m-%dT%H:%M:%S+00:00"), | |
513 | + vrad, vtan, sqrt(vrad * vrad + vtan * vtan), | |
514 | + datum_b, datum_t, datum_n, datum_p, datum_d, | |
515 | + x_hee, y_hee | |
516 | + ) | |
517 | + cdf_handle.close() | |
533 | 518 | |
534 | 519 | return all_data |
535 | 520 | ... | ... |