import forkmap #import multiprocessing # if forkmap fails... import star from readandreplace_fct import * import src.lib.utils as fn from MCS_interface import MCS_interface #### rewriteconfig = True nofitnum = False redofromscratch = True #### if rewriteconfig == True: psfstars = star.readmancat(psfstarcat) nbrpsf = len(psfstars) starscouplelist = repr([(int(s.x), int(s.y)) for s in psfstars]) config_template = justread(os.path.join(configdir, "template_pyMCS_psf_config.py")) # Select images to treat db = KirbyBase() if thisisatest : print "This is a test run." images = db.select(imgdb, ['gogogo', 'treatme', 'testlist',psfkeyflag], [True, True, True, True], returnType='dict', sortFields=['setname', 'mjd']) elif update: print "This is an update." images = db.select(imgdb, ['gogogo', 'treatme', 'updating', psfkeyflag], [True, True, True, True], returnType='dict', sortFields=['setname', 'mjd']) askquestions=False else :
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'] infolist = ["", date, seeing, ell, skylevel, stddev, airmass, az] f2nimg.writeinfo(infolist) # try to add new stuff: plot sextracted objects. print "WARNING: if you want to display these images, you need to change your default.param output parameters to: NUMBER / X_IMAGE / Y_IMAGE only you can use the imgplot.param as the PARAMETERS_NAME of your default_see_template.sex file" starcat = os.path.join(alidir, image["imgname"] + '.cat') import star extstars = star.readmancat(starcat) for star in extstars: f2nimg.drawcircle(star.x, star.y, r=10, colour='white', label=None) #pngname = "%04d.png" % (i+1) pngname = image['imgname'] + ".png" pngpath = os.path.join(pngdir, pngname) f2nimg.tonet(pngpath) orderlink = os.path.join( pngdir, "%05i.png" % (i + 1)) # a link to get the images sorted for the movies etc. os.symlink(pngpath, orderlink) if i == 0: print "Here is the first image:"
import headerstuff import star import numpy as np import matplotlib.pyplot as plt import matplotlib.dates print "You want to analyze the deconvolution %s" %deckey print "Deconvolved object : %s" % decobjname if plotnormfieldname == None: print "I will use the normalization coeffs used for the deconvolution." else: print "Using %s for the normalization." % (plotnormfieldname) deckeynormused = plotnormfieldname ptsources = star.readmancat(ptsrccat) print "Number of point sources : %i" % len(ptsources) print "Names of sources : %s" % ", ".join([s.name for s in ptsources]) db = KirbyBase() images = db.select(imgdb, [deckeyfilenum], ['\d\d*'], returnType='dict', useRegExp=True, sortFields=['mjd']) print "%i images" % len(images) fieldnames = db.getFieldNames(imgdb) plt.figure(figsize=(15,15)) mhjds = np.array([image["mhjd"] for image in images]) for s in ptsources:
# We look for the ds9 region files, read them, and mask corresponding regions in the original images !!. # Useful only with a small number of psf stars, very polluted (i.e. space images such HST images) execfile("../config.py") from kirbybase import KirbyBase, KBError from variousfct import * import cosmics # used to read and write the fits files import ds9reg import glob import numpy as np import star import sys psfstars = star.readmancat(psfstarcat) # We read the region files for i, s in enumerate(psfstars): s.filenumber = (i + 1) possiblemaskfilepath = os.path.join(configdir, "%s_mask_%s.reg" % (psfkey, s.name)) if os.path.exists(possiblemaskfilepath): s.reg = ds9reg.regions( 128, 128) # hardcoded for now # THIS IS BAD MALTE, THIS IS BAD !!!!! s.reg.readds9(possiblemaskfilepath, verbose=False) s.reg.buildmask(verbose=False) print "You masked %i pixels of star %s." % (np.sum(s.reg.mask), s.name) else: print "No mask file for star %s." % (s.name)
print "I made an alias to this reference image here :" print linkpath print "Saturation level (in e-) of ref image : %.2f" % ( refimage["saturlevel"] * refimage["gain"]) print "Now I will need some alignment stars (write them into configdir/alistars.cat)." proquest(askquestions) # Load the reference sextractor catalog refsexcat = os.path.join(alidir, refimage['imgname'] + ".cat") refautostars = star.readsexcat(refsexcat, maxflag=16, posflux=True) refautostars = star.sortstarlistbyflux(refautostars) refscalingfactor = refimage['scalingfactor'] # read and identify the manual reference catalog refmanstars = star.readmancat( alistarscat) # So these are the "manual" star coordinates id = star.listidentify( refmanstars, refautostars, tolerance=identtolerance, onlysingle=True, verbose=True) # We find the corresponding precise sextractor coordinates if len(id["nomatchnames"]) != 0: print "Warning : the following stars could not be identified in the sextractor catalog :" print "\n".join(id["nomatchnames"]) print "I will go on, disregarding these stars." proquest(askquestions) preciserefmanstars = star.sortstarlistbyflux(id["match"]) maxalistars = len(refmanstars)
execfile("../config.py") from kirbybase import KirbyBase, KBError from variousfct import * from readandreplace_fct import * import star import numpy as np in2filepath = os.path.join(decdir, "in2.txt") in2file = open(in2filepath) in2filelines = in2file.readlines() in2file.close() print "Reading from :" print in2filepath ptsrcs = star.readmancat(ptsrccat) nbptsrcs = len(ptsrcs) #print in2filelines # quick and dirty filtering of the trash ... goodlines = [] for line in in2filelines: if line[0] == "-" or line[0] == "|": continue goodlines.append(line) # we translate all this into a tiny db : ptsrcdb = [] for (i, ptsrc) in enumerate(ptsrcs):
import star import numpy as np import matplotlib.pyplot as plt db = KirbyBase() images = db.select(imgdb, ['gogogo', 'treatme'], [True, True], returnType='dict') nbrofimages = len(images) print "I respect treatme, and selected only %i images" % (nbrofimages) # Read the manual star catalog : photomstarscatpath = os.path.join(configdir, "photomstars.cat") photomstars = star.readmancat(photomstarscatpath) print "Photom stars :" #print "\n".join(["%s\t%.2f\t%.2f" % (s.name, s.x, s.y) for s in photomstars]) for s in photomstars: plt.figure(figsize=(12, 8)) peakadus = np.array( [image["%s_%s_peakadu" % (sexphotomname, s.name)] for image in images]) n, bins, patches = plt.hist(peakadus, 1000, range=(0, 67000), histtype='stepfilled', facecolor='grey')
""" Little helper that creates the psfstars.cat files from the alistar catalog and the psfname """ execfile("../config.py") from variousfct import * import star import os, sys # Read the manual star catalog : alistarscatpath = os.path.join(configdir, "alistars.cat") alistars = star.readmancat(alistarscatpath) psfnamestars = [ e for e in psfname ] # we assume psfname is only the list of stars, named with only 1 letter (no aa or other funny stuff) mystars = [s for s in alistars if s.name in psfnamestars] print "I will write individual coordinates catalogs for the following stars:" print[star.name for star in mystars] proquest(askquestions) # the stars if os.path.isfile(os.path.join(configdir, "psf_%s.cat" % psfname)): print "The psf catalog already exists ! I stop here." sys.exit() else: file = open(os.path.join(configdir, "psf_%s.cat" % psfname), 'w') for star in mystars: file.write(star.name + '\t' + str(star.x) + '\t' + str(star.y) + '\t' +
import star import numpy as np db = KirbyBase() images = db.select(imgdb, ["gogogo", "treatme", "decfilenum_" + selectiondeckey], [True, True, '\d\d*'], returnType='dict', useRegExp=True) print "I will export the deconvolution %s" % selectiondeckey print "This corresponds to ", len(images), "images." # We extend these general fields by the flux measurements for the selected deconvolution. ptsrc = star.readmancat(os.path.join(configdir, selectiondeckey + "_ptsrc.cat")) print "Sources : " + ", ".join([src.name for src in ptsrc]) proquest(askquestions) generalcolumns = [{ "name": fieldname, "data": [image[fieldname] for image in images] } for fieldname in generalfields] generalcolumns.append({ "name": "decnormcoeff", "data": [image["decnorm_" + selectiondeckey] for image in images] }) # This field called "decnormcoeff" is the actual normalization coefficients used for your deconvolution.
startat = 0 # you can put here an "iteration" number from which you want to restart, if you had to stop the scirpt etc. redofromscratch = True ###################### lookbackkey = deckey # This could be hardcoded to any other string. lookbackdir = os.path.join(workdir, "lookback_" + deckey) # Greeting ... print "So you want to build lookback pages for", deckey, "?" # We read in the point sources that should be available ptsrc = [src.name for src in star.readmancat(ptsrccat)] print "Sources : ", ptsrc variousfct.proquest(askquestions) if os.path.isdir(lookbackdir) and redofromscratch: print "The lookbackdir exists." print "I would delete existing stuff." variousfct.proquest(askquestions) shutil.rmtree(lookbackdir) if not os.path.isdir(lookbackdir): os.mkdir(lookbackdir) db = KirbyBase()
# Instead of 3, I simply move back or change wisely the config files previously backuped: execfile(os.path.join(configdir, 'deconv_config_update.py')) allimages = db.select(imgdb, [deckeyfilenum], ['\d\d*'], returnType='dict', useRegExp=True, sortFields=[deckeyfilenum]) refimage = [ image for image in allimages if image['imgname'] == refimgname ][0] allimages.insert(0, refimage.copy()) images[0][deckeyfilenum] = mcsname(1) nbimg = len(allimages) ptsrc = star.readmancat(ptsrccat) nbptsrc = len(ptsrc) # alter in.txt in_backuped = open( os.path.join(workdir, 'updating_' + os.path.basename(inpath)), 'r').readlines() new_in_backuped = open(inpath, 'w') toreplace = [] for ind, line in enumerate(in_backuped): if line[0] not in ['|', '-'] and in_backuped[ind - 1][0] == '-': indstart = ind for i in np.arange(len(in_backuped))[ind:]: if in_backuped[i][0] == '-':
else: images = db.select(imgdb, ['gogogo', 'treatme'], [True, True], returnType='dict') nbrofimages = len(images) # we prepare the database if "nbrcoeffstars" not in db.getFieldNames(imgdb): print "I will add some fields to the database." proquest(askquestions) db.addFields(imgdb, [ 'nbrcoeffstars:int', 'maxcoeffstars:int', 'medcoeff:float', 'sigcoeff:float', 'spancoeff:float' ]) # we read the handwritten star catalog normstars = star.readmancat(normstarscat) for s in normstars: print s.name print "Checking reference image ..." refsexcat = os.path.join(alidir, refimgname + ".alicat") refcatstars = star.readsexcat(refsexcat, propfields=[ "FLUX_APER", "FLUX_APER1", "FLUX_APER2", "FLUX_APER3", "FLUX_APER4" ]) id = star.listidentify(normstars, refcatstars, tolerance=identtolerance) refidentstars = id["match"] # now refidentstars contains the same stars as normstars, but with sex fluxes. ''' print "="*25