def read_point_offsets(fname): """Reads per-tod pointing offsets, returning it in the form {todID: [dx,dy]).""" res = {} for line in utils.lines(fname): if line[0] == '#': continue toks = line.split() id = ".".join(toks[0].split(".")[:3]) res[id] = np.array([float(toks[5]),float(toks[6])]) return res
def read_noise_cut(fname, id=None): """Given a filename, reads the set of detectors to cut for each tod, returning it as a dictionary of id:detlist.""" res = {} for line in utils.lines(fname): if line[0] == '#': continue if id and not line.startswith(id): continue toks = line.split() res[toks[0]] = np.array([int(w) for w in toks[2:]],dtype=np.int32) return res
def read_tags(fname): """Read a set of detector tag definitions from file. Returns a dict[tag] -> array of ids.""" res = {} for line in utils.lines(fname): toks = line.split() name = toks[0] ids = np.array([int(tok) for tok in toks[1:]],dtype=int) res[name] = ids return res
def read_pickup_cut(fname): """Given a filename, reads cuts in the pickup cut format id scan_direction hex azpix1 azpix2 az1 az2 strength.""" res = {} for line in utils.lines(fname): if line[0] == '#': continue id, dir, hex, ap1, ap2, az1, az2, strength = line.split() if id not in res: res[id] = [] res[id].append([int(dir),int(hex),float(az1),float(az2),float(strength)]) return res
def read_point_slopes(fname): """Reads per-tod pointing slopes, returning it in the form {todID: [az0, xslope,yslope]). az0 will be in degrees and the slopes in arcmin per degree.""" res = {} for line in utils.lines(fname): if line[0] == '#': continue toks = line.split() id = ".".join(toks[0].split(".")[:3]) res[id] = np.array([float(toks[1]), float(toks[2]), float(toks[3])]) return res
def read_site(fname): """Given a filename or file, parse a file with key = value information and return it as a Bunch.""" res = bunch.Bunch() for line in utils.lines(fname): line = line.strip() if len(line) == 0 or line.startswith("#"): continue a = ast.parse(line) id = a.body[0].targets[0].id res[id] = ast.literal_eval(a.body[0].value) return res
def read_polangle(fname, mode="auto"): """Reads polarization angles in radians, discarding ones marked bad (the negative ones). The format is returned as id,val.""" ids, res = [], [] for line in utils.lines(fname): if line.startswith("#"): continue toks = line.split() if mode == "irca" or len(toks) > 2: id, ang = int(toks[0]), float(toks[3])*np.pi/180 else: id, ang = int(toks[0]), float(toks[1])*np.pi/180 if ang < 0: continue ids.append(id) res.append(ang) return np.array(ids), np.array(res)
def read_pylike_format(fname): """Givnen a file with a simple python-like format with lines of foo = [num,num,num,...], return it as a dictionary of names->lists, while preserving nan values.""" res = {} for line in utils.lines(fname): if line.isspace(): continue try: a = ast.parse(line.replace("nan", "'nan'")) # Does not handle nan except TypeError as e: raise IOError("Unparsable file %s (%s)" % (str(fname), str(e))) id = a.body[0].targets[0].id res[id] = ast.literal_eval(a.body[0].value) # reinsert all the nans. This assumes no nested lists for i, v in enumerate(res[id]): if v == "'nan'": res[id][i] = np.nan elif v == "nan": res[id][i] = np.nan return res
def read_pylike_format(fname): """Givnen a file with a simple python-like format with lines of foo = [num,num,num,...], return it as a dictionary of names->lists, while preserving nan values.""" res = {} for line in utils.lines(fname): if line.isspace(): continue try: a = ast.parse(line.replace("nan", "'nan'")) # Does not handle nan except TypeError as e: raise IOError("Unparsable file %s (%s)" % (str(fname), e.message)) id = a.body[0].targets[0].id res[id] = ast.literal_eval(a.body[0].value) # reinsert all the nans. This assumes no nested lists for i, v in enumerate(res[id]): if v == "'nan'": res[id][i] = np.nan elif v == "nan": res[id][i] = np.nan return res
def read_cut(fname, permissive=True): """Read the act cut format, returning ids, cuts, offset, where cuts is a Multirange object.""" nsamp, ndet, offset = None, None, None dets, cuts = [], [] for line in utils.lines(fname): if "=" in line: # Header key-value pair toks = line.split() if toks[0] == "n_det": ndet = int(toks[2]) elif toks[0] == "n_samp": nsamp = int(toks[2]) elif toks[0] == "samp_offset": offset = int(toks[2]) else: continue # Ignore others elif ":" in line: parts = line.split(":") uid = int(parts[0].split()[0]) if len(parts) > 1 and "(" in parts[1]: toks = parts[1].split() ranges = np.array([[int(w) for w in tok[1:-1].split(",")] for tok in toks]) ranges = np.minimum(ranges, nsamp) cuts.append(sampcut.from_list([ranges], nsamp)) # Handle uncut detectors else: cuts.append(sampcut.empty(1, nsamp)) dets.append(uid) # Add any missing detectors if we are in permissive mode if permissive: missing = set(range(ndet)) - set(dets) for uid in missing: dets.append(uid) cuts.append(sampcut.empty(1, nsamp)) # Filter out fully cut tods odets, ocuts = [], [] for det, cut in zip(dets, cuts): if cut.sum() < cut.nsamp: odets.append(det) ocuts.append(cut) if len(ocuts) == 0: ocuts = sampcut.full(0, nsamp) else: ocuts = sampcut.stack(ocuts) return odets, ocuts, offset
def read_cut(fname, permissive=True): """Read the act cut format, returning ids, cuts, offset, where cuts is a Multirange object.""" nsamp, ndet, offset = None, None, None dets, cuts = [], [] for line in utils.lines(fname): if "=" in line: # Header key-value pair toks = line.split() if toks[0] == "n_det": ndet = int(toks[2]) elif toks[0] == "n_samp": nsamp = int(toks[2]) elif toks[0] == "samp_offset": offset = int(toks[2]) else: continue # Ignore others elif ":" in line: parts = line.split(":") uid = int(parts[0].split()[0]) if len(parts) > 1 and "(" in parts[1]: toks = parts[1].split() ranges = np.array([[int(w) for w in tok[1:-1].split(",")] for tok in toks]) ranges = np.minimum(ranges, nsamp) cuts.append(sampcut.from_list([ranges],nsamp)) # Handle uncut detectors else: cuts.append(sampcut.empty(1, nsamp)) dets.append(uid) # Add any missing detectors if we are in permissive mode if permissive: missing = set(range(ndet))-set(dets) for uid in missing: dets.append(uid) cuts.append(sampcut.empty(1,nsamp)) # Filter out fully cut tods odets, ocuts = [], [] for det, cut in zip(dets, cuts): if cut.sum() < cut.nsamp: odets.append(det) ocuts.append(cut) if len(ocuts) == 0: ocuts = sampcut.full(0,nsamp) else: ocuts = sampcut.stack(ocuts) return odets, ocuts, offset
def read_gain_correction_ascii(fname, id=None): """Read lines of the format id[:tag] val or id tag val. Returns it as a dict of {id: {tag:val,...}}. So a single TOD may be covered by multiple entries in the file, each of which covers a different subset. Lines that start with # will be ignored. If the id argument is passed in, only lines with matching id will be returned.""" res = {} for line in utils.lines(fname): if line.startswith("#"): continue if id and not line.startswith(id) and not line.startswith("*"): continue # Parse the line line = line.replace(":"," ") toks = line.split() if len(toks) == 2: tod_id, value = toks tag = "*" else: tod_id, tag, value = toks value = float(value) # And insert it at the right location if tod_id not in res: res[tod_id] = {} res[tod_id][tag] = value return res