Exemplo n.º 1
0
def imdiff(A, B, maskname, band, header, options):
    s = "[0]"

    targname = A[1]["targname"].rstrip(" ")
    if targname == "":
        objname = A[1]["object"].replace(" ", "_")
    else:
        objname = targname.replace(" ", "_")

    operand1 = A[0] + '[0]'
    operand2 = B[0] + '[0]'

    imnumA = A[0].split('_')[-1].rstrip(".fits")
    imnumB = B[0].split('_')[-1].rstrip(".fits")

    dname = "{0}_{1}_{2}_{3}-{4}_{5}-{6}.fits".format(maskname, objname, band,
                                                      A[1]["frameid"],
                                                      B[1]["frameid"], imnumA,
                                                      imnumB)

    try:
        os.remove(dname)
    except:
        pass
    print "Data Diff {0}-{1}".format(operand1, operand2)
    IO.imarith(operand1, '-', operand2, dname)
    ''' Now handle variance '''
    numreads = header["READS0"]
    RN_adu = Detector.RN / np.sqrt(numreads) / Detector.gain
    varname = "var_{0}_{1}_{2}_{3}+{4}_{5}+{6}.fits".format(
        maskname, objname, band, A[1]["frameid"], B[1]["frameid"], imnumA,
        imnumB)

    print "Var Sum {0}+{1}".format(operand1, operand2)
    IO.imarith(operand1, '+', operand2, "tmp_" + varname)
    try:
        os.remove(varname)
    except:
        pass
    print "Var add RN {0}+{1}".format(operand1, RN_adu**2)
    IO.imarith("tmp_" + varname, '+', RN_adu**2, varname)

    try:
        os.remove("tmp_" + varname)
    except:
        pass

    return dname, varname
def imdiff(A, B, maskname, band, header, options):
    s = "[0]"

    targname = A[1]["targname"].rstrip(" ")
    if targname == "":
        objname = A[1]["object"].replace(" ", "_")
    else:
        objname = targname.replace(" ", "_")

    operand1 = A[0] + '[0]'
    operand2 = B[0] + '[0]'

    imnumA = A[0].split('_')[-1].rstrip(".fits")
    imnumB = B[0].split('_')[-1].rstrip(".fits")

    dname = "{0}_{1}_{2}_{3}-{4}_{5}-{6}.fits".format(maskname, objname, band,
        A[1]["frameid"], B[1]["frameid"], imnumA, imnumB)

    try: os.remove(dname)
    except:pass
    print "Data Diff {0}-{1}".format(operand1,operand2)
    IO.imarith(operand1, '-', operand2, dname)

    ''' Now handle variance '''
    numreads = header["READS0"]
    RN_adu = Detector.RN / np.sqrt(numreads) / Detector.gain
    varname = "var_{0}_{1}_{2}_{3}+{4}_{5}+{6}.fits".format(maskname, objname, band,
        A[1]["frameid"], B[1]["frameid"], imnumA, imnumB)

    
    print "Var Sum {0}+{1}".format(operand1,operand2)
    IO.imarith(operand1, '+', operand2, "tmp_" + varname)
    try: os.remove(varname)
    except: pass
    print "Var add RN {0}+{1}".format(operand1,RN_adu**2)
    IO.imarith("tmp_" + varname, '+', RN_adu**2, varname)

    try: os.remove("tmp_" + varname)
    except: pass


    return dname, varname
Exemplo n.º 3
0
def handle_flats(flatlist,
                 maskname,
                 band,
                 options,
                 extension=None,
                 edgeThreshold=450,
                 lampOffList=None,
                 longslit=None):
    '''
    handle_flats is the primary entry point to the Flats module.

    handle_flats takes a list of individual exposure FITS files and creates:
    1. A CRR, dark subtracted, pixel-response flat file.
    2. A set of polynomials that mark the edges of a slit

    Inputs:
    flatlist: 
    maskname: The name of a mask
    band: A string indicating the bandceil

    Outputs:

    file {maskname}/flat_2d_{band}.fits -- pixel response flat
    file {maskname}/edges.np
    '''

    tick = time.time()

    # Check
    bpos = np.ones(92) * -1

    #Retrieve the list of files to use for flat creation.
    flatlist = IO.list_file_to_strings(flatlist)
    if len(flatlist) == 0:
        print('WARNING: No flat files found.')
        raise IOError('No flat files found')
    # Print the filenames to Standard-out
    for flat in flatlist:
        debug(str(flat))

    #Determine if flat files headers are in agreement
    for fname in flatlist:

        hdr, dat, bs = IO.readmosfits(fname, options, extension=extension)
        try:
            bs0
        except:
            bs0 = bs

        if np.any(bs0.pos != bs.pos):
            print "bs0: " + str(bs0.pos) + " bs: " + str(bs.pos)
            error("Barset do not seem to match")
            raise Exception("Barsets do not seem to match")

        if hdr["filter"] != band:
            error("Filter name %s does not match header filter name "
                  "%s in file %s" % (band, hdr["filter"], fname))
            raise Exception("Filter name %s does not match header filter name "
                            "%s in file %s" % (band, hdr["filter"], fname))
        for i in xrange(len(bpos)):
            b = hdr["B{0:02d}POS".format(i + 1)]
            if bpos[i] == -1:
                bpos[i] = b
            else:
                if bpos[i] != b:
                    error("Bar positions are not all the same in "
                          "this set of flat files")
                    raise Exception("Bar positions are not all the same in "
                                    "this set of flat files")
    bs = bs0

    # Imcombine the lamps ON flats
    info("Attempting to combine files in {}".format(flatlist))
    out = os.path.join("combflat_2d_{:s}.fits".format(band))
    IO.imcombine(flatlist, out, options, reject="minmax", nlow=1, nhigh=1)

    # Imcombine the lamps OFF flats and subtract the off from the On sets
    if lampOffList != None:
        #Retrieve the list of files to use for flat creation.
        info("Attempting to combine Lamps off files in {}".format(lampOffList))
        lampOffList = IO.list_file_to_strings(lampOffList)
        for flat in lampOffList:
            debug(str(flat))
        out = os.path.join("combflat_lamps_off_2d_{:s}.fits".format(band))
        IO.imcombine(flatlist, out, options, reject="minmax", nlow=1, nhigh=1)
        file_on = os.path.join("combflat_2d_{:s}.fits".format(band))
        file_off = os.path.join("combflat_lamps_off_2d_{:s}.fits".format(band))
        file_on_save = os.path.join(
            "combflat_lamps_on_2d_{:s}.fits".format(band))
        IO.imarith(file_on, '-', file_off, file_on_save)

    debug("Combined '%s' to '%s'" % (flatlist, maskname))
    #     info("Combined flats for '%s'" % (maskname))
    path = "combflat_2d_%s.fits" % band
    (header, data) = IO.readfits(path, use_bpm=True)
    info("Flat written to %s" % path)

    # Edge Trace
    if bs.long_slit:
        info("Long slit mode recognized")
        info("Central row position:   " + str(longslit["row_position"]))
        info("Upper and lower limits: " + str(longslit["yrange"][0]) + " " +
             str(longslit["yrange"][1]))
        results = find_longslit_edges(data,
                                      header,
                                      bs,
                                      options,
                                      edgeThreshold=edgeThreshold,
                                      longslit=longslit)
    elif bs.long2pos_slit:
        info("Long2pos mode recognized")
        results = find_long2pos_edges(data,
                                      header,
                                      bs,
                                      options,
                                      edgeThreshold=edgeThreshold,
                                      longslit=longslit)
    else:
        info('Finding slit edges in {}'.format(path))
        results = find_and_fit_edges(data,
                                     header,
                                     bs,
                                     options,
                                     edgeThreshold=edgeThreshold)
    results[-1]["maskname"] = maskname
    results[-1]["band"] = band
    np.save("slit-edges_{0}".format(band), results)
    save_ds9_edges(results, options)

    # Generate Flat
    out = "pixelflat_2d_%s.fits" % (band)
    if lampOffList != None:
        make_pixel_flat(data, results, options, out, flatlist, lampsOff=True)
    else:
        make_pixel_flat(data, results, options, out, flatlist, lampsOff=False)

    info("Pixel flat took {0:6.4} s".format(time.time() - tick))
def handle_flats(flatlist, maskname, band, options, extension=None,edgeThreshold=450,lampOffList=None,longslit=None):
    '''
    handle_flats is the primary entry point to the Flats module.

    handle_flats takes a list of individual exposure FITS files and creates:
    1. A CRR, dark subtracted, pixel-response flat file.
    2. A set of polynomials that mark the edges of a slit

    Inputs:
    flatlist: 
    maskname: The name of a mask
    band: A string indicating the bandceil

    Outputs:

    file {maskname}/flat_2d_{band}.fits -- pixel response flat
    file {maskname}/edges.np
    '''

    tick = time.time()

    # Check
    bpos = np.ones(92) * -1

    #Retrieve the list of files to use for flat creation.
    flatlist = IO.list_file_to_strings(flatlist)
    if len(flatlist) == 0:
        print('WARNING: No flat files found.')
        raise IOError('No flat files found')
    # Print the filenames to Standard-out
    for flat in flatlist:
        debug(str(flat))

    #Determine if flat files headers are in agreement
    for fname in flatlist:

        hdr, dat, bs = IO.readmosfits(fname, options, extension=extension)
        try: bs0
        except: bs0 = bs

        if np.any(bs0.pos != bs.pos):
            print("bs0: "+str(bs0.pos)+" bs: "+str(bs.pos))
            error("Barset do not seem to match")
            raise Exception("Barsets do not seem to match")

        if hdr["filter"] != band:
            error ("Filter name %s does not match header filter name "
                    "%s in file %s" % (band, hdr["filter"], fname))
            raise Exception("Filter name %s does not match header filter name "
                    "%s in file %s" % (band, hdr["filter"], fname))
        for i in range(len(bpos)):
            b = hdr["B{0:02d}POS".format(i+1)]
            if bpos[i] == -1:
                bpos[i] = b
            else:
                if bpos[i] != b:
                    error("Bar positions are not all the same in "
                            "this set of flat files")
                    raise Exception("Bar positions are not all the same in "
                            "this set of flat files")
    bs = bs0

    # Imcombine the lamps ON flats
    info("Attempting to combine files in {}".format(flatlist))
    out = os.path.join("combflat_2d_{:s}.fits".format(band))
    IO.imcombine(flatlist, out, options, reject="minmax", nlow=1, nhigh=1)

    # Imcombine the lamps OFF flats and subtract the off from the On sets
    if lampOffList != None: 
        #Retrieve the list of files to use for flat creation. 
        info("Attempting to combine Lamps off files in {}".format(lampOffList))
        lampOffList = IO.list_file_to_strings(lampOffList)
        for flat in lampOffList:
            debug(str(flat))
        out = os.path.join("combflat_lamps_off_2d_{:s}.fits".format(band))
        IO.imcombine(flatlist, out, options, reject="minmax", nlow=1, nhigh=1)
        file_on = os.path.join("combflat_2d_{:s}.fits".format(band))
        file_off = os.path.join("combflat_lamps_off_2d_{:s}.fits".format(band))
        file_on_save = os.path.join("combflat_lamps_on_2d_{:s}.fits".format(band))
        IO.imarith(file_on, '-', file_off, file_on_save)

    debug("Combined '%s' to '%s'" % (flatlist, maskname))
#     info("Combined flats for '%s'" % (maskname))
    path = "combflat_2d_%s.fits" % band
    (header, data) = IO.readfits(path, use_bpm=True)
    info("Flat written to %s" % path)

    # Edge Trace
    if bs.long_slit:
        info( "Long slit mode recognized")
        info( "Central row position:   "+str(longslit["row_position"]))
        info( "Upper and lower limits: "+str(longslit["yrange"][0])+" "+str(longslit["yrange"][1]))
        results = find_longslit_edges(data,header, bs, options, edgeThreshold=edgeThreshold, longslit=longslit)
    elif bs.long2pos_slit:
        info( "Long2pos mode recognized")
        results = find_long2pos_edges(data,header, bs, options, edgeThreshold=edgeThreshold, longslit=longslit)
    else:
        info('Finding slit edges in {}'.format(path))
        results = find_and_fit_edges(data, header, bs, options,edgeThreshold=edgeThreshold)
    results[-1]["maskname"] = maskname
    results[-1]["band"] = band
    np.save("slit-edges_{0}".format(band), results)
    save_ds9_edges(results, options)

    # Generate Flat
    out = "pixelflat_2d_%s.fits" % (band)
    if lampOffList != None: 
         make_pixel_flat(data, results, options, out, flatlist, lampsOff=True)
    else:
         make_pixel_flat(data, results, options, out, flatlist, lampsOff=False)

    info( "Pixel flat took {0:6.4} s".format(time.time()-tick))