def __init__(self, headingformat='A', indexformat='6F12.2', dataformat='6E12.3'): self.heading_reader = _dummy_reader() self.index_reader = ff.FortranRecordReader(indexformat) self.data_reader = ff.FortranRecordReader(dataformat)
def readVSOP(dataDir, pl): """ Read the periodic terms for a heliocentric ecliptical planet position from a VSOP87D.* file. Args: dataDir (str): Directory where the VSOP87D.* files are located. pl (int): Planet ID: 1-8 = Mercury - Neptune. Returns: tuple (float,float,float): Tuple containing (lonTerms, latTerms, radTerms): - lonTerms (float): Numpy array containing VSOP87D periodic terms for heliocentric ecliptical longitude. - latTerms (float): Numpy array containing VSOP87D periodic terms for heliocentric ecliptical latitude. - radTerms (float): Numpy array containing VSOP87D periodic terms for heliocentric distance. References: - [Bretagnon & Francou, A&A 202, 309 (1988)](https://ui.adsabs.harvard.edu/abs/1988A%26A...202..309B). - [VSOP87D.* data files](http://cdsarc.u-strasbg.fr/viz-bin/Cat?cat=VI/81): click on FTP, download VSOP87D.*. """ exts = ['mer', 'ven', 'ear', 'mar', 'jup', 'sat', 'ura', 'nep'] fileName = dataDir + '/VSOP87D.' + exts[pl - 1] inFile = open(fileName, 'r') import fortranformat as ff formatHeader = ff.FortranRecordReader( '(40x,I3, 16x,I1,I8)') # Block header format formatBody = ff.FortranRecordReader( '(79x,F18.11,F14.11,F20.11)') # Block body format lonTerms = [] latTerms = [] radTerms = [] for iBlock in range(3 * 6): # 3 variables (l,b,r), up to 6 powers (0-5) line = inFile.readline() var, power, nTerm = formatHeader.read(line) # print(var,power,nTerm) if line == '': break # EoF for iLine in range(nTerm): line = inFile.readline() a, b, c = formatBody.read(line) # print(iLine, var,power, a,b,c) if var == 1: lonTerms.append([power, a, b, c]) # var=1: ecliptic longitude if var == 2: latTerms.append([power, a, b, c]) # var=2: ecliptic latitude if var == 3: radTerms.append([power, a, b, c]) # var=3: radial distance return lonTerms, latTerms, radTerms
def preprocessor(self, table, line, index): if index == 0: num_entries = int(line.strip().split()[0]) self.fformat_temperature = fortranformat.FortranRecordReader(f'{num_entries}F6.2') self.fformat_ioneq = fortranformat.FortranRecordReader(f'2I3,{num_entries}E10.2') elif index == 1: self.temperature = 10.**np.array(self.fformat_temperature.read(line), dtype=float) else: line = self.fformat_ioneq.read(line) line = line[:2] + [self.temperature, np.array(line[2:], dtype=float)] table.append(line)
def parse_rmat_lines(lines): lines = list(lines) ff_1_to_5 = ff.FortranRecordReader("(6E16.9)") ff_6 = ff.FortranRecordReader("(7E16.9)") parsed = [ff_1_to_5.read(l) for l in lines] parsed[-1] = ff_6.read(lines[-1]) flattened = [x for y in parsed for x in y] row = {} for key, index in RMAT_KEYS.items(): row[key] = float(flattened[index]) return row
def parse_survey_trailer(line1, line2): ffe4 = ff.FortranRecordReader("(3E16.9)") metadata = {} # RMIN RMAX missing for linacs. circumference C is present for linacs but is # actually just the linac length. If it's a linac then RMIN and RMAX are missing as # well, but if we parse using ffe4 then it will just set them to zero, which is # wrong. They should be None if they're missing. So we test for this here. circular = False if len(line2.split()) == 3: circular = True line1 = ffe4.read(line1) line2 = ffe4.read(line2) # Missing the machine centre. Missing for linacs. metadata["X"] = line1[0] metadata["Y"] = line1[1] metadata["Z"] = line1[2] if not circular: metadata["RMIN"] = None metadata["RMAX"] = None else: metadata["RMIN"] = line2[0] metadata["RMAX"] = line2[1] # Circumference or linac length. metadata["C"] = line2[2] return metadata
def read_linelist(filename, save_sqlite=False): if os.path.exists(filename): line_parse = ff.FortranRecordReader( '(F11.4,F7.3,F6.2,F12.3,F5.1,1X,A8,A2,F12.3,F5.1,1X,A8,A2,F6.2,F6.2,F6.2,A4,I2,I2,I3,F6.3,I3,F6.3,A8,A2,A8,A2,2I5,I6)' ) #line_parse = ff.FortranRecordReader('(F11.4,F7.3,F6.2,F12.3,F5.2,1X,A10,F12.3,F5.2,1X,A10,F6.2,A4,I2,I3,F6.3,I3,F6.3,I5,1X,A1,A1,1X,A1,A1,I1,A3,I5,I6)') col_names = 'WL,GFLOG,CODE,E,XJ,LABEL,EP,XJP,LABELP,GAMMAR,GAMMAS,GAMMAW,REF,NBLO,NBUP,ISO1,X1,ISO2,X2,OTHER1,OTHER2,LANDE,LANDEP,ISOSHIFT'.split( ',') print('len of col_names %i' % (len(col_names))) r = open(filename, 'r') rows_list = [] for line in r: line_arr = line_parse.read(line) dict1 = OrderedDict(zip(col_names, line_arr)) rows_list.append(dict1) df = pd.DataFrame(rows_list) r.close() if save_sqlite: con = sqlite.connect(filename + '.sqlite') df.to_sql('linelist', con) con.commit() con.close() return df
class WgfaParser(GenericIonParser): """ Information about each possible transition in an ion, including level indices, wavelengths, and decay rates. """ filetype = 'wgfa' dtypes = [int, int, float, float, float, str, str] units = [ None, None, u.angstrom, u.dimensionless_unscaled, 1 / u.s, None, None ] headings = [ 'lower_level', 'upper_level', 'wavelength', 'gf', 'A', 'lower_label', 'upper_label' ] descriptions = [ 'lower level index', 'upper level index', 'transition wavelength', 'oscillator strength', 'radiative decay rate', 'lower level label', 'upper level label' ] fformat = fortranformat.FortranRecordReader('(2I5,F15.3,2E15.3,A30,A30)') def preprocessor(self, table, line, index): super().preprocessor(table, line, index) # remove the dash in the second-to-last entry table[-1][-2] = table[-1][-2].split('-')[0].strip()
def preprocessor(self, table, line, index): tmp = line.strip().split() # 5-point fit for type 6, 9-point fit for type 2 n_spline = 5 if int(tmp[2]) == 6 else 9 fformat = fortranformat.FortranRecordReader('(3I3,{}E10.3)'.format(3+n_spline)) line = fformat.read(line) row = line[:6] + [np.array(line[6:])] table.append(row)
def _do_load(self, filename): """Clears internal lists and loads from file.""" with open(filename, "r") as h: fr = ff.FortranRecordReader('(2i5, 2f10.5, 1x, a100)') self.nmetal, self.nimax, self.eps, self.switer, self.title = fr.read( h.readline()) self.title = self.title.strip() # atoms part self.elems, self.nelemx, self.ip, self.ig0, self.ig1, self.cclog = [], [], [], [], [], [] fr = ff.FortranRecordReader('(a2, 2x, i6, f10.3, 2i5, f10.5)') for i in range(self.nmetal): symbol_, nelemx, ip, ig0, ig1, cclog = fr.read(h.readline()) if len(symbol_.strip()) == 0: raise RuntimeError("Doesn't seem to be a FileDissoc") self.elems.append(adjust_atomic_symbol(symbol_)) self.nelemx.append(nelemx) self.ip.append(ip) self.ig0.append(ig0) self.ig1.append(ig1) self.cclog.append(cclog) if len(self.elems) == 0: raise RuntimeError("Doesn't seem to be a FileDissoc") # molecules part (remaining lines) self.mol, self.c, self.mmax, self.nelem, self.natom = [], [], [], [], [] fr = ff.FortranRecordReader( '(a3, 5x, e11.5, 4e12.5, i1, 4(i2,i1))') for line in h: if not line.strip(): # empty line is considered end-of-file break vars = fr.read(line) self.mol.append(vars[0]) self.c.append(vars[1:6]) mmax = vars[6] self.mmax.append(mmax) self.nelem.append(vars[7::2][:mmax]) self.natom.append(vars[8::2][:mmax]) self.nmol = len(self.mol)
def parse_survey_rows(line3, line4): ffe3 = ff.FortranRecordReader("(4E16.9)") ffe4 = ff.FortranRecordReader("(3E16.9)") line3 = ffe3.read(line3) line4 = ffe3.read(line4) row = {} # Survey bits row["X"] = try_float(line3[0]) row["Y"] = try_float(line3[1]) row["Z"] = try_float(line3[2]) row["SUML"] = try_float(line3[3]) row["THETA"] = try_float(line4[0]) row["PHI"] = try_float(line4[1]) row["PSI"] = try_float(line4[2]) return row
def parse_twiss_row(line1, line2, line3): ffr = ff.FortranRecordReader("(5E16.9)") line1 = ffr.read(line1) line2 = ffr.read(line2) line3 = ffr.read(line3) row = {} all_lines = line1 + line2 + line3 for key, index in TWISS_KEYS.items(): with contextlib.suppress(TypeError, ValueError): entry = float(all_lines[index]) row[key] = entry return row
def parse_common_two_lines(line1, line2): line1 = ff.FortranRecordReader("(A4,A16,F12.6,4E16.9,A19,E16.9)").read( line1) line2 = ff.FortranRecordReader("(5E16.9)").read(line2) keyword = line1[0].strip() name = line1[1].strip() row = {key: 0.0 for key in COMMON_COLUMNS + SURVEY_COLUMNS} row["KEYWORD"] = keyword row["NAME"] = name row["NOTE"] = "" if keyword == "": return row data = line1[2:6] + line2 + [line1[6], line1[7], line1[8]] keyword_columns = COMMON_COLUMN_POSITIONS[keyword] for key, index in keyword_columns.items(): row[key] = data[index] return row
def loadPdgCrossSection(file) : import fortranformat as _ff f = open(file,'r') # load header text header = True headerText = [] while(header) : l = f.readline() if l.find("---") != -1: header = False else : headerText.append(l.strip('\n\r ')) # decode header nheader = len(headerText) keys = headerText[4].split() ffmt = headerText[5][headerText[5].find('('):headerText[5].find(')')+1] # print nheader # print headerText # print keys # print ffmt # read preable preamble = {} for i in range(0,nheader-2,1) : l = f.readline().strip('\r\n ') preamble[headerText[i]] = l # print headerText[i],l # print preamble # read data table fr = _ff.FortranRecordReader(ffmt) data = [] for i in range(0,int(preamble['NUMBER_OF_DATA_POINTS']),1) : data.append(fr.read(f.readline())) data = _np.array(data) dataDict = {} # format into dict for k in keys[0:-1] : # FLAG not present idx = keys.index(k) dataDict[k] = data[:,idx] # close file f.close() return dataDict
def read_and_store(): ''' Read in lines from infile and store them in midfile. Split up even lines into variables for future calculations''' # is a midfile really needed?? i = 1 for line in infile: midfile.write(line) line = line.rstrip() # remove whitespace if i % 2 is 0: # print("line " + str(i)) # test # print(line) # import pdb # initiate debugger while working with emacs/console # pdb.set_trace() var = ff.FortranRecordReader(lineFormat).read(line) # print(type(var)) # test # print(dir(var)) # print(var) pave.append(float(var[0])) tave.append(float(var[1])) if var[3] == 0.0: altbot.append(alttop[alttop.__len__() - 1]) # import pdb # initiate debugger using emacs/console # pdb.set_trace() else: altbot.append(float(var[3])) if var[4] == 0.0: pbot.append(ptop[alttop.__len__() - 1]) else: pbot.append(float(var[4])) if var[5] == 0.0: tbot.append(ttop[alttop.__len__() - 1]) else: tbot.append(float(var[5])) alttop.append(float(var[6])) ptop.append(float(var[7])) ttop.append(float(var[8])) i = i + 1 infile.close() midfile.close()
def get_data(self, formatline=None): # reads data using the information in FORMAT # if FORMAT information in file header is missing or does not work # then create 'struct' data format based on channel details information idx = self.find_index('*END OF HEADER') lines = self.lines[idx + 1:] data = [] # if formatline is None, try reading without any format (assume columns are space limited; # if space limited strategy does not work, try to create format line) if formatline is None: try: print( "Trying to read file using format created using column width" ) print("Reading data using format", self.channel_details['fmt_struct']) fmt_len = self.fmt_len(self.channel_details['fmt_struct']) fmt_struct = self.channel_details['fmt_struct'] for i in range(len(lines)): if len(lines[i].strip()) > 1: # py2-3 migration... # data.append(struct.unpack(self.channel_details['fmt_struct'], lines[i].rstrip().ljust(fmt_len))) data.append( struct.unpack( fmt_struct, lines[i].rstrip().ljust( fmt_len).encode('utf-8'))) # data.append([r for r in lines[i].split()]) except Exception as e: print(e) data = np.genfromtxt(StringIO(''.join(lines)), delimiter='', dtype=str, comments=None) print("Reading data using delimiter was successful !") else: ffline = ff.FortranRecordReader(formatline) for i in range(len(lines)): if len(lines[i]) > 0: data.append([float(r) for r in ffline.read(lines[i])]) data = np.asarray(data) if self.debug: print(data) # if data is at only one, convert list to 2D matrix if len(data.shape) == 1: data = data.reshape((1, -1)) return data
def _read_line(l, n): if n == 8: form = '1x, 4(f10.6), 2x, f8.2, 2x, f10.6' lenght = 63 elif n == 10: form = 'f10.6,f10.6,2f10.6,2f10.6,f6.1,f10.6,f11.7' lenght = 87 try: lin = ff.FortranRecordReader(form).read(l[:lenght]) except ValueError: lin = [np.nan] * int(n - 2) ap, nstar = l[lenght:].split() lin.append(float(ap)) lin.append(int(nstar)) return lin
def _do_load(self, filename): with open(filename, "r") as h: #-- row 1 self.titre = h.readline().strip('\n') #-- row 2 fr = ff.FortranRecordReader('(I6,2F6.1,2X,2F8.4)') vars = fr.read(h.readline()) [self.ntot, self.zut1, _, self.zut2, self.zut3] = vars #-- row 3 self.jmax = int(h.readline()) #-- lambda vector #-- Reads sequence of jmax float numbers of 14 characters each disposed in #-- rows of 5 columns or less # function to calculate number of 5-column rows needed to store a sequence num_rows = lambda x: int(math.ceil(float(x) / 5)) FLOAT_SIZE = 14 # size of stored float value in characters nr = num_rows(self.jmax) v = [] for i in range(nr): v.extend([ float(x) for x in a99.chunk_string(a99.readline_strip(h), FLOAT_SIZE) ]) self.lambdh = np.array(v) if not (len(self.lambdh) == self.jmax): raise RuntimeError("len(lambdh)=%d should be %d" % (len(self.lambdh), self.jmax)) #-- (lambda) x (atmospheric layer) matrix v = [] # Will accumulate values for future reshape FLOAT_SIZE = 12 # size of stored float value in characters for s in h.readlines(): s = s.strip('\n') v.extend([float(x) for x in a99.chunk_string(s, FLOAT_SIZE)]) if len(v) / self.jmax != self.ntot: raise RuntimeError( "Should have found %d values for th matrix (found %d)" % (self.jmax * self.ntot, len(v))) # Note that matrix is filled horizontally, i.e., first row, then second row, ... # whereas it was dumped in the file by column; hence we have to transpose it self.th = np.reshape(v, (self.ntot, self.jmax)).T
class AbundParser(GenericParser): filetype = 'abund' dtypes = [int, float, str] units = [None, u.dimensionless_unscaled, None] headings = ['Z', 'abundance', 'element'] descriptions = ['atomic number', 'abundance relative to H', 'element'] fformat = fortranformat.FortranRecordReader('(I3,F7.3,A5)') def __init__(self, abundance_filename, **kwargs): super().__init__(abundance_filename, **kwargs) self.full_path = kwargs.get( 'full_path', os.path.join(self.ascii_dbase_root, 'abundance', self.filename)) def postprocessor(self, df): df['abundance'] = 10.**(df['abundance'] - df['abundance'][df['Z'] == 1]) # repair missing data if df['element'][0] == '': col = [] for atomic_number in df['Z']: col.append(plasmapy.particles.atomic_symbol( int(atomic_number))) df['element'] = Column(col) df = super().postprocessor(df) return df def to_hdf5(self, hf, df): dataset_name = os.path.splitext(os.path.basename(self.filename))[0] footer = f"""{dataset_name} ------------------ {df.meta['footer']}""" for row in df: grp_name = '/'.join([row['element'].lower(), 'abundance']) if grp_name not in hf: grp = hf.create_group(grp_name) grp.attrs['footer'] = '' grp.attrs['chianti_version'] = df.meta['chianti_version'] else: grp = hf[grp_name] grp.attrs['footer'] += footer if dataset_name not in grp: ds = grp.create_dataset(dataset_name, data=row['abundance']) ds.attrs['unit'] = df['abundance'].unit.to_string() ds.attrs['description'] = df.meta['descriptions']['abundance']
def write_syn_runlog(path, v0=4750, v1=8250, DELTA_NU=0.0111111111): ''' Input: - path : full path to an existing runlog - v0 : starting wavenumber (cm-1) - v1 : last wavenumber (cm-1) - delta_nu : wavenumber spacing (cm-1) This will write a new runlog that can be used to generate synthetic spectra. The new runlog will be saved in the same place but the filename will finish with "_syn.grl" ''' with open(path, 'r') as infile: content = infile.readlines() syn_dict = { 'BPW': 7, 'POINTER': 0, 'APF': 'N1', 'DELTA_NU': DELTA_NU, 'IFIRST': int(round(v0 / DELTA_NU)), 'ILAST': int(round(v1 / DELTA_NU)), 'SNR': 1000, } header = [' '] + content[3].split() ID_dict = {key: header.index(key) for key in syn_dict} fmt = list(parse.parse('format={}\n', content[2]))[0] reader = ff.FortranRecordReader(fmt) writer = ff.FortranRecordWriter(fmt) for i, line in enumerate(content): if i >= 4: data = reader.read(line) for key in syn_dict: data[ID_dict[key]] = syn_dict[key] new_line = writer.write(data) + '\n' content[i] = new_line new_path = path.replace('.grl', '_syn.grl') with open(new_path, 'w') as outfile: outfile.writelines(content)
def __read_data(self, f): if len(self.__dataindex) != SAOGROUPMAX: return for i in range(len(SAOFORMAT)): elements = list() record_format = SAOFORMAT[i] element_max = self.__parse_format(record_format) if self.__dataindex[i] == 0: line_cnt = 0 else: line_cnt = int(math.ceil(self.__dataindex[i] / float(element_max))) line_reader = ff.FortranRecordReader(record_format) for j in range(line_cnt): line = f.readline() datums = line_reader.read(line) elements.extend(datums) self.__metadata[i] = SAOGroup(i, elements)
class FblvlParser(GenericIonParser): """ Energy levels and configuration related to the calculation of the free-bound continuum. Only available for those ions and levels for which a free-bound continuum can be calculated. """ filetype = 'fblvl' dtypes = [int, str, int, int, str, int, float, float] units = [None, None, None, None, None, None, 1 / u.cm, 1 / u.cm] headings = [ 'level', 'config', 'n', 'L', 'L_label', 'multiplicity', 'E_obs', 'E_th' ] descriptions = [ 'level index', 'configuration', 'principal quantum number', 'azimuthal quantum number', 'orbital angular momentum', 'multiplicity, 2s+1', 'observed energy', 'theoretical energy' ] fformat = fortranformat.FortranRecordReader('(I5,A20,2I5,A3,I5,2F20.3)')
class ElvlcParser(GenericIonParser): """ Energy levels and configurations for each level in an ion. """ filetype = 'elvlc' dtypes = [int, str, str, int, str, float, float, float] units = [None, None, None, u.dimensionless_unscaled, None, u.dimensionless_unscaled, 1/u.cm, 1/u.cm] headings = ['level', 'config', 'label', 'multiplicity', 'L_label', 'J', 'E_obs', 'E_th'] descriptions = [ 'level index', 'configuration', 'level label', 'multiplicity, 2s+1', 'orbital angular momentum', 'total angular momentum', 'observed energy', 'theoretical energy' ] fformat = fortranformat.FortranRecordReader('(I7,A30,A5,I5,A5,F5.1,F15.3,F15.3)')
def _do_save_as(self, filename): with open(filename, "w") as h: # h.writelines([' %-2s%6.2f\n' % (self.ele[i], self.abol[i]) # for i in xrange(len(self))]) # h.writelines(['1\n', '1\n']) a99.write_lf( h, "%5d%5d%10.5f%10.5f %s" % (self.nmetal, self.nimax, self.eps, self.switer, self.title)) # atoms part fr = ff.FortranRecordReader('(a2, 2x, i6, f10.3, 2i5, f10.5)') for elems, nelemx, ip, ig0, ig1, cclog in \ zip(self.elems, self.nelemx, self.ip, self.ig0, self.ig1, self.cclog): elems = elems.upper().strip( ) # to follow convention: right-aligned upper case a99.write_lf( h, '%2s %6d%10.3f%5d%5d%10.5f' % (elems, nelemx, ip, ig0, ig1, cclog)) for mol, c, mmax, nelem, natom in \ zip(self.mol, self.c, self.mmax, self.nelem, self.natom): mol = mol.upper().strip( ) # to follow convention: right-aligned upper case l = [ "%3s %11.5e%12.5e%12.5e%12.5e%12.5e%1d" % tuple([mol] + c + [mmax]) ] for nelemm, natomm in zip(nelem, natom): l.append("%2d%1d" % (nelemm, natomm)) s = "".join(l) a99.write_lf(h, s) a99.write_lf(h, "") a99.write_lf(h, "") # two blank lines to signal end-of-file
i = 0 while(True): txt = f.readline() if re.match('thermo', txt): break # read header ngl = 0 ns = 0 nall = 0 ifzm1 = 0 inew = 0 tinf = 1e6 txt = f.readline() reader = ff.FortranRecordReader('4F10.3,a10') arr = reader.read(txt) tg_int = arr[:-1] thedate = arr[-1] def define_fill_thermo(): fill = np.zeros(3) fill[:] = True thermo = np.zeros([9, 3]) return fill, thermo # sp_name = ' '
def read_log(log_file, return_table=False): """Read the .log file from pccdpack and return a dict with the results""" f = open(log_file, 'r') f = f.readlines() result = OrderedDict() # {star_n:{aperture:{'params':{},z:[]},'positions':[]} star = None wave_n_pos = 16 wave_pos = None wavetype = None read_star = False read_apertures = False _aperture = 0 params_index = None z_index = None if return_table: t = None i = 0 while i < len(f): lin = f[i].strip('\n').strip(' ') if re.match('No. of waveplate positions :\s+\d+', lin): wave_n_pos = int(re.findall('\d+', lin)[0]) if re.match('Waveplate pos. : [\s+\d+]+ =\s+\d+', lin): wave_pos = [int(v) for v in re.findall('\d+', lin)[:-1]] if re.match('No. of apertures observed:\s+\d+', lin): n_apertures = int(re.findall('\d+', lin)[0]) if re.match('Waveplate type :', lin): wavetype = lin.split(':')[1].strip() if re.match('STAR # \s+\d+\s+ .*', lin): star = int(re.findall('\d+', lin)[0]) result[star] = OrderedDict() result[star]['positions'] = wave_pos read_star = True if re.match('APERTURE = \s+\d+.\d+.', lin): aperture = float(re.findall('\d+.\d+', lin)[0]) result[star][aperture] = {'z': [], 'params': OrderedDict()} _aperture += 1 read_apertures = True params_index = i + 2 z_index = i + 5 if read_star and read_apertures: plin = f[params_index].strip('\n').strip(' ') if wavetype == 'half': form = '1x, 4(f10.6), 2x, f8.2, 2x, f10.6' npar = 6 elif wavetype == 'quarter': form = 'f10.6,f10.6,2f10.6,2f10.6,f6.1,f10.6,f11.7' npar = 8 else: raise NotImplementedError( 'Not implementd for {}'.format(wavetype)) try: params = ff.FortranRecordReader(form).read(plin) except ValueError: params = [np.nan] * npar plin = f[params_index - 1].strip('\n').strip(' ') pnames = plin.split() del plin for n, v in zip(pnames, params): result[star][aperture]['params'][n] = v z = [] while len(z) < wave_n_pos: zread = ff.FortranRecordReader('(1x,4(f10.6))') try: zlin = zread.read(f[z_index]) except Exception: zlin = [np.nan] * 4 for k in zlin: try: z.append(float(k)) except: z.append(np.nan) z_index += 1 del zlin z = np.array(z) result[star][aperture]['z'] = z if _aperture == n_apertures: read_star = False read_apertures = False i = z_index i += 1 if return_table: for s in result.keys(): for a in result[s].keys(): if a != 'positions': z = result[s][a]['z'] params = result[s][a]['params'] if t is None: p = ['f8'] * len(params.keys()) t = Table( names=['STAR', 'APERTURE', 'Z', *params.keys()], dtype=['i4', 'f8', '{}f8'.format(wave_n_pos), *p]) try: t.add_row([s, a, z, *params.values()]) except Exception as e: print(['STAR', 'APERTURE', 'Z', *pnames], [star, aperture, z, *params]) raise e t.meta['pccdpack wavetype'] = wavetype t.meta['pccdpack wave_npos'] = wave_n_pos t.meta['pccdpack wave_pos'] = ','.join(str(i) for i in wave_pos) return t for i in t.colnames: t.rename_column(i, str(i).lower()) return result
def _parse_perf_file(self, filename): """ Parses a [name]perf.dat file from a CHARM run :param filename: fullname of the [name]perf.dat file to parse :return: """ # Open up the file rows = [] rotor_num = [] revolution_num = [] variable_names = [] meta_data_rows = [] meta_data_variablenames = [] psi_ind = [] with open(filename, "r") as f: # Read in the number of rotors, and atmosphere properties line = f.readline() # variable names line = f.readline() # variable values data = line.split() num_rotors = int(data[0]) rho = float(data[1]) speed_of_sound = float(data[2]) revolution = 0 # arrays for blade level properties line = f.readline() # variable names line while line: revolution += 1 meta_data_variablenames = ['revolution'] + line.split() meta_data_variablenames[1] = 'rotor' for irotor in range(num_rotors): line = f.readline() # variable values line reader = ff.FortranRecordReader( "(I9, 2I5, F11.2, F10.2, E10.4, F12.4, F12.3, 2E11.3)") data = reader.read(line) charm_rotor_num = int(data[0]) num_psi = int(data[1]) num_radial_locs = int(data[2]) meta_data_row = [ revolution, charm_rotor_num, num_psi, num_radial_locs ] + list(map(float, data[3:])) meta_data_rows.append(meta_data_row) # Read in radial data line = f.readline() # variable names variable_names = line.split() skip = False for ipsi in range(num_psi): if skip: break for iradial in range(num_radial_locs): cur_pos = f.tell() line = f.readline() # variable values try: row_data = [float(d) for d in line.split()] except: f.seek(cur_pos) skip = True if skip: break rotor_num.append(charm_rotor_num) revolution_num.append(revolution) psi_ind.append(ipsi) rows.append(row_data) line = f.readline() # Create a pandas data frame dictionary df_dict = { 'rotor': rotor_num, 'revolution': revolution_num, 'psi_ind': psi_ind } rows = np.array(rows) for ivar, var_name in enumerate(variable_names): df_dict[var_name] = rows[:, ivar] self.row_data = pd.DataFrame(df_dict) # Create metadata pandas data frame meta_df = {} for ivar, var_name in enumerate(meta_data_variablenames): meta_df[var_name] = np.array([row[ivar] for row in meta_data_rows]) self.rotor_metadata = pd.DataFrame(meta_df)
def __init__(self, str_array): ''' :param str_array: string array of the strip forces on a surface, from AVL ''' import numpy as np from io import BytesIO i = 0 strs = str_array[i].split() self.surf_num = int(strs[2]) self.name = strs[3] i = 1 strs = str_array[i].split() self.nchord = int(strs[3]) self.nspan = int(strs[7]) i = 2 strs = str_array[i].split() try: self.surf_area = float(strs[3]) self.mac = float(strs[7]) except ValueError: if strs[2][0] == "=": tempstr = strs[2][1:] self.surf_area = float(tempstr) self.mac = float(strs[-1]) else: raise ValueError(f'Unable to convert "{str_array[0][2:22]}" to float. Instead, found "{strs[2]}".') i = 3 strs = str_array[i].split() self.CLsurf = float(strs[2]) self.cl_surf = float(strs[5]) i = 4 strs = str_array[i].split() self.CY_surf = float(strs[2]) self.cm_surf = float(strs[5]) i = 5 strs = str_array[i].split() self.CD_surf = float(strs[2]) self.cn_surf = float(strs[5]) i = 6 strs = str_array[i].split() self.CDi_surf = float(strs[2]) self.CDvsurf = float(strs[5]) bio = BytesIO("".join(str_array[14:]).encode("utf8")) # data = np.genfromtxt(bio, ) # Manually parse data since sometimes there are a different number of rows data = [] for line in str_array[14:]: line = line.replace("*", " ") reader = ff.FortranRecordReader("(2X,I4,11(1X,F8.4),1X,F8.3)") line_data = reader.read(line) line_data = [np.nan if x is None else x for x in line_data] data.append(line_data) data = np.array(data) self.j = data[:,0] self.yle = data[:,1] self.chord = data[:,2] self.area = data[:,3] self.c_cl = data[:,4] self.ai = -data[:,5] self.cl_norm = data[:,6] self.cl = data[:,7] self.cd = data[:,8] self.cdv = data[:,9] self.cm_c4 = data[:,10] self.cm_le = data[:,11] try: self.cp_xc = data[:,12] except (IndexError): self.cp_xc = data[:, 11] self.cp_xc = np.nan self.data = data
import fortranformat as ff import vpython as vp x = [] ; y = [] ; z = [] ; myLabels = [] myFile = open('fake data.txt','r') myText = myFile.readlines() myFirstLineList = myText[0].split(" ",1) myFortranFormat = myFirstLineList[0] myTempString = myFirstLineList[1] myConceptCount = int(myTempString[2:4]) myDimensionCount = int(myTempString[8:10]) myFirstSplit = myFortranFormat.split('(') myConceptsPerLine = int(myFirstSplit[1].split('F')[0]) myLinesToSkip = (myDimensionCount % myConceptsPerLine) + 2 myFortranData = ff.FortranRecordReader(myFortranFormat) myLine = 1 myConceptLoopCounter = 1 while myConceptLoopCounter <= myConceptCount: myTempXYZ = myFortranData.read(myText[myLine]) x.append(myTempXYZ[0]) y.append(myTempXYZ[1]) z.append(myTempXYZ[2]) myLine += myLinesToSkip myConceptLoopCounter += 1 myConceptLoopCounter = 1 while myConceptLoopCounter <= myConceptCount: myLabel = myText[myLine].strip() myLabels.append(myLabel) myLine += 1 myConceptLoopCounter += 1 print("myConceptCount is ",myConceptCount) print("Labels are ",myLabels)
def fformat(self, value): self._fformat = value if self._fformat: self._parser = ff.FortranRecordReader(self._fformat)
LONM = -86.7 files = glob.glob(PATH + "goes*.txt") #files = glob.glob(PATH+"goes-xrs-report_2017-lance-07-10.txt") numofevents = 0 n = 0 n_errors = 0 eventTags = [] for filename in tqdm(files): print(filename) with open(filename, "r") as ins: for line in ins: try: #ffline = ff.FortranRecordReader('(I2,I3,I2,I2,I2,A2,A4,1X,A4,1X,A4,1X,A1,I2,A1,I2,A3,22X,A1,1X,I2,4X,A4,1X,E7.1,1X,I5,1X,I2,I2,F4.1,1X,F7.1,1X,F7.2)') ffline = ff.FortranRecordReader( '(I2,I3,I2,I2,I2,A2,A4,1X,A4,1X,A4,1X,A1,I2,A1,I2,A3,22X,A1,I3,4X,A4,1X,E7.1,1X,I5,1X,I2,I2,F4.1,1X,F7.1,1X,F7.2)' ) data = ffline.read(line) T_value = False ##################porque se hace esto?????=>creo que para llevar una bandera de que se leyo bien el tiempo except: try: #Fixing GOES T #ffline = ff.FortranRecordReader('(I2,I3,I2,I2,I2,A2,A4,1X,A4,1X,A4,1X,A1,I2,A1,I2,A3,22X,A1,1X,I2,4X,A4,1X,A1,E7.1,1X,I5,1X,I2,I2,F4.1,1X,F7.1,1X,F7.2)') ffline = ff.FortranRecordReader( '(I2,I3,I2,I2,I2,A2,A4,1X,A4,1X,A4,1X,A1,I2,A1,I2,A3,22X,A1,I3,4X,A4,1X,A1,E7.1,1X,I5,1X,I2,I2,F4.1,1X,F7.1,1X,F7.2)' ) data = ffline.read(line) T_value = True except: break try: