def __init__(self, filename): """Initializes a config file if does not exist. If exists, uses it to validate the file, and setup default initial parameters""" self.cfg_spec = ConfigObj(config_spec_text.splitlines(), list_values=False) self.cfg_filename = filename valid = Validator() if not os.path.exists(self.cfg_filename): #no .dreampyrc file found cfg = ConfigObj(configspec=self.cfg_spec, stringify=True, list_values=True) cfg.filename = self.cfg_filename test = cfg.validate(valid, copy=True) cfg.write() self.cfg = ConfigObj(self.cfg_filename, configspec=self.cfg_spec) rtn = self.cfg.validate(valid, preserve_errors=True) if isinstance(rtn, bool) and rtn: logger.info("Config file validated") self.tested = True else: self.tested = False res = flatten_errors(self.cfg, rtn) self.errortxt = '' for row in res: self.errortxt += 'In Section %s, key %s has error: %s' % ( row[0], row[1], row[2]) logger.error(self.errortxt)
def __init__(self, errorname, reason): """ @param errorname: The name of the error for exception. @type errorname: String @param reason: An explanatory text that details the error @type reason: String """ self.errorname = errorname self.reason = reason self.args = (self.reason, ) self.message = errorname self.message += ": " self.message += reason logger.error(self.message)
def process_cross_scan( self, stepsize=1.0, # in arcseconds apply_Tsys=False, Tsys=[], baseline=True, ): """ Cross scan If apply_Tsys is True, pass a list or dictionary for Tsys with Tsys values. If baseline is True, removes the off-source value from the overall measurements """ if self.header.get('Dcs.ObsPgm') != 'CrossScan': logger.error("Not a cross-scan datafile") return self.numpixels = self.data.BasebandLevel.shape[1] logger.info( "Processing IFProc Continuum CrossScan Observation with ObsNum %d" % int(self.header.get('Dcs.ObsNum'))) ind = numpy.where(self.data.BufPos == 0) self.off_source = {} for pixel in range(self.numpixels): self.off_source[pixel] = numpy.histogram( self.data.BasebandLevel[ind, pixel].flatten())[1][:4].mean() if self.header.get('CrossScan.MapCoord') == 'Az': xpos = numpy.degrees(self.data.TelAzMap[ind]) * 3600. else: xpos = numpy.degrees(self.data.TelElMap[ind]) * 3600. mapsize = 2 * int( ((xpos[-1] - xpos[0]) * 0.95) / 2.) # slightly smaller numsteps = int(mapsize / stepsize + 1) self.grid = numpy.linspace(-mapsize / 2, mapsize / 2, numsteps) self.continuum = numpy.zeros((self.grid.size, self.numpixels), dtype=self.data.BasebandLevel.dtype) cont = {} if baseline: for pixel in range(self.numpixels): cont[pixel] = (self.data.BasebandLevel[ind, pixel] - self.off_source[pixel]).flatten() else: for pixel in range(self.numpixels): cont[pixel] = self.data.BasebandLevel[ind, pixel].flatten() for pixel in range(self.numpixels): f = interp1d(xpos, cont[pixel]) self.continuum[:, pixel] = f(self.grid) return self.continuum
def validate_dictionary(cdic): """This function validates a dictionary against the config spec here""" cfg_spec = ConfigObj(config_spec_text.splitlines(), list_values=False) valid = Validator() cfg = ConfigObj(cdic, configspec=cfg_spec) rtn = cfg.validate(valid, preserve_errors=True) if isinstance(rtn, bool) and rtn: return True else: res = flatten_errors(cfg, rtn) errortxt = '' for row in res: errortxt += 'In Section %s, key %s has error: %s' % ( row[0], row[1], row[2]) logger.error(errortxt) return False
def process_map(self, remove_offset=False, numoff=20, scigrid=False, **kwargs): """ processes Map Obspgm that has been made using compressed continuum mode. Uses a regridding algorithm and uses some kwargs arguments to derive output grid size and sampling """ if self.header.ObsPgm not in ('Map', 'Lissajous'): logger.error("Not a Map datafile") return else: maptype = self.header.ObsPgm logger.info( "Processing MSIP 1mm Continuum Map data and regridding for Observation with ObsNum %d" % int(self.header.ObsNum)) self.numpixels = self.data.BasebandLevel.shape[1] xlength = numpy.degrees(self.header.get( '%s.XLength' % maptype)) * 3600.0 ylength = numpy.degrees(self.header.get( '%s.YLength' % maptype)) * 3600.0 if maptype == 'Lissajous': xlength = xlength / numpy.cos(numpy.radians(45)) xlength = ylength / numpy.cos(numpy.radians(45)) ind = numpy.where(self.data.BufPos == 0) xpos = numpy.degrees(self.data.TelAzMap[ind]) * 3600. ypos = numpy.degrees(self.data.TelElMap[ind]) * 3600. rows = self.header.get('Map.RowsPerScan') z = {} self.off_source = {} for chan in range(self.numpixels): z[chan] = self.data.BasebandLevel[ind, chan].flatten() self.off_source[chan] = numpy.histogram( self.data.BasebandLevel[ind, chan].flatten())[1][:4].mean() if remove_offset: z[chan] = z[chan] - self.off_source[chan] print(z[chan].shape) ramp = kwargs.get('ramp', 5.) numpoints = kwargs.get('numpoints', 100) numypoints = kwargs.get('numypoints', 100) xlength = xlength * (1. - ramp / 100.) ylength = ylength * (1. - ramp / 100.) ind = numpy.logical_and(xpos > -xlength / 2., xpos < xlength / 2.) xpos, ypos = xpos[ind], ypos[ind] # add a tiny random number to stop griddata from crashing when two pixels are same xpos = xpos + numpy.random.random(xpos.size) * 1e-6 ypos = ypos + numpy.random.random(ypos.size) * 1e-6 for chan in range(self.numpixels): z[chan] = z[chan][ind] ind = numpy.logical_and(ypos > -ylength / 2., ypos < ylength / 2.) xpos, ypos = xpos[ind], ypos[ind] for chan in range(self.numpixels): z[chan] = z[chan][ind] self.xi = numpy.linspace(-xlength / 2, xlength / 2, numpoints) self.yi = numpy.linspace(-ylength / 2, ylength / 2, numypoints) print("Making %d x %d map" % (numpoints, numypoints)) #self.z = z #self.xpos = xpos #self.ypos = ypos self.BeamMap = numpy.zeros( (self.yi.size, self.xi.size, self.numpixels)) for chan in range(self.numpixels): #self.BeamMap[chan] = numpy.zeros((self.yi.size, self.xi.size), # dtype='float') if scigrid: self.BeamMap[:, :, chan] = scipy.interpolate.griddata( xpos, ypos, z[chan], (self.xi, self.yi), method='cubic') else: self.BeamMap[:, :, chan] = griddata(xpos, ypos, z[chan], self.xi, self.yi)