Ejemplo n.º 1
0
def blank_uvcal_from_uvdata(uvdata):
    """initialize UVCal object with same times, antennas, and frequencies as uvdata.

    Parameters
    ----------
    uvdata: UVData object
        UVData object that you wish to generate a blanck UVCal object from.

    Returns
    -------
    uvcal: UVCal object
        UVCal object with all flags set to False
        and all gains set to unity with same antennas, freqs, jones, and times
        as uvdata.
    """
    uvcal = UVCal()
    uvcal.Nfreqs = uvdata.Nfreqs
    uvcal.Njones = uvdata.Npols
    uvcal.Ntimes = uvdata.Ntimes
    uvcal.Nspws = uvdata.Nspws
    uvcal.history = ""
    uvcal.Nspws = uvdata.Nspws
    uvcal.telescope_name = uvdata.telescope_name
    uvcal.telescope_location = uvdata.telescope_location
    uvcal.Nants_data = uvdata.Nants_data
    uvcal.Nants_telescope = uvdata.Nants_telescope
    uvcal.ant_array = np.asarray(list(set(uvdata.ant_1_array).union(set(uvdata.ant_2_array))))
    uvcal.antenna_names = uvdata.antenna_names
    uvcal.antenna_numbers = uvdata.antenna_numbers
    uvcal.antenna_positions = uvdata.antenna_positions
    uvcal.spw_array = uvdata.spw_array
    uvcal.freq_array = uvdata.freq_array
    uvcal.jones_array = uvdata.polarization_array
    uvcal.time_array = np.unique(uvdata.time_array)
    uvcal.integration_time = np.mean(uvdata.integration_time)
    uvcal.lst_array = np.unique(uvdata.lst_array)
    uvcal.gain_convention = "divide"  # always use divide for this package.
    uvcal.flag_array = np.zeros(
        (uvcal.Nants_data, uvcal.Nspws, uvcal.Nfreqs, uvcal.Ntimes, uvcal.Njones),
        dtype=np.bool,
    )
    uvcal.quality_array = np.zeros_like(uvcal.flag_array, dtype=np.float64)
    uvcal.x_orientation = uvdata.x_orientation
    uvcal.gain_array = np.ones_like(uvcal.flag_array, dtype=np.complex128)
    uvcal.cal_style = "redundant"
    uvcal.cal_type = "gain"
    uvcal.time_range = (
        uvcal.time_array.min() - uvcal.integration_time / 2.0,
        uvcal.time_array.max() + uvcal.integration_time / 2.0,
    )
    uvcal.channel_width = np.median(np.diff(uvcal.freq_array))

    return uvcal
Ejemplo n.º 2
0
 def uvcal_from_data(self):
     """
     Generate an empty uvcal object from visibility parameters
     """
     uvc = UVCal()
     uvc.Njones = self.measured_vis.Npols
     uvc.Nfreqs = self.measured_vis.Nfreqs
     uvc.Ntimes = self.measured_vis.Ntimes
     uvc.Nspws = self.measured_vis.Nspws
     uvc.time_range = (self.measured_vis.time_array.min(),
                       self.measured_vis.time_array.max())
     uvc.telescope_name = self.measured_vis.telescope_name
     uvc.Nants_data = self.measured_vis.Nants_data
     uvc.Nants_telescope = self.measured_vis.Nants_telescope
     uvc.ant_array = np.unique(self.measured_vis.ant_1_array)
     uvc.antenna_names = self.measured_vis.antenna_names
     uvc.antenna_numbers = self.measured_vis.antenna_numbers
     uvc.freq_array = self.measured_vis.freq_array
     uvc.channel_width = self.measured_vis.channel_width
     uvc.jones_array = self.measured_vis.polarization_array
     uvc.time_array = np.unique(self.measured_vis.time_array)
     uvc.integration_time = self.measured_vis.integration_time
     uvc.x_orientation = 'east'  #always
     uvc.cal_type = 'gain'
     uvc.quality_array = np.zeros(
         (self.measured_vis.Nants_data, 1, self.measured_vis.Nfreqs,
          self.measured_vis.Ntimes, self.measured_vis.Npols))
     uvc.git_origin_cal='calibrated with stefcal_uvdata version %s with run id %s'\
                         %(self.meta_params.stefcal_version_str,self.meta_params.id)
     uvc.gain_array = np.ones(
         (self.meta_params.Nants_data, 1, self.model_vis.Nfreqs,
          self.model_vis.Ntimes, self.model_vis.Npols),
         dtype=complex)
     uvc.flag_array = np.empty(
         (self.meta_params.Nants_data, 1, self.model_vis.Nfreqs,
          self.model_vis.Ntimes, self.model_vis.Npols),
         dtype=bool)
     uvc.flag_array[:] = False
     return uvc
Ejemplo n.º 3
0
	gains[index150, :, poi_i, 0] = x150 
	gains[index150, :, poi_i, 1] = y150 
	gains[index230, :, poi_i, 0] = x230 
	gains[index230, :, poi_i, 1] = y230 
	gains[index320, :, poi_i, 0] = x320 
	gains[index320, :, poi_i, 1] = y320 
	gains[index400, :, poi_i, 0] = x400 
	gains[index400, :, poi_i, 1] = y400
	gains[index524, :, poi_i, 0] = x524
	gains[index524, :, poi_i, 1] = y524

	datafile.close()
	###

cal = UVCal()
cal.cal_type = 'gain'
cal.set_gain()
cal.Nfreqs = Nfreqs
cal.Njones = Njones
cal.Ntimes = Ntimes
#
# Change the history comment to list field, freq range name, instrument, averaging sample set, pointing JD reference,
# calibration catalogs, and whatever else is important.
#
cal.history = 'EXAMPLE HISTORY, PLEASE CHANGE: EoR0 highband per frequency, per pointing, per polarization bandpass for MWA, averaged per cable over Season 1 using an early version of KGS. Pointing JD is referenced from Aug 23,2013.'
#
cal.Nspws = 1
cal.freq_array = freq_array.reshape(cal.Nspws, -1)
cal.freq_range = [freq_array[0], freq_array[-1]]  # valid frequencies for solutions.
cal.channel_width = np.diff(freq_array)[0]
cal.jones_array = jones_array
Ejemplo n.º 4
0
# Prepare cal file
uvc = UVCal()

# Automatically import as much as we can for the data
required = [r[1:] for r in uvc.required()]
for var in required:
    try:
        attr = getattr(uvd, var)
    except:
        pass
    else:
        setattr(uvc, var, attr)

# Change what's left manually
uvc.cal_style = 'redundant'
uvc.cal_type = 'gain'
uvc.gain_convention = 'multiply'

command_given = sys.argv[1:]
command_given = ["'" + x + "'" if ' ' in x else x for x in command_given]
command_given = ' '.join(command_given)
script_path = os.path.dirname(os.path.abspath(__file__))
git_hash = sub.check_output(['git', '-C', script_path, 'rev-parse',
                             'HEAD']).strip().decode('UTF-8')
uvc.history = 'Created using mkgains.py\nCommand run: mkgains.py %s\nmkgains.py Git Hash: %s' % (
    command_given, git_hash)

uvc.Njones = uvd.Npols
uvc.jones_array = uvd.polarization_array
uvc.ant_array = np.arange(uvc.Nants_data)
uvc.time_range = [
Ejemplo n.º 5
0
def write_cal(fname,
              gains,
              freqs,
              times,
              flags=None,
              quality=None,
              total_qual=None,
              write_file=True,
              return_uvc=True,
              outdir='./',
              overwrite=False,
              gain_convention='divide',
              history=' ',
              x_orientation="east",
              telescope_name='HERA',
              cal_style='redundant',
              **kwargs):
    '''Format gain solution dictionary into pyuvdata.UVCal and write to file

    Arguments:
        fname : type=str, output file basename
        gains : type=dictionary, holds complex gain solutions. keys are antenna + pol
                tuple pairs, e.g. (2, 'x'), and keys are 2D complex ndarrays with time
                along [0] axis and freq along [1] axis.
        freqs : type=ndarray, holds unique frequencies channels in Hz
        times : type=ndarray, holds unique times of integration centers in Julian Date
        flags : type=dictionary, holds boolean flags (True if flagged) for gains.
                Must match shape of gains.
        quality : type=dictionary, holds "quality" of calibration solution. Must match
                  shape of gains. See pyuvdata.UVCal doc for more details.
        total_qual : type=dictionary, holds total_quality_array. Key(s) are polarization
            string(s) and values are 2D (Ntimes, Nfreqs) ndarrays.
        write_file : type=bool, if True, write UVCal to calfits file
        return_uvc : type=bool, if True, return UVCal object
        outdir : type=str, output file directory
        overwrite : type=bool, if True overwrite output files
        gain_convention : type=str, gain solutions formatted such that they 'multiply' into data
                          to get model, or 'divide' into data to get model
                          options=['multiply', 'divide']
        history : type=str, history string for UVCal object.
        x_orientation : type=str, orientation of X dipole, options=['east', 'north']
        telescope_name : type=str, name of telescope
        cal_style : type=str, style of calibration solutions, options=['redundant', 'sky']. If
                    cal_style == sky, additional params are required. See pyuvdata.UVCal doc.
        kwargs : additional atrributes to set in pyuvdata.UVCal
    Returns:
        if return_uvc: returns UVCal object
        else: returns None
    '''
    # helpful dictionaries for antenna polarization of gains
    str2pol = {'x': -5, 'y': -6}
    pol2str = {-5: 'x', -6: 'y'}

    # get antenna info
    ant_array = np.array(sorted(map(lambda k: k[0], gains.keys())), np.int)
    antenna_numbers = copy.copy(ant_array)
    antenna_names = np.array(map(lambda a: "ant{}".format(a), antenna_numbers))
    Nants_data = len(ant_array)
    Nants_telescope = len(antenna_numbers)

    # get polarization info
    pol_array = np.array(sorted(set(map(lambda k: k[1].lower(),
                                        gains.keys()))))
    jones_array = np.array(map(lambda p: str2pol[p], pol_array), np.int)
    Njones = len(jones_array)

    # get time info
    time_array = np.array(times, np.float)
    Ntimes = len(time_array)
    time_range = np.array([time_array.min(), time_array.max()], np.float)
    if len(time_array) > 1:
        integration_time = np.median(np.diff(time_array)) * 24. * 3600.
    else:
        integration_time = 0.0

    # get frequency info
    freq_array = np.array(freqs, np.float)
    Nfreqs = len(freq_array)
    Nspws = 1
    freq_array = freq_array[None, :]
    spw_array = np.arange(Nspws)
    channel_width = np.median(np.diff(freq_array))

    # form gain, flags and qualities
    gain_array = np.empty((Nants_data, Nspws, Nfreqs, Ntimes, Njones),
                          np.complex)
    flag_array = np.empty((Nants_data, Nspws, Nfreqs, Ntimes, Njones), np.bool)
    quality_array = np.empty((Nants_data, Nspws, Nfreqs, Ntimes, Njones),
                             np.float)
    total_quality_array = np.empty((Nspws, Nfreqs, Ntimes, Njones), np.float)
    for i, p in enumerate(pol_array):
        if total_qual is not None:
            total_quality_array[0, :, :, i] = total_qual[p].T[None, :, :]
        for j, a in enumerate(ant_array):
            # ensure (a, p) is in gains
            if (a, p) in gains:
                gain_array[j, :, :, :, i] = gains[(a, p)].T[None, :, :]
                if flags is not None:
                    flag_array[j, :, :, :, i] = flags[(a, p)].T[None, :, :]
                else:
                    flag_array[j, :, :, :, i] = np.zeros(
                        (Nspws, Nfreqs, Ntimes), np.bool)
                if quality is not None:
                    quality_array[j, :, :, :,
                                  i] = quality[(a, p)].T[None, :, :]
                else:
                    quality_array[j, :, :, :, i] = np.ones(
                        (Nspws, Nfreqs, Ntimes), np.float)
            else:
                gain_array[j, :, :, :, i] = np.ones((Nspws, Nfreqs, Ntimes),
                                                    np.complex)
                flag_array[j, :, :, :, i] = np.ones((Nspws, Nfreqs, Ntimes),
                                                    np.bool)
                quality_array[j, :, :, :, i] = np.ones((Nspws, Nfreqs, Ntimes),
                                                       np.float)

    if total_qual is None:
        total_quality_array = None

    # Check gain_array for values close to zero, if so, set to 1
    zero_check = np.isclose(gain_array, 0, rtol=1e-10, atol=1e-10)
    gain_array[zero_check] = 1.0 + 0j
    flag_array[zero_check] += True
    if zero_check.max() is True:
        print(
            "Some of values in self.gain_array were zero and are flagged and set to 1."
        )

    # instantiate UVCal
    uvc = UVCal()

    # enforce 'gain' cal_type
    uvc.cal_type = "gain"

    # create parameter list
    params = [
        "Nants_data", "Nants_telescope", "Nfreqs", "Ntimes", "Nspws", "Njones",
        "ant_array", "antenna_numbers", "antenna_names", "cal_style",
        "history", "channel_width", "flag_array", "gain_array",
        "quality_array", "jones_array", "time_array", "spw_array",
        "freq_array", "history", "integration_time", "time_range",
        "x_orientation", "telescope_name", "gain_convention",
        "total_quality_array"
    ]

    # create local parameter dict
    local_params = locals()

    # overwrite with kwarg parameters
    local_params.update(kwargs)

    # set parameters
    for p in params:
        uvc.__setattr__(p, local_params[p])

    # run check
    uvc.check()

    # write to file
    if write_file:
        # check output
        fname = os.path.join(outdir, fname)
        if os.path.exists(fname) and overwrite is False:
            print("{} exists, not overwriting...".format(fname))
        else:
            print "saving {}".format(fname)
            uvc.write_calfits(fname, clobber=True)

    # return object
    if return_uvc:
        return uvc