コード例 #1
0
def mms_pgs_mphigeo(mag_temp, pos_temp):
    """
    Generates the 'mphigeo' transformation matrix
    """
    pos_data = get_data(pos_temp)

    if pos_data is None:
        logging.error('Error with position data')
        return

    # the following is heisted from the IDL version
    # transformation to generate other_dim dim for mphigeo from thm_fac_matrix_make
    # All the conversions to polar and trig simplifies to this.
    # But the reason the conversion is why this is the conversion that is done, is lost on me.
    # The conversion swaps the x & y components of position, reflects over x=0,z=0 then projects into the xy plane
    pos_conv = np.stack(
        (-pos_data.y[:, 1], pos_data.y[:, 0], np.zeros(len(pos_data.times))))
    pos_conv = np.transpose(pos_conv, [1, 0])
    store_data(pos_temp, data={'x': pos_data.times, 'y': pos_conv})

    # transform into GSE because the particles are in GSE
    cotrans(name_in=pos_temp,
            name_out=pos_temp,
            coord_in='gei',
            coord_out='gse')

    # create orthonormal basis set
    z_basis = tnormalize(mag_temp, return_data=True)
    x_basis = tcrossp(z_basis, pos_temp, return_data=True)
    x_basis = tnormalize(x_basis, return_data=True)
    y_basis = tcrossp(z_basis, x_basis, return_data=True)

    return (x_basis, y_basis, z_basis)
コード例 #2
0
 def test_tcrossp(self):
     """ cross product tests"""
     cp = tcrossp([3, -3, 1], [4, 9, 2], return_data=True)
     self.assertTrue(cp.tolist() == [-15, -2, 39])
     cp = tcrossp([3, -3, 1], [4, 9, 2])
     cp = get_data(cp)
     self.assertTrue(cp.y[0, :].tolist() == [-15, -2, 39])
     store_data('var1', data={'x': [0], 'y': [[3, -3, 1]]})
     store_data('var2', data={'x': [0], 'y': [[4, 9, 2]]})
     cp = tcrossp('var1', 'var2', return_data=True)
     self.assertTrue(cp[0].tolist() == [-15, -2, 39])
     cp = tcrossp('var1', 'var2', newname='test_crossp')
     cp = get_data('test_crossp')
     self.assertTrue(cp.y[0, :].tolist() == [-15, -2, 39])
コード例 #3
0
def xgse(mag_temp):
    """
    Generates the 'xgse' transformation matrix
    """
    mag_data = get_data(mag_temp)

    # xaxis of this system is X of the gse system. Z is mag field
    x_axis = np.zeros((len(mag_data.times), 3))
    x_axis[:, 0] = 1

    # create orthonormal basis set
    z_basis = tnormalize(mag_temp, return_data=True)
    y_basis = tcrossp(z_basis, x_axis, return_data=True)
    y_basis = tnormalize(y_basis, return_data=True)
    x_basis = tcrossp(y_basis, z_basis, return_data=True)

    return (x_basis, y_basis, z_basis)
コード例 #4
0
def dsi2j2000(name_in=None,
              name_out=None,
              no_orb=False,
              J20002DSI=False,
              noload=False):
    """
    This function transform a time series data between the DSI and J2000 coordinate systems

    Parameters:

        name_in : str
            input tplot variable to be transformed

        name_out : str
            Name of the tplot variable in which the transformed data is stored

        J20002DSI : bool
            Set to transform data from J2000 to DSI. If not set, it transforms data from DSI to J2000.

    Returns:
        None

    """
    if (name_in is None) or (name_in not in tplot_names(quiet=True)):
        print('Input of Tplot name is undifiend')
        return

    if name_out is None:
        print('Tplot name for output is undifiend')
        name_out = 'result_of_dsi2j2000'

    # prepare for transformed Tplot Variable
    reload = not noload
    dl_in = get_data(name_in, metadata=True)
    get_data_array = get_data(name_in)
    time_array = get_data_array[0]
    time_length = time_array.shape[0]
    dat = get_data_array[1]

    # Get the SGI axis by interpolating the attitude data
    dsiz_j2000 = erg_interpolate_att(name_in, noload=noload)['sgiz_j2000']

    # Sun direction in J2000
    sundir = np.array([[1., 0., 0.]]*time_length)

    if no_orb:
        store_data('sundir_gse', data={'x': time_array, 'y': sundir})

    else:  # Calculate the sun directions from the instantaneous satellite locations
        if reload:
            tr = get_timespan(name_in)
            orb(trange=time_string([tr[0] - 60., tr[1] + 60.]))
            tinterpol('erg_orb_l2_pos_gse', time_array)
            scpos = get_data('erg_orb_l2_pos_gse-itrp')[1]
            sunpos = np.array([[1.496e+08, 0., 0.]]*time_length)
            sundir = sunpos - scpos
            store_data('sundir_gse', data={'x': time_array, 'y': sundir})
            tnormalize('sundir_gse', newname='sundir_gse')

    # Derive DSI-X and DSI-Y axis vectors in J2000.
    # The elementary vectors below are the definition of DSI. The detailed relationship
    # between the spin phase, sun pulse timing, sun direction, and the actual subsolar point
    # on the spining s/c body should be incorporated into the calculation below.
    if reload:
        cotrans(name_in='sundir_gse', name_out='sundir_j2000',
                coord_in='gse', coord_out='j2000')
    sun_j2000 = get_data('sundir_j2000')
    dsiy = tcrossp(dsiz_j2000['y'], sun_j2000[1], return_data=True)
    dsix = tcrossp(dsiy, dsiz_j2000['y'], return_data=True)
    dsix_j2000 = {'x': time_array, 'y': dsix}
    dsiy_j2000 = {'x': time_array, 'y': dsiy}

    if not J20002DSI:
        print('DSI --> J2000')
        mat = cart_trans_matrix_make(
            dsix_j2000['y'], dsiy_j2000['y'], dsiz_j2000['y'])
        j2000x_in_dsi = np.dot(mat, np.array([1., 0., 0.]))
        j2000y_in_dsi = np.dot(mat, np.array([0., 1., 0.]))
        j2000z_in_dsi = np.dot(mat, np.array([0., 0., 1.]))
        mat = cart_trans_matrix_make(
            j2000x_in_dsi, j2000y_in_dsi, j2000z_in_dsi)
        dat_new = np.einsum("ijk,ik->ij", mat, dat)
    else:
        print('J2000 --> DSI')
        mat = cart_trans_matrix_make(
            dsix_j2000['y'], dsiy_j2000['y'], dsiz_j2000['y'])
        dat_new = np.einsum("ijk,ik->ij", mat, dat)

    store_data(name_out, data={'x': time_array, 'y': dat_new}, attr_dict=dl_in)
    options(name_out, 'ytitle', '\n'.join(name_out.split('_')))
コード例 #5
0
def erg_interpolate_att(erg_xxx_in=None, noload=False):
    """
    This function interpolates erg att data to match erg_xxx_in.

    Parameters:
        erg_xxx_in : str
            input tplot variable relating to ERG to be transformed

    Returns:
        output_dictionary : dict
            Dictionary which has below keys.
                spinperiod: output variable in which the interpolated data of ERG spin period is stored
                spinphase: output variable in which the interpolated data of ERG spin phase is stored
                sgix_j2000, sgiy_j2000, or sgiz_j2000: output interporated SGI axis vector for each component
                sgax_j2000, sgay_j2000, or sgaz_j2000: output interporated SGA axis vector for each component

    """
    if (erg_xxx_in is None) or (erg_xxx_in not in tnames()):
        print('inputted Tplot variable name is None, or not defined')
        return

    reload = not noload

    time_array = get_data(erg_xxx_in)[0]

    # Prepare some constants
    dtor = np.pi / 180.

    output_dictionary = {}

    # Load the attitude data
    if tnames('erg_att_sprate') == ['erg_att_sprate']:
        if reload:
            degap('erg_att_sprate', dt=8., margin=.5)
        sprate = get_data('erg_att_sprate')
        if sprate[0].min() > time_array.min() + 8. or sprate[0].max(
        ) < time_array.max() - 8.:
            tr = get_timespan(erg_xxx_in)
            if reload:
                att(trange=time_string([tr[0] - 60., tr[1] + 60.]))
    else:
        tr = get_timespan(erg_xxx_in)
        if reload:
            att(trange=time_string([tr[0] - 60., tr[1] + 60.]))

    # Interpolate spin period
    if reload:
        degap('erg_att_sprate', dt=8., margin=.5)
    sprate = get_data('erg_att_sprate')
    sper = 1. / (sprate[1] / 60.)
    sperInterp = np.interp(time_array, sprate[0], sper)
    spinperiod = {'x': time_array, 'y': sperInterp}
    output_dictionary['spinperiod'] = spinperiod

    # Interpolate spin phase
    if reload:
        degap('erg_att_spphase', dt=8., margin=.5)
    sphase = get_data('erg_att_spphase')
    if (sphase[0][0] <= time_array[0])\
            and (time_array[-1] <= sphase[0][-1]):
        ph_nn = interpolate.interp1d(sphase[0], sphase[1],
                                     kind="nearest")(time_array)
        dt = time_array - \
            interpolate.interp1d(sphase[0], sphase[0], kind="nearest")(time_array)
    elif (time_array[0] < sphase[0][0])\
            or (sphase[0][-1] < time_array)[-1]:
        ph_nn = interpolate.interp1d(sphase[0],
                                     sphase[1],
                                     kind="nearest",
                                     fill_value='extrapolate')(time_array)
        dt = time_array - \
            interpolate.interp1d(sphase[0], sphase[0], kind="nearest", fill_value='extrapolate')(time_array)

    per_nn = spinperiod['y']

    sphInterp = np.fmod(ph_nn + 360. * dt / per_nn, 360.)
    sphInterp = np.fmod(sphInterp + 360., 360.)
    spinphase = {'x': time_array, 'y': sphInterp}
    output_dictionary['spinphase'] = spinphase

    # Interporate SGI-Z axis vector
    if reload:
        degap('erg_att_izras', dt=8., margin=0.5)
        degap('erg_att_izdec', dt=8., margin=0.5)
    ras = get_data('erg_att_izras')
    dec = get_data('erg_att_izdec')
    time0 = ras[0]
    ras = ras[1]
    dec = dec[1]
    ez = np.cos((90. - dec) * dtor)
    ex = np.sin((90. - dec) * dtor) * np.cos(ras * dtor)
    ey = np.sin((90. - dec) * dtor) * np.sin(ras * dtor)
    ex_interp = np.interp(time_array, time0, ex)
    ey_interp = np.interp(time_array, time0, ey)
    ez_interp = np.interp(time_array, time0, ez)
    sgiz_j2000 = {
        'x': time_array,
        'y': np.array([ex_interp, ey_interp, ez_interp]).T
    }
    output_dictionary['sgiz_j2000'] = sgiz_j2000

    # Interporate SGA-X axis vector
    if reload:
        degap('erg_att_gxras', dt=8., margin=0.5)
        degap('erg_att_gxdec', dt=8., margin=0.5)
    ras = get_data('erg_att_gxras')
    dec = get_data('erg_att_gxdec')
    time0 = ras[0]
    ras = ras[1]
    dec = dec[1]
    ez = np.cos((90. - dec) * dtor)
    ex = np.sin((90. - dec) * dtor) * np.cos(ras * dtor)
    ey = np.sin((90. - dec) * dtor) * np.sin(ras * dtor)
    ex_interp = np.interp(time_array, time0, ex)
    ey_interp = np.interp(time_array, time0, ey)
    ez_interp = np.interp(time_array, time0, ez)
    sgax_j2000 = {
        'x': time_array,
        'y': np.array([ex_interp, ey_interp, ez_interp]).T
    }
    output_dictionary['sgax_j2000'] = sgax_j2000

    # Interporate SGA-Z axis vector
    if reload:
        degap('erg_att_gzras', dt=8., margin=0.5)
        degap('erg_att_gzdec', dt=8., margin=0.5)
    ras = get_data('erg_att_gzras')
    dec = get_data('erg_att_gzdec')
    time0 = ras[0]
    ras = ras[1]
    dec = dec[1]
    ez = np.cos((90. - dec) * dtor)
    ex = np.sin((90. - dec) * dtor) * np.cos(ras * dtor)
    ey = np.sin((90. - dec) * dtor) * np.sin(ras * dtor)
    ex_interp = np.interp(time_array, time0, ex)
    ey_interp = np.interp(time_array, time0, ey)
    ez_interp = np.interp(time_array, time0, ez)
    sgaz_j2000 = {
        'x': time_array,
        'y': np.array([ex_interp, ey_interp, ez_interp]).T
    }
    output_dictionary['sgaz_j2000'] = sgaz_j2000

    # Derive the other three axes (SGA-Y, SGI-X, SGI-Y)
    sgay = tcrossp(output_dictionary['sgaz_j2000']['y'],
                   output_dictionary['sgax_j2000']['y'],
                   return_data=True)
    sgay_j2000 = {'x': time_array, 'y': sgay}
    output_dictionary['sgay_j2000'] = sgay_j2000

    sgiy = tcrossp(output_dictionary['sgiz_j2000']['y'],
                   output_dictionary['sgax_j2000']['y'],
                   return_data=True)
    sgiy_j2000 = {'x': time_array, 'y': sgiy}
    output_dictionary['sgiy_j2000'] = sgiy_j2000

    sgix = tcrossp(output_dictionary['sgiy_j2000']['y'],
                   output_dictionary['sgiz_j2000']['y'],
                   return_data=True)
    sgix_j2000 = {'x': time_array, 'y': sgix}
    output_dictionary['sgix_j2000'] = sgix_j2000

    return output_dictionary