コード例 #1
0
def make_calfits(fname,
                 data_array,
                 freq_array,
                 time_array,
                 jones_array,
                 ants,
                 flag_array=None,
                 channel_width=0.0,
                 gain_convention='multiply',
                 history='',
                 telescope_name='HERA',
                 x_orientation='east',
                 integration_time=10.0,
                 freq_range=None,
                 clobber=False):
    """
    make a calfits file from data_array etc. objects
   
    fname : str, filename

    data_array : ndarray, shape=(Nants, Nfreqs, Ntimes, Npols)

    freq_array : ndarray, shape=Nfreqs

    time_array : ndarray, shape=Ntimes

    jones_array : ndarray, shape=Npols

    ants : ndarray, shape=Nants
    """
    # specify ant params
    Nants_data = len(ants)
    Nants_telescope = len(ants)
    ant_array = np.array(ants, np.int)
    antenna_names = np.array(ants, np.str)
    antenna_numbers = np.array(ants, np.int)

    # frequency params
    Nfreqs = len(freq_array)
    if freq_range is None:
        freq_range = np.array([freq_array[0], freq_array[-1]])
    freq_array = freq_array.reshape(1, -1)

    # pol params
    Njones = len(jones_array)

    # time params
    Ntimes = len(time_array)
    time_range = np.array([time_array[0], time_array[-1]])

    # spw params
    Nspws = 1
    spw_array = np.array([0])
    data_array = data_array[:, np.newaxis, :, :, :]
    if flag_array is not None:
        flag_array = flag_array[:, np.newaxis, :, :, :].astype(np.bool)

    # data params
    if data_array.shape[2] > 1:
        gain_array = data_array
        delay_array = None
        cal_type = 'gain'
    else:
        gain_array = None
        delay_array = data_array
        cal_type = 'delay'

    if flag_array is None:
        flag_array = np.array(np.zeros_like(data_array), np.bool)
    quality_array = np.zeros_like(data_array, np.float64)

    # make blank uvc
    uvc = UVCal()
    params = [
        'Nants_data', 'Nants_telescope', 'ant_array', 'antenna_names',
        'antenna_numbers', 'Nfreqs', 'freq_array', 'Njones', 'Ntimes',
        'time_range', 'Nspws', 'spw_array', 'data_array', 'gain_array',
        'delay_array', 'jones_array', 'time_array', 'freq_array', 'cal_type',
        'flag_array', 'quality_array', 'channel_width', 'gain_convention',
        'history', 'telescope_name', 'x_orientation', 'integration_time',
        'freq_range'
    ]
    for p in params:
        uvc.__setattr__(p, locals()[p])

    uvc.write_calfits(fname, clobber=clobber)
コード例 #2
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