def map_state_to_msid(self, state, msid, ftype="msids"): """ Create a new derived field by interpolating a state to the times of a MSID or model component. Parameters ---------- state : string The state to be interpolated. msid : string The msid or model component to interpolate the state to. ftype : string, optional The field type to use. "msids" or "model". Default: "msids" Examples -------- >>> ds.map_state_to_msid("ccd_count", "1dpamzt") """ state = state.lower() msid = msid.lower() ftype = ftype.lower() units = get_units("states", state) def _state(ds): msid_times = ds.times(ftype, msid) state_times = ds.times("states", state)[1] indexes = np.searchsorted(state_times, msid_times) v = ds["states", state][indexes].value if v.dtype.char in ['S', 'U']: return APStringArray(v, msid_times) else: return APQuantity(v, msid_times, unit=units) self.add_derived_field(ftype, state, _state, units, display_name=self.fields["states", state].display_name, depends=[(ftype, msid)])
def from_xija(cls, model, components, interp_times=None, masks=None): if masks is None: masks = {} if interp_times is None: t = model.times else: t = interp_times table = {} for k in components: if k == "roll": key = "off_nominal_roll" elif k == "sim_z": key = "simpos" else: key = k if k == "dpa_power": mvals = model.comp[k].mvals*100. / model.comp[k].mult mvals += model.comp[k].bias elif k == "fptemp_11": mvals = model.comp["fptemp"].mvals elif k == "earthheat__fptemp": key = "earth_solid_angle" mvals = model.comp["earthheat__fptemp"].dvals else: mvals = model.comp[k].mvals unit = get_units("model", key) mask = masks.get(key, None) if interp_times is None: v = mvals else: v = Ska.Numpy.interpolate(mvals, model.times, interp_times) times = Quantity(t, "s") table[key] = APQuantity(v, times, unit, dtype=v.dtype, mask=mask) return cls(table=table)
def add_averaged_field(self, field, n=10): """ Add a new field from an average of another. Parameters ---------- field : string or (type, name) tuple The field to be averaged. n : integer, optional The number of samples to average over. Default: 5 Examples -------- >>> ds.add_averaged_field(("msids", "1dpicacu"), n=10) """ ftype, fname = self._determine_field(field) def _avg(ds): v = ds[ftype, fname] return APQuantity(moving_average(v.value, n=n), v.times, unit=v.unit, mask=v.mask) display_name = "Average %s" % self.fields[ftype, fname].display_name units = get_units(ftype, fname) self.add_derived_field(ftype, "avg_%s" % fname, _avg, units, display_name=display_name, depends=[(ftype, fname)])
def from_load_file(cls, temps_file, esa_file=None): data = {} table = ascii.read(temps_file) comp = list(table.keys())[-1] key = "fptemp_11" if comp == "fptemp" else comp times = Quantity(table["time"], 's') data[key] = APQuantity(table[comp].data, times, get_units("model", key), dtype=table[comp].data.dtype) if esa_file is not None: etable = ascii.read(esa_file) key = "earth_solid_angle" times = Quantity(etable["time"], 's') data[key] = APQuantity(etable[key].data, times, get_units("model", key), dtype=etable[key].data.dtype) return cls(table=data)
def get_values(self, time): time = get_time(time, fmt='secs') t = Quantity(time, "s") values = {} for key in self.keys(): v = Ska.Numpy.interpolate(self[key].value, self[key].times.value, [time], method='linear')[0] unit = get_units("model", key) values[key] = APQuantity(v, t, unit=unit, dtype=v.dtype) return values
def __init__(self, table, times, state_codes=None, masks=None, derived_msids=None): super(MSIDs, self).__init__() if state_codes is None: state_codes = {} if masks is None: masks = {} for k, v in table.items(): mask = masks.get(k, None) t = Quantity(times[k], "s") if v.dtype.char in ['S', 'U']: self.table[k] = APStringArray(v, t, mask=mask) else: unit = get_units("msids", k) self.table[k] = APQuantity(v, t, unit=unit, dtype=v.dtype, mask=mask) self.state_codes = state_codes if derived_msids is None: derived_msids = [] self.derived_msids = derived_msids
def __init__(self, table): import numpy.lib.recfunctions as rf new_table = OrderedDict() if isinstance(table, np.ndarray): state_names = list(table.dtype.names) if "tstart" not in state_names: table = rf.append_fields(table, ["tstart", "tstop"], [ date2secs(table["datestart"]), date2secs(table["datestop"]) ], usemask=False) state_names += ["tstart", "tstop"] else: state_names = list(table.keys()) if "tstart" not in state_names: table["tstart"] = date2secs(table["datestart"]) table["tstop"] = date2secs(table["datestop"]) state_names += ["tstart", "tstop"] times = Quantity([table["tstart"], table["tstop"]], "s") for k in state_names: v = np.asarray(table[k]) if k == "trans_keys" and v.dtype.char == "O": new_table[k] = APStringArray( np.array([",".join(d) for d in v]), times) elif v.dtype.char in ['S', 'U', 'O']: new_table[k] = APStringArray(v, times) else: new_table[k] = APQuantity(v, times, get_units("states", k), dtype=v.dtype) if "off_nom_roll" not in state_names: v = calc_off_nom_rolls(new_table) new_table["off_nom_roll"] = APQuantity(v, times, "deg", dtype=v.dtype) super(States, self).__init__(table=new_table)
def add_diff_data_model_field(self, msid, ftype_model="model"): r""" Create a field which gives the difference between the data and the model for a particular MSID. Parameters ---------- msid : string The MSID to take the diff of data and model of. ftype_model : string, optional The model type (e.g., "model", "model0", etc.) of the model field to be diffed with the MSID. """ msid = msid.lower() ftype_model = ftype_model.lower() units = get_units("msids", msid) def _diff(ds): return ds["msids", msid]-ds[ftype_model, msid] display_name = self.fields["msids", msid].display_name.replace('_', '\_') self.add_derived_field(ftype_model, "diff_%s" % msid, _diff, units, display_name="$\mathrm{\Delta(%s)}$" % display_name, depends=[("msids", msid), (ftype_model, msid)])
def from_load_page(cls, load, components, time_range=None): components = [comp.lower() for comp in components] load = find_load(load) mylog.info("Reading model data from the %s load." % load) components = ensure_list(components) if "fptemp_11" in components: components.append("earth_solid_angle") data = {} for comp in components: if comp == "earth_solid_angle": url = "http://cxc.cfa.harvard.edu/acis/FP_thermPredic/" url += "%s/ofls%s/earth_solid_angles.dat" % (load[:-1].upper(), load[-1].lower()) table_key = comp else: c = comp_map[comp].upper() table_key = "fptemp" if comp == "fptemp_11" else comp url = "http://cxc.cfa.harvard.edu/acis/%s_thermPredic/" % c url += "%s/ofls%s/temperatures.dat" % (load[:-1].upper(), load[-1].lower()) u = requests.get(url) if not u.ok: if table_key == "earth_solid_angle": mylog.warning("Could not find the earth solid angles file. Skipping.") else: mylog.warning("Could not find the model page for '%s'. Skipping." % comp) continue table = ascii.read(u.text) if time_range is None: idxs = np.ones(table["time"].size, dtype='bool') else: idxs = np.logical_and(table["time"] >= time_range[0], table["time"] <= time_range[1]) times = Quantity(table["time"][idxs], 's') data[comp] = APQuantity(table[table_key].data[idxs], times, get_units("model", comp), dtype=table[table_key].data.dtype) return cls(table=data)