Ejemplo n.º 1
0
 def calc_pointing(self) :
     """Calculates the telescope pointing.
     
     Should only be used if in alt/az mode. 
     At every time the Ra and Dec of the telescope time is calculated.
     These are stored as attributes (not fields) named ra and dec.  This
     requires the fields 'CRVAL3', 'CRVAL2' and 'DATE-OBS' to be set.
     """
     self.ra = sp.zeros(self.dims[0])
     self.dec = sp.zeros(self.dims[0])
     for ii in range(self.dims[0]) :
         self.ra[ii], self.dec[ii] = utils.elaz2radecGBT(
                                         self.field['CRVAL3'][ii],
                                         self.field['CRVAL2'][ii],
                                         self.field['DATE-OBS'][ii])
Ejemplo n.º 2
0
    def calc_pointing(self):
        """Calculates the telescope pointing.
        
        Should only be used if in alt/az mode. 
        At every time the Ra and Dec of the telescope time is calculated.
        These are stored as attributes (not fields) named ra and dec.  This
        requires the fields 'CRVAL3', 'CRVAL2' and 'DATE-OBS' to be set.
        """

        try:
            self.ra = self.field['RA'].copy()
            self.dec = self.field['DEC'].copy()
        except KeyError:
            msg = ("WARNING: Calculating pointing from Az and El.  This is"
                   " known to have arcminute level errors.")
            warnings.warn(msg)
            self.ra = sp.zeros(self.dims[0])
            self.dec = sp.zeros(self.dims[0])
            for ii in range(self.dims[0]):
                self.ra[ii], self.dec[ii] = utils.elaz2radecGBT(
                    self.field['CRVAL3'][ii], self.field['CRVAL2'][ii],
                    self.field['DATE-OBS'][ii])
Ejemplo n.º 3
0
    def calc_pointing(self) :
        """Calculates the telescope pointing.
        
        Should only be used if in alt/az mode. 
        At every time the Ra and Dec of the telescope time is calculated.
        These are stored as attributes (not fields) named ra and dec.  This
        requires the fields 'CRVAL3', 'CRVAL2' and 'DATE-OBS' to be set.
        """

        try:
            self.ra = self.field['RA'].copy()
            self.dec = self.field['DEC'].copy()
        except KeyError:
            msg = ("WARNING: Calculating pointing from Az and El.  This is"
                   " known to have arcminute level errors.")
            warnings.warn(msg)
            self.ra = sp.zeros(self.dims[0])
            self.dec = sp.zeros(self.dims[0])
            for ii in range(self.dims[0]) :
                self.ra[ii], self.dec[ii] = utils.elaz2radecGBT(
                                                self.field['CRVAL3'][ii],
                                                self.field['CRVAL2'][ii],
                                                self.field['DATE-OBS'][ii])
Ejemplo n.º 4
0
def calibrate_pol(Data, m_total, RM_dir, R_to_sky, DP_correct, RM_correct):
    """Subtracts a Map out of Data."""

    # Data is a DataBlock object.  It holds everything you need to know about
    # the data in a single scan and IF.  You should get to know them very well.
    # Data.data is a numpy masked array (see numpy documentation) and holds the
    # acctual data.  It is a 4 dimensional array.  The demensions are (in
    # order): (time, pol, cal, freq).  Each dimension can be any length which
    # you can figure out by looking at Data.dims = sp.shape(Data.data).
    # Data.field is a python dictionary that holds all the other data that you
    # might care about from the origional fits file.  For instance,
    # Data.field['CAL'] is an array with length dims[2].  It normally has
    # values ['T', 'F']. Data.field['CRVAL4'] tells you about the polarization
    # axis of Data.data.  By SDfits convension each polarization is represented
    # by an integer: 1=I, 2=Q, 3=U, 4=V, -5=XX, -6=YY, -7=XY, -8=YX.

    # Also this depends on having the polarizations rotated correctly to IQUV.
    # Some code to do this has been hacked together in the rotate_pol module,
    # but I don't trust it yet.

    # Some dimension checks.
    # We expect 4 polarizations.
    if not Data.dims[1] == 4:
        raise ce.DataError('Require 4 polarizations.')
    # We expect polarizations to be in order IQUV.
    if (Data.field['CRVAL4'][0] != -5 or Data.field['CRVAL4'][1] != -7
            or Data.field['CRVAL4'][2] != -8 or Data.field['CRVAL4'][3] != -6):
        raise ce.DataError('Expected the polarization basis to be XY.')

    # A useful function that might need:
    Data.calc_freq()
    # Now data has an atribute Data.freq which is an array that gives the
    # frequency along the last axis.

    # Data.field['CRVAL1'] is center frequency in Hz.
    # Data.data 4 dim array 2nd index polarization, 4th index frequency.

    # Need to get parallactic angle:
    Data.calc_PA()
    # This gives an array (Data.PA) of PA values of length = time dim.
    #   print Data.dims[0]

    # This segment of the code is for Rotation Measure Component

    # Since the RM Tables have half hour time divisions and scans are shorter, we can do 1 selection.

    if RM_correct == True:
        Comp_Time = 0.0
        Full_date = Data.field['DATE-OBS'][Data.dims[0] / 2]
        Date = Full_date.split('T')[0]
        Year = Date.split('-')[0]
        Month = Date.split('-')[1]
        Day = Date.split('-')[2]
        Full_time = Full_date.split('T')[1]
        Hour = Full_time.split(':')[0]
        Min = Full_time.split(':')[1]
        Sec = Full_time.split(':')[2]
        if int(Min) <= 15:
            Comp_Time = float(Hour) + 0.0
        elif int(Min) <= 45:
            Comp_Time = float(Hour) + 0.5
        else:
            Comp_Time = float(Hour) + 1.0
    #Victor's tables have time in format Hour (xx.xx), Az (deg), El (deg), RM
    # Angle phi = RM*(wavelength)^2 where phi is in radians and wavelength is in meters

        RM_file_name = RM_dir + Year + Month + Day + '_RM.txt'
        RM_data = np.loadtxt(RM_file_name)
        RA_RM = sp.zeros(len(RM_data[:, 0]))
        DEC_RM = sp.zeros(len(RM_data[:, 0]))
        for i in range(0, len(RM_data[:, 0])):
            RM_Hr = int(RM_data[i, 0])
            if RM_data[i, 0] % 1 == 0:
                RM_Min = '00'
                minutes = 0.0
            else:
                RM_Min = '30'
                minutes = 0.5
            Test = float(RM_Hr) + minutes
            if str(Comp_Time) == str(Test):
                UT_RM = Year + '-' + Month + '-' + Day + 'T' + str(
                    RM_Hr) + ':' + RM_Min + ':00.00'
                EL_RM = RM_data[i, 2]
                AZ_RM = RM_data[i, 1]
                RA_RM[i], DEC_RM[i] = utils.elaz2radecGBT(EL_RM, AZ_RM, UT_RM)
    #Now have tables of RA/DEC to compare to actual RA/DEC
        RM = 0

#This segment of the code is for Differential Phase Correction Generation

# Can determine the differential phase prior to the loop:
    if DP_correct == True:
        # Set up a table of data to examine (calon-caloff to get Tcal)
        Tcal = ma.mean(Data.data[:, :, 0, :], axis=0) - ma.mean(
            Data.data[:, :, 1, :], axis=0)
        #    Tcal = ma.mean(Data.data[:,:,0,:]-Data.data[:,:,1,:],axis=0)

        # This version was if we arbitrarily set 4 possible phases and found closest match. There seems to be
        # enough variability in the phase within the four categories that this doesn't quite work.

        # Randomly pick frequency bin near one of the zero crossings to compare U's
        #    U_test = Tcal[1,230]/sp.sqrt(Tcal[1,230]**2+Tcal[2,230]**2)
        #    print Tcal[:,191]
        #    print Tcal[1,:]
        #    U_test = Tcal[1,191]
        #    print U_test
        #    chi_sq =sp.zeros(4)
        #    dp_dat = sp.zeros((4,2))
        #    dp_dat[0] = [0.1354,2.341]
        #    dp_dat[1] = [0.0723, 2.4575]
        #    dp_dat[1] = [0.0730,2.611] #calculated specifically for sess 81
        #    dp_dat[2] = [0.1029,0.045]
        #    dp_dat[3] = [0,0]
        #    dp_dat[3] = [0.1669,5.609] # giving problems because closer for sess 81 at given freq
        #    min = 10
        #    val = 5
        #    for i in range(0,4):
        #       chi_sq[i] = U_test-sp.cos(dp_dat[i,0]*Data.freq[230]/1000000+dp_dat[i,1])/sp.sqrt(Tcal[1,230]**2+Tcal[2,230]**2)
        #        print sp.cos(dp_dat[i,0]*Data.freq[191]/1000000+dp_dat[i,1])
        #        chi_sq[i] = U_test-sp.cos(dp_dat[i,0]*Data.freq[191]/1000000+dp_dat[i,1])
        #        if abs(chi_sq[i]) < min:
        #            min = abs(chi_sq[i])
        #            val = i
        # val tells which of the correction functions to use.
        #    print chi_sq
        #    print val
        #    print Data.freq[191]

        # Alternate code for solving differential phase for each scan.
        fitfunc = lambda p, x: sp.cos(p[0] * x + p[1])
        errfunc = lambda p, x, y: fitfunc(p, x) - y
        freqs = sp.zeros(Data.dims[3])
        U_data = sp.zeros(Data.dims[3])
        V_data = sp.zeros(Data.dims[3])
        R_data = sp.zeros(Data.dims[3])
        for i in range(0, Data.dims[3]):
            freqs[i] = Data.freq[Data.dims[3] - i - 1] / 1000000
            U_data[i] = Tcal[1, Data.dims[3] - i - 1]
            V_data[i] = Tcal[2, Data.dims[3] - i - 1]
            R_data[i] = U_data[i] / sp.sqrt(U_data[i]**2 + V_data[i]**2)
#    print np.any(np.isnan(R_data))
#    print np.any(np.isinf(R_data))
#    print freqs

        for j in range(0, Data.dims[3]):
            if int(freqs[j]) == 710:
                mask_num = j
            if int(freqs[j]) == 740:
                mask_num2 = j
        Datain = R_data[mask_num:mask_num2]
        fin = freqs[mask_num:mask_num2]
        bad_pts = np.logical_or(np.isnan(Datain), np.isinf(Datain))
        good_ind = np.where(np.logical_not(bad_pts))
        Datain = Datain[good_ind]
        fin = fin[good_ind]
        R0 = [0.18, 1.0]
        if len(good_ind[0]) > 1:
            #            print good_ind[0]
            R, success = optimize.leastsq(errfunc,
                                          R0[:],
                                          args=(fin, Datain),
                                          maxfev=10000)
            R[1] = R[1] % (2 * sp.pi)
            print R
        else:
            R = [0.0, 0.0]
            print "Not able to resolve a noise cal phase, setting phase to zero."

# This starts the actual data processing for the given scan

    for time_index in range(0, Data.dims[0]):

        # Extra data needed for Rotation Measure Correction
        if RM_correct == True:
            RA = Data.field['CRVAL2'][time_index]
            DEC = Data.field['CRVAL3'][time_index]
            #        print RA
            #        print DEC
            RM = 0
            valid = []
            for i in range(0, len(RA_RM)):
                if RA_RM[i] != 0:
                    if abs(RA - RA_RM[i]) < 10.0:
                        if abs(DEC - DEC_RM[i]) < 10.0:
                            RM = RM_data[i, 3]
                            valid.append(i)
            RA_M = 10.0
            DEC_M = 10.0
            for j in range(0, len(valid)):
                if abs(RA - RA_RM[valid[j]]) < RA_M:
                    if abs(DEC - DEC_RM[valid[j]]) < DEC_M:
                        RM = RM_data[valid[j], 3]

#        print RM

#Generate a sky matrix for this time index (assumes a XY basis):
        m_sky = sp.zeros((4, 4))
        m_sky[0, 0] = 0.5 * (1 + ma.cos(2 * Data.PA[time_index] * sp.pi / 180))
        m_sky[0, 1] = -ma.sin(2 * Data.PA[time_index] * sp.pi / 180)
        m_sky[0, 3] = 0.5 * (1 - ma.cos(2 * Data.PA[time_index] * sp.pi / 180))
        m_sky[1, 0] = 0.5 * ma.sin(2 * Data.PA[time_index] * sp.pi / 180)
        m_sky[1, 1] = ma.cos(2 * Data.PA[time_index] * sp.pi / 180)
        m_sky[1, 3] = -0.5 * ma.sin(2 * Data.PA[time_index] * sp.pi / 180)
        m_sky[2, 2] = 1
        m_sky[3, 0] = 0.5 * (1 - ma.cos(2 * Data.PA[time_index] * sp.pi / 180))
        m_sky[3, 1] = ma.sin(2 * Data.PA[time_index] * sp.pi / 180)
        m_sky[3, 3] = 0.5 * (1 + ma.cos(2 * Data.PA[time_index] * sp.pi / 180))

        M_sky = sp.mat(m_sky)
        M_sky = M_sky.I
        #        print M_sky

        #        for cal_index in range(0,Data.dims[2]):
        for cal_index in range(0, 2):
            # Determines the Gains to use
            for freq in range(0, Data.dims[3]):
                # Tells which mueller matrix to use.
                freq_limit = len(m_total[0, :])
                frequency = int(Data.freq[freq] / 1000)
                #               print frequency
                bin = int((900000 - frequency) * freq_limit / 200000)
                #               print bin
                #               if freq_limit == 200:
                #                   bin = 900-frequency
                #Not setup to work with spectrometer data.
                #               elif freq_limit == 260:
                #                   bin = 929-frequency
                #               print bin

                #Generate a sky matrix for this time index:
                #With faraday rotation  sky matrix now frequency dependent
                if RM_correct == True:
                    wavelength = 300000.0 / float(
                        frequency
                    )  # should be in meters given that frequency is in kHz
                    #               print wavelength
                    Phi = RM * wavelength * wavelength
                    #               print Phi
                    m_sky = sp.zeros((4, 4))
                    m_sky[0, 0] = 0.5 * (
                        1 +
                        ma.cos(2 * Data.PA[time_index] * sp.pi / 180 + Phi))
                    m_sky[0,
                          1] = -ma.sin(2 * Data.PA[time_index] * sp.pi / 180 +
                                       Phi)
                    m_sky[0, 3] = 0.5 * (
                        1 -
                        ma.cos(2 * Data.PA[time_index] * sp.pi / 180 + Phi))
                    m_sky[1, 0] = 0.5 * ma.sin(2 * Data.PA[time_index] *
                                               sp.pi / 180 + Phi)
                    m_sky[1,
                          1] = ma.cos(2 * Data.PA[time_index] * sp.pi / 180 +
                                      Phi)
                    m_sky[1, 3] = -0.5 * ma.sin(2 * Data.PA[time_index] *
                                                sp.pi / 180 + Phi)
                    m_sky[2, 2] = 1
                    m_sky[3, 0] = 0.5 * (
                        1 -
                        ma.cos(2 * Data.PA[time_index] * sp.pi / 180 + Phi))
                    m_sky[3,
                          1] = ma.sin(2 * Data.PA[time_index] * sp.pi / 180 +
                                      Phi)
                    m_sky[3, 3] = 0.5 * (
                        1 +
                        ma.cos(2 * Data.PA[time_index] * sp.pi / 180 + Phi))

                    M_sky = sp.mat(m_sky)
                    M_sky = M_sky.I
#               print M_sky

# Converts files into vector format
                XY_params = Data.data[time_index, :, cal_index, freq]
                # Next there is a matrix multiplication that will generate
                # a new set of xy values. (Differential gain correction)
                XY_params[0] = XY_params[0] * m_total[0, bin]
                XY_params[3] = XY_params[3] * m_total[1, bin]
                XY_params[1] = XY_params[1] * sp.sqrt(
                    m_total[0, bin] * m_total[1, bin])
                XY_params[2] = XY_params[2] * sp.sqrt(
                    m_total[0, bin] * m_total[1, bin])

                # Add in correction for differential phase

                if DP_correct == True:
                    XY_params[1] = XY_params[1] * sp.cos(
                        R[0] * frequency / 1000 + R[1]
                    ) - XY_params[2] * sp.sin(R[0] * frequency / 1000 + R[1])
                    XY_params[2] = XY_params[1] * sp.sin(
                        R[0] * frequency / 1000 + R[1]
                    ) + XY_params[2] * sp.cos(R[0] * frequency / 1000 + R[1])

    #Rotate to sky coordinates (and RM correct if set)

                if R_to_sky == True:
                    XY_params = np.dot(M_sky, XY_params)

    #Write corrected data to the new file.

                for i in range(0, Data.dims[1]):
                    Data.data[time_index, i, cal_index, freq] = XY_params[i]
Ejemplo n.º 5
0
def calibrate_pol(Data, m_total,RM_dir,R_to_sky,DP_correct,RM_correct) :
    """Subtracts a Map out of Data."""
        
    # Data is a DataBlock object.  It holds everything you need to know about
    # the data in a single scan and IF.  You should get to know them very well.
    # Data.data is a numpy masked array (see numpy documentation) and holds the
    # acctual data.  It is a 4 dimensional array.  The demensions are (in
    # order): (time, pol, cal, freq).  Each dimension can be any length which
    # you can figure out by looking at Data.dims = sp.shape(Data.data).
    # Data.field is a python dictionary that holds all the other data that you
    # might care about from the origional fits file.  For instance,
    # Data.field['CAL'] is an array with length dims[2].  It normally has
    # values ['T', 'F']. Data.field['CRVAL4'] tells you about the polarization
    # axis of Data.data.  By SDfits convension each polarization is represented
    # by an integer: 1=I, 2=Q, 3=U, 4=V, -5=XX, -6=YY, -7=XY, -8=YX.

    # Also this depends on having the polarizations rotated correctly to IQUV.
    # Some code to do this has been hacked together in the rotate_pol module,
    # but I don't trust it yet.

    # Some dimension checks.
    # We expect 4 polarizations.
    if not Data.dims[1] == 4 :
       	raise ce.DataError('Require 4 polarizations.')
    # We expect polarizations to be in order IQUV.
    if (Data.field['CRVAL4'][0] != -5 or Data.field['CRVAL4'][1] != -7 or
        Data.field['CRVAL4'][2] != -8 or Data.field['CRVAL4'][3] != -6) :
       	raise ce.DataError('Expected the polarization basis to be XY.')

    # A useful function that might need:
    Data.calc_freq()
    # Now data has an atribute Data.freq which is an array that gives the
    # frequency along the last axis.
          
    # Data.field['CRVAL1'] is center frequency in Hz. 
    # Data.data 4 dim array 2nd index polarization, 4th index frequency. 

    # Need to get parallactic angle:
    Data.calc_PA()
    # This gives an array (Data.PA) of PA values of length = time dim.
#   print Data.dims[0]



# This segment of the code is for Rotation Measure Component

    # Since the RM Tables have half hour time divisions and scans are shorter, we can do 1 selection.

    if RM_correct==True:
        Comp_Time = 0.0
        Full_date = Data.field['DATE-OBS'][Data.dims[0]/2]
        Date = Full_date.split('T')[0]
        Year = Date.split('-')[0]
        Month = Date.split('-')[1]
        Day = Date.split('-')[2]
        Full_time = Full_date.split('T')[1]
        Hour = Full_time.split(':')[0]
        Min = Full_time.split(':')[1]
        Sec = Full_time.split(':')[2]
        if int(Min)<=15:
            Comp_Time = float(Hour)+0.0
        elif int(Min)<=45:
            Comp_Time = float(Hour)+0.5
        else :
            Comp_Time = float(Hour)+1.0
    #Victor's tables have time in format Hour (xx.xx), Az (deg), El (deg), RM
    # Angle phi = RM*(wavelength)^2 where phi is in radians and wavelength is in meters

        RM_file_name = RM_dir + Year + Month + Day + '_RM.txt'
        RM_data = np.loadtxt(RM_file_name)
        RA_RM = sp.zeros(len(RM_data[:,0]))
        DEC_RM = sp.zeros(len(RM_data[:,0]))
        for i in range(0,len(RM_data[:,0])):
            RM_Hr = int(RM_data[i,0])
            if RM_data[i,0]%1 == 0 :
                RM_Min = '00'
                minutes = 0.0
            else:
                RM_Min = '30'
                minutes = 0.5
            Test = float(RM_Hr)+minutes
            if str(Comp_Time) == str(Test):
                UT_RM = Year+'-'+Month+'-'+Day+'T'+str(RM_Hr)+':'+RM_Min+':00.00'
                EL_RM = RM_data[i,2]
                AZ_RM = RM_data[i,1]
                RA_RM[i], DEC_RM[i] = utils.elaz2radecGBT(EL_RM,AZ_RM,UT_RM)
    #Now have tables of RA/DEC to compare to actual RA/DEC
        RM = 0




#This segment of the code is for Differential Phase Correction Generation

# Can determine the differential phase prior to the loop:
    if DP_correct==True:
# Set up a table of data to examine (calon-caloff to get Tcal)
        Tcal = ma.mean(Data.data[:,:,0,:],axis=0)-ma.mean(Data.data[:,:,1,:],axis=0)
#    Tcal = ma.mean(Data.data[:,:,0,:]-Data.data[:,:,1,:],axis=0)



# This version was if we arbitrarily set 4 possible phases and found closest match. There seems to be
# enough variability in the phase within the four categories that this doesn't quite work.

    # Randomly pick frequency bin near one of the zero crossings to compare U's
#    U_test = Tcal[1,230]/sp.sqrt(Tcal[1,230]**2+Tcal[2,230]**2)
#    print Tcal[:,191]
#    print Tcal[1,:]
#    U_test = Tcal[1,191]
#    print U_test
#    chi_sq =sp.zeros(4)
#    dp_dat = sp.zeros((4,2))
#    dp_dat[0] = [0.1354,2.341]
#    dp_dat[1] = [0.0723, 2.4575]
#    dp_dat[1] = [0.0730,2.611] #calculated specifically for sess 81
#    dp_dat[2] = [0.1029,0.045]
#    dp_dat[3] = [0,0]
#    dp_dat[3] = [0.1669,5.609] # giving problems because closer for sess 81 at given freq
#    min = 10
#    val = 5
#    for i in range(0,4):
#       chi_sq[i] = U_test-sp.cos(dp_dat[i,0]*Data.freq[230]/1000000+dp_dat[i,1])/sp.sqrt(Tcal[1,230]**2+Tcal[2,230]**2)
#        print sp.cos(dp_dat[i,0]*Data.freq[191]/1000000+dp_dat[i,1])
#        chi_sq[i] = U_test-sp.cos(dp_dat[i,0]*Data.freq[191]/1000000+dp_dat[i,1])
#        if abs(chi_sq[i]) < min:
#            min = abs(chi_sq[i])
#            val = i
# val tells which of the correction functions to use.    
#    print chi_sq
#    print val
#    print Data.freq[191]



# Alternate code for solving differential phase for each scan.
        fitfunc = lambda p,x: sp.cos(p[0]*x+p[1])
        errfunc = lambda p,x,y: fitfunc(p,x)-y
        freqs = sp.zeros(Data.dims[3])
        U_data = sp.zeros(Data.dims[3])
        V_data = sp.zeros(Data.dims[3])
        R_data = sp.zeros(Data.dims[3])
        for i in range(0,Data.dims[3]):
            freqs[i] = Data.freq[Data.dims[3]-i-1]/1000000
            U_data[i] = Tcal[1,Data.dims[3]-i-1]
            V_data[i] = Tcal[2,Data.dims[3]-i-1]
            R_data[i] = U_data[i]/sp.sqrt(U_data[i]**2+V_data[i]**2)
#    print np.any(np.isnan(R_data))
#    print np.any(np.isinf(R_data))
#    print freqs    

        for j in range(0,Data.dims[3]):
            if int(freqs[j])==710:
                mask_num = j
            if int(freqs[j])==740:
                mask_num2 = j
        Datain = R_data[mask_num:mask_num2]
        fin = freqs[mask_num:mask_num2]
        bad_pts = np.logical_or(np.isnan(Datain),np.isinf(Datain))
        good_ind = np.where(np.logical_not(bad_pts))
        Datain = Datain[good_ind]
        fin = fin[good_ind]
        R0 = [0.18,1.0]
        if len(good_ind[0])>1:
#            print good_ind[0]
            R,success = optimize.leastsq(errfunc,R0[:],args=(fin,Datain),maxfev=10000)
            R[1] = R[1]%(2*sp.pi)
            print R
        else:
            R=[0.0,0.0]
            print "Not able to resolve a noise cal phase, setting phase to zero."
  

# This starts the actual data processing for the given scan
         
    for time_index in range(0,Data.dims[0]):

# Extra data needed for Rotation Measure Correction
        if RM_correct==True:
            RA = Data.field['CRVAL2'][time_index]
            DEC = Data.field['CRVAL3'][time_index]
#        print RA
#        print DEC
            RM = 0
            valid = []
            for i in range(0,len(RA_RM)):
                if RA_RM[i] != 0:
                    if abs(RA-RA_RM[i])<10.0:
                        if abs(DEC-DEC_RM[i])<10.0:
                            RM = RM_data[i,3]     
                            valid.append(i)
            RA_M = 10.0
            DEC_M = 10.0
            for j in range(0,len(valid)):
                if abs(RA-RA_RM[valid[j]])<RA_M:
                    if abs(DEC-DEC_RM[valid[j]])<DEC_M:
                        RM = RM_data[valid[j],3]  
                         
#        print RM
 
    #Generate a sky matrix for this time index (assumes a XY basis):
        m_sky = sp.zeros((4,4))
        m_sky[0,0] = 0.5*(1+ma.cos(2*Data.PA[time_index]*sp.pi/180))
        m_sky[0,1] = -ma.sin(2*Data.PA[time_index]*sp.pi/180)
        m_sky[0,3] = 0.5*(1-ma.cos(2*Data.PA[time_index]*sp.pi/180))
        m_sky[1,0] = 0.5*ma.sin(2*Data.PA[time_index]*sp.pi/180)
        m_sky[1,1] = ma.cos(2*Data.PA[time_index]*sp.pi/180)
        m_sky[1,3] = -0.5*ma.sin(2*Data.PA[time_index]*sp.pi/180)
        m_sky[2,2] = 1
        m_sky[3,0] = 0.5*(1-ma.cos(2*Data.PA[time_index]*sp.pi/180))
        m_sky[3,1] = ma.sin(2*Data.PA[time_index]*sp.pi/180)
        m_sky[3,3] = 0.5*(1+ma.cos(2*Data.PA[time_index]*sp.pi/180))

        M_sky = sp.mat(m_sky)
        M_sky = M_sky.I
#        print M_sky

#        for cal_index in range(0,Data.dims[2]):
        for cal_index in range(0,2):
        # Determines the Gains to use   
            for freq in range(0,Data.dims[3]):
     # Tells which mueller matrix to use. 
               freq_limit = len(m_total[0,:])
               frequency = int(Data.freq[freq]/1000)
#               print frequency
               bin = int((900000-frequency)*freq_limit/200000)
#               print bin
#               if freq_limit == 200:
#                   bin = 900-frequency
#Not setup to work with spectrometer data.
#               elif freq_limit == 260:
#                   bin = 929-frequency
#               print bin

    #Generate a sky matrix for this time index:
    #With faraday rotation  sky matrix now frequency dependent
               if RM_correct==True:
                   wavelength = 300000.0/float(frequency) # should be in meters given that frequency is in kHz
#               print wavelength
                   Phi = RM*wavelength*wavelength
#               print Phi
                   m_sky = sp.zeros((4,4)) 
                   m_sky[0,0] = 0.5*(1+ma.cos(2*Data.PA[time_index]*sp.pi/180+Phi))
                   m_sky[0,1] = -ma.sin(2*Data.PA[time_index]*sp.pi/180+Phi) 
                   m_sky[0,3] = 0.5*(1-ma.cos(2*Data.PA[time_index]*sp.pi/180+Phi))
                   m_sky[1,0] = 0.5*ma.sin(2*Data.PA[time_index]*sp.pi/180+Phi) 
                   m_sky[1,1] = ma.cos(2*Data.PA[time_index]*sp.pi/180+Phi) 
                   m_sky[1,3] = -0.5*ma.sin(2*Data.PA[time_index]*sp.pi/180+Phi)
                   m_sky[2,2] = 1
                   m_sky[3,0] = 0.5*(1-ma.cos(2*Data.PA[time_index]*sp.pi/180+Phi))
                   m_sky[3,1] = ma.sin(2*Data.PA[time_index]*sp.pi/180+Phi)
                   m_sky[3,3] = 0.5*(1+ma.cos(2*Data.PA[time_index]*sp.pi/180+Phi))
  
                   M_sky = sp.mat(m_sky)
                   M_sky = M_sky.I 
#               print M_sky 

    # Converts files into vector format 
               XY_params = Data.data[time_index,:,cal_index,freq]       
    # Next there is a matrix multiplication that will generate 
    # a new set of xy values. (Differential gain correction)
               XY_params[0] = XY_params[0]*m_total[0,bin]
               XY_params[3] = XY_params[3]*m_total[1,bin]
               XY_params[1] = XY_params[1]*sp.sqrt(m_total[0,bin]*m_total[1,bin])
               XY_params[2] = XY_params[2]*sp.sqrt(m_total[0,bin]*m_total[1,bin])

    # Add in correction for differential phase

               if DP_correct==True:
                   XY_params[1] = XY_params[1]*sp.cos(R[0]*frequency/1000+R[1])-XY_params[2]*sp.sin(R[0]*frequency/1000+R[1])
                   XY_params[2] = XY_params[1]*sp.sin(R[0]*frequency/1000+R[1])+XY_params[2]*sp.cos(R[0]*frequency/1000+R[1])

    #Rotate to sky coordinates (and RM correct if set)

               if R_to_sky==True:
                   XY_params = np.dot(M_sky,XY_params)

    #Write corrected data to the new file. 

               for i in range(0,Data.dims[1]):
                    Data.data[time_index,i,cal_index,freq] = XY_params[i]