def PGetPoln(VLtab, imQ, imU, err, RMSsize=100): """ Get polarization info for VL table VLtab input VL table imQ Q pol Image imU U pol Image, same geometry as Q err Obit Err/message object RMSsize halfwidth in pixels of RMS box """ dQ = imQ.Desc dU = imU.Desc if not ImageDesc.PCheckCompat(dQ, dU): OErr.printErrMsg(err, "Error: Q,U images are incompatible") nx = dQ.Dict['inaxes'][0] ny = dQ.Dict['inaxes'][1] size = min(RMSsize, nx / 2, ny / 2) # Actual RMS box size fblank = FArray.fblank # Get Q, U images pixQ = Image.PReadPlane(imQ, err) pixU = Image.PReadPlane(imU, err) if err.isErr: OErr.printErrMsg(err, "Error Reading Q,U images") # Make interpolators fiQ = FInterpolate.FInterpolate('Q', pixQ, dQ, 2) fiU = FInterpolate.FInterpolate('U', pixU, dU, 2) # Open table VLtab.Open(Table.READWRITE, err) if err.isErr: OErr.printErrMsg(err, "Error Opening table") nrow = VLtab.Desc.Dict['nrow'] for irow in range(1, nrow + 1): row = VLtab.ReadRow(irow, err) pos = [row['RA(2000)'][0], row['DEC(2000)'][0]] # pixel pixel = ImageDesc.PGetPixel(dQ, pos, err) # In image? if (pixel[0] < 2.0) or (pixel[1] < 2.0) or (pixel[0] > nx - 2) or ( pixel[1] > ny - 2): continue # Fluxes qval = FInterpolate.PPosition(fiQ, pos, err) uval = FInterpolate.PPosition(fiU, pos, err) # Valid? if (qval == fblank) or (uval == fblank): continue row['Q CENTER'][0] = qval row['U CENTER'][0] = uval row['P FLUX'][0] = (qval * qval + uval * uval)**0.5 if err.isErr: OErr.printErrMsg(err, "Error Interpolating") # RMS box blc = [int(pixel[0]) - RMSsize, int(pixel[1]) - RMSsize] trc = [int(pixel[0]) + RMSsize, int(pixel[1]) + RMSsize] if blc[0] < 0: blc[0] = 0 trc[0] = min(2 * size, nx - 1) if blc[1] < 0: blc[1] = 0 trc[1] = min(2 * size, ny - 1) if trc[0] > nx - 1: d = trc[0] - nx blc[0] = max(1, blc[0] - d) trc[0] = nx - 1 if trc[1] > ny - 1: d = trc[1] - ny blc[1] = max(1, blc[1] - d) trc[1] = ny - 1 # RMSes box = FArray.PSubArr(pixQ, blc, trc, err) rmsQ = box.RMS del box box = FArray.PSubArr(pixU, blc, trc, err) rmsU = box.RMS del box row['POL RMS'][0] = 0.5 * (rmsQ + rmsU) VLtab.WriteRow(irow, row, err) if err.isErr: OErr.printErrMsg(err, "Error Writing table") # end loop over table VLtab.Close(err) del pixQ, pixU, fiQ, fiU
def PImageAdd (in1Image, in2Image, outImage, err, \ chkPos=False, factor1=1.0, factor2=1.0): """ Adds Pixels in in2Image from in1Image and write to outImage Adds scaled pixel values, writes history * in1Image = input Obit Python Image 1 * in2Image = input Obit Python Image 2 * outImage = output Obit Python Image, must be defined but not instantiated * err = Python Obit Error/message stack * chkPos = If true also check the coordinates on each axis Check is if pixels are within 0.01 of a pixel * factor1 = Scaling factor for in1Image * factor2 = Scaling factor for in2Image """ ################################################################ # Checks if not Image.PIsA(in1Image): raise TypeError("in1Image MUST be a Python Obit Image") if not Image.PIsA(in2Image): raise TypeError("in2Image MUST be a Python Obit Image") if not Image.PIsA(outImage): raise TypeError("outImage MUST be a Python Obit Image") if not err.IsA(): raise TypeError("err MUST be an OErr") # # Clone output from input 1 in1Image.Clone(outImage, err) # Open images Image.POpen(in1Image, Image.READONLY, err) Image.POpen(in2Image, Image.READONLY, err) Image.POpen(outImage, Image.WRITEONLY, err) # Get input descriptor to see how many planes in1Desc = in1Image.Desc in2Desc = in2Image.Desc # Check compatibility ImageDesc.PCheckCompat(in1Desc, in2Desc, chkPos=chkPos) inDescDict = in1Desc.Dict ndim = inDescDict["naxis"] inNaxis = inDescDict["inaxes"] # Work buffer inImageArray = Image.PGetFArray(in1Image) ImageBuffer1 = FArray.PCopy(inImageArray, err) ImageBuffer2 = FArray.PCopy(inImageArray, err) # list of planes to loop over (0-rel) if (ndim > 0) and (inNaxis[2] > 0): planes = list(range(inNaxis[2])) else: planes = [0] # Loop over planes for iPlane in planes: doPlane = [iPlane + 1, 1, 1, 1, 1] # Get image planes Image.PGetPlane(in1Image, ImageBuffer1, doPlane, err) Image.PGetPlane(in2Image, ImageBuffer2, doPlane, err) # Scale FArray.PSMul(ImageBuffer1, factor1) FArray.PSMul(ImageBuffer2, factor2) # Add FArray.PAdd(ImageBuffer1, ImageBuffer2, ImageBuffer2) # Write output Image.PPutPlane(outImage, ImageBuffer2, doPlane, err) # end loop over planes # Close in2Image.Close(err) in2Image.Close(err) outImage.Close(err) # Error? if err.isErr: OErr.printErrMsg(err, "Error subtracting Images") # Write history in1History = History.History("history", in1Image.List, err) in2History = History.History("history", in2Image.List, err) outHistory = History.History("history", outImage.List, err) # Copy Histories outHistory.Open(History.READWRITE, err) outHistory.TimeStamp(" Start Obit PImageAdd", err) outHistory.WriteRec(-1, "/ PImageAdd Input 1 History", err) outHistory.Close(err) info = in1Image.List.Dict # FITS? - copy header if ("FileType" in info) and (info["FileType"][2][0] == 0): History.PCopyHeader(in1History, outHistory, err) #Not needed History.PCopy(in1History, outHistory, err) outHistory.Open(History.READWRITE, err) outHistory.WriteRec(-1, "/ ", err) outHistory.WriteRec(-1, "/ ****** PImageAdd Input 2 History", err) outHistory.Close(err) info = in2Image.List.Dict # FITS? - copy header if ("FileType" in info) and (info["FileType"][2][0] == 0): History.PCopyHeader(in2History, outHistory, err) History.PCopy(in2History, outHistory, err) # Add this programs history outHistory.Open(History.READWRITE, err) outHistory.TimeStamp(" Start Obit PImageAdd", err) outHistory.WriteRec(-1, OSystem.PGetPgmName() + " factor1 = " + str(factor1), err) outHistory.WriteRec(-1, OSystem.PGetPgmName() + " factor2 = " + str(factor2), err) outHistory.Close(err)