Exemplo n.º 1
0
 def plot_composite_spectrum(self, nc, hold=False, *args, **kwargs):
     """Plots the redshift composite data. """
     self.check_alive()
     if not hold:
         self.clear()
     if isinstance(nc, RedshiftNetCDFFile):
         hdu = nc.hdu
     elif isinstance(nc, RedshiftScan):
         hdu = nc
     else:
         raise LMTRedshiftError(
             "plot composite spectrum",
             "Argument nc should be of type RedshiftNetCDFFile or RedshiftScan"
         )
     if hasattr(hdu, 'compspectrum'):
         spectrum = blank_to_nan(hdu.compspectrum)
         frequencies = hdu.compfreq
     elif hasattr(hdu.data, 'compspectrum'):
         spectrum = blank_to_nan(hdu.data.compspectrum)
         frequencies = hdu.data.compfreq
     else:
         raise LMTRedshiftError(
             "plot composite spectra",
             """There are no composite spectra in this hdu. Make sure to run make_composite_scan on the HDU first"""
         )
     self.plot(frequencies,
               spectrum.mean(axis=0),
               drawstyle='steps-mid',
               label="CompSpectrum")
     self.set_xlabel("Frequency (GHz)")
     self.set_legend(loc='best', prop=FontProperties(size="xx-small"))
     self.set_ylabel("TA*(K)")
     self.set_subplot_title(
         '%s: Source %s' % (hdu.header.obs_string(), hdu.header.SourceName))
Exemplo n.º 2
0
    def plot_crosscan(self,
                      nc,
                      board=None,
                      hold=False,
                      fitgauss=False,
                      *args,
                      **kwargs):
        """Plots the redshift crossscan data. """
        self.check_alive()
        if not hold:
            self.clear()
        if isinstance(nc, RedshiftNetCDFFile):
            hdu = nc.hdu
        elif isinstance(nc, RedshiftScan):
            hdu = nc
        else:
            raise LMTRedshiftError(
                "plot tsys",
                "Argument nc should be of type RedshiftNetCDFFile or RedshiftScan"
            )
        if not hasattr(hdu, 'continuum'):
            raise LMTRedshiftError(
                "plot crosscan",
                "hdu does not have continuum object. Process first with process_scan()"
            )
        if board is None:
            boards = hdu.header.BoardNumber
        else:
            boards = [board]
        if hdu.header.get('CrossScan.MapCoord') == 'Az':
            x = numpy.degrees(hdu.data.XPos) * 3600
            xlabel = 'Az Offset (")'
        elif hdu.header.get('CrossScan.MapCoord') == 'El':
            x = numpy.degrees(hdu.data.YPos) * 3600
            xlabel = 'El Offset (")'
        for board in boards:
            # if align_sign:
            #     if int(hdu.header.ChassisNumber) in (1, 2):
            #         sgn = -1.0
            #     else:
            #         sgn = 1.0
            # else:
            #     sgn = 1.0
            self.plot(x,
                      hdu.continuum[:, board],
                      drawstyle='steps-mid',
                      label='%d.%d' % (int(hdu.header.ChassisNumber), board))
            if fitgauss:
                fit = Gauss1DFit(x, hdu.continuum[:, board])
                out = fit._run()
                print(out.pprint())
                self.plot(x, out.y, 'r-')

        self.set_xlabel(xlabel)
        self.set_legend(loc='best', prop=FontProperties(size="xx-small"))
        self.set_ylabel("TA*(K)")
        self.set_subplot_title(
            '%s: Source %s' % (hdu.header.obs_string(), hdu.header.SourceName))
Exemplo n.º 3
0
    def plot_map_contour(self,
                         nc,
                         nlevels=10,
                         cmap=cm.get_cmap('Spectral'),
                         board=1,
                         hold=False):
        self.check_alive()
        if not hold:
            self.clear()
        if isinstance(nc, RedshiftNetCDFFile):
            hdu = nc.hdu
        elif isinstance(nc, RedshiftScan):
            hdu = nc
        else:
            raise LMTRedshiftError(
                "plot map",
                "Argument nc should be of type RedshiftNetCDFFile or RedshiftScan"
            )
        if hasattr(hdu, 'BeamMap'):
            beammap = hdu.BeamMap
        else:
            raise LMTRedshiftError(
                "plot map",
                "No BeamMap attribute found. Please run process_scan() on the hdu first"
            )
        hdr, data, fname = self._get_hdu(nc)
        xi = hdu.xi
        yi = hdu.yi
        stepsize = xi[1] - xi[0]
        zi = beammap[:, :, board]
        X, Y = numpy.meshgrid(xi, yi)

        cs = self.contour(xi, yi, zi, nlevels, linewidths=0.5, colors='k')
        cs = self.contourf(xi, yi, zi, nlevels, cmap=cmap)
        self.colorbar()

        def format_coord(x, y):
            ind = numpy.logical_and(
                numpy.abs(X - x) < stepsize,
                numpy.abs(Y - y) < stepsize)
            zzz = nanmean(zi[ind])
            return 'x=%1.4f, y=%1.4f, z=%1.4f' % (x, y, zzz)

        self.set_xlim(xi[0], xi[-1])
        self.set_ylim(yi[0], yi[-1])
        ax, kw = self.plotobj._get_current_axes()
        ax.format_coord = format_coord
        self.redraw_plot()
        self.set_subplot_title(
            '%s: Source %s; Board:%d' %
            (os.path.basename(fname), hdr.get('Source.SourceName', ''), board),
            size=10)
Exemplo n.º 4
0
def ReadAccumFiletoDic(filename, numhead=17, numchan=512):
    """Read accum.dat and return dictionary of header, chan and data arrays
    The dictionary keys are chassis first. Each chassis can have six board keys
    """
    if not os.path.exists(filename):
        raise LMTRedshiftError("ReadAccumFiletoDic",
                               "File %s does not exist" % filename)
    try:
        acc = open(filename, "r")
    except:
        raise LMTRedshiftError("ReadAccumFiletoDic",
                               "File IO error: %s" % filename)
    TotalLines = countLines(acc)
    numlines_per_board = numchan + numhead
    numboards = TotalLines / numlines_per_board
    headers = {}
    chans = {}
    datas = {}
    for boards in range(numboards):
        head = ''
        for line in range(numhead):
            head += acc.readline()
        headerdic = parseheader(head)
        chassis, board = int(headerdic.get('chassis',
                                           0)), int(headerdic.get('board', 0))
        if headers.has_key(chassis):
            headers[chassis][board] = headerdic
        else:
            headers[chassis] = {board: headerdic}
        ch = []
        da = []
        for line in range(numchan):
            (c, d) = acc.readline().split()
            ch.append(float(c))
            da.append(float(d))
        ch = numpy.array(ch)
        da = numpy.array(da)
        if chans.has_key(chassis):
            chans[chassis][board] = ch
        else:
            chans[chassis] = {board: ch}
        if datas.has_key(chassis):
            datas[chassis][board] = da
        else:
            datas[chassis] = {board: da}
    acc.close()
    return (headers, chans, datas)
 def __init__(self, filename, mode='r'):
     if mode in ('r', 'a') and not os.path.exists(filename):
         raise LMTRedshiftError('Redshift NetCDF Error',
                                'File %s not found' % filename)
     LMTNetCDFFile.__init__(self, filename, mode)
     self.filename = filename
     self.hdus = OrderedDict()
     if mode in ('r', 'a'):
         self._make_hdus()
Exemplo n.º 6
0
 def keypress_event(self, event):
     if not event.inaxes: return
     if event.key.lower() == 'n':
         if len(self.winpair) % 2 == 0:
             #start new window pair
             self.winpair = []
             self.winpair.append(event.xdata)
             print(
                 "xwin1 = %.3f. Press 'n' to mark other end of window. 'e' to exit; 'r' to reset"
                 % event.xdata)
             ymin, ymax = self.get_ylims()
             self.basewinlines.append(
                 self.vline(event.xdata,
                            ymin=ymin,
                            ymax=ymax,
                            linestyle='--',
                            color='r',
                            linewidth=2,
                            label='_nolegend_'))
         else:
             #finish existing pair
             self.winpair.append(event.xdata)
             self.basewin.append(self.winpair)
             print(
                 "xwin1 = %.3f, xlim2 = %.3f. Press 'n' to start marking next window., Press 'e' to  exit, 'r' to reset"
                 % (self.winpair[0], self.winpair[1]))
             ymin, ymax = self.get_ylims()
             self.basewinlines.append(
                 self.vline(event.xdata,
                            ymin=ymin,
                            ymax=ymax,
                            linestyle='--',
                            color='r',
                            linewidth=2,
                            label='_nolegend_'))
     elif event.key.lower() == 'e':
         if len(self.winpair) != 2:
             print(
                 "Last window was not completed. It is being dropped. Full baseline window: %s"
                 % self.basewin)
             return
         if hasattr(self.nc.hdu, 'windows') and type(
                 self.nc.hdu.windows) == types.DictType:
             self.nc.hdu.windows[self.board] = self.basewin
         elif hasattr(self.nc.hdu.data, 'windows') and type(
                 self.nc.hdu.data.windows) == types.DictType:
             self.nc.hdu.data.windows[self.board] = self.basewin
         else:
             raise LMTRedshiftError(
                 "make_windows", "netcdf instance has no windows attribute")
         self.disconnect_event(self.basewinsig)
         print("Exiting make_windows. Final baseline windows: %s" %
               self.basewin)
     elif event.key.lower() == 'r':
         self.reset_basewinlines()
Exemplo n.º 7
0
 def plot_tsys(self,
               nc,
               board=None,
               hold=False,
               set_subplot_title=True,
               *args,
               **kwargs):
     """Plots the redshift Tsys data. This is to be obtained
     from the CalObsNum scan in a prescribed way. If the Cal data is not
     available in the scan, it will produce an error"""
     self.check_alive()
     if not hold:
         self.clear()
     if isinstance(nc, RedshiftNetCDFFile):
         hdu = nc.hdu
     elif isinstance(nc, RedshiftScan):
         hdu = nc
     else:
         raise LMTRedshiftError(
             "plot tsys",
             "Argument nc should be of type RedshiftNetCDFFile or RedshiftScan"
         )
     if not hasattr(hdu, 'cal'):
         raise LMTRedshiftError(
             "plot tsys",
             "hdu does not have cal object. Process first with get_cal()")
     if board is None:
         boards = hdu.header.BoardNumber
     else:
         boards = [board]
     for board in boards:
         self.plot(hdu.frequencies[board, :],
                   hdu.cal.Tsys[board, :],
                   drawstyle='steps-mid',
                   label='%d.%d' % (int(hdu.header.ChassisNumber), board))
     self.set_xlabel("Frequency (GHz)")
     self.set_legend(loc='best', prop=FontProperties(size="xx-small"))
     self.set_ylabel("Tsys (K)")
     if set_subplot_title:
         self.set_subplot_title(
             '%s: Source %s' %
             (hdu.header.obs_string(), hdu.header.SourceName))
 def _get_group_hdu(self, groupname):
     if not self.groups.has_key(groupname):
         raise LMTRedshiftError('make_group_hdu',
                                'Group with name %s not found' % groupname)
     group = self.groups[groupname]
     data = self._populate_data(group.variables)
     header = self._populate_headers(group.variables, group.dimensions)
     return RedshiftScan(data=data,
                         header=header,
                         filename=self.filename,
                         groupname=groupname)
Exemplo n.º 9
0
 def _get_hdu(self, nc):
     if isinstance(nc, RedshiftNetCDFFile):
         hdr = nc.hdu.header
         data = nc.hdu.data
         fname = nc.filename
     elif isinstance(nc, RedshiftScan):
         hdr = nc.header
         data = nc.data
         fname = nc.filename
     else:
         raise LMTRedshiftError(
             "Argument Error",
             "Argument nc should be of type RedshiftNetCDFFile or RedshiftScan"
         )
     return hdr, data, fname
Exemplo n.º 10
0
 def plot_line_frequencies(self,
                           nc,
                           yloc=None,
                           elim=[],
                           minint=0.0,
                           z=0.0,
                           txtsize=6.0,
                           hold=True,
                           *args,
                           **kwargs):
     self.check_alive()
     if not hold:
         self.clear()
     if isinstance(nc, RedshiftNetCDFFile):
         hdu = nc.hdu
     elif isinstance(nc, RedshiftScan):
         hdu = nc
     else:
         raise LMTRedshiftError(
             "plot spectrum",
             "Argument nc should be of type RedshiftNetCDFFile or RedshiftScan"
         )
     fmin, fmax = hdu.frequencies.min(), hdu.frequencies.max()
     freqs = numpy.array(linefreqdict.keys())
     redfreqs = freqs / (z + 1.)
     ind = numpy.logical_and(redfreqs >= fmin, redfreqs <= fmax)
     if yloc is None:
         ymin, ymax = self.get_ylims()
         yloc = ymin + (ymax - ymin) * 0.8
     for line in freqs[ind]:
         if linefreqdict[line][2] >= minint:
             if linefreqdict[line][0] not in elim:
                 freqtext = "--" + linefreqdict[line][0]
                 freq = line / (z + 1)
                 self.set_text(freq,
                               yloc,
                               freqtext,
                               rotation='vertical',
                               horizontalalignment='center',
                               verticalalignment='center',
                               size=txtsize)
Exemplo n.º 11
0
 def __init__(self,
              ObsNum,
              chassis,
              usedb=False,
              corr_linear=True,
              chassis_pix_map={
                  0: 'A',
                  1: 'B',
                  2: 'C',
                  3: 'D'
              }):
     """
     corr_linear : correct for linearity issues in pixels
     """
     self.ObsNum = ObsNum
     self.chassis = chassis
     self.chas_str = 'RedshiftChassis_%d_' % self.chassis
     self.chassis_pix_map = chassis_pix_map
     self.corr_linear = corr_linear
     self.SingleFileCal = False
     if usedb:
         #here we do what it takes to access database and get the files
         pass
     else:
         #filenames = glob.glob('/data_lmt/RedshiftChassis%d/RedshiftChassis%d_*_%06d_00_0001.nc' % (self.chassis, self.chassis, self.ObsNum))
         filename = make_generic_filename(self.ObsNum, self.chassis)
         print("Cal filename", filename)
         #ftup = [(time.localtime(os.path.getmtime(f)), f) for f in filenames]
         #ftup.sort()
         #ftup.reverse()
         #fname = ftup[0][1]
         fname = filename
         #nc = RedshiftNetCDFFile(fname)
         nc = RedshiftNetCDFFile(fname)
         self.header = nc.hdu.header
         #eventually replace with real ambient temperature
         #this will be one of the values of Rsfend.Temperature
         #like self.Tamb = self.header.get('Rsfend.Temperature')[6]
         try:
             self.Tamb = self.header.get('Rsfend.Temperature')[6]
         except:
             self.Tamb = 279.0
         if self.Tamb < 100.0:
             #maybe GPIB card is not reading?
             self.Tamb = 279.0  #6 deg C
         if self.Tamb > 400:
             self.Tamb = 279.0
         self.datestr = self._get_datestr(fname)
         #if nc.hdu.header.get('Dcs.Receiver') != 'RedshiftReceiver':
         #    raise LMTRedshiftError('RedshiftCalibration', 'netcdf file %s not a RedshiftReceiver file' % fname)
         if nc.hdu.header.get('Dcs.ObsPgm') != 'Cal':
             raise LMTRedshiftError(
                 'RedshiftCalibration',
                 'netcdf file %s not a RedshiftReceiver Cal file' % fname)
         #self.CalMode = int(nc.hdu.header.get('Dcs.CalMode'))
         self.CalMode = int(nc.hdu.header.get('Cal.CalMode'))
         #self.CalMode = int(nc.hdu.header.get('%s.SwitchSettings' % self.chas_str, 0))
         self.ccal = get_corr_cal_matrix(nc.hdu.header)
         if self.corr_linear:
             nc.hdu._get_freqs()
             self.frequencies = nc.hdu.frequencies
         if nc.hdu.data.AccData.shape[0] == 6:
             self.SingleFileCal = True
             self.process_calibration(self.CalMode, nc)
         else:
             self.SingleFileCal = False
             nc.close()
             self.process_calibration(self.CalMode)
Exemplo n.º 12
0
def make_corr_cal_nc(ncfile=None,
                     chassis=0,
                     direc='.',
                     writedb=False,
                     copy=False):
    """
    Given a directory, this function finds all the
    corr_cal files in that directory in the form of
    corr_cal_ccid_chano_slotno.dat. For each file it
    reads the corr_cal matrix and creates a new netCDF file
    with all corr_cal matrices in it.
    If writedb is True, it will also file the data into the redshiftdb
    database into the corrcal table"""
    ccal = OrderedDict()
    slotdic = _get_bid(direc + os.path.sep + 'calib200.dat', chassis=chassis)
    time_units = "hours since 0001-01-01 00:00:00.0"
    calendar = "gregorian"
    obsdate = _get_obsdate(direc + os.path.sep + 'calib200.dat',
                           chassis=chassis)
    for slotno in (5, 8, 11, 14, 17, 20):
        files = glob.glob(direc + os.path.sep + 'corr_cal_*_%d_%d.dat' %
                          (chassis, slotno))
        if files:
            fname = files[0]
            ccid = os.path.basename(fname).split('_')[2]
            if os.path.exists(fname + '.info'):
                svd, zero_lag = _get_ccal_info(fname + '.info')
            else:
                svd, zero_lag = 0, 0
            corr_cal = numpy.loadtxt(fname)
            ccal[slotno] = (slotno, slotdic[slotno], ccid, svd, zero_lag,
                            corr_cal)
            reducedate = _get_reduce_date(fname)
    if ncfile is None:
        ncfile = direc + os.path.sep + 'corr_cal_%s_%d.nc' % (
            ccal.values()[0][2], chassis)
    nc = LMTNetCDFFile(ncfile, 'w')
    numcards = len(ccal.keys())
    dims = {"numchannels": 256, "scalarval": 1, "ccid_slen": 9}
    create_dimensions(nc, dims, numcards=numcards)
    hvars = {
        "slotno": {
            "type": numpy.dtype(int),
            "dim": ("numcards", ),
            "attributes": {
                "long_name": "VME Slot number for correlator card"
            }
        },
        "boardid": {
            "type": numpy.dtype(int),
            "dim": ("numcards", ),
            "attributes": {
                "long_name":
                "Board ID as inscribed on board, and programmed on card."
            }
        },
        "chassis": {
            "type": numpy.dtype(int),
            "dim": ("scalarval", ),
            "attributes": {
                "long_name": "Chassis Number for the board (one of 0,1,2,3)"
            }
        },
        "corr_cal_id": {
            "type": numpy.dtype(int),
            "dim": ("numcards", ),
            "attributes": {
                "long_name": "corr cal id for each board"
            }
        },
        "svd": {
            "type": numpy.dtype(int),
            "dim": ("numcards", ),
            "attributes": {
                "long_name": "svd cut off value used in corr_cal calculation"
            }
        },
        "zerolag": {
            "type": numpy.dtype(int),
            "dim": ("numcards", ),
            "attributes": {
                "long_name": "zero lag of board"
            }
        },
        "obsdate": {
            "type": numpy.dtype(float),
            "dim": ("scalarval", ),
            "attributes": {
                "long_name": "observed datetime for frequency calibration",
                "units": time_units,
                "calendar": calendar,
            },
        },
        "reducedate": {
            "type": numpy.dtype(float),
            "dim": ("scalarval", ),
            "attributes": {
                "long_name": "reduced datetime for frequency calibration",
                "units": time_units,
                "calendar": calendar,
            },
        },
        "createdate": {
            "type": numpy.dtype(float),
            "dim": ("scalarval", ),
            "attributes": {
                "long_name":
                "creation datetime for frequency calibration netcdf file",
                "units": time_units,
                "calendar": calendar,
            },
        },
    }
    createdate = datetime.datetime.now()
    hdic = create_variables(nc, hvars, hname='RedshiftChassis_%d_' % chassis)
    dvars = {
        "corr_cal": {
            "type": numpy.dtype(float),
            "dim": ("numcards", "numchannels", "numchannels"),
            "attributes": {
                "long_name": "corr_cal matrix"
            }
        }
    }
    ddic = create_variables(nc,
                            dvars,
                            fstr='Data',
                            hname='RedshiftChassis_%d_' % chassis)

    #print ccal
    #fill in header variables
    nc.variables['Header.RedshiftChassis_%d_.slotno' %
                 chassis][:] = numpy.array(ccal.keys(), dtype=numpy.dtype(int))
    nc.variables['Header.RedshiftChassis_%d_.boardid' %
                 chassis][:] = numpy.array([c[1] for c in ccal.values()],
                                           dtype=numpy.dtype(int))
    #print numpy.array([c[1] for c in ccal.values()], dtype=numpy.dtype(int))
    nc.variables['Header.RedshiftChassis_%d_.corr_cal_id' %
                 chassis][:] = numpy.array([int(c[2]) for c in ccal.values()],
                                           dtype=numpy.dtype(int))
    nc.variables['Header.RedshiftChassis_%d_.chassis' % chassis][:] = chassis
    nc.variables['Header.RedshiftChassis_%d_.svd' % chassis][:] = numpy.array(
        [c[3] for c in ccal.values()], dtype=numpy.dtype(int))
    nc.variables['Header.RedshiftChassis_%d_.zerolag' %
                 chassis][:] = numpy.array([c[4] for c in ccal.values()],
                                           dtype=numpy.dtype(int))
    nc.variables['Header.RedshiftChassis_%d_.obsdate' % chassis][:] = date2num(
        obsdate, units=time_units, calendar=calendar)
    nc.variables['Header.RedshiftChassis_%d_.reducedate' %
                 chassis][:] = date2num(reducedate,
                                        units=time_units,
                                        calendar=calendar)
    nc.variables['Header.RedshiftChassis_%d_.createdate' %
                 chassis][:] = date2num(createdate,
                                        units=time_units,
                                        calendar=calendar)

    for i, val in enumerate(ccal.values()):
        nc.variables['Data.RedshiftChassis_%d_.corr_cal' %
                     chassis][i, :] = val[5]
    nc.close()
    print(ccal[5][1])

    if writedb:
        #write to CorrCal Table
        for slotno in ccal.keys():
            ccaltable = CorrCalTable(obsdate=obsdate,
                                     reducedate=reducedate,
                                     createdate=createdate,
                                     chassis=chassis,
                                     boardid=int(ccal[slotno][1]),
                                     slotno=slotno,
                                     svd=ccal[slotno][3],
                                     zerolag=ccal[slotno][4],
                                     corr_cal_id="%s" % ccal[slotno][2],
                                     filename=ncfile,
                                     valid=True)
            ccaltable.save()
    if copy:
        basefile = os.path.basename(ncfile)
        try:
            shutil.copyfile(ncfile, "/raw/rsr/cal" + os.path.sep + basefile)
        except:
            raise LMTRedshiftError(
                "make_corr_cal_nc",
                "Error in copy of file %s to system directory /raw/rsr/cal" %
                basefile)
    return ncfile
Exemplo n.º 13
0
 def find_obspgms(nc):
     """
     If all scans are the same (or all 'Cal' and one other type), return the type of scan.
     If there are different types of scans, return a list with number of scans of each type.
     """
     on = []
     x = 0
     bs = []
     y = 0
     ps = []
     z = 0
     cal = []
     w = 0
     for groupname in self.hdus.keys():
         hdu = self._get_group_hdu(groupname)
         if 'On' in hdu.header.keys():
             on.append(groupname)
             x = 1
         if 'Bs' in hdu.header.keys():
             bs.append(groupname)
             y = 1
         if 'Ps' in hdu.header.keys():
             ps.append(groupname)
             z = 1
         if 'Cal' in hdu.header.keys():
             cal.append(groupname)
             w = 1
     if x + y + z == 0:
         if w == 1:
             raise LMTRedshiftError(
                 'Redshift NetCDF Error',
                 'nc contains only Cal scans. Cal scan export to SDFits not supported.'
             )
         else:
             raise LMTRedshiftError('Redshift NetCDF Error',
                                    'Unknown scan types in nc.')
     elif x + y + z > 1:
         print(
             "There are %d 'On' scans, %d 'Bs' scans, %d 'Ps' scans, and %d 'Cal' scans in this nc."
             % (len(on), len(bs), len(ps), len(cal)))
         return [len(on), len(bs), len(ps), len(cal)]
     elif w == 1:
         if on:
             print(
                 "All scans are 'On' scans and 'Cal' Scans. 'Cal' scan export to SDFits not supported; exporting only 'On' scans."
             )
             return 'On'
         if bs:
             print(
                 "All scans are 'Bs' scans and 'Cal' Scans. 'Cal' scan export to SDFits not supported; exporting only 'Bs' scans."
             )
             return 'Bs'
         if ps:
             print(
                 "All scans are 'Ps' scans and 'Cal' Scans. 'Cal' scan export to SDFits not supported; exporting only 'Ps' scans."
             )
             return 'Ps'
     else:
         if on:
             print("All scans are 'On' scans.")
             return 'On'
         if bs:
             print("All scans are 'Bs' scans.")
             return 'Bs'
         if ps:
             print("All scans are 'Ps' scans.")
             return 'Ps'
Exemplo n.º 14
0
    def export_to_sdfits(self, sdfilename, obs_list=None):
        """
        Convert nc data to fits format and save as a fits file. (sdfilename.fits)
        If nc is hetergeneous (except if all 'Cal' and one other type), ask user whether to save separate fits files.
        If user says no, ask which scan type to save.
        If user does not properly specify, use first non-cal scan type.
        """
        from dreampy.redshift.io.fits.redshift_sdfits import RedshiftSDFITS

        def find_obspgms(nc):
            """
            If all scans are the same (or all 'Cal' and one other type), return the type of scan.
            If there are different types of scans, return a list with number of scans of each type.
            """
            on = []
            x = 0
            bs = []
            y = 0
            ps = []
            z = 0
            cal = []
            w = 0
            for groupname in self.hdus.keys():
                hdu = self._get_group_hdu(groupname)
                if 'On' in hdu.header.keys():
                    on.append(groupname)
                    x = 1
                if 'Bs' in hdu.header.keys():
                    bs.append(groupname)
                    y = 1
                if 'Ps' in hdu.header.keys():
                    ps.append(groupname)
                    z = 1
                if 'Cal' in hdu.header.keys():
                    cal.append(groupname)
                    w = 1
            if x + y + z == 0:
                if w == 1:
                    raise LMTRedshiftError(
                        'Redshift NetCDF Error',
                        'nc contains only Cal scans. Cal scan export to SDFits not supported.'
                    )
                else:
                    raise LMTRedshiftError('Redshift NetCDF Error',
                                           'Unknown scan types in nc.')
            elif x + y + z > 1:
                print(
                    "There are %d 'On' scans, %d 'Bs' scans, %d 'Ps' scans, and %d 'Cal' scans in this nc."
                    % (len(on), len(bs), len(ps), len(cal)))
                return [len(on), len(bs), len(ps), len(cal)]
            elif w == 1:
                if on:
                    print(
                        "All scans are 'On' scans and 'Cal' Scans. 'Cal' scan export to SDFits not supported; exporting only 'On' scans."
                    )
                    return 'On'
                if bs:
                    print(
                        "All scans are 'Bs' scans and 'Cal' Scans. 'Cal' scan export to SDFits not supported; exporting only 'Bs' scans."
                    )
                    return 'Bs'
                if ps:
                    print(
                        "All scans are 'Ps' scans and 'Cal' Scans. 'Cal' scan export to SDFits not supported; exporting only 'Ps' scans."
                    )
                    return 'Ps'
            else:
                if on:
                    print("All scans are 'On' scans.")
                    return 'On'
                if bs:
                    print("All scans are 'Bs' scans.")
                    return 'Bs'
                if ps:
                    print("All scans are 'Ps' scans.")
                    return 'Ps'

        check_sdfilename = sdfilename.split('.')
        if check_sdfilename[-1] == 'fits':
            junk_variable = check_sdfilename.pop()
            sdfilename = '.'.join(check_sdfilename)
        if os.path.exists(sdfilename + '.fits'):
            raise LMTRedshiftError('Redshift NetCDF Error',
                                   "Filename already exists.")
        if (type(obs_list) != list) and (obs_list != None):
            raise LMTRedshiftError('Redshift NetCDF Error',
                                   "obs_list must be a list.")
        if (self.file_format == 'NETCDF3_CLASSIC') or (len(self.hdus) == 1):
            sdfits = RedshiftSDFITS(self)
            sdfits.save_fits(sdfilename + '.fits')
        else:
            obspgm = find_obspgms(self)
            if obs_list != None:
                if ('Bs' in obs_list) or ('Ps' in obs_list) or ('On'
                                                                in obs_list):
                    any_scans = False
                    if ('On' in obs_list) and (obspgm[0] > 0):
                        if os.path.exists(sdfilename + '_On.fits'):
                            print("%s_On.fits already exists" % sdfilename)
                            raise LMTRedshiftError('Redshift NetCDF Error',
                                                   "Filename already exists.")
                        sdfits_On = RedshiftSDFITS(self, obspgm='On')
                        sdfits_On.save_fits(sdfilename + '_On.fits')
                        print("On scans exported to %s_On.fits" % sdfilename)
                        any_scans = True
                    if ('Bs' in obs_list) and (obspgm[1] > 0):
                        if os.path.exists(sdfilename + '_Bs.fits'):
                            print("%s_Bs.fits already exists" % sdfilename)
                            raise LMTRedshiftError('Redshift NetCDF Error',
                                                   "Filename already exists.")
                        sdfits_Bs = RedshiftSDFITS(self, obspgm='Bs')
                        sdfits_Bs.save_fits(sdfilename + '_Bs.fits')
                        print("Bs scans exported to %s_Bs.fits" % sdfilename)
                        any_scans = True
                    if ('Ps' in obs_list) and (obspgm[2] > 0):
                        if os.path.exists(sdfilename + '_Ps.fits'):
                            print("%s_Ps.fits already exists" % sdfilename)
                            raise LMTRedshiftError('Redshift NetCDF Error',
                                                   "Filename already exists.")
                        sdfits_Ps = RedshiftSDFITS(self, obspgm='Ps')
                        sdfits_Ps.save_fits(sdfilename + '_Ps.fits')
                        print("Ps scans exported to %s_Ps.fits" % sdfilename)
                        any_scans = True
                    if any_scans == False:
                        print("No scans of the type(s) you wanted.")
                else:
                    raise LMTRedshiftError(
                        'Redshift NetCDF Error',
                        "obs_list must contain at least one of the strings 'On','Bs', and 'Ps'."
                    )
            elif type(obspgm) == str:
                if obspgm == 'Cal':
                    raise LMTRedshiftError(
                        'Redshift NetCDF Error',
                        "'Cal' scan export to SDFits not supported.")
                else:
                    sdfits = RedshiftSDFITS(self, obspgm=obspgm)
                    sdfits.save_fits(sdfilename + '.fits')
            else:
                multiple_files = raw_input(
                    'Would you like to save each scan type as its own SDFits file? (excluding Cal) (y/n): '
                )
                multiple_files = multiple_files.lower()
                if multiple_files == 'y' or multiple_files == 'yes':
                    if obspgm[0]:
                        if os.path.exists(sdfilename + '_On.fits'):
                            print("%s_On.fits already exists" % sdfilename)
                            raise LMTRedshiftError('Redshift NetCDF Error',
                                                   "Filename already exists.")
                        sdfits_On = RedshiftSDFITS(self, obspgm='On')
                        sdfits_On.save_fits(sdfilename + '_On.fits')
                        print("On scans exported to %s_On.fits" % sdfilename)
                    if obspgm[1]:
                        if os.path.exists(sdfilename + '_Bs.fits'):
                            print("%s_Bs.fits already exists" % sdfilename)
                            raise LMTRedshiftError('Redshift NetCDF Error',
                                                   "Filename already exists.")
                        sdfits_Bs = RedshiftSDFITS(self, obspgm='Bs')
                        sdfits_Bs.save_fits(sdfilename + '_Bs.fits')
                        print("Bs scans exported to %s_Bs.fits" % sdfilename)
                    if obspgm[2]:
                        if os.path.exists(sdfilename + '_Ps.fits'):
                            print("%s_Ps.fits already exists" % sdfilename)
                            raise LMTRedshiftError('Redshift NetCDF Error',
                                                   "Filename already exists.")
                        sdfits_Ps = RedshiftSDFITS(self, obspgm='Ps')
                        sdfits_Ps.save_fits(sdfilename + '_Ps.fits')
                        print("Ps scans exported to %s_Ps.fits" % sdfilename)
                    if obspgm[3]:
                        print(
                            "Reminder: 'Cal' scans not saved. ('Cal' scan export to SDFits not supported.)"
                        )
                elif obspgm[0] == obspgm[1] == obspgm[2] == 1:
                    two_scans = raw_input(
                        'Would you liked to save two scan types but not the third? (y/n): '
                    )
                    if two_scans == 'y' or two_scans == 'yes':
                        dont_save = raw_input(
                            'Which scan type would you like NOT to save? (On, Bs, Ps -- Case Sensitive): '
                        )
                        if (dont_save != 'On') and (dont_save != 'Bs') and (
                                dont_save != 'Ps'):
                            print(
                                "Scan type entered incorrectly. Enter only two letters. First should be uppercase and second should be lowercase."
                            )
                            dont_save = raw_input(
                                'One more try... Which scan type would you like NOT to save? (On, Bs, Ps -- Case Sensitive): '
                            )
                        if (dont_save != 'On') and (dont_save != 'Bs') and (
                                dont_save != 'Ps'):
                            raise LMTRedshiftError(
                                'Redshift NetCDF Error',
                                "Scan type entered incorrectly.")
                        if dont_save != 'On':
                            if os.path.exists(sdfilename + '_On.fits'):
                                print("%s_On.fits already exists" % sdfilename)
                                raise LMTRedshiftError(
                                    'Redshift NetCDF Error',
                                    "Filename already exists.")
                            sdfits_On = RedshiftSDFITS(self, obspgm='On')
                            sdfits_On.save_fits(sdfilename + '_On.fits')
                            print("On scans exported to %s_On.fits" %
                                  sdfilename)
                        if dont_save != 'Bs':
                            if os.path.exists(sdfilename + '_Bs.fits'):
                                print("%s_Bs.fits already exists" % sdfilename)
                                raise LMTRedshiftError(
                                    'Redshift NetCDF Error',
                                    "Filename already exists.")
                            sdfits_Bs = RedshiftSDFITS(self, obspgm='Bs')
                            sdfits_Bs.save_fits(sdfilename + '_Bs.fits')
                            print("Bs scans exported to %s_Bs.fits" %
                                  sdfilename)
                        if dont_save != 'Ps':
                            if os.path.exists(sdfilename + '_Ps.fits'):
                                print("%s_Ps.fits already exists" % sdfilename)
                                raise LMTRedshiftError(
                                    'Redshift NetCDF Error',
                                    "Filename already exists.")
                            sdfits_Ps = RedshiftSDFITS(self, obspgm='Ps')
                            sdfits_Ps.save_fits(sdfilename + '_Ps.fits')
                            print("Ps scans exported to %s_Ps.fits" %
                                  sdfilename)
                    else:
                        observepgm = raw_input(
                            'Select which scan type to save. (On, Bs, Ps -- Case Sensitive): '
                        )
                        if (observepgm == 'On') or (observepgm
                                                    == 'Bs') or (observepgm
                                                                 == 'Ps'):
                            sdfits = RedshiftSDFITS(self, obspgm=observepgm)
                            sdfits.save_fits(sdfilename + '.fits')
                            print("%s scans exported to %s.fits" %
                                  (observepgm, sdfilename))
                        else:
                            print(
                                "Saving only scans with obspgm of first scan that isn't a cal scan."
                            )
                            sdfits = RedshiftSDFITS(self)
                            sdfits.save_fits(sdfilename + '.fits')
                            print("%s scans exported to %s.fits" %
                                  (sdfits.obspgm, sdfilename))
                else:
                    observepgm = raw_input(
                        'Select which scan type to save. (On, Bs, Ps -- Case Sensitive): '
                    )
                    if (observepgm == 'On') or (observepgm
                                                == 'Bs') or (observepgm
                                                             == 'Ps'):
                        sdfits = RedshiftSDFITS(self, obspgm=observepgm)
                        sdfits.save_fits(sdfilename + '.fits')
                        print("%s scans exported to %s.fits" %
                              (observepgm, sdfilename))
                    else:
                        print(
                            "Saving only scans with obspgm of first scan that isn't a cal scan."
                        )
                        sdfits = RedshiftSDFITS(self)
                        sdfits.save_fits(sdfilename + '.fits')
                        print("%s scans exported to %s.fits" %
                              (sdfits.obspgm, sdfilename))
Exemplo n.º 15
0
 def plot_data(self,
               nc,
               board=None,
               datatype='AccData',
               rpt=None,
               xtype='Chan',
               norm=True,
               hold=False,
               dosubplots=True,
               smooth=1,
               *args,
               **kwargs):
     """
     Plots one of datatype = 'AccData', or 'RefData'
     against xtype.
     xtype can be one of 'Chan' or 'Freq'
     None (just uses index numbers).
     The smooth parameter allows a simple boxcar average of input
     data. If smooth=1, raw data is plotted, smooth=10 does a boxcar
     average of 10 adjacent samples.
     """
     self.check_alive()
     if not hold:
         self.clear()
     if datatype not in ('AccData', 'RefData'):
         raise LMTRedshiftError(
             "Argument Error",
             """Datatype should be one of 'AccData', 'RefData'""")
     if xtype not in ('chan', 'Chan', 'c', 'Freq', 'freq', 'f', None):
         raise LMTRedshiftError(
             "Argument Error",
             """xtype should be one of 'Chan', 'Freq', None""")
     hdr, data, fname = self._get_hdu(nc)
     if xtype is None:
         xlabel = 'Index'
         x = numpy.arange(data.AccData.shape[2])
     elif xtype.lower() in ('chan', 'c'):
         xlabel = 'Chan'
         x = numpy.arange(data.AccData.shape[2])
     xlabel = xtype
     if board is None:
         #do all boards
         boards = hdr.BoardNumber
     else:
         if board < 0 or board > 5:
             raise LMTRedshiftError("Argument Error",
                                    """board should be between 0 and 5""")
         boards = [board]
     if rpt is None:
         #do all repeats
         #repeats = range(data.AccData.shape[0])
         repeats = self._get_repeats(nc)
         rpt = 1
     else:
         repeats = [rpt - 1]
     for repeat in range(repeats):
         for board in boards:
             if dosubplots:
                 self.add_subplot(2, 3, board + 1)
             dt = getattr(data, datatype).copy()[repeat, board, :]
             dt = apply_bad_lags(dt, int(hdr.ChassisNumber), board)
             if norm:
                 dt = dt / float(data.AccSamples[repeat, board])
             self.plot(
                 rebin(x, smooth=smooth),
                 rebin(dt, smooth=smooth),
                 drawstyle='steps',
                 label="%s C%dB%d r%d" %
                 (datatype, int(hdr.ChassisNumber), board + 1, repeat),
                 *args,
                 **kwargs)
             self.set_legend(loc='best',
                             prop=FontProperties(size="xx-small"))
             self.set_xlabel(xlabel)
             self.set_ylabel(datatype)
     self.set_figtitle('%s: Source %s' % (hdr.obs_string(), hdr.SourceName))
Exemplo n.º 16
0
    def implot_map(self,
                   nc,
                   board=1,
                   vmin=None,
                   vmax=None,
                   extents=None,
                   colorbar=True,
                   hold=False,
                   **kwargs):
        self.check_alive()
        if not hold:
            self.clear()
        if isinstance(nc, RedshiftNetCDFFile):
            hdu = nc.hdu
        elif isinstance(nc, RedshiftScan):
            hdu = nc
        else:
            raise LMTRedshiftError(
                "implot map",
                "Argument nc should be of type RedshiftNetCDFFile or RedshiftScan"
            )
        if hasattr(hdu, 'BeamMap'):
            beammap = hdu.BeamMap
        else:
            raise LMTRedshiftError(
                "implot map",
                "No BeamMap attribute found. Please run process_scan() on the hdu first"
            )
        hdr, data, fname = self._get_hdu(nc)
        xi = hdu.xi
        yi = hdu.yi
        stepsize = xi[1] - xi[0]
        zi = beammap[:, :, board]
        if extents is None:
            extents = [xi[0], xi[-1], yi[0], yi[-1]]

        print(extents)
        self.image = self.imshow(zi,
                                 cmap=cm.get_cmap('Spectral'),
                                 interpolation='bilinear',
                                 vmin=vmin,
                                 vmax=vmax,
                                 origin='lower',
                                 extent=extents,
                                 **kwargs)
        X, Y = numpy.meshgrid(xi, yi)

        def format_coord(x, y):
            ind = numpy.logical_and(
                numpy.abs(X - x) < stepsize,
                numpy.abs(Y - y) < stepsize)
            zzz = nanmean(zi[ind])
            return 'x=%1.4f, y=%1.4f, z=%1.4f' % (x, y, zzz)

        ax, kw = self.plotobj._get_current_axes()
        ax.format_coord = format_coord
        self.set_subplot_title(
            '%s: Source %s; Board:%d' %
            (os.path.basename(fname), hdr.get('Source.SourceName', ''), board),
            size=10)
        if colorbar:
            self.colorbar()
        return self.image
Exemplo n.º 17
0
 def plot_spectra(self,
                  nc,
                  board=None,
                  avgrepeats=True,
                  rpt=None,
                  hold=False,
                  *args,
                  **kwargs):
     """Plots the redshift spectral data. This is to be obtained
     from the ACF in a prescribed way. If the spectral data is not
     available in the scan, it will produce an error"""
     if isinstance(nc, RedshiftNetCDFFile):
         hdu = nc.hdu
     elif isinstance(nc, RedshiftScan):
         hdu = nc
     else:
         raise LMTRedshiftError(
             "plot spectra",
             "Argument nc should be of type RedshiftNetCDFFile or RedshiftScan"
         )
     if hasattr(hdu, 'spectrum'):
         spectrum = blank_to_nan(hdu.spectrum)
         frequencies = hdu.frequencies
     elif hasattr(hdu.data, 'spectrum'):
         spectrum = blank_to_nan(hdu.data.spectrum)
         frequencies = hdu.data.frequencies
     else:
         raise LMTRedshiftError(
             "plot spectra",
             """There are no spectra in this hdu. Make sure to run process_scan on the HDU first"""
         )
     if hasattr(hdu, 'ylabel'):
         ylabel = hdu.ylabel
     elif hasattr(hdu.header, 'ylabel'):
         ylabel = hdu.header.ylabel
     else:
         ylabel = ''
     self.check_alive()
     if not hold:
         self.clear()
     if board is None:
         boards = hdu.header.BoardNumber
     else:
         boards = [board]
     if rpt is None:
         #do all repeats
         #repeats = range(hdu.data.AccData.shape[0])
         repeats = self._get_repeats(nc)
         rpt = 1
     else:
         repeats = [rpt - 1]
     if avgrepeats:
         for board in boards:
             self.plot(frequencies[board, :],
                       spectrum[:, board, :].mean(axis=0),
                       drawstyle='steps-mid',
                       label='%d.%d' %
                       (int(hdu.header.ChassisNumber), board))
     else:
         for repeat in range(repeats):
             for board in boards:
                 self.plot(frequencies[board, :],
                           spectrum[repeat, board, :],
                           drawstyle='steps-mid',
                           label='%d.%d r%d' %
                           (int(hdu.header.ChassisNumber), board, repeat))
     self.set_xlabel("Frequency (GHz)")
     self.set_legend(loc='best', prop=FontProperties(size="xx-small"))
     self.set_ylabel(ylabel)
     self.set_subplot_title(
         '%s: Source %s' % (hdu.header.obs_string(), hdu.header.SourceName))