def findSkyLevel(self): Psky = fm.array( self.data_sci['array'].copy().values / (self.data_sci['integtime'].copy().values)[:, None]).mean('ch') date = self.data_sci['date'].copy().values t_sci = np.array([s[:-4] for s in date], 'datetime64[us]') flag1 = t_sci < t_sci[-1] #flag2 = data_sci['bufpos'] == 'ON' flag = flag1 #& flag2 model_sky = models.Polynomial1D(2) model_sky.c0 = 0.0 model_sky.c1 = -1.0 model_sky.c2 = 1.0 pfit = fitting.LinearLSQFitter() opfit = fitting.FittingWithOutlierRemoval(pfit, sigma_clip, niter=15, sigma=2.0) #fitted_sky = pfit(model_sky, t_sci[flag], Psky[flag]) fitted_sky, filtered_data = opfit(model_sky, t_sci[flag] - t_sci[0], Psky[flag]) #opfit = fitting.FittingWithOutlierRemoval(pfit, sigma_clip,niter=3, sigma=3.0) #fitted_sky,filtered_data = opfit(model_sky,t_sci[flag][~filtered_data],Psky[flag][~filtered_data]) #print(filtered_data) #print(fitted_sky.c1,fitted_sky.c0) self.t_sci = t_sci self.fitted_sky = fitted_sky self.Psky = Psky self.flag = flag self.filtered_data = filtered_data
def to_logG(ilogG): ifmch = ilogG.fmch.values fmch = np.fromstring(ilogG.bfmch.values, i8) logG = interp1d(ifmch, ilogG, axis=0)(fmch) # coords tcoords = {'fmch': fmch} chcoords = deepcopy(ilogG.fma.chcoords) return fm.array(logG, tcoords, chcoords)
def to_ilogG(logG): bfmch = logG.fmch.values.astype(i8).tobytes() ifmch = np.arange(logG.fmch.min(), logG.fmch.max() + 1) glogG = logG.groupby('fmch').mean('t') ilogG = interp1d(glogG.fmch, glogG, axis=0)(ifmch) # coords tcoords = {'fmch': ifmch} chcoords = deepcopy(logG.fma.chcoords) scalarcoords = {'bfmch': bfmch} return fm.array(ilogG, tcoords, chcoords, scalarcoords)
def modulate(self): """Create a modulated array from the demodulated one. This method is only available when the array is demodulated. It is equivalent to the fm.modulate function (recommended to use). i.e. array.fma.modulate() <=> fm.modulate(array) Returns: array (xrray.DataArray): A modulated array. """ if self.ismodulated: raise FMArrayError('already modulated') # modulate data fmch = self.fmch.values.copy() lextch = np.max([0, np.min(self.chid.values)]) rextch = np.max([0, np.min((self.chno - self.chid).values)]) extshape = (self.shape[0], self.shape[1] + lextch + rextch) extchid = np.arange( np.min(self.chid) - lextch, np.max(self.chid) + rextch + 1) newchid = np.arange(self.chno) if np.ptp(fmch): data = np.full(extshape, np.nan) data[:, lextch:extshape[1] - rextch] = self.values data = fm.utils.rollrows(data, -fmch) data = data[:, np.in1d(extchid, newchid)] else: data = self.values.copy() # update coords fmch = -fmch if self.isdemodulated_r else fmch fsig = interp1d(self.chid, self.fsig, fill_value='extrapolate')(newchid) fimg = interp1d(self.chid, self.fimg, fill_value='extrapolate')(newchid) tcoords = deepcopy(self.tcoords) tcoords.update({'fmch': fmch}) chcoords = deepcopy(self.chcoords) chcoords.update({'fsig': fsig, 'fimg': fimg}) chcoords.pop('chid') scalarcoords = deepcopy(self.scalarcoords) scalarcoords.update({'status': self.MODULATED}) scalarcoords.pop('chno') return fm.array(data, tcoords, chcoords, scalarcoords)
def ones(shape, dtype=None, **kwargs): """Create a modulated array of given shape and type, filled with ones. Args: shape (sequence of ints): 2D shape of the array. dtype (data-type, optional): The desired data-type for the array. kwargs (optional): Other arguments of the array (*coords, attrs, and name). Returns: array (xarray.DataArray): A modulated array filled with ones. """ data = np.ones(shape, dtype) return fm.array(data, **kwargs)
def demodulate(self, reverse=False): """Create a demodulated array from the modulated one. This method is only available when the array is modulated. It is equivalent to the fm.demodulate function (recommended to use). i.e. array.fma.demodulate(reverse) <=> fm.demodulate(array, reverse) Args: reverse (bool, optional): If True, the array is reverse-demodulated (i.e. -1 * fmch is used for demodulation). Default is False. Returns: array (xarray.DataArray): A demodulated array. """ if self.isdemodulated: raise FMArrayError('already demodulated') # demodulate data fmch = self.fmch.values.copy() fmch = -fmch if reverse else fmch newshape = (self.shape[0], self.shape[1] + np.ptp(fmch)) if np.ptp(fmch): data = np.full(newshape, np.nan) data[:, :-np.ptp(fmch)] = self.values data = fm.utils.rollrows(data, fmch - np.min(fmch)) else: data = self.values.copy() # update coords chno = self.shape[1] chid = np.arange(np.min(fmch), np.min(fmch) + newshape[1]) fsig = interp1d(np.arange(chno), self.fsig, fill_value='extrapolate')(chid) fimg = interp1d(np.arange(chno), self.fimg, fill_value='extrapolate')(chid) status = self.DEMODULATED_R if reverse else self.DEMODULATED tcoords = deepcopy(self.tcoords) tcoords.update({'fmch': fmch}) chcoords = deepcopy(self.chcoords) chcoords.update({'fsig': fsig, 'fimg': fimg, 'chid': chid}) scalarcoords = deepcopy(self.scalarcoords) scalarcoords.update({'status': status, 'chno': chno}) return fm.array(data, tcoords, chcoords, scalarcoords)
def RSkyCal(self): self.findSkyLevel() self.P_cal = fm.array( self.data_cal['array'].copy().values / (self.data_cal['integtime'].copy().values)[:, None]).mean('ch') #Psky = findSkyLevel(data_sci) # TSYS self.Tsys = self.Tamb * ( self.P_cal[self.data_cal['chopperpos'] == 'S']).mean('t') / ( (self.P_cal[self.data_cal['chopperpos'] == 'R']).mean('t') - (self.P_cal[self.data_cal['chopperpos'] == 'S']).mean('t')) # R-sky self.Ta = self.Tsys * (self.Psky - self.fitted_sky(self.t_sci - self.t_sci[0]) ) / self.fitted_sky(self.t_sci - self.t_sci[0])
def getarray(fitsname, arrayid, scantype, offsetsec=0.0, *, computeam=True, ignore_antennalog=False): """Create a modulated array from a FMFITS. Args: fitsname (str): File name of a FMFITS. arrayid (str): An array ID with which the output fmarray is created. scantype (str): A scan type with which the output fmarray is created. offsetsec (float, optional): A float value of FM offset time in units of sec. computeam (bool, optional): If True, atmospheric model is computed. Default is True. ignore_antennalog (bool, optional): Whether ignoring antenna log. Default is False. Returns: array (xarray.DataArray): A modulated array of the spacified `arrayid` and `scantype`. """ with fits.open(Path(fitsname).expanduser()) as f: # fits data fmlo = f['fmlolog'].data be = f['backend'].data if 'antenna' in f: ant = f['antenna'].data # info for *coords d = f['obsinfo'].data info = dict(zip(d.names, d[d['arrayid'] == arrayid][0])) info.update(f['obsinfo'].header) # ptcoords ptcoords = { 'xref': info['RA'], 'yref': info['DEC'], 'coordsys': 'RADEC', 'status': 'MODULATED', 'restfreq': info['rfcenter'], } # chcoords step = info['chwidth'] start = info['rfcenter'] - step * (info['chcenter'] - 1) end = start + step * info['chtotaln'] chcoords = { 'fsig': np.arange(start, end, step), 'fimg': np.arange(start, end, step)[::-1] - 2 * info['ifcenter'], } # tcoords and flags tcoords = {} if scantype == 'ON': t, flag_fmlo, flag_be, flag_ant = makeflags( f, arrayid, scantype, offsetsec) tcoords.update({ 'fmch': (fmlo['FMFREQ'][flag_fmlo] / step).astype(i8), 'vrad': fmlo['VRAD'][flag_fmlo].astype(f8), 'time': t, }) if 'antenna' in f: if not ignore_antennalog: tcoords.update({ 'x': ant['RA'][flag_ant], 'y': ant['DEC'][flag_ant], }) else: flag_be = (be['arrayid'] == arrayid) & (be['scantype'] == scantype) # finally data = be['arraydata'][flag_be].astype(f8) array = fm.array(data, tcoords, chcoords, ptcoords) if scantype == 'ON': array = array.squeeze() if computeam: fm.models.computeam(array) return array else: labels = array.fma.tcoords.keys() return array.squeeze().drop(labels)