Exemple #1
0
def reduce_science(args):
    for spec in args.specid:
        spec_ind_sci = np.where(args.sci_df['Specid'] == spec)[0]
        for amp in config.Amps:
            amp_ind_sci = np.where(args.sci_df['Amp'] == amp)[0]
            sci_sel = np.intersect1d(spec_ind_sci, amp_ind_sci) 
            for ind in sci_sel:
                if args.instr == "virus":
                    if not args.use_trace_ref:
                        ifucen = np.loadtxt(op.join(args.configdir, 
                                                    'IFUcen_files', 
                                                    args.ifucen_fn[amp][0]
                                                    + args.sci_df['Ifuid'][ind] 
                                                    + '.txt'), 
                                                    usecols=[0,1,2,4], 
                                               skiprows=args.ifucen_fn[amp][1])
                        
                    else:
                        if args.sci_df['Ifuid'][ind] == '004':
                            ifucen = np.loadtxt(op.join(args.configdir,
                                                    'IFUcen_files',
                                                    'IFUcen_HETDEX_reverse_R.txt'),
                                                    usecols=[0,1,2,4],
                                               skiprows=args.ifucen_fn[amp][1])
                            ifucen[224:,:] = ifucen[-1:223:-1,:]
                        else:
                            ifucen = np.loadtxt(op.join(args.configdir,
                                                    'IFUcen_files',
                                                    'IFUcen_HETDEX.txt'),
                                                    usecols=[0,1,2,4],
                                               skiprows=args.ifucen_fn[amp][1])
                else:
                    ifucen = np.loadtxt(op.join(args.configdir, 'IFUcen_files', 
                                        args.ifucen_fn[amp][0]), 
                              usecols=[0,1,2], skiprows=args.ifucen_fn[amp][1])
                if args.debug:
                    print("Working on Sci for %s, %s" %(spec, amp)) 
                if args.check_if_twi_exists:
                    fn = op.join(args.twi_dir,'fiber_*_%s_%s_%s_%s.pkl' %(spec, 
                                                   args.sci_df['Ifuslot'][ind],
                                                     args.sci_df['Ifuid'][ind],
                                                                          amp))
                    calfiles = glob.glob(fn)
                    if not calfiles:
                        print("No cals found for %s,%s: %s"
                              %(spec, amp, args.sci_df['Files'][ind]))
                        print("If you want to produce cals include "
                              "--reduce_twi")
                  
                sci1 = Amplifier(args.sci_df['Files'][ind],
                                 args.sci_df['Output'][ind],
                                 calpath=args.twi_dir, skypath=args.sky_dir,
                                 debug=False, refit=False, 
                                 dark_mult=args.dark_mult[amp],
                                 darkpath=args.darkdir, biaspath=args.biasdir,
                                 virusconfig=args.configdir, 
                                 specname=args.specname[amp],
                                 use_pixelflat=(args.pixelflats<1),
                                 use_trace_ref=args.use_trace_ref,
                                 calculate_shift=args.adjust_trace,
                                 fiber_date=args.fiber_date,
                                 cont_smooth=args.cont_smooth)
                #sci1.load_fibers()
                #if sci1.fibers and not args.start_from_scratch:
                #    if sci1.fibers[0].spectrum is not None:
                #        sci1.prepare_image()
                #        sci1.sky_subtraction()
                #        sci1.clean_cosmics()
                #else:
                sci1.load_all_cal()
                if args.adjust_trace:
                    sci1.refit=True
                    sci1.get_trace()
                    sci1.refit=False
                sci1.fiberextract()
                if args.refit_fiber_to_fiber:
                    sci1.refit=True
                    sci1.get_fiber_to_fiber()
                    sci1.refit=False
                sci1.sky_subtraction()
                sci1.clean_cosmics()
                sci1.fiberextract()
                sci1.sky_subtraction()
                sci2 = Amplifier(args.sci_df['Files'][ind].replace(amp, 
                                                      config.Amp_dict[amp][0]),
                                 args.sci_df['Output'][ind],
                                 calpath=args.twi_dir, skypath=args.sky_dir, 
                                 debug=False, refit=False, 
                             dark_mult=args.dark_mult[config.Amp_dict[amp][0]],
                                 darkpath=args.darkdir, biaspath=args.biasdir,
                                 virusconfig=args.configdir, 
                                 specname=args.specname[amp],
                                 use_pixelflat=(args.pixelflats<1),
                                 use_trace_ref=args.use_trace_ref,
                                 calculate_shift=args.adjust_trace,
                                 fiber_date=args.fiber_date,
                                 cont_smooth=args.cont_smooth)
                #sci2.load_fibers()
                #if sci2.fibers and not args.start_from_scratch:
                #    if sci2.fibers[0].spectrum is not None:
                #        sci2.prepare_image()
                #        sci2.sky_subtraction()
                #        sci2.clean_cosmics()
                #else:
                sci2.load_all_cal()
                if args.adjust_trace:
                    sci2.refit=True
                    sci2.get_trace()
                    sci2.refit=False
                sci2.fiberextract()
                if args.refit_fiber_to_fiber:
                    sci2.refit=True
                    sci2.get_fiber_to_fiber()
                    sci2.refit=False
                sci2.sky_subtraction()
                sci2.clean_cosmics()
                sci2.fiberextract()
                sci2.sky_subtraction()
                outname = op.join(args.sci_df['Output'][ind],
                                  'S%s_%s_sci_%s.fits' %(
                          op.basename(args.sci_df['Files'][ind]).split('_')[0],
                                                   args.sci_df['Ifuslot'][ind], 
                                                      config.Amp_dict[amp][1]))
                make_spectrograph_image(sci1.clean_image, sci2.clean_image, 
                                        sci1.header, outname)
                make_spectrograph_image(sci1.error, sci2.error, 
                                        sci1.header, op.join(op.dirname(outname), 'ee.'+op.basename(outname)))                   
                make_error_frame(sci1.clean_image, sci2.clean_image, sci1.mask,
                                 sci2.mask, sci1.header, outname)
                outname = op.join(args.sci_df['Output'][ind],
                                  'cS%s_%s_sci_%s.fits' %(
                          op.basename(args.sci_df['Files'][ind]).split('_')[0],
                                                   args.sci_df['Ifuslot'][ind], 
                                                      config.Amp_dict[amp][1]))
                make_spectrograph_image(np.where(sci1.mask==0, 
                                                 sci1.clean_image, 0.0),
                                        np.where(sci2.mask==0, 
                                                 sci2.clean_image, 0.0),
                                        sci1.header, outname)
                make_error_frame(sci1.clean_image, sci2.clean_image, sci1.mask,
                                 sci2.mask, sci1.header, outname)
                outname = op.join(args.sci_df['Output'][ind],
                                  'CsS%s_%s_sci_%s.fits' %(
                          op.basename(args.sci_df['Files'][ind]).split('_')[0],
                                                   args.sci_df['Ifuslot'][ind], 
                                                      config.Amp_dict[amp][1]))
                make_spectrograph_image(sci1.continuum_sub, sci2.continuum_sub, 
                                        sci1.header, outname)
                make_error_frame(sci1.continuum_sub, sci2.continuum_sub, 
                                 sci1.mask, sci2.mask, sci1.header, outname)
                outname = op.join(args.sci_df['Output'][ind],
                                  'cCsS%s_%s_sci_%s.fits' %(
                          op.basename(args.sci_df['Files'][ind]).split('_')[0],
                                                   args.sci_df['Ifuslot'][ind], 
                                                      config.Amp_dict[amp][1]))
                make_spectrograph_image(np.where(sci1.mask==0, 
                                                 sci1.continuum_sub, 0.0),
                                        np.where(sci2.mask==0, 
                                                 sci2.continuum_sub, 0.0),
                                        sci1.header, outname)
                make_error_frame(sci1.continuum_sub, sci2.continuum_sub,
                                 sci1.mask, sci2.mask, sci1.header, outname)
                outname = op.join(args.sci_df['Output'][ind],
                                  'cCsS%s_%s_sci_%s_imstat.png' %(
                          op.basename(args.sci_df['Files'][ind]).split('_')[0],
                                                   args.sci_df['Ifuslot'][ind], 
                                                      config.Amp_dict[amp][1]))
                imstat(sci1.residual, sci2.residual, sci1.fibers,
                       sci2.fibers, outname)
                Fe, FeS = recreate_fiberextract(sci1, sci2, 
                                                wavelim=args.wvl_dict[amp], 
                                                disp=args.disp[amp])
                outname = op.join(args.sci_df['Output'][ind],
                                  'Fe%s_%s_sci_%s.fits' %(
                          op.basename(args.sci_df['Files'][ind]).split('_')[0],
                                                   args.sci_df['Ifuslot'][ind], 
                                                      config.Amp_dict[amp][1]))
                make_fiber_image(Fe, sci1.header, outname, args, amp)
                make_fiber_error(Fe, sci1.header, outname, args, amp)
                make_cube_file(args, outname, ifucen, args.cube_scale, 
                               config.Amp_dict[amp][1])
                outname = op.join(args.sci_df['Output'][ind],
                                  'FeS%s_%s_sci_%s.fits' %(
                          op.basename(args.sci_df['Files'][ind]).split('_')[0],
                                                   args.sci_df['Ifuslot'][ind], 
                                                      config.Amp_dict[amp][1]))
                make_fiber_image(FeS, sci1.header, outname, args, amp)
                make_fiber_error(FeS, sci1.header, outname, args, amp)
                make_cube_file(args, outname, ifucen, args.cube_scale, 
                               config.Amp_dict[amp][1])
                if args.save_sci_fibers:
                    sci1.save_fibers()
                    sci2.save_fibers()
                if args.save_sci_amplifier:
                    sci1.save()
                    sci2.save()
                if args.debug:
                    print("Finished working on Sci for %s, %s" %(spec, amp))                    
Exemple #2
0
def custom(args):
    lowfib = int(112 / 4. - 1.)
    midfib = int(112 / 2. - 1.)
    highfib = int(3.* 112. / 4. - 1.)
    trace_list = {"LL":[],"LU":[],"RU":[],"RL":[]}
    amps = {"LL":"LL","LU":"LL","RU":"RU","RL":"RU"}
    for spec in args.specid:
        spec_ind_twi = np.where(args.twi_df['Specid'] == spec)[0]
        spec_ind_sci = np.where(args.sci_df['Specid'] == spec)[0]
        for ind in spec_ind_twi:
            amp = args.twi_df['Amp'][ind]
            AMP = amps[amp]
            twi = Amplifier(args.twi_df['Files'][ind],
                            args.twi_df['Output'][ind],
                            calpath=args.twi_df['Output'][ind], 
                            debug=True, dark_mult=0.0,
                            darkpath=args.darkdir, biaspath=args.biasdir,
                            virusconfig=args.configdir, 
                            specname=args.specname[AMP],
                            use_pixelflat=(args.pixelflats<1),
                            init_lims=args.wvl_dict[AMP], 
                            check_fibermodel=True, check_wave=True,
                            fsize=args.fsize, 
                            fibmodel_nbins=args.fibmodel_bins,
                            sigma=args.fibmodel_sig,
                            power=args.fibmodel_pow,
                            use_trace_ref=args.use_trace_ref)
               
            twi.load_fibers()
            if len(twi.fibers)==0:
                twi.get_trace()
            else:
                if not hasattr(twi.fibers[0],'trace'):        
                    twi.get_trace()
            blue = int(twi.D /4.)
            green = int(twi.D /2.)
            red = int(3.*twi.D /4.)
            trace_list[amp].append(np.array([twi.fibers[lowfib].trace[blue],
                                            twi.fibers[lowfib].trace[green],
                                            twi.fibers[lowfib].trace[red],
                                            twi.fibers[midfib].trace[blue],
                                            twi.fibers[midfib].trace[green],
                                            twi.fibers[midfib].trace[red],
                                            twi.fibers[highfib].trace[blue],
                                            twi.fibers[highfib].trace[green],
                                            twi.fibers[highfib].trace[red]]))
        for ind in spec_ind_sci:
            amp = args.sci_df['Amp'][ind]
            AMP = amps[amp]
            print(args.sci_df['Files'][ind])
            
            sci = Amplifier(args.sci_df['Files'][ind],
                            args.sci_df['Output'][ind],
                            calpath=args.twi_dir, skypath=args.sky_dir,
                            debug=False, refit=True, 
                            dark_mult=args.dark_mult[AMP],
                            darkpath=args.darkdir, biaspath=args.biasdir,
                            virusconfig=args.configdir, 
                            specname=args.specname[AMP],
                            use_pixelflat=(args.pixelflats<1),
                            use_trace_ref=args.use_trace_ref,
                            calculate_shift=False)
            sci.load_fibers()
            if len(sci.fibers)==0:
                sci.get_trace()
            else:
                if not hasattr(sci.fibers[0],'trace'):        
                    sci.get_trace()
            blue = int(sci.D /4.)
            green = int(sci.D /2.)
            red = int(3.*sci.D /4.)
            trace_list[amp].append(np.array([sci.fibers[lowfib].trace[blue],
                                            sci.fibers[lowfib].trace[green],
                                            sci.fibers[lowfib].trace[red],
                                            sci.fibers[midfib].trace[blue],
                                            sci.fibers[midfib].trace[green],
                                            sci.fibers[midfib].trace[red],
                                            sci.fibers[highfib].trace[blue],
                                            sci.fibers[highfib].trace[green],
                                            sci.fibers[highfib].trace[red]]))
    import matplotlib.pyplot as plt
    plt.figure(figsize=(12,12))
    ax1 = plt.axes([0.1,0.1,0.35,0.35])
    ax2 = plt.axes([0.1,0.55,0.35,0.35])
    ax3 = plt.axes([0.55,0.1,0.35,0.35])
    ax4 = plt.axes([0.55,0.55,0.35,0.35])
    amps = ["LL","LU","RU","RL"]
    ax = [ax1,ax2,ax3,ax4]
    for i,amp in enumerate(amps):
        TR = np.array(trace_list[amp])
        avg = biweight_location(TR,axis=(0,))
        print(TR-avg)
        ax[i].plot(TR-avg)
    fn = op.join(args.output, args.scidir_date[0], args.instr, 
                        'trace_%s.png' %args.specid[0])
    plt.savefig(fn,dpi=150)
def main():
    args = parse_args()
    for spec in args.specid:
        spec_ind_twi = np.where(args.twi_df['Specid'] == spec)[0]
        for amp in config.Amps:
            amp_ind_twi = np.where(args.twi_df['Amp'] == amp)[0]
            twi_sel = np.intersect1d(spec_ind_twi, amp_ind_twi)
            for ind in twi_sel:
                if args.debug:
                    print("Working on Cal for %s, %s" % (spec, amp))
                twi1 = Amplifier(args.twi_df['Files'][ind],
                                 args.twi_df['Output'][ind],
                                 calpath=args.twi_df['Output'][ind],
                                 debug=True,
                                 refit=True,
                                 dark_mult=0.0,
                                 darkpath=args.darkdir,
                                 biaspath=args.biasdir,
                                 virusconfig=args.configdir,
                                 specname=args.specname[amp],
                                 use_pixelflat=(args.pixelflats < 1),
                                 init_lims=args.wvl_dict[amp],
                                 check_fibermodel=True,
                                 check_wave=True,
                                 fsize=args.fsize,
                                 fibmodel_nbins=args.fibmodel_bins,
                                 sigma=args.fibmodel_sig,
                                 power=args.fibmodel_pow)
                twi1.get_trace()
                fname = op.join(
                    args.configdir, 'Fiber_Locations', args.twidir_date[0],
                    'fiber_loc_%s_%s_%s_%s.txt' %
                    (args.twi_df['Specid'][ind], args.twi_df['Ifuslot'][ind],
                     args.twi_df['Ifuid'][ind], amp))
                if len(twi1.fibers) == args.nfibers[amp]:
                    print("The correct number of fibers, %i, were found." %
                          args.nfibers[amp])
                    Y = np.zeros((len(twi1.fibers), 2))
                    col = int(twi1.D * config.frac)
                    for i, fiber in enumerate(twi1.fibers):
                        Y[i, 0] = fiber.trace[col]
                    print("Printing file for fibers.")
                    create_fiber_file(fname, config.frac, Y)
                else:
                    print("Only found %i fibers instead of the expected %i" %
                          (len(twi1.fibers), args.nfibers[amp]))
                    Y = find_missing_fibers(config.frac, twi1)
                    create_fiber_file(fname, config.frac, Y)
                twi2 = Amplifier(args.twi_df['Files'][ind].replace(
                    amp, config.Amp_dict[amp][0]),
                                 args.twi_df['Output'][ind],
                                 calpath=args.twi_df['Output'][ind],
                                 debug=True,
                                 refit=True,
                                 dark_mult=0.0,
                                 darkpath=args.darkdir,
                                 biaspath=args.biasdir,
                                 virusconfig=args.configdir,
                                 specname=args.specname[amp],
                                 use_pixelflat=(args.pixelflats < 1),
                                 init_lims=args.wvl_dict[amp],
                                 check_fibermodel=True,
                                 check_wave=True,
                                 fsize=args.fsize,
                                 fibmodel_nbins=args.fibmodel_bins,
                                 sigma=args.fibmodel_sig,
                                 power=args.fibmodel_pow)
                twi2.get_trace()
                folder = op.join(args.configdir, 'Fiber_Locations',
                                 args.twidir_date[0])
                mkpath(folder)
                fname = op.join(
                    args.configdir, 'Fiber_Locations', args.twidir_date[0],
                    'fiber_loc_%s_%s_%s_%s.txt' %
                    (args.twi_df['Specid'][ind], args.twi_df['Ifuslot'][ind],
                     args.twi_df['Ifuid'][ind], config.Amp_dict[amp][0]))

                if len(twi2.fibers) == args.nfibers[config.Amp_dict[amp][0]]:
                    print("The correct number of fibers, %i, were found." %
                          args.nfibers[config.Amp_dict[amp][0]])
                    Y = np.zeros((len(twi2.fibers), 2))
                    col = int(twi2.D * config.frac)
                    for i, fiber in enumerate(twi2.fibers):
                        Y[i, 0] = fiber.trace[col]
                    print("Printing file for fibers.")
                    create_fiber_file(fname, config.frac, Y)
                else:
                    print("Only found %i fibers instead of the expected %i" %
                          (len(twi2.fibers),
                           args.nfibers[config.Amp_dict[amp][0]]))
                    Y = find_missing_fibers(config.frac, twi2)
                    create_fiber_file(fname, config.frac, Y)