def get_all_isochrones(filename): #first go through the file and find how many total rows and columns #of data you have, how many isochrones you have, and the starting #index & metadata of each isochrone # pretend these are the values you get f=open(filename,'r') lines = f.readlines() f.close() start_indices = [] metallicities = [] ages = [] Nrows = 0 N_isochrones = 0 for i in range(len(lines)): # The start of the column head # log age/yr if lines[i].startswith('# l'): start_indices.append(i+1) if not lines[i].startswith('#'): Nrows += 1 if lines[i].startswith('#\tI'): N_isochrones += 1 line = lines[i] line = line.split() metallicities.append(float(line[4])) ages.append(float(line[7])) colhead = lines[start_indices[0]-1].strip() colhead = colhead.replace('#','') colhead = colhead.replace('/','') col_keys = colhead.split() col_keys[0] = 'LogAge' Ncols = len(col_keys) #now go back through the file and read in all the rows and columns of data data = np.ndarray(shape=(Nrows,Ncols), dtype=float) row = 0 for i in range(len(lines)): #print i if lines[i].startswith('#'): continue items = [is_numeric(m) for m in lines[i].split()] if len(lines[i].split()) != Ncols: items.append('') data[row] = items row += 1 IsoDict = {} #this will help with slicing: see below start_indices = np.concatenate((start_indices,[-1])) #create your dictionary of isochrone objects for i in range(N_isochrones): key = create_key(metallicities[i],ages[i]) IsoDict[key] = Isochrone(metallicities[i], ages[i], data[start_indices[i]:start_indices[i+1]], col_keys) return IsoDict, col_keys
def get_all_isochrones(filename): #first go through the file and find how many total rows and columns #of data you have, how many isochrones you have, and the starting #index & metadata of each isochrone # pretend these are the values you get f = open(filename, 'r') lines = f.readlines() f.close() start_indices = [] metallicities = [] ages = [] Nrows = 0 N_isochrones = 0 for i in range(len(lines)): # The start of the column head # log age/yr if lines[i].startswith('# l'): start_indices.append(i + 1) if not lines[i].startswith('#'): Nrows += 1 if lines[i].startswith('#\tI'): N_isochrones += 1 line = lines[i] line = line.split() metallicities.append(float(line[4])) ages.append(float(line[7])) colhead = lines[start_indices[0] - 1].strip() colhead = colhead.replace('#', '') colhead = colhead.replace('/', '') col_keys = colhead.split() col_keys[0] = 'LogAge' Ncols = len(col_keys) #now go back through the file and read in all the rows and columns of data data = np.ndarray(shape=(Nrows, Ncols), dtype=float) row = 0 for i in range(len(lines)): #print i if lines[i].startswith('#'): continue items = [is_numeric(m) for m in lines[i].split()] if len(lines[i].split()) != Ncols: items.append('') data[row] = items row += 1 IsoDict = {} #this will help with slicing: see below start_indices = np.concatenate((start_indices, [-1])) #create your dictionary of isochrone objects for i in range(N_isochrones): key = create_key(metallicities[i], ages[i]) IsoDict[key] = Isochrone(metallicities[i], ages[i], data[start_indices[i]:start_indices[i + 1]], col_keys) return IsoDict, col_keys
def read_table_mixed_type(file,delimiter=','): # writen to read a csv file with mixed float/str # add funcionality for comment lines? lines = open(file,'r').readlines() col_keys = lines[0].split(delimiter) Ncols = len(col_keys) Nrows = len(lines)-1 data = {} for key in col_keys: data[key] = [] for line in lines[1::]: item = line.strip().split(delimiter) for j,key in enumerate(col_keys): data[key].append(is_numeric(item[j])) return data
def read_table_mixed_type(file, delimiter=','): # writen to read a csv file with mixed float/str # add funcionality for comment lines? lines = open(file, 'r').readlines() col_keys = lines[0].split(delimiter) Ncols = len(col_keys) Nrows = len(lines) - 1 data = {} for key in col_keys: data[key] = [] for line in lines[1::]: item = line.strip().split(delimiter) for j, key in enumerate(col_keys): data[key].append(is_numeric(item[j])) return data
def get_data(filename): print 'dont use get_data, use GenUtils.read_table' sys.exit() f = open(filename, 'r') lines = f.readlines() f.close() d = {} col_heads = lines[0].replace('#', '') col_keys = col_heads.split() for key in col_keys: d[key] = [] for i in range(len(lines)): if lines[i].startswith('#'): continue data = lines[i].split() for key, i in zip(col_keys, range(len(col_keys))): d[key].append(is_numeric(data[i])) return d
def get_data(filename): print 'dont use get_data, use GenUtils.read_table' sys.exit() f = open(filename,'r') lines = f.readlines() f.close() d = {} col_heads = lines[0].replace('#','') col_keys = col_heads.split() for key in col_keys: d[key]=[] for i in range(len(lines)): if lines[i].startswith('#'): continue data = lines[i].split() for key,i in zip(col_keys,range(len(col_keys))): d[key].append(is_numeric(data[i])) return d
def read_tagged_data(filename): # Hopefully you pasted the line with the fiter names on the header. f = open(filename, 'r') lines = f.readlines() f.close() i = 0 d = {} for line in lines: if line.startswith('#'): line = line.replace('#', '') col_keys = line.split() i += 1 for key in col_keys: d[key] = [] if i == 0: print 'I don' 't know the names of the damn filters.' for line in lines: if line.startswith('#'): continue row = line.split() for key, i in zip(col_keys, range(len(col_keys))): d[key].append(is_numeric(row[i])) return d
def read_tagged_data(filename): # Hopefully you pasted the line with the fiter names on the header. f = open(filename,'r') lines = f.readlines() f.close() i = 0 d = {} for line in lines: if line.startswith('#'): line = line.replace('#','') col_keys = line.split() i += 1 for key in col_keys: d[key] = [] if i == 0: print 'I don''t know the names of the damn filters.' for line in lines: if line.startswith('#'): continue row = line.split() for key,i in zip(col_keys,range(len(col_keys))): d[key].append( is_numeric(row[i])) return d