def object_plot(fitsfile, catalog): image = f2n.fromfits(fitsfile, verbose=False) image.setzscale('auto', 'auto') image.makepilimage('log', negative=False) print('\033[1;34mPlotting sources on {0}...\033[0m'.format(catalog)) extension = os.path.splitext(os.path.basename(catalog))[1] if extension == '.pysexcat': coordinates = np.genfromtxt(catalog, delimiter=None, comments='#')[:, [1, 2]] elif extension == '.cnd': coordinates = np.genfromtxt(catalog, delimiter=',', comments='#', skip_header=1)[:, [1, 2]] for i, coordinate in enumerate(coordinates): x, y = coordinate[0], coordinate[1] label = '{0}'.format(i + 1) image.drawcircle(x, y, r=10, colour=(0, 255, 0), label=label) image.writetitle(os.path.basename(fitsfile)) fitshead, fitsextension = os.path.splitext(fitsfile) image.tonet('{0}.png'.format(fitshead)) print('\033[1;34mAll sources plotted on: {0}.png\033[0m'.format(fitshead)) return True
def fits2png(self, image_path): """ Source plot module. @param image_data: data part of the FITS image @type image_data: numpy array @param ra: RA coordinate of object, skycoords. @type ra: string @param dec: DEC coordinate of object, skycoords. @type dec: string @param mark_color: Color of the plot marks @type mark_color: str @returns: boolean """ try: import f2n except ImportError: print('Python cannot import f2n. Make sure f2n is installed.') raise SystemExit if image_path: hdu = fits.open(image_path)[0] else: print("No image provided!") raise SystemExit image = f2n.fromfits(image_path, verbose=False) image.setzscale('auto', 'auto') image.makepilimage('log', negative=False) image.writetitle(os.path.basename(image_path)) fitshead, fitsextension = os.path.splitext(image_path) image.tonet('{0}.png'.format(fitshead)) return True
def make_png(filename): myimage = f2n.fromfits(filename) myimage.makepilimage("log", negative = False) try: filepng = filename.replace('.fits','.png') myimage.tonet(filepng) except Exception, err: print(str(err)) sys.exit(2)
def object_plot(self, image_path, ra, dec, mark_color="red"): """ Source plot module. @param image_data: data part of the FITS image @type image_data: numpy array @param ra: RA coordinate of object, skycoords. @type ra: string @param dec: DEC coordinate of object, skycoords. @type dec: string @param mark_color: Color of the plot marks @type mark_color: str @returns: boolean """ try: import f2n except ImportError: print('Python cannot import f2n. Make sure f2n is installed.') raise SystemExit if image_path: hdu = fits.open(image_path)[0] else: print("No image provided!") raise SystemExit wcs = WCS(hdu.header) # plot an ellipse for each object if ":" not in (ra or dec): co = coordinates.SkyCoord('{0} {1}'.format(ra, dec), unit=(u.deg, u.deg), frame='icrs') else: co = coordinates.SkyCoord('{0} {1}'.format(ra, dec), unit=(u.hourangle, u.deg), frame='icrs') print('Target Coordinates:', co.to_string(style='hmsdms', sep=':')) image = f2n.fromfits(image_path, verbose=False) image.setzscale('auto', 'auto') image.makepilimage('log', negative=False) ac = AstCalc() x, y = ac.sky2xy(image_path, ra, dec) label = '{0}'.format(co.to_string(style='hmsdms', sep=':')) image.drawcircle(x, y, r=10, colour=(0, 255, 0), label=label) image.writetitle(os.path.basename(image_path)) fitshead, fitsextension = os.path.splitext(image_path) image.tonet('{0}.png'.format(fitshead)) print('\033[1;34mSource plotted on: {0}.png\033[0m'.format(fitshead)) return True
def pngGet(self,ra,dec,size,name=""): if name=="": name=str(ra)+"_"+str(dec)+"_x"+str(size) url=self.ESOgetURL(ra,dec,size) urllib.urlretrieve(url,name+'.fit') myimage= f2n.fromfits(name+'.fit') myimage.setzscale("auto") # z2 = "ex" means extrema -> maximum in this case. myimage.makepilimage("log", negative = True) # We can choose to make a negative image. myimage.tonet(name+".png")
def fits2png(fitsfile, outdir, asteroid=None, SPEED_MIN=float(config.get('asteroids', 'SPEED_MIN'))): ''' Transforms FITS images into PNG files. @param fitsfile: FITS file. @type fitsfile: string @param outdir: Output directory for the png files. @type outdir: string @param asteroid: numpy array for the moving objects. @type asteroid: numpy.ndarray @param SPEED_MIN: Minimum speed of a moving object. @type SPEED_MIN: float ''' try: import f2n except ImportError: print('Python cannot import f2n. Make sure f2n is installed.') raise SystemExit image = f2n.fromfits(fitsfile, verbose=False) image.setzscale('auto', 'auto') image.makepilimage('log', negative=False) if asteroid.size: for i in range(len(asteroid)): x = asteroid.iloc[i]['x'] y = asteroid.iloc[i]['y'] speed = asteroid.iloc[i]['Speed(px/min)'] label = '{0}'.format(int(asteroid.iloc[i]['ObjectID'])) if speed >= SPEED_MIN: color = (0, 255, 0) else: color = (255, 0, 0) image.drawcircle(x, y, r=10, colour=color, label=label) image.writetitle(os.path.basename(fitsfile)) fits_head = os.path.splitext(os.path.basename(fitsfile))[0] hdu = fits.open(fitsfile) obs_date = hdu[0].header['date-obs'] image.writeinfo([obs_date], colour=(255, 100, 0)) image.tonet(os.path.join(outdir, fits_head + '.png'))
def convert(image): myimage = f2n.fromfits(image) cContours = cv2.drawContour(img, [cnt],0,(255,0,0),-1 ) for cnt in cContours: leftmost = tuple(cnt[cnt[:,:,0].argmin()][0]) rightmost = tuple(cnt[cnt[:,:,0].argmax()][0]) topmost = tuple(cnt[cnt[:,:,1].argmin()][0]) bottommost = tuple(cnt[cnt[:,:,1].argmax()][0]) print(leftmost, rightmost, topmost, bottommost) myimage.setzscale() myimage.makepilimage("log", negative = False) myimage.tonet((imageName+".png"))
def fitsPNGs(self): bining=int(cfg['rebin']) self.png_dest=map(lambda x:cfg["dir_image_base"]+"/"+self.getToday()+"/"+cfg["dir_image_png"]+"/"+os.path.basename(x),self.pngs) for i,fits in enumerate(self.solvefits): print "Generating PNG from:",fits myimage = f2n.fromfits(fits) myimage.setzscale("auto") myimage.rebin(bining,method="max") # z2 = "ex" means extrema -> maximum in this case. myimage.makepilimage("log", negative = False) # We can choose to make a negative image. #myimage.writeinfo([fits,"DATE:"+self.dates[i]+" EXP="+str(self.exposures[i]),"TELE:"+self.telescopes[i]], colour=(255,100,0)) dir_dest=os.path.dirname(self.png_dest[i]) if not os.path.exists( dir_dest): os.makedirs(dir_dest) myimage.tonet(self.png_dest[i])
def pngGet(self,ra,dec,size,name=""): try: if name=="": name=str(ra)+"_"+str(dec)+"_x"+str(size) url=self.ESOgetURL(ra,dec,size) print url urllib.urlretrieve(url,name+'.fit') myimage= f2n.fromfits(name+'.fit') #myimage.setzscale("auto") myimage.setzscale(z1="auto",z2="flat",samplesizelimit=10000,nsig=3) # z2 = "ex" means extrema -> maximum in this case. myimage.makepilimage("lin", negative = False) # We can choose to make a negative image. myimage.tonet(name+".png") except: print "FAIL to get DSS image"
def catalog_plot(self, fitsfile, catalog): try: import f2n except ImportError: print('Python cannot import f2n. Make sure f2n is installed.') raise SystemExit image = f2n.fromfits(fitsfile, verbose=False) image.setzscale('auto', 'auto') image.makepilimage('log', negative=False) print('\033[1;34mPlotting sources on {0}...\033[0m'.format(catalog)) extension = os.path.splitext(os.path.basename(catalog))[1] if extension == '.cat': coordinates = np.genfromtxt(catalog, delimiter=None, comments='#')[:, [1, 2]] elif extension == '.txt': coordinates = np.genfromtxt(catalog, delimiter=None, comments='#')[:, [0, 1]] elif extension == '.cnd': coordinates = np.genfromtxt(catalog, delimiter=',', comments='#', skip_header=1)[:, [1, 2]] for i, coordinate in enumerate(coordinates): x, y = coordinate[0], coordinate[1] label = '{0}'.format(i + 1) image.drawcircle(x, y, r=10, colour=(0, 255, 0), label=label) image.writetitle(os.path.basename(fitsfile)) fitshead, fitsextension = os.path.splitext(fitsfile) image.tonet('{0}.png'.format(fitshead)) print('\033[1;34mAll sources plotted on: {0}.png\033[0m'.format( fitshead)) return True
def aperturesPNGs(self): bining=int(cfg['rebin']) self.apertures_dest=map(lambda x:cfg["dir_image_base"]+"/"+self.getToday()+"/"+cfg["dir_image_apertures"]+"/"+os.path.basename(x),self.pngsApertures) for i,fits in enumerate(self.aperturesfiles): print "Generating PNG from:",fits try: myimage = f2n.fromfits(fits) except: return myimage.setzscale( z1=50,z2="auto") myimage.rebin(bining,method="max") #z2 = "ex" means extrema -> maximum in this case. myimage.makepilimage("log", negative = True) # We can choose to make a negative image. #myimage.writetitle(fits, colour=(200, 0, 0)) #myimage.writeinfo([fits,"DATE:"+self.dates[i]+" EXP="+str(self.exposures[i]),"TELE:"+self.telescopes[i]], colour=(255,100,0)) dir_dest=os.path.dirname(self.apertures_dest[i]) if not os.path.exists( dir_dest): os.makedirs(dir_dest) #myimage.pilimage=myimage.pilimage.convert("L") myimage.tonet(self.apertures_dest[i])
def pngGet(self, ra, dec, size, name=""): try: if name == "": name = str(ra) + "_" + str(dec) + "_x" + str(size) url = self.ESOgetURL(ra, dec, size) print url urllib.urlretrieve(url, name + '.fit') myimage = f2n.fromfits(name + '.fit') #myimage.setzscale("auto") myimage.setzscale(z1="auto", z2="flat", samplesizelimit=10000, nsig=3) # z2 = "ex" means extrema -> maximum in this case. myimage.makepilimage("lin", negative=False) # We can choose to make a negative image. myimage.tonet(name + ".png") return True except: print "FAIL to get DSS image" return False
def showstars(self, verbose=True): """ Uses f2n to write a png image with circled stars. """ try: import f2n except ImportError: print "Couldn't import f2n -- install it !" return if verbose: print "Writing png ..." myimage = f2n.fromfits(self.filepath, verbose=False) #myimage.rebin(int(myimage.xb/1000.0)) myimage.setzscale("auto", "auto") myimage.makepilimage("log", negative=False) #myimage.upsample() myimage.drawstarlist(self.starlist, r=8, autocolour="flux") myimage.writetitle(os.path.basename(self.filepath)) #myimage.writeinfo(["This is a demo", "of some possibilities", "of f2n.py"], colour=(255,100,0)) if not os.path.isdir("alipy_visu"): os.makedirs("alipy_visu") myimage.tonet(os.path.join("alipy_visu", self.name + "_stars.png"))
def showstars(self, verbose=True): """ Uses f2n to write a png image with circled stars. """ try: import f2n except ImportError: print "Couldn't import f2n -- install it !" return if verbose: print "Writing png ..." myimage = f2n.fromfits(self.filepath, verbose=False) #myimage.rebin(int(myimage.xb/1000.0)) myimage.setzscale("auto", "auto") myimage.makepilimage("log", negative = False) #myimage.upsample() myimage.drawstarlist(self.starlist, r=8, autocolour="flux") myimage.writetitle(os.path.basename(self.filepath)) #myimage.writeinfo(["This is a demo", "of some possibilities", "of f2n.py"], colour=(255,100,0)) if not os.path.isdir("alipy_visu"): os.makedirs("alipy_visu") myimage.tonet(os.path.join("alipy_visu", self.name + "_stars.png"))
def FitsCutToPng(inputfile, outputfile, x, y, a, s): """ convert fits file to png/jpg with assigned size -inputfile the input fits file name including path -outputfile the output png file name including path -x coordinate x -y coordinate y -a border of the square picture around (x, y) -s the string under the circled star """ if (x < 0 or x > 3056 or y < 0 or y > 3056 or a < 0 or a > 3056): print "x or y or a is out of border, please check!" return # if(x-a<0 or x+a>3056 or y-a<0 or y+a>3056): # print "x-a or x+a or y-a or y+a is out of border, please check!" if x - a < 0: left = 0 p = a - x else: left = x - a p = 0 if x + a > 3056: right = 3056 else: right = x + a if y - a < 0: top = 0 q = y else: top = y - a q = a if y + a > 3056: bottom = 3056 q = a - (3056 - y) else: bottom = y + a q = 0 # print "-----" # print p # print q # print len(sys.argv) # print "-----" tmpImage = f2n.fromfits(inputfile) tmpImage.crop(left, right, top, bottom) tmpImage.setzscale("auto", "flat", 3, 100000, 300) #tmpImage.setzscale("flat", "flat", 2, 100000, 300, 65000) tmpImage.makepilimage("lin", negative=False) if s != "": #tmpImage.drawcircle(x, y, 3, (0,255,0), s) #modified by xlp at 20150206 tmpImage.drawcircle(x, y, 5, (0, 255, 0), None) tmpImage.tonet("tmp.png") # tmpImage.pilimage.save("tmp", "FIT") fgImg = Image.open("tmp.png") bgImg = Image.new("RGB", (2 * a, 2 * a), (100, 100, 100)) bgImg.paste(fgImg, (p, q)) bgImg.save(outputfile, "JPEG") os.system("rm tmp.png")
plotskim.append(""" <area shape="rect" COORDS="%i,%i,%i,%i" href="" onClick="return false" onmouseover="plotskim.src='%s';" alt=""> """ % (xmin, ymin, xmax, ymax, pngimage)) plotskim.append("</map>") filldict["zoomplot"] = "".join(plotskim) filldict["overviewplot"] = '<img src="overview.png" class="imgmargin">' ########################################## # PREPARING THE FITS IMAGES ########################################## # Deconvolution images : f2ng = f2n.fromfits(os.path.join(decdir, "g" + image[deckeyfilenum] + ".fits"), verbose=False) f2ng.setzscale(-30.0, "ex") f2ng.makepilimage(scale="log", negative=False) f2ng.upsample(4) f2ng.drawstarlist(objcosmicslist) f2ng.writetitle("Object") f2ng.tonet(os.path.join(destdir, "g.png")) f2nresi = f2n.fromfits(os.path.join( decdir, "resi" + image[deckeyfilenum] + ".fits"), verbose=False) f2nresi.setzscale(-30, +30) f2nresi.makepilimage(scale="lin", negative=True) f2nresi.upsample(2) f2nresi.writetitle("Residues", colour=(255))
def FitsCutToPng(inputfile, outputfile, x, y, a, s): """ convert fits file to png/jpg with assigned size usage: inputfile outputfile x y border string -inputfile the input fits file name including path -outputfile the output png file name including path -x coordinate x -y coordinate y -a border of the square picture around (x, y) -s the string under the circled star """ if(x<0 or x>3056 or y<0 or y>3056 or a<0 or a>3056): print "x or y or a is out of border, please check!" return # if(x-a<0 or x+a>3056 or y-a<0 or y+a>3056): # print "x-a or x+a or y-a or y+a is out of border, please check!" if x-a<0: left=0 p=a-x else: left=x-a p=0 if x+a>3056: right=3056 else: right=x+a if y-a<0: top=0 q=y else: top=y-a q=a if y+a>3056: bottom=3056 q=a-(3056-y) else: bottom=y+a q=0 # print "-----" # print p # print q # print len(sys.argv) # print "-----" # open input file tmpImage = f2n.fromfits(inputfile) tmpImage.crop(left, right, top, bottom) tmpImage.setzscale("auto", "flat", 3, 100000, 300, 65000) tmpImage.makepilimage("lin", negative = False) if s!="": tmpImage.drawcircle(x, y, 5, (0,255,0), s) if x==1528 and y==1528 and a==1528: Info = inputfile tmpImage.writeinfo([Info], colour=(255,0,0)) tmpImage.tonet("tmp.png") # convert png to jpg fgImg=Image.open("tmp.png") bgImg=Image.new("RGB", (2*a, 2*a), (100,100,100)) bgImg.paste(fgImg,(p,q)) bgImg.save(outputfile, "JPEG") os.system("rm tmp.png")
# The following two lines are only needed if f2n.py and f2n_fonts # are not yet copied somewhere into your usual python path ! import sys sys.path.append("../.") # The directory that contains f2n.py and f2n_fonts ! import f2n import copy myimage = f2n.fromfits("example.fits") mylargeimage = copy.deepcopy(myimage) myimage.crop(70, 170, 60, 160) myimage.setzscale("auto", "ex") linimage = copy.deepcopy(myimage) logimage = copy.deepcopy(myimage) linimage.makepilimage("lin", negative = False) logimage.makepilimage("log", negative = False) linimage.upsample(2) linimage.writetitle("lin") linimage.drawcircle(112, 101, r=15) logimage.upsample(2) logimage.writetitle("log") mylargeimage.crop(30, 230, 60, 160)
print "- " * 30 print i + 1, image['imgname'] toreport = str(image['imgname']) + '\t' + str(i + 1) report.write(toreport) imgpsfdir = os.path.join(psfdir, image['imgname']) resultsdir = os.path.join(imgpsfdir, "results") pngpath = os.path.join(pngdir, image['imgname'] + ".png") blank256 = f2n.f2nimage(shape=(256, 256), fill=0.0, verbose=False) blank256.setzscale(0.0, 1.0) blank256.makepilimage(scale="lin", negative=False) totpsfimg = f2n.fromfits(os.path.join(resultsdir, "psf_1.fits"), verbose=False) #totpsfimg.rebin(2) totpsfimg.setzscale(1.0e-7, 1.0e-3) totpsfimg.makepilimage(scale="log", negative=False) totpsfimg.upsample(2) totpsfimg.writetitle("Total PSF") numpsfimg = f2n.fromfits(os.path.join(resultsdir, "psf_num_1.fits"), verbose=False) numpsfimg.setzscale(-0.02, 0.02) numpsfimg.makepilimage(scale="lin", negative=False) numpsfimg.upsample(2) numpsfimg.writetitle("Numerical PSF") txtendpiece = f2n.f2nimage(shape=(256, 256), fill=0.0, verbose=False) txtendpiece.setzscale(0.0, 1.0)
plotskim.append(""" <area shape="rect" COORDS="%i,%i,%i,%i" href="" onClick="return false" onmouseover="plotskim.src='%s';" alt=""> """ % (xmin, ymin, xmax, ymax, pngimage)) plotskim.append("</map>") filldict["zoomplot"] = "".join(plotskim) filldict["overviewplot"] = '<img src="overview.png" class="imgmargin">' ########################################## # PREPARING THE FITS IMAGES ########################################## # Deconvolution images : f2ng = f2n.fromfits(os.path.join(decdir, "g" +image[deckeyfilenum]+".fits"), verbose=False) f2ng.setzscale(-50, "ex") f2ng.makepilimage(scale = "log", negative = False) f2ng.upsample(4) f2ng.drawstarlist(objcosmicslist) f2ng.writetitle("Object") f2ng.tonet(os.path.join(destdir, "g.png")) f2nresi = f2n.fromfits(os.path.join(decdir, "resi" +image[deckeyfilenum]+".fits"), verbose=False) f2nresi.setzscale(-10, 10) f2nresi.makepilimage(scale = "lin", negative = False) f2nresi.upsample(2) f2nresi.writetitle("Residues") f2nresi.tonet(os.path.join(destdir, "resi.png"))
momentsml.tools.table.addstats(cat, "snr") momentsml.tools.io.writepickle(cat, catpath) print momentsml.tools.table.info(cat) writecat = cat["tru_flux", "snr_mean"] print writecat writecat.write(writecatpath, format="ascii") if name == "shearw": momentsml.tools.io.writepickle(cat, catpath) print momentsml.tools.table.info(cat) writecat = cat["x", "y", "snr"] print writecat writecat.write(writecatpath, format="ascii") for icas in range(ncas): myimage = f2n.fromfits(fitsimgpath) myimage.crop(0, stampsize * nrea, icas * stampsize, (icas + 1) * stampsize) #myimage.setzscale(-0.1, 3) #myimage.makepilimage("log", negative = False) myimage.setzscale(-0.3, 1.0) myimage.makepilimage("lin", negative=False) myimage.upsample(5) pngimgpath = os.path.join(workdir, "case_{}.png".format(icas)) myimage.tonet(pngimgpath) subprocess.Popen("display {}".format(pngimgpath), shell=True)
def irafalign(filepath, uknstarlist, refstarlist, shape, alifilepath=None, outdir="alipy_out", makepng=False, hdu=0, verbose=True): """ Uses iraf geomap and gregister to align the image. Three steps : * Write the matched source lists into an input file for geomap * Compute a geomap transform from these stars. * Run gregister :param filepath: FITS file to be aligned :type filepath: string :param uknstarlist: A list of stars from the "unknown" image to be aligned, that matches to ... :type uknstarlist: list of Star objects :param refstarlist: ... the list of corresponding stars in the reference image. :type refstarlist: list of Star objects :param shape: Output shape (width, height) :type shape: tuple :param alifilepath: where to save the aligned image. If None, I put it in the default directory. :type alifilepath: string :param makepng: If True I make a png of the aligned image as well. :type makepng: boolean :param hdu: The hdu of the fits file that you want me to use. 0 is primary. If multihdu, 1 is usually science. """ try: from pyraf import iraf except ImportError: print("Couldn't import pyraf !") return assert len(uknstarlist) == len(refstarlist) if len(uknstarlist) < 2: if verbose: print("Not enough stars for using geomap !") return basename = os.path.splitext(os.path.basename(filepath))[0] geomapinpath = basename + ".geomapin" geodatabasepath = basename + ".geodatabase" if os.path.isfile(geomapinpath): os.remove(geomapinpath) if os.path.isfile(geodatabasepath): os.remove(geodatabasepath) # Step 1, we write the geomap input. table = [] for (uknstar, refstar) in zip(uknstarlist, refstarlist): table.append([refstar.x, refstar.y, uknstar.x, uknstar.y]) geomap = open(geomapinpath, "w") writer = csv.writer(geomap, delimiter="\t") writer.writerows(table) geomap.close() # Step 2, geomap iraf.unlearn(iraf.geomap) iraf.geomap.fitgeom = "rscale" # You can change this to: # shift, xyscale, rotate, rscale iraf.geomap.function = "polynomial" # Surface type iraf.geomap.maxiter = 3 # Maximum number of rejection iterations iraf.geomap.reject = 3.0 # Rejection limit in sigma units # other options you could specify : # (xxorder= 2) Order of x fit in x # (xyorder= 2) Order of x fit in y # (xxterms= half) X fit cross terms type # (yxorder= 2) Order of y fit in x # (yyorder= 2) Order of y fit in y # (yxterms= half) Y fit cross terms type # (calctyp= real) Computation type iraf.geomap.transfo = "broccoli" # keep it iraf.geomap.interac = "no" # keep it iraf.geomap.verbose = "yes" # keep it # iraf.geomap.results = "bla.summary" # The optional results summary files geomapblabla = iraf.geomap(input=geomapinpath, database=geodatabasepath, xmin=1, xmax=shape[0], ymin=1, ymax=shape[1], Stdout=1) # We read this output ... for line in geomapblabla: if "X and Y scale:" in line: mapscale = line.split()[4:6] if "Xin and Yin fit rms:" in line: maprmss = line.split()[-2:] if "X and Y axis rotation:" in line: mapangles = line.split()[-4:-2] if "X and Y shift:" in line: mapshifts = line.split()[-4:-2] # not used? geomaprms = math.sqrt( float(maprmss[0]) * float(maprmss[0]) + float(maprmss[1]) * float(maprmss[1])) geomapangle = float(mapangles[0]) # % 360.0 geomapscale = 1.0 / float(mapscale[0]) if mapscale[0] != mapscale[1]: raise RuntimeError("Error reading geomap scale") if verbose: print(("IRAF geomap : Rotation %+11.6f [deg], " "scale %8.6f, RMS %.3f [pixel]") % (geomapangle, geomapscale, geomaprms)) # Step 3 if alifilepath is None: alifilepath = os.path.join(outdir, basename + "_gregister.fits") else: outdir = os.path.split(alifilepath)[0] if not os.path.isdir(outdir): os.makedirs(outdir) if os.path.isfile(alifilepath): os.remove(alifilepath) iraf.unlearn(iraf.gregister) iraf.gregister.geometry = "geometric" # linear, distortion, geometric iraf.gregister.interpo = "spline3" # linear, spline3 iraf.gregister.boundary = "constant" # padding with zero iraf.gregister.constant = 0.0 iraf.gregister.fluxconserve = "yes" if verbose: print("IRAF gregister ...") regblabla = iraf.gregister(input='%s[%s]' % (filepath, hdu), output=alifilepath, database=geodatabasepath, transform="broccoli", Stdout=1) # not used? if verbose: print("IRAF gregister done !") if os.path.isfile(geomapinpath): os.remove(geomapinpath) if os.path.isfile(geodatabasepath): os.remove(geodatabasepath) if makepng: try: import f2n except ImportError: print("Couldn't import f2n -- install it !") return myimage = f2n.fromfits(alifilepath, verbose=False) myimage.setzscale("auto", "auto") myimage.makepilimage("log", negative=False) myimage.writetitle(os.path.basename(alifilepath)) if not os.path.isdir(outdir): os.makedirs(outdir) myimage.tonet( os.path.join(outdir, os.path.basename(alifilepath) + ".png"))
#images = db.select(imgdb, ['gogogo', 'ell'], [True, ">0.65"], returnType='dict', sortFields=['mjd']) #images = db.select(imgdb, ['recno'], ['*'], returnType='dict', sortFields=['mjd']) print "I will treat", len(images), "images." proquest(askquestions) starttime = datetime.now() for i, image in enumerate(images): print "- " * 40 print i + 1, "/", len(images), ":", image['imgname'] fitsfile = image['rawimg'] f2nimg = f2n.fromfits(fitsfile) if crop: f2nimg.irafcrop(cropregion) f2nimg.setzscale(z1, z2) f2nimg.rebin(rebin) f2nimg.makepilimage(scale="log", negative=False) f2nimg.writetitle(image['imgname']) date = image['datet'] seeing = "Seeing : %4.2f" % image['seeing'] ell = "Ellipticity : %4.2f" % image['ell'] skylevel = "Sky level : %4.2f" % image['skylevel'] stddev = "Preali sky stddev : %4.2f" % image['prealistddev'] airmass = "Airmass : %4.2f" % image['airmass'] az = "Azimuth : %6.2f" % image['az']
# If cosmics are detected, we want to know where : cosmicslistpath = os.path.join(objdir, image['imgname'], "cosmicslist.pkl") if os.path.exists(cosmicslistpath): cosmicslist = readpickle(cosmicslistpath, verbose=False) else: cosmicslist = [] # And there number : ncosmics = "Cosmics : %i" % image[objcosmicskey] gpath = os.path.join(objdir, image['imgname'], "g.fits") sigpath = os.path.join(objdir, image['imgname'], "sig.fits") # First we build the image for g : g = f2n.fromfits(gpath, verbose=False) g.setzscale(z1, z2) g.makepilimage(scale = "log", negative = False) g.upsample(4) g.drawstarlist(cosmicslist) g.writetitle("g.fits") # We write long image names on two lines ... if len(image['imgname']) > 25: infolist = [image['imgname'][0:25], image['imgname'][25:]] else: infolist = [image['imgname']] g.writeinfo(infolist) # And now for sig :
errmsg = '' for i,image in enumerate(images): try: print "+ " * 30 print "%5i/%i : %s" % (i+1, nbrimages, image["imgname"]) pngpath = os.path.join(pngdirpath, "%s_sky.png" % image['imgname']) if os.path.isfile(pngpath) and not redofromscratch: print "File exists, I skip..." continue skyimagepath = os.path.join(alidir, image["imgname"] + "_sky.fits") skysubimagepath = os.path.join(alidir, image["imgname"] + "_skysub.fits") skyimage = f2n.fromfits(skyimagepath) skyimage.setzscale("ex", "ex") skyimage.rebin(3) skyimage.makepilimage(scale = "lin", negative = False) skyimage.upsample(2) skyimage.writetitle("Sky", colour=(255, 0, 0)) skysubimage = f2n.fromfits(skysubimagepath) skysubimage.setzscale("auto", "auto") skysubimage.rebin(3) skysubimage.makepilimage(scale = "log", negative = False) skysubimage.upsample(2) skysubimage.writetitle("Skysubtracted image") # We read the 20 strongest stars from sextractor : sexcatpath = os.path.join(alidir, image['imgname'] + ".cat")
import f2n myimage = f2n.fromfits("20131203_m06-0848+50_1053.fit") # We crop the FITS image (xmin, xmax, ymin, ymax). myimage.crop(70, 170, 60, 160) # The image is now 100 x 100 pixels. myimage.setzscale(4500, "ex") # z2 = "ex" means extrema -> maximum in this case. myimage.makepilimage("lin", negative = False) # We can choose to make a negative image. myimage.showcutoffs(redblue = True) # Pixels below z1 will be blue, above z2 will be red. # We upsample (= "zoom" without interpolation) the pixels. myimage.upsample(5) # The image is now 500 x 500 # We draw some labels and circles. # myimage.drawstarsfile("stars.cat", r=3, colour=(255, 0,0)) # Note that all the coordinates are those from the original image ! # Of course, the function drawstars allows you to the same # without any catalog file. # Same for drawing a rectangle... myimage.drawrectangle(118, 149, 137, 155, colour=(0,255,0), label="Empty region") myimage.writetitle("Hello World !", colour=(200, 200, 0)) myimage.writeinfo(["This is a demo", "of some possibilities",
cosmicslistpath = os.path.join(workdir, objkey, image['imgname'], "cosmicslist.pkl") if os.path.exists(cosmicslistpath): cosmicslist = readpickle(cosmicslistpath, verbose=False) else: cosmicslist = [] # To get the number of cosmics is easier ... objcosmicskey = objkey + "_cosmics" # objkey is redefined up there... ncosmics = image[objcosmicskey] pngpath = os.path.join(pngdir, image['imgname'] + ".png") if deconvonly: f2ndec = f2n.fromfits(os.path.join(decdir, "dec" + code + ".fits"), verbose=False) f2ndec.setzscale(-20, "auto") f2ndec.makepilimage(scale="exp", negative=False) f2ndec.upsample(8) #f2ndec.writeinfo(["Deconvolution"]) decpngpath = os.path.join(pngdir, image['imgname'] + "_deconly.png") f2n.compose([[f2ndec]], pngpath) continue #else... f2ndec = f2n.fromfits(os.path.join(decdir, "dec" + code + ".fits"), verbose=False) f2ndec.setzscale(-20, "auto") f2ndec.makepilimage(scale="log", negative=False) f2ndec.upsample(2)
def run_fits2png(input): image = f2n.fromfits(input[0]) #colour=( The way to read a FITS file. image.setzscale() # By default, automatic cutoffs will be calculated. makepilimage(image) # linear scale # image.writetitle(input[0], colour=(0,50,250)) image.tonet(input[1]) # write the png file
# The following two lines are only needed if f2n.py and f2n_fonts # are not yet copied somewhere into your usual python path ! import sys sys.path.append("../.") # The directory that contains f2n.py and f2n_fonts ! import f2n myimage = f2n.fromfits("example.fits") # We crop the FITS image (xmin, xmax, ymin, ymax). myimage.crop(70, 170, 60, 160) # The image is now 100 x 100 pixels. myimage.setzscale(4500, "ex") # z2 = "ex" means extrema -> maximum in this case. myimage.makepilimage("lin", negative=False) # We can choose to make a negative image. myimage.showcutoffs(redblue=True) # Pixels below z1 will be blue, above z2 will be red. # We upsample (= "zoom" without interpolation) the pixels. myimage.upsample(5) # The image is now 500 x 500 # We draw some labels and circles. myimage.drawstarfile("stars.cat", r=3, colour=(255, 0, 0)) # Note that all the coordinates are those from the original image ! # Of course, the function drawstars allows you to the same # without any catalog file.
preciserefmanstarsasdicts = [{ "name": s.name, "x": s.x, "y": s.y } for s in preciserefmanstars] refautostarsasdicts = [{ "name": s.name, "x": s.x, "y": s.y } for s in refautostars] #print refmanstarsasdicts combifitsfile = os.path.join(workdir, "%s.fits" % combibestkey) #combifitsfile = os.path.join(workdir, "ali", "%s_ali.fits" % refimgname) f2nimg = f2n.fromfits(combifitsfile) f2nimg.setzscale(z1=-5, z2=1000) #f2nimg.rebin(2) f2nimg.makepilimage(scale="log", negative=False) #f2nimg.drawstarlist(refautostarsasdicts, r = 30, colour = (150, 150, 150)) #f2nimg.drawstarlist(preciserefmanstarsasdicts, r = 7, colour = (255, 0, 0)) #f2nimg.writeinfo(["Sextractor stars (flag-filtered) : %i" % len(refautostarsasdicts)], colour = (150, 150, 150)) #f2nimg.writeinfo(["","Identified alignment stars with corrected sextractor coordinates : %i" % len(preciserefmanstarsasdicts)], colour = (255, 0, 0)) # We draw the rectangles around qso and empty region : lims = [map(int, x.split(':')) for x in lensregion[1:-1].split(',')] #f2nimg.drawrectangle(lims[0][0], lims[0][1], lims[1][0], lims[1][1], colour=(0,255,0), label = "Lens")
# usage: read_fits file.fits file.png import sys sys.path.append("/data/wwwhelio/html/gui/f2n") # The directory that contains f2n.py and f2n_fonts ! sys.path.append("/usr/local/www/apache22/data/hfc-gui/f2n") # The directory that contains f2n.py and f2n_fonts ! import pyfits,numpy,f2n import Image,ImageOps,ImageDraw # Converts the FITS file to PNG #if 'efz' in sys.argv[1] : # data = pyfits.getdata(sys.argv[1]) # im = Image.fromarray(numpy.uint8(data)) # im = im.transpose(Image.FLIP_TOP_BOTTOM) # im.save(sys.argv[2]) #else : myimage = f2n.fromfits(sys.argv[1]) myimage.setzscale("ex", "ex") myimage.makepilimage("lin") myimage.tonet(sys.argv[2]) # Get the P_ANGLE value from the FITS header hdulist = pyfits.open(sys.argv[1]) hd = hdulist[0].header if hd.has_key('p_angle') : angle = hdulist[0].header['p_angle'] # Rotate the PNG file from P_ANGLE if angle != 0 : im = Image.open(sys.argv[2]) im_rot = im.rotate(angle) im_rot.save(sys.argv[2]) # Get the SC_ROLL value from the FITS header
# and diff with ignored folders dirs_to_parse = source_dirs.difference(ignore_folders) print dirs_to_parse print ignore_folders print source_dirs # go through new folders for dir in dirs_to_parse: for fits_object in fits_parser.index_directory_of_fits_files( os.path.join(args.fits[0], dir), completed)['files']: out_stem = os.path.join(args.fits[1], 'images', str(fits_object['epoch'])) # json dump with open(out_stem + '.json', 'w') as file: json.dump(fits_object, file) # convert and dump image myimage = f2n.fromfits(fits_object['path']) myimage.setzscale() myimage.makepilimage('lin') print "------" myimage.tonet(out_stem + '.png') # add path to completed list completed.append(str(fits_object['path'])) with open(completed_path, "w+") as file: json.dump(completed, file) # refresh index of out dir with open(os.path.join(args.fits[1], 'images', 'index.json'), 'w') as file: json.dump([ f.replace('.json', '') for f in os.listdir(os.path.join(args.fits[1], 'images')) if f.endswith('json') and not f == 'ignore_folders.json' and not f == 'index.json'
def loadImageFromFits(self,fits): self.fitsToCrop = f2n.fromfits(fits)
def asteroids_phot(self, image_path, multi_object=True, target=None, aper_radius=None, plot_aper_test=False, radius=11, exposure=None, sqlite_file=None, table_name="asteroids", gain=0.57, max_mag=20, comp_snr=50): """ Photometry of asteroids. @param image_path: Path of FITS file. @type image_path: path @param multi_object: Apply photometry for other asteroids in the frame? @type multi_object: float @param target: Target object that photometry applied. If None, will be taken form FITS header. @type target: float @param radius: Aperture radius @type radius: float @param exposure: Exposure keyword of phot images. @type exposure: float @param plot_aper_test: Plot aperture test graph @type plot_aper_test: bloean @param exportdb: Export results SQLite3 database @type exportdb: path @param db_table: SQLite3 database table @type db_table: path @param gain: gain value for the image expressed in electrons per adu. @type gain: float @param max_mag: Faintest object limit. @type max_mag: float @param comp_snr: Minimum SNR of detected comparison star. @type comp_snr: float @return: bolean and file """ if ".fit" in os.path.basename(image_path): fitslist = sorted(glob.glob(image_path)) if fitslist == 0: print('No image FITS found in the {0}'.format(image_path)) raise SystemExit else: fitslist = sorted(glob.glob(image_path + '/*.fit?')) if fitslist == 0: print('No image FITS found in the {0}'.format(image_path)) raise SystemExit # aper_trigger check count aper_count = 0 for id, fitsfile in enumerate(fitslist): if fitsfile: hdu = fits.open(fitsfile)[0] else: print("FITS image has not been provided by the user!") raise SystemExit sb = Query() ac = AstCalc() to = TimeOps() fo = FitsOps(fitsfile) header = hdu.header w = WCS(header) naxis1 = fo.get_header('naxis1') naxis2 = fo.get_header('naxis2') odate = fo.get_header('date-obs') t1 = Time("{0}".format(odate), out_subfmt="date") dt = TimeDelta(12 * 3600, format='sec') onight = t1 - dt exptime = fo.get_header('exptime') if exptime is not None and exposure is not None: if float(exptime) != exposure: continue if aper_count == 0: aper_trigger = id aper_count += 1 if target is None: objct = fo.get_header('object') else: objct = str(target) filter = fo.get_header('filter').replace(" ", "_") # t1 = Time(odate.replace('T', ' ')) # exptime = fo.get_header('exptime') # dt = TimeDelta(exptime / 2.0, format='sec') # odate_middle = t1 + dt # jd = to.date2jd(odate_middle.value) jd = to.date2jd(odate) ra_dec = ac.center_finder(fitsfile, wcs_ref=True) image = f2n.fromfits(fitsfile, verbose=False) image.setzscale('auto', 'auto') image.makepilimage('log', negative=False) request = sb.find_skybot_objects(odate, ra_dec[0].degree, ra_dec[1].degree, radius=radius) if request[0]: if multi_object: asteroids = Table(np.sort(request[1][::-1], order=['m_v'])) else: asteroids = Table(np.sort(request[1], order=['num'])) mask = asteroids['num'] == str(objct).upper() asteroids = asteroids[mask] elif request[0] is False: print(request[1]) raise SystemExit data = hdu.data.astype(float) bkg = sep.Background(data) data_sub = data - bkg for i in range(len(asteroids)): if float(asteroids['m_v'][i]) <= max_mag: c = coordinates.SkyCoord('{0} {1}'.format( asteroids['ra(h)'][i], asteroids['dec(deg)'][i]), unit=(u.hourangle, u.deg), frame='icrs') # asteroid's X and Y coor a_x, a_y = w.wcs_world2pix(c.ra.degree, c.dec.degree, 1) if naxis1 < a_x or naxis2 < a_y or a_x < 0 or a_y < 0: continue # phot asteroids flux, fluxerr, flag = sep.sum_circle(data_sub, a_x, a_y, 6, err=bkg.globalrms, gain=gain) if flux == 0.0 or fluxerr == 0.0: print("Bad asteroid selected (out of frame!)!") raise SystemExit if id == aper_trigger: snr = [] for aper in range(30): # phot asteroids flux_test, fluxerr_test, flag_test = sep.sum_circle( data_sub, a_x, a_y, aper, err=bkg.globalrms, gain=gain) snr.append([aper, (flux_test / fluxerr_test)]) npsnr = np.array(snr) maxtab, mintab = peakdet(npsnr[:, 1], 0.1) try: aper_radius = maxtab[:, 0][0] print("Aperture calculated: {0} px".format( aper_radius)) except IndexError: continue if plot_aper_test: plt.title(asteroids['num'][i]) plt.xlabel('Aperture (px)') plt.ylabel('SNR') plt.scatter(npsnr[:, 0], npsnr[:, 1]) plt.scatter(maxtab[:, 0], maxtab[:, 1], color='red') plt.scatter(mintab[:, 0], mintab[:, 1], color='yellow') plt.show() magt_i = ac.flux2mag(flux, float(exptime)) magt_i_err = fluxerr / flux * 2.5 / math.log(10) min_mag_ast = float(asteroids['m_v'][i]) - 2 label = '{0}'.format(asteroids['num'][i]) image.drawcircle(a_x, a_y, r=aper_radius, colour=(255, 0, 0), label=label) if i == 0 and id == aper_trigger: comptable = sb.query_color(c.ra.degree, c.dec.degree, 5.0 / 60.0, min_mag=min_mag_ast, max_mag=19.5) s_comptable = sb.sort_stars(comptable, min_mag_ast) if len(s_comptable) == 0: continue phot_res_list = [] # phot comp. stars for j in range(len(s_comptable)): # star's X and Y coor s_x, s_y = w.wcs_world2pix(s_comptable['RAJ2000'][j], s_comptable['DEJ2000'][j], 1) if naxis1 < s_x or naxis2 < s_y or s_x < 0 or s_y < 0: continue # print('Circle', s_x, s_y, 10) flux, fluxerr, flag = sep.sum_circle(data_sub, s_x, s_y, aper_radius, err=bkg.globalrms, gain=gain) if flux == 0.0 or fluxerr == 0.0: print("Bad star selected!") raise SystemExit if (flux / fluxerr) <= comp_snr: continue magc_i = ac.flux2mag(flux, float(exptime)) magc_i_err = fluxerr / flux * 2.5 / math.log(10) try: magt = (float(magt_i) - float(magc_i)) + s_comptable['Rmag'][j] magt_err = math.sqrt( math.pow(float(magt_i_err), 2) + math.pow(float(magc_i_err), 2)) except: continue label = '{0}'.format(s_comptable['NOMAD1'][j]) image.drawcircle(s_x, s_y, r=aper_radius, colour=(0, 255, 0), label=label) phot_res_list.append([ asteroids['num'][i], jd, onight.iso, float(magt_i), float(magt_i_err), float(magc_i), float(magc_i_err), float(magt), float(magt_err), asteroids['m_v'][i], s_comptable['NOMAD1'][j], s_comptable['Rmag'][j] ]) np_phot_res = np.array(phot_res_list) if len(np_phot_res) == 0: continue # magnitude average magt_avr = np.average(np_phot_res[:, 7].astype(float), weights=np_phot_res[:, 8].astype(float)) # magt_std calc. magt_std = np.std(np_phot_res[:, 7].astype(float)) np_magt_avr_std = [[magt_avr, magt_std, filter, exptime] for i in range(len(np_phot_res))] k = np.array(np_magt_avr_std).reshape( len(np_magt_avr_std), 4) # numpy array with magt_avr np_phot_res_avg_std = np.concatenate((np_phot_res, k), axis=1) phot_res_table = Table( np_phot_res_avg_std, names=('ast_num', 'jd', 'onight', 'magt_i', 'magt_i_err', 'magc_i', 'magc_i_err', 'magt', 'magt_err', 'ast_mag_cat', 'nomad1', 'star_Rmag', 'magt_avr', 'magt_std', 'filter', 'exposure'), dtype=('U10', 'S25', 'U10', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'U20', 'f8', 'f8', 'f8', 'U20', 'f8')) phot_res_table['magt_i'].format = '.3f' phot_res_table['magt_i_err'].format = '.3f' phot_res_table['magc_i'].format = '.3f' phot_res_table['magc_i_err'].format = '.3f' phot_res_table['magt'].format = '.3f' phot_res_table['magt_err'].format = '.3f' phot_res_table['magt_avr'].format = '.3f' phot_res_table['magt_std'].format = '.3f' with open( '{0}/{1}.txt'.format(os.getcwd(), asteroids['num'][i]), 'a') as f_handle: f_handle.seek(0, os.SEEK_END) if not os.path.isfile(str(f_handle)): phot_res_table.write( f_handle, format='ascii.commented_header') else: phot_res_table.write(f_handle, format='ascii.no_header') if sqlite_file is not None: self.table_to_database(phot_res_table, sqlite_file=sqlite_file, table_name=table_name) # Test time.sleep(0.2) self.update_progress( "Photometry is done for: {0}".format(fitsfile), id / len(fitslist)) image.writetitle(os.path.basename(fitsfile)) fitshead, fitsextension = os.path.splitext(fitsfile) image.writeinfo([odate], colour=(255, 100, 0)) image.tonet('{0}.png'.format(fitshead)) self.update_progress("Photometry done!", 1) return (True)
def asteroids_phot(self, image_path, multi_object=True, target=None, aper_radius=None, plot_aper_test=False, radius=11, exposure=None, sqlite_file=None, table_name="asteroids", gain=0.57, max_mag=20, comp_snr=50): """ Photometry of asteroids. @param image_path: Path of FITS file. @type image_path: path @param multi_object: Apply photometry for other asteroids in the frame? @type multi_object: float @param target: Target object that photometry applied. If None, will be taken form FITS header. @type target: float @param radius: Aperture radius @type radius: float @param exposure: Exposure keyword of phot images. @type exposure: float @param plot_aper_test: Plot aperture test graph @type plot_aper_test: bloean @param exportdb: Export results SQLite3 database @type exportdb: path @param db_table: SQLite3 database table @type db_table: path @param gain: gain value for the image expressed in electrons per adu. @type gain: float @param max_mag: Faintest object limit. @type max_mag: float @param comp_snr: Minimum SNR of detected comparison star. @type comp_snr: float @return: bolean and file """ if ".fit" in os.path.basename(image_path): fitslist = sorted(glob.glob(image_path)) if fitslist == 0: print('No image FITS found in the {0}'.format(image_path)) raise SystemExit else: fitslist = sorted(glob.glob(image_path + '/*.fit?')) if fitslist == 0: print('No image FITS found in the {0}'.format(image_path)) raise SystemExit # aper_trigger check count aper_count = 0 for id, fitsfile in enumerate(fitslist): if fitsfile: hdu = fits.open(fitsfile)[0] else: print("FITS image has not been provided by the user!") raise SystemExit sb = Query() ac = AstCalc() to = TimeOps() fo = FitsOps(fitsfile) header = hdu.header w = WCS(header) naxis1 = fo.get_header('naxis1') naxis2 = fo.get_header('naxis2') odate = fo.get_header('date-obs') t1 = Time("{0}".format(odate), out_subfmt="date") dt = TimeDelta(12 * 3600, format='sec') onight = t1 - dt exptime = fo.get_header('exptime') if exptime is not None and exposure is not None: if float(exptime) != exposure: continue if aper_count == 0: aper_trigger = id aper_count += 1 if target is None: objct = fo.get_header('object') else: objct = str(target) filter = fo.get_header('filter').replace(" ", "_") # t1 = Time(odate.replace('T', ' ')) # exptime = fo.get_header('exptime') # dt = TimeDelta(exptime / 2.0, format='sec') # odate_middle = t1 + dt # jd = to.date2jd(odate_middle.value) jd = to.date2jd(odate) ra_dec = ac.center_finder(fitsfile, wcs_ref=True) image = f2n.fromfits(fitsfile, verbose=False) image.setzscale('auto', 'auto') image.makepilimage('log', negative=False) request = sb.find_skybot_objects(odate, ra_dec[0].degree, ra_dec[1].degree, radius=radius) if request[0]: if multi_object: asteroids = Table(np.sort(request[1][::-1], order=['m_v'])) else: asteroids = Table(np.sort(request[1], order=['num'])) mask = asteroids['num'] == str(objct).upper() asteroids = asteroids[mask] elif request[0] is False: print(request[1]) raise SystemExit data = hdu.data.astype(float) bkg = sep.Background(data) data_sub = data - bkg for i in range(len(asteroids)): if float(asteroids['m_v'][i]) <= max_mag: c = coordinates.SkyCoord('{0} {1}'.format( asteroids['ra(h)'][i], asteroids['dec(deg)'][i]), unit=(u.hourangle, u.deg), frame='icrs') # asteroid's X and Y coor a_x, a_y = w.wcs_world2pix(c.ra.degree, c.dec.degree, 1) if naxis1 < a_x or naxis2 < a_y or a_x < 0 or a_y < 0: continue # phot asteroids flux, fluxerr, flag = sep.sum_circle( data_sub, a_x, a_y, 6, err=bkg.globalrms, gain=gain) if flux == 0.0 or fluxerr == 0.0: print("Bad asteroid selected (out of frame!)!") raise SystemExit if id == aper_trigger: snr = [] for aper in range(30): # phot asteroids flux_test, fluxerr_test, flag_test = sep.sum_circle( data_sub, a_x, a_y, aper, err=bkg.globalrms, gain=gain) snr.append([aper, (flux_test/fluxerr_test)]) npsnr = np.array(snr) maxtab, mintab = peakdet(npsnr[:, 1], 0.1) try: aper_radius = maxtab[:, 0][0] print("Aperture calculated: {0} px".format( aper_radius)) except IndexError: continue if plot_aper_test: plt.title(asteroids['num'][i]) plt.xlabel('Aperture (px)') plt.ylabel('SNR') plt.scatter(npsnr[:, 0], npsnr[:, 1]) plt.scatter(maxtab[:, 0], maxtab[:, 1], color='red') plt.scatter(mintab[:, 0], mintab[:, 1], color='yellow') plt.show() magt_i = ac.flux2mag(flux, float(exptime)) magt_i_err = fluxerr / flux * 2.5 / math.log(10) min_mag_ast = float(asteroids['m_v'][i]) - 2 label = '{0}'.format(asteroids['num'][i]) image.drawcircle(a_x, a_y, r=aper_radius, colour=(255, 0, 0), label=label) if i == 0 and id == aper_trigger: comptable = sb.query_color(c.ra.degree, c.dec.degree, 5.0 / 60.0, min_mag=min_mag_ast, max_mag=19.5) s_comptable = sb.sort_stars(comptable, min_mag_ast) if len(s_comptable) == 0: continue phot_res_list = [] # phot comp. stars for j in range(len(s_comptable)): # star's X and Y coor s_x, s_y = w.wcs_world2pix(s_comptable['RAJ2000'][j], s_comptable['DEJ2000'][j], 1) if naxis1 < s_x or naxis2 < s_y or s_x < 0 or s_y < 0: continue # print('Circle', s_x, s_y, 10) flux, fluxerr, flag = sep.sum_circle( data_sub, s_x, s_y, aper_radius, err=bkg.globalrms, gain=gain) if flux == 0.0 or fluxerr == 0.0: print("Bad star selected!") raise SystemExit if (flux / fluxerr) <= comp_snr: continue magc_i = ac.flux2mag(flux, float(exptime)) magc_i_err = fluxerr / flux * 2.5 / math.log(10) try: magt = (float(magt_i) - float(magc_i)) + s_comptable['Rmag'][j] magt_err = math.sqrt( math.pow(float(magt_i_err), 2) + math.pow(float(magc_i_err), 2)) except: continue label = '{0}'.format(s_comptable['NOMAD1'][j]) image.drawcircle(s_x, s_y, r=aper_radius, colour=(0, 255, 0), label=label) phot_res_list.append([asteroids['num'][i], jd, onight.iso, float(magt_i), float(magt_i_err), float(magc_i), float(magc_i_err), float(magt), float(magt_err), asteroids['m_v'][i], s_comptable['NOMAD1'][j], s_comptable['Rmag'][j]]) np_phot_res = np.array(phot_res_list) if len(np_phot_res) == 0: continue # magnitude average magt_avr = np.average(np_phot_res[:, 7].astype(float), weights=np_phot_res[:, 8].astype( float)) # magt_std calc. magt_std = np.std(np_phot_res[:, 7].astype(float)) np_magt_avr_std = [[magt_avr, magt_std, filter, exptime] for i in range( len(np_phot_res))] k = np.array(np_magt_avr_std).reshape( len(np_magt_avr_std), 4) # numpy array with magt_avr np_phot_res_avg_std = np.concatenate( (np_phot_res, k), axis=1) phot_res_table = Table(np_phot_res_avg_std, names=('ast_num', 'jd', 'onight', 'magt_i', 'magt_i_err', 'magc_i', 'magc_i_err', 'magt', 'magt_err', 'ast_mag_cat', 'nomad1', 'star_Rmag', 'magt_avr', 'magt_std', 'filter', 'exposure'), dtype=('U10', 'S25', 'U10', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'U20', 'f8', 'f8', 'f8', 'U20', 'f8')) phot_res_table['magt_i'].format = '.3f' phot_res_table['magt_i_err'].format = '.3f' phot_res_table['magc_i'].format = '.3f' phot_res_table['magc_i_err'].format = '.3f' phot_res_table['magt'].format = '.3f' phot_res_table['magt_err'].format = '.3f' phot_res_table['magt_avr'].format = '.3f' phot_res_table['magt_std'].format = '.3f' with open('{0}/{1}.txt'.format( os.getcwd(), asteroids['num'][i]), 'a') as f_handle: f_handle.seek(0, os.SEEK_END) if not os.path.isfile(str(f_handle)): phot_res_table.write( f_handle, format='ascii.commented_header') else: phot_res_table.write(f_handle, format='ascii.no_header') if sqlite_file is not None: self.table_to_database(phot_res_table, sqlite_file=sqlite_file, table_name=table_name) # Test time.sleep(0.2) self.update_progress( "Photometry is done for: {0}".format(fitsfile), id / len(fitslist)) image.writetitle(os.path.basename(fitsfile)) fitshead, fitsextension = os.path.splitext(fitsfile) image.writeinfo([odate], colour=(255, 100, 0)) image.tonet('{0}.png'.format(fitshead)) self.update_progress("Photometry done!", 1) return(True)
def loadImageFromFits(self, fits): self.fitsToCrop = f2n.fromfits(fits)
def get_cropped_image(filepath, xmin, xmax, ymin, ymax): im = f2n.fromfits(filepath) # TODO don't read the file each time im.crop(xmin, xmax, ymin, ymax) return im
"y": s.y } for s in preciserefmanstars] refautostarsasdicts = [{ "name": s.name, "x": s.x, "y": s.y } for s in refautostars] #print refmanstarsasdicts if defringed: reffitsfile = os.path.join(alidir, refimage['imgname'] + "_defringed.fits") else: reffitsfile = os.path.join(alidir, refimage['imgname'] + "_skysub.fits") f2nimg = f2n.fromfits(reffitsfile) f2nimg.setzscale(z1=0, z2=1000) #f2nimg.rebin(2) f2nimg.makepilimage(scale="log", negative=False) f2nimg.drawstarlist(refautostarsasdicts, r=30, colour=(150, 150, 150)) #f2nimg.drawstarlist(refmanstarsasdicts, r = 25, colour = (0, 0, 255)) f2nimg.drawstarlist(preciserefmanstarsasdicts, r=5, colour=(255, 0, 0)) f2nimg.writeinfo( ["Sextractor stars (flag-filtered) : %i" % len(refautostarsasdicts)], colour=(150, 150, 150)) #f2nimg.writeinfo(["", "Stars that you dutifully wrote in the alignment star catalog : %i" % len(refmanstarsasdicts)], colour = (0, 0, 255)) f2nimg.writeinfo([ "", "Identified alignment stars with corrected sextractor coordinates : %i" %
hdu = pyfits.PrimaryHDU(pixelarray.transpose(), hdr) hdu.verify("fix") hdu.writeto(newfitsfilepath) print "Filter histo :" filterhisto = [(f, filterlist.count(f)) for f in sorted(list(set(filterlist)))] print "\n".join(["%s : %i" % h for h in filterhisto]) ######## print "\n Now I make pngs of these croped images vs raw images" proquest(askquestions) if not os.path.isdir(pngdir): os.mkdir(pngdir) croppaths = os.popen('ls '+destdir) croppaths = [os.path.join(destdir,croppath).split('\n')[0] for croppath in croppaths] for origpath, croppath in zip(origpaths,croppaths): cropimage = f2n.fromfits(croppath) cropimage.setzscale("auto", "auto") cropimage.makepilimage(scale = 'log', negative = False) pngpath = os.path.join(pngdir,os.path.basename(croppath)).split('.fits')[0]+'.png' cropimage.tonet(pngpath)
ignore_folders_path = os.path.join(args.fits[1], 'ignore_folders.json') source_dirs = set([name for name in os.listdir(args.fits[0]) if os.path.isdir(os.path.join(args.fits[0], name))]) # and diff with ignored folders dirs_to_parse = source_dirs.difference(ignore_folders) print dirs_to_parse print ignore_folders print source_dirs # go through new folders for dir in dirs_to_parse: for fits_object in fits_parser.index_directory_of_fits_files(os.path.join(args.fits[0], dir), completed)['files']: out_stem = os.path.join(args.fits[1], 'images', str(fits_object['epoch'])) # json dump with open(out_stem + '.json', 'w') as file: json.dump(fits_object, file) # convert and dump image myimage = f2n.fromfits(fits_object['path']) myimage.setzscale() myimage.makepilimage('lin') print "------" myimage.tonet(out_stem + '.png') # add path to completed list completed.append(str(fits_object['path'])) with open(completed_path, "w+") as file: json.dump(completed, file) # refresh index of out dir with open(os.path.join(args.fits[1], 'images', 'index.json'), 'w') as file: json.dump([f.replace('.json', '') for f in os.listdir(os.path.join(args.fits[1], 'images')) if f.endswith('json') and not f == 'ignore_folders.json' and not f == 'index.json'], file) if args.init is not None:
# The following two lines are only needed if f2n.py and f2n_fonts # are not yet copied somewhere into your usual python path ! import sys sys.path.append("../.") # The directory that contains f2n.py and f2n_fonts ! # Now we can go on as usual : import f2n # And show a minimalistic exemple : myimage = f2n.fromfits("example.fits") # The way to read a FITS file. myimage.setzscale() # By default, automatic cutoffs will be calculated. myimage.makepilimage("lin") # By default, a log transformation would be used. myimage.tonet("1_simple.png") # We write the png.
def irafalign(filepath, uknstarlist, refstarlist, shape, alifilepath=None, outdir="alipy_out", makepng=False, hdu=0, verbose=True): """ Uses iraf geomap and gregister to align the image. Three steps : * Write the matched source lists into an input file for geomap * Compute a geomap transform from these stars. * Run gregister :param filepath: FITS file to be aligned :type filepath: string :param uknstarlist: A list of stars from the "unknown" image to be aligned, that matches to ... :type uknstarlist: list of Star objects :param refstarlist: ... the list of corresponding stars in the reference image. :type refstarlist: list of Star objects :param shape: Output shape (width, height) :type shape: tuple :param alifilepath: where to save the aligned image. If None, I put it in the default directory. :type alifilepath: string :param makepng: If True I make a png of the aligned image as well. :type makepng: boolean :param hdu: The hdu of the fits file that you want me to use. 0 is primary. If multihdu, 1 is usually science. """ try: from pyraf import iraf except ImportError: print("Couldn't import pyraf !") return assert len(uknstarlist) == len(refstarlist) if len(uknstarlist) < 2: if verbose: print("Not enough stars for using geomap !") return basename = os.path.splitext(os.path.basename(filepath))[0] geomapinpath = basename + ".geomapin" geodatabasepath = basename + ".geodatabase" if os.path.isfile(geomapinpath): os.remove(geomapinpath) if os.path.isfile(geodatabasepath): os.remove(geodatabasepath) # Step 1, we write the geomap input. table = [] for (uknstar, refstar) in zip(uknstarlist, refstarlist): table.append([refstar.x, refstar.y, uknstar.x, uknstar.y]) geomap = open(geomapinpath, "wb") # b needed for csv writer = csv.writer(geomap, delimiter="\t") writer.writerows(table) geomap.close() # Step 2, geomap iraf.unlearn(iraf.geomap) iraf.geomap.fitgeom = "rscale" # You can change this to: # shift, xyscale, rotate, rscale iraf.geomap.function = "polynomial" # Surface type iraf.geomap.maxiter = 3 # Maximum number of rejection iterations iraf.geomap.reject = 3.0 # Rejection limit in sigma units # other options you could specify : # (xxorder= 2) Order of x fit in x # (xyorder= 2) Order of x fit in y # (xxterms= half) X fit cross terms type # (yxorder= 2) Order of y fit in x # (yyorder= 2) Order of y fit in y # (yxterms= half) Y fit cross terms type # (calctyp= real) Computation type iraf.geomap.transfo = "broccoli" # keep it iraf.geomap.interac = "no" # keep it iraf.geomap.verbose = "yes" # keep it # iraf.geomap.results = "bla.summary" # The optional results summary files geomapblabla = iraf.geomap(input=geomapinpath, database=geodatabasepath, xmin=1, xmax=shape[0], ymin=1, ymax=shape[1], Stdout=1) # We read this output ... for line in geomapblabla: if "X and Y scale:" in line: mapscale = line.split()[4:6] if "Xin and Yin fit rms:" in line: maprmss = line.split()[-2:] if "X and Y axis rotation:" in line: mapangles = line.split()[-4:-2] if "X and Y shift:" in line: mapshifts = line.split()[-4:-2] # not used? geomaprms = math.sqrt(float(maprmss[0]) * float(maprmss[0]) + float(maprmss[1]) * float(maprmss[1])) geomapangle = float(mapangles[0]) # % 360.0 geomapscale = 1.0 / float(mapscale[0]) if mapscale[0] != mapscale[1]: raise RuntimeError("Error reading geomap scale") if verbose: print(("IRAF geomap : Rotation %+11.6f [deg], " "scale %8.6f, RMS %.3f [pixel]") % (geomapangle, geomapscale, geomaprms)) # Step 3 if alifilepath is None: alifilepath = os.path.join(outdir, basename + "_gregister.fits") else: outdir = os.path.split(alifilepath)[0] if not os.path.isdir(outdir): os.makedirs(outdir) if os.path.isfile(alifilepath): os.remove(alifilepath) iraf.unlearn(iraf.gregister) iraf.gregister.geometry = "geometric" # linear, distortion, geometric iraf.gregister.interpo = "spline3" # linear, spline3 iraf.gregister.boundary = "constant" # padding with zero iraf.gregister.constant = 0.0 iraf.gregister.fluxconserve = "yes" if verbose: print("IRAF gregister ...") regblabla = iraf.gregister(input='%s[%s]' % (filepath, hdu), output=alifilepath, database=geodatabasepath, transform="broccoli", Stdout=1) # not used? if verbose: print("IRAF gregister done !") if os.path.isfile(geomapinpath): os.remove(geomapinpath) if os.path.isfile(geodatabasepath): os.remove(geodatabasepath) if makepng: try: import f2n except ImportError: print("Couldn't import f2n -- install it !") return myimage = f2n.fromfits(alifilepath, verbose=False) myimage.setzscale("auto", "auto") myimage.makepilimage("log", negative=False) myimage.writetitle(os.path.basename(alifilepath)) if not os.path.isdir(outdir): os.makedirs(outdir) myimage.tonet( os.path.join(outdir, os.path.basename(alifilepath) + ".png"))
import momentsml import matplotlib.pyplot as plt import numpy as np import config import os import f2n filepath = os.path.join(config.workdir, "psf.fits") psf = momentsml.tools.io.fromfits(filepath) print psf.shape #plt.matshow(np.log10(psf), cmap=plt.get_cmap("gray")) #plt.show() myimage = f2n.fromfits(filepath) #crop = 1 #myimage.crop(crop, -crop, crop, -crop) myimage.setzscale(1.0e-5, 0.001) myimage.makepilimage("log", negative=False) #myimage.setzscale(-0.3, 1.0) #myimage.makepilimage("lin", negative = False) myimage.upsample(5) pngimgpath = os.path.join(config.workdir, "psf.png") myimage.tonet(pngimgpath)