SciQLop / speasy

Space Physics made EASY! A simple Python package to deal with main Space Physics WebServices (CDA,SSC,AMDA,..)
Other
24 stars 7 forks source link

astropy units and tables #44

Closed Dolgalad closed 2 years ago

Dolgalad commented 2 years ago

Description

Convert SpeasyVariable object to astropy.table.Table with correct units (when conversion to astropy.unit.Unit is possible). Only works if :

Note : dataframe columns are renamed to param.meta["PARAMETER_COMPONENTS"] when possible

What I Did

def to_astropy_table(parameter):
    # some units may not be convertable
    try:
        units = astropy.units.Unit(parameter.meta["PARAMETER_UNITS"])
    except:
        units = None

    df = parameter.to_dataframe(datetime_index=True)

    unit_map = {c:units for c in df.columns}
    return astropy.table.Table.from_pandas(df, units=unit_map, index=True)
jeandet commented 2 years ago

@brenard-irap, AMDA CSV files columns names don't match PARAMETER_COMPONENTS is this something we can fix? I would like to avoid using metadata as much as possible since it is WS dependent and this method has to work with all Speasy time series.

brenard-irap commented 2 years ago

PARAMETER_COMPONENTS can be not unique when you download two parameters in the same file (two magnetic fields vector for example: bx, by & bz). The columns names in the CSV must be unique...

Dolgalad commented 2 years ago

I'll ignore PARAMETER_COMPONENTS. It was essentially for esthetics purposes.

jeandet commented 2 years ago

We could handle this here https://github.com/SciQLop/speasy/blob/main/speasy/webservices/amda/utils.py#L43 if we consider that we always request one parameter at time. Then in a second time, we can see how to consolidate this on AMDA side.

Dolgalad commented 2 years ago

PR #45