Exemplo n.º 1
0
def test_unixtime_roundtrip(tzone):
    _environ = os.environ.copy()
    try:
        os.environ['TZ'] = tzone
        y, m, d = 2000, 1, 1
        epoch = cdfepoch.compute_tt2000([[y, m, d]])
        unixtime = cdfepoch.unixtime(epoch)
        assert unixtime == [946684800.0]
    finally:
        os.environ.clear()
        os.environ.update(_environ)
Exemplo n.º 2
0
def test_unixtime():
    x = cdfepoch.unixtime([500000000100, 123456789101112131])
    assert x[0] == 946728435.816
    assert x[1] == 1070184724.917112
Exemplo n.º 3
0
def _convert_cdf_time_types(data,
                            atts,
                            properties,
                            to_datetime=False,
                            to_unixtime=False):
    '''
    # Converts CDF time types into either datetime objects, unixtime, or nothing
    # If nothing, ALL CDF_EPOCH16 types are converted to CDF_EPOCH, because xarray can't handle int64s
    '''

    if not hasattr(data, '__len__'):
        data = [data]

    if to_datetime and to_unixtime:
        print(
            "Cannot convert to both unixtime and datetime.  Continuing with conversion to unixtime."
        )
        to_datetime = False

    # Convert all data in the "data" variable to unixtime or datetime if needed
    data_type = properties['Data_Type_Description']
    if len(data) == 0 or data_type not in ('CDF_EPOCH', 'CDF_EPOCH16',
                                           'CDF_TIME_TT2000'):
        new_data = data
    else:
        if to_datetime:
            new_data = cdfepoch.to_datetime(data)
            if 'UNITS' in atts:
                atts['UNITS']['Data'] = 'Datetime (UTC)'
        elif to_unixtime:
            new_data = cdfepoch.unixtime(data)
            if 'UNITS' in atts:
                atts['UNITS']['Data'] = 'seconds'
        else:
            if data_type == 'CDF_EPOCH16':
                new_data = cdfepoch.compute(cdfepoch.breakdown(data)[0:7])
            else:
                new_data = data

    # Convert all the attributes in the "atts" dictionary to unixtime or datetime if needed
    new_atts = {}
    for att in atts:
        data_type = atts[att]['Data_Type']
        data = atts[att]['Data']
        if not hasattr(data, '__len__'):
            data = [data]
        if len(data) == 0 or data_type not in ('CDF_EPOCH', 'CDF_EPOCH16',
                                               'CDF_TIME_TT2000'):
            new_atts[att] = data
        else:
            if to_datetime:
                new_atts[att] = cdfepoch.to_datetime(data)
            elif to_unixtime:
                new_atts[att] = cdfepoch.unixtime(data)
            else:
                if data_type == 'CDF_EPOCH16':
                    new_atts[att] = cdfepoch.compute(
                        cdfepoch.breakdown(data)[0:7])
                else:
                    new_atts[att] = data

    return new_data, new_atts