def Fit(model_file, obs_file, plot):
    obs_values = tb.readtxt(obs_file,
                            ['wg_lines', 'flux_lines', 'sflux_lines'])
    model_values = tb.readtxt(model_file,
                              ['wg_lines', 'flux_lines', 'sflux_lines'])
    Lm, Lo = Match(model_values[0], obs_values[0])
    if plot is True:
        fig, ax1 = pl.subplots()
        pl.xlabel('obs')
        pl.ylabel('template')
        ax1.plot(Lo, Lm, 'r^')

    degree = 2
    if (len(Lm) <= degree):
        degree = int(len(Lm) - 1)
        print "Changed degree to : ", degree

    fit_coef = np.polyfit(Lo, Lm, deg=degree)
    points = np.linspace(100, 1500, 100)
    dispersion = np.polyval(fit_coef, Lo)
    dispersion2 = np.polyval(fit_coef, points)
    if plot is True:
        ax1.plot(points, dispersion2, color='k')
        fig, ax1 = pl.subplots()
        ax1.plot(Lo, Lm - dispersion, 'r^')
        pl.axhline(y=0., linewidth=1, color='k')
        pl.xlabel('template')
        pl.ylabel('residuals')
    print 'rms residuals = ', np.std(Lm - dispersion)
    return fit_coef, Lm
 def BuildDefectMap(self):
     xsize = int(np.round(self.x_end - self.x_start, 3))
     ysize = int(np.round(self.y_end - self.y_start, 3))
     defect_map = np.zeros([ysize, xsize])
     if self.orientation == 'y':
         defect_map = defect_map.T
         xsize = ysize
     '''Bad S/N for mono-> switch to broadband flat'''
     #fits, wgth  = tb.readtxt(self.monochro_flat_list, ['fits', 'wavelength'])
     #path = self.monochro_flat_path
     fits, wgth = tb.readtxt(self.masterflat_list, ['fits', 'wavelength'])
     out = zip(*sorted(zip(wgth, fits)))
     length = len(out[0])
     if length == 1:
         ref1 = instru.telinst(self.masterflat_path + out[1][0])
         defect_map = Flip((ref1.Image(self.masterflat_path + out[1][0]))\
                           [self.y_start : self.y_end, self.x_start : self.x_end], flip=self.flip)
     elif length > 1:
         defect_map = self.SyntheticMap(defect_map, length, out, xsize)
         if self.orientation == 'y':
             defect_map = defect_map.T  # To come back to the footprint
     return defect_map
    def BuildSyntheticFlat(self):
        ''' monitoring photodiode, R is in (mA/W)'''
        wg, responsivity, qe =\
        tb.readfile(self.spectrum.monitoring_photodiode_file ,\
                    ['wavelength', 'responsivity','QE'])

        xsize = self.spectrum.x_end - self.spectrum.x_start
        ysize = self.spectrum.y_end - self.spectrum.y_start
        syntheticflat = np.zeros([ysize, xsize])
        pointsX = []
        pointsY = []
        values = []
        fits, wgth = tb.readtxt(self.monochro_flat_list,
                                ['fits', 'wavelength'])
        for img, w in zip(fits, wgth):
            pixel = self.dispersion.Wavelength2Pixel(w)
            # ! pixel is relative to the object-> translate with respect to frame
            pixel = self.dispersion.RefPos2FramePos(pixel)
            flat = instru.telinst(self.monochro_flat_path + img)
            head = flat.header
            iphtot = head['IPHTOT']
            siphtot = head['SIPHTOT']
            image_data = tb.Flip((flat.Image(self.monochro_flat_path + img))\
                              [self.spectrum.y_start:self.spectrum.y_end, self.spectrum.x_start:self.spectrum.x_end], flip=self.spectrum.flip)
            which_amp = flat.IsInAmp(self.spectrum.y_start,
                                     self.spectrum.x_start,
                                     Trimmed=True)
            low = head['LOW' + str(which_amp)]
            high = head['HIGH' + str(which_amp)]
            '''remove 5s outliers'''
            if (self.spectrum.orientation == 'x'):
                signal     = tb.Flip((image_data[:, int(pixel)])[(image_data[:, int(pixel)] > low)\
                                            & (image_data[:, int(pixel)] < high)],flip=self.spectrum.flip)
            if (self.spectrum.orientation == 'y'):
                signal     = tb.Flip((image_data[int(pixel),:])[(image_data[int(pixel),:] > low)\
                                            & (image_data[int(pixel),:] < high)],flip=self.spectrum.flip)

            respinterp = interp.griddata(wg, responsivity, w)
            signal = np.mean(signal) / (iphtot / (respinterp))
            '''Convert flux into nb of e_ because CCD counts e-, then to QE'''
            logging.info('calib : ' + str(w) + str(signal))

            logging.debug('Normalizing flat ' + img + ' by photodiode QE')
            if (self.spectrum.plot):
                if (self.spectrum.orientation == 'x'):
                    syntheticflat[:, int(pixel)] = signal
                if (self.spectrum.orientation == 'y'):
                    syntheticflat[int(pixel), :] = signal

            for i in range(int(ysize)):
                pointsX.append(int(pixel))
                pointsY.append(i)
                values.append(signal)

        values = values / max(values)
        ''' Build the synthetic flat'''
        points = np.vstack((np.array(pointsY), np.array(pointsX))).T
        values = (np.array(values)).T
        grid_x, grid_y = np.mgrid[0:ysize, 0:xsize]
        interp_map = griddata(points,
                              values, (grid_x, grid_y),
                              method='linear')
        '''Control plots'''
        if (self.spectrum.plot):
            self.ControlPlot2(values, syntheticflat, interp_map)

        return interp_map
Beispiel #4
0
import os, sys
import pylab as pl
import toolbox as tb
import numpy as np
from croaks import NTuple

if __name__ == "__main__":
    narg = len(sys.argv)
    if narg < 2:
        print "joinlist.py file1 file2"
        print "join points"
        print

    file1 = sys.argv[1]
    file2 = sys.argv[2]
    data1 = tb.readtxt(file1, ['img', 'wght', 'FLUX_AUTO'])
    data2 = tb.readtxt(file2, ['FILENAME', 'WAVELENGTH', 'SPOT_0_FLUX'])
    data1 = np.array(data1).transpose()
    data2 = np.array(data2).transpose()
    #data1 = data1[data1[:, 0].argsort()]
    #data2 = data2[data2[:, 0].argsort()]

    #parsing img number
    for i, item2 in enumerate(data2):
        img = ((item2[0].split("\\")[-1]).strip('.fits').split('_'))[-1]
        data2[i, 0] = img
        print i, data2[i, 0]

    #sys.exit()
    img = []
    wght = []
    for obj in objects:
        for filt in filters:
            colors = iter(cm.rainbow(np.linspace(0, 2, 200)))
            print obj, filt
            fig, axarr = pl.subplots(2, sharex=True)
            nb = 0
            for file in files:
                split = re.split('[_.]', file)
                Object = split[1]
                Filter = split[2]
                img = re.split('/', split[0])[-1]
                if ((obj == Object) and (filt == Filter)):
                    nb += 1
                    #value, keys = tb.readlist(file)
                    values = tb.readtxt(
                        file,
                        ['pixel', 'aper_flux', 'center', 'psf_flux', 'sigma'])
                    pl.ylabel('aper_flux / psf_flux')
                    pl.xlabel('pixel')
                    title = str(obj) + ' ' + str(filt)
                    print 'title :', title, img
                    pl.title(title)
                    color = next(colors)

                    axarr[0].plot(
                        values[0],
                        values[1],
                        color=color,
                        linewidth=1,
                        #linestyle='None',
                        drawstyle='steps',
                        default=None)
    args = parser.parse_args()
    return args


if __name__ == "__main__":
    args = grabargs()
    file1 = args.file
    plot = args.plot
    name = args.value
    site = args.site
    method = args.distrib
    MIN = args.min
    MAX = args.max
    print MIN, MAX
    [hour, jd, value] = tb.readtxt(file1, ['entry', 'jd', 'value'])
    base = 24.  # if sampling is hourly

    if plot:
        fig = pl.figure(1)
        pl.plot(hour / base, value, label=site + ' 2017')
        pl.xlabel('time (days)')
        pl.ylabel(name)
        pl.legend()

    value = value[(value < MAX) & (value > MIN)]

    fig = pl.figure(2)
    n, bins, patches = pl.hist(value,
                               50,
                               normed=True,
            color='k',
            label='amp ' + str(ampli) + ' g = ' + str(round(g, 2)) +
            ' (e/ADU)')
    #pl.plot(meanflat,b*meanflat + c,'b') # show linear part
    #pl.plot(meanflat,( meanflat/b + (1/(1+meanflat*c))), 'r')
    pl.xlabel('flux (ADU)')
    pl.ylabel('variance')
    pl.legend(loc='upper left')
    pl.title('Quadratc fit of the PTC')
    out = [float(ampli), GAIN_HEADER, g, sg, c, c_error, a, a_error, KN]
    return out


if __name__ == '__main__':
    args = grabargs()
    file = args.img
    flux_min = 0.0
    flux_max = 58000.0 * 2.  # it's a pair!
    values = tb.readtxt(file, ['ampl', 'mean_sum', 'rms_diff'])
    values = np.array(values).transpose()
    amps = ('11', '12', '21', '22')
    colors = iter(cm.rainbow(np.linspace(0, 1, 4)))
    for amp in amps:
        ptc = values[(values[:, 0] == int(amp)) & (values[:, 1] >= flux_min) &
                     (values[:, 1] <= flux_max)]
        meanflat = ptc[:, 1]
        variance = ptc[:, 2]**2
        determine_gain(meanflat, variance, amp, colors)

    pl.show()