def appendcatalogs(catalog1, catalog2, catalogOUT): """ The task appends catalogs using only the catalog1's header. Catalog1(withheader)+catalog2(woheader) The final (composed) catalog is saved as catalogOUT. NEEDLESS TO SAY BOTH CATALOGS HAVE TO HAVE THE SAME FORMAT (ROWS&COLUMNS) !!! ----- """ print 'Reading file1: ', catalog1 data1 = C.loaddata(catalog1) # Loading the whole catalog1 content. head1 = C.loadheader(catalog1) # Loading the original header1. print 'Reading file2: ', catalog2 data2 = C.loaddata(catalog2) # Loading the whole catalog2 content. head2 = C.loadheader(catalog2) # Loading the original header2. outcat = catalogOUT print outcat try: nf1 = N.shape(data1)[0] nc1 = N.shape(data1)[1] except: nf1 = 1 nc1 = N.shape(data1)[0] try: nf2 = N.shape(data2)[0] nc2 = N.shape(data2)[1] except: nf2 = 1 nc2 = N.shape(data2)[0] print 'Dimensions catalogue_1: ncols: %i, nraws: %i' % (nf1, nc1) print 'Dimensions catalogue_2: ncols: %i, nraws: %i' % (nf2, nc2) if nc1 == nc2: nf = nf1 + nf2 nc = nc1 newdata = N.zeros((nf, nc), float) for ii in range(nf1): if nf1 < 2: newdata[ii, :] = data1[:] else: newdata[ii, :] = data1[ii, :] for ii in range(nf2): if nf2 < 2: newdata[ii + nf1, :] = data2[:] else: newdata[ii + nf1, :] = data2[ii, :] C.savedata(newdata, outcat, dir="", header=head1) # Saving and creating the new catalog. else: print 'Different number of rows between catalogs. Impossible to append catalogs !!'
def minimum_photouncert(catalog, columns): """ """ data = C.loaddata(catalog) # Loading the whole catalog content. head = C.loadheader(catalog) # Loading the original header. mm = A.get_magnitudes(catalog, columns) em = A.get_errmagnitudes(catalog, columns) nl = len(mm[:, 0]) # nl is the number of detections inside every single band. nf = len(mm[0, :]) # nf is the number of bands inside the catalog. errmag = N.zeros((nl, nf), float) # Where the new photo errors will be saved. for jj in range(nf): for ii in range(nl): if em[ii, jj] < 0.01: errmag[ii, jj] = 0.03 elif em[ii, jj] > 1.0: errmag[ii, jj] = 1.0 else: errmag[ii, jj] = em[ii, jj] # New values of mags error overwrites now the original data. vars, evars, posref, zpe, zpo = A.get_usefulcolumns(columns) data[:, evars] = errmag[:, N.arange(nf)] finalcatalog = catalog[:-3] + 'ecor.cat' C.savedata(data, finalcatalog, dir="", header=head) # Saving & creating a new catalog.
def replace_photo_uncert(catalog, columns): """ """ data = C.loaddata(catalog) # Loading the whole catalog content. head = C.loadheader(catalog) # Loading the original header. mm = A.get_magnitudes(catalog, columns) em = A.get_errmagnitudes(catalog, columns) filters = B.get_filter_list(columns) nl = len(mm[:, 0]) # nl is the number of detections inside every single band. nf = len(mm[0, :]) # nf is the number of bands inside the catalog. errmag = U.zeros((nl, nf), float) # Where the new photo errors will be saved. for jj in range(nf): maglim = B.get_limitingmagnitude(mm[:, jj], em[:, jj], 1., 0.25) print 'Limiting Magnitude for filter %s: %.3f' % (filters[jj], maglim) for ii in range(nl): if mm[ii, jj] == -99.: errmag[ii, jj] = 0.00 elif mm[ii, jj] == 99.: errmag[ii, jj] = maglim else: errmag[ii, jj] = em[ii, jj] # New values of mags error overwrites now the original data. vars, evars, posref, zpe, zpo = get_usefulcolumns(columns) data[:, evars] = errmag[:, U.arange(nf)] finalcatalog = catalog[:-3] + 'upp.cat' C.savedata(data, finalcatalog, dir="", header=head) # Saving & creating a new catalog.
def replace_kerttu_errmags(catalog, columns, finalcatalog): """ import alhambra_kerttu_fixerrmags as AFM catalog = '/Users/albertomolino/doctorado/articulos/ALHAMBRA/kerttu/test_photoz/kerttu.cat' columns = '/Users/albertomolino/doctorado/articulos/ALHAMBRA/kerttu/test_photoz/kerttu.columns' finalcatalog = '/Users/albertomolino/doctorado/articulos/ALHAMBRA/kerttu/test_photoz/kerttu3.cat' AFM.replace_kerttu_errmag(catalog,columns,finalcatalog) ------ """ data = C.loaddata(catalog) # Loading the whole catalog content. head = C.loadheader(catalog) # Loading the original header. mm = A.get_magnitudes(catalog, columns) em = A.get_errmagnitudes(catalog, columns) filters = B.get_filter_list(columns) nl = len(mm[:, 0]) # nl is the number of detections inside every single band. nf = len(mm[0, :]) # nf is the number of bands inside the catalog. errmag = U.zeros((nl, nf), float) # Where the new photo errors will be saved. for jj in range(nf): for ii in range(nl): if mm[ii, jj] == -99.: errmag[ii, jj] = 0.00 else: errmag[ii, jj] = em[ii, jj] # New values of mags error overwrites now the original data. vars, evars, posref, zpe, zpo = A.get_usefulcolumns(columns) data[:, evars] = errmag[:, U.arange(nf)] C.savedata(data, finalcatalog, dir="", header=head) # Saving & creating a new catalog.
def compress_bpz_catalogues(catalogue, sample, outname): """ It selects a subsample of sources from an input catalogue. :param catalogue: :return: """ head1 = C.loadheader(catalogue) data1 = C.loaddata(catalogue) C.savedata(data1[sample, :], outname, dir="", header=head1)
def replacing_nans_catalogs(catalog, newname): """ vars = [] evars = [] data = C.loaddata(catalog) mags = data[:,vars] emags = data[:,evars] """ vars = N.array([ 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 102, 105, 108, 111 ]) evars = vars[:] + 1 s2n_vars = vars[:] + 2 data = C.loaddata(catalog) # Loading the whole catalog content. head = C.loadheader(catalog) # Loading the original header. mm = data[:, vars] em = data[:, evars] s2n = data[:, s2n_vars] nl = len(mm[:, 0]) # nl is the number of detections inside every single band. nf = len(mm[0, :]) # nf is the number of bands inside the catalog. newmag = U.zeros((nl, nf), float) # Where the new photo errors will be saved. errmag = U.zeros((nl, nf), float) # Where the new photo errors will be saved. new_s2n = U.zeros((nl, nf), float) for jj in range(len(vars)): for ii in range(nl): if abs(mm[ii, jj]) > 60.: newmag[ii, jj] = -99.0 errmag[ii, jj] = 0.00 new_s2n[ii, jj] = -1 elif s2n[ii, jj] < 0.00001: new_s2n[ii, jj] = 0. else: newmag[ii, jj] = mm[ii, jj] errmag[ii, jj] = em[ii, jj] new_s2n[ii, jj] = s2n[ii, jj] # New values of mags error overwrites now the original data. data[:, vars] = newmag[:, U.arange(nf)] data[:, evars] = errmag[:, U.arange(nf)] data[:, s2n_vars] = new_s2n[:, U.arange(nf)] C.savedata(data, newname, dir="", header=head) # Saving & creating a new catalog.
def run(): cs = U.get_str(lista_fluxcomp, 0) nc = len(cs) cat = U.get_str(lista_catalogs, 0) ncat = len(cat) print 'Number of catalogues to convert: ', nc cols = U.get_str(lista_columns, 0) ncols = len(cols) print 'nc,ncat,ncols: ', nc, ncat, ncols for ii in range(nc): head = coeio.loadheader(cs[ii]) nh = len(head) print 'Number of variables: ', nh # pausa = raw_input('paused') nf = nh - 5 body = coeio.loaddata(cs[ii]) ng = len(body) # pausa = raw_input('paused') field = ((cat[ii].split('/')[-1]).split('.')[1])[2] pointing = ((cat[ii].split('/')[-1]).split('.')[1])[5] ccd = ((cat[ii].split('/')[-1]).split('.')[1])[8] print 'Reading Field: %s, Pointing: %s, CCD: %s' % (field, pointing, ccd) # pausa = raw_input('paused') filename = final_root + cat[ii].split('/')[-1][:-3] + 'flux_comparison' print 'filename', filename filename2 = final_root + cat[ii].split('/')[-1][:-3] + 'columns' A.copyfile(cols[ii], filename2) # pausa = raw_input('paused') outfile = open(filename, 'w') for ii in range(len(head)): outfile.write('%s \n' % (head[ii])) for ss in range(ng): ids = body[ss, 0] # print 'IDs',ids ids814 = convert814ids(ids, int(field), int(pointing), int(ccd)) for jj in range(nh): if jj == 0: outfile.write('%s ' % (ids814)) if jj == 1: outfile.write('%.2f ' % (body[ss, 1])) if jj == 2: outfile.write('%.2f ' % (body[ss, 2])) if jj == 3: outfile.write('%.2f ' % (body[ss, 3])) if jj == 4: outfile.write('%.2f ' % (body[ss, 4])) if jj > 4: # print body[ss,jj] # pausa = raw_input('paused') outfile.write('%s ' % (body[ss, jj])) outfile.write('\n') outfile.close()
def runZPcal_catalogue(reference, frame, final): """ ---- filter_ref_cat,alig_frame_cat,alig_cal_frame_cat """ plots = 1 data2 = C.loaddata(frame) # Loading the whole catalog2 content. head2 = C.loadheader(frame) # Loading the original header2. pos_mags = 12 # ([12,20,21,22]) mag_r = U.get_data(reference, 12) mag_f = U.get_data(frame, 12) # good_sample = U.greater_equal(mag_r,16.) * U.less_equal(mag_r,21.5) good_sample = U.greater_equal(mag_r, 16.) * U.less_equal(mag_r, 19.) mag_r2, mag_f2 = U.multicompress(good_sample, (mag_r, mag_f)) offset = U.mean_robust(mag_f2 - mag_r2) if plots: plt.figure(11, figsize=(12, 9), dpi=80, facecolor='w', edgecolor='k') plt.clf() plt.plot(mag_r, (mag_f - mag_r - offset), 'ko', ms=10, alpha=0.1) plt.xlim(16, 25) plt.ylim(-5, 5.) plt.xlabel('AB', size=25) plt.ylabel('Mf-Mr', size=25) plt.xticks(fontsize=25) plt.yticks(fontsize=25) plt.legend(['Offset: %.4f' % (offset)], loc='upper right', numpoints=1) plt.title(A.getfilename(frame), size=15) plt.grid() figurename = final[:-3] + 'png' print 'figurename: ', figurename plt.savefig(figurename, dpi=100) plt.close() # Here it saves the offset in an ASCII file fileout = open(final[:-3] + 'txt', 'w') linea = '%s %.5f \n' % (final, offset) fileout.write(linea) fileout.close() # The offset is only applied to m!=99. magnitudes. new_mags = U.where(abs(mag_f) < 99, mag_f - offset, mag_f) data2[:, pos_mags] = new_mags C.savedata(data2, final, dir="", header=head2) print ' '
def flagging_dobledetections_mergecolumns(catalog): """ This serves to append an extra column (each to both inputted catalogs) indicating either a detection was repeated and with the lowest S/N of the two. Sources flagged as 1 are those detections to be excluded when combining both catalogs into a single one. -------- import alhambra_overlap as alhov cat2 = '/Volumes/amb22/catalogos/reduction_v4e/f02/f02p01_colorproext_2_ISO.cat' alhov.flagging_dobledetections_mergecolumns(cat2) """ data = coeio.loaddata(catalog) # Loading the whole catalog content. head = coeio.loadheader(catalog) # Loading the original header. nc = len(data.T) # Number columns dim = len(data[:,0]) # Number elements print 'nc,dim',nc,dim var1 = head[-3].split()[-1] var2 = head[-2].split()[-1] if var1 == var2: print 'Duplicated columns. Merging information...' uno = data[:,72] dos = data[:,73] tres = uno+dos newdata = U.zeros((dim,nc-1),float) for ii in range(nc-1): for jj in range(dim): if ii == nc-1: print 'pepe' newdata[jj,ii] = tres[jj] else: newdata[jj,ii] = data[jj,ii] head2 = head[:-1] head2[-1]='#' outcat = catalog[:-4]+'.mergedcolumns.cat' coeio.savedata(newdata,outcat, dir="",header=head2) # Saving and creating the new catalog. # Renaming files ap.renamefile(catalog,catalog+'.oldold.cat') if not os.path.exists(catalog): ap.renamefile(outcat,catalog)
def remove_detections_bysegmmaps(field, pointing, ccd): """ It uses the segmentation-maps to remove fake detections when masking out saturated stars. ---- import alhambra_fakedets as AF AF.remove_detections_bysegmmaps(2,1,1) """ root = '/Volumes/amb22/catalogos/reduction_v4f/f0%i/' % (field) root2images = '/Volumes/amb22/imagenes/f0%i/' % (field) catalog = root + 'f0%ip0%i_colorproext_%i_ISO.irmsF814W.free.cat' % ( field, pointing, ccd) ids, x, y, area = U.get_data(catalog, (0, 3, 4, 5)) dim = len(ids) valor = U.zeros(dim) ima1 = root2images + 'f0%ip0%i_F814W_%i.swp.seg.fits' % (field, pointing, ccd) ima2 = root2images + 'f0%ip0%i_F814W_%i.swp.segnomask.fits' % ( field, pointing, ccd) segm1 = pyfits.open(ima1)[0].data segm2 = pyfits.open(ima2)[0].data for ii in range(dim): xo = x[ii] yo = y[ii] dimx = U.shape(datos[yo - size:yo + size, xo - size:xo + size])[1] dimy = U.shape(datos[yo - size:yo + size, xo - size:xo + size])[0] perc[ii] = (datos[yo - size:yo + size, xo - size:xo + size].sum() / (dimx * dimy * 1.)) # Defining the sample to be keep. good = U.greater(valor, 0) idr = U.compress(good, ids) dim2 = len(idr) print 'Dimensions: Original: %i, Final: %i, Excluded: %i detections. ' % ( dim, dim2, dim - dim2) finalcat = root + 'f0%ip0%i_colorproext_%i_ISO.irmsF814W.free.cat' % ( field, pointing, ccd) data1 = coeio.loaddata(catalog) # Loading the whole catalog content. head = coeio.loadheader(catalog) data2 = data1[good, :] coeio.savedata(data2, finalcat, dir="", header=head) # Saving & creating a new catalog.
def remove_fakeabsorptions_F814W(field, pointing, ccd): """ Using the rmsweight images, it gets rid of detections with imrs_F814W < 0.5. ------------------------------------------- import alhambra_fakedets as AF AF.remove_fakeabsorptions_F814W(2,1,1) """ root = '/Volumes/amb22/catalogos/reduction_v4f/f0%i/' % (field) catalog = root + 'f0%ip0%i_colorproext_%i_ISO.cat' % (field, pointing, ccd) ids, x, y, area = U.get_data(catalog, (0, 3, 4, 5)) dim = len(ids) perc = U.zeros(dim) # Opening F814W Weight image ima = alh.alhambra_invrmsimagelist(field, pointing, ccd)[-1] datos = pyfits.open(ima)[0].data for ii in range(dim): if area[ii] > 1: size = int(round(U.sqrt(area[ii]) / 2.)) xo = x[ii] yo = y[ii] dimx = U.shape(datos[yo - size:yo + size, xo - size:xo + size])[1] dimy = U.shape(datos[yo - size:yo + size, xo - size:xo + size])[0] perc[ii] = (datos[yo - size:yo + size, xo - size:xo + size].sum() / (dimx * dimy * 1.)) # Defining the sample to be keep. good = U.greater(perc, 0.5) idr = U.compress(good, ids) dim2 = len(idr) print 'Dimensions: Original: %i, Final: %i, Excluded: %i detections. ' % ( dim, dim2, dim - dim2) finalcat = root + 'f0%ip0%i_colorproext_%i_ISO.irmsF814W.free.cat' % ( field, pointing, ccd) data1 = coeio.loaddata(catalog) # Loading the whole catalog content. head = coeio.loadheader(catalog) data2 = data1[good, :] coeio.savedata(data2, finalcat, dir="", header=head) # Saving & creating a new catalog.
def __init__(self, run_name, id_str=None, cat=None, probs=None, flux_comparison=None, columns=None, xy_cols=None, spectra='CWWSB_fuv_f_f.list', show_plots=1, save_plots=0, verbose=False): self.run_name = run_name #Define names of data catalogs if cat == None: self.cat = run_name + '.bpz' else: self.cat = bpz self.bpzstr = loadfile(self.cat) self.bpzparams = {} i = 0 while self.bpzstr[i][:2] == '##': line = self.bpzstr[i][2:] if '=' in line: [key, value] = string.split(line, '=') self.bpzparams[key] = value i = i + 1 self.bpzcols = [] while self.bpzstr[i][:2] == '# ': line = self.bpzstr[i][2:] [col, key] = string.split(line) self.bpzcols.append(key) i = i + 1 self.spectra = self.bpzparams.get('SPECTRA', 'CWWSB_fuv_f_f.list') self.columns = self.bpzparams.get('COLUMNS', run_name + '.columns') self.flux_comparison = self.bpzparams.get( 'FLUX_COMPARISON', run_name + '.flux_comparison') self.interp = str2num(self.bpzparams.get('INTERP', '2')) self.verbose = verbose params = params_cl() if verbose: print 'params', params self.bw = 'BW' in params self.thick = 'THICK' in params #Look for the corresponding ID number if ',' in str( id_str ) and xy_cols <> None: #If the input is a pair of X,Y coordinates x, y = map(float, tuple(split(id_str), ',')) x_cat, y_cat = get_data(self.cat, xy_cols) self.id = argmin(dist(x_cat, y_cat, x, y)) + 1 elif id_str == None: self.id = None else: if id_str[-2:] == '.i': self.id = ravel(loaddata(id_str).astype(int)) elif id_str[0] == 'i': #id_str[1:] #self.id = self.id[int(id_str[1:])-1] self.id = id_str elif singlevalue(id_str): self.id = int(id_str) else: self.id = array(id_str).astype(int) self.templates = get_str(sed_dir + self.spectra, 0)
def flux_comparison_plots(self, show_plots=1, save_plots=0, colors={}, nomargins=0, outdir='', redo=False): verbose = self.verbose if verbose: print 'Reading flux comparison data from %s' % self.flux_comparison #Get the flux comparison data #z = zeros(4, Float) #print less(z, 1) all = get_2Darray(self.flux_comparison) #Read the whole file #print 'less' #print less(all, 1) id = all[:, 0] #ID column #Get the row which contains the ID number we are interested in if self.id == None: self.id = id # DEFAULT: DO 'EM ALL if type(self.id) == str: i_ids = [str2num(self.id[1:]) - 1] #i_ids = [int(self.id)] else: try: n = len( self.id) # IF THIS FAILS, THEN IT'S NOT AN ARRAY / LIST i_ids = [] for selfid in self.id: i_id = findmatch1(id, selfid) if i_id == -1: print 'OBJECT #%d NOT FOUND.' % selfid sys.exit() i_ids.append(i_id) except: i_id = findmatch1(id, self.id) if i_id == -1: print 'OBJECT NOT FOUND.' sys.exit() else: i_ids = [i_id] ncols = len(all[0, :]) nf = (ncols - 5) / 3 for i_id in i_ids: if nomargins: figure(1, figsize=(2, 2)) clf() axes([0, 0, 1, 1]) else: #thick(top=0.9) thick() figure(1) clf() ioff() print 'sed plot %d / %d : #%d' % (i_id + 1, len(i_ids), id[i_id]) if save_plots: outimg = self.run_name + '_sed_%d.png' % id[i_id] if exists(join(outdir, outimg)) and not redo: print join(outdir, outimg), 'ALREADY EXISTS' continue ft = all[i_id, 5:5 + nf] # FLUX (from spectrum for that TYPE) fo = all[i_id, 5 + nf:5 + 2 * nf] # FLUX (OBSERVED) efo = all[i_id, 5 + 2 * nf:5 + 3 * nf] # FLUX_ERROR (OBSERVED) #efo = array(efo) #print type(efo) #observed = less(efo, 1) observed = efo < 1 #print 'ft', ft #print 'fo', fo #print 'efo', efo prar = array([ft, fo, efo]) if 0: print ' ft fo efo' print transpose(prar) #Get the redshift, type and magnitude of the galaxy m, z, t = all[i_id, 1], all[i_id, 2], all[i_id, 3] params = params_cl() bpzdata = loaddata(self.bpzstr) if len(bpzdata.shape) == 1: bpzdata.shape = (1, len(bpzdata)) bpzdata = transpose(bpzdata) #odds = bpzdata[self.bpzcols.index('ODDS')] if 'ODDS' in self.bpzcols: i = self.bpzcols.index('ODDS') else: i = self.bpzcols.index('ODDS_1') odds = bpzdata[i] # Z-SPEC if 'Z_S' in self.bpzcols: #bpzdata = transpose(loaddata(self.bpzstr)) zspeccol = self.bpzcols.index('Z_S') zspec = bpzdata[zspeccol] #zspec = ravel(loaddata(params['ZSPEC'])) if 'ZSPEC' in params: z = zspec[i_id] print "z SET TO SPECTROSCOPIC VALUE OF %.3f" % z ## z = 0.1972 ## print "z ARTIFICIALLY SET TO ", z #print "USING INTERP=2 (YOUR RESULTS MAY VARY)" if verbose: print "type=", t interp = self.interp if verbose: print "USING INTERP=%d" % interp t = (t + interp) / (1. * interp + 1) #print 'ARTIFICIALLY SETTING z & TYPE' # NOTE: THIS DOESN'T MOVE THE BLUE BOXES! #z, t = 3.14, 1 #t, z = 5.33, 2.77 #t = (t + 2) / 3. print "%.3f" % t, betweentypes = ndec(t) ## t = int(round(t)) ## print t, sed = self.templates[int(t - 1)] print sed, print "z=", z, #odds = [odds] print 'odds=%.2f' % odds[i_id] #print 'odds=%.2f' % odds if 'Z_S' in self.bpzcols: print 'zspec=%.3f' % zspec[i_id] #Get the filter wavelengths if verbose: print 'Reading filter information from %s' % self.columns filters = get_str( self.columns, 0, nrows=nf, ) lambda_m = ft * 0. for i in range(len(filters)): lambda_m[i] = filter_center(filters[i]) #print filters #print lambda_m # chisq 2 eft = ft / 15. #eft = zeros(nf) + max(eft) #print eft #print max(eft) #print 'eft', eft eft = array([max(eft)] * nf) #print 'eft', eft #print 'efo', efo #eft = ones(nf) * max(eft) #eft = zeros(nf) + max(eft) #ef = sqrt(efo**2 + eft**2) #ef = hypot(eft, eft) #ef = hypot(efo, efo) #print type(efo), type(eft) #efo = array(efo.tolist()) #eft = array(eft.tolist()) ef = hypot(efo, eft) # dfosq = ((ft - fo) / ef)**2 dfosqsum = sum(dfosq) # #observed = less(efo, 1) observed = efo < 1 #print observed nfobs = 0 for obs1 in observed: if obs1: nfobs += 1 #print nfobs #nfobs = sum(observed) # if nfobs > 1: dof = max([nfobs - 3, 1]) # 3 params (z, t, a) chisq2 = dfosqsum / dof elif nfobs: # == 1 chisq2 = 999. else: chisq2 = 9999. print 'chisq2 = ', chisq2 #Convert back to AB magnitudes efo1 = e_frac2mag(efo / fo) efo2 = flux2mag(efo) fo = flux2mag(fo) ft = flux2mag(ft) eft = p2p(ft) / 15. * ones(nf) seen = less(fo, 90) efo = where(seen, efo1, efo2) fo = where(seen, fo, 99) ptitle = "#%d, type=%.2f, bpz=%.2f, odds=%.2f" % (id[i_id], t, z, odds[i_id]) if 'Z_S' in self.bpzcols: ptitle += ", zspec=%.2f" % zspec[i_id] # -DC CORRECTING FLUX ERRORS: # mag = (-99, 0) NOT HANDLED CORRECLY BY BPZ WHEN OUTPUTTING TO .flux_comparison # ASSIGNS LARGE ERROR TO FLUX (DIVIDING BY ZERO MAG_ERROR) # MAYBE bpz SHOULD ASSIGN mag = (-99, maglimit) INSTEAD, LIKE IT DOES FOR mag = (99, #efo = where(less(efo, 1), efo, 0) #fomin = where(fo, fo-efo, 0) #fomax = where(fo, fo+efo, 2*efo) #print sed, z x, y = obs_spectrum_AB(sed, z) if nomargins: ax = gca() ax.set_xticklabels([]) ax.set_yticklabels([]) else: xlabel(r"Wavelength $\lambda$ ($\AA$)") ylabel("Magnitude (AB)") #title(ptitle) if betweentypes: # INTERPOLATE! --DC t1 = int(t) t2 = t1 + 1 sed2 = self.templates[int(t2 - 1)] x2, y2 = obs_spectrum_AB(sed2, z) y2 = match_resol(x2, y2, x) y = (t2 - t) * y + (t - t1) * y2 #Normalize spectrum to model fluxes y_norm = match_resol(x, y, lambda_m) if sum(ft): #dm = mean(y_norm - ft) # Normalize magnitudes #print dm #pause() #y = y - mean(y_norm - ft) # Normalize magnitudes y_norm_seen = compress(seen, y_norm) ft_seen = compress(seen, ft) # # Check for model flux non-detections (may throw everything off!) dm1 = min(y_norm_seen) y_norm_seen = y_norm_seen - dm1 seen2 = less(y_norm_seen, 90) y_norm_seen = compress(seen2, y_norm_seen) ft_seen = compress(seen2, ft_seen) # #print y_norm_seen #print ft_seen dm2 = mean(y_norm_seen - ft_seen) # Normalize magnitudes #print y_norm_seen - ft_seen #print dm2 y = y - dm1 - dm2 y_norm_seen = y_norm_seen - dm2 #print y_norm_seen # # Re-Normalize to brightest: i = argmin(ft_seen) #print ft_seen[i] dm3 = y_norm_seen[i] - ft_seen[i] y = y - dm3 y_norm_seen = y_norm_seen - dm3 #print y_norm_seen #print ft_seen else: print 'OBSERVED FIT MINIZED & COMPRIMISED!!' black = 'grey60' xrange = prange(lambda_m, None, 0.075) xrange = prangelog(lambda_m, None, 0.075) inxrange = between(xrange[0], x, xrange[1]) xinxrange = compress(inxrange, x) yinxrange = compress(inxrange, y) yyy2 = concatenate([fo, ft]) #yyy = concatenate([fo, ft, yinxrange, fo+efo, fo-efo]) #yyy = concatenate([fo, ft, fo+efo, fo-efo, fo-eft, fo+eft]) #yyy = concatenate([fo, ft, fo-eft, fo+eft]) #fog = compress(observed, fo) #efog = compress(observed, efo) fog = compress(observed * seen, fo) efog = compress(observed * seen, efo) ftg = compress(observed * seen, ft) eftg = compress(observed * seen, eft) yyy = concatenate([ftg - eftg, ftg + eftg, fog + efog, fog - efog]) #yyy = concatenate([fog, ft, fog+efog, fog-efog]) #yyy = concatenate([fog, ft, fog+efog+0.2, fog-efog-0.2]) #print yyy, 'yyy' #print minmax(yyy) yrange = prange(yyy) #print yrange yyymax = min( [max(yyy), 1.5 * max(yyy2), 1.5 * max(fo)] ) # cap max, so you don't show big peaks and so you don't squash all efo # THIS SHOULD FORCE THE PLOT WINDOW TO WHAT I WANT # BUT IT DOESN'T QUITE WORK #plot([xrange[0]], [yrange[0]]) #plot([xrange[1]], [yrange[1]]) zorder = -1 if nobox: zorder = 10 plot(xinxrange.tolist(), yinxrange.tolist(), color='gray', zorder=zorder) linewidth = 1.5 + 2 * self.thick fontsize = 3 + 0.5 * self.thick maxy = max(y) indict = os.path.join(os.environ['BPZPATH'], 'plots/filtnicktex.dict') filtdict = loaddict(indict, silent=True) def customfiltcolor1(filt, lam): if lam < 4500: color = 'magenta' elif lam > 10000: color = 'red' elif filt[:3] == 'HST': color = 0, 0.8, 0 elif filt[-4:] == 'LRIS': color = 'orange' else: color = 'gray50' #print color, lam, filt return color def customfiltcolor(filt, lam): color = 'red' for key in colors.keys(): if string.find(filt, key) > -1: color = colors[key] color = str(color) # for 0.50 if string.find(color, ',') > -1: color = tuple(stringsplitatof(color, ',')) break return color #print ' filt lambda fo efo ft chi' #print 'filt lambda fo efo ft chi' if verbose: print ' filt lambda m dm mt chi' for i in range(len(filters)): color = 'red' blue = 'blue' colorful = 'COLORFUL' in params if 1: color = customfiltcolor(filters[i], lambda_m[i]) if filters[i] in filtdict.keys(): filtnick = filtdict[filters[i]][1:-1] else: filtnick = filters[i] if len(filtnick) > 8: filtnick = filtnick[:8] if verbose: print '%8s %7.1f %5.2f %6.3f %5.3f %.3f' \ % (filtnick, lambda_m[i], fo[i], efo[i], ft[i], sqrt(dfosq[i])) ms = [7, 4][nomargins] if observed[i]: # OBSERVED if max(eft) < yyymax * 10: #print 'max(eft) < yyymax*10' rectwidth = .015 * (xrange[1] - xrange[0]) if 1: # logx rectwidthlog = .015 * (log10(xrange[1]) - log10(xrange[0])) rectwidth = rectwidthlog * lambda_m[i] / log10(e) if not nobox: # blue rectangles (model fluxes) rectangle( [lambda_m[i] - rectwidth, ft[i] - eft[i]], [lambda_m[i] + rectwidth, ft[i] + eft[i]], color=blue, linewidth=linewidth) else: print 'NOT max(eft) < yyymax*10' plot([lambda_m[i]], [ft[i]], 'v', markersize=6, markerfacecolor='blue') if fo[i]: # DETECTED zorder = 1 if filters[i][:3] == 'HST': zorder = 5 plot([lambda_m[i]], [fo[i]], 'o', mfc=color, mec=color, ms=12, zorder=zorder) # Set plot limits (at least x) for ploterrorbars xlim(xrange) ylim(yrange[::-1]) if seen[i]: ploterrorbars(array([lambda_m[i]]), array([fo[i]]), array([efo[i]]), color='k', xlog=plotlogx, lw=linewidth, zorder=zorder + 1) else: ploterrorbars(array([lambda_m[i]]), array([99]), array([99 - efo[i]]), color='k', xlog=plotlogx, lw=linewidth, zorder=zorder + 1) yl = min([fo[i], ft[i] - eft[i] * 0.7]) else: # NOT DETECTED if 0: print 'NOT DETECTED' print[lambda_m[i]], [fo[i]] print[lambda_m[i], lambda_m[i]], [0, efo[i]] plot([lambda_m[i]], [fo[i]], '^', markerfacecolor=color, markeredgecolor=color, markersize=ms) plot([lambda_m[i], lambda_m[i]], [0., efo[i]], linewidth=linewidth, color=color) yl = yyymax * 0.04 else: # NOT OBSERVED plot([lambda_m[i]], [0], 'o', markerfacecolor='w', markeredgecolor=color, markersize=ms) if self.thick: plotconfig(696) configure('fontsize_min', 2) configure('fontface', 'HersheySans-Bold') p.frame.spine_style['linewidth'] = 3 p.frame.ticks_style['linewidth'] = 3 p.frame.subticks_style['linewidth'] = 3 if colorful: legend1('DuPont/NOT', 'magenta') legend1('ACS', (0, 0.8, 0)) legend1('Keck', 'orange') legend1('NTT', 'red') if plotlogx: semilogx() retickx() xlim(xrange) ylim(yrange[::-1]) if show_plots: if show_plots == True: print 'KILL PLOT WINOW TO TERMINATE OR CONTINUE.' show() print elif show_plots > 1: print 'Hit <Enter> to CONTINUE.' show() pause() show_plots += 1 # ZSPEC thick bw show? save?:eps/png if save_plots: print 'SAVING', join(outdir, outimg) if save_plots == 'png': savepng(join(outdir, decapfile(outimg))) elif save_plots == 'pdf': savepdf(join(outdir, decapfile(outimg))) else: savepngpdf(join(outdir, decapfile(outimg))) clear() # OR ELSE SUBSEQUENT PLOTS WILL PILE ON #print self.thick print
def get_alhambra_GOLD(field,pointing,ccd): """ import alhambragold as alhgold alhgold.get_alhambra_GOLD(2,1,1) """ root_catalogs = '/Volumes/amb22/catalogos/reduction_v4f/f0%i/'%(field) root_gold = '/Volumes/amb22/catalogos/reduction_v4f/GOLD/' catalog = root_catalogs+'alhambra.F0%iP0%iC0%i.ColorProBPZ.cat' %(field,pointing,ccd) if os.path.exists(catalog): data1 = coeio.loaddata(catalog) # Loading the whole catalog content. head1 = coeio.loadheader(catalog) # Loading the original header. nc1 = len(data1.T) dim1 = len(data1[:,0]) nh = len(head1) # Final catalog. catout = root_gold+'alhambra.gold.F0%iP0%iC0%i.ColorProBPZ.cat' %(field,pointing,ccd) outfile = open(catout,'w') # Reducing the length of the catalogs according to input ids ids = U.get_str(catalog,0) mo = U.get_data(catalog,65) cond1 = U.less(mo,23.000) data2 = data1[cond1,:] nraws = U.shape(data2)[0] ncols = U.shape(data2)[1] # Setting the IDs to its final values (including F814W+field+pointing+ccd) finalids = alh.getalhambrafinalids(field,pointing,ccd,'ISO') finalids2 = U.compress(cond1,finalids) # Restoring header... for ii in range(nh): outfile.write('%s \n'%(head1[ii])) formato = '%s %i %i %i %.4f %.4f %.3f %.3f %i %.2f %.2f %.4f %.3f %.3f %.1f %.2f %.3f %.2f %i ' formato += '%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f ' formato += '%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f ' formato += '%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f ' formato += '%.3f %.3f %.3f ' formato += '%i %i %.3f %i %.2f %i ' formato += '%.3f %.3f %.3f %.3f %.3f %.3f %.3f ' formato += '%.3f %.3f %.3f %.3f ' formato += '%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f ' formato += '%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f ' formato += '%.3f %.3f %.3f %.3f %i %i ' form = formato.split() # Here it defines the format to be used. for jj in range(nraws): for ss in range(ncols): goodform = '' goodform = form[ss]+' ' if ss == 0: outfile.write(goodform%(int(finalids2[jj]))) else: outfile.write(goodform%(data2[jj,ss])) outfile.write(' \n') outfile.close()
def append_IDs2_3arcs_catalogues(cat, ids): """ import alhambra_3arcs as A3 cat = '/Volumes/amb22/catalogos/reduction_v4f/3arcs/f02/alhambra.f02p02c01.3arcs.cat' ids = U.get_str('/Volumes/amb22/catalogos/reduction_v4f/3arcs/f02/alhambra.f02p01c01.3arcs.idsfrommatch.bis.cat',0) A3.append_IDs2_3arcs_catalogues(cat,ids) """ data1 = C.loaddata(cat) # Output name catout = cat[:-3] + 'final.cat' outfile = open(catout, 'w') newheader = """# 1 ID # 2 RA # 3 DEC # 4 X # 5 Y # 6 Area # 7 F365W_1_3arcs # 8 dF365W_1_3arcs # 9 F396W_1_3arcs # 10 dF396W_1_3arcs # 11 F427W_1_3arcs # 12 dF427W_1_3arcs # 13 F458W_1_3arcs # 14 dF458W_1_3arcs # 15 F489W_1_3arcs # 16 dF489W_1_3arcs # 17 F520W_1_3arcs # 18 dF520W_1_3arcs # 19 F551W_1_3arcs # 20 dF551W_1_3arcs # 21 F582W_1_3arcs # 22 dF582W_1_3arcs # 23 F613W_1_3arcs # 24 dF613W_1_3arcs # 25 F644W_1_3arcs # 26 dF644W_1_3arcs # 27 F675W_1_3arcs # 28 dF675W_1_3arcs # 29 F706W_1_3arcs # 30 dF706W_1_3arcs # 31 F737W_1_3arcs # 32 dF737W_1_3arcs # 33 F768W_1_3arcs # 34 dF768W_1_3arcs # 35 F799W_1_3arcs # 36 dF799W_1_3arcs # 37 F830W_1_3arcs # 38 dF830W_1_3arcs # 39 F861W_1_3arcs # 40 dF861W_1_3arcs # 41 F892W_1_3arcs # 42 dF892W_1_3arcs # 43 F923W_1_3arcs # 44 dF923W_1_3arcs # 45 F954W_1_3arcs # 46 dF954W_1_3arcs # 47 J_1_3arcs # 48 dJ_1_3arcs # 49 H_1_3arcs # 50 dH_1_3arcs # 51 KS_1_3arcs # 52 dKS_1_3arcs # 53 F814W_1_3arcs # 54 dF814W_1_3arcs # # ID RA DEC X Y Area F365W_1_3arcs dF365W_1_3arcs F396W_1_3arcs dF396W_1_3arcs F427W_1_3arcs dF427W_1_3arcs F458W_1_3arcs dF458W_1_3arcs F489W_1_3arcs dF489W_1_3arcs F520W_1_3arcs dF520W_1_3arcs F551W_1_3arcs dF551W_1_3arcs F582W_1_3arcs dF582W_1_3arcs F613W_1_3arcs dF613W_1_3arcs F644W_1_3arcs dF644W_1_3arcs F675W_1_3arcs dF675W_1_3arcs F706W_1_3arcs dF706W_1_3arcs F737W_1_3arcs dF737W_1_3arcs F768W_1_3arcs dF768W_1_3arcs F799W_1_3arcs dF799W_1_3arcs F830W_1_3arcs dF830W_1_3arcs F861W_1_3arcs dF861W_1_3arcs F892W_1_3arcs dF892W_1_3arcs F923W_1_3arcs dF923W_1_3arcs F954W_1_3arcs dF954W_1_3arcs J_1_3arcs dJ_1_3arcs H_1_3arcs dH_1_3arcs KS_1_3arcs dKS_1_3arcs F814W_1_3arcs dF814W_1_3arcs""" nh = newheader.split('\pp') formato = '%s %.4f %.4f %.3f %.3f %i ' formato += '%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f ' formato += '%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f ' formato += '%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f ' form = formato.split() nraws = U.shape(data1)[0] ncols = U.shape(data1)[1] # print 'nraws,ncols',nraws,ncols for ii in range(len(nh)): outfile.write('%s \n' % (nh[ii])) # Here it defines the format to be used. for jj in range(nraws): for ss in range(ncols): # print jj,nraws goodform = '' goodform = form[ss] + ' ' if ss == 0: outfile.write(goodform % (int(ids[jj]))) else: outfile.write(goodform % (data1[jj, ss])) outfile.write(' \n') outfile.close()
import numpy as N import coeio as C import useful as U root_to_cat = '/Users/albertomolino/doctorado/articulos/SPLUS/' root_to_cat += 'calibration/sample4simulations/data/20180329/NGSL_convolved/' #catalog = root_to_cat + 'SPLUS_SDSS_filters_4Laura.list_sample_stars_' #catalog += 'SDSSDR10_aa_chi2_red_min.eq.0.03_20102017.cat' catalog = root_to_cat + 'sample_NGSL_convolved_29032018.dat' new_catalog = catalog[:-3]+'extended.cat' # display information? verbose = 0 # Reading the catalogue. datos = C.loaddata(catalog) head = C.loadheader(catalog) # Reading catalog dimensions. n_stars_ori = N.shape(datos)[0] n_cols = N.shape(datos)[1] n_filters = 12 #20 ##################################### # Magnitude range we want to expand. m_min = 12 #################################### m_max = 21 #################################### delta_m = 0.01 #################################### base_m = N.arange(m_min,m_max+delta_m,delta_m) n_m_ele = len(base_m) # Number of stars per magnitude-bin.
def check_variable_candidate(alhambraid): """ It replaces failed magnitudes in the ALHAMBRA catalogues (artificial absorptions, non-observed sources assigned as non-detected with upper limits) by m=-99,em=99 It might decrease the amount of low Odds at bright magnitudes. ---- import alhambra_photools as A A.replacing_fakeabsorptions(2,1,2) A.check_sample(image,catalog,posID,posX,posY,posMAG) A.alhambra_id_finder(ra1,dec1) idd = int(id[pos]) A.alhambra_colorstamp_byID(id) f,p,c,ids = A.alhambra_id_finder(37.4992,1.2482) """ field = int(str(alhambraid)[3]) pointing = int(str(alhambraid)[4]) ccd = int(str(alhambraid)[5]) numero = str(alhambraid)[-5:] print numero for ii in range(2): if numero[0] == '0': numero = numero[1:] print numero root2cats = '/Volumes/amb22/catalogos/reduction_v4f/f0%i/' % (field) catalog = root2cats + 'originals/f0%ip0%i_colorproext_%i_ISO.cat' % ( field, pointing, ccd) cols1 = root2cats + 'f0%ip0%i_%i_tot_ISO_eB10.columns' % (field, pointing, ccd) cols2 = root2cats + 'f0%ip0%i_colorproext_%i_ISO_phz_eB10.columns' % ( field, pointing, ccd) if os.path.exists(cols1): columns = cols1 else: columns = cols2 filters = B.get_filter_list(columns) print filters data = C.loaddata(catalog) # Loading the whole catalog content. head = C.loadheader(catalog) # Loading the original header. m = A.get_magnitudes(catalog, columns) # em = get_errmagnitudes(catalog,columns) root2 = '/Volumes/amb22/catalogos/reduction_v4e/' fluxc1 = root2 + 'f0%i/f0%ip0%i_colorproext_%i_ISO_phz_eB11.flux_comparison' % ( field, field, pointing, ccd) fluxc2 = root2 + 'f0%i/f0%ip0%i_colorproext_%i_ISO.flux_comparison' % ( field, field, pointing, ccd) if os.path.exists(fluxc1): fluxc = fluxc1 else: fluxc = fluxc2 ido, ftt, foo, efoo, zb, tb, mm = P.get_usefulfluxcomparison( columns, fluxc) pos = A.get_position(ido, int(numero)) print ido[pos], mm[pos] plt.figure(1, figsize=(10, 7), dpi=80, facecolor='w', edgecolor='k') plt.clf() # P.plot1sedfitting(foo[:,jj],efoo[:,jj],ftt[:,jj],zb[jj],tb[jj],root_bpz_sed+'eB11.list',filters) plt.plot(U.arange(20) + 1, foo[0:20, pos], 'k-', alpha=0.4, lw=6) plt.plot(U.arange(20) + 1, foo[0:20, pos], 'ko', alpha=0.4, ms=12) plt.errorbar(U.arange(20) + 1, foo[0:20, pos], (efoo[0:20, pos] / 1.), fmt="ko", alpha=0.4, ms=10) minf = (foo[0:20, pos].min()) * 1.1 maxf = (foo[0:20, pos].max()) * 1.1 maxef = (efoo[0:20, pos].max()) * 1.1 # plt.ylim(minf-maxef,maxf+maxef) plt.xlim(0, 21) plt.xlabel('Filter', size=25) plt.ylabel('Flux', size=25) plt.legend(['Magnitude: %.2f' % (m[pos][-1])], loc='upper right', numpoints=1, fontsize=20) plt.title(alhambraid, size=25) plt.grid() plt.show() namefig = '/Users/albertomolino/doctorado/photo/variability/analysis/vocheck.ID%s.png' % ( alhambraid) plt.savefig(namefig, dpi=125) outcat = '/Users/albertomolino/doctorado/photo/variability/analysis/vocheck.ID%s.cat' % ( alhambraid) A.select_rows_bylist_pro(catalog, ido[pos], outcat) print ' '
""" # This loop was needed for the original version # where the FIELDS were strings rather than integers. tiles_name = U.get_str(mastercat,0) nt = len(tiles_name) tiles = N.zeros(nt) for ss in range(nt): tiles[ss] = int(tiles_name[ss].split('-')[-1]) """ tiles = U.get_data(mastercat, 0) # use get_str with original cat! single_tiles = N.unique(tiles).astype(int) n_single_tiles = len(single_tiles) # Reading entire catalogue. header_mastercat = C.loadheader(mastercat) data_mastercat = C.loaddata(mastercat) for ii in range(n_single_tiles): tile_cat = root2cat + 'tile%i_S82.cat' % (single_tiles[ii]) good = N.equal(tiles, single_tiles[ii]) n_gals = len(data_mastercat[good, 1]) if not os.path.exists(tile_cat): """ fwhm = data_mastercat[good,9] magr = data_mastercat[good,82] # Petro seeing,stars = sct.get_seeing_from_data_pro(fwhm,magr) fwhm_norm = fwhm /(1.*seeing) data_mastercat[good,9] = fwhm_norm data_mastercat[good,0] = N.ones(n_gals) * int(single_tiles[ii]) """ C.savedata(data_mastercat[good, :],
def replace_fakeabsorptions_pro(field, pointing, ccd): """ Updated version to get rid of detections with imrs_* < 0.5 setting their magnitudes to m=-99,em=0.0. Additionally,it removes detections with imrs_F814W == 0.0 ---------------------------------------------------------------------------- It replaces failed magnitudes in the ALHAMBRA catalogues (artificial absorptions, non-observed sources assigned as non-detected with upper limits) by m=-99,em=99 It might decrease the amount of low Odds at bright magnitudes. ---- import alhambra_photools as A A.replace_fakeabsorptions_pro(2,1,2) """ plots = 1 root = '/Volumes/amb22/catalogos/reduction_v4f/f0%i/' % (field) catalog = root + 'f0%ip0%i_colorproext_%i_ISO.irmsF814W.free.cat' % ( field, pointing, ccd) catweight = root + 'f0%ip0%i_ColorProBPZ_%i_ISO.rmsweights.dat' % ( field, pointing, ccd) dataweight = coeio.loaddata(catweight) # print U.shape(dataweight) # ids,x,y,area = U.get_data(catalog,(0,3,4,5)) cols1 = root + 'f0%ip0%i_%i_tot_ISO_eB10.columns' % (field, pointing, ccd) cols2 = root + 'f0%ip0%i_colorproext_%i_ISO_phz_eB10.columns' % ( field, pointing, ccd) if os.path.exists(cols1): columns = cols1 else: columns = cols2 data = coeio.loaddata(catalog) # Loading the whole catalog content. head = coeio.loadheader(catalog) # Loading the original header. vars, evars, posref, zpe, zpo = alh.get_usefulcolumns(columns) # print 'dim vars', len(vars) mags = data[:, vars] nl = U.shape(data)[ 0] # nl is the number of detections inside every single band. nf = len(vars) # nf is the number of bands inside the catalog. # print 'nl,nf: ',nl,nf kk = 0 for jj in range(nl): filtoff = 0 for ii in range(nf): pos_mag = vars[ii] pos_emag = evars[ii] if dataweight[jj, ii] < 0.5: # print data[jj,pos_mag],pos_mag,pos_emag data[jj, pos_mag] = -99.0000 data[jj, pos_emag] = 0.0000 data[jj, 67] -= 1 kk += 1 filtoff += 1 # print data[jj,0] # print data[jj,pos_mag] # if filtoff > 0: # print '%i excluded for detection %i: '%(filtoff,data[jj,0]) # # pausa = raw_input('paused') print 'Replaced %i magnitudes. ' % (kk) # New values of mags error overwrites now the original data. finalcatalog = root + 'f0%ip0%i_colorproext_%i_ISO.test.cat' % ( field, pointing, ccd) coeio.savedata(data, finalcatalog, dir="", header=head) # Saving & creating a new catalog.
import numpy as N import coeio as C import useful as U root_to_cat = '/Users/albertomolino/doctorado/articulos/SPLUS/' root_to_cat += 'calibration/sample4simulations/data/20180329/NGSL_convolved/' catalog = root_to_cat + 'sample_final_models_convolved_04042018.cat' catalog_sdss_colors = root_to_cat + 'IveReal_GR_normed.cat' models = U.get_str(root_to_cat + 'ngsl_pick_wd.list', 0) new_catalog = catalog[:-3] + 'extended_gr.cat' # display information? verbose = 0 # Reading the general catalogue. datos_main = C.loaddata(catalog) head_main = C.loadheader(catalog) # gr synthetic models #gr_models = datos_main[:,6]-datos_main[:,8] #SPLUS gr_models = datos_main[:, 16] - datos_main[:, 17] #SDSS # Reading the SDSS_color catalog. sdss_gr, density_gr = U.get_data(catalog_sdss_colors, (0, 1)) n_color_bins = len(sdss_gr) - 1 # Reading catalog dimensions. n_stars_ori = N.shape(datos_main)[0] n_cols = N.shape(datos_main)[1] n_filters = n_cols - 1 #12 #20 #####################################
def __init__(self, run_name, id_str=None, cat=None,probs=None,flux_comparison=None,columns=None, xy_cols=None,spectra='CWWSB_fuv_f_f.list', show_plots=1,save_plots=0,verbose=False ): self.run_name=run_name #Define names of data catalogs if cat == None: self.cat=run_name+'.bpz' else: self.cat =bpz self.bpzstr = loadfile(self.cat) self.bpzparams = {} i = 0 while self.bpzstr[i][:2] == '##': line = self.bpzstr[i][2:] if '=' in line: [key, value] = string.split(line, '=') self.bpzparams[key] = value i = i + 1 self.bpzcols = [] while self.bpzstr[i][:2] == '# ': line = self.bpzstr[i][2:] [col, key] = string.split(line) self.bpzcols.append(key) i = i + 1 self.spectra = self.bpzparams.get('SPECTRA', 'CWWSB_fuv_f_f.list') self.columns = self.bpzparams.get('COLUMNS', run_name+'.columns') self.flux_comparison = self.bpzparams.get('FLUX_COMPARISON', run_name+'.flux_comparison') self.interp = str2num(self.bpzparams.get('INTERP', '2')) self.verbose = verbose params = params_cl() if verbose: print 'params', params self.bw = 'BW' in params self.thick = 'THICK' in params #Look for the corresponding ID number if ',' in str(id_str) and xy_cols<>None: #If the input is a pair of X,Y coordinates x,y=map(float,tuple(split(id_str),',')) x_cat,y_cat=get_data(self.cat,xy_cols) self.id=argmin(dist(x_cat,y_cat,x,y))+1 elif id_str == None: self.id = None else: if id_str[-2:] == '.i': self.id = ravel(loaddata(id_str).astype(int)) elif id_str[0] == 'i': #id_str[1:] #self.id = self.id[int(id_str[1:])-1] self.id = id_str elif singlevalue(id_str): self.id=int(id_str) else: self.id = array(id_str).astype(int) self.templates=get_str(sed_dir+self.spectra,0)
def flux_comparison_plots(self,show_plots=1,save_plots=0,colors={},nomargins=0,outdir='',redo=False): verbose = self.verbose if verbose: print 'Reading flux comparison data from %s' % self.flux_comparison #Get the flux comparison data #z = zeros(4, Float) #print less(z, 1) all=get_2Darray(self.flux_comparison) #Read the whole file #print 'less' #print less(all, 1) id=all[:,0] #ID column #Get the row which contains the ID number we are interested in if self.id == None: self.id = id # DEFAULT: DO 'EM ALL if type(self.id) == str: i_ids = [str2num(self.id[1:])-1] #i_ids = [int(self.id)] else: try: n = len(self.id) # IF THIS FAILS, THEN IT'S NOT AN ARRAY / LIST i_ids = [] for selfid in self.id: i_id = findmatch1(id, selfid) if i_id == -1: print 'OBJECT #%d NOT FOUND.' % selfid sys.exit() i_ids.append(i_id) except: i_id = findmatch1(id, self.id) if i_id == -1: print 'OBJECT NOT FOUND.' sys.exit() else: i_ids = [i_id] ncols=len(all[0,:]) nf=(ncols-5)/3 for i_id in i_ids: if nomargins: figure(1, figsize=(2,2)) clf() axes([0,0,1,1]) else: #thick(top=0.9) thick() figure(1) clf() ioff() print 'sed plot %d / %d : #%d' % (i_id+1, len(i_ids), id[i_id]) if save_plots: outimg = self.run_name+'_sed_%d.png' % id[i_id] if exists(join(outdir, outimg)) and not redo: print join(outdir, outimg), 'ALREADY EXISTS' continue ft=all[i_id,5:5+nf] # FLUX (from spectrum for that TYPE) fo=all[i_id,5+nf:5+2*nf] # FLUX (OBSERVED) efo=all[i_id,5+2*nf:5+3*nf] # FLUX_ERROR (OBSERVED) #efo = array(efo) #print type(efo) #observed = less(efo, 1) observed = efo < 1 #print 'ft', ft #print 'fo', fo #print 'efo', efo prar = array([ft, fo, efo]) if 0: print ' ft fo efo' print transpose(prar) #Get the redshift, type and magnitude of the galaxy m,z,t=all[i_id,1],all[i_id,2],all[i_id,3] params = params_cl() bpzdata = loaddata(self.bpzstr) if len(bpzdata.shape) == 1: bpzdata.shape = (1, len(bpzdata)) bpzdata = transpose(bpzdata) #odds = bpzdata[self.bpzcols.index('ODDS')] if 'ODDS' in self.bpzcols: i = self.bpzcols.index('ODDS') else: i = self.bpzcols.index('ODDS_1') odds = bpzdata[i] # Z-SPEC if 'Z_S' in self.bpzcols: #bpzdata = transpose(loaddata(self.bpzstr)) zspeccol = self.bpzcols.index('Z_S') zspec = bpzdata[zspeccol] #zspec = ravel(loaddata(params['ZSPEC'])) if 'ZSPEC' in params: z = zspec[i_id] print "z SET TO SPECTROSCOPIC VALUE OF %.3f" % z ## z = 0.1972 ## print "z ARTIFICIALLY SET TO ", z #print "USING INTERP=2 (YOUR RESULTS MAY VARY)" if verbose: print "type=", t interp = self.interp if verbose: print "USING INTERP=%d" % interp t = (t + interp) / (1.*interp + 1) #print 'ARTIFICIALLY SETTING z & TYPE' # NOTE: THIS DOESN'T MOVE THE BLUE BOXES! #z, t = 3.14, 1 #t, z = 5.33, 2.77 #t = (t + 2) / 3. print "%.3f" % t, betweentypes = ndec(t) ## t = int(round(t)) ## print t, sed=self.templates[int(t-1)] print sed, print "z=", z, #odds = [odds] print 'odds=%.2f' % odds[i_id] #print 'odds=%.2f' % odds if 'Z_S' in self.bpzcols: print 'zspec=%.3f' % zspec[i_id] #Get the filter wavelengths if verbose: print 'Reading filter information from %s' % self.columns filters=get_str(self.columns,0,nrows=nf,) lambda_m=ft*0. for i in range(len(filters)): lambda_m[i]=filter_center(filters[i]) #print filters #print lambda_m # chisq 2 eft = ft / 15. #eft = zeros(nf) + max(eft) #print eft #print max(eft) #print 'eft', eft eft = array([max(eft)] * nf) #print 'eft', eft #print 'efo', efo #eft = ones(nf) * max(eft) #eft = zeros(nf) + max(eft) #ef = sqrt(efo**2 + eft**2) #ef = hypot(eft, eft) #ef = hypot(efo, efo) #print type(efo), type(eft) #efo = array(efo.tolist()) #eft = array(eft.tolist()) ef = hypot(efo, eft) # dfosq = ((ft - fo) / ef) ** 2 dfosqsum = sum(dfosq) # #observed = less(efo, 1) observed = efo < 1 #print observed nfobs = 0 for obs1 in observed: if obs1: nfobs += 1 #print nfobs #nfobs = sum(observed) # if nfobs > 1: dof = max([nfobs - 3, 1]) # 3 params (z, t, a) chisq2 = dfosqsum / dof elif nfobs: # == 1 chisq2 = 999. else: chisq2 = 9999. print 'chisq2 = ', chisq2 #Convert back to AB magnitudes efo1 = e_frac2mag(efo/fo) efo2 = flux2mag(efo) fo = flux2mag(fo) ft = flux2mag(ft) eft = p2p(ft) / 15. * ones(nf) seen = less(fo, 90) efo = where(seen, efo1, efo2) fo = where(seen, fo, 99) ptitle = "#%d, type=%.2f, bpz=%.2f, odds=%.2f" % (id[i_id], t, z, odds[i_id]) if 'Z_S' in self.bpzcols: ptitle += ", zspec=%.2f" % zspec[i_id] # -DC CORRECTING FLUX ERRORS: # mag = (-99, 0) NOT HANDLED CORRECLY BY BPZ WHEN OUTPUTTING TO .flux_comparison # ASSIGNS LARGE ERROR TO FLUX (DIVIDING BY ZERO MAG_ERROR) # MAYBE bpz SHOULD ASSIGN mag = (-99, maglimit) INSTEAD, LIKE IT DOES FOR mag = (99, #efo = where(less(efo, 1), efo, 0) #fomin = where(fo, fo-efo, 0) #fomax = where(fo, fo+efo, 2*efo) #print sed, z x,y=obs_spectrum_AB(sed,z) if nomargins: ax = gca() ax.set_xticklabels([]) ax.set_yticklabels([]) else: xlabel(r"Wavelength $\lambda$ ($\AA$)") ylabel("Magnitude (AB)") #title(ptitle) if betweentypes: # INTERPOLATE! --DC t1 = int(t) t2 = t1 + 1 sed2=self.templates[int(t2-1)] x2,y2 = obs_spectrum_AB(sed2,z) y2 = match_resol(x2,y2,x) y = (t2 - t) * y + (t - t1) * y2 #Normalize spectrum to model fluxes y_norm=match_resol(x,y,lambda_m) if sum(ft): #dm = mean(y_norm - ft) # Normalize magnitudes #print dm #pause() #y = y - mean(y_norm - ft) # Normalize magnitudes y_norm_seen = compress(seen, y_norm) ft_seen = compress(seen, ft) # # Check for model flux non-detections (may throw everything off!) dm1 = min(y_norm_seen) y_norm_seen = y_norm_seen - dm1 seen2 = less(y_norm_seen, 90) y_norm_seen = compress(seen2, y_norm_seen) ft_seen = compress(seen2, ft_seen) # #print y_norm_seen #print ft_seen dm2 = mean(y_norm_seen - ft_seen) # Normalize magnitudes #print y_norm_seen - ft_seen #print dm2 y = y - dm1 - dm2 y_norm_seen = y_norm_seen - dm2 #print y_norm_seen # # Re-Normalize to brightest: i = argmin(ft_seen) #print ft_seen[i] dm3 = y_norm_seen[i] - ft_seen[i] y = y - dm3 y_norm_seen = y_norm_seen - dm3 #print y_norm_seen #print ft_seen else: print 'OBSERVED FIT MINIZED & COMPRIMISED!!' black = 'grey60' xrange = prange(lambda_m, None, 0.075) xrange = prangelog(lambda_m, None, 0.075) inxrange = between(xrange[0], x, xrange[1]) xinxrange = compress(inxrange, x) yinxrange = compress(inxrange, y) yyy2 = concatenate([fo, ft]) #yyy = concatenate([fo, ft, yinxrange, fo+efo, fo-efo]) #yyy = concatenate([fo, ft, fo+efo, fo-efo, fo-eft, fo+eft]) #yyy = concatenate([fo, ft, fo-eft, fo+eft]) #fog = compress(observed, fo) #efog = compress(observed, efo) fog = compress(observed*seen, fo) efog = compress(observed*seen, efo) ftg = compress(observed*seen, ft) eftg = compress(observed*seen, eft) yyy = concatenate([ftg-eftg, ftg+eftg, fog+efog, fog-efog]) #yyy = concatenate([fog, ft, fog+efog, fog-efog]) #yyy = concatenate([fog, ft, fog+efog+0.2, fog-efog-0.2]) #print yyy, 'yyy' #print minmax(yyy) yrange = prange(yyy) #print yrange yyymax = min([max(yyy), 1.5*max(yyy2), 1.5*max(fo)]) # cap max, so you don't show big peaks and so you don't squash all efo # THIS SHOULD FORCE THE PLOT WINDOW TO WHAT I WANT # BUT IT DOESN'T QUITE WORK #plot([xrange[0]], [yrange[0]]) #plot([xrange[1]], [yrange[1]]) zorder = -1 if nobox: zorder = 10 plot(xinxrange.tolist(), yinxrange.tolist(), color='gray', zorder=zorder) linewidth = 1.5 + 2 * self.thick fontsize = 3 + 0.5 * self.thick maxy = max(y) indict = os.path.join(os.environ['BPZPATH'], 'plots/filtnicktex.dict') filtdict = loaddict(indict, silent=True) def customfiltcolor1(filt, lam): if lam < 4500: color = 'magenta' elif lam > 10000: color = 'red' elif filt[:3] == 'HST': color = 0, 0.8, 0 elif filt[-4:] == 'LRIS': color = 'orange' else: color = 'gray50' #print color, lam, filt return color def customfiltcolor(filt, lam): color = 'red' for key in colors.keys(): if string.find(filt, key) > -1: color = colors[key] color = str(color) # for 0.50 if string.find(color, ',') > -1: color = tuple(stringsplitatof(color, ',')) break return color #print ' filt lambda fo efo ft chi' #print 'filt lambda fo efo ft chi' if verbose: print ' filt lambda m dm mt chi' for i in range(len(filters)): color = 'red' blue = 'blue' colorful = 'COLORFUL' in params if 1: color = customfiltcolor(filters[i], lambda_m[i]) if filters[i] in filtdict.keys(): filtnick = filtdict[filters[i]][1:-1] else: filtnick = filters[i] if len(filtnick) > 8: filtnick = filtnick[:8] if verbose: print '%8s %7.1f %5.2f %6.3f %5.3f %.3f' \ % (filtnick, lambda_m[i], fo[i], efo[i], ft[i], sqrt(dfosq[i])) ms = [7,4][nomargins] if observed[i]: # OBSERVED if max(eft) < yyymax*10: #print 'max(eft) < yyymax*10' rectwidth = .015 * (xrange[1] - xrange[0]) if 1: # logx rectwidthlog = .015 * (log10(xrange[1]) - log10(xrange[0])) rectwidth = rectwidthlog * lambda_m[i] / log10(e) if not nobox: # blue rectangles (model fluxes) rectangle([lambda_m[i]-rectwidth, ft[i]-eft[i]], [lambda_m[i]+rectwidth, ft[i]+eft[i]], color=blue, linewidth=linewidth) else: print 'NOT max(eft) < yyymax*10' plot([lambda_m[i]], [ft[i]], 'v', markersize=6, markerfacecolor='blue') if fo[i]: # DETECTED zorder = 1 if filters[i][:3] == 'HST': zorder = 5 plot([lambda_m[i]], [fo[i]], 'o', mfc=color, mec=color, ms=12, zorder=zorder) # Set plot limits (at least x) for ploterrorbars xlim(xrange) ylim(yrange[::-1]) if seen[i]: ploterrorbars(array([lambda_m[i]]), array([fo[i]]), array([efo[i]]), color='k', xlog=plotlogx, lw=linewidth, zorder=zorder+1) else: ploterrorbars(array([lambda_m[i]]), array([99]), array([99-efo[i]]), color='k', xlog=plotlogx, lw=linewidth, zorder=zorder+1) yl = min([fo[i], ft[i]-eft[i]*0.7]) else: # NOT DETECTED if 0: print 'NOT DETECTED' print [lambda_m[i]], [fo[i]] print [lambda_m[i], lambda_m[i]], [0, efo[i]] plot([lambda_m[i]], [fo[i]], '^', markerfacecolor=color, markeredgecolor=color, markersize=ms) plot([lambda_m[i], lambda_m[i]], [0., efo[i]], linewidth=linewidth, color=color) yl = yyymax*0.04 else: # NOT OBSERVED plot([lambda_m[i]], [0], 'o', markerfacecolor='w', markeredgecolor=color, markersize=ms) if self.thick: plotconfig(696) configure('fontsize_min', 2) configure('fontface', 'HersheySans-Bold') p.frame.spine_style['linewidth'] = 3 p.frame.ticks_style['linewidth'] = 3 p.frame.subticks_style['linewidth'] = 3 if colorful: legend1('DuPont/NOT', 'magenta') legend1('ACS', (0,0.8,0)) legend1('Keck', 'orange') legend1('NTT', 'red') if plotlogx: semilogx() retickx() xlim(xrange) ylim(yrange[::-1]) if show_plots: if show_plots == True: print 'KILL PLOT WINOW TO TERMINATE OR CONTINUE.' show() print elif show_plots > 1: print 'Hit <Enter> to CONTINUE.' show() pause() show_plots += 1 # ZSPEC thick bw show? save?:eps/png if save_plots: print 'SAVING', join(outdir, outimg) if save_plots == 'png': savepng(join(outdir, decapfile(outimg))) elif save_plots == 'pdf': savepdf(join(outdir, decapfile(outimg))) else: savepngpdf(join(outdir, decapfile(outimg))) clear() # OR ELSE SUBSEQUENT PLOTS WILL PILE ON #print self.thick print
#spectra = 'COSMOSeB11new_recal' spectra = 'eB11' # J-PLUS related data mainroot = '/Users/albertomolino/Postdoc/T80S_Pipeline/Commisioning/S82/Dec2017/' root2cats = mainroot + 'splus_cats_NGSL/JPLUS_specz_sample_March2018/' jplus_spz_cat = root2cats + 'jplus2sdssdr12_z05.cat' jplus_spz_columns = root2cats + 'jplus2sdssdr12_z05.columns' tiles = U.get_data(jplus_spz_cat, 1) single_tiles = N.unique(tiles).astype(int) n_single_tiles = len(single_tiles) # Reading entire catalogue. header_jplus = C.loadheader(jplus_spz_cat) data_jplus = C.loaddata(jplus_spz_cat) #C.savedata(data1[sample,:],outname,dir="",header=head1) #n_single_tiles = 1 for ii in range(n_single_tiles): tile_jplus_spz_upl_cat = root2cats + 'tile_%i_jplus2sdssdr12_z05.ecor.upp.cat' % ( single_tiles[ii]) good = N.equal(tiles, single_tiles[ii]) n_gals = len(data_jplus[good, 0]) if n_gals > 50: if not os.path.exists(tile_jplus_spz_upl_cat): tile_jplus_spz_cat = root2cats + 'tile_%i_jplus2sdssdr12_z05.cat' % ( single_tiles[ii]) # Individual (tile) catalogue. if not os.path.exists(tile_jplus_spz_cat):