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')
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')
# 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}' ] if args.verbose: print(f'\tSaving to disk...') out.write() # Save orbit for plotting: orbits.append(out) # Remove CDF files if not args.save: if args.verbose: print(f'\tRemoving {filename}...') os.remove(filename) # Write out the SWMF command to include satellites: with open('sats.include', 'w') as f: f.write(f'#SATELLITE\n{len(orbits)}\t\t\tnSatellite\n') for orb in orbits: f.write('MHD RAY date time\tStringSatellite\n') f.write('-1\t\t\tDnOutput\n')
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()