def readascii(self): ''' Read an ascii ".idl" output file and load the contents into the object. ''' from sys import version_info import re import datetime as dt from numpy import zeros, reshape import gzip # slurp entire file. if self.attrs['file'][-3:] == '.gz': try: infile = gzip.open(self.attrs['file'], 'rt') except ValueError: #Python2.7 (Windows) compatibility infile = gzip.open(self.attrs['file']) else: infile = open(self.attrs['file'], 'r') raw = infile.readlines() infile.close() # Parse header title = raw[raw.index('TITLE\n') + 1] self.attrs['title'] = title[title.index('"') + 1:title.rindex('"')] i = raw.index('NUMERICAL VALUES\n') self.attrs['nvars'] = int(raw[i + 1].split()[0]) self.attrs['ntheta'] = int(raw[i + 2].split()[0]) self.attrs['nphi'] = int(raw[i + 3].split()[0]) # Convenience: nphi, ntheta = self.attrs['nphi'], self.attrs['ntheta'] i = raw.index('TIME\n') self.attrs['time'] = dt.datetime( int(raw[i + 1].split()[0]), #year int(raw[i + 2].split()[0]), #month int(raw[i + 3].split()[0]), #day int(raw[i + 4].split()[0]), #hour int(raw[i + 5].split()[0]), #min int(raw[i + 6].split()[0]), #sec int(raw[i + 7].split()[0]) * 1000 #microsec ) i = raw.index('SIMULATION\n') self.attrs['iter'] = int(raw[i + 1].split()[0]) self.attrs['simtime'] = float(raw[i + 2].split()[0]) i = raw.index('DIPOLE TILT\n') self.tilt = zeros(2) self.tilt[0] = float(raw[i + 1].split()[0]) self.tilt[1] = float(raw[i + 2].split()[0]) i = raw.index('VARIABLE LIST\n') namevar = [] units = {} for j in range(i + 1, i + self.attrs['nvars'] + 1): match = re.match(r'\s*\d+\s+([\w\s\W]+)\[([\w\s\W]+)\]', raw[j]) if match: name = (match.group(1).strip()).lower() namevar.append(name) units[name] = match.group(2).strip() else: raise ValueError('Could not parse %s' % raw[j]) ### Read all data ### # Create data arrays nPts = self.attrs['ntheta'] * self.attrs['nphi'] for key in namevar: self['n_' + key] = dmarray(zeros(nPts), {'units': units[key]}) self['s_' + key] = dmarray(zeros(nPts), {'units': units[key]}) i = raw.index('BEGIN NORTHERN HEMISPHERE\n') + 1 # Some compilers insert line breaks automatically when fortran format # string is not adequately specified. Let's see if that's the # case here: how many lines does it take to cover all variables? nvars, nvarline, nwrap = len(namevar), 0, 0 while nvarline < nvars: nvarline += len(raw[i + nwrap].split()) nwrap += 1 # Fill data arrays: for j in range(nPts): # Build list of variables; accounting for line-wrapping: parts = [] iLine = i + j * nwrap for iwrap in range(nwrap): parts += raw[iLine + iwrap].split() for k in range(self.attrs['nvars']): self['n_' + namevar[k]][j] = parts[k] i = raw.index('BEGIN SOUTHERN HEMISPHERE\n') + 1 for j in range(nPts): parts = [] iLine = i + j * nwrap for iwrap in range(nwrap): parts += raw[iLine + iwrap].split() for k in range(self.attrs['nvars']): self['s_' + namevar[k]][j] = parts[k] # Create 2-D arrays. for key in namevar: nkey, skey = 'n_' + key, 's_' + key self[nkey] = reshape(self[nkey], (ntheta, nphi), 'F') self[skey] = reshape(self[skey], (ntheta, nphi), 'F') # Some extra grid info: self.dlon = self['n_psi'][0, 3] - self['n_psi'][0, 2] self.dlat = self['n_theta'][3, 0] - self['n_theta'][2, 0]
def readascii(self): ''' Read an ascii ".idl" output file and load the contents into the object. ''' from sys import version_info import re import datetime as dt from numpy import zeros, reshape import gzip # slurp entire file. if self.attrs['file'][-3:]=='.gz': infile=gzip.open(self.attrs['file'],'rt') else: infile = open(self.attrs['file'], 'r') raw = infile.readlines() infile.close() # Parse header title = raw[raw.index('TITLE\n')+1] self.attrs['title'] = title[title.index('"')+1:title.rindex('"')] i = raw.index('NUMERICAL VALUES\n') self.attrs['nvars'] = int(raw[i+1].split()[0]) self.attrs['ntheta']= int(raw[i+2].split()[0]) self.attrs['nphi'] = int(raw[i+3].split()[0]) # Convenience: nphi, ntheta = self.attrs['nphi'], self.attrs['ntheta'] i = raw.index('TIME\n') self.attrs['time'] = dt.datetime( int(raw[i+1].split()[0]), #year int(raw[i+2].split()[0]), #month int(raw[i+3].split()[0]), #day int(raw[i+4].split()[0]), #hour int(raw[i+5].split()[0]), #min int(raw[i+6].split()[0]), #sec int(raw[i+7].split()[0])*1000 #microsec ) i = raw.index('SIMULATION\n') self.attrs['iter'] = int(raw[i+1].split()[0]) self.attrs['simtime'] = float(raw[i+2].split()[0]) i = raw.index('DIPOLE TILT\n') self.tilt = zeros(2) self.tilt[0] = float(raw[i+1].split()[0]) self.tilt[1] = float(raw[i+2].split()[0]) i = raw.index('VARIABLE LIST\n') namevar = [] units = {} for j in range(i+1,i+self.attrs['nvars']+1): match = re.match('\s*\d+\s+([\w\s\W]+)\[([\w\s\W]+)\]',raw[j]) if match: name = (match.group(1).strip()).lower() namevar.append(name) units[name] = match.group(2).strip() else: raise ValueError('Could not parse %s' % raw[j]) ### Read all data ### # Create data arrays nPts = self.attrs['ntheta']*self.attrs['nphi'] for key in namevar: self['n_'+key] = dmarray(zeros(nPts), {'units':units[key]}) self['s_'+key] = dmarray(zeros(nPts), {'units':units[key]}) i = raw.index('BEGIN NORTHERN HEMISPHERE\n')+1 # Fill data arrays for j, line in enumerate(raw[i:i+nPts]): parts = line.split() for k in range(self.attrs['nvars']): self['n_'+namevar[k]][j] = parts[k] i = raw.index('BEGIN SOUTHERN HEMISPHERE\n')+1 for j, line in enumerate(raw[i:i+nPts]): parts = line.split() for k in range(self.attrs['nvars']): self['s_'+namevar[k]][j] = parts[k] # Create 2-D arrays. for key in namevar: nkey, skey = 'n_'+key, 's_'+key self[nkey] = reshape(self[nkey], (ntheta, nphi), 'F') self[skey] = reshape(self[skey], (ntheta, nphi), 'F')
def readascii(self): ''' Read an ascii ".idl" output file and load the contents into the object. ''' import re import datetime as dt from numpy import zeros, reshape # slurp entire file. infile = open(self.attrs['file'], 'r') raw = infile.readlines() infile.close() # Parse header title = raw[raw.index('TITLE\n')+1] self.attrs['title'] = title[title.index('"')+1:title.rindex('"')] i = raw.index('NUMERICAL VALUES\n') self.attrs['nvars'] = int(raw[i+1].split()[0]) self.attrs['ntheta']= int(raw[i+2].split()[0]) self.attrs['nphi'] = int(raw[i+3].split()[0]) # Convenience: nphi, ntheta = self.attrs['nphi'], self.attrs['ntheta'] i = raw.index('TIME\n') self.attrs['time'] = dt.datetime( int(raw[i+1].split()[0]), #year int(raw[i+2].split()[0]), #month int(raw[i+3].split()[0]), #day int(raw[i+4].split()[0]), #hour int(raw[i+5].split()[0]), #min int(raw[i+6].split()[0]), #sec int(raw[i+7].split()[0])*1000 #microsec ) i = raw.index('SIMULATION\n') self.attrs['iter'] = int(raw[i+1].split()[0]) self.attrs['simtime'] = float(raw[i+2].split()[0]) i = raw.index('DIPOLE TILT\n') self.tilt = zeros(2) self.tilt[0] = float(raw[i+1].split()[0]) self.tilt[1] = float(raw[i+2].split()[0]) i = raw.index('VARIABLE LIST\n') namevar = [] units = {} for j in range(i+1,i+self.attrs['nvars']+1): match = re.match('\s*\d+\s+([\w\s\W]+)\[([\w\s\W]+)\]',raw[j]) if match: name = (match.group(1).strip()).lower() namevar.append(name) units[name] = match.group(2).strip() else: raise ValueError('Could not parse %s' % raw[j]) ### Read all data ### # Create data arrays nPts = self.attrs['ntheta']*self.attrs['nphi'] for key in namevar: self['n_'+key] = dmarray(zeros(nPts), {'units':units[key]}) self['s_'+key] = dmarray(zeros(nPts), {'units':units[key]}) i = raw.index('BEGIN NORTHERN HEMISPHERE\n')+1 # Fill data arrays for j, line in enumerate(raw[i:i+nPts]): parts = line.split() for k in range(self.attrs['nvars']): self['n_'+namevar[k]][j] = parts[k] i = raw.index('BEGIN SOUTHERN HEMISPHERE\n')+1 for j, line in enumerate(raw[i:i+nPts]): parts = line.split() for k in range(self.attrs['nvars']): self['s_'+namevar[k]][j] = parts[k] # Create 2-D arrays. for key in namevar: nkey, skey = 'n_'+key, 's_'+key self[nkey] = reshape(self[nkey], (ntheta, nphi), 'F') self[skey] = reshape(self[skey], (ntheta, nphi), 'F')