Example #1
0
    def createCube_forTestDataInOpdImages(self):
        tt = '20191210_143625'
        fold = os.path.join(Configuration.OPD_DATA_FOLDER,
                            'OPDImages', tt, 'hdf5')

        hduList = pyfits.open(os.path.join(fold, 'ampVect.fits'))
        self._cmdAmplitude = hduList[0].data
        hduList = pyfits.open(os.path.join(fold, 'modeRange.fits'))
        self._actsVector = hduList[0].data
        hduList = pyfits.open(os.path.join(fold, 'template.fits'))
        vector_of_push_pull = hduList[0].data

        cube = None
        tip_til_det = TipTiltDetrend()
        r = ROI()
#         for i in range(self._actsVector.shape[0]):
        for i in range(7):
            k = i * vector_of_push_pull.shape[0]
            imaList = []
            for j in range(vector_of_push_pull.shape[0]):
                l = k + j
                file_name = os.path.join(fold, 'img_%04d.h5') %l
                img = self._ic.from4D(file_name)
                roi = r.roiGenerator(img)
                ima_tt = tip_til_det.tipTiltRemover(img, roi, 3)
                imaList.append(ima_tt)

            image = imaList[0] + imaList[1] - imaList[2]

            if cube is None:
                cube = image
            else:
                cube = np.ma.dstack((cube, image))

        return cube
Example #2
0
 def __init__(self):
     """The constructor """
     self._maskthreshold = 5
     self._rmsthreshold = 3
     self._logger = logging.getLogger('CALIBALL:')
     self._ic = InterferometerConverter()
     self._zOnM4 = ZernikeOnM4()
     self._ttd = TipTiltDetrend()
Example #3
0
    def createCubeFromImageFolder(self, data_file_path, tiptilt_detrend=None, phase_ambiguity=None):
        cube_all_act = None
        where = self._indexReorganization()
        ampl_reorg = self._amplitudeReorganization(self._actsVector,
                                                   self._indexingList,
                                                   self._cmdAmplitude,
                                                   self._nPushPull)
        for i in range(self._actsVector.shape[0]):
            for k in range(self._nPushPull):
                p = self._nPushPull * i + k
                n = where[p][0][0]
                mis_amp = k* self._indexingList.shape[1] + n
                mis = k * self._indexingList.shape[1] * self._template.shape[0] \
                        + n * self._template.shape[0]

                name = 'img_%04d.h5' %mis
                file_name = os.path.join(data_file_path, name)
                image_for_dim = self._ic.from4D(file_name)

                image_sum = np.zeros((image_for_dim.shape[0],
                                     image_for_dim.shape[1]))
                for l in range(1, self._template.shape[0]):
                    name = 'img_%04d.h5' %(mis+l)
                    file_name = os.path.join(data_file_path, name)
                    ima = self._ic.from4D(file_name)
                    image = image_sum + ima * self._template[l]
                img_if = image / (2 * ampl_reorg[mis_amp] * (self._template.shape[0] - 1))
                if tiptilt_detrend is None:
                    img_if = img_if
                else:
                    r = ROI()
                    roi = r.roiGenerator(img_if)
                    tt = TipTiltDetrend()
                    img_if = tt.tipTiltRemover(img_if, roi, 3)

                if_push_pull_kth = img_if-np.ma.median(img_if)

                if k == 0:
                    all_push_pull_act_jth = if_push_pull_kth
                else:
                    all_push_pull_act_jth = np.ma.dstack((all_push_pull_act_jth,
                                                          if_push_pull_kth))

            if self._nPushPull == 1:
                if_act_jth = all_push_pull_act_jth
            else:
                if_act_jth = np.ma.mean(all_push_pull_act_jth, axis=2)

            if cube_all_act is None:
                cube_all_act = if_act_jth
            else:
                cube_all_act = np.ma.dstack((cube_all_act, if_act_jth))
        self._cube = cube_all_act

        return self._cube
Example #4
0
    def __init__(self, folder_name):
        """The constructor """
        self._folderName = folder_name
        self._maskthreshold = 1000 #5
        self._rmsthreshold = 3
        self._logger = logging.getLogger('CALIBALL:')
        self._ic = InterferometerConverter()
        self._ttd = TipTiltDetrend()

        self._cube = None
        self._cube_ttr = None
Example #5
0
    def createCube(self, tiptilt_detrend=None, phase_ambiguity=None):
        '''
            Args:
                ttDetrend = in the creation of the cube the images are reduced
                            removing tip tilt on the central segment

                phaseSolve=

            Returns:
                self._cube = cube
        '''
        cube_all_act = None
        self._ttData()
        self._logCubeCreation(tiptilt_detrend, phase_ambiguity)   #modRB here add the Zernike removal or best decide how to manage the Zernike shapes, if to remove them in flattening only
        where = self._indexReorganization()                       #modRB here add the image immersion in the full frame
        #misure_pos, misure_neg = self._splitMeasureFromFits(self._cubeMeasure)
        ampl_reorg = self._amplitudeReorganization(self._actsVector,
                                                   self._indexingList,
                                                   self._cmdAmplitude,
                                                   self._nPushPull)


        for i in range(self._actsVector.shape[0]):
            for k in range(self._nPushPull):
                p = self._nPushPull * i + k
                n = where[p][0][0]
                mis_amp = k* self._indexingList.shape[1] + n
                mis = k * self._indexingList.shape[1] * self._template.shape[0] \
                        + n * self._template.shape[0]

                #img_pos = misure_pos[:,:,mis]
                #img_neg = misure_neg[:,:,mis]
                image_sum = np.zeros((self._cubeMeasure.shape[0],
                                     self._cubeMeasure.shape[1]))
                for l in range(1, self._template.shape[0]):
                    image = image_sum + self._cubeMeasure[:,:, mis+l] * self._template[l]
                img_if = image / (2 * ampl_reorg[mis_amp] * (self._template.shape[0] - 1))
                if tiptilt_detrend is None:
                    img_if = img_if
                else:
                    r = ROI()
                    roi = r.roiGenerator(img_if)
                    tt = TipTiltDetrend()
                    img_if = tt.tipTiltRemover(img_if, roi, 3)

                if_push_pull_kth = img_if-np.ma.median(img_if)

                if k == 0:
                    all_push_pull_act_jth = if_push_pull_kth
                else:
                    all_push_pull_act_jth = np.ma.dstack((all_push_pull_act_jth,
                                                          if_push_pull_kth))

            if self._nPushPull == 1:
                if_act_jth = all_push_pull_act_jth
            else:
                if_act_jth = np.ma.mean(all_push_pull_act_jth, axis=2)

            if cube_all_act is None:
                cube_all_act = if_act_jth
            else:
                cube_all_act = np.ma.dstack((cube_all_act, if_act_jth))
        self._cube = cube_all_act

        return self._cube
Example #6
0
    def createCubeFromImageFolder(self, data_file_path=None, tiptilt_detrend=None,
                                  phase_ambiguity=None):
        '''
        Parameters
        ----------
                data_file_path: string
                                measurement data file path

        Other Parameters
        ----------
                ttDetrend: optional
                            in the creation of the cube the images are reduced
                            removing tip tilt on the central segment

                phaseSolve: optional

        Returns
        -------
                cube = masked array [pixels, pixels, number of images]
                        cube from analysis
        '''
        if data_file_path is None:
            data_file_path = self._h5Folder #viene da loadInfoFromIFFsTtFolder
        else:
            data_file_path = data_file_path
        
        where = self._indexReorganization()
        ampl_reorg = self._amplitudeReorganization(self._actsVector,
                                                   self._indexingList,
                                                   self._cmdAmplitude,
                                                   self._nPushPull)
        cube_all_act = []
        for i in range(self._actsVector.shape[0]):
            print(i)
            for k in range(self._nPushPull):
                p = self._nPushPull * i + k
                n = where[p]
                mis_amp = k* self._indexingList.shape[1] + n
                mis_push = (k * self._indexingList.shape[1]+ n) * 2*self._template.shape[0]
                mis_pull = self._template.shape[0] + mis_push
                mis_list = [mis_push, mis_pull]

                pp_images = []
                for mis in mis_list:
                    name = 'img_%04d.h5' %mis
                    file_name = os.path.join(data_file_path, name)
                    image0 = self._ic.from4D(file_name)

#                 image_sum = np.zeros((image_for_dim.shape[0],
#                                       image_for_dim.shape[1]))
                    image_list = [image0]
                    for l in range(1, self._template.shape[0]):
                        name = 'img_%04d.h5' %(mis+l)
                        file_name = os.path.join(data_file_path, name)
                        ima = self._ic.from4D(file_name)
                        image_list.append(ima)

                    image = np.zeros((image0.shape[0], image0.shape[1]))
                    for p in range(1, len(image_list)):
                        #opd2add   = opdtemp[*,*,p]*form[p]+opdtemp[*,*,p-1]*form[p-1]
                        #opd      += opd2add
                        opd2add = image_list[p] * self._template[p] + image_list[p-1] * self._template[p-1]
                        master_mask2add = np.ma.mask_or(image_list[p].mask, image_list[p-1].mask)
                        if p==1:
                            master_mask = master_mask2add
                        else:
                            master_mask = np.ma.mask_or(master_mask, master_mask2add)
                        image += opd2add
                    image = np.ma.masked_array(image, mask=master_mask)
                    #image = image + ima * self._template[l] #sbagliato
                    pp_images.append(image)
                image = pp_images[0] + pp_images[1] #da rivedere molto
                img_if = image / (2 * ampl_reorg[mis_amp] * (self._template.shape[0] - 1))
                if tiptilt_detrend is None:
                    img_if = img_if
                else:
                    r = ROI()
                    roi = r.roiGenerator(img_if)
                    tt = TipTiltDetrend()
                    img_if = tt.tipTiltDetrend(img_if, roi, 3)

                if_push_pull_kth = img_if

                if k == 0:
                    all_push_pull_act_jth = if_push_pull_kth
                else:
                    all_push_pull_act_jth = np.ma.dstack((all_push_pull_act_jth,
                                                          if_push_pull_kth))

            if self._nPushPull == 1:
                if_act_jth = all_push_pull_act_jth
            else:
                if_act_jth = np.ma.mean(all_push_pull_act_jth, axis=2)

            cube_all_act.append(if_act_jth)
        self._cube = np.ma.dstack(cube_all_act)
        return self._cube
Example #7
0
class Caliball():
    def __init__(self):
        """The constructor """
        self._maskthreshold = 5
        self._rmsthreshold = 3
        self._logger = logging.getLogger('CALIBALL:')
        self._ic = InterferometerConverter()
        self._zOnM4 = ZernikeOnM4()
        self._ttd = TipTiltDetrend()

    @staticmethod
    def _storageFolder():
        """ Creates the path for measurement data"""
        return os.path.join(Configuration.OPD_DATA_FOLDER, "Caliball")

    def doStat(self):
        cube = self._readCube()
        #cube_ttr = self._readCube(1)
        rs_img = self._readRsImg()
        mask_point = np.zeros(cube.shape[2])
        for i in range(mask_point.shape[0]):
            mask_point[i] = np.sum(np.invert(cube[:, :, i].mask))
        mask_ord = np.sort(mask_point)
        aa = np.where(mask_point == min(mask_point))[0][0]
        bad_dataset = cube[:, :, aa]
        bad_mask = cube[:, :, aa].mask

        plt.plot(np.arange(mask_point.shape[0]), mask_ord, 'o')
        plt.xscale('log')
        plt.ylabel('# valid points')
        plt.xlabel('# frames')
        plt.title('Cumulative plot of mask valid points')

        rs = image_extender._imageExtender(rs_img)
        coef, mat = self._zOnM4.zernikeFit(rs, np.array([2, 3]))
        rs_ttr = self._ttd.ttRemoverFromCoeff(coef, rs)
        r0 = rs_ttr.std()

        cube_ttr = self.readCubeTTR()
        rs_vect = np.zeros(cube_ttr.shape[0])
        for j in range(cube_ttr.shape[0]):
            rs_vect[j] = cube_ttr[j, :, :].std()

        plt.figure()
        plt.plot(np.arange(cube_ttr.shape[0]), rs_vect, label='Data')
        plt.yscale('log')
        plt.plot(np.zeros(cube_ttr.shape[0]) + r0, label='Average')
        plt.ylabel('m RMS')
        plt.xlabel('# frames')
        plt.title('Images WFE')
        plt.legend()
        return bad_dataset, bad_mask

    #plot(x, mask_ord, 'o'); pyplot.xscale('log'); plt.ylabel('# valid points'); plt.xlabel('# frames'); plt.title('Cumulative plot of mask valid points')

    def pixel_std(self):
        cube_ttr = self._readCube(1)
        std_image = cube_ttr.std(axis=2)
        mean_image = cube_ttr.mean(axis=2)

        plt.imshow(std_image, origin='lower')
        plt.colorbar()
        plt.title('Pixel StDev')
        plt.figure()
        plt.imshow(mean_image, origin='lower')
        plt.colorbar()
        plt.title('RS average value')
        return std_image, mean_image

    def doselaverage(self):
        cube_ttr = self._readCube(1)
        mask_point = np.zeros(cube_ttr.shape[2])
        for i in range(mask_point.shape[0]):
            mask_point[i] = np.sum(cube_ttr[:, :, i].mask)
        idx = np.where(mask_point <= (min(mask_point) + self._maskthreshold))

        rs_std = np.zeros(idx[0].shape[0])
        for i in range(idx[0].shape[0]):
            rs_std[i] = np.std(cube_ttr[:, :, idx[0][i]])

        rthresh = np.mean(rs_std) + self._rmsthreshold * np.std(rs_std)
        idr = np.where(rs_std < rthresh)
        r_img = np.mean(cube_ttr[:, :, idr], axis=3)
        std_img = np.std(cube_ttr[:, :, idr], axis=3)
        r_image = r_img[:, :, 0]
        std_image = std_img[:, :, 0]

        plt.imshow(r_image, origin='lower')
        plt.colorbar()
        plt.title('RS measurement error')
        plt.figure()
        plt.imshow(std_image, origin='lower')
        plt.colorbar()
        plt.title('Pixel StDev, thresh = %d' % self._rmsthreshold)
        return r_image, std_image

    # non deve starci idr ma gli idx[idr]

###

    def createImgCubeFile(self):
        path = Caliball._storageFolder()
        total_cube = None
        for j in range(1, 4):
            fold = os.path.join(path, 'test%d' % j)
            cube = self._createMeasurementCube(fold)
            if total_cube is None:
                total_cube = cube
            else:
                total_cube = np.ma.dstack((total_cube, cube))
        self._saveCube(total_cube, 'Total_Cube.fits')
        return total_cube

    def createCubeTTrFromCube(self):
        # ci mette un'eternità a fare l'estenzione dell'immagine
        cube = self._readCube()
        cube_ttr = None
        for i in range(cube.shape[2]):
            image = image_extender._imageExtender(cube[:, :, i])
            coef, mat = self._zOnM4.zernikeFit(image, np.array([2, 3]))
            image_ttr = self._ttd.ttRemoverFromCoeff(coef, image)
            if cube_ttr is None:
                cube_ttr = image_ttr
            else:
                cube_ttr = np.ma.dstack((cube_ttr, image_ttr))
        self._saveCube(cube_ttr, 'Total_Cube_ttr.fits')
        return cube_ttr

    def createCubeTTrFromRuna(self):
        file_name = '/Users/rm/imgcubefit.fits'
        hduList = pyfits.open(file_name)
        cube_runa = hduList[0].data  #(3000, 500, 496)
        cube = self._readCube()
        cube_ttr = None
        for i in range(cube_runa.shape[0]):
            image = np.ma.masked_array(cube_runa[i, :, :],
                                       mask=cube.mask[:, :, i])
            if cube_ttr is None:
                cube_ttr = image
            else:
                cube_ttr = np.ma.dstack((cube_ttr, image))
        return cube_ttr

    def createRsImgFile(self):
        cube = self._readCube()
        mask_point = np.zeros(cube.shape[2])
        for i in range(mask_point.shape[0]):
            mask_point[i] = np.sum(cube[:, :, i].mask)
        idx = np.where(mask_point <= (min(mask_point) + self._maskthreshold))
        tot = idx[0].shape[0]
        image = np.sum(cube[:, :, idx], 3) / tot
        mask = np.prod(cube[:, :, idx].mask, 3)
        rs_img = np.ma.masked_array(image[:, :, 0], mask=mask)

        fits_file_name = os.path.join(Caliball._storageFolder(), 'rs_img.fits')
        pyfits.writeto(fits_file_name, rs_img.data)
        pyfits.append(fits_file_name, rs_img.mask.astype(int))
        return rs_img

    def _createMeasurementCube(self, fold):
        cube = None
        list = os.listdir(fold)
        for i in range(len(list) - 1):
            name = 'img_%04d.h5' % i
            file_name = os.path.join(fold, name)
            ima = self._ic.from4D(file_name)
            if cube is None:
                cube = ima
            else:
                cube = np.ma.dstack((cube, ima))
        return cube

    def _saveCube(self, total_cube, name):
        fits_file_name = os.path.join(Caliball._storageFolder(), name)
        pyfits.writeto(fits_file_name, total_cube.data)
        pyfits.append(fits_file_name, total_cube.mask.astype(int))

    def _readCube(self, ttr=None):
        if ttr is None:
            file_name = os.path.join(Caliball._storageFolder(),
                                     'Total_Cube.fits')
        else:
            #file_name = os.path.join(Caliball._storageFolder(), 'Total_Cube_ttr_runa.fits')
            file_name = os.path.join(Caliball._storageFolder(),
                                     'Total_Cube_ttr.fits')
        hduList = pyfits.open(file_name)
        cube = np.ma.masked_array(hduList[0].data,
                                  hduList[1].data.astype(bool))
        return cube

    def readCubeTTR(self):
        file_name = '/Users/rm/imgcubefit.fits'
        #file_name = '/mnt/cargo/data/M4/OTT-Review/CaliBallTest/imgcubefit.fits'
        hduList = pyfits.open(file_name)
        cube_ttr = hduList[0].data  #(3000, 500, 496)
        return cube_ttr

    def _readRsImg(self):
        file_name = os.path.join(Caliball._storageFolder(), 'rs_img.fits')
        #file_name = '/mnt/cargo/data/M4/OTT-Review/CaliBallTest/RS-img.fits'
        hduList = pyfits.open(file_name)
        rs_img = np.ma.masked_array(hduList[0].data,
                                    hduList[1].data.astype(bool))
        return rs_img
Example #8
0
    def createCubeFromImageFolder(self,
                                  data_file_path,
                                  tiptilt_detrend=None,
                                  phase_ambiguity=None):
        '''
        Parameters
        ----------
                data_file_path: string
                                measurement data file path

        Other Parameters
        ----------
                ttDetrend: optional
                            in the creation of the cube the images are reduced
                            removing tip tilt on the central segment

                phaseSolve: optional

        Returns
        -------
                cube = masked array [pixels, pixels, number of images]
                        cube from analysis
        '''
        cube_all_act = None
        where = self._indexReorganization()
        ampl_reorg = self._amplitudeReorganization(self._actsVector,
                                                   self._indexingList,
                                                   self._cmdAmplitude,
                                                   self._nPushPull)
        for i in range(self._actsVector.shape[0]):
            for k in range(self._nPushPull):
                p = self._nPushPull * i + k
                n = where[p][0][0]
                mis_amp = k * self._indexingList.shape[1] + n
                mis = k * self._indexingList.shape[1] * self._template.shape[0] \
                        + n * self._template.shape[0]

                name = 'img_%04d.h5' % mis
                file_name = os.path.join(data_file_path, name)
                image_for_dim = self._ic.from4D(file_name)

                image_sum = np.zeros(
                    (image_for_dim.shape[0], image_for_dim.shape[1]))
                for l in range(1, self._template.shape[0]):
                    name = 'img_%04d.h5' % (mis + l)
                    file_name = os.path.join(data_file_path, name)
                    ima = self._ic.from4D(file_name)
                    image = image_sum + ima * self._template[l]
                img_if = image / (2 * ampl_reorg[mis_amp] *
                                  (self._template.shape[0] - 1))
                if tiptilt_detrend is None:
                    img_if = img_if
                else:
                    r = ROI()
                    roi = r.roiGenerator(img_if)
                    tt = TipTiltDetrend()
                    img_if = tt.tipTiltDetrend(img_if, roi, 3)

                if_push_pull_kth = img_if - np.ma.median(img_if)

                if k == 0:
                    all_push_pull_act_jth = if_push_pull_kth
                else:
                    all_push_pull_act_jth = np.ma.dstack(
                        (all_push_pull_act_jth, if_push_pull_kth))

            if self._nPushPull == 1:
                if_act_jth = all_push_pull_act_jth
            else:
                if_act_jth = np.ma.mean(all_push_pull_act_jth, axis=2)

            if cube_all_act is None:
                cube_all_act = if_act_jth
            else:
                cube_all_act = np.ma.dstack((cube_all_act, if_act_jth))
        self._cube = cube_all_act

        return self._cube
Example #9
0
    def createCube(self, tiptilt_detrend=None, phase_ambiguity=None):
        '''
        Other Parameters
        ----------
                ttDetrend: optional
                            in the creation of the cube the images are reduced
                            removing tip tilt on the central segment

                phaseSolve: optional

        Returns
        -------
                cube = masked array [pixels, pixels, number of images]
                        cube from analysis
        '''
        cube_all_act = None
        self._ttData()
        self._logCubeCreation(tiptilt_detrend, phase_ambiguity)
        where = self._indexReorganization()
        #misure_pos, misure_neg = self._splitMeasureFromFits(self._cubeMeasure)
        ampl_reorg = self._amplitudeReorganization(self._actsVector,
                                                   self._indexingList,
                                                   self._cmdAmplitude,
                                                   self._nPushPull)

        for i in range(self._actsVector.shape[0]):
            for k in range(self._nPushPull):
                p = self._nPushPull * i + k
                n = where[p][0][0]
                mis_amp = k * self._indexingList.shape[1] + n
                mis = k * self._indexingList.shape[1] * self._template.shape[0] \
                        + n * self._template.shape[0]

                #img_pos = misure_pos[:,:,mis]
                #img_neg = misure_neg[:,:,mis]
                image_sum = np.zeros(
                    (self._cubeMeasure.shape[0], self._cubeMeasure.shape[1]))
                for l in range(1, self._template.shape[0]):
                    image = image_sum + self._cubeMeasure[:, :, mis +
                                                          l] * self._template[l]
                img_if = image / (2 * ampl_reorg[mis_amp] *
                                  (self._template.shape[0] - 1))
                if tiptilt_detrend is None:
                    img_if = img_if
                else:
                    r = ROI()
                    roi = r.roiGenerator(img_if)
                    tt = TipTiltDetrend()
                    img_if = tt.tipTiltDetrend(img_if, roi, 3)

                if_push_pull_kth = img_if - np.ma.median(img_if)

                if k == 0:
                    all_push_pull_act_jth = if_push_pull_kth
                else:
                    all_push_pull_act_jth = np.ma.dstack(
                        (all_push_pull_act_jth, if_push_pull_kth))

            if self._nPushPull == 1:
                if_act_jth = all_push_pull_act_jth
            else:
                if_act_jth = np.ma.mean(all_push_pull_act_jth, axis=2)

            if cube_all_act is None:
                cube_all_act = if_act_jth
            else:
                cube_all_act = np.ma.dstack((cube_all_act, if_act_jth))
        self._cube = cube_all_act

        return self._cube
Example #10
0
class Caliball():
    """
    Class for data analysis.

    HOW TO USE IT::

        from m4.caliball_average import Caliball
        folder_name = 'tt_rotBall_num'
        cal = Caliball(folder_name)
        cal.createDataForAnalysis()
        r_image, std_image = cal.dataSelectionAndAnalysis()

    """

    def __init__(self, folder_name):
        """The constructor """
        self._folderName = folder_name
        self._maskthreshold = 1000 #5
        self._rmsthreshold = 3
        self._logger = logging.getLogger('CALIBALL:')
        self._ic = InterferometerConverter()
        self._ttd = TipTiltDetrend()

        self._cube = None
        self._cube_ttr = None


    @staticmethod
    def _storageFolder():
        """ Creates the path for measurement data"""
        return fold_name.CALIBALL_ROOT_FOLDER

    def createDataForAnalysis(self):
        '''Create file fits for data analysis

        Returns
        ----------
        self._folderName: string
                        folder name for data
        '''
        self._cube = self._createMeasurementCube()
        self._cube_ttr = self._createCubeTTrFromCube()
        rs_ima = self._createRsImgFile()
        return self._folderName

    def validMaskPoint(self):
        '''Create the cumulative plot of mask valid points
        (valid points within each frames)

        Returns
        ----------
        bad_dataset: numpy array
                    worst measurement data
        bad_mask: numpy array
                    worst mask
        '''
        cube = self._readCube()
        #cube_ttr = self._readCube(1)
        mask_point = np.zeros(cube.shape[2])
        for i in range(mask_point.shape[0]):
            mask_point[i] = np.sum(np.invert(cube[:, :, i].mask))
        mask_ord = np.sort(mask_point)
        aa = np.where(mask_point == min(mask_point))[0][0]
        bad_dataset = cube[:, :, aa]
        bad_mask = cube[:, :, aa].mask

        plt.plot(np.arange(mask_point.shape[0]), mask_ord, 'o'); plt.xscale('log')
        plt.ylabel('# valid points'); plt.xlabel('# frames')
        plt.title('Cumulative plot of mask valid points')
        return bad_dataset, bad_mask

    def validFrames(self):
        '''Create the plot of individual RMS of each frames
        (without tip/tilt) together with RMS of the average frame
        '''
        rs_img = self._readRsImg()
        coef, mat = zernike.zernikeFit(rs_img, np.array([2, 3]))
        surf = zernike.zernikeSurface(rs_img, coef, mat)
        rs_ttr = rs_img - surf
        r0 = rs_ttr.std()

        cube_ttr = self._readCube(1)
        rs_vect = np.zeros(cube_ttr.shape[2])
        for j in range(cube_ttr.shape[2]):
            rs_vect[j] = cube_ttr[:, :, j].std()

        plt.figure()
        plt.plot(np.arange(cube_ttr.shape[2]), rs_vect, label='Data'); plt.yscale('log')
        plt.plot(np.zeros(cube_ttr.shape[2]) + r0, label='Average')
        plt.ylabel('m RMS'); plt.xlabel('# frames')
        plt.title('Images WFE'); plt.legend()
    #plot(x, mask_ord, 'o'); pyplot.xscale('log'); plt.ylabel('# valid points');
    #plt.xlabel('# frames'); plt.title('Cumulative plot of mask valid points')

    def pixel_std(self):
        '''
        Create the plot of single pixel stdev and mean along the sequence

        Returns
        -------
        std_image: masked array
                single pixel stdev along the sequence
        mean_image: masked array
                mean along the sequence
        '''
        cube_ttr = self._readCube(1)
        std_image = cube_ttr.std(axis=2)
        mean_image = cube_ttr.mean(axis=2)

        plt.imshow(std_image, origin='lower'); plt.colorbar()
        plt.title('Pixel StDev')
        plt.figure()
        plt.imshow(mean_image, origin='lower'); plt.colorbar()
        plt.title('RS average value')
        return std_image, mean_image

    def dataSelectionAndAnalysis(self):
        '''
        Create the plot of pixel stdev and mean after
        rejecting the frames whit bad mask

        Returns
        -------
        std_image: masked array
                stdev along the sequence
        mean_image: masked array
                mean along the sequence
        '''
        cube_ttr = self._readCube(1)
        mask_point = np.zeros(cube_ttr.shape[2])
        for i in range(mask_point.shape[0]):
            mask_point[i] = np.sum(np.invert(cube_ttr[:, :, i].mask))
        idx = np.where(mask_point >= (max(mask_point) - self._maskthreshold))

        rs_std = np.zeros(idx[0].shape[0])
        for i in range(idx[0].shape[0]):
            rs_std[i] = np.std(cube_ttr[:, :, idx[0][i]])

        rthresh = np.mean(rs_std)+self._rmsthreshold*np.std(rs_std)
        idr = np.where(rs_std < rthresh)

        idxr = idr
        for i in range(idr[0].shape[0]):
            idxr[0][i] = idx[0][idr[0][i]]
        r_img = np.mean(cube_ttr[:, :, idxr], axis=2)
        std_img = np.std(cube_ttr[:, :, idxr], axis=2)
        mask = np.prod(cube_ttr[:, :, idxr].mask, 2)
        r_image = np.ma.masked_array(r_img[:, :, 0], mask=mask[:, :, 0])
        std_image = np.ma.masked_array(std_img[:, :, 0], mask=mask[:, :, 0])

        plt.imshow(r_image, origin='lower'); plt.colorbar()
        plt.title('RS measurement error')
        plt.figure()
        plt.imshow(std_image, origin='lower'); plt.colorbar()
        plt.title('Pixel StDev, thresh = %d' %self._rmsthreshold)
        return r_image, std_image
    # non deve starci idr ma gli idx[idr]

###

    def _createCubeTTrFromCube(self, fitEx=None):
        # ci mette un eternit a fare l estenzione dell immagine
        #cube = self._readCube()
        cube_ttr = None
        if fitEx is None:
            for i in range(self._cube.shape[2]):
                image = self._cube[:, :, i]
                coef, mat = zernike.zernikeFit(image, np.array([2, 3]))
                surf = zernike.zernikeSurface(image, coef, mat)
                image_ttr = image - surf
                if cube_ttr is None:
                    cube_ttr = image_ttr
                else:
                    cube_ttr = np.ma.dstack((cube_ttr, image_ttr))
        else:
            for i in range(self._cube.shape[2]):
                coef, interf_coef = tip_tilt_interf_fit.fit(self._cube[:, :, i])
                image_ttr = self._ttd.ttRemoverFromCoeff(coef, self._cube[:, :, i])
                if cube_ttr is None:
                    cube_ttr = image_ttr
                else:
                    cube_ttr = np.ma.dstack((cube_ttr, image_ttr))

        self._saveCube(cube_ttr, 'Total_Cube_ttr.fits')
        return cube_ttr

    def _createRsImgFile(self):
        cube = self._readCube()
        mask_point = np.zeros(cube.shape[2])
        for i in range(mask_point.shape[0]):
            mask_point[i] = np.sum(cube[:, :, i].mask)
        idx = np.where(mask_point <= (min(mask_point) + self._maskthreshold))
        tot = idx[0].shape[0]
        image = np.sum(cube[:, :, idx], 3) / tot
        mask = np.prod(cube[:, :, idx].mask, 3)
        rs_img = np.ma.masked_array(image[:, :, 0], mask=mask)

        fits_file_name = os.path.join(Caliball._storageFolder(), self._folderName, 'rs_img.fits')
        pyfits.writeto(fits_file_name, rs_img.data)
        pyfits.append(fits_file_name, rs_img.mask.astype(int))
        return rs_img

    def _createMeasurementCube(self):
        cube = None
        fold = os.path.join(Caliball._storageFolder(), self._folderName, 'hdf5')
        list = os.listdir(fold)
        for i in range(len(list)-1):
            name = 'img_%04d.h5' %i
            file_name = os.path.join(fold, name)
            ima = self._ic.from4D(file_name)
            if cube is None:
                cube = ima
            else:
                cube = np.ma.dstack((cube, ima))
        self._saveCube(cube, 'Total_Cube.fits')
        return cube

    def _saveCube(self, total_cube, name):
        fits_file_name = os.path.join(Caliball._storageFolder(), self._folderName, name)
        pyfits.writeto(fits_file_name, total_cube.data)
        pyfits.append(fits_file_name, total_cube.mask.astype(int))

    def _readCube(self, ttr=None):
        if ttr is None:
            file_name = os.path.join(Caliball._storageFolder(), self._folderName,
                                     'Total_Cube.fits')
        else:
            #file_name = os.path.join(Caliball._storageFolder(), 'Total_Cube_ttr_runa.fits')
            file_name = os.path.join(Caliball._storageFolder(), self._folderName,
                                     'Total_Cube_ttr.fits')
        hduList = pyfits.open(file_name)
        cube = np.ma.masked_array(hduList[0].data,
                                  hduList[1].data.astype(bool))
        return cube

    def _readRsImg(self):
        file_name = os.path.join(Caliball._storageFolder(), self._folderName,
                                 'rs_img.fits')
        #file_name = '/mnt/cargo/data/M4/OTT-Review/CaliBallTest/RS-img.fits'
        hduList = pyfits.open(file_name)
        rs_img = np.ma.masked_array(hduList[0].data,
                                    hduList[1].data.astype(bool))
        return rs_img