Ejemplo n.º 1
0
    def __init__(self, opt):

        output_directory = opt.common.output_directory.val
        filename = ""

        self.results_dict = {}
        self.results_dict['simulation_mode'] = opt.simulation.sim_mode.val
        self.results_dict[
            'simulation_realisations'] = opt.simulation.sim_realisations.val
        self.results_dict['ch'] = opt.observation.obs_channel.val
        self.noise_dict = {}

        opt.pipeline.useSignal.val = 1
        opt.simulation.sim_use_fast.val = 1
        opt.pipeline.split = 0
        opt.noise.ApplyRandomPRNU.val = 1

        opt.timeline.apply_lc.val = 0
        opt.timeline.useLDC.val = 0
        opt.pipeline.useAllen.val = 1
        opt.timeline.use_T14.val = 0
        opt.timeline.obs_time.val = 0 * u.hr  # 0 means n_exp overides obs_time
        opt.timeline.n_exp.val = 1000.0  # uses 1000 exposures
        # opt.timeline.n_exp.val = 400

        noise_type = int(opt.noise.sim_noise_source.val)

        nb_dict = {
            'rn': [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
            'sn': [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
            'spat': [1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
            'spec': [1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
            'emm_switch': [1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1],
            'zodi_switch': [1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1],
            'dc_switch': [1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0],
            'source_switch': [1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
            'diff': [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
            'jitter_switch': [1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
            'fano': [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
            'noise_tag': [
                'All noise', 'All photon noise', 'Source photon noise',
                'Dark current noise', 'Zodi noise', 'Emission noise',
                'Read noise', 'Spatial jitter noise', 'Spectral jitter noise',
                'Combined jitter noise', 'No noise - no background',
                'No noise - all background', 'Fano noise'
            ],
            'color': [
                '0.5', 'b', 'b', 'k', 'orange', 'pink', 'y', 'g', 'purple',
                'r', '0.8', 'c', 'c'
            ]
        }

        opt.noise.EnableReadoutNoise.val = nb_dict['rn'][noise_type]
        opt.noise.EnableShotNoise.val = nb_dict['sn'][noise_type]
        opt.noise.EnableSpatialJitter.val = nb_dict['spat'][noise_type]
        opt.noise.EnableSpectralJitter.val = nb_dict['spec'][noise_type]
        opt.noise.EnableFanoNoise.val = nb_dict['fano'][noise_type]
        opt.background.EnableEmission.val = nb_dict['emm_switch'][noise_type]
        opt.background.EnableZodi.val = nb_dict['zodi_switch'][noise_type]
        opt.background.EnableDC.val = nb_dict['dc_switch'][noise_type]
        opt.background.EnableSource.val = nb_dict['source_switch'][noise_type]

        opt.diff = nb_dict['diff'][noise_type]
        opt.noise_tag = nb_dict['noise_tag'][noise_type]
        opt.color = nb_dict['color'][noise_type]

        self.noise_dict[nb_dict['noise_tag'][noise_type]] = {}

        jexosim_msg("Noise type: %s" % (nb_dict['noise_tag'][noise_type]), 1)

        start = 0
        end = int(start + opt.no_real)

        opt = self.run_JexoSimA(opt)

        if opt.observation_feasibility == 0:
            jexosim_msg("Observation not feasible...", opt.diagnostics)
            self.feasibility = 0
        else:
            self.feasibility = 1

            n_ndr0 = opt.n_ndr * 1
            ndr_end_frame_number0 = opt.ndr_end_frame_number * 1
            frames_per_ndr0 = opt.frames_per_ndr * 1
            duration_per_ndr0 = opt.duration_per_ndr * 1
            n_exp0 = opt.n_exp

            if n_ndr0 > 10000:

                opt.pipeline.split = 1
                if opt.diagnostics == 1:
                    jexosim_msg('number of NDRs > 10000: using split protocol',
                                opt.diagnostics)
            else:
                opt.pipeline.split = 0

        opt.pipeline.split = 0

        for j in range(start, end):

            if (opt.no_real - start) > 1:
                jexosim_msg("", 1)
                jexosim_msg("============= REALIZATION %s =============" % (j),
                            1)
                jexosim_msg(opt.lab, 1)
                jexosim_msg("", 1)

            opt = self.run_JexoSimA1(opt)  # set QE grid for this realization
            jexosim_msg("QE variations set", 1)
            jexosim_msg("Number of exposures %s" % (n_exp0), 1)

            # =============================================================================
            #  split simulation into chunks to permit computation - makes no difference to final results
            #  =============================================================================
            if opt.pipeline.split == 1:

                jexosim_msg('Splitting data series into chunks',
                            opt.diagnostics)
                # uses same QE grid and jitter timeline but otherwise randomoses noise
                ndrs_per_round = opt.effective_multiaccum * int(
                    5000 / opt.multiaccum)
                # ndrs_per_round = opt.effective_multiaccum*int(50/opt.multiaccum)

                total_chunks = len(np.arange(0, n_ndr0, ndrs_per_round))

                idx = np.arange(0, n_ndr0,
                                ndrs_per_round)  # list of starting ndrs

                for i in range(len(idx)):
                    jexosim_msg(
                        '=== Chunk %s / %s=====' % (i + 1, total_chunks),
                        opt.diagnostics)

                    if idx[i] == idx[-1]:
                        opt.n_ndr = n_ndr0 - idx[i]

                        opt.ndr_end_frame_number = ndr_end_frame_number0[
                            idx[i]:]
                        opt.frames_per_ndr = frames_per_ndr0[idx[i]:]
                        opt.duration_per_ndr = duration_per_ndr0[idx[i]:]

                    else:
                        opt.n_ndr = idx[i + 1] - idx[i]

                        opt.ndr_end_frame_number = ndr_end_frame_number0[
                            idx[i]:idx[i + 1]]
                        opt.frames_per_ndr = frames_per_ndr0[idx[i]:idx[i + 1]]
                        opt.duration_per_ndr = duration_per_ndr0[idx[i]:idx[i +
                                                                            1]]

                    opt.n_exp = int(opt.n_ndr / opt.effective_multiaccum)

                    if i == 0:
                        opt.use_external_jitter = 0

                        opt = self.run_JexoSimB(opt)
                        opt = self.run_pipeline_stage_1(opt)
                        if opt.pipeline.pipeline_auto_ap.val == 1:
                            opt.pipeline.pipeline_ap_factor.val = opt.AvBest
                        if (opt.noise.EnableSpatialJitter.val == 1
                                or opt.noise.EnableSpectralJitter.val == 1
                                or opt.noise.EnableAll.val
                                == 1) and opt.noise.DisableAll.val != 1:
                            opt.input_yaw_jitter, opt.input_pitch_jitter, opt._input_frame_osf = opt.yaw_jitter, opt.pitch_jitter, opt.frame_osf

                    else:
                        opt.pipeline.pipeline_auto_ap.val = 0
                        opt.use_external_jitter = 1  # uses the jitter timeline from the first realization

                        opt = self.run_JexoSimB(opt)
                        opt = self.run_pipeline_stage_1(opt)

                    jexosim_msg(
                        'Aperture used %s' %
                        (opt.pipeline.pipeline_ap_factor.val), opt.diagnostics)
                    binnedLC = opt.pipeline_stage_1.binnedLC
                    data = opt.pipeline_stage_1.opt.data_raw

                    #After chunks processed, now recombine
                    if i == 0:
                        data_stack = data
                        binnedLC_stack = binnedLC
                    else:
                        data_stack = np.dstack((data_stack, data))
                        binnedLC_stack = np.vstack((binnedLC_stack, binnedLC))

                aa = data_stack.sum(axis=0)
                bb = aa.sum(axis=0)
                jexosim_plot('test_from_sim',
                             opt.diagnostics,
                             ydata=bb[opt.effective_multiaccum::opt.
                                      effective_multiaccum])

                aa = binnedLC_stack.sum(axis=1)
                jexosim_plot('test_from_pipeline', opt.diagnostics, ydata=aa)

                opt.n_ndr = n_ndr0
                opt.ndr_end_frame_number = ndr_end_frame_number0
                opt.frames_per_ndr = frames_per_ndr0
                opt.duration_per_ndr = duration_per_ndr0
                opt.n_exp = n_exp0

            elif opt.pipeline.split == 0:

                opt = self.run_JexoSimB(opt)
                if j == start:  # first realization sets the ap, then the other use the same one
                    opt.pipeline.pipeline_auto_ap.val = 1
                else:
                    opt.pipeline.pipeline_auto_ap.val = 0
                opt = self.run_pipeline_stage_1(opt)
                if j == start:  # first realization sets the ap, then the other use the same one
                    if opt.pipeline.pipeline_apply_mask.val == 1:
                        opt.pipeline.pipeline_ap_factor.val = opt.AvBest

                binnedLC_stack = opt.pipeline_stage_1.binnedLC
                data_stack = opt.pipeline_stage_1.opt.data_raw
                jexosim_plot('testvvv',
                             opt.diagnostics,
                             ydata=binnedLC_stack.sum(axis=1))

            aa = data_stack.sum(axis=0)
            import matplotlib.pyplot as plt
            plt.figure('data raw noise')
            plt.plot(opt.x_wav_osr[1::3], aa.std(axis=1))
            plt.figure('binned lc noise')
            std = binnedLC_stack.std(axis=0)
            plt.plot(opt.pipeline_stage_1.binnedWav, std)

            #take binnedLC_stack and now complete through pipeline stage 2
            opt.pipeline_stage_1.binnedLC = binnedLC_stack
            opt = self.run_pipeline_stage_2(opt)
            self.pipeline = opt.pipeline_stage_2

            self.noise_dict[opt.noise_tag]['wl'] = self.pipeline.binnedWav
            self.results_dict['input_spec'] = opt.cr
            self.results_dict['input_spec_wl'] = opt.cr_wl

            if j == start:
                self.noise_dict[
                    opt.noise_tag]['signal_std_stack'] = self.pipeline.ootNoise
                self.noise_dict[opt.noise_tag][
                    'signal_mean_stack'] = self.pipeline.ootSignal
                if opt.pipeline.useAllen.val == 1:
                    self.noise_dict[opt.noise_tag][
                        'fracNoT14_stack'] = self.pipeline.noiseAt1hr

                self.noise_dict[
                    opt.noise_tag]['signal_std_mean'] = self.pipeline.ootNoise
                self.noise_dict[opt.noise_tag][
                    'signal_mean_mean'] = self.pipeline.ootSignal
                if opt.pipeline.useAllen.val == 1:
                    self.noise_dict[opt.noise_tag][
                        'fracNoT14_mean'] = self.pipeline.noiseAt1hr

                self.noise_dict[opt.noise_tag]['signal_std_std'] = np.zeros(
                    len(self.pipeline.binnedWav))
                self.noise_dict[opt.noise_tag]['signal_mean_std'] = np.zeros(
                    len(self.pipeline.binnedWav))
                if opt.pipeline.useAllen.val == 1:
                    self.noise_dict[opt.noise_tag]['fracNoT14_std'] = np.zeros(
                        len(self.pipeline.binnedWav))

            else:
                self.noise_dict[opt.noise_tag]['signal_std_stack'] = np.vstack(
                    (self.noise_dict[opt.noise_tag]['signal_std_stack'],
                     self.pipeline.ootNoise))
                self.noise_dict[
                    opt.noise_tag]['signal_mean_stack'] = np.vstack(
                        (self.noise_dict[opt.noise_tag]['signal_mean_stack'],
                         self.pipeline.ootSignal))
                if opt.pipeline.useAllen.val == 1:
                    self.noise_dict[
                        opt.noise_tag]['fracNoT14_stack'] = np.vstack(
                            (self.noise_dict[opt.noise_tag]['fracNoT14_stack'],
                             self.pipeline.noiseAt1hr))

                self.noise_dict[
                    opt.noise_tag]['signal_std_mean'] = self.noise_dict[
                        opt.noise_tag]['signal_std_stack'].mean(axis=0)
                self.noise_dict[
                    opt.noise_tag]['signal_mean_mean'] = self.noise_dict[
                        opt.noise_tag]['signal_mean_stack'].mean(axis=0)
                if opt.pipeline.useAllen.val == 1:
                    self.noise_dict[
                        opt.noise_tag]['fracNoT14_mean'] = self.noise_dict[
                            opt.noise_tag]['fracNoT14_stack'].mean(axis=0)

                self.noise_dict[
                    opt.noise_tag]['signal_std_std'] = self.noise_dict[
                        opt.noise_tag]['signal_std_stack'].std(axis=0)
                self.noise_dict[
                    opt.noise_tag]['signal_mean_std'] = self.noise_dict[
                        opt.noise_tag]['signal_mean_stack'].std(axis=0)
                if opt.pipeline.useAllen.val == 1:
                    self.noise_dict[
                        opt.noise_tag]['fracNoT14_std'] = self.noise_dict[
                            opt.noise_tag]['fracNoT14_stack'].std(axis=0)

            self.noise_dict[opt.noise_tag]['bad_map'] = opt.bad_map
            self.noise_dict[
                opt.noise_tag]['example_exposure_image'] = opt.exp_image
            self.noise_dict[
                opt.noise_tag]['pixel wavelengths'] = opt.x_wav_osr[1::3].value

            self.results_dict['noise_dic'] = self.noise_dict

            time_tag = (datetime.now().strftime('%Y_%m_%d_%H%M_%S'))
            self.results_dict['time_tag'] = time_tag

            if j != start:
                os.remove(filename)  # delete previous temp file
                txt_file = '%s.txt' % (filename)
                os.remove(txt_file)
            filename = '%s/OOT_SNR_%s_%s_TEMP.pickle' % (output_directory,
                                                         opt.lab, time_tag)
            self.filename = 'OOT_SNR_%s_%s_TEMP.pickle' % (opt.lab, time_tag)
            with open(filename, 'wb') as handle:
                pickle.dump(self.results_dict,
                            handle,
                            protocol=pickle.HIGHEST_PROTOCOL)

            if j == end - 1:
                os.remove(filename)  # delete previous temp file
                filename = '%s/OOT_SNR_%s_%s.pickle' % (output_directory,
                                                        opt.lab, time_tag)
                with open(filename, 'wb') as handle:
                    pickle.dump(self.results_dict,
                                handle,
                                protocol=pickle.HIGHEST_PROTOCOL)

                jexosim_msg('Results in %s' % (filename), 1)
                self.filename = 'OOT_SNR_%s_%s.pickle' % (opt.lab, time_tag)

            write_record(opt, output_directory, self.filename,
                         opt.params_file_path)
Ejemplo n.º 2
0
    def __init__(self, opt):
        
        output_directory = opt.common.output_directory.val
        filename =""
        
        self.results_dict ={}
        self.results_dict['simulation_mode'] = opt.simulation.sim_mode.val
        self.results_dict['simulation_realisations'] = opt.simulation.sim_realisations.val
        self.results_dict['ch'] =  opt.observation.obs_channel.val 
        self.noise_dict ={}    
   
        opt.pipeline.useSignal.val=1
        opt.simulation.sim_use_fast.val =1
        opt.pipeline.split  = 0
        opt.noise.ApplyRandomPRNU.val=1
                      
        opt.timeline.apply_lc.val = 0
        opt.timeline.useLDC.val = 0
        opt.pipeline.useAllen.val =1
        opt.timeline.use_T14.val=0
        opt.timeline.obs_time.val = 0*u.hr # 0 means n_exp overides obs_time
        opt.timeline.n_exp.val = 1000.0 # uses 1000 exposures 
        # opt.timeline.n_exp.val = 10.0 # uses 1000 exposures 

     
        noise_type = int(opt.noise.sim_noise_source.val)
   
        nb_dict = {'rn'             :[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
               'sn'                 :[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
               'spat'               :[1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0],                                   
               'spec'               :[1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],                                      
               'emm_switch'         :[1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0],                    
               'zodi_switch'        :[1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0],    
               'dc_switch'          :[1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
               'source_switch'      :[1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
               'diff'               :[0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1],
               'jitter_switch'      :[1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
               'fano'               :[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
               'sunshield_switch'   :[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
               'noise_tag': [ 'All noise','All photon noise','Source photon noise','Dark current noise',
                        'Zodi noise','Emission noise','Read noise','Spatial jitter noise',
                        'Spectral jitter noise','Combined jitter noise','No noise - no background','No noise - all background', 'Fano noise', 'Sunshield noise'],  
                        'color': ['0.5','b', 'b','k','orange','pink', 'y','g','purple','r', '0.8','c', 'brown']
              } 
                
        opt.noise.EnableReadoutNoise.val = nb_dict['rn'][noise_type]
        opt.noise.EnableShotNoise.val = nb_dict['sn'][noise_type]
        opt.noise.EnableSpatialJitter.val= nb_dict['spat'][noise_type]
        opt.noise.EnableSpectralJitter.val= nb_dict['spec'][noise_type] 
        opt.noise.EnableFanoNoise.val= nb_dict['fano'][noise_type]   
        opt.background.EnableEmission.val = nb_dict['emm_switch'][noise_type]
        opt.background.EnableZodi.val = nb_dict['zodi_switch'][noise_type]   
        opt.background.EnableSunshield.val = nb_dict['sunshield_switch'][noise_type]  
        opt.background.EnableDC.val  =  nb_dict['dc_switch'][noise_type]
        opt.background.EnableSource.val  = nb_dict['source_switch'][noise_type]
        opt.diff = nb_dict['diff'][noise_type]      
        opt.noise_tag = nb_dict['noise_tag'][noise_type]
        opt.color = nb_dict['color'][noise_type]
                        
        self.noise_dict[nb_dict['noise_tag'][noise_type]] ={}
        
        jexosim_msg ("Noise type: %s"%(nb_dict['noise_tag'][noise_type]), 1) 
  
        start = 0 
        end = int(start + opt.no_real)

        for j in range(start, end):
            
            
            if (opt.no_real-start) > 1:
                   jexosim_msg ("", 1)    
                   jexosim_msg ("============= REALIZATION %s ============="%(j), 1)
                   jexosim_msg (opt.lab, 1)                   
                   jexosim_msg ("", 1)  
            
            opt = self.run_JexoSimA(opt)
            opt = self.run_JexoSimA1(opt)
             
            
            # np.save('/Users/user1/Desktop/fp_signal.npy', opt.fp_signal[1::3,1::3].value)
            # np.save('/Users/user1/Desktop/fp_wav.npy', opt.x_wav_osr[1::3].value)
                        
                        
            # xxxx
            
     
            if opt.observation_feasibility ==0:      
                jexosim_msg ("Observation not feasible...", opt.diagnostics) 
                self.feasibility = 0
            else:
                self.feasibility = 1
                              
                opt = self.run_JexoSimB(opt)
                
                opt = self.run_pipeline_stage_1(opt)       
                opt = self.run_pipeline_stage_2(opt)              
                self.pipeline = opt.pipeline_stage_2
                
                
                self.noise_dict[opt.noise_tag]['wl'] = self.pipeline.binnedWav
                self.results_dict['input_spec'] = opt.cr
                self.results_dict['input_spec_wl'] = opt.cr_wl            


                
          

                         
                if j == start:                    
                    self.noise_dict[opt.noise_tag]['signal_std_stack'] = self.pipeline.ootNoise
                    self.noise_dict[opt.noise_tag]['signal_mean_stack'] = self.pipeline.ootSignal
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_stack'] = self.pipeline.noiseAt1hr
                        
                    self.noise_dict[opt.noise_tag]['signal_std_mean'] = self.pipeline.ootNoise
                    self.noise_dict[opt.noise_tag]['signal_mean_mean'] = self.pipeline.ootSignal
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_mean'] = self.pipeline.noiseAt1hr
                        
                    self.noise_dict[opt.noise_tag]['signal_std_std'] = np.zeros(len(self.pipeline.binnedWav))
                    self.noise_dict[opt.noise_tag]['signal_mean_std'] = np.zeros(len(self.pipeline.binnedWav))
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_std'] = np.zeros(len(self.pipeline.binnedWav))
                   

                else:
                    self.noise_dict[opt.noise_tag]['signal_std_stack'] = np.vstack((self.noise_dict[opt.noise_tag]['signal_std_stack'], self.pipeline.ootNoise))
                    self.noise_dict[opt.noise_tag]['signal_mean_stack'] = np.vstack((self.noise_dict[opt.noise_tag]['signal_mean_stack'], self.pipeline.ootSignal))
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_stack'] = np.vstack((self.noise_dict[opt.noise_tag]['fracNoT14_stack'], self.pipeline.noiseAt1hr))

                    self.noise_dict[opt.noise_tag]['signal_std_mean'] = self.noise_dict[opt.noise_tag]['signal_std_stack'].mean(axis=0)
                    self.noise_dict[opt.noise_tag]['signal_mean_mean'] = self.noise_dict[opt.noise_tag]['signal_mean_stack'].mean(axis=0)
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_mean'] = self.noise_dict[opt.noise_tag]['fracNoT14_stack'].mean(axis=0)
                        
                    self.noise_dict[opt.noise_tag]['signal_std_std'] = self.noise_dict[opt.noise_tag]['signal_std_stack'].std(axis=0)
                    self.noise_dict[opt.noise_tag]['signal_mean_std'] = self.noise_dict[opt.noise_tag]['signal_mean_stack'].std(axis=0)
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_std'] = self.noise_dict[opt.noise_tag]['fracNoT14_stack'].std(axis=0)

                self.noise_dict[opt.noise_tag]['bad_map'] = opt.bad_map
                self.noise_dict[opt.noise_tag]['example_exposure_image'] = opt.exp_image
                self.noise_dict[opt.noise_tag]['pixel wavelengths'] = opt.x_wav_osr[1::3].value
                self.noise_dict[opt.noise_tag]['focal_plane_star_signal'] = opt.fp_signal[1::3,1::3].value

                self.results_dict['noise_dic'] = self.noise_dict

                time_tag = (datetime.now().strftime('%Y_%m_%d_%H%M_%S'))
                self.results_dict['time_tag'] =  time_tag
         
                if j != start:
                    os.remove(filename)  # delete previous temp file
                    txt_file = '%s.txt'%(filename)
                    os.remove(txt_file) 
                filename = '%s/OOT_SNR_%s_%s_TEMP.pickle'%(output_directory, opt.lab, time_tag)
                self.filename = 'OOT_SNR_%s_%s_TEMP.pickle'%(opt.lab, time_tag)
                with open(filename, 'wb') as handle:
                    pickle.dump(self.results_dict , handle, protocol=pickle.HIGHEST_PROTOCOL)
                   
                if j == end-1:
                    os.remove(filename)  # delete previous temp file
                    filename = '%s/OOT_SNR_%s_%s.pickle'%(output_directory, opt.lab, time_tag)
                    with open(filename, 'wb') as handle:
                        pickle.dump(self.results_dict , handle, protocol=pickle.HIGHEST_PROTOCOL)
                    
                    jexosim_msg('Results in %s'%(filename), 1)
                    self.filename = 'OOT_SNR_%s_%s.pickle'%(opt.lab, time_tag)
                                      
                write_record(opt, output_directory, self.filename, opt.params_file_path)
Ejemplo n.º 3
0
    def __init__(self, opt):

        output_directory = opt.common.output_directory.val
        filename = ""

        self.results_dict = {}
        self.results_dict['simulation_mode'] = opt.simulation.sim_mode.val
        self.results_dict[
            'simulation_realisations'] = opt.simulation.sim_realisations.val
        self.results_dict['ch'] = opt.observation.obs_channel.val

        opt.pipeline.useSignal.val = 0
        opt.simulation.sim_use_fast.val = 1
        opt.pipeline.split = 0
        opt.noise.ApplyRandomPRNU.val = 1

        opt.timeline.apply_lc.val = 1
        opt.timeline.useLDC.val = 1
        opt.pipeline.useAllen.val = 0
        opt.pipeline.fit_gamma.val = 0  #keep zero for uncert on p

        start = 0
        end = int(start + opt.no_real)

        if (opt.no_real - start) > 1:
            jexosim_msg("Monte Carlo selected", 1)

        opt = self.run_JexoSimA(opt)

        if opt.observation_feasibility == 0:
            jexosim_msg("Observation not feasible...", opt.diagnostics)
            self.feasibility = 0
        else:
            self.feasibility = 1
            n_ndr0 = opt.n_ndr * 1
            lc0 = opt.lc_original * 1
            ndr_end_frame_number0 = opt.ndr_end_frame_number * 1
            frames_per_ndr0 = opt.frames_per_ndr * 1
            duration_per_ndr0 = opt.duration_per_ndr * 1
            n_exp0 = opt.n_exp

            if n_ndr0 > 10000:

                opt.pipeline.split = 1
                if opt.diagnostics == 1:
                    jexosim_msg('number of NDRs > 10000: using split protocol',
                                opt.diagnostics)
            else:
                opt.pipeline.split = 0

            for j in range(start, end):

                if (opt.no_real - start) > 1:
                    jexosim_msg("", 1)
                    jexosim_msg(
                        "============= REALIZATION %s =============" % (j), 1)
                    jexosim_msg(opt.lab, 1)
                    jexosim_msg("", 1)

                pp = time.time()

                opt = self.run_JexoSimA1(
                    opt)  # set QE grid for this realization
                jexosim_msg("QE variations set", 1)
                jexosim_msg("Number of exposures %s" % (n_exp0), 1)

                lc0 = opt.lc_original * 1

                # =============================================================================
                #  # split simulation into chunks to permit computation - makes no difference to final results
                # =============================================================================
                if opt.pipeline.split == 1:

                    jexosim_msg('Splitting data series into chunks',
                                opt.diagnostics)
                    # uses same QE grid and jitter timeline but otherwise randomoses noise
                    ndrs_per_round = opt.effective_multiaccum * int(
                        5000 / opt.multiaccum)
                    # ndrs_per_round = opt.effective_multiaccum*int(50/opt.multiaccum)

                    total_chunks = len(np.arange(0, n_ndr0, ndrs_per_round))

                    idx = np.arange(0, n_ndr0,
                                    ndrs_per_round)  # list of starting ndrs

                    for i in range(len(idx)):
                        jexosim_msg(
                            '=== Chunk %s / %s=====' % (i + 1, total_chunks),
                            opt.diagnostics)

                        if idx[i] == idx[-1]:
                            opt.n_ndr = n_ndr0 - idx[i]
                            opt.lc_original = lc0[:, idx[i]:]
                            opt.ndr_end_frame_number = ndr_end_frame_number0[
                                idx[i]:]
                            opt.frames_per_ndr = frames_per_ndr0[idx[i]:]
                            opt.duration_per_ndr = duration_per_ndr0[idx[i]:]

                        else:
                            opt.n_ndr = idx[i + 1] - idx[i]
                            opt.lc_original = lc0[:, idx[i]:idx[i + 1]]
                            opt.ndr_end_frame_number = ndr_end_frame_number0[
                                idx[i]:idx[i + 1]]
                            opt.frames_per_ndr = frames_per_ndr0[idx[i]:idx[i +
                                                                            1]]
                            opt.duration_per_ndr = duration_per_ndr0[
                                idx[i]:idx[i + 1]]

                        opt.n_exp = int(opt.n_ndr / opt.effective_multiaccum)

                        if i == 0:
                            opt.pipeline.pipeline_auto_ap.val = 1
                            opt.use_external_jitter = 0

                            opt = self.run_JexoSimB(opt)
                            opt = self.run_pipeline_stage_1(opt)

                            opt.pipeline.pipeline_ap_factor.val = opt.AvBest
                            if (opt.noise.EnableSpatialJitter.val == 1
                                    or opt.noise.EnableSpectralJitter.val == 1
                                    or opt.noise.EnableAll.val
                                    == 1) and opt.noise.DisableAll.val != 1:
                                opt.input_yaw_jitter, opt.input_pitch_jitter, opt._input_frame_osf = opt.yaw_jitter, opt.pitch_jitter, opt.frame_osf

                        else:
                            opt.pipeline.pipeline_auto_ap.val = 0
                            opt.use_external_jitter = 1  # uses the jitter timeline from the first realization

                            opt = self.run_JexoSimB(opt)
                            opt = self.run_pipeline_stage_1(opt)

                        jexosim_msg(
                            'Aperture used %s' %
                            (opt.pipeline.pipeline_ap_factor.val),
                            opt.diagnostics)
                        data0 = opt.pipeline_stage_1.binnedLC
                        data = opt.pipeline_stage_1.opt.data_raw

                        if i == 0:
                            data_stack = data
                            data_stack0 = data0
                        else:
                            data_stack = np.dstack((data_stack, data))
                            data_stack0 = np.vstack((data_stack0, data0))

                    aa = data_stack.sum(axis=0)
                    bb = aa.sum(axis=0)
                    jexosim_plot('test_from_sim',
                                 opt.diagnostics,
                                 ydata=bb[opt.effective_multiaccum::opt.
                                          effective_multiaccum])
                    aa = data_stack0.sum(axis=1)
                    jexosim_plot('test_from_pipeline',
                                 opt.diagnostics,
                                 ydata=aa)

                    opt.n_ndr = n_ndr0
                    opt.ndr_end_frame_number = ndr_end_frame_number0
                    opt.frames_per_ndr = frames_per_ndr0
                    opt.duration_per_ndr = duration_per_ndr0
                    opt.n_exp = n_exp0

                elif opt.pipeline.split == 0:

                    opt = self.run_JexoSimB(opt)
                    opt = self.run_pipeline_stage_1(opt)
                    if j == start:  # first realization sets the ap, then the other use the same one
                        opt.use_auto_Ap = 1
                        opt.pipeline.pipeline_ap_factor.val = opt.AvBest
                    else:
                        opt.use_auto_Ap = 0

                    data_stack0 = opt.pipeline_stage_1.binnedLC
                    jexosim_plot('testvvv',
                                 opt.diagnostics,
                                 ydata=data_stack0.sum(axis=1))

                opt.pipeline_stage_1.binnedLC = data_stack0
                opt = self.run_pipeline_stage_2(opt)
                pipeline = opt.pipeline_stage_2

                p = pipeline.transitDepths
                if j == start:
                    p_stack = p
                else:
                    p_stack = np.vstack((p_stack, p))

                jexosim_msg(
                    "time to complete realization %s %s" %
                    (j, time.time() - pp), opt.diagnostics)

                self.results_dict['wl'] = pipeline.binnedWav
                self.results_dict['input_spec'] = opt.cr
                self.results_dict['input_spec_wl'] = opt.cr_wl

                if j == start:  # if only one realisation slightly different format
                    self.results_dict['p_stack'] = np.array(p)
                    self.results_dict['p_std'] = np.zeros(len(p))
                    self.results_dict['p_mean'] = np.array(p)
                else:
                    self.results_dict['p_stack'] = np.vstack(
                        (self.results_dict['p_stack'], p))
                    self.results_dict['p_std'] = self.results_dict[
                        'p_stack'].std(axis=0)
                    self.results_dict['p_mean'] = self.results_dict[
                        'p_stack'].mean(axis=0)

                time_tag = (datetime.now().strftime('%Y_%m_%d_%H%M_%S'))

                self.results_dict['time_tag'] = time_tag
                self.results_dict['bad_map'] = opt.bad_map
                self.results_dict['example_exposure_image'] = opt.exp_image
                self.results_dict['pixel wavelengths'] = opt.x_wav_osr[
                    1::3].value

                if j != start:
                    os.remove(filename)  # delete previous temp file

                filename = '%s/Full_transit_%s_TEMP.pickle' % (
                    output_directory, opt.lab)
                with open(filename, 'wb') as handle:
                    pickle.dump(self.results_dict,
                                handle,
                                protocol=pickle.HIGHEST_PROTOCOL)

            os.remove(filename)  # delete previous temp file
            # write final file
            filename = '%s/Full_transit_%s_%s.pickle' % (output_directory,
                                                         opt.lab, time_tag)
            with open(filename, 'wb') as handle:
                pickle.dump(self.results_dict,
                            handle,
                            protocol=pickle.HIGHEST_PROTOCOL)

            jexosim_msg('Results in %s' % (filename), 1)
            self.filename = 'Full_transit_%s_%s.pickle' % (opt.lab, time_tag)

            write_record(opt, output_directory, self.filename,
                         opt.params_file_path)
Ejemplo n.º 4
0
    def __init__(self, opt):
        
        output_directory = opt.common.output_directory.val
        filename=""
        
        self.results_dict ={}
        self.results_dict['simulation_mode'] = opt.simulation.sim_mode.val
        self.results_dict['simulation_realisations'] = opt.simulation.sim_realisations.val
        self.results_dict['ch'] =  opt.observation.obs_channel.val 
        self.noise_dict ={}  
        self.feasibility =1

        opt.pipeline.useSignal.val=1
        opt.simulation.sim_use_fast.val =1
        opt.pipeline.split  = 0
        opt.noise.ApplyRandomPRNU.val=1

        opt.timeline.apply_lc.val = 0
        opt.timeline.useLDC.val = 0
        opt.pipeline.useAllen.val =1

        opt.pipeline.pipeline_auto_ap.val = 0 # for noise budget keep this to a fixed value (i.e. choose 0) so same for all sims

        opt.timeline.obs_time.val = 0*u.hr
        opt.timeline.n_exp.val = 1000.0
         
        noise_list = [0,2,3,4,5,6,7,8,9,12,13]
        # noise_list = [0,2,3,4,5,6,7,8,9]
        # noise_list = [2,9,12]


        start = 0 
        end = int(start + opt.no_real)      

        nb_dict = {'rn'             :[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
               'sn'                 :[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
               'spat'               :[1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0],                                   
               'spec'               :[1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],                                      
               'emm_switch'         :[1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0],                    
               'zodi_switch'        :[1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0],    
               'dc_switch'          :[1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
               'source_switch'      :[1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
               'diff'               :[0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1],
               'jitter_switch'      :[1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
               'fano'               :[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
               'sunshield_switch'   :[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
               'noise_tag': [ 'All noise','All photon noise','Source photon noise','Dark current noise',
                        'Zodi noise','Emission noise','Read noise','Spatial jitter noise',
                        'Spectral jitter noise','Combined jitter noise','No noise - no background','No noise - all background', 'Fano noise', 'Sunshield noise'],  
                        'color': ['0.5','b', 'b','k','orange','pink', 'y','g','purple','r', '0.8','c', 'c','brown']
              } 
        

        for i in noise_list:
            self.noise_dict[nb_dict['noise_tag'][i]] ={}
            
        for j in range(start,end):
            seed = np.random.randint(100000000)
            for i in noise_list:
    
                opt.noise.EnableReadoutNoise.val = nb_dict['rn'][i]
                opt.noise.EnableShotNoise.val = nb_dict['sn'][i]
                opt.noise.EnableSpatialJitter.val= nb_dict['spat'][i]
                opt.noise.EnableSpectralJitter.val= nb_dict['spec'][i]
                opt.noise.EnableFanoNoise.val= nb_dict['fano'][i]
                opt.background.EnableEmission.val = nb_dict['emm_switch'][i]
                opt.background.EnableZodi.val = nb_dict['zodi_switch'][i]  
                opt.background.EnableSunshield.val = nb_dict['sunshield_switch'][i]   
                opt.background.EnableDC.val  =  nb_dict['dc_switch'][i]
                opt.background.EnableSource.val  = nb_dict['source_switch'][i]
                opt.diff = nb_dict['diff'][i]      
                opt.noise_tag = nb_dict['noise_tag'][i]
                opt.color = nb_dict['color'][i]
  
                jexosim_msg('==========================================', 1)
                jexosim_msg('Noise source:%s'%(opt.noise_tag), 1)     
                
                np.random.seed(seed)
                opt = self.run_JexoSimA(opt)
                opt = self.run_JexoSimA1(opt)
                
                n_ndr0 = opt.n_ndr*1
                ndr_end_frame_number0 = opt.ndr_end_frame_number*1
                frames_per_ndr0 = opt.frames_per_ndr*1
                duration_per_ndr0 = opt.duration_per_ndr*1
                n_exp0 = opt.n_exp           
                            
                if n_ndr0 > 10000:
    
                    opt.pipeline.split = 1
                    if opt.diagnostics ==1 :
                       jexosim_msg ('number of NDRs > 10000: using split protocol', opt.diagnostics)
                else:
                    opt.pipeline.split = 0 

                opt.pipeline.split = 1                      

              # =============================================================================
              #  split simulation into chunks to permit computation - makes no difference to final results    
              #  =============================================================================
                if opt.pipeline.split ==1:
              
                   jexosim_msg('Splitting data series into chunks', opt.diagnostics)
                   # uses same QE grid and jitter timeline but otherwise randomoses noise
                   ndrs_per_round = opt.effective_multiaccum*int(5000/opt.multiaccum)  
                   ndrs_per_round = opt.effective_multiaccum*int(50/opt.multiaccum)  
  
                   total_chunks = len(np.arange(0, n_ndr0, ndrs_per_round))
                   
                   idx = np.arange(0, n_ndr0, ndrs_per_round) # list of starting ndrs
                   
                   for i in range(len(idx)):
                       jexosim_msg('=== Chunk %s / %s====='%(i+1, total_chunks), opt.diagnostics)
                       
                       if idx[i] == idx[-1]:
                           opt.n_ndr = n_ndr0 - idx[i]

                           opt.ndr_end_frame_number = ndr_end_frame_number0[idx[i]:]
                           opt.frames_per_ndr=  frames_per_ndr0[idx[i]:]
                           opt.duration_per_ndr = duration_per_ndr0[idx[i]:]  
                          
                       else:
                           opt.n_ndr = idx[i+1]- idx[i]

                           opt.ndr_end_frame_number = ndr_end_frame_number0[idx[i]: idx[i+1]]
                           opt.frames_per_ndr=  frames_per_ndr0[idx[i]: idx[i+1]]
                           opt.duration_per_ndr = duration_per_ndr0[idx[i]: idx[i+1]]
                  
                       opt.n_exp = int(opt.n_ndr/opt.effective_multiaccum)
                                   
                       if i == 0: 
                           opt.use_external_jitter = 0
                           
                           opt = self.run_JexoSimB(opt)
                           opt  = self.run_pipeline_stage_1(opt)  
                           if opt.pipeline.pipeline_auto_ap.val == 1:
                               opt.pipeline.pipeline_ap_factor.val= opt.AvBest 
                           if (opt.noise.EnableSpatialJitter.val  ==1 or opt.noise.EnableSpectralJitter.val  ==1 or opt.noise.EnableAll.val == 1) and opt.noise.DisableAll.val != 1:
                               opt.input_yaw_jitter, opt.input_pitch_jitter, opt._input_frame_osf = opt.yaw_jitter, opt.pitch_jitter, opt.frame_osf                     
                                                      
                       else:
                           opt.pipeline.pipeline_auto_ap.val = 0
                           opt.use_external_jitter = 1 # uses the jitter timeline from the first realization

                           opt = self.run_JexoSimB(opt)
                           opt  = self.run_pipeline_stage_1(opt)                
                                  
                       jexosim_msg('Aperture used %s'%(opt.pipeline.pipeline_ap_factor.val), opt.diagnostics)
                       binnedLC = opt.pipeline_stage_1.binnedLC
                       data = opt.pipeline_stage_1.opt.data_raw
                       
                       #After chunks processed, now recombine                                           
                       if i ==0:
                           data_stack = data
                           binnedLC_stack = binnedLC   
                       else:
                           data_stack = np.dstack((data_stack,data))
                           binnedLC_stack = np.vstack((binnedLC_stack, binnedLC))                  
                   
                   aa = data_stack.sum(axis=0)
                   bb = aa.sum(axis=0)
                   jexosim_plot('test_from_sim', opt.diagnostics,
                            ydata=bb[opt.effective_multiaccum::opt.effective_multiaccum] )

                   
                   aa = binnedLC_stack.sum(axis=1)
                   jexosim_plot('test_from_pipeline', opt.diagnostics,
                                        ydata=aa)                            

                   opt.n_ndr  = n_ndr0             
                   opt.ndr_end_frame_number  = ndr_end_frame_number0  
                   opt.frames_per_ndr  = frames_per_ndr0 
                   opt.duration_per_ndr = duration_per_ndr0
                   opt.n_exp = n_exp0                                
                           
                elif opt.pipeline.split ==0:

                   opt  = self.run_JexoSimB(opt)
                   if j==start:  # first realization sets the ap, then the other use the same one
                       opt.pipeline.pipeline_auto_ap.val= 1
                   else:
                       opt.pipeline.pipeline_auto_ap.val= 0
                   opt  = self.run_pipeline_stage_1(opt)
                   if j==start:  # first realization sets the ap, then the other use the same one
                       if opt.pipeline.pipeline_apply_mask.val == 1:
                           opt.pipeline.pipeline_ap_factor.val= opt.AvBest

                   binnedLC_stack  = opt.pipeline_stage_1.binnedLC
                   data_stack = opt.pipeline_stage_1.opt.data_raw
                   jexosim_plot('testvvv', opt.diagnostics,
                                ydata=binnedLC_stack.sum(axis=1) )  

            
                           
                #take binnedLC_stack and now complete through pipeline stage 2
                opt.pipeline_stage_1.binnedLC = binnedLC_stack    
                opt = self.run_pipeline_stage_2(opt)
                self.pipeline = opt.pipeline_stage_2  
                self.opt = opt
                        
                self.noise_dict[opt.noise_tag]['wl'] = self.pipeline.binnedWav
                 
                if j == start:                    
                    self.noise_dict[opt.noise_tag]['signal_std_stack'] = self.pipeline.ootNoise
                    self.noise_dict[opt.noise_tag]['signal_mean_stack'] = self.pipeline.ootSignal
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_stack'] = self.pipeline.noiseAt1hr
                        
                    self.noise_dict[opt.noise_tag]['signal_std_mean'] = self.pipeline.ootNoise
                    self.noise_dict[opt.noise_tag]['signal_mean_mean'] = self.pipeline.ootSignal
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_mean'] = self.pipeline.noiseAt1hr
                        
                    self.noise_dict[opt.noise_tag]['signal_std_std'] = np.zeros(len(self.pipeline.binnedWav))
                    self.noise_dict[opt.noise_tag]['signal_mean_std'] = np.zeros(len(self.pipeline.binnedWav))
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_std'] = np.zeros(len(self.pipeline.binnedWav))

                    self.noise_dict[opt.noise_tag]['bad_map'] = opt.bad_map
                    self.noise_dict[opt.noise_tag]['example_exposure_image'] = opt.exp_image
                    self.noise_dict[opt.noise_tag]['pixel wavelengths'] = opt.x_wav_osr[1::3].value
                                     

                else:
                    self.noise_dict[opt.noise_tag]['signal_std_stack'] = np.vstack((self.noise_dict[opt.noise_tag]['signal_std_stack'], self.pipeline.ootNoise))
                    self.noise_dict[opt.noise_tag]['signal_mean_stack'] = np.vstack((self.noise_dict[opt.noise_tag]['signal_mean_stack'], self.pipeline.ootSignal))
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_stack'] = np.vstack((self.noise_dict[opt.noise_tag]['fracNoT14_stack'], self.pipeline.noiseAt1hr))

                    self.noise_dict[opt.noise_tag]['signal_std_mean'] = self.noise_dict[opt.noise_tag]['signal_std_stack'].mean(axis=0)
                    self.noise_dict[opt.noise_tag]['signal_mean_mean'] = self.noise_dict[opt.noise_tag]['signal_mean_stack'].mean(axis=0)
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_mean'] = self.noise_dict[opt.noise_tag]['fracNoT14_stack'].mean(axis=0)
                        
                    self.noise_dict[opt.noise_tag]['signal_std_std'] = self.noise_dict[opt.noise_tag]['signal_std_stack'].std(axis=0)
                    self.noise_dict[opt.noise_tag]['signal_mean_std'] = self.noise_dict[opt.noise_tag]['signal_mean_stack'].std(axis=0)
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_std'] = self.noise_dict[opt.noise_tag]['fracNoT14_stack'].std(axis=0)

                self.noise_dict[opt.noise_tag]['bad_map'] = opt.bad_map
                self.noise_dict[opt.noise_tag]['example_exposure_image'] = opt.exp_image
                self.noise_dict[opt.noise_tag]['pixel wavelengths'] = opt.x_wav_osr[1::3].value
                    
                self.results_dict['noise_dic'] = self.noise_dict         
 
            # dump pickle file at end of each cycle of noise sims
            if opt.simulation.sim_output_type.val == 1:         
                time_tag = (datetime.now().strftime('%Y_%m_%d_%H%M_%S'))
                self.results_dict['time_tag'] =  time_tag
         
                # if j != start:
                #     os.remove(filename)  # delete previous temp file
                # filename = '%s/Noise_budget_%s_TEMP.pickle'%(output_directory, opt.lab)
                # with open(filename, 'wb') as handle:
                #     pickle.dump(self.results_dict , handle, protocol=pickle.HIGHEST_PROTOCOL)
                   
                if j == end-1:
                    # os.remove(filename)  # delete previous temp file
                    filename = '%s/Noise_budget_%s_%s.pickle'%(output_directory, opt.lab, time_tag)
                    with open(filename, 'wb') as handle:
                        pickle.dump(self.results_dict , handle, protocol=pickle.HIGHEST_PROTOCOL)
     
                        jexosim_msg('Results in %s'%(filename), 1)
                        self.filename = 'Noise_budget_%s_%s.pickle'%(opt.lab, time_tag)

                        write_record(opt, output_directory, self.filename, opt.params_file_path)
Ejemplo n.º 5
0
    def __init__(self, opt):

        output_directory = opt.common.output_directory.val
        filename = ""

        self.results_dict = {}
        self.results_dict['simulation_mode'] = opt.simulation.sim_mode.val
        self.results_dict[
            'simulation_realisations'] = opt.simulation.sim_realisations.val
        self.results_dict['ch'] = opt.observation.obs_channel.val
        self.noise_dict = {}

        opt.pipeline.useSignal.val = 1
        opt.simulation.sim_use_fast.val = 1
        opt.pipeline.split = 0
        opt.noise.ApplyRandomPRNU.val = 1

        opt.timeline.apply_lc.val = 0
        opt.timeline.useLDC.val = 0
        opt.pipeline.useAllen.val = 1

        opt.pipeline.pipeline_auto_ap.val = 0  # for noise budget keep this to a fixed value (i.e. choose 0) so same for all sims

        opt.timeline.obs_time.val = 0 * u.hr
        opt.timeline.n_exp.val = 1000.0

        noise_list = [0, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13]
        # noise_list = [12]

        start = 0
        end = int(start + opt.no_real)

        nb_dict = {
            'rn': [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
            'sn': [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
            'spat': [1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
            'spec': [1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
            'emm_switch': [1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0],
            'zodi_switch': [1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0],
            'dc_switch': [1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
            'source_switch': [1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
            'diff': [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1],
            'jitter_switch': [1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
            'fano': [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
            'sunshield_switch': [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
            'noise_tag': [
                'All noise', 'All photon noise', 'Source photon noise',
                'Dark current noise', 'Zodi noise', 'Emission noise',
                'Read noise', 'Spatial jitter noise', 'Spectral jitter noise',
                'Combined jitter noise', 'No noise - no background',
                'No noise - all background', 'Fano noise', 'Sunshield noise'
            ],
            'color': [
                '0.5', 'b', 'b', 'k', 'orange', 'pink', 'y', 'g', 'purple',
                'r', '0.8', 'c', 'c', 'brown'
            ]
        }

        for i in noise_list:
            self.noise_dict[nb_dict['noise_tag'][i]] = {}

        for j in range(start, end):

            seed = np.random.randint(100000000)
            for i in noise_list:

                opt.noise.EnableReadoutNoise.val = nb_dict['rn'][i]
                opt.noise.EnableShotNoise.val = nb_dict['sn'][i]
                opt.noise.EnableSpatialJitter.val = nb_dict['spat'][i]
                opt.noise.EnableSpectralJitter.val = nb_dict['spec'][i]
                opt.noise.EnableFanoNoise.val = nb_dict['fano'][i]
                opt.background.EnableEmission.val = nb_dict['emm_switch'][i]
                opt.background.EnableZodi.val = nb_dict['zodi_switch'][i]
                opt.background.EnableSunshield.val = nb_dict[
                    'sunshield_switch'][i]
                opt.background.EnableDC.val = nb_dict['dc_switch'][i]
                opt.background.EnableSource.val = nb_dict['source_switch'][i]
                opt.diff = nb_dict['diff'][i]
                opt.noise_tag = nb_dict['noise_tag'][i]
                opt.color = nb_dict['color'][i]

                jexosim_msg('==========================================', 1)
                jexosim_msg('Noise source:%s' % (opt.noise_tag), 1)

                if (opt.no_real - start) > 1:
                    print("============= REALIZATION %s =============" % (j))
                    print(opt.lab)

                np.random.seed(seed)
                opt = self.run_JexoSimA(opt)
                opt = self.run_JexoSimA1(opt)

                if opt.observation_feasibility == 0:
                    jexosim_msg("Observation not feasible...", opt.diagnostics)
                    self.feasibility = 0
                else:
                    self.feasibility = 1
                    opt = self.run_JexoSimB(opt)

                    if opt.simulation.sim_output_type.val == 1:

                        opt = self.run_pipeline_stage_1(opt)
                        opt = self.run_pipeline_stage_2(opt)

                        self.pipeline = opt.pipeline_stage_2
                        self.opt = opt

                        self.noise_dict[
                            opt.noise_tag]['wl'] = self.pipeline.binnedWav

                        if j == start:
                            self.noise_dict[opt.noise_tag][
                                'signal_std_stack'] = self.pipeline.ootNoise
                            self.noise_dict[opt.noise_tag][
                                'signal_mean_stack'] = self.pipeline.ootSignal
                            if opt.pipeline.useAllen.val == 1:
                                self.noise_dict[opt.noise_tag][
                                    'fracNoT14_stack'] = self.pipeline.noiseAt1hr

                            self.noise_dict[opt.noise_tag][
                                'signal_std_mean'] = self.pipeline.ootNoise
                            self.noise_dict[opt.noise_tag][
                                'signal_mean_mean'] = self.pipeline.ootSignal
                            if opt.pipeline.useAllen.val == 1:
                                self.noise_dict[opt.noise_tag][
                                    'fracNoT14_mean'] = self.pipeline.noiseAt1hr

                            self.noise_dict[
                                opt.noise_tag]['signal_std_std'] = np.zeros(
                                    len(self.pipeline.binnedWav))
                            self.noise_dict[
                                opt.noise_tag]['signal_mean_std'] = np.zeros(
                                    len(self.pipeline.binnedWav))
                            if opt.pipeline.useAllen.val == 1:
                                self.noise_dict[
                                    opt.noise_tag]['fracNoT14_std'] = np.zeros(
                                        len(self.pipeline.binnedWav))

                            self.noise_dict[
                                opt.noise_tag]['bad_map'] = opt.bad_map
                            self.noise_dict[opt.noise_tag][
                                'example_exposure_image'] = opt.exp_image
                            self.noise_dict[opt.noise_tag][
                                'pixel wavelengths'] = opt.x_wav_osr[
                                    1::3].value

                        else:
                            self.noise_dict[
                                opt.noise_tag]['signal_std_stack'] = np.vstack(
                                    (self.noise_dict[
                                        opt.noise_tag]['signal_std_stack'],
                                     self.pipeline.ootNoise))
                            self.noise_dict[opt.noise_tag][
                                'signal_mean_stack'] = np.vstack(
                                    (self.noise_dict[
                                        opt.noise_tag]['signal_mean_stack'],
                                     self.pipeline.ootSignal))
                            if opt.pipeline.useAllen.val == 1:
                                self.noise_dict[opt.noise_tag][
                                    'fracNoT14_stack'] = np.vstack(
                                        (self.noise_dict[
                                            opt.noise_tag]['fracNoT14_stack'],
                                         self.pipeline.noiseAt1hr))

                            self.noise_dict[opt.noise_tag][
                                'signal_std_mean'] = self.noise_dict[
                                    opt.noise_tag]['signal_std_stack'].mean(
                                        axis=0)
                            self.noise_dict[opt.noise_tag][
                                'signal_mean_mean'] = self.noise_dict[
                                    opt.noise_tag]['signal_mean_stack'].mean(
                                        axis=0)
                            if opt.pipeline.useAllen.val == 1:
                                self.noise_dict[opt.noise_tag][
                                    'fracNoT14_mean'] = self.noise_dict[
                                        opt.noise_tag]['fracNoT14_stack'].mean(
                                            axis=0)

                            self.noise_dict[opt.noise_tag][
                                'signal_std_std'] = self.noise_dict[
                                    opt.noise_tag]['signal_std_stack'].std(
                                        axis=0)
                            self.noise_dict[opt.noise_tag][
                                'signal_mean_std'] = self.noise_dict[
                                    opt.noise_tag]['signal_mean_stack'].std(
                                        axis=0)
                            if opt.pipeline.useAllen.val == 1:
                                self.noise_dict[opt.noise_tag][
                                    'fracNoT14_std'] = self.noise_dict[
                                        opt.noise_tag]['fracNoT14_stack'].std(
                                            axis=0)

                        self.noise_dict[opt.noise_tag]['bad_map'] = opt.bad_map
                        self.noise_dict[opt.noise_tag][
                            'example_exposure_image'] = opt.exp_image
                        self.noise_dict[opt.noise_tag][
                            'pixel wavelengths'] = opt.x_wav_osr[1::3].value
                        self.noise_dict[opt.noise_tag][
                            'focal_plane_star_signal'] = opt.fp_signal[
                                1::3, 1::3].value

                        self.results_dict['noise_dic'] = self.noise_dict

                    elif opt.simulation.sim_output_type.val == 2:

                        filename = output.run(opt)
                        write_record(opt, output_directory, filename,
                                     opt.params_file_path)

            if self.feasibility == 1:

                # dump pickle file at end of each cycle of noise sims
                if opt.simulation.sim_output_type.val == 1:
                    time_tag = (datetime.now().strftime('%Y_%m_%d_%H%M_%S'))
                    self.results_dict['time_tag'] = time_tag

                    # if j != start:
                    #     os.remove(filename)  # delete previous temp file
                    # filename = '%s/Noise_budget_%s_TEMP.pickle'%(output_directory, opt.lab)
                    # with open(filename, 'wb') as handle:
                    #     pickle.dump(self.results_dict , handle, protocol=pickle.HIGHEST_PROTOCOL)

                    if j == end - 1:
                        # os.remove(filename)  # delete previous temp file
                        filename = '%s/Noise_budget_%s_%s.pickle' % (
                            output_directory, opt.lab, time_tag)
                        with open(filename, 'wb') as handle:
                            pickle.dump(self.results_dict,
                                        handle,
                                        protocol=pickle.HIGHEST_PROTOCOL)

                            jexosim_msg('Results in %s' % (filename), 1)
                            self.filename = 'Noise_budget_%s_%s.pickle' % (
                                opt.lab, time_tag)

                            write_record(opt, output_directory, self.filename,
                                         opt.params_file_path)
Ejemplo n.º 6
0
    def __init__(self, opt):

        output_directory = opt.common.output_directory.val
        filename = ""
        runtag = int(np.random.uniform(0, 100000))

        self.results_dict = {}
        self.results_dict['simulation_mode'] = opt.simulation.sim_mode.val
        self.results_dict[
            'simulation_realisations'] = opt.simulation.sim_realisations.val
        self.results_dict['ch'] = opt.observation.obs_channel.val

        opt.pipeline.useSignal.val = 0
        opt.simulation.sim_use_fast.val = 1
        opt.pipeline.split = 0
        opt.noise.ApplyRandomPRNU.val = 1

        opt.timeline.apply_lc.val = 1
        opt.timeline.useLDC.val = 1
        opt.pipeline.useAllen.val = 0
        opt.pipeline.fit_gamma.val = 0  #keep zero for uncert on p

        start = 0
        end = int(start + opt.no_real)

        if (opt.no_real - start) > 1:
            jexosim_msg("Monte Carlo selected", 1)

        opt = self.run_JexoSimA(opt)

        # np.save('/Users/user1/Desktop/fp_signal.npy', opt.fp_signal[1::3,1::3].value)
        # np.save('/Users/user1/Desktop/fp_wav.npy', opt.x_wav_osr[1::3].value)

        # xxxx

        if opt.observation_feasibility == 0:
            jexosim_msg("Observation not feasible...", opt.diagnostics)
            self.feasibility = 0
        else:
            self.feasibility = 1
            n_ndr0 = opt.n_ndr * 1

            ndr_end_frame_number0 = opt.ndr_end_frame_number * 1
            frames_per_ndr0 = opt.frames_per_ndr * 1
            duration_per_ndr0 = opt.duration_per_ndr * 1
            n_exp0 = opt.n_exp
            lc0 = opt.lc_original * 1  # important this happens at this stage

            if n_ndr0 > 10000:

                opt.pipeline.split = 1
                if opt.diagnostics == 1:
                    jexosim_msg('number of NDRs > 10000: using split protocol',
                                opt.diagnostics)
            else:
                opt.pipeline.split = 0

            # #delete
            # opt.pipeline.split = 1

            for j in range(start, end):

                if (opt.no_real - start) > 1:
                    jexosim_msg("", 1)
                    jexosim_msg(
                        "============= REALIZATION %s =============" % (j), 1)
                    jexosim_msg(opt.lab, 1)
                    jexosim_msg("", 1)

                pp = time.time()

                opt = self.run_JexoSimA1(
                    opt)  # set QE grid for this realization
                jexosim_msg("QE variations set", 1)
                jexosim_msg("Number of exposures %s" % (n_exp0), 1)

                print(opt.diagnostics)

                # =============================================================================
                #  # split simulation into chunks to permit computation - makes no difference to final results
                # =============================================================================
                if opt.pipeline.split == 1:

                    jexosim_msg('Splitting data series into chunks',
                                opt.diagnostics)
                    # uses same QE grid and jitter timeline but otherwise randomoses noise
                    ndrs_per_round = opt.effective_multiaccum * int(
                        5000 / opt.multiaccum)
                    # ndrs_per_round = opt.effective_multiaccum*int(500/opt.multiaccum)

                    total_chunks = len(np.arange(0, n_ndr0, ndrs_per_round))

                    idx = np.arange(0, n_ndr0,
                                    ndrs_per_round)  # list of starting ndrs

                    for i in range(len(idx)):
                        jexosim_msg(
                            '=== Realisation %s Chunk %s / %s=====' %
                            (j, i + 1, total_chunks), 1)
                        jexosim_msg(opt.lab, 1)
                        if idx[i] == idx[-1]:
                            opt.n_ndr = n_ndr0 - idx[i]
                            opt.lc_original = lc0[:, idx[i]:]
                            opt.ndr_end_frame_number = ndr_end_frame_number0[
                                idx[i]:]
                            opt.frames_per_ndr = frames_per_ndr0[idx[i]:]
                            opt.duration_per_ndr = duration_per_ndr0[idx[i]:]

                        else:
                            opt.n_ndr = idx[i + 1] - idx[i]
                            opt.lc_original = lc0[:, idx[i]:idx[i + 1]]
                            print('idx start......', idx[i])

                            opt.ndr_end_frame_number = ndr_end_frame_number0[
                                idx[i]:idx[i + 1]]
                            opt.frames_per_ndr = frames_per_ndr0[idx[i]:idx[i +
                                                                            1]]
                            opt.duration_per_ndr = duration_per_ndr0[
                                idx[i]:idx[i + 1]]

                        opt.n_exp = int(opt.n_ndr / opt.effective_multiaccum)

                        if i == 0:
                            opt.pipeline.pipeline_auto_ap.val = 1
                            opt.use_external_jitter = 0

                            opt = self.run_JexoSimB(opt)
                            opt = self.run_pipeline_stage_1(opt)

                            opt.pipeline.pipeline_ap_factor.val = opt.AvBest
                            if (opt.noise.EnableSpatialJitter.val == 1
                                    or opt.noise.EnableSpectralJitter.val == 1
                                    or opt.noise.EnableAll.val
                                    == 1) and opt.noise.DisableAll.val != 1:
                                opt.input_yaw_jitter, opt.input_pitch_jitter, opt._input_frame_osf = opt.yaw_jitter, opt.pitch_jitter, opt.frame_osf

                        else:
                            opt.pipeline.pipeline_auto_ap.val = 0
                            opt.use_external_jitter = 1  # uses the jitter timeline from the first realization

                            opt = self.run_JexoSimB(opt)
                            opt = self.run_pipeline_stage_1(opt)

                        jexosim_msg(
                            'Aperture used %s' %
                            (opt.pipeline.pipeline_ap_factor.val),
                            opt.diagnostics)
                        binnedLC = opt.pipeline_stage_1.binnedLC
                        data = opt.pipeline_stage_1.opt.data_raw

                        if i == 0:
                            data_stack = data * 1
                            binnedLC_stack = binnedLC * 1
                        else:
                            data_stack = np.dstack((data_stack, data))
                            binnedLC_stack = np.vstack(
                                (binnedLC_stack, binnedLC))

                        del data
                        del binnedLC

                    aa = data_stack.sum(axis=0)
                    bb = aa.sum(axis=0)
                    jexosim_plot('test_from_sim',
                                 opt.diagnostics,
                                 ydata=bb[opt.effective_multiaccum::opt.
                                          effective_multiaccum])
                    aa = binnedLC_stack.sum(axis=1)
                    jexosim_plot('test_from_pipeline',
                                 opt.diagnostics,
                                 ydata=aa)

                    opt.n_ndr = n_ndr0
                    opt.ndr_end_frame_number = ndr_end_frame_number0
                    opt.frames_per_ndr = frames_per_ndr0
                    opt.duration_per_ndr = duration_per_ndr0
                    opt.n_exp = n_exp0

                elif opt.pipeline.split == 0:

                    opt = self.run_JexoSimB(opt)
                    if j == start:  # first realization sets the ap, then the other use the same one
                        opt.pipeline.pipeline_auto_ap.val = 1
                    else:
                        opt.pipeline.pipeline_auto_ap.val = 0
                    opt = self.run_pipeline_stage_1(opt)
                    if j == start:  # first realization sets the ap, then the other use the same one
                        opt.pipeline.pipeline_ap_factor.val = opt.AvBest

                    binnedLC_stack = opt.pipeline_stage_1.binnedLC
                    jexosim_plot('testvvv',
                                 opt.diagnostics,
                                 ydata=binnedLC_stack.sum(axis=1))

                opt.pipeline_stage_1.binnedLC = binnedLC_stack
                opt = self.run_pipeline_stage_2(opt)
                pipeline = opt.pipeline_stage_2

                p = pipeline.transitDepths
                if j == start:
                    p_stack = p
                else:
                    p_stack = np.vstack((p_stack, p))

                jexosim_msg(
                    "time to complete realization %s %s" %
                    (j, time.time() - pp), opt.diagnostics)

                self.results_dict['wl'] = pipeline.binnedWav
                self.results_dict['input_spec'] = opt.cr
                self.results_dict['input_spec_wl'] = opt.cr_wl

                if j == start:  # if only one realisation slightly different format
                    self.results_dict['p_stack'] = np.array(p)
                    self.results_dict['p_std'] = np.zeros(len(p))
                    self.results_dict['p_mean'] = np.array(p)
                else:
                    self.results_dict['p_stack'] = np.vstack(
                        (self.results_dict['p_stack'], p))
                    self.results_dict['p_std'] = self.results_dict[
                        'p_stack'].std(axis=0)
                    self.results_dict['p_mean'] = self.results_dict[
                        'p_stack'].mean(axis=0)

                time_tag = (datetime.now().strftime('%Y_%m_%d_%H%M_%S'))

                self.results_dict['time_tag'] = time_tag
                self.results_dict['bad_map'] = opt.bad_map
                self.results_dict['example_exposure_image'] = opt.exp_image
                self.results_dict['pixel wavelengths'] = opt.x_wav_osr[
                    1::3].value
                self.results_dict['focal_plane_star_signal'] = opt.fp_signal[
                    1::3, 1::3].value

                # fq = '/Users/user1/Desktop/tempGit/JexoSim_A/output/Full_eclipse_MIRI_LRS_slitless_SLITLESSPRISM_FAST_HD_209458_b_2021_07_18_0852_28.pickle'

                # with open(fq, 'rb') as handle:
                #   rd = pickle.load(handle)
                # rd['focal_plane_star_signal'] = opt.fp_signal[1::3,1::3]
                # rd['pixel wavelengths'] = opt.x_wav_osr[1::3].value
                # with open(fq, 'wb') as handle:
                #         pickle.dump(rd , handle, protocol=pickle.HIGHEST_PROTOCOL)

                # run('Full_transit_NIRSpec_BOTS_G140M_F100LP_SUB2048_NRSRAPID_K2-18_b_2021_07_18_1402_38.pickle',0)
                # run('Full_transit_NIRSpec_BOTS_G235M_F170LP_SUB2048_NRSRAPID_K2-18_b_2021_07_18_0709_24.pickle', 2)
                # run('Full_transit_NIRSpec_BOTS_G395M_F290LP_SUB2048_NRSRAPID_K2-18_b_2021_07_17_2339_51.pickle',4)

                # run('Full_eclipse_NIRCam_TSGRISM_F444W_SUBGRISM64_4_output_RAPID_HD_209458_b_2021_07_19_0654_29.pickle',0)
                # run('Full_eclipse_NIRCam_TSGRISM_F322W2_SUBGRISM64_4_output_RAPID_HD_209458_b_2021_07_19_0359_08.pickle',1)
                # run('Full_eclipse_MIRI_LRS_slitless_SLITLESSPRISM_FAST_HD_209458_b_2021_07_18_0852_28.pickle', 2)

                if j != start:
                    os.remove(filename)  # delete previous temp file

                filename = '%s/Full_transit_%s_TEMP%s.pickle' % (
                    output_directory, opt.lab, runtag)
                if opt.observation.obs_type.val == 2:
                    filename = '%s/Full_eclipse_%s_TEMP%s.pickle' % (
                        output_directory, opt.lab, runtag)

                with open(filename, 'wb') as handle:
                    pickle.dump(self.results_dict,
                                handle,
                                protocol=pickle.HIGHEST_PROTOCOL)

                del pipeline
                del binnedLC_stack
                gc.collect()

            os.remove(filename)  # delete previous temp file
            # write final file
            filename = '%s/Full_transit_%s_%s.pickle' % (output_directory,
                                                         opt.lab, time_tag)
            if opt.observation.obs_type.val == 2:
                filename = '%s/Full_eclipse_%s_%s.pickle' % (output_directory,
                                                             opt.lab, time_tag)

            with open(filename, 'wb') as handle:
                pickle.dump(self.results_dict,
                            handle,
                            protocol=pickle.HIGHEST_PROTOCOL)

            jexosim_msg('Results in %s' % (filename), 1)
            self.filename = 'Full_transit_%s_%s.pickle' % (opt.lab, time_tag)
            if opt.observation.obs_type.val == 2:
                self.filename = 'Full_eclipse_%s_%s.pickle' % (opt.lab,
                                                               time_tag)

            write_record(opt, output_directory, self.filename,
                         opt.params_file_path)
Ejemplo n.º 7
0
    def __init__(self, opt):

        output_directory = opt.common.output_directory.val
        filename = ""

        self.results_dict = {}
        self.results_dict['simulation_mode'] = opt.simulation.sim_mode.val
        self.results_dict[
            'simulation_realisations'] = opt.simulation.sim_realisations.val
        self.results_dict['ch'] = opt.observation.obs_channel.val

        opt.pipeline.useSignal.val = 0
        opt.simulation.sim_use_fast.val = 1
        opt.pipeline.split = 0
        opt.noise.ApplyRandomPRNU.val = 1

        opt.timeline.apply_lc.val = 1
        opt.timeline.useLDC.val = 1
        opt.pipeline.useAllen.val = 0
        opt.pipeline.fit_gamma.val = 0  #keep zero for uncert on p

        start = 0
        end = int(start + opt.no_real)

        if (opt.no_real - start) > 1:
            jexosim_msg("Monte Carlo selected", 1)

        opt = self.run_JexoSimA(opt)

        if opt.observation_feasibility == 0:
            jexosim_msg("Observation not feasible...", opt.diagnostics)
            self.feasibility = 0
        else:
            self.feasibility = 1
            n_ndr0 = opt.n_ndr * 1

            ndr_end_frame_number0 = opt.ndr_end_frame_number * 1
            frames_per_ndr0 = opt.frames_per_ndr * 1
            duration_per_ndr0 = opt.duration_per_ndr * 1
            n_exp0 = opt.n_exp
            lc0 = opt.lc_original * 1  # important this happens at this stage

            if n_ndr0 > 10000:

                opt.pipeline.split = 1
                if opt.diagnostics == 1:
                    jexosim_msg('number of NDRs > 10000: using split protocol',
                                opt.diagnostics)
            else:
                opt.pipeline.split = 0

            # #delete
            # opt.pipeline.split = 1

            for j in range(start, end):

                if (opt.no_real - start) > 1:
                    jexosim_msg("", 1)
                    jexosim_msg(
                        "============= REALIZATION %s =============" % (j), 1)
                    jexosim_msg(opt.lab, 1)
                    jexosim_msg("", 1)

                pp = time.time()

                opt = self.run_JexoSimA1(
                    opt)  # set QE grid for this realization
                jexosim_msg("QE variations set", 1)
                jexosim_msg("Number of exposures %s" % (n_exp0), 1)

                # =============================================================================
                #  # split simulation into chunks to permit computation - makes no difference to final results
                # =============================================================================
                if opt.pipeline.split == 1:

                    jexosim_msg('Splitting data series into chunks',
                                opt.diagnostics)
                    # uses same QE grid and jitter timeline but otherwise randomoses noise
                    ndrs_per_round = opt.effective_multiaccum * int(
                        5000 / opt.multiaccum)
                    # ndrs_per_round = opt.effective_multiaccum*int(500/opt.multiaccum)

                    total_chunks = len(np.arange(0, n_ndr0, ndrs_per_round))

                    idx = np.arange(0, n_ndr0,
                                    ndrs_per_round)  # list of starting ndrs

                    for i in range(len(idx)):
                        jexosim_msg(
                            '=== Realisation %s Chunk %s / %s=====' %
                            (j, i + 1, total_chunks), opt.diagnostics)

                        if idx[i] == idx[-1]:
                            opt.n_ndr = n_ndr0 - idx[i]
                            opt.lc_original = lc0[:, idx[i]:]
                            opt.ndr_end_frame_number = ndr_end_frame_number0[
                                idx[i]:]
                            opt.frames_per_ndr = frames_per_ndr0[idx[i]:]
                            opt.duration_per_ndr = duration_per_ndr0[idx[i]:]

                        else:
                            opt.n_ndr = idx[i + 1] - idx[i]
                            opt.lc_original = lc0[:, idx[i]:idx[i + 1]]
                            print('idx start......', idx[i])

                            opt.ndr_end_frame_number = ndr_end_frame_number0[
                                idx[i]:idx[i + 1]]
                            opt.frames_per_ndr = frames_per_ndr0[idx[i]:idx[i +
                                                                            1]]
                            opt.duration_per_ndr = duration_per_ndr0[
                                idx[i]:idx[i + 1]]

                        opt.n_exp = int(opt.n_ndr / opt.effective_multiaccum)

                        if i == 0:
                            opt.pipeline.pipeline_auto_ap.val = 1
                            opt.use_external_jitter = 0

                            opt = self.run_JexoSimB(opt)
                            opt = self.run_pipeline_stage_1(opt)

                            opt.pipeline.pipeline_ap_factor.val = opt.AvBest
                            if (opt.noise.EnableSpatialJitter.val == 1
                                    or opt.noise.EnableSpectralJitter.val == 1
                                    or opt.noise.EnableAll.val
                                    == 1) and opt.noise.DisableAll.val != 1:
                                opt.input_yaw_jitter, opt.input_pitch_jitter, opt._input_frame_osf = opt.yaw_jitter, opt.pitch_jitter, opt.frame_osf

                        else:
                            opt.pipeline.pipeline_auto_ap.val = 0
                            opt.use_external_jitter = 1  # uses the jitter timeline from the first realization

                            opt = self.run_JexoSimB(opt)
                            opt = self.run_pipeline_stage_1(opt)

                        jexosim_msg(
                            'Aperture used %s' %
                            (opt.pipeline.pipeline_ap_factor.val),
                            opt.diagnostics)
                        binnedLC = opt.pipeline_stage_1.binnedLC
                        data = opt.pipeline_stage_1.opt.data_raw

                        if i == 0:
                            data_stack = data
                            binnedLC_stack = binnedLC
                        else:
                            data_stack = np.dstack((data_stack, data))
                            binnedLC_stack = np.vstack(
                                (binnedLC_stack, binnedLC))

                    aa = data_stack.sum(axis=0)
                    bb = aa.sum(axis=0)
                    jexosim_plot('test_from_sim',
                                 opt.diagnostics,
                                 ydata=bb[opt.effective_multiaccum::opt.
                                          effective_multiaccum])
                    aa = binnedLC_stack.sum(axis=1)
                    jexosim_plot('test_from_pipeline',
                                 opt.diagnostics,
                                 ydata=aa)

                    opt.n_ndr = n_ndr0
                    opt.ndr_end_frame_number = ndr_end_frame_number0
                    opt.frames_per_ndr = frames_per_ndr0
                    opt.duration_per_ndr = duration_per_ndr0
                    opt.n_exp = n_exp0

                elif opt.pipeline.split == 0:

                    opt = self.run_JexoSimB(opt)
                    if j == start:  # first realization sets the ap, then the other use the same one
                        opt.pipeline.pipeline_auto_ap.val = 1
                    else:
                        opt.pipeline.pipeline_auto_ap.val = 0
                    opt = self.run_pipeline_stage_1(opt)
                    if j == start:  # first realization sets the ap, then the other use the same one
                        opt.pipeline.pipeline_ap_factor.val = opt.AvBest

                    binnedLC_stack = opt.pipeline_stage_1.binnedLC
                    jexosim_plot('testvvv',
                                 opt.diagnostics,
                                 ydata=binnedLC_stack.sum(axis=1))

# =============================================================================
# Now put intermediate data (binned light curves) into FITS files
# =============================================================================
                opt.pipeline_stage_1.binnedLC *= u.electron
                opt.pipeline_stage_1.binnedWav *= u.um

                self.filename = output.run(opt)

                write_record(opt, output_directory, self.filename,
                             opt.params_file_path)

                jexosim_msg(
                    'File saved as %s/%s' % (output_directory, self.filename),
                    1)