def _writeOutputCoordinates(self): """ Outputs coordinates for each pixel. The output files are named as original_input_file_name_coordinates.txt Pickles all the information to a file named coordinates.pk for further processing. The pickled filename has been hardcoded, but this should not matter as any program using this file should know the filename. """ tmpdic = {} for slit, value in self.slits.items(): list = [] fh = open('%s_coordinates.txt' % slit[:-5], 'w') fh.write('#File written by findSlitmaskPosition.py on %s\n'\ % datetime.datetime.isoformat(datetime.datetime.now())) fh.write('#x\ty\tx2\ty2\tRA\tDEC\n') for tmp1, x, y, tmp2 in zip(value['coordinates'], value['coordinatesX'], value['coordinatesY'], value['coordinatesXY']): fh.write('%i %i %f %f %f %f \n' % (x, y, tmp2[0], tmp2[1], tmp1[0], tmp1[1])) list.append([x, y, tmp2[0], tmp2[1], tmp1[0], tmp1[1]]) tmpdic[slit] = list fh.close() write.cPickleDumpDictionary(tmpdic, 'coordinates.pk')
def parseInformation(self): """ Parses the file given in the class constructor for velocity information. Assumes that the input is in the emsao log file format. :return: velocity information :rtype: dictionary """ out = [] #read in data and loop over it data = open(self.file, 'r').readlines() for line in data: if len(line) > 1: tmp = line.split() if line.startswith('File:'): file = tmp[1] pixel = tmp[2] if line.startswith('Object:'): object = tmp[1] if line.startswith('Combined'): cvel = tmp[3] cvelerr = tmp[5] cz = tmp[8] if line.startswith('Emission'): evel = tmp[3] evelerr = tmp[5] ez = tmp[8] else: out.append([file, pixel, cvel, cvelerr, cz, evel, evelerr, ez, object]) #dumps to a file write.cPickleDumpDictionary(out, 'velocity.pk') self.velocityInfo = out return self.velocityInfo
def _getAndProcessData(home, outfile='SubDists2.pkl'): """ Retrieve data. """ path = home + '/Research/CANDELS/v2s/' db = 'sams.db' #SQL query where all the magic happens.. or doesn't query = """select l1.redshift, l1.ra, l1.dec, l1.halo_id, l1.gal_id, l1.mhalo from testing l1 inner join ( select l2.halo_id, l2.gal_id from testing l2 where l2.halo_id in (select halo_id from testing group by halo_id having count(*) > 1) intersect select l3.halo_id, l3.gal_id from testing l3 group by l3.halo_id, l3.gal_id having count(*) == 1 ) using (halo_id, gal_id) where l1.redshift > 0.1 and l1.redshift < 6.0 """ #pull out data data = sq.get_data_sqliteSMNfunctions(path, db, query) #group data grouped = _findValues(data) #dump it to a picked file wr.cPickleDumpDictionary(grouped, outfile) return grouped
def pickleVars(self): ''' This simple method pickles all important variables ''' write.cPickleDumpDictionary(self.result, 'results.pk') write.cPickleDumpDictionary(self.slits, 'slits.pk') write.cPickleDumpDictionary(self.fitting, 'fitting.pk')
def combineVelocityCoordinates(self, vel='velocity.pk', coord='coordinates.pk'): """ Combine velocity and coordinate information. Takes the data from pickled files. :param vel: pickled velocity information :type vel: string :param coord: pickled coordinate information :type coord: string """ #load in data coords = read.cPickledData(coord) vel = read.cPickledData(vel) #output out = [] #loop over all time and space... for file in coords.keys(): for coordline in coords[file]: for velline in vel: if velline[0] == file.replace('.fits', '') and int( velline[1]) == coordline[1]: #this is match but need to test if there's any useful veocity info if float(velline[2]) > 0.0 or float(velline[5]) > 0.0: tmp = coordline + [ float(velline[2]), float(velline[3]), float(velline[4]), float(velline[5]), float(velline[6]), float(velline[7]), file ] out.append(tmp) info = {'coordinates': coords, 'velocities': vel, 'data': out} self.velocityField = info write.cPickleDumpDictionary(self.velocityField, 'velocityField.pk')
def parseInformation(self): """ Parses the file given in the class constructor for velocity information. Assumes that the input is in the emsao log file format. :return: velocity information :rtype: dictionary """ out = [] #read in data and loop over it data = open(self.file, 'r').readlines() for line in data: if len(line) > 1: tmp = line.split() if line.startswith('File:'): file = tmp[1] pixel = tmp[2] if line.startswith('Object:'): object = tmp[1] if line.startswith('Combined'): cvel = tmp[3] cvelerr = tmp[5] cz = tmp[8] if line.startswith('Emission'): evel = tmp[3] evelerr = tmp[5] ez = tmp[8] else: out.append([ file, pixel, cvel, cvelerr, cz, evel, evelerr, ez, object ]) #dumps to a file write.cPickleDumpDictionary(out, 'velocity.pk') self.velocityInfo = out return self.velocityInfo
def combineVelocityCoordinates(self, vel='velocity.pk', coord='coordinates.pk'): """ Combine velocity and coordinate information. Takes the data from pickled files. :param vel: pickled velocity information :type vel: string :param coord: pickled coordinate information :type coord: string """ #load in data coords = read.cPickledData(coord) vel = read.cPickledData(vel) #output out = [] #loop over all time and space... for file in coords.keys(): for coordline in coords[file]: for velline in vel: if velline[0] == file.replace('.fits', '') and int(velline[1]) == coordline[1]: #this is match but need to test if there's any useful veocity info if float(velline[2]) > 0.0 or float(velline[5]) > 0.0: tmp = coordline + [float(velline[2]), float(velline[3]), float(velline[4]), float(velline[5]), float(velline[6]), float(velline[7]), file] out.append(tmp) info = {'coordinates': coords, 'velocities': vel, 'data': out} self.velocityField = info write.cPickleDumpDictionary(self.velocityField, 'velocityField.pk')
def getDataSlow(db='database.db', path='/Users/sammy/Research/CANDELS/v2/'): """ Get data from the lcone table in db. :param db: name of the SQLite database file. :type db: string :param path: full path of the database file :type path: string :Warning: This is extremely slow way to pull out data from a database. Should be rewritten using table joins etc. so that less queries could be performed. :return: redshift, halo mass and radius, projected distances of subhalo galaxies [kpc], halo_id of the main halo :rtype: dict """ conversion = 0.000277777778 # degree to arcsecond redshifts = [ 'redshift < 0.5 and', 'redshift >= 0.5 and redshift < 1.0 and', 'redshift >= 1.0 and redshift < 2.0 and', 'redshift >= 2.0 and redshift < 3.0 and', 'redshift >= 3.0 and redshift < 4.0 and', 'redshift >= 4.0 and redshift < 5.0 and', 'redshift >= 5.0 and redshift < 6.0 and', 'redshift >= 6.0 and redshift < 7.0 and' ] for i, red in enumerate(redshifts): print red qr = 'select halo_id from lcone where %s gal_id > 1' % red #qr = 'select halo_id from lcone where %s mhalo > 12.7' % red #pull out data ids = sq.get_data_sqliteSMNfunctions(path, db, qr) uids = np.unique(ids) print len(uids), 'haloes' saveid = [] saveorig = [] savedist = [] savemhalo = [] saverhalo = [] saveredshift = [] #we should now look for each unique id for id in uids: query = 'select redshift, ra, dec, halo_id, mhalo, rhalo from lcone where halo_id = {0:d}'.format( id) #print query data = sq.get_data_sqliteSMNfunctions(path, db, query) #if multiples, then don't take it if len(set(data[:, 0])) > 1: print 'skipping', id continue if len(data[:, 1]) < 2: print 'no subhaloes', id, data[:, 1] continue #redshift z = data[0, 0] #look the diameter distance from the lookup table dd = cosmocalc(z, 71.0, 0.28)['PS_kpc'] #the first halo, assume it to be the main halo RADeg1 = data[0, 1] decDeg1 = data[0, 2] #the following haloes, assume to be subhaloes RADeg2 = data[1:, 1] decDeg2 = data[1:, 2] #calculate the angular separation on the sky sep = Coords.calcAngSepDeg(RADeg1, decDeg1, RADeg2, decDeg2) physical_distance = sep * dd / conversion #these are all the main halo parameters saveredshift.append(z) saveid.append(int(data[0, 3])) savemhalo.append(data[0, 4]) saverhalo.append(data[0, 5]) savedist.append(physical_distance) saveorig.append(id) out = dict(halo_ids=saveid, distances=savedist, original=saveorig, mhalo=savemhalo, rhalo=saverhalo, redshift=saveredshift) wr.cPickleDumpDictionary(out, 'distances%i.pickle' % (i + 1))
def _pickleVars(self): """ This simple method pickles all important variables. Mainly useful for debugging when trying to figure out what went wrong. By default this method is not called as it is quite slow to pickle all this information and moreover it is not usually needed. """ if self.debug: tmp1 = self.result.copy() tmp2 = self.direct.copy() del tmp1['WCS'] del tmp2['WCS'] write.cPickleDumpDictionary(tmp1, 'results.pk') write.cPickleDumpDictionary(self.slits, 'slits.pk') write.cPickleDumpDictionary(self.fitting, 'fitting.pk') write.cPickleDumpDictionary(tmp2, 'direct.pk') del tmp1 del tmp2 else: tmp1 = self.result.copy() del tmp1['WCS'] write.cPickleDumpDictionary(tmp1, 'results.pk') write.cPickleDumpDictionary(self.slits, 'slits.pk') write.cPickleDumpDictionary(self.fitting, 'fitting.pk') del tmp1