def standard_calibration(field, CCD, epochs, FILTER):

    radec_file = np.sort(glob.glob("%s/SHARED/%s/%s/CALIBRATIONS/matchRADEC_%s_%s_*-02.npy" %
                            (astropath, field, CCD, field, CCD)), kind='mergesort')

    mRADEC_all = []
    for npy in radec_file: mRADEC_all.append(np.load(npy))
    mRADEC_all = np.asarray(mRADEC_all)
    mRADEC = mRADEC_all[np.argmin(mRADEC_all[:,2])]
    wheather_all = []
    print epochs

    for epoch in epochs[:,0]:
        print 'Loading epoch %s' % (epoch)

        #################################### loading catalogs, fits and match files ###################################
        imag_file = "%s/DATA/%s/%s/%s_%s_%s_image_crblaster.fits" % (astropath, field, CCD, field, CCD, epoch)
        if not os.path.exists(imag_file):
            print 'No image file: %s' % (imag_file)
            continue

        cata_file = "%s/catalogues/%s/%s/%s_%s_%s_image_crblaster_thresh%s_minarea%s_backsize64_zp.dat" % \
                            (jorgepath, field, CCD, field, CCD, epoch, str(thresh), str(minarea))
        if not os.path.exists(cata_file):
            print 'No catalog file: %s' % (cata_file)
            continue
        cata = Table.read(cata_file, format = 'ascii')
        #sys.exit()

        ##################################### Reading image and header ################################################

        print 'Applying transformation from ADU to MAG'
        hdufits = fits.open(imag_file)
        GAIN = float(hdufits[0].header['GAINA'])
        AIRMASS = float(hdufits[0].header['AIRMASS'])
        SCALE = float(hdufits[0].header['PIXSCAL1'])
        FWHM_p = float(hdufits[0].header['FWHM'])
        SEEING = FWHM_p*SCALE
        EXP_TIME = float(hdufits[0].header['EXPTIME'])
        MJD = float(hdufits[0].header['MJD-OBS'])
        FILTER = hdufits[0].header['FILTER'][0]
        CCDID = int(hdufits[0].header['CCDNUM'])
        CTE_file = np.loadtxt('%s/sextractor/zeropoint/psmFitDES-mean-%s.csv' % (jorgepath, FILTER),
                                delimiter = ',', skiprows = 1, usecols = [4,5,6,7,8,9,10,11,19], dtype = str)
        Ag = float(CTE_file[CCDID-1][2])
        err_Ag = float(CTE_file[CCDID-1][3])
        Kg = float(CTE_file[CCDID-1][6])
        err_Kg = float(CTE_file[CCDID-1][7])

        wheather_all.append([epoch, AIRMASS, SEEING])

        sigma1, alpha, beta = empty_aperture(field, CCD, epoch, n_aper = 100, verbose = False)
        if sigma1 != None:
            num_pix = np.pi * cata['A_IMAGE'] * cata['B_IMAGE'] * cata['KRON_RADIUS']**2
            new_flux_err = np.sqrt(sigma1**2 * alpha**2 * num_pix**beta + cata['FLUX_AUTO']/GAIN)

        ########################## Transformation from ADU to magnitudes and errors ###################################

        mag_auto = -2.5 * np.log10(cata['FLUX_AUTO']) + 2.5 * np.log10(EXP_TIME) - Ag
        mag_auto_err = np.sqrt(((2.5*cata['FLUXERR_AUTO'])/(cata['FLUX_AUTO'][:]*np.log(10)))**2 + err_Ag**2)

        new_mag_auto_zac = -2.5 * np.log10(cata['FLUX_AUTO']) + 2.5 * np.log10(EXP_TIME) - Ag - Kg * AIRMASS
        new_mag_auto_zac_err = np.sqrt(((2.5 * cata['FLUXERR_AUTO']) / (cata['FLUX_AUTO'] * np.log(10))) **2 +
                                        err_Ag **2 + (AIRMASS * err_Kg) **2)
        if sigma1 != None:
            new_mag_auto_zac_err_cor = np.sqrt(((2.5 * new_flux_err) / (cata['FLUX_AUTO'] * np.log(10))) **2 +
                                                err_Ag **2 + (AIRMASS * err_Kg) **2)

        print '\tTransformation Done!'
        print cata.colnames

        if sigma1 != None:
            print 'Mean absolute deviation of FLUX_ERR_COR \t%0.6f' % \
                    np.mean(np.abs(cata['FLUXERR_AUTO'] - new_flux_err))
        print 'Mean absolute deviation of MAG_AUTO \t\t%0.6f' % np.mean(np.abs(mag_auto - new_mag_auto_zac))
        print 'Mean absolute deviation of ERR_MAG_AUTO \t%0.6f' % np.mean(np.abs(mag_auto_err - new_mag_auto_zac_err))
        if sigma1 != None:
            print 'Mean absolute deviation of ERR_MAG_AUTO_COR \t%0.6f' % \
                    np.mean(np.abs(new_mag_auto_zac_err - new_mag_auto_zac_err_cor))

        # cata.remove_columns(['MAG_AUTO', 'MAGERR_AUTO'])
        # cata.remove_columns(['FLUXERR_AUTO_COR','MAG_AUTO_AIRMASS','MAGERR_AUTO_AIRMASS','MAGERR_AUTO_AIRMASS_COR'])
        if sigma1 != None:
            cata.add_column(Column(new_flux_err, name = 'FLUXERR_AUTO_COR', unit = u.count), index = 7)
        # cata.add_column(Column(mag_auto, name = 'MAG_AUTO', unit = u.mag), index = 8)
        # cata.add_column(Column(mag_auto, name = 'MAGERR_AUTO', unit = u.mag), index = 9)
        cata['MAG_AUTO'] = mag_auto
        cata['MAGERR_AUTO'] = mag_auto_err
        cata.add_column(Column(new_mag_auto_zac, name = 'MAG_AUTO_ZAC', unit = u.mag), index = 9)
        cata.add_column(Column(new_mag_auto_zac_err, name = 'MAGERR_AUTO_ZAC', unit = u.mag), index = 11)
        if sigma1 != None:
            cata.add_column(Column(new_mag_auto_zac_err_cor, name = 'MAGERR_AUTO_ZAC_COR', unit = u.mag), index = 12)

        print cata.colnames

        new_cata_file = cata_file
        cata.write(new_cata_file, format = 'ascii.commented_header', delimiter = '\t')
        print '_______________________________________________________________________________________'
    return wheather_all
def standard_calibration_color(field, CCD, epochs, FILTER, color_table):

    radec_file = np.sort(glob.glob("%s/SHARED/%s/%s/CALIBRATIONS/matchRADEC_%s_%s_*-02.npy" %
                                    (astropath, field, CCD, field, CCD)), kind='mergesort')

    mRADEC_all = []
    for npy in radec_file: mRADEC_all.append(np.load(npy))
    mRADEC_all = np.asarray(mRADEC_all)
    mRADEC = mRADEC_all[np.argmin(mRADEC_all[:,2])]
    wheather_all = []
    print epochs

    for epoch in epochs[:,0]:
        print 'Loading epoch %s' % (epoch)

        ################################### loading catalogs, fits and match files ####################################
        imag_file = "%s/DATA/%s/%s/%s_%s_%s_image_crblaster.fits" % (astropath, field, CCD, field, CCD, epoch)
        if not os.path.exists(imag_file):
            print 'No image file: %s' % (imag_file)
            continue

        cata_file = "%s/catalogues/%s/%s/%s_%s_%s_image_crblaster_thresh%s_minarea%s_backsize64_zp.dat" % \
                        (jorgepath, field, CCD, field, CCD, epoch, str(thresh), str(minarea))
        if not os.path.exists(cata_file):
            print 'No catalog file: %s' % (cata_file)
            continue
        cata = Table.read(cata_file, format = 'ascii')
        print 'Length of catalogue: %i' % (len(cata))
        #sys.exit()

        ######################################## Reading image and header #############################################

        print 'Applying transformation from ADU to MAG'
        hdufits = fits.open(imag_file)
        GAIN = float(hdufits[0].header['GAINA'])
        AIRMASS = float(hdufits[0].header['AIRMASS'])
        SCALE = float(hdufits[0].header['PIXSCAL1'])
        FWHM_p = float(hdufits[0].header['FWHM'])
        SEEING = FWHM_p*SCALE
        EXP_TIME = float(hdufits[0].header['EXPTIME'])
        MJD = float(hdufits[0].header['MJD-OBS'])
        FILTER = hdufits[0].header['FILTER'][0]
        CCDID = int(hdufits[0].header['CCDNUM'])
        CTE_file = np.loadtxt('%s/sextractor/zeropoint/psmFitDES-mean-%s.csv' %
                    (jorgepath, FILTER), delimiter = ',', skiprows = 1, usecols = [4,5,6,7,8,9,10,11,19], dtype = str)
        Ag = float(CTE_file[CCDID-1][2])
        err_Ag = float(CTE_file[CCDID-1][3])
        Kg = float(CTE_file[CCDID-1][6])
        err_Kg = float(CTE_file[CCDID-1][7])
        bg = float(CTE_file[CCDID-1][4])
        err_bg = float(CTE_file[CCDID-1][4])
        g_r0 = bg = float(CTE_file[CCDID-1][8])

        wheather_all.append([epoch, AIRMASS, SEEING])

		########################################### open color table ##################################################

        tree_XY_color = cKDTree(np.transpose([color_table['X'],color_table['Y']]))
        XY_cata = np.transpose([cata['X_IMAGE'], cata['Y_IMAGE']])
        superpos_ind = tree_XY_color.query(XY_cata, k = 1, distance_upper_bound=4)[1]
        index_filter = (superpos_ind < len(color_table)) # dice los obj de single epoch encontrados en color
        index = superpos_ind[index_filter]			     # indices de los correspondientes colores

        print len(color_table['Median_g'][index])
        print len(cata['FLUX_AUTO'])

        if FILTER == 'g':
            if len(color_table['Median_g'][index]) != len(cata['FLUX_AUTO']):
                color_mask_aux = []
                g_r = []
                for l, bol in enumerate(index_filter):
                    if bol:
                        position = superpos_ind[l]
                        if color_table['Median_r'][position] != 0.:
                            color_mask_aux.append(True)
                            g_r.append(color_table['Median_g'][position] - color_table['Median_r'][position])
                        else:
                            color_mask_aux.append(False)
                            g_r.append(None)
                    else:
                        color_mask_aux.append(False)
                        g_r.append(None)
                color_mask = np.array(color_mask_aux)
                g_r = np.array(g_r)
            else:
                color_mask = (color_table['Median_r'][index] != 0.)
                g_r = np.array(color_table['Median_g'][index] - color_table['Median_r'][index])

        if FILTER == 'r':
            color_mask_aux = []
            g_r = []
            for l, bol in enumerate(index_filter):
                if bol:
                    position = superpos_ind[l]
                    if color_table['Median_r'][position] != 0.:
                        color_mask_aux.append(True)
                        g_r.append(color_table['Median_g'][position] - color_table['Median_r'][position])
                    else:
                        color_mask_aux.append(False)
                        g_r.append(None)
                else:
                    color_mask_aux.append(False)
                    g_r.append(None)
            color_mask = np.array(color_mask_aux)
            g_r = np.array(g_r)

        print len(g_r)
        print len(cata['FLUX_AUTO'])
        ########################################### new errors estimation #############################################

        sigma1, alpha, beta = empty_aperture(field, CCD, epoch, n_aper = 100, verbose = False)
        if sigma1 != None:
            num_pix = np.pi * cata['A_IMAGE'] * cata['B_IMAGE'] * cata['KRON_RADIUS']**2
            new_flux_err = np.sqrt(sigma1**2 * alpha**2 * num_pix**beta + cata['FLUX_AUTO']/GAIN)

        ############################ Transformation from ADU to magnitudes and errors #################################

        print 'Empty aperture done!'
        mag_auto = -2.5*np.log10(cata['FLUX_AUTO']) + 2.5*np.log10(EXP_TIME) - Ag
        mag_auto_err = np.sqrt(((2.5*cata['FLUXERR_AUTO'])/(cata['FLUX_AUTO'][:]*np.log(10)))**2 + err_Ag**2)

        new_mag_auto_zac = np.zeros (len(cata['FLUX_AUTO']))
        new_mag_auto_zac_err = np.zeros (len(cata['FLUX_AUTO']))
        if sigma1 != None: new_mag_auto_zac_err_cor = np.zeros (len(cata['FLUX_AUTO']))
        for kk in range(len(cata['FLUX_AUTO'])):
            if color_mask[kk]:
                new_mag_auto_zac[kk] = -2.5 * np.log10(cata['FLUX_AUTO'][kk]) + 2.5 * np.log10(EXP_TIME) - \
                                                    Ag - Kg * AIRMASS - bg * (g_r[kk] - g_r0)
                new_mag_auto_zac_err[kk] = np.sqrt(((2.5 * cata['FLUXERR_AUTO'][kk]) / (cata['FLUX_AUTO'][kk] *
                                        np.log(10))) **2 + err_Ag **2 + (AIRMASS * err_Kg ) **2)# + (g_r[kk]*err_bg)**2)
                if sigma1 != None:
                    new_mag_auto_zac_err_cor[kk] = np.sqrt(((2.5 * new_flux_err[kk]) / (cata['FLUX_AUTO'][kk] *
                                        np.log(10))) **2 + err_Ag **2 + (AIRMASS * err_Kg)**2)# + (g_r[kk]*err_bg)**2)
            else:
                new_mag_auto_zac[kk] = -2.5 * np.log10(cata['FLUX_AUTO'][kk]) + 2.5 * np.log10(EXP_TIME) -\
                                        Ag - Kg * AIRMASS
                new_mag_auto_zac_err[kk] = np.sqrt(((2.5 * cata['FLUXERR_AUTO'][kk]) / (cata['FLUX_AUTO'][kk] *
                                                    np.log(10))) **2 + err_Ag **2 + (AIRMASS * err_Kg) **2)
                if sigma1 != None:
                    new_mag_auto_zac_err_cor[kk] = np.sqrt(((2.5 * new_flux_err[kk]) / (cata['FLUX_AUTO'][kk] *
                                        np.log(10))) **2 + err_Ag **2 + (AIRMASS * err_Kg) **2)

        print '\tTransformation Done!'
        print cata.colnames

        if sigma1 != None:
             print 'Mean absolute deviation of FLUX_ERR_COR \t%0.6f' % \
                    np.mean(np.abs(cata['FLUXERR_AUTO'] - new_flux_err))
        print 'Mean absolute deviation of MAG_AUTO \t\t%0.6f' % np.mean(np.abs(mag_auto - new_mag_auto_zac))
        print 'Mean absolute deviation of ERR_MAG_AUTO \t%0.6f' % np.mean(np.abs(mag_auto_err - new_mag_auto_zac_err))
        if sigma1 != None:
             print 'Mean absolute deviation of ERR_MAG_AUTO_COR \t%0.6f' % \
                    np.mean(np.abs(new_mag_auto_zac_err - new_mag_auto_zac_err_cor))

        # cata.remove_columns(['MAG_AUTO', 'MAGERR_AUTO'])
        # cata.remove_columns(['FLUXERR_AUTO_COR','MAG_AUTO_AIRMASS','MAGERR_AUTO_AIRMASS','MAGERR_AUTO_AIRMASS_COR'])
        if sigma1 != None:
            cata.add_column(Column(new_flux_err, name = 'FLUXERR_AUTO_COR', unit = u.count), index = 7)
        # cata.add_column(Column(mag_auto, name = 'MAG_AUTO', unit = u.mag), index = 8)
        # cata.add_column(Column(mag_auto, name = 'MAGERR_AUTO', unit = u.mag), index = 9)
        cata['MAG_AUTO'] = mag_auto
        cata['MAGERR_AUTO'] = mag_auto_err
        cata.add_column(Column(new_mag_auto_zac, name = 'MAG_AUTO_ZAC', unit = u.mag), index = 9)
        cata.add_column(Column(new_mag_auto_zac_err, name = 'MAGERR_AUTO_ZAC', unit = u.mag), index = 11)
        if sigma1 != None:
            cata.add_column(Column(new_mag_auto_zac_err_cor, name = 'MAGERR_AUTO_ZAC_COR', unit = u.mag), index = 12)

        print cata.colnames

        new_cata_file = cata_file
        cata.write(new_cata_file, format = 'ascii.commented_header', delimiter = '\t')
        print '_______________________________________________________________________________________'
    return wheather_all