def test_time_string(self): """Test time_string function.""" self.assertTrue(time_string(fmt='%Y-%m-%d') == datetime.now().strftime('%Y-%m-%d')) self.assertTrue(time_string(1450181243.767, fmt='%Y-%m-%d') == '2015-12-15') self.assertTrue(time_string(1450181243.767, fmt='%Y-%m-%d/%H:%M:%S') == '2015-12-15/12:07:23') self.assertTrue(time_string(1450181243.767) == '2015-12-15 12:07:23.767000') self.assertTrue(time_string([1450181243.767, 1450181263.767]) == ['2015-12-15 12:07:23.767000', '2015-12-15 12:07:43.767000'])
def test_time_string(self): """Test time_string function.""" t0 = time_string(None) t1 = time_datetime(None) t2 = time_datetime(1000000000.0) t3 = time_string_one(None) print("Time_string functions test:", t0, t1, t2, t3) self.assertTrue(time_string(1450181243.767) == '2015-12-15 12:07:23.767000') self.assertTrue(time_string([1450181243.767, 1450181263.767]) == ['2015-12-15 12:07:23.767000', '2015-12-15 12:07:43.767000'])
def test_time_double(self): """Test time_double function.""" self.assertTrue(time_string(time_double(), fmt='%Y-%m-%d') == datetime.now().strftime('%Y-%m-%d')) self.assertTrue(time_double('2015-12-15/12:00') == 1450180800.0000000) self.assertTrue(time_double('2015-12-15/12') == 1450180800.0000000) #self.assertTrue(time_double('2015-12-15/6') == 1450159200.0000000) #this one doesn't work self.assertTrue(time_double('2015-12-15/6:00') == 1450159200.0000000) self.assertTrue(time_double('2015-12-15/06:00') == 1450159200.0000000) self.assertTrue(time_double('2015-12-15') == 1450137600.0000000) self.assertTrue(time_double('2015 12 15') == 1450137600.0000000) self.assertTrue(time_double('2015-12') == 1448928000.0000000) self.assertTrue(time_double('2015') == 1420070400.0000000) self.assertTrue(time_double('2015-12-15 12:07:23.767000') == 1450181243.767) self.assertTrue(time_double(['2015-12-15 12:07:23.767000', '2015-12-15 12:07:43.767000']) == [1450181243.767, 1450181263.767])
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('_')))
def cl_format_time(s): """Return a string formated for Cluster web services.""" # Date format: YYYY-MM-DDThh:mm:ssZ r = time_string(time_double(s), "%Y-%m-%dT%H:%M:%SZ") return r
from pytplot import get_data, store_data # extract the data from a tplot variable times, data = get_data('mms1_fgm_b_gsm_srvy_l2') # e.g., # >>> times[0] # 1444953613.330852 # >>> data[0] # array([ 8580.49 , 7339.21 , 6250.034, 12905.493], dtype=float32) # convert the unix time to a string from pyspedas.utilities.time_string import time_string print(time_string(1444953613.330852)) from pyspedas.utilities.time_double import time_double print(time_double('2015-10-16 00:00:13.330852')) # create new tplot variables store_data('b_gsm_vec', data={'x': times, 'y': data[:, 0:3]}) # B-field vector store_data('b_gsm_mag', data={ 'x': times, 'y': data[:, 3] }) # B-field magnitude tplot(['b_gsm_mag', 'b_gsm_vec'])
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