Example #1
0
    def line_process(self,
                     skymode='internal',
                     refpath='./esocombine/',
                     skymask=None,
                     lmin=4900,
                     lmax=9000,
                     deepwhite=None):
        """

        Produces final cubes optimised for fields that are relatively 
        empty in continuum sources but that may have very extended 
        emission lines.

        lmin -> the minimum wavelength to consider
        lmax -> the maximum wavelength to consider
        refpath -> where the reference cubes for wcs resempling are 
        skymask -> a skymask to be used for identify good regions for skysubtraction 
                   (expected in image coordinates)
        skymode -> internal: use good pixels (i.e. not containing sources or defined in skymask) to perform 
                   skysubtraction plus run ZAP on it.
        deepwhite -> if set to an image, this is used to mask sources during sky subtraction.
                     otherwise the cube itself is used 
                   
        """

        import os
        import glob
        import subprocess
        import muse_redux_line as ex

        #first, list how many OBs are there
        listob = glob.glob('OB*')
        listob.sort()
        nobs = len(listob)
        print('Process {} OBs'.format(nobs))

        #now make space as needed for final products
        if not os.path.exists('linecombine'):
            os.makedirs('linecombine')

        #rerun pipe enabling resampling on final ESO cube
        ex.individual_resample(listob, refpath=refpath)

        #next construct the ifu mask for each exposure
        ex.make_ifumasks(listob, refpath=refpath)

        #compute illumination correction
        ex.make_illcorr(listob)

        #now do background subtraction
        if ('internal' in skymode):
            ex.internalskysub(listob, skymask, deepwhite=deepwhite)
        else:
            print("Sky subtraction mode {} not supported".format(skymode))

        #change dir
        currdir = os.getcwd()
        os.chdir('linecombine')
        print('Changing dir to linecombine...')

        fl1 = open('cubes.lst', 'w')
        fl2 = open('masks.lst', 'w')

        #loop over OBs
        for oob in range(nobs):
            #count how many science exposures
            nsci = len(
                glob.glob("../{}/Proc/OBJECT_RED_0*.fits*".format(
                    listob[oob])))
            #reconstruct names
            for ll in range(nsci):
                fl1.write(
                    '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_zapsky.fits\n'.
                    format(listob[oob], ll + 1))
                fl2.write('../{}/Proc/MASK_EXP{}_ILLCORR_edges.fits\n'.format(
                    listob[oob], ll + 1))
        fl1.close()
        fl2.close()

        #make the temp combine
        ex.combine_cubes("cubes.lst", "masks.lst")
        os.chdir(currdir)

        print("All done!")
Example #2
0
    def cubex_process(self, refpath='./esocombine/', skymask=None):
        """
  
        Take individual cubes and process them with CubEx to tweak the illumination and perform sky subtraction
        CubEx is a privite code by S. Cantalupo and cannot be redistributed. Contact the author directly. 
        
        refpath -> where the reference cubes for wcs resempling are 
        
        skymask -> mask this region before running cubesharp (ds9 region in image units)

        """

        import os
        import glob
        import subprocess
        import muse_redux_line as ex
        import muse_redux_cubex as cx
        import multiprocessing
        import numpy as np

        #first, list how many OBs are there
        listob = glob.glob('OB*')
        listob.sort()
        nobs = len(listob)
        print('Process {} OBs'.format(nobs))

        #now make space as needed for final products
        if not os.path.exists('cubexcombine'):
            os.makedirs('cubexcombine')

        #rerun pipeline enabling resampling on final ESO cube using modules coded for line_process
        ex.individual_resample(listob, refpath=refpath)

        #now do the first two passes of cubex on each OB to prepare a temporary cube
        cx.cubex_driver(listob, skymask=skymask)

        #prepare for intermediate combine
        #dump to disk file lists
        topdir = os.getcwd()
        os.chdir('cubexcombine')

        fl1 = open('cubes.lst', 'w')
        fl2 = open('masks.lst', 'w')

        #loop over OBs
        for oob in range(nobs):
            #count how many science exposures
            nsci = len(
                glob.glob("../{}/Proc/OBJECT_RED_0*.fits*".format(
                    listob[oob])))
            #reconstruct names
            for ll in range(nsci):
                fl1.write(
                    '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_skysub2.fits\n'.
                    format(listob[oob], ll + 1))
                fl2.write(
                    '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_fix2_SliceEdgeMask.fits\n'
                    .format(listob[oob], ll + 1))
        fl1.close()
        fl2.close()

        #make the temp combine
        cx.combine_cubes("cubes.lst", "masks.lst")

        #now make two independent halves
        fl1cube = open('cubes_half1.lst', 'w')
        fl1mask = open('masks_half1.lst', 'w')
        fl2cube = open('cubes_half2.lst', 'w')
        fl2mask = open('masks_half2.lst', 'w')

        #loop over OBs
        counter = 0
        for oob in range(nobs):
            #count how many science exposures
            nsci = len(
                glob.glob("../{}/Proc/OBJECT_RED_0*.fits*".format(
                    listob[oob])))
            #reconstruct names
            for ll in range(nsci):
                counter = counter + 1
                if (counter % 2 == 0):
                    fl1cube.write(
                        '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_skysub2.fits\n'
                        .format(listob[oob], ll + 1))
                    fl1mask.write(
                        '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_fix2_SliceEdgeMask.fits\n'
                        .format(listob[oob], ll + 1))
                else:
                    fl2cube.write(
                        '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_skysub2.fits\n'
                        .format(listob[oob], ll + 1))
                    fl2mask.write(
                        '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_fix2_SliceEdgeMask.fits\n'
                        .format(listob[oob], ll + 1))

        #close files
        fl1cube.close()
        fl1mask.close()
        fl2cube.close()
        fl2mask.close()

        #now combine
        cx.combine_cubes("cubes_half1.lst", "masks_half1.lst", halfset='half1')
        cx.combine_cubes("cubes_half2.lst", "masks_half2.lst", halfset='half2')

        #back to top
        os.chdir(topdir)

        #now do the final pass of cubex using the tmp combined cube for better masking
        cx.cubex_driver(listob,
                        last=True,
                        highsn='../../cubexcombine/COMBINED_CUBE.fits',
                        skymask=skymask)

        #make the final combined cube
        #dump to disk file lists
        topdir = os.getcwd()
        os.chdir('cubexcombine')

        fl1 = open('cubes_final.lst', 'w')
        fl2 = open('masks_final.lst', 'w')

        #loop over OBs
        for oob in range(nobs):
            #count how many science exposures
            nsci = len(
                glob.glob("../{}/Proc/OBJECT_RED_0*.fits*".format(
                    listob[oob])))
            #reconstruct names
            for ll in range(nsci):
                fl1.write(
                    '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_skysubhsn.fits\n'.
                    format(listob[oob], ll + 1))
                fl2.write(
                    '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_fixhsn_SliceEdgeMask.fits\n'
                    .format(listob[oob], ll + 1))
        fl1.close()
        fl2.close()

        #make the temp combine
        cx.combine_cubes("cubes_final.lst", "masks_final.lst", final=True)

        #now make two independent halves
        fl1cube = open('cubes_final_half1.lst', 'w')
        fl1mask = open('masks_final_half1.lst', 'w')
        fl2cube = open('cubes_final_half2.lst', 'w')
        fl2mask = open('masks_final_half2.lst', 'w')

        #loop over OBs
        counter = 0
        for oob in range(nobs):
            #count how many science exposures
            nsci = len(
                glob.glob("../{}/Proc/OBJECT_RED_0*.fits*".format(
                    listob[oob])))
            #reconstruct names
            for ll in range(nsci):
                counter = counter + 1
                if (counter % 2 == 0):
                    fl1cube.write(
                        '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_skysubhsn.fits\n'
                        .format(listob[oob], ll + 1))
                    fl1mask.write(
                        '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_fixhsn_SliceEdgeMask.fits\n'
                        .format(listob[oob], ll + 1))
                else:
                    fl2cube.write(
                        '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_skysubhsn.fits\n'
                        .format(listob[oob], ll + 1))
                    fl2mask.write(
                        '../{}/Proc/DATACUBE_FINAL_LINEWCS_EXP{}_fixhsn_SliceEdgeMask.fits\n'
                        .format(listob[oob], ll + 1))

        #close files
        fl1cube.close()
        fl1mask.close()
        fl2cube.close()
        fl2mask.close()

        #now combine
        cx.combine_cubes("cubes_final_half1.lst",
                         "masks_final_half1.lst",
                         halfsetfinal='half1')
        cx.combine_cubes("cubes_final_half2.lst",
                         "masks_final_half2.lst",
                         halfsetfinal='half2')

        #now run quality checks on final redux products
        #Typically one uses first pass, so check those
        cx.dataquality("cubes.lst", "masks.lst")

        #back to top level
        os.chdir(topdir)
        print 'All done with cubex redux'