def PNormalizeImage(SumWtImage, SumWt2, outImage, err, minWt=0.1): """ Sum an image onto Weighting accumulators Normalize SumWtImage by SumWt2 write to outImage Minimum allowed value in SumWt2 is minWt * SumWtImage = First output image, must be defined (i.e. files named) but not fully created. * SumWt2 = Second output image, like SumWtImage * outImage = Output image, must be defined. * err = Python Obit Error/message stack * minWt = minimum summed weight (lower values blanked). """ ################################################################ # Checks if not Image.PIsA(outImage): print "Actually ",outImage.__class__ raise TypeError,"outImage MUST be a Python Obit Image" if not Image.PIsA(SumWtImage): print "Actually ",SumWtImage.__class__ raise TypeError,"SumWtImage MUST be a Python Obit Image" if not Image.PIsA(SumWt2): print "Actually ",SumWt2.__class__ raise TypeError,"SumWt2 MUST be a Python Obit Image" if not OErr.OErrIsA(err): raise TypeError,"err MUST be an OErr" # # Open files Image.POpen(outImage, 2, err) Image.POpen(SumWtImage, 1, err) Image.POpen(SumWt2, 1, err) # Get descriptor to see how many planes outDesc = Image.PGetDesc(SumWtImage) outDescDict = ImageDesc.PGetDict(outDesc) outNaxis = outDescDict["inaxes"] print "Accumulation naxis",outNaxis # Get input descriptor to see how many planes inDesc = Image.PGetDesc(outImage) inDescDict = ImageDesc.PGetDict(outDesc) ndim = inDescDict["naxis"] inNaxis = inDescDict["inaxes"] #print "debug input naxis is ",inNaxis # Test if compatible if inNaxis[2] < outNaxis[2]: print "input has",inNaxis[2],"planes and output",outNaxis[2] raise RuntimeError,"input image has too few planes " if (ndim>0) and (inNaxis[2]>0): # list of planes to loop over (0-rel) planes = range(inNaxis[2]) else: planes = [0] # # Loop over planes for iPlane in planes: doPlane = [iPlane+1,1,1,1,1] # Get images Image.PGetPlane (SumWtImage, None, doPlane, err) Image.PGetPlane (SumWt2, None, doPlane, err) OErr.printErrMsg(err, "Error reading images") # Clip FArray.PClipBlank (SumWt2.FArray, minWt, 1.0e25) # Divide FArray.PDiv (SumWtImage.FArray, SumWt2.FArray, outImage.FArray) # Write Image.PPutPlane(outImage, None, doPlane, err) OErr.printErrMsg(err, "Error Writing normalized image ") # end loop over planes # close output Image.PClose(outImage, err) Image.PClose(SumWtImage, err) Image.PClose(SumWt2, err) # Write history - sorta inHistory = History.History("history", SumWtImage.List, err) outHistory = History.History("history", outImage.List, err) # Copy History History.PCopy(inHistory, outHistory, err) outHistory.Open(History.READWRITE, err) outHistory.TimeStamp(" Start Obit PNormalize",err) outHistory.Close(err)
for plane in range(Idim[2]): print "Plane",plane+1 planeDim = [plane+1,1,1,1,1] # Only 3D IImage.GetPlane(None, planeDim, err) # Read Ipol plane (IData) VImage.GetPlane(None, planeDim, err) # Read Vpol plane (VData) # Get IPol Image plane statistics for clipping pos=[0,0] Imax = FArray.PMax(IData, pos) Irms = IData.RMS Imin = max(Imax*MaxFactor,Irms*RMSFactor) print "Max",Imax,"RMS",Irms,"clip",Imin # Blank noise and below DR limit WtData = FArray.PCopy(IData, err) # Work array copy of IPol DivData = FArray.PClone(IData, err) # Clone of IPol array OErr.printErrMsg(err, "Error copying FArray") FArray.PClipBlank(WtData, Imin, 1.0e30) # Determine mean fractional VPol FArray.PDiv(VData, WtData, DivData) # Print Mean ratio print "Mean ratio",DivData.Mean # Multiple fraction times weight FArray.PMul(DivData, WtData, DivData) # Accumulate sumX = sumX + DivData.Sum sumWt = sumWt + WtData.Sum del WtData, DivData # Some cleanup # Get average instrumental VPol calibration VCor = sumX/sumWt print "Average fractional VPol",VCor