def testBinary(self): ''' This tests the ability to open a file and correctly read the attributes and variables as well as properly reshape the arrays and remove unused dimensions. ''' # Open 2D file: f = gitm.GitmBin( os.path.join(self.pth, 'data', 'pybats_test', 'gitm_2D.bin')) # Check some critical attributes/values: self.assertEqual(self.nVars, f.attrs['nVars']) self.assertEqual(self.nLat, f.attrs['nLat']) self.assertEqual(self.vers, f.attrs['version']) self.assertEqual(self.time, f['time']) self.assertEqual(self.shape, f['Longitude'].shape) self.assertAlmostEqual(self.lat1, f['Latitude'][0, -1], 6) self.assertAlmostEqual(-1 * self.lat1, f['Latitude'][0, 0], 6)
def load_multiple_gitm_bin(filelist, magfile=None, *args, **kwargs): ''' Loads a list of GITM binary files into their own GitmBin data structures. The list may be an ascii file containing a list of files or a list object. A list of the data structures is returned. A 3DION or 3DMAG file may be specified for the entire list using the keyword arguement "magfile". Input: filelist = python list of file names or an ASCII file containing a list of filenames magfile = 3DMAG or 3DION file (default=None) ''' # import local packages from os import path outlist = list() if type(filelist) == str: func_name = "load_multiple_gitm_bin" fsize = path.getsize(filelist) if(fsize > 2.0e9): print func_name,"ERROR: File list size [",(fsize*1e-9),"GB > 2 GB]" return outlist elif(fsize == 0.0): print func_name, "ERROR: empty file list [", filelist, "]" return outlist # Read in the list of files f = open(filelist, "r") namelist = f.readlines() else: namelist = filelist for name in namelist: name = string.strip(name) outlist.append(gitm.GitmBin(name, magfile=magfile)) if outlist[-1].attrs['nAlt'] > 1: outlist[-1].calc_2dion() return(outlist)
def quickplot(infile, outfile, min, max, backfile): ''' Takes file name, infile, and creates a PNG plot thing. Yeah. ''' # Import shit. I needed a lot of shit this time. import numpy as np from spacepy.pybats import gitm import matplotlib.pyplot as plt from matplotlib.cm import get_cmap from matplotlib.ticker import ScalarFormatter, FormatStrFormatter # Convert lat lon from rad to degrees. p = 180.0 / np.pi f = plt.figure(figsize=(6, 7)) #make a fig. ax = f.add_subplot(111) #make an ax. # Open file. a = gitm.GitmBin(infile) t = a['Temperature'][:, :, :] sz = t.shape c = sz[0] / 2.0 IsNotDiff = re.match(r'none', backfile) if IsNotDiff: diff = np.log10(a['Rho'][c, :, :]) else: base = gitm.GitmBin(backfile) diff = 100 * (a['Rho'][c, :, :] - base['Rho'][c, :, :]) / base['Rho'][c, :, :] diff[diff > max] = max diff[diff < min] = min # Create the contour for an altitude slice and call it 'cnt'. # The 61 is the number of contours; you could use a vector of values to set # levels manually if you wish. get_cmap accepts any of the color map names # from the colormap demo pic from the Matplotlib gallery; adding '_r' # reverses the colormap. u = a['V!Dn!N (north)'][c, :, :] v = a['V!Dn!N (up)'][c, :, :] x = a['Latitude'][c, :, :] * p * 111.0 y = a['Altitude'][c, :, :] / 1000.0 if (min > -1.0e31 and max < 1.0e32): lev = (np.arange(61)) / 60.0 * (max - min) + min cnt = ax.contourf(x, y, diff, 61, cmap=get_cmap('RdBu_r'), levels=lev) else: cnt = ax.contourf(x, y, diff, 61, cmap=get_cmap('RdBu_r')) # Configure axis. ax.set_xlabel('Latitude (in km)') ax.set_ylabel('Altitude (km)') if IsNotDiff: ax.set_title(r'Rho at longitude=%5.2f$^{\circ}$' % (a['Longitude'][c, 0, 0] * p)) else: ax.set_title(r'Pecent Diff of Rho at longitude=%5.2f$^{\circ}$' % (a['Longitude'][c, 0, 0] * p)) f.suptitle('File=%s' % (a.attrs['file'])) q = plt.quiver(x, y, u, v, angles='xy', scale=5000, color='b') # Add a colorbar and set the tick format to exponential notation. cb = plt.colorbar(cnt) cb.formatter = FormatStrFormatter('%7.2E') cb.update_ticks() # Add the quivers. #ax.quiver(a['Longitude'][:,:,0]*p, p*a['Latitude'][:,:,0], # a['V!Dn!N (east)'][:,:,0],a['V!Dn!N (north)'][:,:,0]) #f.savefig('filename.png') if outfile[-4] != '.png': outfile += '.png' f.savefig(outfile) # Draw to screen. if plt.isinteractive(): plt.draw() #In interactive mode, you just "draw". #else: # W/o interactive mode, "show" stops the user from typing more # at the terminal until plots are drawn. #plt.show() plt.close(f)
Note that as pybats.gitm is more developed, a plot like this should be made using syntax like, >>>a=gitm.GitmBin('filename') >>>a.add_alt_slice(0, 'Rho', add_cbar=True) That's how most pybats stuff works right now. ''' # Import shit. I needed a lot of shit this time. import numpy as np from spacepy.pybats import gitm import matplotlib.pyplot as plt from matplotlib.cm import get_cmap from matplotlib.ticker import ScalarFormatter, FormatStrFormatter # Open file. a=gitm.GitmBin('./3DALL_t061213_000000.bin') # Make contour of rho at lowest altitude (index 0). # Convert lat lon from rad to degrees. p=180.0/np.pi f=plt.figure() #make a fig. ax=f.add_subplot(111) #make an ax. # Create the contour for an altitude slice and call it 'cnt' (no jokes, please.) # The '61' is the number of contours; you could use a vector of values to set # levels manually if you wish. get_cmap accepts any of the color map names # from the colormap demo pic from the Matplotlib gallery; adding '_r' # reverses the colormap. cnt=ax.contourf(a['Longitude'][:,:,0]*p, p*a['Latitude'][:,:,0], a['Rho'][:,:,0], 61, cmap=get_cmap('Spectral_r'))