def rrhr(band,skypos,tranges,skyrange,width=False,height=False,stepsz=1., verbose=0,response=True,hdu=False,retries=20): """Generate a high resolution relative response (rrhr) map.""" imsz = gxt.deg2pix(skypos,skyrange) # TODO the if width / height flat, flatinfo = cal.flat(band) npixx,npixy = flat.shape fltsz = flat.shape pixsz = flatinfo['CDELT2'] detsize = 1.25 # Rotate the flat into the correct orientation to start. flat = np.flipud(np.rot90(flat)) # NOTE: This upsample interpolation is done _last_ in the canonical # pipeline as part of the poissonbg.c routine. # The interpolation function is "congrid" in the same file. # TODO: Should this be first order interpolation? (i.e. bilinear) hrflat = scipy.ndimage.interpolation.zoom(flat,4.,order=0,prefilter=False) img = np.zeros(hrflat.shape)[ hrflat.shape[0]/2.-imsz[0]/2.:hrflat.shape[0]/2.+imsz[0]/2., hrflat.shape[1]/2.-imsz[1]/2.:hrflat.shape[1]/2+imsz[1]/2.] for trange in tranges: t0,t1=trange entries = gQuery.getArray(gQuery.aspect(t0,t1),retries=retries) n = len(entries) asptime = np.float64(np.array(entries)[:,2])/tscale aspra = np.float32(np.array(entries)[:,3]) aspdec = np.float32(np.array(entries)[:,4]) asptwist= np.float32(np.array(entries)[:,5]) aspflags= np.float32(np.array(entries)[:,6]) asptwist= np.float32(np.array(entries)[:,9]) aspra0 = np.zeros(n)+skypos[0] aspdec0 = np.zeros(n)+skypos[1] xi_vec, eta_vec = gnomonic.gnomfwd_simple( aspra,aspdec,aspra0,aspdec0,-asptwist,1.0/36000.,0.) col = 4.*( ((( xi_vec/36000.)/(detsize/2.)*(detsize/(fltsz[0]*pixsz)) + 1.)/2. * fltsz[0]) - (fltsz[0]/2.) ) row = 4.*( (((eta_vec/36000.)/(detsize/2.)*(detsize/(fltsz[1]*pixsz)) + 1.)/2. * fltsz[1]) - (fltsz[1]/2.) ) vectors = mc.rotvec(np.array([col,row]),-asptwist) for i in range(n): if verbose>1: mc.print_inline('Stamping '+str(asptime[i])) # FIXME: Clean this mess up a little just for clarity. img += scipy.ndimage.interpolation.shift(scipy.ndimage.interpolation.rotate(hrflat,-asptwist[i],reshape=False,order=0,prefilter=False),[vectors[1,i],vectors[0,i]],order=0,prefilter=False)[hrflat.shape[0]/2.-imsz[0]/2.:hrflat.shape[0]/2.+imsz[0]/2.,hrflat.shape[1]/2.-imsz[1]/2.:hrflat.shape[1]/2+imsz[1]/2.]*dbt.compute_exptime(band,[asptime[i],asptime[i]+1],verbose=verbose,retries=retries)*gxt.compute_flat_scale(asptime[i]+0.5,band,verbose=0) return img
def hashresponse(band,events,verbose=0): """Given detector xi, eta, return the response at each position.""" # Hash out the response correction if verbose: mc.print_inline("Applying the response correction.") flat, _ = cal.flat(band) events['col'], events['row'] = xieta2colrow( events['xi'], events['eta'], band) events['flat'] = flat[np.array(events['col'], dtype='int16'), np.array(events['row'], dtype='int16')] events['scale'] = gxt.compute_flat_scale(events['t'], band) # TODO: Separately do the binlinearly interpolated response events['response'] = (events['flat']*events['scale']) return events
def xieta2colrow(xi, eta, band, detsize=1.25): """Convert detector xi, eta into col, row.""" flat, flatinfo = cal.flat(band) # should be able to get npix from the header... npixx = flat.shape[0] npixy = flat.shape[1] pixsz = flatinfo['CDELT2'] flatfill = detsize/(npixx*pixsz) col = ((( xi/36000.)/(detsize/2.)*flatfill + 1.)/2. * npixx) row = (((eta/36000.)/(detsize/2.)*flatfill + 1.)/2. * npixy) # You could theoretically drop a cut on detector position / detsize here... # Also, is this cut absolutely necessary? I think it's already been taken # care of by the flag==0 assertion in the SQL query. #cut = ((col > 0.) & (col < flat.shape[0]-1) & # (row > 0.) & (row < flat.shape[1]-1)) #cut = np.where(ix == True) return col, row
def create_rr(csvfile,band,eclipse,aspfile=0.,expstart=0.,expend=0.,retries=20, detsize=1.25,pltscl=68.754932): """DEPRECATED: Creates a relative response map for an eclipse, given a photon list. """ aspum = pltscl/1000.0 print "Loading flat file..." flat, flatinfo = cal.flat(band) npixx = flat.shape[0] npixy = flat.shape[1] pixsz = flatinfo['CDELT2'] flatfill = detsize/(npixx*pixsz) print "Retrieving aspect data..." if aspfile: aspra, aspdec, asptwist, asptime, aspheader, aspflags = load_aspect([aspfile]) else: aspra, aspdec, asptwist, asptime, aspheader, aspflags = web_query_aspect(eclipse,retries=retries) minasp = min(asptime) maxasp = max(asptime) print " trange= ( "+str(minasp)+" , "+str(maxasp)+" )" ra0, dec0, roll0 = aspheader['RA'], aspheader['DEC'], aspheader['ROLL'] print " [RA, DEC, ROLL] = ["+str(ra0)+", "+str(dec0)+", "+str(roll0)+"]" print "Computing aspect vectors..." print "Calculating aspect solution vectors..." xi_vec, eta_vec = np.array([]), np.array([]) #for i, ra in enumerate(aspra): #xi, eta = gnomfwd_simple(ras[i],decs[i],ra0,dec0,-twists[i], 1.0/36000.0, 0.) #xi, eta = gnomfwd_simple(ra0,dec0,ras[i],decs[i],-twists[i], 1.0/36000.0, 0.) xi_vec, eta_vec = gnomfwd_simple(ra0,dec0,aspra,aspdec,-asptwist, 1.0/36000.0, 0.) #xi_vec = np.append(xi_vec,xi) #eta_vec = np.append(eta_vec,eta) flat_scale = compute_flat_scale(asptime.mean(),band) #startt=time.time() if not expstart: expstart = asptime.min()+GPSSECS if not expend: expend = asptime.max()+GPSSECS flatbuff = np.zeros([960,960]) # Rotate the flat into the correct orientation to start flatbuff[80:960-80,80:960-80] = np.flipud(np.rot90(flat)) expt = 0 rr = np.zeros([960,960]) col = ((( xi_vec/36000.)/(detsize/2.)*flatfill + 1.)/2. * npixx)-400. row = (((eta_vec/36000.)/(detsize/2.)*flatfill + 1.)/2. * npixy)-400. #print len(asptime),len(xi_vec),len(eta_vec),len(col),len(row) for i in xrange(len(asptime)-1): #print i if (asptime[i]+GPSSECS)<expstart or (asptime[i]+GPSSECS)>expend: print " ",asptime[i]+GPSSECS," out of range." continue elif (aspflags[i]%2!=0) or (aspflags[i+1]%2!=0): continue print " ",asptime[i]+GPSSECS," flagged." else: #print "ping" rr += scipy.ndimage.interpolation.shift(scipy.ndimage.interpolation.rotate(flatbuff,-asptwist[i],reshape=False,order=0,prefilter=False),[col[i],row[i]],order=0,prefilter=False) #print time.time()-startt #startt=time.time() expt+=1 # Need to modify this to handle NUV files better. t,x,y,flags=load_txy(csvfile) exp = compute_exposure(t,x,y,flags,band,eclipse) deadt = compute_deadtime(t,x,y,band,eclipse) return rr*flat_scale*(1-deadt),exp