コード例 #1
0
    def testRead(self):
        from spacepy.pybats import SatOrbit
        from numpy.testing import assert_array_equal as assert_array

        sat = SatOrbit('data/pybats_test/testsat.dat')

        # Check header:
        self.assertEqual(sorted(sat.attrs['head']),
                         sorted(['test', 'header', 'values']))
        self.assertEqual(sat.attrs['coor'], 'SMG')

        # Check time and position:
        assert_array(sat['time'], self.time)
        assert_array(sat['xyz'], self.pos)
コード例 #2
0
    def testWrite(self):
        '''
        Create a file from scratch.
        '''
        from os import remove
        from spacepy.pybats import SatOrbit
        from numpy.testing import assert_array_equal as assert_array

        # New empty object:
        sat = SatOrbit()

        # Load in fake data:
        sat['time'] = self.time
        sat['xyz'] = self.pos

        # Add some info:
        sat.attrs['coor'] = 'SMG'
        sat.attrs['file'] = 'testsat.dat'
        sat.attrs['head'] = ['test', 'header', 'values']

        sat.write()
        import os
        os.listdir('.')

        #### Now, test the integrity of the file:
        sat = SatOrbit('./testsat.dat')

        # Check header:
        self.assertEqual(sorted(sat.attrs['head']),
                         sorted(['test', 'header', 'values']))
        self.assertEqual(sat.attrs['coor'], 'SMG')

        # Check time and position:
        assert_array(sat['time'], self.time)
        assert_array(sat['xyz'], self.pos)

        # Get rid of file:
        remove('./testsat.dat')
コード例 #3
0
    def testWrite(self):
        '''
        Create a file from scratch.
        '''
        from os import remove
        from spacepy.pybats import SatOrbit
        from numpy.testing import assert_array_equal as assert_array

        # New empty object:
        sat = SatOrbit()

        # Load in fake data:
        sat['time'] = self.time
        sat['xyz']  = self.pos

        # Add some info:
        sat.attrs['coor'] = 'SMG'
        sat.attrs['file'] = 'testsat.dat'
        sat.attrs['head'] = ['test','header','values']

        sat.write()
        import os
        os.listdir('.')

        #### Now, test the integrity of the file:
        sat = SatOrbit('./testsat.dat')

        # Check header:
        self.assertEqual(sorted(sat.attrs['head']),
                         sorted(['test','header','values']))
        self.assertEqual(sat.attrs['coor'], 'SMG')

        # Check time and position:
        assert_array(sat['time'], self.time)
        assert_array(sat['xyz'],  self.pos)

        # Get rid of file:
        remove('./testsat.dat')
コード例 #4
0
    # Perform coordinate conversion:
    if args.coord == sats[s]['coord']:
        xyz = cdf[sats[s]['var']][...]
    else:
        if args.verbose:
            print(f"\tRotating from {sats[s]['coord']} to {args.coord}...")
        xyz_file = Coords(cdf[sats[s]['var']][...],
                          sats[s]['coord'],
                          'car',
                          ticks=Ticktock(cdf[sats[s]['time']][...]))
        xyz = xyz_file.convert(args.coord, 'car').data

    # Convert to satellite orbit object:
    if args.verbose: print(f'\tConverting to SWMF input file...')
    out = SatOrbit()
    out['time'] = cdf[sats[s]['time']][...]
    out['xyz'] = xyz.transpose()

    # Convert units:
    if sats[s]['units'] == 'km':
        out['xyz'] /= 6371

    # Add attributes to file:
    out.attrs['file'] = f'{s}.sat'
    out.attrs['coor'] = sats[s]['coord']
    if out.attrs['coor'] == 'SM': out.attrs['coor'] = 'SMG'
    tnow = dt.datetime.now()
    out.attrs['head'] = [
        f'Created with swmf_orbit_fetch.py on ' + f'{tnow:%Y-%m-%d %H:%M%S}'
    ]
コード例 #5
0
    fns = glob.glob(os.path.join(datadir, '*{}*').format(sat))
    fns = sorted(fns)
    # read and concatenate position arrays
    with open(fns[0], 'r') as fh:
        data = fh.readlines()
    for fn in fns[1:]:
        with open(fn, 'r') as fh:
            data.extend(fh.readlines())
    # drop header lines
    data = [line.strip().split() for line in data if not line.startswith('#')]

    # grab times and positions
    parsetime = partial(dup.parse, ignoretz=True)
    tmp = np.asarray(data)
    time = tmp[:, 0]
    pos = tmp[:, 1:].astype(float)
    time = np.vectorize(parsetime)(time)
    rll_order = [2, 0, 1]  # reorder columns for coords
    geo_sph = spc.Coords(pos[:, rll_order], 'SPH', 'sph')
    geo_car = geo_sph.convert('SPH', 'car')

    # make and fill SatOrbit for writing
    satdata = SatOrbit()
    satdata['time'] = time
    satdata['xyz'] = geo_car.data.T  #SatOrbit is 3xN, not Nx3
    satdata.attrs['coor'] = 'GEO'
    outfname = os.path.join(os.path.abspath('..'), 'swmf_input',
                            'Event19890312', '{}.dat'.format(sat))
    satdata.attrs['file'] = outfname
    satdata.write()