Exemplo n.º 1
0
 def test_get_Lstar_OPQuiet_multi(self):
     """Test Lstar on OPQ forcing multiprocess"""
     cpu_actual = spacepy.config['ncpus']
     # To trigger a worker pool, number of calcs must be
     # more than double number of cpus
     spacepy.config['ncpus'] = 4
     try:
         ticks = spacepy.time.tickrange(self.ticks.ISO[0], self.ticks.ISO[-1], 1/1440.)
         ncalc = len(ticks)  # Forced 10 times in test data range
         loci = spacepy.coordinates.Coords([[nc-4, 6-nc, 0] for nc in range(ncalc)], 'GEO', 'car')
         omnivals = spacepy.omni.get_omni(ticks, dbase='Test')
         expected = {'Lstar': array([[6.84698], [5.58814], [4.35608],
                                     [3.13613], [1.97344], [1.41439],
                                     [2.05375], [3.19979], [4.35920],
                                     [5.38242]]),
                     'Lm': array([[7.61427481], [6.13826804], [4.65907084],
                                  [3.21519241], [1.97225109], [1.41105356],
                                  [2.05626165], [3.28414042], [4.6531115 ],
                                  [5.92800457]])
                     }
         # OPQ won't use the OMNI, but if they're passed in
         # the code still processes them, so answers should be identical
         actuali= ib.get_Lstar(ticks, loci, [90], extMag="OPQUIET", omnivals=omnivals)
         actualn = ib.get_Lstar(ticks, loci, [90], extMag="OPQUIET", omnivals=None)
     finally:
         spacepy.config['ncpus'] = cpu_actual
     # Check that results are as expected
     numpy.testing.assert_almost_equal(expected['Lstar'], actuali['Lstar'], decimal=5)
     numpy.testing.assert_almost_equal(expected['Lm'], actuali['Lm'], decimal=5)
     numpy.testing.assert_almost_equal(expected['Lstar'], actualn['Lstar'], decimal=5)
     numpy.testing.assert_almost_equal(expected['Lm'], actualn['Lm'], decimal=5)
Exemplo n.º 2
0
 def test_get_Lstar_TooManyPA(self):
     """test OP-Quiet with too many pitch angles"""
     with self.assertRaises(ValueError) as cm:
         ib.get_Lstar(
             self.ticks, self.loci, numpy.arange(5, 180, 5),
             extMag="OPQUIET", omnivals=self.omnivals)
     self.assertEqual('Too many pitch angles requested; 25 is maximum.',
                      str(cm.exception))
Exemplo n.º 3
0
def calc_Lvals(year):

    counts_file = "/home/wyatt/Documents/SAMPEX/bounce/correlation/data/accepted_" + str(
        year) + ".csv"
    times = pd.read_csv(counts_file,
                        header=None,
                        names=["Burst", "Time"],
                        usecols=[0, 1])
    times['Time'] = pd.to_datetime(times['Time'])
    times = times.set_index('Burst')
    irb_L_list = []
    samp_L_list = []
    indices = times.index.drop_duplicates()
    for ind in indices:
        print(ind)
        start = times.loc[ind].iloc[0]["Time"]
        end = times.loc[ind].iloc[-1]["Time"]
        dataObj = sp.OrbitData(date=start)
        orbitInfo = dataObj.read_time_range(
            pd.to_datetime(start),
            pd.to_datetime(end),
            parameters=['GEI_X', 'GEI_Y', 'GEI_Z', 'L_Shell'])

        X = (orbitInfo['GEI_X'].to_numpy() / Re)[0]
        Y = (orbitInfo['GEI_Y'].to_numpy() / Re)[0]
        Z = (orbitInfo['GEI_Z'].to_numpy() / Re)[0]
        position = np.array([X, Y, Z])
        ticks = Ticktock(start)
        coords = spc.Coords(position, 'GEI', 'car')
        irb_Lstar = irb.get_Lstar(ticks, coords, extMag=magField)
        irb_Lstar = irb_Lstar['Lm'][0]
        samp_Lstar = orbitInfo['L_Shell'].to_numpy()[0]
        irb_L_list.append(irb_Lstar[0])
        samp_L_list.append(samp_Lstar)
    return irb_L_list, samp_L_list
Exemplo n.º 4
0
    def _bounce_period(self, times, energy=1):
        """
        calculates electron bounce period at edge of loss cone
        energy in MeV
        """
        start = times[0]
        end = times[-1]
        dataObj = sp.OrbitData(date=start)
        orbitInfo = dataObj.read_time_range(
            pd.to_datetime(start),
            pd.to_datetime(end),
            parameters=['GEI_X', 'GEI_Y', 'GEI_Z', 'L_Shell', 'GEO_Lat'])

        X = (orbitInfo['GEI_X'].to_numpy() / self.Re)[0]
        Y = (orbitInfo['GEI_Y'].to_numpy() / self.Re)[0]
        Z = (orbitInfo['GEI_Z'].to_numpy() / self.Re)[0]
        position = np.array([X, Y, Z])
        ticks = Ticktock(times[0])
        coords = spc.Coords(position, 'GEI', 'car')

        #something bad is happening with IRBEM, the L values are crazy for a lot of
        #these places, so for now I'll use sampex's provided L vals
        Lstar = irb.get_Lstar(ticks, coords, extMag='0')
        Lstar = abs(Lstar['Lm'][0])

        # Lstar = orbitInfo['L_Shell'].to_numpy()[0]
        self.Lstar = Lstar
        loss_cone = self._find_loss_cone(coords, ticks)  #in degrees
        period = 5.62 * 10**(-2) * Lstar / np.sqrt(energy) * (
            1 - .43 * np.sin(np.deg2rad(loss_cone)))
        return period[0]
Exemplo n.º 5
0
 def test_get_Lstar_OPQuiet(self):
     # test OP-Quiet
     expected = {'Xj': array([[ 0.001051], [ 0.002722]]),
         'Lstar': array([[ 3.029621], [ 2.059631]]),
         'Blocal': array([ 1019.052401, 3467.52999]),
         'Lm': array([[ 3.091352], [ 2.056261]]),
         'Bmin': array([ 1018.669701,  3459.500966 ]),
         'MLT': array([ 11.97159175,  12.13313906])}
     actual = ib.get_Lstar(self.ticks, self.loci, [90], extMag="OPQUIET", omnivals=self.omnivals)
     for key in expected.keys():
         numpy.testing.assert_almost_equal(expected[key], actual[key], decimal=6)
Exemplo n.º 6
0
 def test_get_Lstar_T05(self):
     # test T05
     expected = {'Xj': array([[ 0.266114], [ 0.186008]]),
                 'Lstar': array([[ 3.015461], [ 2.043043]]),
                 'Bmirr': array([[ 1150.670441], [ 3895.810805]]),
                 'Lm': array([[ 3.087026], [ 2.059734]]),
                 'Bmin': array([ 1015.468031,  3432.146907]),
                 'MLT': array([ 11.97159175,  12.13313906])}
     actual = ib.get_Lstar(self.ticks, self.loci, [70], extMag='T05', omnivals=self.omnivals)
     for key in expected:
         numpy.testing.assert_almost_equal(expected[key], actual[key], decimal=6)
Exemplo n.º 7
0
 def test_get_Lstar_T01(self):
     # test T01STORM
     expected = {'Xj': array([[ 0.000403], [ 0.00269002]]),
         'Lstar': array([[ 3.025887], [ 2.054195]]),
         'Bmirr': array([[ 1031.008992], [ 3451.98937]]),
         'Lm': array([[ 3.079151], [ 2.059326]]),
         'Bmin': array([ 1030.456337,  3444.077016 ]),
         'MLT': array([ 11.97159175,  12.13313906])}
     actual = ib.get_Lstar(self.ticks, self.loci, [90], omnivals=self.omnivals)
     for key in expected.keys():
         numpy.testing.assert_almost_equal(expected[key], actual[key], decimal=6)
Exemplo n.º 8
0
 def test_get_Lstar_T05(self):
     # test T05
     expected = {'Xj': array([[ 0.26613994], [ 0.18600614]]),
                 'Lstar': array([[ 3.01546295], [ 2.04304142]]),
                 'Bmirr': array([[ 1150.72621313], [ 3895.75273416]]), 
                 'Lm': array([[ 3.08699012], [ 2.05974291]]),
                 'Bmin': array([ 1015.51219038,  3432.08956401]),
                 'MLT': array([ 11.97159175,  12.13313906])}
     actual = ib.get_Lstar(self.ticks, self.loci, [70], extMag='T05', omnivals=self.omnivals)
     for key in expected:
         numpy.testing.assert_almost_equal(expected[key], actual[key], decimal=6)
Exemplo n.º 9
0
 def test_get_Lstar_T01(self):
     # test T01STORM
     expected = {'Xj': array([[ 0.00040101], [ 0.00269002]]),
         'Lstar': array([[ 3.02588835], [ 2.0523954]]), 
         'Bmirr': array([[ 1031.05446706], [ 3451.97910165]]),
         'Lm': array([[ 3.07910515], [ 2.05932846]]),
         'Bmin': array([ 1030.50271503,  3444.0670669 ]),
         'MLT': array([ 11.97159175,  12.13313906])}    
     actual = ib.get_Lstar(self.ticks, self.loci, [90], omnivals=self.omnivals)
     for key in expected.keys():
         numpy.testing.assert_almost_equal(expected[key], actual[key], decimal=6)
Exemplo n.º 10
0
 def test_get_Lstar_T01(self):
     # test T01STORM
     expected = {
         'Xj': array([[0.00040101], [0.00269002]]),
         'Lstar': array([[3.02588835], [2.0523954]]),
         'Bmirr': array([[1031.05446706], [3451.97910165]]),
         'Lm': array([[3.07910515], [2.05932846]]),
         'Bmin': array([1030.50271503, 3444.0670669]),
         'MLT': array([11.97159175, 12.13313906])
     }
     actual = ib.get_Lstar(self.ticks,
                           self.loci, [90],
                           omnivals=self.omnivals)
     for key in expected.keys():
         numpy.testing.assert_almost_equal(expected[key],
                                           actual[key],
                                           decimal=6)
Exemplo n.º 11
0
def ComputeLshell(inTime, c1, c2, c3, inCoordName, inCoordType):
    """
    This function uses spacepy to compute L shell and MLT from given time and position values.
    It uses the IRBEM library to do the calculations.
    
    INPUTS:
    inTime:       array:  time in UTC timestamp
    c1:           array:  x, alt
    c2:           array:  y, lat
    c3:           array:  z, lon
    inCoordName:  string: GDZ, GEO, GSM, GSE, SM, GEI, MAG, SPH, RLL
    inCoordType:  string: car, sph
    
    OUTPUT:
    c1:       array:  Lm
    c2:       array:  Lstar
    c3:       array:  MLT
    """

    # Start timer
    tic = time.perf_counter()
    #print("Starting ...")

    #pack position arrays in desired format
    sat_track = [[c1[i], c2[i], c3[i]] for i in range(len(c1))]

    #convert utc_ts to spacepy time strings
    @np.vectorize
    def ts_to_spacepystr(ts):
        return dt.strftime(dt.utcfromtimestamp(int(ts)), '%Y-%m-%dT%H:%M:%S')

    #perform coordinate conversion
    cvals = Coords(sat_track, inCoordName, inCoordType)
    tt = Ticktock(ts_to_spacepystr(inTime), 'ISO')

    qq = get_Lstar(tt, cvals)
    Lm = abs(np.reshape(qq['Lm'], -1))
    Lstar = abs(np.reshape(qq['Lstar'], -1))
    MLT = qq['MLT']

    toc = time.perf_counter()
    print('Computed Lm,lstar,MLT in', "{:0.4f}".format(toc - tic),
          'seconds for', len(inTime), ' positions.')

    return Lm, Lstar, MLT
Exemplo n.º 12
0
 def test_get_Lstar_T05(self):
     # test T05
     expected = {
         'Xj': array([[0.26613994], [0.18600614]]),
         'Lstar': array([[3.01546295], [2.04304142]]),
         'Bmirr': array([[1150.72621313], [3895.75273416]]),
         'Lm': array([[3.08699012], [2.05974291]]),
         'Bmin': array([1015.51219038, 3432.08956401]),
         'MLT': array([11.97159175, 12.13313906])
     }
     actual = ib.get_Lstar(self.ticks,
                           self.loci, [70],
                           extMag='T05',
                           omnivals=self.omnivals)
     for key in expected:
         numpy.testing.assert_almost_equal(expected[key],
                                           actual[key],
                                           decimal=6)