def testReadZip(self): from spacepy.pybats import rim # Open file: iono = rim.Iono( os.path.join(self.pth, 'data', 'pybats_test', 'it000321_104510_000.idl.gz'))
def testIonoCalc(self): '''Test calculations made by rim.Iono objects.''' from spacepy.pybats import rim iono = rim.Iono( os.path.join(spacepy_testing.datadir, 'pybats_test', 'it000321_104510_000.idl.gz')) iono.calc_I() for key in self.knownI: self.assertAlmostEqual(self.knownI[key], iono[key])
def testAddCont(self): from spacepy.pybats import rim import matplotlib as mpl import matplotlib.pyplot as plt iono = rim.Iono( os.path.join(spacepy_testing.datadir, 'pybats_test', 'it000321_104510_000.idl.gz')) out = iono.add_cont('n_jr', add_cbar=True) self.assertTrue(isinstance(out[0], plt.Figure)) self.assertTrue(isinstance(out[1], plt.Axes)) self.assertTrue(isinstance(out[2], mpl.contour.QuadContourSet)) self.assertTrue(isinstance(out[3], mpl.colorbar.Colorbar))
def testReadAscii(self): import gzip from os import remove from shutil import copyfileobj from spacepy.pybats import rim # Unzip file and create a copy of it: name_in = 'data/pybats_test/it000321_104510_000.idl.gz' name_out = name_in[:-3] with gzip.open(name_in, 'rb') as f_in, open(name_out, 'wb') as f_out: copyfileobj(f_in, f_out) # Test file: iono = rim.Iono(name_out) # Remove temp file: remove(name_out)
def calc_I_swmf(fname): """ See string for calc_I in SpacePy's pybats.rim """ # Read the IE data file data = rim.Iono(fname) data['time'] = data.attrs['time'] # Compute dLon and dLat data.dlon = data['n_psi' ][0,3] - data['n_psi' ][0,2] data.dlat = data['n_theta'][3,0] - data['n_theta'][2,0] # Calculate some physically meaningful values/units units = 1E-6*1E-6 # micro amps to amps, amps to MegaAmps R = (6371.0+110.0)*1000.0 # Radius of Earth + iono altitude dTheta = np.pi*data.dlat/180. dPhi = np.pi*data.dlon/180. # -----NORTHERN HEMISPHERE----- # Get relevant values: colat = data['n_theta']*np.pi/180. integrand = data['n_jr']*np.sin(colat)*dTheta*dPhi # Get locations of "up" and "down" loc_up = data['n_jr']>0 loc_do = data['n_jr']<0 data['n_I'] = units*R**2 * np.sum(integrand) data['n_Iup'] = units*R**2 * np.sum(integrand[loc_up]) data['n_Idown'] = units*R**2 * np.sum(integrand[loc_do]) data['n_Itotal']= units*R**2 * np.sum(np.abs(integrand)) * 0.5 # -----SOUTHERN HEMISPHERE----- # Get relevant values: colat = data['s_theta']*np.pi/180. integrand = data['s_jr']*np.sin(colat)*dTheta*dPhi # Get locations of "up" and "down" loc_up = data['s_jr']>0 loc_do = data['s_jr']<0 data['s_I'] = units*R**2 * np.sum(integrand) data['s_Iup'] = units*R**2 * np.sum(integrand[loc_up]) data['s_Idown'] = units*R**2 * np.sum(integrand[loc_do]) data['s_Itotal']= units*R**2 * np.sum(np.abs(integrand)) * 0.5 return data['n_Itotal'], data['s_Itotal']
def testReadAscii(self): import gzip from os import remove from shutil import copyfileobj from spacepy.pybats import rim try: # Unzip file and create a copy of it: name_in = os.path.join(self.pth, 'data', 'pybats_test', 'it000321_104510_000.idl.gz') name_out = name_in[:-3] with gzip.open(name_in, 'rb') as f_in, open(name_out, 'wb') as f_out: copyfileobj(f_in, f_out) # Test file: iono = rim.Iono(name_out) finally: # Remove temp file: remove(name_out)
def test_rotation(): ''' ''' import matplotlib.pyplot as plt # An example file to work with: ie = rim.Iono('Wam_20150316/IE/it150317_142400_000.idl') # Rotate file: rotate(ie) # Save NetCDF: create_netcdf(ie) # Get maximum field: z = 0 for x in 'nev': z = max(z, np.abs(ie['n_e' + x]).max()) # Create a big plot: fig = plt.figure() ie.add_cont('n_phi', add_cbar=True, target=fig, loc=121) ie.add_cont('n_jr', add_cbar=True, target=fig, loc=122) fig.suptitle(ie.attrs['time']) fig = plt.figure(figsize=(11, 8)) ie.add_cont('n_ex', target=fig, loc=231, maxz=z, add_cbar=True) ie.add_cont('n_ey', target=fig, loc=232, maxz=z, add_cbar=True) ie.add_cont('n_ez', target=fig, loc=233, maxz=z, add_cbar=True) ie.add_cont('n_en', target=fig, loc=234, maxz=z, add_cbar=True) ie.add_cont('n_ee', target=fig, loc=235, maxz=z, add_cbar=True) ie.add_cont('n_ev', target=fig, loc=236, maxz=z, add_cbar=True) # Create moar plots: plot_rotated(ie) return ie
def testReadZip(self): from spacepy.pybats import rim # Open file: iono = rim.Iono('data/pybats_test/it000321_104510_000.idl.gz')
def testReadWrapped(self): '''Test reading files where entries are wrapped over multiple lines.''' from spacepy.pybats import rim iono = rim.Iono( os.path.join(spacepy_testing.datadir, 'pybats_test', 'it_wrapped.idl.gz'))
def create_figure(iefile): # Open IE file, calculate azimuthal current: ie = rim.Iono(iefile) ie.calc_j() ie['n_jphi'] /= 1000. ie['s_jphi'] /= 1000. # Create figure: fig = plt.figure(figsize=(10, 10)) fig.subplots_adjust(left=.08, bottom=.052, top=.95, right=.962, wspace=.217, hspace=.23) # Set common keyword arguments: kwargs = {'max_colat': args.colat, 'target': fig, 'add_cbar': True} # Add IE plots: # FACs: with plt.style.context('default'): zmax = max(np.abs(ie['n_jr']).max(), np.abs(ie['s_jr']).max()) ie.add_cont('n_jr', loc=431, **kwargs) ie.add_cont('s_jr', loc=434, label='', **kwargs) # J_phi: zmax = max(np.abs(ie['n_jphi']).max(), np.abs(ie['s_jphi']).max()) ie.add_cont('n_jphi', loc=432, **kwargs) ie.add_cont('s_jphi', loc=435, label='', **kwargs) # J_phi: zmax = max(np.abs(ie['n_phi']).max(), np.abs(ie['s_phi']).max()) ie.add_cont('n_phi', loc=433, **kwargs) ie.add_cont('s_phi', loc=436, label='', **kwargs) a4 = fig.add_subplot(413) a5 = fig.add_subplot(414, sharex=a4) # Add IMF data: a4.plot(imf['time'], imf['bz'], color=bzcolor, lw=1.25) a4.plot(imf['time'], imf['by'], color=bycolor, lw=1.25) a4.hlines(0, imf['time'][0], imf['time'][-1], color='k', linestyles='dashed') a4.legend(['B$_Z$', 'B$_Y$'], loc='upper left') a4.set_ylabel('IMF ($nT$)') # Add Pdyn: a5.plot(imf['time'], imf['pram'], color=pdcolor) a5.grid(False, axis='y') a5.set_ylabel('P$_{dyn}$ ($nPa$)') applySmartTimeTicks(a5, imf['time'], dolabel=True) a4.vlines(ie.attrs['time'], a4.get_ylim()[0], a4.get_ylim()[1], colors='k', linestyles='dashed', linewidths=2) lim = a5.get_ylim() a5.vlines(ie.attrs['time'], lim[0], lim[1], colors='k', linestyles='dashed', linewidths=2) fig.savefig(outdir + f'iono_t{ie.attrs["time"]:%Y%m%d_%H%M%S}.png') return fig
def swmf_read(fname, debug=False): """ This function reads in an IDL file to help plot SWMF data on a polar plot based on Latitude and magnetic local time (MLT) data. For further information, please refer to the SWMF Manual and/or spacepy.pybats.rim manual for data structures. Input: ------ fname SWMF event filename for a given time Output: ------- dict Dictionary containing latitude, longitude and FACs for North and North Hemispheres. """ # Retrieve data from file !!! data = rim.Iono(fname) # Get size of array, and elements in dictionary if debug: print(data.attrs['ntheta'], data.attrs['nphi']) print(data.keys()) # Store everything in a separate dictionary swmf_data = {} swmf_data['n_MLT'] = data['n_psi'] * np.pi / 180.0 + np.pi / 2. swmf_data['n_Lat'] = data['n_theta'] swmf_data['n_Jr'] = np.array(data['n_jr']) swmf_data['s_MLT'] = data['s_psi'] * np.pi / 180.0 + np.pi / 2. swmf_data['s_Lat'] = 180. - data['s_theta'] swmf_data['s_Jr'] = np.array(data['s_jr']) if debug: # Test Plot for SWMF ax1 = plt.subplot(121, projection='polar') # Polar Plot ax2 = plt.subplot(122, projection='polar') # Polar Plot ax1.contourf(swmf_data['n_MLT'], swmf_data['n_Lat'], swmf_data['n_Jr'], cmap='bwr') ax2.contourf(swmf_data['s_MLT'], swmf_data['s_Lat'], swmf_data['s_Jr'], cmap='bwr') plt.show() return swmf_data # Return plottable data to user... #============================================================================= # MAIN FUNCTION: # t_date = dt.datetime(2011, 9, 26, 15, 0, 0) # t = t_date.timetuple() # Stripped the numbers into a timetuple # t_end = dt.datetime(2011, 9, 26, 15, 4, 0) # # The year formatting is done differently because SWMF IE files start with the # # last two digits of the year # while(t_date < t_end): # t = t_date.timetuple() # Stripped the numbers into a timetuple # filename = (('./SWMF-MAGNIT/Sept2011_Event_CUSIA/' + # 'it{0}{1:0=2d}{2:0=2d}_{3:0=2d}{4:0=2d}{5:0=2d}_000.idl') # .format(str(t[0])[2:4], t[1], t[2], t[3], t[4], t[5])) # print(filename) # SWMF_Data = swmf_read(filename)#, debug=True) # t_date = t_date + dt.timedelta(minutes = 2) # # Test Plot for SWMF # ax1 = plt.subplot(121, projection = 'polar') # Polar Plot # ax2 = plt.subplot(122, projection = 'polar') # Polar Plot # ax1.contourf(SWMF_Data['n_MLT'], SWMF_Data['n_Lat'], SWMF_Data['n_Jr'], # cmap='bwr') # ax2.contourf(SWMF_Data['s_MLT'], SWMF_Data['s_Lat'], SWMF_Data['s_Jr'], # cmap='bwr') # plt.show()