def batchSim(self): flist = os.listdir(self.srcDir) flist.sort() imgs = [] for tfilename in flist: if tfilename.find("fit")>-1: imgs.append(tfilename) print("total %d images"%(len(imgs))) for i, timg in enumerate(imgs): if i > 59 and i< 600: tIdx = i - 50 templateImg = imgs[tIdx] print("\n\nprocess %d %s, template is %d %s"%(i, timg, tIdx, templateImg)) self.simImage(timg, templateImg) thumbnail = getThumbnail(self.srcDir, timg, stampSize=(100,100), grid=(5, 5), innerSpace = 1) thumbnail = scipy.ndimage.zoom(thumbnail, 4, order=0) outpre= timg.split(".")[0] thumbnailPath = "%s/%s_thb.jpg"%(self.preViewDir, outpre) Image.fromarray(thumbnail).save(thumbnailPath)
def diffImage(self, imgName, tmplParms): starttime = datetime.now() resultFlag = True oImgPre = imgName.split(".")[0] os.system("rm -rf %s/*" % (self.diff)) status = tmplParms[0] #tmplImgName = tmplParms[1][-1][0] tmplImgName = tmplParms[1][0][0] tmplImgPre = tmplImgName.split(".")[0] if status == '1': ttmplPath = self.tmplAlignDir else: ttmplPath = self.tmplDiffDir ofitPath = "%s/%s.fit" % (self.doDiffTmpl, tmplImgPre) if not os.path.exists(ofitPath): os.system("rm -rf %s/*" % (self.doDiffTmpl)) os.system("cp %s/%s.fit %s/%s.fit" % (ttmplPath, tmplImgPre, self.doDiffTmpl, tmplImgPre)) os.system("cp %s/%s.fit %s/ti.fit" % (self.doDiffTmpl, tmplImgPre, self.diff)) os.system("cp %s/%s.fit %s/oi.fit" % (self.alignDir, oImgPre, self.diff)) objTmpResi, objTmpConv, runSuccess = self.tools.runHotpants( 'oi.fit', 'ti.fit', self.diff) if not runSuccess: self.log.error("diffImage failure: %s" % (imgName)) return False os.system("cp %s/%s %s/%s_resi.fit" % (self.diff, objTmpResi, self.diffImgDir, oImgPre)) os.system("cp %s/%s %s/%s_conv.fit" % (self.diff, objTmpConv, self.diffImgDir, oImgPre)) ''' ''' tgrid = 1 tsize = 800 tzoom = 1 timg = getThumbnail(self.diff, objTmpResi, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) #timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_resi.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) endtime = datetime.now() runTime = (endtime - starttime).seconds self.log.info("********** image diff total use %d seconds" % (runTime)) return resultFlag
def simImage(self, oImg): outpre = oImg.split(".")[0] os.system("rm -rf %s/*" % (self.tmpDir)) os.system("cp %s/%s %s/%s" % (self.srcDir, oImg, self.tmpDir, self.objectImg)) self.removeHeader(self.objectImg) self.objectImgCat = self.runSextractor(self.objectImg) mchFile, nmhFile = self.runSelfMatch(self.objectImgCat, self.r16) self.osn16 = nmhFile osn16s = selectTempOTs(self.osn16, self.tmpDir) tdata = np.loadtxt("%s/%s" % (self.tmpDir, osn16s)) if len(tdata.shape) < 2 or tdata.shape[0] < 100: print("%s has too little stars, break this run" % (oImg)) return osn16sf = filtOTs(osn16s, self.tmpDir) tdata = np.loadtxt("%s/%s" % (self.tmpDir, osn16sf)) if len(tdata.shape) < 2 or tdata.shape[0] < 45: print("%s has too little stars, break this run" % (oImg)) return imgSimClass = ImageSimulation() otImgs = imgSimClass.getTmpOtImgs(osn16sf, self.objectImg) psfView = genPSFView(otImgs) thumbnail = getThumbnail(self.srcDir, oImg, stampSize=(100, 100), grid=(5, 5), innerSpace=1) thumbnail = scipy.ndimage.zoom(thumbnail, 4, order=0) ''' plt.clf() plt.figure(figsize=(20,20)) plt.imshow(psfView, interpolation = "nearest", cmap='gray') plt.show() plt.clf() plt.figure(figsize=(20,20)) plt.imshow(thumbnail, interpolation = "nearest", cmap='gray') plt.show() ''' tpath11 = "%s_view" % (self.srcDir) if not os.path.exists(tpath11): os.system("mkdir %s" % (tpath11)) dpath1 = "%s/%s_psf.jpg" % (tpath11, outpre) dpath2 = "%s/%s_thb.jpg" % (tpath11, outpre) Image.fromarray(psfView).save(dpath1) Image.fromarray(thumbnail).save(dpath2)
def diffImage(self): starttime = datetime.datetime.now() resultFlag = True oImgPre = self.origObjectImg[:self.origObjectImg.index(".")] os.system("cp %s/%s %s/%s" % (self.templateDir, self.templateImg, self.tmpDir, self.templateImg)) os.system( "cp %s/%s %s/%s" % (self.templateDir, self.badPixCat, self.tmpDir, self.badPixCat)) os.system("cp %s/%s %s/%s" % (self.templateDir, self.templateImgCat, self.tmpDir, self.templateImgCat)) #self.newImageName = self.tools.imageAlign(self.tmpDir, self.objectImg, self.transHG) self.objTmpResi = self.tools.runHotpants(self.newImageName, self.templateImg, self.tmpDir) fpar = 'sex_diff.par' sexConf = [ '-DETECT_MINAREA', '3', '-DETECT_THRESH', '2.5', '-ANALYSIS_THRESH', '2.5' ] resiCat = self.tools.runSextractor(self.objTmpResi, self.tmpDir, self.tmpDir, fpar, sexConf) ''' self.tools.runSelfMatch(self.tmpDir, resiCat, 1) #debug: get ds9 reg file tdata = np.loadtxt("%s/%s"%(self.tmpDir, resiCat)) self.log.info("resi image star %d"%(tdata.shape[0])) ''' ''' mchRadius = 10 mchFile, nmhFile, mchPair = self.tools.runCrossMatch(self.tmpDir, resiCat, self.templateImgCat, mchRadius) tdata = np.loadtxt("%s/%s"%(self.tmpDir, mchFile)) print("resi star not match template %d"%(tdata.shape[0])) ''' mchFile, nmhFile, mchPair = self.tools.runCrossMatch( self.tmpDir, resiCat, self.badPixCat, 1) #1 and 5 os.system("cp %s/%s %s/%s" % (self.tmpDir, nmhFile, self.resiCatDir, "%s.cat" % (oImgPre))) tdata = np.loadtxt("%s/%s" % (self.tmpDir, nmhFile)) self.log.info("resi star not match template and remove badpix %d" % (tdata.shape[0])) if tdata.shape[0] < 3000: size = self.subImgSize fSubImgs, fparms = self.tools.getWindowImgs( self.tmpDir, self.newImageName, self.templateImg, self.objTmpResi, tdata, size) tXY = fparms[:, 0:2] tRaDec = self.wcs.all_pix2world(tXY, 1) fparms = np.concatenate((fparms, tRaDec), axis=1) fotpath = '%s/%s_otimg.npz' % (self.destDir, oImgPre) np.savez_compressed(fotpath, fot=fSubImgs, parms=fparms) resiImgs = [] for timg in fSubImgs: resiImgs.append(timg[2]) preViewPath = "%s/%s_psf.jpg" % (self.preViewDir, oImgPre) #if not os.path.exists(preViewPath): psfView = genPSFView(resiImgs) Image.fromarray(psfView).save(preViewPath) resultFlag = True else: tmsgStr = "%s.fit resi image has %d objects, maybe wrong" % ( oImgPre, tdata.shape[0]) self.log.error(tmsgStr) self.tools.sendTriggerMsg(tmsgStr) resultFlag = False tgrid = 4 tsize = 500 tzoom = 1 timg = getThumbnail(self.tmpDir, self.objTmpResi, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_resi.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) timg = getThumbnail(self.tmpDir, self.newImageName, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_obj.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) timg = getThumbnail(self.tmpDir, self.templateImg, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_tmp.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) endtime = datetime.datetime.now() runTime = (endtime - starttime).seconds self.log.info("********** image diff total use %d seconds" % (runTime)) return resultFlag ''' '''
def simImage(self, oImg, tImg): starttime = datetime.datetime.now() self.objectImgOrig = oImg self.templateImgOrig = tImg os.system("rm -rf %s/*" % (self.tmpDir)) oImgfz = "%s/%s.fz" % (self.srcDir, oImg) tImgfz = "%s/%s.fz" % (self.srcDir, tImg) if (not os.path.exists(oImgfz)) or (not os.path.exists(tImgfz)): print("%s or %s not exist" % (oImgfz, tImgfz)) return os.system("cp %s/%s.fz %s/%s.fz" % (self.srcDir, oImg, self.tmpDir, self.objectImg)) os.system("cp %s/%s.fz %s/%s.fz" % (self.srcDir, tImg, self.tmpDir, self.templateImg)) os.system("%s %s/%s.fz" % (self.funpackProgram, self.tmpDir, self.objectImg)) os.system("%s %s/%s.fz" % (self.funpackProgram, self.tmpDir, self.templateImg)) self.removeHeaderAndOverScan(self.objectImg) self.removeHeaderAndOverScan(self.templateImg) sexConf = [ '-DETECT_MINAREA', '7', '-DETECT_THRESH', '5', '-ANALYSIS_THRESH', '5' ] fpar = 'sex_diff_fot.par' self.objectImgCat = self.runSextractor(self.objectImg, fpar, sexConf) self.templateImgCat = self.runSextractor(self.templateImg, fpar, sexConf) tdata = np.loadtxt("%s/%s" % (self.tmpDir, self.objectImgCat)) print("objImg extract star %d" % (tdata.shape[0])) if len(tdata.shape) < 2 or tdata.shape[0] < 5000: print("%s has too little stars, break this run" % (oImg)) return tdata = np.loadtxt("%s/%s" % (self.tmpDir, self.templateImgCat)) print("tempImg extract star %d" % (tdata.shape[0])) if len(tdata.shape) < 2 or tdata.shape[0] < 5000: print("%s has too little stars, break this run" % (tImg)) return badPixCat = self.processBadPix() ''' ''' mchFile, nmhFile = self.runSelfMatch(self.objectImgCat, self.r16) self.osn16 = nmhFile mchFile, nmhFile = self.runSelfMatch(self.templateImgCat, self.r16) self.tsn16 = nmhFile mchFile, nmhFile, mchPair = self.runCrossMatch(self.osn16, self.tsn16, 10) osn16_tsn16_cm5 = mchFile osn16_tsn16_cm5_pair = mchPair self.gridStatistic(osn16_tsn16_cm5, gridNum=4) newimage, h = self.getMatchPos(self.osn16, self.tsn16, osn16_tsn16_cm5_pair) newName = "new.fit" newPath = "%s/%s" % (self.tmpDir, newName) if os.path.exists(newPath): os.remove(newPath) hdu = fits.PrimaryHDU(newimage) hdul = fits.HDUList([hdu]) hdul.writeto(newPath) ''' tdata = np.loadtxt("%s/%s"%(self.tmpDir, self.objectImgCat)) tdata = np.array([tdata]) tdata2 = cv2.perspectiveTransform(tdata, h) tdata2 = tdata2[0] oiTransName = "%s_trans.cat"%(self.objectImgCat[:self.objectImgCat.index(".")]) oiTransPath = "%s/%s"%(self.tmpDir, oiTransName) with open(oiTransPath, 'w') as fp1: for tobj in tdata2: fp1.write("%.3f %.3f\n"%(tobj[0], tobj[1])) ''' self.objTmpResi = self.runHotpants(newName, self.templateImg) tgrid = 4 tsize = 1000 tzoom = 1 oImgPre = oImg[:oImg.index(".")] timg = getThumbnail(self.tmpDir, self.objTmpResi, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_resi.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) timg = getThumbnail(self.tmpDir, self.objectImg, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_obj.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) timg = getThumbnail(self.tmpDir, self.templateImg, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_tmp.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) fpar = 'sex_diff.par' sexConf = [ '-DETECT_MINAREA', '3', '-DETECT_THRESH', '2.5', '-ANALYSIS_THRESH', '2.5' ] resiCat = self.runSextractor(self.objTmpResi, fpar, sexConf) mchFile, nmhFile = self.runSelfMatch(resiCat, 1) tdata = np.loadtxt("%s/%s" % (self.tmpDir, resiCat)) print("resi image star %d" % (tdata.shape[0])) mchRadius = 10 mchFile, nmhFile, mchPair = self.runCrossMatch(resiCat, self.templateImgCat, mchRadius) tdata = np.loadtxt("%s/%s" % (self.tmpDir, mchFile)) print("resi star match template %d" % (tdata.shape[0])) mchFile, nmhFile, mchPair = self.runCrossMatch(mchFile, badPixCat, 5) tdata = np.loadtxt("%s/%s" % (self.tmpDir, nmhFile)) print("resi star match template and remove badpix %d" % (tdata.shape[0])) size = self.subImgSize fSubImgs, fparms = self.getWindowImgs(newName, self.templateImg, self.objTmpResi, tdata, size) oImgPre = oImg[:oImg.index(".")] fotpath = '%s/%s_otimg_fot.npz' % (self.destDir, oImgPre) np.savez_compressed(fotpath, fot=fSubImgs, parms=fparms) self.log.info("\n******************") self.log.info("simulation False OT, total sub image %d" % (len(fSubImgs))) resiImgs = [] for timg in fSubImgs: resiImgs.append(timg[2]) preViewPath = "%s/%s_psf.jpg" % (self.preViewDir, oImgPre) #if not os.path.exists(preViewPath): psfView = genPSFView(resiImgs) Image.fromarray(psfView).save(preViewPath) endtime = datetime.datetime.now() runTime = (endtime - starttime).seconds self.log.debug("image diff total use %d seconds" % (runTime))
def imgSimulate(self, srcFitDir, destFitDir, destCatAddDir): try: tfiles0 = os.listdir(srcFitDir) tfiles0.sort() tfiles = [] for tfile in tfiles0: if tfile.find('mon_objt_190116') > 0: tfiles.append(tfile[:33]) tmpImgPath = "%s/%s" % (destFitDir, tfiles[0]) if not os.path.exists(tmpImgPath): os.system("cp %s/%s %s" % (srcFitDir, tfiles[0], tmpImgPath)) os.system( "cp %s/%s %s/%s" % (srcFitDir, tfiles[0], self.templateDir, self.templateImg)) sexConf = [ '-DETECT_MINAREA', '5', '-DETECT_THRESH', '3', '-ANALYSIS_THRESH', '3' ] fpar = 'sex_diff.par' imgSimClass = ImageSimulation(self.tmpDir) for i, imgName in enumerate(tfiles[1:]): #if i<460: # continue starttime = datetime.now() self.log.info("\n\n**********\nsimulate %d: %s" % (i, imgName)) os.system("rm -rf %s/*" % (self.tmpDir)) imgpre = imgName.split(".")[0] tobjFitsFullPath = "%s/%s.fit" % (srcFitDir, imgpre) if not os.path.exists(tobjFitsFullPath): self.log.error("%s.fit not exist, stop" % (imgpre)) break if os.path.exists("%s/%s.fit" % (destFitDir, imgpre)): self.log.info("%s.fit already simulated, skip" % (imgpre)) continue os.system("cp %s/%s.fit %s/%s" % (srcFitDir, imgpre, self.tmpDir, self.objectImg)) #sexConf=['-DETECT_MINAREA','10','-DETECT_THRESH','5','-ANALYSIS_THRESH','5'] sexConf = [ '-DETECT_MINAREA', '5', '-DETECT_THRESH', '3', '-ANALYSIS_THRESH', '3' ] self.objectImgCat = self.tools.runSextractor(self.objectImg, self.tmpDir, self.tmpDir, fpar, sexConf=sexConf) mchFile16, nmhFile16 = self.tools.runSelfMatch( self.tmpDir, self.objectImgCat, 16) self.osn16 = nmhFile16 objCali = self.magCali(self.osn16) osn16s = selectTempOTs(objCali, self.tmpDir) tdata = np.loadtxt("%s/%s" % (self.tmpDir, osn16s)) if len(tdata.shape) < 2 or tdata.shape[0] < 100: self.log.error("%s has too little stars, break this run" % (self.objectImg)) continue osn16sf = filtOTs(osn16s, self.tmpDir) tdata = np.loadtxt("%s/%s" % (self.tmpDir, osn16sf)) if len(tdata.shape) < 2 or tdata.shape[0] < 45: self.log.error("%s has too little stars, break this run" % (self.objectImg)) continue mchFile, nmhFile = self.tools.runSelfMatch( self.tmpDir, self.objectImgCat, 24) self.osn32 = nmhFile osn32f = filtOTs(self.osn32, self.tmpDir) posmagFile = "simAdd190324.cat" if i == 0 or (not os.path.exists( "%s/%s" % (self.templateDir, posmagFile))): tposmagFile = "" else: tposmagFile = posmagFile os.system("cp %s/%s %s/%s" % (self.templateDir, posmagFile, self.tmpDir, posmagFile)) simFile, simPosFile, tmpOtImgs = imgSimClass.simulateImage1( osn32f, self.objectImg, osn16sf, self.objectImg, posmagFile=tposmagFile) self.objectImgSim = simFile self.objectImgSimCatAdd = simPosFile #destFitDir, destCatAddDir os.system("cp %s/%s %s/%s.fit" % (self.tmpDir, self.objectImgSim, destFitDir, imgpre)) os.system("cp %s/%s %s/%s.cat" % (self.tmpDir, self.objectImgSimCatAdd, destCatAddDir, imgpre)) if i == 0 or (not os.path.exists( "%s/%s" % (self.templateDir, posmagFile))): os.system("cp %s/%s %s/%s" % (self.tmpDir, self.objectImgSimCatAdd, self.templateDir, posmagFile)) if i % 50 == 1 or i >= len(tfiles) - 3: os.system("cp %s/%s %s/%s" % (self.templateDir, self.templateImg, self.tmpDir, self.templateImg)) self.simTmpResi, runSuccess = self.tools.runHotpants( self.objectImgSim, self.templateImg, self.tmpDir) if not runSuccess: self.log.info("%s.fit sim diff error..." % (imgpre)) continue sexConf = [ '-DETECT_MINAREA', '3', '-DETECT_THRESH', '2.5', '-ANALYSIS_THRESH', '2.5' ] #self.objectImgSimCat = self.tools.runSextractor(self.objectImgSim, self.tmpDir, self.tmpDir, sexConf=sexConf) self.simTmpResiCat = self.tools.runSextractor( self.simTmpResi, self.tmpDir, self.tmpDir, fpar, sexConf=sexConf) #simTmpResiCatEf = filtByEllipticity(self.simTmpResiCat, self.tmpDir, maxEllip=0.5) #mchFile, nmhFile = self.tools.runSelfMatch(self.tmpDir, simTmpResiCatEf, 16) #simTmpResiCatEf_sn32 = nmhFile #simTmpResiCatEf_sn32 = simTmpResiCatEf #mchFile, nmhFile, mchPair = self.tools.runCrossMatch(self.tmpDir, self.objectImgSimCatAdd, simTmpResiCatEf, 5) mchFile, nmhFile, mchPair = self.tools.runCrossMatch( self.tmpDir, self.objectImgSimCatAdd, self.simTmpResiCat, 5) tdata1 = np.loadtxt("%s/%s" % (self.tmpDir, self.objectImgSimCatAdd)) tdata2 = np.loadtxt("%s/%s" % (self.tmpDir, self.simTmpResiCat)) tdata3 = np.loadtxt("%s/%s" % (self.tmpDir, mchFile)) self.log.info( "sim add %d, diff find %d, sim match diff %d, find percentage %.2f%%" % (tdata1.shape[0], tdata2.shape[0], tdata3.shape[0], tdata3.shape[0] * 100.0 / tdata1.shape[0])) os.system( "cp %s/%s %s/%s_simResi.fit" % (self.tmpDir, self.simTmpResi, destFitDir, imgpre)) os.system("cp %s/%s %s/%s_simResi.cat" % (self.tmpDir, self.simTmpResiCat, destCatAddDir, imgpre)) tgrid = 4 tsize = 500 tzoom = 2 timg = getThumbnail(self.tmpDir, self.simTmpResi, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_resi.jpg" % (self.preViewSimResiDir, imgpre) Image.fromarray(timg).save(preViewPath) endtime = datetime.now() runTime = (endtime - starttime).seconds self.log.info("sim: %s use %d seconds" % (imgName, runTime)) #break except Exception as e: print(str(e)) tstr = traceback.format_exc() print(tstr)
def imgDiff(self, srcDir, destDir): self.alignFitsDir = srcDir self.resiFitDir = destDir try: tfiles0 = os.listdir(self.alignFitsDir) tfiles0.sort() tfiles = [] for tfile in tfiles0: if tfile.find('mon_objt_190116') > 0: tfiles.append(tfile[:33]) os.system("rm -rf %s/*" % (self.templateDir)) tmpImgName = tfiles[0] os.system("cp %s/%s %s/%s" % (self.alignFitsDir, tmpImgName, self.templateDir, self.templateImg)) for i, imgName in enumerate(tfiles[1:]): if i % 50 != 1 and i < len(tfiles) - 3: continue starttime = datetime.now() self.log.info("diff %d: %s" % (i, imgName)) os.system("rm -rf %s/*" % (self.tmpDir)) imgpre = imgName.split(".")[0] tobjFitsFullPath = "%s/%s.fit" % (self.alignFitsDir, imgpre) if not os.path.exists(tobjFitsFullPath): self.log.error("%s.fit not exist, stop" % (imgpre)) break if os.path.exists("%s/%s.fit" % (self.resiFitDir, imgpre)): self.log.info("%s.fit already diffed, skip" % (imgpre)) continue os.system( "cp %s/%s.fit %s/%s" % (self.alignFitsDir, imgpre, self.tmpDir, self.objectImg)) os.system("cp %s/%s %s/%s" % (self.templateDir, self.templateImg, self.tmpDir, self.templateImg)) self.objTmpResi, runSuccess = self.tools.runHotpants( self.objectImg, self.templateImg, self.tmpDir) if not runSuccess: self.log.info("%s.fit diff error..." % (imgpre)) continue os.system( "cp %s/%s %s/%s.fit" % (self.tmpDir, self.objTmpResi, self.resiFitDir, imgpre)) tgrid = 4 tsize = 500 tzoom = 2 timg = getThumbnail(self.tmpDir, self.objTmpResi, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_resi.jpg" % (self.preViewDir, imgpre) Image.fromarray(timg).save(preViewPath) endtime = datetime.now() runTime = (endtime - starttime).seconds self.log.info("diff: %s use %d seconds" % (imgName, runTime)) #break except Exception as e: print(str(e)) tstr = traceback.format_exc() print(tstr)
def diff(self, srcDir, destDir, tmpFit, objFits, reverse=False): try: if not os.path.exists(destDir): os.system("mkdir -p %s" % (destDir)) self.regImg2DB(srcDir, tmpFit) os.system("rm -rf %s/*" % (self.templateDir)) os.system("cp %s/%s %s/%s" % (srcDir, tmpFit, self.templateDir, self.templateImg)) self.makeTemplate() for i, imgName in enumerate(objFits): starttime = datetime.now() self.log.info("diff %d: %s" % (i, imgName)) self.regImg2DB(srcDir, imgName) os.system("rm -rf %s/*" % (self.tmpDir)) imgpre = imgName.split(".")[0] tobjFitsFullPath = "%s/%s.fit" % (srcDir, imgpre) if not os.path.exists(tobjFitsFullPath): self.log.error("%s.fit not exist, stop" % (imgpre)) break if os.path.exists("%s/diffResi/%s.fit" % (destDir, imgpre)): self.log.info("%s.fit already diffed, skip" % (imgpre)) continue os.system("cp %s/%s.fit %s/%s" % (srcDir, imgpre, self.tmpDir, self.objectImg)) os.system("cp %s/%s %s/%s" % (self.templateDir, self.templateImg, self.tmpDir, self.templateImg)) os.system("cp %s/%s %s/%s" % (self.templateDir, self.badPixCat, self.tmpDir, self.badPixCat)) self.objTmpResi, runSuccess = self.tools.runHotpants( self.objectImg, self.templateImg, self.tmpDir) if not runSuccess: self.log.info("%s.fit diff error..." % (imgpre)) continue if reverse: os.system("cp %s/%s %s/diffResi/%s_r.fit" % (self.tmpDir, self.objTmpResi, destDir, imgpre)) else: os.system("cp %s/%s %s/diffResi/%s.fit" % (self.tmpDir, self.objTmpResi, destDir, imgpre)) tgrid = 4 tsize = 500 tzoom = 2 timg = getThumbnail(self.tmpDir, self.objTmpResi, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) if reverse: preViewPath = "%s/preview/%s_resi_r.jpg" % (destDir, imgpre) else: preViewPath = "%s/preview/%s_resi.jpg" % (destDir, imgpre) Image.fromarray(timg).save(preViewPath) fpar = 'sex_diff.par' sexConf = [ '-DETECT_MINAREA', '3', '-DETECT_THRESH', '2.5', '-ANALYSIS_THRESH', '2.5' ] self.objectImgCat = self.tools.runSextractor( self.objectImg, self.tmpDir, self.tmpDir, fpar, sexConf) resiCat = self.tools.runSextractor(self.objTmpResi, self.tmpDir, self.tmpDir, fpar, sexConf) mchFile, nmhFile, mchPair = self.tools.runCrossMatch( self.tmpDir, resiCat, self.objectImgCat, 1) #1 and 5 badPixProps2 = np.loadtxt("%s/%s" % (self.tmpDir, nmhFile)) mchFile, nmhFile, mchPair = self.tools.runCrossMatch( self.tmpDir, mchFile, self.badPixCat, 1) #1 and 5 badPixProps = np.loadtxt("%s/%s" % (self.tmpDir, self.badPixCat)) resiCatTrans = self.getRaDec(nmhFile) objProps = np.loadtxt("%s/%s" % (self.tmpDir, resiCatTrans)) tstr = "%s, resi objs %d, orgBadPix %d, nmBad %d" % ( imgName, objProps.shape[0], badPixProps.shape[0], badPixProps2.shape[0]) self.log.info(tstr) #size = self.subImgSize size = 68 if objProps.shape[0] < 20000 and objProps.shape[0] > 0: totSubImgs, totParms = getWindowImgs( self.tmpDir, self.objectImg, self.templateImg, self.objTmpResi, objProps, size) if totParms.shape[0] > 0: if reverse: fotpath = '%s/subImgs/%s_r.npz' % (destDir, imgpre) else: fotpath = '%s/subImgs/%s.npz' % (destDir, imgpre) np.savez_compressed(fotpath, imgs=totSubImgs, parms=totParms) self.classifyAndUpload(destDir, imgName, reverse=reverse) endtime = datetime.now() runTime = (endtime - starttime).seconds self.log.info("diff: %s use %d seconds" % (imgName, runTime)) #break except Exception as e: print(str(e)) tstr = traceback.format_exc() print(tstr)
def diffImage(self): starttime = datetime.now() resultFlag = True oImgPre = self.origObjectImg[:self.origObjectImg.index(".")] os.system("cp %s/%s %s/%s" % (self.templateDir, self.templateImg, self.tmpDir, self.templateImg)) os.system( "cp %s/%s %s/%s" % (self.templateDir, self.badPixCat, self.tmpDir, self.badPixCat)) os.system("cp %s/%s %s/%s" % (self.templateDir, self.templateImgCat, self.tmpDir, self.templateImgCat)) #self.newImageName = self.tools.imageAlign(self.tmpDir, self.objectImg, self.transHG) self.objTmpResi, runSuccess = self.tools.runHotpants( self.newImageName, self.templateImg, self.tmpDir) if not runSuccess: return False os.system("cp %s/%s %s/%s.fit" % (self.tmpDir, self.objTmpResi, self.resiFitsDir, oImgPre)) tgrid = 1 tsize = 4096 tzoom = 1 timg = getThumbnail(self.tmpDir, self.objTmpResi, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) #timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_resi.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) ''' return ''' fpar = 'sex_diff.par' sexConf = [ '-DETECT_MINAREA', '3', '-DETECT_THRESH', '2.5', '-ANALYSIS_THRESH', '2.5' ] resiCat = self.tools.runSextractor(self.objTmpResi, self.tmpDir, self.tmpDir, fpar, sexConf) mchFile, nmhFile, mchPair = self.tools.runCrossMatch( self.tmpDir, resiCat, self.objectImgCatTrans, 1) #1 and 5 badPixProps2 = np.loadtxt("%s/%s" % (self.tmpDir, nmhFile)) tdata1 = np.loadtxt("%s/%s" % (self.tmpDir, mchFile)) tdata2 = np.loadtxt("%s/%s" % (self.tmpDir, self.objectImgCatTrans)) tIdx1 = np.loadtxt("%s/%s" % (self.tmpDir, mchPair)).astype(np.int) tIdx1 = tIdx1 - 1 origData = tdata2[tIdx1[:, 1]] if origData.shape[0] == tdata1.shape[0]: outCatName = "%s_orgpos.cat" % (mchFile[:mchFile.index(".")]) outCatPath = "%s/%s" % (self.tmpDir, outCatName) tstr = "" i = 0 for td in tdata1: tstr += "%.4f %.4f %.2f %.2f %.2f %.3f %.3f %.3f %.2f %.2f %d %.4f %.4f %.4f %.4f\n"%\ (td[0],td[1],td[2],td[3],td[4],td[5],td[6],td[7],td[8],td[9],td[10],origData[i][11], origData[i][12], origData[i][13], origData[i][14]) i = i + 1 fp0 = open(outCatPath, 'w') fp0.write(tstr) fp0.close() mchFile = outCatName else: self.log.error("add orig pos error") ''' self.tools.runSelfMatch(self.tmpDir, resiCat, 1) #debug: get ds9 reg file tdata = np.loadtxt("%s/%s"%(self.tmpDir, resiCat)) self.log.info("resi image star %d"%(tdata.shape[0])) ''' ''' ''' mchRadius = 15 #15 10 mchFile, nmhFile, mchPair = self.tools.runCrossMatch( self.tmpDir, mchFile, self.templateImgCat, mchRadius) fotProps = np.loadtxt("%s/%s" % (self.tmpDir, mchFile)) mchFile, nmhFile, mchPair = self.tools.runCrossMatch( self.tmpDir, nmhFile, self.badPixCat, 1) #1 and 5 os.system("cp %s/%s %s/%s" % (self.tmpDir, nmhFile, self.resiCatDir, "%s.cat" % (oImgPre))) totProps = np.loadtxt("%s/%s" % (self.tmpDir, nmhFile)) #badPixProps = np.loadtxt("%s/%s"%(self.tmpDir, self.badPixCat)) badPixProps = np.array([]) tstr = "orgBadPix %d, nmBad %d, match %d, noMatch %d" % ( badPixProps.shape[0], badPixProps2.shape[0], fotProps.shape[0], totProps.shape[0]) self.log.info(tstr) #size = self.subImgSize size = 68 if totProps.shape[0] < 500 and totProps.shape[0] > 0: totSubImgs, totParms = getWindowImgs(self.tmpDir, self.newImageName, self.templateImg, self.objTmpResi, totProps, size) if totParms.shape[0] > 0: tXY = totParms[:, 0:2] tRaDec = self.wcs.all_pix2world(tXY, 1) totParms = np.concatenate((totParms, tRaDec), axis=1) fotpath = '%s/%s_totimg.npz' % (self.destDir, oImgPre) np.savez_compressed(fotpath, imgs=totSubImgs, parms=totParms) resiImgs = [] for timg in totSubImgs: resiImgs.append(timg[2]) preViewPath = "%s/%s_tot.jpg" % (self.preViewDir, oImgPre) #if not os.path.exists(preViewPath): psfView = genPSFView(resiImgs) Image.fromarray(psfView).save(preViewPath) if fotProps.shape[0] > 0 and fotProps.shape[0] < 2000: fotSubImgs, fotParms = getWindowImgs(self.tmpDir, self.newImageName, self.templateImg, self.objTmpResi, fotProps, size) if fotParms.shape[0] > 0: tXY = fotParms[:, 0:2] tRaDec = self.wcs.all_pix2world(tXY, 1) fotParms = np.concatenate((fotParms, tRaDec), axis=1) fotpath = '%s/%s_fotimg.npz' % (self.destDir, oImgPre) np.savez_compressed(fotpath, imgs=fotSubImgs, parms=fotParms) ''' if badPixProps.shape[0]>0: badSubImgs, badParms = getWindowImgs(self.tmpDir, self.newImageName, self.templateImg, self.objTmpResi, badPixProps, size) if badParms.shape[0]>0: fotpath = '%s/%s_badimg.npz'%(self.destDir, oImgPre) np.savez_compressed(fotpath, imgs=badSubImgs, parms=badParms) if badPixProps2.shape[0]>0: badSubImgs, badParms = getWindowImgs(self.tmpDir, self.newImageName, self.templateImg, self.objTmpResi, badPixProps2, size) if badParms.shape[0]>0: fotpath = '%s/%s_badimg2.npz'%(self.destDir, oImgPre) np.savez_compressed(fotpath, imgs=badSubImgs, parms=badParms) ''' resultFlag = True else: tmsgStr = "%s.fit resi image has %d tot objects, maybe wrong" % ( oImgPre, totProps.shape[0]) self.log.error(tmsgStr) #self.sendMsg(tmsgStr) resultFlag = False ''' tgrid = 4 tsize = 500 tzoom = 1 timg = getThumbnail(self.tmpDir, self.objTmpResi, stampSize=(tsize,tsize), grid=(tgrid, tgrid), innerSpace = 1) #timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_resi.jpg"%(self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) ''' ''' timg = getThumbnail(self.tmpDir, self.newImageName, stampSize=(tsize,tsize), grid=(tgrid, tgrid), innerSpace = 1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_obj.jpg"%(self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) timg = getThumbnail(self.tmpDir, self.templateImg, stampSize=(tsize,tsize), grid=(tgrid, tgrid), innerSpace = 1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_tmp.jpg"%(self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) ''' endtime = datetime.now() runTime = (endtime - starttime).seconds self.log.info("********** image diff total use %d seconds" % (runTime)) return resultFlag
def diffImage(self, imgName, tmplParms): starttime = datetime.now() resultFlag = True oImgPre = imgName.split(".")[0] os.system("rm -rf %s/*" % (self.diff)) status = tmplParms[0] #tmplImgName = tmplParms[1][-1][0] tmplImgName = tmplParms[1][0][0] tmplImgPre = tmplImgName.split(".")[0] if status == '1': ttmplPath = self.tmplAlignDir else: ttmplPath = self.tmplDiffDir ofitPath = "%s/%s.fit" % (self.doDiffTmpl, tmplImgPre) if not os.path.exists(ofitPath): os.system("rm -rf %s/*" % (self.doDiffTmpl)) os.system("cp %s/%s.fit %s/%s.fit" % (ttmplPath, tmplImgPre, self.doDiffTmpl, tmplImgPre)) os.system("cp %s/%s.cat %s/%s.cat" % (ttmplPath, tmplImgPre, self.doDiffTmpl, tmplImgPre)) os.system("cp %s/%s_badpix.cat %s/%s_badpix.cat" % (ttmplPath, tmplImgPre, self.doDiffTmpl, tmplImgPre)) os.system("cp %s/%s.fit %s/ti.fit" % (self.doDiffTmpl, tmplImgPre, self.diff)) os.system("cp %s/%s.cat %s/ti.cat" % (self.doDiffTmpl, tmplImgPre, self.diff)) os.system("cp %s/%s_badpix.cat %s/ti_badpix.cat" % (self.doDiffTmpl, tmplImgPre, self.diff)) os.system("cp %s/%s.fit %s/oi.fit" % (self.cmbDir, oImgPre, self.diff)) os.system("cp %s/%s.cat %s/oi.cat" % (self.cmbCatDir, oImgPre, self.diff)) theader = fits.getheader("%s/oi.fit" % (self.diff)) dateObs = theader['DATE-OBS'] timeObs = theader['TIME-OBS'] dtStr = "%sT%s" % (dateObs, timeObs) objTmpResi, runSuccess = self.tools.runHotpants( 'oi.fit', 'ti.fit', self.diff) if not runSuccess: self.log.error("diffImage failure: %s" % (imgName)) return False fpar = 'sex_diff.par' #sexConf=['-DETECT_MINAREA','3','-DETECT_THRESH','2.5','-ANALYSIS_THRESH','2.5'] sexConf = [ '-DETECT_MINAREA', '5', '-DETECT_THRESH', '2.5', '-ANALYSIS_THRESH', '2.5' ] resiCat, isSuccess = self.tools.runSextractor(objTmpResi, self.diff, self.diff, fpar, sexConf) if not isSuccess: self.log.error("diffImage runSextractor failure") return isSuccess os.system("cp %s/%s %s/%s_resi.fit" % (self.diff, objTmpResi, self.diffImgDir, oImgPre)) os.system("cp %s/%s %s/%s_resi.cat" % (self.diff, resiCat, self.diffCatDir, oImgPre)) mchFile, nmhFile, mchPair = self.tools.runCrossMatch( self.diff, resiCat, 'oi.cat', 1) #1 and 5 badPix2Path = "%s/%s" % (self.diff, nmhFile) if os.path.exists(badPix2Path): badPixProps2 = np.loadtxt(badPix2Path) else: badPixProps2 = np.array([]) mchRadius = 15 #15 10 mchFile, nmhFile, mchPair = self.tools.runCrossMatch( self.diff, mchFile, 'ti.cat', mchRadius) fotPath = "%s/%s" % (self.diff, mchFile) if os.path.exists(fotPath): fotProps = np.loadtxt(fotPath) else: fotProps = np.array([]) mchFile, nmhFile, mchPair = self.tools.runCrossMatch( self.diff, nmhFile, 'ti_badpix.cat', 1) #1 and 5 os.system("cp %s/%s %s/%s_tot.cat" % (self.diff, nmhFile, self.diffCatDir, oImgPre)) totPath = "%s/%s" % (self.diff, nmhFile) if os.path.exists(totPath): totProps = np.loadtxt(totPath) else: totProps = np.array([]) badPixPath = "%s/ti_badpix.cat" % (self.diff) if os.path.exists(badPixPath): badPixProps = np.loadtxt(badPixPath) else: badPixProps = np.array([]) #badPixProps = np.array([]) tstr = "orgBadPix %d, nmBad %d, match %d, noMatch %d" % ( badPixProps.shape[0], badPixProps2.shape[0], fotProps.shape[0], totProps.shape[0]) self.log.info(tstr) print(tstr) #size = self.subImgSize size = 68 if totProps.shape[0] < 500 and totProps.shape[0] > 0: wcsPath = "%s/%s.wcs" % (ttmplPath, tmplImgPre) wcs = WCS(wcsPath) totSubImgs, totParms = getWindowImgs(self.diff, 'oi.fit', 'ti.fit', objTmpResi, totProps, size) if totParms.shape[0] > 0: tXY = totParms[:, 0:2] #tdates = np.repeat(dtStr,tXY.shape[0]).reshape((tXY.shape[0],1)) tRaDec = wcs.all_pix2world(tXY, 1) totParms = np.concatenate((totParms, tRaDec), axis=1) fotpath = '%s/%s_totimg.npz' % (self.destDir, oImgPre) np.savez_compressed(fotpath, imgs=totSubImgs, parms=totParms, obsUtc=dtStr) resiImgs = [] for timg in totSubImgs: resiImgs.append(timg[2]) preViewPath = "%s/%s_tot.jpg" % (self.subImgViewDir, oImgPre) #if not os.path.exists(preViewPath): psfView = genPSFView(resiImgs) Image.fromarray(psfView).save(preViewPath) if fotProps.shape[0] > 0 and fotProps.shape[0] < 3000: fotSubImgs, fotParms = getWindowImgs(self.diff, 'oi.fit', 'ti.fit', objTmpResi, fotProps, size) if fotParms.shape[0] > 0: tXY = fotParms[:, 0:2] #tdates = np.repeat(dtStr,tXY.shape[0]).reshape((tXY.shape[0],1)) tRaDec = wcs.all_pix2world(tXY, 1) fotParms = np.concatenate((fotParms, tRaDec), axis=1) fotpath = '%s/%s_fotimg.npz' % (self.destDir, oImgPre) np.savez_compressed(fotpath, imgs=fotSubImgs, parms=fotParms, obsUtc=dtStr) ''' if badPixProps.shape[0]>0: badSubImgs, badParms = getWindowImgs(self.diff, 'oi.fit', 'ti.fit', objTmpResi, badPixProps, size) if badParms.shape[0]>0: fotpath = '%s/%s_badimg.npz'%(self.destDir, oImgPre) np.savez_compressed(fotpath, imgs=badSubImgs, parms=badParms) if badPixProps2.shape[0]>0: badSubImgs, badParms = getWindowImgs(self.diff, 'oi.fit', 'ti.fit', objTmpResi, badPixProps2, size) if badParms.shape[0]>0: tXY = badParms[:,0:2] tRaDec = wcs.all_pix2world(tXY, 1) badParms = np.concatenate((badParms, tRaDec), axis=1) fotpath = '%s/%s_badimg2.npz'%(self.destDir, oImgPre) np.savez_compressed(fotpath, imgs=badSubImgs, parms=badParms, obsUtc=dtStr) ''' resultFlag = True else: tmsgStr = "%s.fit resi image has %d tot objects, maybe wrong" % ( oImgPre, totProps.shape[0]) self.log.error(tmsgStr) #self.sendMsg(tmsgStr) resultFlag = False ''' ''' tgrid = 4 tsize = 500 tzoom = 1 timg = getThumbnail(self.diff, objTmpResi, stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_resi.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) ''' ''' timg = getThumbnail(self.diff, 'oi.fit', stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_obj.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) timg = getThumbnail(self.diff, 'ti.fit', stampSize=(tsize, tsize), grid=(tgrid, tgrid), innerSpace=1) timg = scipy.ndimage.zoom(timg, tzoom, order=0) preViewPath = "%s/%s_tmp.jpg" % (self.origPreViewDir, oImgPre) Image.fromarray(timg).save(preViewPath) endtime = datetime.now() runTime = (endtime - starttime).seconds self.log.info("********** image diff total use %d seconds" % (runTime)) return resultFlag
def simImage(self, oImg, tImg): starttime = datetime.datetime.now() self.objectImgOrig = oImg self.templateImgOrig = tImg os.system("rm -rf %s/*" % (self.tmpDir)) os.system("cp %s/%s %s/%s" % (self.srcDir, oImg, self.tmpDir, self.objectImg)) os.system("cp %s/%s %s/%s" % (self.srcDir, tImg, self.tmpDir, self.templateImg)) self.removeHeaderAndOverScan(self.objectImg) self.removeHeaderAndOverScan(self.templateImg) sexConf = [ '-DETECT_MINAREA', '7', '-DETECT_THRESH', '5', '-ANALYSIS_THRESH', '5' ] fpar = 'sex_diff_fot.par' self.objectImgCat = self.runSextractor(self.objectImg, fpar, sexConf) self.templateImgCat = self.runSextractor(self.templateImg, fpar, sexConf) tdata = np.loadtxt("%s/%s" % (self.tmpDir, self.objectImgCat)) print("objImg extract star %d" % (tdata.shape[0])) if len(tdata.shape) < 2 or tdata.shape[0] < 5000: print("%s has too little stars, break this run" % (oImg)) return tdata = np.loadtxt("%s/%s" % (self.tmpDir, self.templateImgCat)) print("tempImg extract star %d" % (tdata.shape[0])) if len(tdata.shape) < 2 or tdata.shape[0] < 5000: print("%s has too little stars, break this run" % (tImg)) return badPixCat = self.processBadPix() ''' ''' mchFile, nmhFile = self.runSelfMatch(self.objectImgCat, self.r16) self.osn16 = nmhFile mchFile, nmhFile = self.runSelfMatch(self.templateImgCat, self.r16) self.tsn16 = nmhFile mchFile, nmhFile, mchPair = self.runCrossMatch(self.osn16, self.tsn16, 10) osn16_tsn16_cm5 = mchFile osn16_tsn16_cm5_pair = mchPair self.gridStatistic(osn16_tsn16_cm5, gridNum=4) newimage = self.getMatchPos3(self.osn16, self.tsn16, osn16_tsn16_cm5_pair) newName = "new.fit" newPath = "%s/%s" % (self.tmpDir, newName) if os.path.exists(newPath): os.remove(newPath) hdu = fits.PrimaryHDU(newimage) hdul = fits.HDUList([hdu]) hdul.writeto(newPath) resImg = self.runHotpants(newName, self.templateImg) timg = getThumbnail(self.tmpDir, resImg, stampSize=(100, 100), grid=(5, 5), innerSpace=1) timg = scipy.ndimage.zoom(timg, 4, order=0) plt.figure(figsize=(12, 12)) plt.imshow(timg, cmap='gray') plt.show() fpar = 'sex_diff.par' sexConf = [ '-DETECT_MINAREA', '3', '-DETECT_THRESH', '1.5', '-ANALYSIS_THRESH', '1.5' ] resiCat = self.runSextractor(resImg, fpar, sexConf) mchFile, nmhFile, mchPair = self.runCrossMatch(resiCat, badPixCat, 5) tdata = np.loadtxt("%s/%s" % (self.tmpDir, nmhFile)) print("resi image star %d" % (tdata.shape[0])) endtime = datetime.datetime.now() runTime = (endtime - starttime).seconds self.log.debug("image diff total use %d seconds" % (runTime))