lstss[omnifitss.keys()[i]] = lstss[omnifitss.keys()[i]][:-1:2]
        half_len_t = len(lstss[omnifitss.keys()[i]])
        dshape = omnifitss[omnifitss.keys()[i]]['xx'].shape
        for pol in ['xx', 'xy', 'yx', 'yy']:
            omnifitss[omnifitss.keys()[i]][pol] = np.nanmean(omnifitss[omnifitss.keys()[i]][pol].reshape((half_len_t, 2, dshape[1], dshape[2])), axis=1)
            omnivarss[omnifitss.keys()[i]][pol] = np.nanmean(omnivarss[omnifitss.keys()[i]][pol].reshape((half_len_t, 2, dshape[1], dshape[2])), axis=1) / 2

###start matching two chunks
for i in range(2):
    if (lstss[omnifitss.keys()[1-i]][[0, -1]] < lstss[omnifitss.keys()[i]][[0, -1]]).all():#pick the later chunk of the 2, and match it to the previous chunk
        overlap_start_index = np.argmin(np.abs(lstss[omnifitss.keys()[1-i]] - lstss[omnifitss.keys()[i]][0]))
        overlap_len = len(lstss[omnifitss.keys()[1-i]]) - overlap_start_index


        #fit for phase first, all freq at once
        phase_cal = omni.solve_phase_degen(omnifitss[omnifitss.keys()[i]]['xx'][:overlap_len].reshape((overlap_len * dshape[1], dshape[2])), omnifitss[omnifitss.keys()[i]]['yy'][:overlap_len].reshape((overlap_len * dshape[1], dshape[2])), omnifitss[omnifitss.keys()[1-i]]['xx'][overlap_start_index:].reshape((overlap_len * dshape[1], dshape[2])), omnifitss[omnifitss.keys()[1-i]]['yy'][overlap_start_index:].reshape((overlap_len * dshape[1], dshape[2])), ubls, [3, 3, 1e3], plot=False)
        print phase_cal,
        if not np.isnan(phase_cal).any():
            for pol in ['xx', 'xy', 'yx', 'yy']:
                omnifitss[omnifitss.keys()[i]][pol] *= np.exp(1.j * ubls[:, :2].dot(phase_cal))
        print omni.solve_phase_degen(omnifitss[omnifitss.keys()[i]]['xx'][:overlap_len].reshape((overlap_len * dshape[1], dshape[2])), omnifitss[omnifitss.keys()[i]]['yy'][:overlap_len].reshape((overlap_len * dshape[1], dshape[2])), omnifitss[omnifitss.keys()[1-i]]['xx'][overlap_start_index:].reshape((overlap_len * dshape[1], dshape[2])), omnifitss[omnifitss.keys()[1-i]]['yy'][overlap_start_index:].reshape((overlap_len * dshape[1], dshape[2])), ubls, [3, 3, 1e3], plot=False)


        #fit for amp
        amps = {}
        for pol in ['xx', 'yy']:
            a = np.concatenate((np.real(omnifitss[omnifitss.keys()[i]][pol][:overlap_len].reshape((overlap_len * dshape[1], dshape[2]))), np.imag(omnifitss[omnifitss.keys()[i]][pol][:overlap_len].reshape((overlap_len * dshape[1], dshape[2]))))).flatten()
            b = np.concatenate((np.real(omnifitss[omnifitss.keys()[1-i]][pol][overlap_start_index:].reshape((overlap_len * dshape[1], dshape[2]))), np.imag(omnifitss[omnifitss.keys()[1-i]][pol][overlap_start_index:].reshape((overlap_len * dshape[1], dshape[2]))))).flatten()
            nan_mask = np.isnan(a+b)
            a = a[~nan_mask]
            b = b[~nan_mask]
            phase_degen_niter += 1
            b[:, 0] = b[:, 0] * np.exp(1.j * ubls['x'][:, :2].dot(phase_degen_iterative_x))[:, None]
            b[:, -1] = b[:, -1] * np.exp(1.j * ubls['y'][:, :2].dot(phase_degen_iterative_y))[:, None]
            realb = np.concatenate((np.real(b.flatten()), np.imag(b.flatten())))

            psol = realAtNiAinv.dot(np.transpose(realA).dot(realNi * realb))
            realb_fit = realA.dot(psol)
            perror = ((realb_fit - realb) * (realNi**.5)).reshape((2, np.sum(cal_ubl_mask), 2, np.sum(cal_time_mask)))

            realbfit_noadditive = realA[:, 0] * psol[0]
            realbfit_additive = realb_fit - realbfit_noadditive
            realb_noadditive = realb - realbfit_additive
            bfit_noadditive = tocomplex(realbfit_noadditive)
            b_noadditive = tocomplex(realb_noadditive)
            if phase_degen_niter == phase_degen_niter_max:
                phase_degen_iterative_x = omni.solve_phase_degen(np.transpose(b_noadditive[:, 0]), np.transpose(b_noadditive[:, 0]), np.transpose(bfit_noadditive[:, 0]), np.transpose(bfit_noadditive[:, 0]), ubls['x'], [3, 3, 1e3])
                phase_degen_iterative_y = omni.solve_phase_degen(np.transpose(b_noadditive[:, -1]), np.transpose(b_noadditive[:, -1]), np.transpose(bfit_noadditive[:, -1]), np.transpose(bfit_noadditive[:, -1]), ubls['y'], [3, 3, 1e3])

            else:
                phase_degen_iterative_x = solve_phase_degen(np.transpose(b_noadditive[:, 0]), np.transpose(b_noadditive[:, 0]), np.transpose(bfit_noadditive[:, 0]), np.transpose(bfit_noadditive[:, 0]), ubls['x'])
                phase_degen_iterative_y = solve_phase_degen(np.transpose(b_noadditive[:, -1]), np.transpose(b_noadditive[:, -1]), np.transpose(bfit_noadditive[:, -1]), np.transpose(bfit_noadditive[:, -1]), ubls['y'])
            phase_degen2['x'] += phase_degen_iterative_x
            phase_degen2['y'] += phase_degen_iterative_y
            print phase_degen_niter, phase_degen2['x'], phase_degen2['y'], np.linalg.norm(perror)

        renorm = 1 / (2 * psol[0])

        print vis_freq, renorm, phase_degen2['x'], phase_degen2['y']

        freqs[fi] = vis_freq
        if len(pick_fs) == 1:
            for i in range(info['nUBL']):
                pol = 'xx'
                plt.subplot(10,info['nUBL']/10+1,i+1)
                u=np.argsort(info['ublcount'])[i]
                plt.scatter((lsts-3)%TPI+3,np.abs(simdata[pol][:,u]), color='g')
                plt.scatter((lsts-3)%TPI+3,np.abs(ampdata[pol][:,u]))
                plt.title(info['ubl'][u]);plt.xlim(4.5,6.5)

            plt.show()

        ###start solving for phase

        #sooolve
        phase_cal = solve_phase_degen(ampdata['xx'][np.ix_(cal_time_mask, cal_ubl_mask)], ampdata['yy'][np.ix_(cal_time_mask, cal_ubl_mask)], simdata['xx'][np.ix_(cal_time_mask, cal_ubl_mask)], simdata['yy'][np.ix_(cal_time_mask, cal_ubl_mask)], info['ubl'][cal_ubl_mask], plot=(len(pick_fs) == 1))
        omni_phase_cal= omni.solve_phase_degen(ampdata['xx'][np.ix_(cal_time_mask, cal_ubl_mask)], ampdata['yy'][np.ix_(cal_time_mask, cal_ubl_mask)], simdata['xx'][np.ix_(cal_time_mask, cal_ubl_mask)], simdata['yy'][np.ix_(cal_time_mask, cal_ubl_mask)], info['ubl'][cal_ubl_mask], [3, 3, 1e3], plot=(len(pick_fs) == 1))
        print phase_cal, 'new', omni_phase_cal


        ###############################################
        ##############compress in time dimension
        ############################################
        tcompress_fac = int(np.round(5.79e-5 * 25 / np.median(jds[1:]-jds[:-1])))#25

        if compress_method == 'deconvolve':
            tcompress = len(jds) / tcompress_fac / 2 * 2 + 1
            compressed2_data = np.empty((4, tcompress, compressed_data.shape[-1]), dtype = 'complex64')
            compressed2_var = np.empty((4, tcompress, compressed_data.shape[-1]), dtype = 'float32')
            compressed2_flag = np.zeros(tcompress, dtype='bool')
            for p in range(4):
                compressed2_result = omni.deconvolve_spectra2(compressed_data[p,:,pick_f], ~compr_flag[:, pick_f], (tcompress + 1)/2, correction_weight = 1e-6, var = compr_var[p, :, pick_f, 0])