cdaweb.py
54 lines
| 1.8 KiB
| text/x-python
|
PythonLexer
r1430 | import os | |||
import PythonProviders | ||||
import pysciqlopcore | ||||
import numpy as np | ||||
import pandas as pds | ||||
import requests | ||||
r1440 | from datetime import datetime, timedelta, timezone | |||
r1430 | from spwc.cdaweb import cdaweb | |||
cd = cdaweb() | ||||
r1434 | def cda_get_sample(metadata, start,stop): | |||
ts_type = pysciqlopcore.ScalarTimeSerie | ||||
default_ctor_args = 1 | ||||
r1430 | try: | |||
r1434 | variable_id = None | |||
dataset_id = None | ||||
r1435 | drop_cols = [] | |||
r1434 | for key,value in metadata: | |||
if key == 'VAR_ID': | ||||
variable_id = value | ||||
elif key == 'DATASET_ID': | ||||
dataset_id = value | ||||
r1435 | elif key == 'drop_col': | |||
drop_cols.append(value) | ||||
r1434 | elif key == 'type': | |||
if value == 'vector': | ||||
ts_type = pysciqlopcore.VectorTimeSerie | ||||
elif value == 'multicomponent': | ||||
ts_type = pysciqlopcore.MultiComponentTimeSerie | ||||
default_ctor_args = (0,2) | ||||
r1440 | tstart=datetime.fromtimestamp(start, tz=timezone.utc) | |||
tend=datetime.fromtimestamp(stop, tz=timezone.utc) | ||||
r1434 | df = cd.get_variable(dataset=dataset_id,variable=variable_id,tstart=tstart,tend=tend) | |||
r1435 | if len(df): | |||
df = df.drop(columns = drop_cols) | ||||
r1434 | t = np.array([d.timestamp() for d in df.index]) | |||
r1430 | values = df.values | |||
r1434 | return ts_type(t,values) | |||
except Exception as e: | ||||
print(traceback.format_exc()) | ||||
r1435 | print("Error in cdaweb.py ",str(e)) | |||
r1434 | return ts_type(default_ctor_args) | |||
r1430 | ||||
r1434 | products = [ | |||
r1435 | ("/CDA/Themis/ThA/tha_fgl_gsm", [], [("type","vector"), ('drop_col','UT__sec'), ("DATASET_ID","THA_L2_FGM"), ("VAR_ID","tha_fgl_gsm")]), | |||
("/CDA/Themis/ThB/thb_fgl_gsm", [], [("type","vector"), ('drop_col','UT__sec'), ("DATASET_ID","THB_L2_FGM"), ("VAR_ID","thb_fgl_gsm")]), | ||||
r1434 | ||||
] | ||||
PythonProviders.register_product(products, cda_get_sample) | ||||
r1430 | ||||