Beispiel #1
0
    def __init__(self,
                 spectrum,
                 atmgrid,
                 telescope,
                 disperser,
                 target,
                 header,
                 filename=""):
        """
        Args:
            filename (:obj:`str`): path to the image
        """
        self.my_logger = parameters.set_logger(self.__class__.__name__)

        self.spectrum = spectrum
        self.header = spectrum.header
        self.disperser = disperser
        self.target = target
        self.telescope = telescope

        self.atmgrid = atmgrid
        self.lambdas = atmgrid[0, index_atm_data:]
        self.lambda_binwidths = np.gradient(self.lambdas)
        self.spectragrid = None

        self.filename = ""
        if filename != "":
            self.filename = filename
            self.load_spectrum(filename)

        if parameters.VERBOSE:
            print self.header
Beispiel #2
0
def SpectractorSimGrid(filename, outputdir):
    """ SpectractorSimGrid
    Main function to simulate several spectra 
    A grid of spectra will be produced for a given target, airmass and pressure

    Args:
        filename (:obj:`str`): filename of the image (data)
        outputdir (:obj:`str`): path to the output directory
        
    """
    my_logger = parameters.set_logger(__name__)
    my_logger.info('\n\tStart SPECTRACTORSIMGRID')
    # Initialisation
    spectrum, telescope, disperser, target = SpectractorInit(
        filename, outputdir)
    # Set output path
    ensure_dir(outputdir)
    # extract the basename : simimar as os.path.basename(file)
    base_filename = filename.split('/')[-1]
    output_filename = os.path.join(
        outputdir, base_filename.replace('spectrum', 'spectrasim'))
    output_atmfilename = os.path.join(
        outputdir, base_filename.replace('spectrum', 'atmsim'))

    # SIMULATE ATMOSPHERE GRID
    # ------------------------
    airmass = spectrum.header['AIRMASS']
    pressure = spectrum.header['OUTPRESS']
    temperature = spectrum.header['OUTTEMP']
    atm = AtmosphereGrid(airmass, pressure, temperature, filename)

    # test if file already exists
    #if os.path.exists(output_atmfilename) and os.path.getsize(output_atmfilename)>MINFILESIZE:
    #    filesize= os.path.getsize(output_atmfilename)
    #    infostring=" atmospheric simulation file %s of size %d already exists, thus load it ..." % (output_atmfilename,filesize)
    #    my_logger.info(infostring)
    #    atmgrid,header=atm.loadfile(output_atmfilename)
    #else:
    atmgrid = atm.compute()
    header = atm.savefile(filename=output_atmfilename)
    atmsim.CleanSimDir()
    if parameters.VERBOSE:
        infostring = '\n\t ========= Atmospheric simulation :  ==============='
        my_logger.info(infostring)
        atm.plot_transmission()  # plot all atm transp profiles
        atm.plot_transm_img()  # plot 2D image summary of atm simulations

    # SPECTRA-GRID
    #-------------
    # in any case we re-calculate the spectra in case of change of transmission function
    spectra = SpectrumSimGrid(spectrum, atmgrid, telescope, disperser, target,
                              header)
    spectragrid = spectra.compute()
    spectra.save_spectra(output_filename)
    if parameters.VERBOSE:
        infostring = '\n\t ========= Spectra simulation :  ==============='
        spectra.plot_spectra()
        spectra.plot_spectra_img()
Beispiel #3
0
    def __init__(self, filtername=""):
        """
        Args:
        filename (:obj:`str`): path to the data filename (for info only)
        """

        self.my_logger = parameters.set_logger(self.__class__.__name__)
        self.filtername = filtername
        self.load_transmission()
Beispiel #4
0
 def __init__(self, airmass, pressure, temperature, filenamedata):
     Atmosphere.__init__(self, airmass, pressure, temperature)
     self.my_logger = parameters.set_logger(self.__class__.__name__)
     self.filenamedata = filenamedata
     # create the numpy array that will contains the atmospheric grid
     self.atmgrid = np.zeros(
         (NB_ATM_POINTS + 1, NB_atm_HEADER + NB_atm_DATA))
     self.atmgrid[0, index_atm_data:] = WL
     self.header = fits.Header()
Beispiel #5
0
 def __init__(self, spectrum, atmosphere, telescope, disperser):
     """
     Args:
         filename (:obj:`str`): path to the image
     """
     Spectrum.__init__(self)
     for k, v in spectrum.__dict__.items():
         self.__dict__[k] = copy.copy(v)
     self.my_logger = parameters.set_logger(self.__class__.__name__)
     self.disperser = disperser
     self.telescope = telescope
     self.atmosphere = atmosphere
     self.lambdas = None
     self.data = None
     self.err = None
Beispiel #6
0
 def __init__(self, label, verbose=False):
     self.my_logger = parameters.set_logger(self.__class__.__name__)
     self.label = label
     self.ra = None
     self.dec = None
     self.coord = None
     self.type = None
     self.redshift = 0
     self.wavelengths = []
     self.spectra = []
     self.verbose = verbose
     self.emission_spectrum = False
     self.hydrogen_only = False
     self.sed = None
     self.load()
Beispiel #7
0
    def __init__(self, logbook="ctiofulllogbook_jun2017_v5.csv"):
        """Load and initialise the logbook

        Args:
            logbook (str): path to the logbook. Must be a CSV file.
        """
        self.my_logger = parameters.set_logger(self.__class__.__name__)
        self.logbook = logbook
        if not os.path.isfile(logbook):
            self.my_logger.error(
                'CSV logbook file {} not found.'.format(logbook))
            sys.exit()
        self.csvfile = open(self.logbook, 'rU')
        self.reader = csv.DictReader(self.csvfile,
                                     delimiter=';',
                                     dialect=csv.excel_tab)
Beispiel #8
0
 def __init__(self, filename, atmgrid_filename="", live_fit=False):
     self.my_logger = parameters.set_logger(self.__class__.__name__)
     self.filename = filename
     self.live_fit = live_fit
     self.A1 = 1.0
     self.A2 = 0.1
     self.ozone = 300.
     self.pwv = 3
     self.aerosols = 0.03
     self.reso = 10.
     self.shift = 1e-3
     self.p = np.array([self.A1, self.A2, self.ozone, self.pwv, self.aerosols, self.reso, self.shift])
     self.lambdas = None
     self.model = None
     self.model_err = None
     self.model_noconv = None
     self.labels = ["$A_1$", "$A_2$", "ozone", "PWV", "VAOD", "reso", "$\lambda_{\\mathrm{shift}}$"]
     self.bounds = ((0, 0, 0, 0, 0, 1, -20), (np.inf, 1.0, np.inf, 10, 1.0, 100, 20))
     self.title = ""
     self.spectrum, self.telescope, self.disperser, self.target = SpectractorSimInit(filename)
     self.airmass = self.spectrum.header['AIRMASS']
     self.pressure = self.spectrum.header['OUTPRESS']
     self.temperature = self.spectrum.header['OUTTEMP']
     self.use_grid = False
     if atmgrid_filename == "":
         self.atmosphere = Atmosphere(self.airmass, self.pressure, self.temperature)
     else:
         self.use_grid = True
         self.atmosphere = AtmosphereGrid(filename, atmgrid_filename)
         if parameters.VERBOSE:
             self.my_logger.info('\n\tUse atmospheric grid models from file %s. ' % atmgrid_filename)
     self.p[0] *= np.max(self.spectrum.data) / np.max(self.simulation(self.spectrum.lambdas, *self.p))
     self.get_truth()
     if 0. in self.spectrum.err:
         self.spectrum.err = np.ones_like(self.spectrum.err)
     if parameters.DEBUG:
         fig = plt.figure()
         for i in range(10):
             a = self.atmosphere.interpolate(300, i, 0.05)
             plt.plot(self.atmosphere.lambdas, a, label='pwv=%dmm' % i)
         plt.grid()
         plt.xlabel('$\lambda$ [nm]')
         plt.ylabel('Atmospheric transmission')
         plt.legend(loc='best')
         plt.show()
Beispiel #9
0
def SpectractorInit(filename, outputdir):
    """ SpectractorInit
    Main function to simulate several spectra 
    A grid of spectra will be produced for a given target, airmass and pressure

    Args:
        filename (:obj:`str`): filename of the image (data)
        outputdir (:obj:`str`): path to the output directory
        
    """
    my_logger = parameters.set_logger(__name__)
    my_logger.info('\n\tStart SPECTRACTORSIM initialisation')
    # Load data spectrum
    spectrum = Spectrum(filename)

    # TELESCOPE TRANSMISSION
    # ------------------------
    telescope = TelescopeTransmission(spectrum.filter)
    if parameters.VERBOSE:
        infostring = '\n\t ========= Telescope transmission :  ==============='
        my_logger.info(infostring)
        telescope.plot_transmission()

    # DISPERSER TRANSMISSION
    # ------------------------
    disperser = Hologram(label=spectrum.disperser)
    if parameters.VERBOSE:
        infostring = '\n\t ========= Disperser transmission :  ==============='
        my_logger.info(infostring)
        disperser.plot_transmission()

    # STAR SPECTRUM
    # ------------------------
    target = spectrum.target
    if parameters.VERBOSE:
        infostring = '\n\t ========= SED : %s  ===============' % target.label
        my_logger.info(infostring)
        target.plot_spectra()

    return spectrum, telescope, disperser, target
Beispiel #10
0
def ImageSim(filename,
             outputdir,
             guess,
             target,
             pwv=5,
             ozone=300,
             aerosols=0,
             A1=1,
             A2=0,
             with_rotation=True,
             with_stars=True):
    """ The basic use of the pipeline consists first to define: 
    - the path to the fits image from which to extract the image, 
    - the path of the output directory to save the extracted spectrum (created automatically if does not exist yet),
    - the rough position of the object in the image,
    - the name of the target (to search for the extra-atmospheric spectrum if available).
    Then different parameters and systematics can be set:
    - pwv: the pressure water vapor (in mm)
    - ozone: the ozone quantity (in XX)
    - aerosols: the vertical aerosol optical depth
    - A1: a global grey absorption parameter for the spectrum
    - A2: the relative amplitude of second order compared with first order
    - with_rotation: rotate the spectrum according to the disperser characteristics (True by default)
    - with_stars: include stars in the image field (True by default)
    """
    my_logger = parameters.set_logger(__name__)
    my_logger.info('\n\tStart IMAGE SIMULATOR')
    # Load reduced image
    image = ImageModel(filename, target=target)
    if parameters.DEBUG:
        image.plot_image(scale='log10', target_pixcoords=guess)
    # Find the exact target position in the raw cut image: several methods
    my_logger.info('\n\tSearch for the target in the image...')
    target_pixcoords = image.find_target_2Dprofile(guess)
    # Background model
    my_logger.info('\n\tBackground model...')
    background = BackgroundModel(level=image.target_bkgd2D(0, 0),
                                 frame=(1600, 1650))
    if parameters.DEBUG:
        background.plot_model()
    # Target model
    my_logger.info('\n\tStar model...')
    star = StarModel(target_pixcoords,
                     image.target_star2D,
                     image.target_star2D.amplitude.value,
                     target=image.target)
    reso = star.sigma
    if parameters.DEBUG:
        star.plot_model()
    # Star field model
    starfield = None
    if with_stars:
        my_logger.info('\n\tStar field model...')
        starfield = StarFieldModel(image, threshold=0.01 * star.amplitude)
        if parameters.VERBOSE:
            image.plot_image(scale='log10',
                             target_pixcoords=starfield.pixcoords)
            starfield.plot_model()
    # Spectrum model
    my_logger.info('\n\tSpectum model...')
    lambdas = np.arange(parameters.LAMBDA_MIN, parameters.LAMBDA_MAX)
    airmass = image.header['AIRMASS']
    pressure = image.header['OUTPRESS']
    temperature = image.header['OUTTEMP']
    telescope = TelescopeTransmission(image.filter)
    spectrumsim = SpectractorSimCore(image,
                                     telescope,
                                     image.disperser,
                                     image.target,
                                     lambdas,
                                     airmass,
                                     pressure,
                                     temperature,
                                     pwv=pwv,
                                     ozone=ozone,
                                     aerosols=aerosols)
    spectrum = SpectrumModel(image,
                             spectrumsim,
                             sigma=reso,
                             A1=A1,
                             A2=A2,
                             reso=reso,
                             rotation=with_rotation)
    # Image model
    my_logger.info('\n\tImage model...')
    image.compute(star, background, spectrum, starfield=starfield)
    image.add_poisson_noise()
    image.convert_to_ADU_units()
    if parameters.VERBOSE:
        image.plot_image(scale="log",
                         title="Image simulation",
                         target_pixcoords=target_pixcoords,
                         units=image.units)
    # Set output path
    ensure_dir(outputdir)
    output_filename = filename.split('/')[-1]
    output_filename = (output_filename.replace('reduc',
                                               'sim')).replace('trim', 'sim')
    output_filename = os.path.join(outputdir, output_filename)
    # Save images and parameters
    image.header['A1'] = A1
    image.header['A2'] = A2
    image.header['OZONE'] = ozone
    image.header['PWV'] = pwv
    image.header['VAOD'] = aerosols
    image.header['reso'] = reso
    image.header['ROTATION'] = int(with_rotation)
    image.header['STARS'] = int(with_stars)
    image.save_image(output_filename, overwrite=True)
    return image
Beispiel #11
0
def SpectractorSim(filename,
                   outputdir,
                   lambdas,
                   pwv=5,
                   ozone=300,
                   aerosols=0.05,
                   overwrite=True):
    """ SpectractorSim
    Main function to simulate several spectra 
    A grid of spectra will be produced for a given target, airmass and pressure

    Args:
        filename (:obj:`str`): filename of the image (data)
        outputdir (:obj:`str`): path to the output directory
        pwv (:obj:`float`): pressure water vapor
        ozone (:obj:`float`): ozone quantity
        aerosols (:obj:`float`): VAOD Vertical Aerosols Optical Depth        
    """
    my_logger = parameters.set_logger(__name__)
    my_logger.info('\n\tStart SPECTRACTORSIM')
    # Initialisation
    spectrum, telescope, disperser, target = SpectractorInit(
        filename, outputdir)

    # SIMULATE ATMOSPHERE
    # -------------------
    airmass = spectrum.header['AIRMASS']
    pressure = spectrum.header['OUTPRESS']
    if pressure < 700:  # sometimes the weather data are bad so force a reasonable pressure
        pressure = 782.5
    temperature = spectrum.header['OUTTEMP']
    if pressure < 700:  # sometimes the weather data are bad so force a reasonable pressure
        temperature = 10.0  # Celcius degrees
    atmosphere = Atmosphere(airmass, pressure, temperature)
    atmosphere.simulate(pwv, ozone, aerosols)

    if parameters.VERBOSE:
        infostring = '\n\t ========= Atmospheric simulation :  ==============='
        my_logger.info(infostring)
        infostring = "\n\t outputdir = " + outputdir
        my_logger.info(infostring)

        atmosphere.plot_transmission()  # plot all atm transp profiles

    # SPECTRUM SIMULATION
    #--------------------
    spectrum_simulation = SpectrumSimulation(spectrum, atmosphere, telescope,
                                             disperser)
    spectrum_simulation.simulate(lambdas)

    if parameters.VERBOSE:
        infostring = '\n\t ========= Spectra simulation :  ==============='
        spectrum_simulation.plot_spectrum(nofit=True)

    # SAVE SPECTRUM
    #------------------
    if outputdir != None:

        if parameters.VERBOSE:
            infostring = "\n\t Simulated spectrum will be saved in outputdir = " + outputdir
            my_logger.info(infostring)

        # extract the basename : simimar as os.path.basename(file)
        base_filename = filename.split('/')[
            -1]  # get "reduc_20170530_213.fits"
        tag_filename = base_filename.split('_')[0]  # get "reduc_"
        search_str = '^%s_(.*)' % (tag_filename)  # get "^reduc_(.*)"
        root_filename = re.findall(search_str,
                                   base_filename)[0]  # get "20170530_213.fits'

        output_filename = 'specsim_' + root_filename  # get "spectrasim_20170530_213.fits"
        output_filename = os.path.join(outputdir, output_filename)

        if parameters.VERBOSE:
            infostring = 'output filename =' + output_filename
            my_logger.info(infostring)

        spectrum_simulation.save_spectrum(output_filename, overwrite=True)

    return spectrum_simulation
Beispiel #12
0
 def __init__(self, airmass, pressure, temperature):
     self.my_logger = parameters.set_logger(self.__class__.__name__)
     self.airmass = airmass
     self.pressure = pressure
     self.temperature = temperature
     self.transmission = lambda x: np.ones_like(x).astype(float)