def get_simulation_data(self): """Extract simulation data. Extracts the simulation data from the SAGE parameter file and returns a dictionary containing the values. """ if not self.args.box_size: raise tao.ConversionError('Must specify a box-size') if not self.args.parameters: raise tao.ConversionError('Must specify a SAGE parameter file') par = open(self.args.parameters, 'r').read() hubble = np.float32(re.search(r'Hubble_h\s+(\d*\.?\d*)', par, re.I).group(1)) if hubble < 1.0: hubble = hubble * 100.0 msg = 'Hubble parameter must be in physical units (not little h)' assert hubble > 1.0, msg hubble = str(hubble) omega_m = re.search(r'Omega\s+(\d*\.?\d*)', par, re.I).group(1) omega_l = re.search(r'OmegaLambda\s+(\d*\.?\d*)', par, re.I).group(1) return { 'box_size': self.args.box_size, 'hubble': hubble, 'omega_m': omega_m, 'omega_l': omega_l, }
def read_file_possibly_gzipped(file,max_chunks): if not hasattr(file,'__read__'): try: f = open(file,'r') except IOError: f = gzip.open(file+'.gz','rb') except: raise tao.ConversionError('Could not open file. Neither as ascii or gz') try: chunks = f.read(max_chunks) except EOFError: ## not enough bytes. Just read-in the entire file then chunks = f.read() except: raise tao.ConversionError('Could not read in from the file') f.close return chunks
def get_simulation_data(self): """Extract simulation data. Extracts the simulation data from the SAGE parameter file and returns a dictionary containing the values. """ if not self.args.box_size: raise tao.ConversionError('Must specify a box-size') if not self.args.parameters: raise tao.ConversionError('Must specify a SAGE parameter file') par = open(self.args.parameters, 'r').read() hubble = re.search(r'Hubble_h\s+(\d*\.?\d*)', par, re.I).group(1) omega_m = re.search(r'Omega\s+(\d*\.?\d*)', par, re.I).group(1) omega_l = re.search(r'OmegaLambda\s+(\d*\.?\d*)', par, re.I).group(1) return { 'box_size': self.args.box_size, 'hubble': hubble, 'omega_m': omega_m, 'omega_l': omega_l, }
def get_snapshot_redshifts(self): """Parse and convert the expansion factors. Uses the expansion factors to calculate snapshot redshifts. Returns a list of redshifts in order of snapshots. """ if not self.args.a_list: raise tao.ConversionError('Must specify a filename for the a-list') redshifts = [] with open(self.args.a_list, 'r') as file: for line in file: line = line.strip() if line: redshifts.append(1.0 / float(line) - 1.0) if len(redshifts) == 0: msg = "Could not parse any redshift values in file {0}"\ .format(self.args.a_list) raise tao.ConversionError(msg) print("Found {0} redshifts in file {1}".format(len(redshifts), self.args.a_list)) return redshifts
def get_snapshot_redshifts(self): """Parse and convert the expansion factors. Uses the expansion factors to calculate snapshot redshifts. Returns a list of redshifts in order of snapshots. """ if not self.args.a_list: raise tao.ConversionError('Must specify a filename for the a-list') redshifts = [] with open(self.args.a_list, 'r') as file: for line in file: redshifts.append(1.0 / float(line) - 1.0) return redshifts
def get_simulation_data(self,trees_dir=None): """Extract simulation data. Extracts the simulation data from the CTREES parameter file and returns a dictionary containing the values. """ if trees_dir is None: if self.args.parameters: par = open(self.args.parameters, 'r').read() hubble = re.search(r'h0\s*=\s*(\d*\.?\d*)', par, re.I).group(1) omega_m = re.search(r'Om\s*=\s*(\d*\.?\d*)', par, re.I).group(1) omega_l = re.search(r'Ol\s*=\s*(\d*\.?\d*)', par, re.I).group(1) box_size = re.search(r'BOX_WIDTH\s*=\s*(\d*\.?\d*)', par, re.I).group(1) else: if not self.args.trees_dir: raise tao.ConversionError('Must specify either the CTREES config or the trees location') ## read in the first chunk bytes. (DO NOT read in entire file) chunk_guess = 5000 first_tree = read_file_possibly_gzipped(self.args.trees_dir+'/tree_0_0_0.dat',chunk_guess) hubble = re.search(r'h0\s*=\s*(\d*\.?\d*)', first_tree, re.I).group(1) omega_m = re.search(r'Omega_M\s*=\s*(\d*\.?\d*)', first_tree, re.I).group(1) omega_l = re.search(r'Omega_L\s*=\s*(\d*\.?\d*)', first_tree, re.I).group(1) box_size = re.search(r'Full\s+box\s+size\s+=\s+(\d*\.?\d*)', first_tree, re.I).group(1) else: ## read in the first chunk bytes. (DO NOT read in entire file) chunk_guess = 5000 first_tree = read_file_possibly_gzipped(trees_dir+'/tree_0_0_0.dat',chunk_guess) hubble = re.search(r'h0\s*=\s*(\d*\.?\d*)', first_tree, re.I).group(1) omega_m = re.search(r'Omega_M\s*=\s*(\d*\.?\d*)', first_tree, re.I).group(1) omega_l = re.search(r'Omega_L\s*=\s*(\d*\.?\d*)', first_tree, re.I).group(1) box_size = re.search(r'Full\s+box\s+size\s+=\s+(\d*\.?\d*)', first_tree, re.I).group(1) return { 'box_size': box_size, 'hubble': hubble, 'omega_m': omega_m, 'omega_l': omega_l, }
def get_snapshot_redshifts(self,scales_file=None): """Parse and convert the expansion factors. Uses the expansion factors to calculate snapshot redshifts. Returns a list of redshifts in order of snapshots. """ if scales_file is None: if not self.args.a_list: raise tao.ConversionError('Must specify a filename for the a-list') else: scales_file = self.args.a_list redshifts = [] with open(scales_file, 'r') as file: for line in file: items = line.split() ## scales.txt contains snapshot number (items[0]) and scale factor (items[1]) redshifts.append(1.0/float(items[1]) - 1.0) return redshifts