amda.py
100 lines
| 3.8 KiB
| text/x-python
|
PythonLexer
r1431 | import traceback | |||
r1429 | import os | |||
r1431 | from datetime import datetime, timedelta, timezone | |||
r1429 | import PythonProviders | |||
import pysciqlopcore | ||||
import numpy as np | ||||
import requests | ||||
r1432 | import copy | |||
r1430 | from spwc.amda import AMDA | |||
r1429 | ||||
r1430 | amda = AMDA() | |||
r1465 | def amda_make_scalar(var=None): | |||
if var is None: | ||||
return pysciqlopcore.ScalarTimeSerie(1) | ||||
else: | ||||
return pysciqlopcore.ScalarTimeSerie(var.time,var.data) | ||||
def amda_make_vector(var=None): | ||||
if var is None: | ||||
return pysciqlopcore.VectorTimeSerie(1) | ||||
else: | ||||
return pysciqlopcore.VectorTimeSerie(var.time,var.data) | ||||
def amda_make_multi_comp(var=None): | ||||
if var is None: | ||||
return pysciqlopcore.MultiComponentTimeSerie((0,2)) | ||||
else: | ||||
return pysciqlopcore.MultiComponentTimeSerie(var.time,var.data) | ||||
def amda_make_spectro(var=None): | ||||
if var is None: | ||||
return pysciqlopcore.SpectrogramTimeSerie((0,2)) | ||||
else: | ||||
r1467 | min_sampling = float(var.meta.get("DATASET_MIN_SAMPLING","nan")) | |||
max_sampling = float(var.meta.get("DATASET_MAX_SAMPLING","nan")) | ||||
r1465 | if "PARAMETER_TABLE_MIN_VALUES[1]" in var.meta: | |||
min_v = np.array([ float(v) for v in var.meta["PARAMETER_TABLE_MIN_VALUES[1]"].split(',') ]) | ||||
max_v = np.array([ float(v) for v in var.meta["PARAMETER_TABLE_MAX_VALUES[1]"].split(',') ]) | ||||
y = (max_v + min_v)/2. | ||||
elif "PARAMETER_TABLE_MIN_VALUES[0]" in var.meta: | ||||
min_v = np.array([ float(v) for v in var.meta["PARAMETER_TABLE_MIN_VALUES[0]"].split(',') ]) | ||||
max_v = np.array([ float(v) for v in var.meta["PARAMETER_TABLE_MAX_VALUES[0]"].split(',') ]) | ||||
y = (max_v + min_v)/2. | ||||
else: | ||||
y = np.logspace(1,3,var.data.shape[1])[::-1] | ||||
r1467 | return pysciqlopcore.SpectrogramTimeSerie(var.time,y,var.data,min_sampling,max_sampling) | |||
r1465 | ||||
def amda_get_sample(metadata,start,stop): | ||||
ts_type = amda_make_scalar | ||||
r1429 | try: | |||
r1430 | param_id = None | |||
for key,value in metadata: | ||||
if key == 'xml:id': | ||||
param_id = value | ||||
elif key == 'type': | ||||
if value == 'vector': | ||||
r1465 | ts_type = amda_make_vector | |||
r1432 | elif value == 'multicomponent': | |||
r1465 | ts_type = amda_make_multi_comp | |||
elif value == 'spectrogram': | ||||
ts_type = amda_make_spectro | ||||
r1439 | tstart=datetime.fromtimestamp(start, tz=timezone.utc) | |||
tend=datetime.fromtimestamp(stop, tz=timezone.utc) | ||||
r1464 | var = amda.get_parameter(start_time=tstart, stop_time=tend, parameter_id=param_id, method="REST") | |||
r1465 | return ts_type(var) | |||
r1429 | except Exception as e: | |||
r1431 | print(traceback.format_exc()) | |||
r1430 | print("Error in amda.py ",str(e)) | |||
r1465 | return ts_type() | |||
r1430 | ||||
if len(amda.component) is 0: | ||||
amda.update_inventory() | ||||
r1432 | parameters = copy.deepcopy(amda.parameter) | |||
r1430 | for name,component in amda.component.items(): | |||
if 'components' in parameters[component['parameter']]: | ||||
parameters[component['parameter']]['components'].append(component) | ||||
else: | ||||
parameters[component['parameter']]['components']=[component] | ||||
products = [] | ||||
for key,parameter in parameters.items(): | ||||
r1439 | path = f"/AMDA/{parameter['mission']}/{parameter.get('observatory','')}/{parameter['instrument']}/{parameter['dataset']}/{parameter['name']}" | |||
r1430 | components = [component['name'] for component in parameter.get('components',[])] | |||
metadata = [ (key,item) for key,item in parameter.items() if key is not 'components' ] | ||||
r1432 | n_components = parameter.get('size',0) | |||
r1465 | if n_components == '3': | |||
r1430 | metadata.append(("type","vector")) | |||
r1465 | elif parameter.get('display_type','')=="spectrogram": | |||
metadata.append(("type","spectrogram")) | ||||
r1432 | elif n_components !=0: | |||
r1465 | metadata.append(("type","multicomponent")) | |||
r1430 | else: | |||
metadata.append(("type","scalar")) | ||||
products.append( (path, components, metadata)) | ||||
r1429 | ||||
r1465 | PythonProviders.register_product(products, amda_get_sample) | |||
r1429 | ||||