def partialDiffcal(self, calmode, calMat_PreRot=[]): """ Normally triple-differential calibration is performed, via makeDiffIms. This function lets you use a subset of polarisation states. 'calmode' chooses which combination of differential measurements to perform. It uses the same numbering system as in diffcal_vampires. calType = '0' ; 0 = Triple Calibration ; 1a = Double cal, Woll. + LCVR, HWP0 ; 1b = Double cal, Woll. + LCVR, HWP45 ; 2a = Double cal, Woll + HWP, LCVR1 ; 2b = Double cal, Woll + HWP, LCVR2 ; 3a = Double cal, LCVR + HWP, Woll.1 ; 3b = Double cal, LCVR + HWP, Woll.2 ; 4a = Single cal, Wollaston (LCVR1, HWP0) ; 4b = Single cal, Wollaston (LCVR1, HWP45) ; 4c = Single cal, Wollaston (LCVR2, HWP0) ; 4d = Single cal, Wollaston (LCVR2, HWP45) ; 5a = Single cal, LCVR (Woll1, HWP0) ; 5b = Single cal, LCVR (Woll1, HWP45) ; 6a = Single cal, HWP (LCVR1, Woll1) """ allowedCalmodes = [ '1a', '2a', '2b', '3a', '3b', '4a', '5a', '5b', '6a' ] if calmode not in allowedCalmodes: print('Please specify one of the following calmodes:') print(allowedCalmodes) imSz = self.allSummedImsOrig.shape[0] allPartialIms = [] count = 0 for cur in self.allPolzstateIms: if calmode is '1a': plusQ1 = cur.h0c0l0 - cur.h0c1l0 minusQ1 = cur.h0c0l1 - cur.h0c1l1 Q = (plusQ1 - minusQ1) / 2 plusU1 = cur.h1c0l0 - cur.h1c1l0 minusU1 = cur.h1c0l1 - cur.h1c1l1 U = (plusU1 - minusU1) / 2 Ia = (cur.h0c0l0 + cur.h0c1l0 + cur.h0c0l1 + cur.h0c1l1) / 4 Ib = (cur.h1c0l0 + cur.h1c1l0 + cur.h1c0l1 + cur.h1c1l1) / 4 I = (Ia + Ib) / 2 if calmode is '2a': plusQ1 = cur.h0c0l0 - cur.h0c1l0 minusQ1 = cur.h2c0l0 - cur.h2c1l0 Q = (plusQ1 - minusQ1) / 2 plusU1 = cur.h1c0l0 - cur.h1c1l0 minusU1 = cur.h3c0l0 - cur.h3c1l0 U = (plusU1 - minusU1) / 2 Ia = (cur.h0c0l0 + cur.h0c1l0 + cur.h2c0l0 + cur.h2c1l0) / 4 Ib = (cur.h1c0l0 + cur.h1c1l0 + cur.h3c0l0 + cur.h3c1l0) / 4 I = (Ia + Ib) / 2 if calmode is '2b': plusQ1 = cur.h0c0l1 - cur.h0c1l1 minusQ1 = cur.h2c0l1 - cur.h2c1l1 Q = (plusQ1 - minusQ1) / 2 plusU1 = cur.h1c0l1 - cur.h1c1l1 minusU1 = cur.h3c0l1 - cur.h3c1l1 U = (plusU1 - minusU1) / 2 Ia = (cur.h0c0l1 + cur.h0c1l1 + cur.h2c0l1 + cur.h2c1l1) / 4 Ib = (cur.h1c0l1 + cur.h1c1l1 + cur.h3c0l1 + cur.h3c1l1) / 4 I = (Ia + Ib) / 2 if calmode is '3a': plusQ1 = cur.h0c0l0 - cur.h0c0l1 minusQ1 = cur.h2c0l0 - cur.h2c0l1 Q = (plusQ1 - minusQ1) / 2 plusU1 = cur.h1c0l0 - cur.h1c0l1 minusU1 = cur.h3c0l0 - cur.h3c0l1 U = (plusU1 - minusU1) / 2 Ia = (cur.h0c0l0 + cur.h0c0l1 + cur.h2c0l0 + cur.h2c0l1) / 4 Ib = (cur.h1c0l0 + cur.h1c0l1 + cur.h3c0l0 + cur.h3c0l1) / 4 I = (Ia + Ib) / 2 if calmode is '3b': plusQ1 = cur.h0c1l0 - cur.h0c1l1 minusQ1 = cur.h2c1l0 - cur.h2c1l1 Q = (plusQ1 - minusQ1) / 2 plusU1 = cur.h1c1l0 - cur.h1c1l1 minusU1 = cur.h3c1l0 - cur.h3c1l1 U = (plusU1 - minusU1) / 2 Ia = (cur.h0c1l0 + cur.h0c1l1 + cur.h2c1l0 + cur.h2c1l1) / 4 Ib = (cur.h1c1l0 + cur.h1c1l1 + cur.h3c1l0 + cur.h3c1l1) / 4 I = (Ia + Ib) / 2 if calmode is '4a': Q = cur.h0c0l0 - cur.h0c1l0 U = cur.h1c0l0 - cur.h1c1l0 I = (cur.h0c0l0 + cur.h0c1l0 + cur.h1c0l0 + cur.h1c1l0) / 4 if calmode is '5a': Q = cur.h0c0l0 - cur.h0c0l1 U = cur.h1c0l0 - cur.h1c0l1 I = (cur.h0c0l0 + cur.h0c0l1 + cur.h1c0l0 + cur.h1c0l1) / 4 if calmode is '5b': Q = cur.h2c0l0 - cur.h2c0l1 U = cur.h3c0l0 - cur.h3c0l1 I = (cur.h2c0l0 + cur.h2c0l1 + cur.h3c0l0 + cur.h3c0l1) / 4 if calmode is '6a': Q = cur.h0c0l0 - cur.h2c0l0 U = cur.h1c0l0 - cur.h3c0l0 I = (cur.h0c0l0 + cur.h2c0l0 + cur.h1c0l0 + cur.h3c0l0) / 4 curIm = np.zeros((imSz, imSz, 4)) curIm[:, :, 0] = I curIm[:, :, 1] = Q curIm[:, :, 2] = U if len(calMat_PreRot) > 0: curIm = plz.matIm(curIm, calMat_PreRot) curIm = plz.rotImPolz(curIm, self.pasPerInd[count]) allPartialIms.append(curIm) count += 1 allPartialIms = np.asarray(allPartialIms) self.PartialImsSummed = np.mean(allPartialIms, axis=0) print('Done.')
def makeDiffIms(self, doMask=False, maskRad=2., maskVal=1., clim=[], deRotate=True, imRange=[], showPlots=True, maskfile=[], calMat_PreRot=[], calMat_PostRot=[], twoCamMode=False, doFlats=False, Ibias=0): if Ibias > 0: print('WARNING: Stokes I bias applied') imSz = self.allSummedImsOrig.shape[0] nSets = self.allSummedImsOrig.shape[2] if len(imRange) > 0: imInd = range(imRange[0], imRange[1]) else: imInd = range(nSets) paInds = range(0, len(self.allPAsOrig), 4) allPAs = np.asarray(self.allPAsOrig[paInds]) npa = len(allPAs) allPAs = allPAs.reshape((int(npa / 4), 4)) self.allPAsReshaped = allPAs nUsedSets = len(imInd) if len(maskfile) > 0: maskfileobj = np.load(maskfile) mask1 = maskfileobj['mask1'] mask2 = maskfileobj['mask2'] else: mask1 = np.ones((imSz, imSz)) mask2 = np.ones((imSz, imSz)) # Assemble rotated images allSummedIms = np.zeros([imSz, imSz, len(imInd), 4, 2, 2]) pasPerInd = [] curPaSet = [] count = 0 fileCount = 0 #TO keep PAs correct - correspsonds to original file index for ind in imInd: print("Rotating set: %d" % ind) for h in range(4): pa = allPAs[ind, h] curPaSet.append(pa) for c in range(2): for l in range(2): curImIn = self.allSummedImsOrig[:, :, ind, h, c, l] # curImIn = allSummedImsOrig[:, :, ind, h, c, l] - h0c0l0med if c == 0: curImIn = curImIn * mask1 if c == 1: curImIn = curImIn * mask2 if doMask: maskPosn = imSz / 2 yGrid, xGrid = np.ogrid[-maskPosn:imSz - maskPosn, -maskPosn:imSz - maskPosn] maskInds = xGrid**2 + yGrid**2 <= maskRad**2 curImIn[maskInds] = maskVal if doFlats: # TODO - hacked in filenames etc right now - fix this. flatNpzFile = np.load('flats750Aug2015.npz') flatC1 = flatNpzFile['flatC1'] flatC2 = flatNpzFile['flatC2'] if c == 0: curImIn = curImIn / flatC1 if c == 1: curImIn = curImIn / flatC2 if twoCamMode: if c == 1: pass curImIn = np.fliplr(curImIn) else: if c == 0: # Rotate channel0 images curImIn = ndimage.interpolation.rotate( np.fliplr(curImIn), bsRot, mode='nearest', reshape=False) if deRotate: allSummedIms[:, :, count, h, c, l] = \ ndimage.interpolation.rotate(curImIn, -pa, mode='nearest', reshape=False) else: allSummedIms[:, :, count, h, c, l] = curImIn # Approximate set of 4 HWP files as having single PA (the average over files) curPa = np.mean(np.asarray(curPaSet)) curPaSet = [] pasPerInd.append(curPa) count += 1 pasPerInd = np.asarray(pasPerInd) self.pasPerInd = pasPerInd if saveAllSummedIms: fname = 'allSummedImsSaved.npz' np.savez(fname, allSummedIms=allSummedIms, pasPerInd=pasPerInd) # Make a set of Stokes images and derotate their polarisation self.allStokesIms = [] self.allPolzstateIms = [] self.allPolzratioQ = [] self.allPolzratioU = [] for count in range(nUsedSets): cur = self.sortPolzState(allSummedIms, count) self.allPolzstateIms.append(cur) plusQ1 = cur.h0c0l0 - cur.h0c1l0 minusQ1 = cur.h0c0l1 - cur.h0c1l1 Q1 = (plusQ1 - minusQ1) / 2 I0 = (cur.h0c0l0 + cur.h0c1l0 + cur.h0c0l1 + cur.h0c1l1) / 4 plusQ2 = cur.h2c0l0 - cur.h2c1l0 minusQ2 = cur.h2c0l1 - cur.h2c1l1 Q2 = (plusQ2 - minusQ2) / 2 I2 = (cur.h2c0l0 + cur.h2c1l0 + cur.h2c0l1 + cur.h2c1l1) / 4 Q = Q1 - Q2 ############Q = Q1 + Q2 I_02 = (I0 + I2) / 2 #pQ = Q / I_02 plusU1 = cur.h1c0l0 - cur.h1c1l0 minusU1 = cur.h1c0l1 - cur.h1c1l1 U1 = (plusU1 - minusU1) / 2 I1 = (cur.h1c0l0 + cur.h1c1l0 + cur.h1c0l1 + cur.h1c1l1) / 4 plusU2 = cur.h3c0l0 - cur.h3c1l0 minusU2 = cur.h3c0l1 - cur.h3c1l1 U2 = (plusU2 - minusU2) / 2 I3 = (cur.h3c0l0 + cur.h3c1l0 + cur.h3c0l1 + cur.h3c1l1) / 4 U = U1 - U2 ##############U = U1 + U2 I_13 = (I1 + I3) / 2 #pU = U / I_13 I = (I_02 + I_13) / 2 curIm = np.zeros((imSz, imSz, 4)) curIm[:, :, 0] = I + Ibias # Ibias is a kludge to increase visibility of some features in fractional polz map curIm[:, :, 1] = Q curIm[:, :, 2] = U if len(calMat_PreRot) > 0: curIm = plz.matIm(curIm, calMat_PreRot) curIm = plz.rotImPolz(curIm, pasPerInd[count]) if len(calMat_PostRot) > 0: curIm = plz.matIm(curIm, calMat_PostRot) self.allStokesIms.append(curIm) # Now calculate fractional polarisations using the triple-ratio method RQ1 = np.sqrt( (cur.h0c0l0 / cur.h0c1l0) / (cur.h0c0l1 / cur.h0c1l1)) RQ2 = np.sqrt( (cur.h2c0l0 / cur.h2c1l0) / (cur.h2c0l1 / cur.h2c1l1)) RQ = np.sqrt(RQ1 / RQ2) pQ = (RQ - 1) / (RQ + 1) RU1 = np.sqrt( (cur.h1c0l0 / cur.h1c1l0) / (cur.h1c0l1 / cur.h1c1l1)) RU2 = np.sqrt( (cur.h3c0l0 / cur.h3c1l0) / (cur.h3c0l1 / cur.h3c1l1)) RU = np.sqrt(RU1 / RU2) pU = (RU - 1) / (RU + 1) # cur_p_frac = np.sqrt(pQ**2 + pU**2) # Make dummy Stokes vector so can reuse the optimised rotImPolz curIm = np.zeros((imSz, imSz, 4)) curIm[:, :, 1] = pQ curIm[:, :, 2] = pU curIm = plz.rotImPolz(curIm, pasPerInd[count]) self.allPolzratioQ.append(curIm[:, :, 1]) self.allPolzratioU.append(curIm[:, :, 2]) if showPlots: self.plotPolzstates(index=count, fignum=1) self.plotStokesIms(index=count, fignum=2) allStokesImsArr = np.asarray(self.allStokesIms) self.StokesImsSummed = np.mean(allStokesImsArr, axis=0) summedpQ = np.asarray(self.allPolzratioQ).mean(axis=0) summedpU = np.asarray(self.allPolzratioU).mean(axis=0) self.polzRatioImsSummed = np.sqrt(summedpQ**2 + summedpU**2)
def makeDiffIms(self, doMask=False, maskRad=2., maskVal=1., clim=[], deRotate=True, imRange=[], showPlots=True): imSz = self.allSummedImsOrig.shape[0] nSets = self.allSummedImsOrig.shape[2] if len(imRange) > 0: imInd = range(imRange[0], imRange[1]) else: imInd = range(nSets) paInds = range(0, len(self.allPAsOrig), 4) allPAs = np.asarray(self.allPAsOrig[paInds]) npa = len(allPAs) allPAs = allPAs.reshape((npa / 4, 4)) nUsedSets = len(imInd) # Assemble rotated images allSummedIms = np.zeros([imSz, imSz, len(imInd), 4, 2, 2]) pasPerInd = [] curPaSet = [] count = 0 fileCount = 0 #TO keep PAs correct - correspsonds to original file index for ind in imInd: print "Rotating set: %d" % ind for h in range(4): pa = allPAs[ind, h] curPaSet.append(pa) for c in range(2): for l in range(2): curImIn = self.allSummedImsOrig[:, :, ind, h, c, l] # curImIn = allSummedImsOrig[:, :, ind, h, c, l] - h0c0l0med if doMask: maskPosn = imSz / 2 yGrid, xGrid = np.ogrid[-maskPosn:imSz - maskPosn, -maskPosn:imSz - maskPosn] maskInds = xGrid**2 + yGrid**2 <= maskRad**2 curImIn[maskInds] = maskVal if c == 0: # Rotate channel0 images curImIn = ndimage.interpolation.rotate( np.fliplr(curImIn), bsRot, mode='nearest', reshape=False) if deRotate: allSummedIms[:, :, count, h, c, l] = \ ndimage.interpolation.rotate(curImIn, -pa, mode='nearest', reshape=False) else: allSummedIms[:, :, count, h, c, l] = curImIn # Approximate set of 4 HWP files as having single PA (the average over files) curPa = np.mean(np.asarray(curPaSet)) curPaSet = [] pasPerInd.append(curPa) count += 1 pasPerInd = np.asarray(pasPerInd) self.pasPerInd = pasPerInd # Make a set of Stokes images and derotate their polarisation self.allStokesIms = [] self.allPolzstateIms = [] for count in range(nUsedSets): cur = self.sortPolzState(allSummedIms, count) self.allPolzstateIms.append(cur) plusQ1 = cur.h0c0l0 - cur.h0c1l0 minusQ1 = cur.h0c0l1 - cur.h0c1l1 Q1 = (plusQ1 - minusQ1) / 2 I0 = (cur.h0c0l0 + cur.h0c1l0 + cur.h0c0l1 + cur.h0c1l1) / 4 plusQ2 = cur.h2c0l0 - cur.h2c1l0 minusQ2 = cur.h2c0l1 - cur.h2c1l1 Q2 = (plusQ2 - minusQ2) / 2 I2 = (cur.h2c0l0 + cur.h2c1l0 + cur.h2c0l1 + cur.h2c1l1) / 4 Q = Q1 - Q2 I_02 = (I0 + I2) / 2 #pQ = Q / I_02 plusU1 = cur.h1c0l0 - cur.h1c1l0 minusU1 = cur.h1c0l1 - cur.h1c1l1 U1 = (plusU1 - minusU1) / 2 I1 = (cur.h1c0l0 + cur.h1c1l0 + cur.h1c0l1 + cur.h1c1l1) / 4 plusU2 = cur.h3c0l0 - cur.h3c1l0 minusU2 = cur.h3c0l1 - cur.h3c1l1 U2 = (plusU2 - minusU2) / 2 I3 = (cur.h3c0l0 + cur.h3c1l0 + cur.h3c0l1 + cur.h3c1l1) / 4 U = U1 - U2 I_13 = (I1 + I3) / 2 #pU = U / I_13 I = (I_02 + I_13) / 2 curIm = np.zeros((imSz, imSz, 4)) curIm[:, :, 0] = I curIm[:, :, 1] = Q curIm[:, :, 2] = U curIm = plz.rotImPolz(curIm, pasPerInd[count]) self.allStokesIms.append(curIm) if showPlots: self.plotPolzstates(index=count, fignum=1) self.plotStokesIms(index=count, fignum=2) allStokesImsArr = np.asarray(self.allStokesIms) self.StokesImsSummed = np.mean(allStokesImsArr, axis=0)
#### RQ = RQ2 #### ignore HWP pQ = (RQ - 1) / (RQ + 1) RU1 = np.sqrt((cur.h1c0l0 / cur.h1c1l0) / (cur.h1c0l1 / cur.h1c1l1)) RU2 = np.sqrt((cur.h3c0l0 / cur.h3c1l0) / (cur.h3c0l1 / cur.h3c1l1)) RU = np.sqrt(RU1 / RU2) #### RU = RU2 #### ignore HWP pU = (RU - 1) / (RU + 1) #cur_p_frac = np.sqrt(pQ**2 + pU**2) # Make dummy Stokes vector so can reuse the optimised rotImPolz curIm = np.zeros((imSz, imSz, 4)) curIm[:, :, 1] = pQ curIm[:, :, 2] = pU curIm = plz.rotImPolz(curIm, pasPerInd[count]) except: print "Error caught!" else: allpQIms.append(curIm[:, :, 1]) allpUIms.append(curIm[:, :, 2]) print count #summedpQIms = np.asarray summedpQ = np.asarray(allpQIms).mean(axis=0) summedpU = np.asarray(allpUIms).mean(axis=0) summed_p = np.sqrt(summedpQ**2 + summedpU**2) #fits.writeto('testorder_normal.fits',summed_p) plt.imshow(summed_p) plt.pause(0.001)
infile = 'toyDisk.fits' hdulist = fits.open(infile) curHDU = hdulist[0] inIm = np.transpose(curHDU.data, (1, 2, 0)) inIm = inIm.astype('f8') # t = cl() # allIms = plz.StokesToPol(inIm) # print cl()-t # Try rotating polz of images... paRange = np.arange(-45, 45, 10) allRotatedImages = [] allRotatedPolIms = [] imsz = inIm.shape[0] for angle in paRange: print "Doing angle %f" % angle curIm = plz.rotImPolz(inIm, angle) allRotatedImages.append(curIm) curPolIm = plz.stokesToPol(curIm) allRotatedPolIms.append(curPolIm) #plt.imshow(curIm[:,:,1]) plt.imshow(curPolIm[0]) plt.pause(0.001) print 'Done.'