def twodof_multi(conn): while True: par = conn.recv() pin, dea = twodof.calc_twodof_model(states=core_states[i], T_pin0=tlm['1pin1at'][t_idxs[i]], T_dea0=tlm['1pdeaat'][t_idxs[i]], times=core_times[i], dt=300.0, par=par) conn.send((pin, dea))
def psmc_temp(pars, times): par = dict(zip(PARNAMES, pars)) if pars != cache['pars']: cache['1pin1at'], cache['1pdeaat'] = \ twodof.calc_twodof_model(states, tlm[0]['1pin1at'], tlm[0]['1pdeaat'], times, dt=300.0, par=par) cache['pars'] = pars print pars return cache[msid]
def main(): mstate_file = 'states.txt' mtemp_file = '1pdeaat.txt' mstates = Ska.Table.read_ascii_table(mstate_file) marray = [] for state in mstates: state_list = list( state ) state_list.append(DateTime(state['tstart']).secs) state_list.append(DateTime(state['tstop']).secs) marray.append(state_list) matlab_states = np.core.records.fromrecords( marray, names ='datestart,datestop,obsid,simpos,ccd_count,fep_count,vid_board,clocking,power,pitch,tstart,tstop') matlab_pred = Ska.Table.read_ascii_table( mtemp_file ) dates = matlab_pred['Time'] state0 = matlab_states[0] msid_string = { '1pdeaat' : '1PDEAAT Prediction (degC)', '1pin1at' : '1PIN1AT Prediction' } dea0 = matlab_pred[0][msid_string['1pdeaat']] pin0 = matlab_pred[0][msid_string['1pin1at']] times = DateTime( dates ).secs ( T_pin, T_dea ) = twodof.calc_twodof_model( matlab_states, pin0, dea0, times, characteristics.model_par) preds = np.core.records.fromarrays([ times, T_pin, T_dea], names='time,1pin1at,1pdeaat') pyt_file = open('python_temps.csv', 'w') pyt_file.write("Time\t%s\t%s\n" % ( msid_string['1pdeaat'], msid_string['1pin1at'] )) for pred in preds: pyt_file.write("%s\t%s\t%s\n" % ( DateTime(pred['time']).date, pred['1pdeaat'], pred['1pin1at'])) for msid in ('1pdeaat', '1pin1at'): figure(1,figsize=(5,3.75)) plot_cxctime( times, matlab_pred[msid_string[msid]], fmt='r.') plot_cxctime( times, preds[msid], fmt='b.') xlabel('CXC Time') ylabel('%s (Deg C)' % msid ) savefig('%s.png' % msid ) clf() hist( matlab_pred[msid_string[msid]] - preds[msid], bins=20) xlabel('Diff (Deg C)') title("matlab - python") savefig('%s_hist.png' % msid ) clf()
date1 = Chandra.Time.DateTime('2008-12-01T00:00:00') states = states08[ (states08['tstart'] > date0.secs) & (states08['tstop'] < date1.secs) ] tstart = states[0].tstart tstop = states[-1].tstop tlm = tlm08[ (tlm08['date'] > tstart) & (tlm08['date'] < tstop) ] dea0 = tlm[0]['1pdeaat'] pin0 = tlm[0]['1pin1at'] twomass = twodof.TwoDOF(states, pin0, dea0) times = tlm['date'] clock0 = time.clock() # T_dea = twomass.calc_model(times) T_pin, T_dea = twodof.calc_twodof_model(states, pin0, dea0, times) print 'Execution time:', time.clock() - clock0 hot = tlm['1pdeaat'] > 40 figure(1) clf() plot((times-tstart)/1000., tlm['1pdeaat']) plot((times-tstart)/1000., T_dea) figure(2, figsize=(6,4.5)) clf() resid = tlm['1pdeaat'] - T_dea hist(resid, bins=50, label='All') title('1pdeaat: Actual - Model') xlabel('Model error (degC)')
import numpy as np import twodof import characteristics as char cols = 'tstart tstop power pitch simpos'.split() states = [] states.append(( 0., 10000., 40., 150., -99162)) states.append((10000., 20000., 80., 90., -50360)) states.append((20000., 30000., 100., 130., 75766)) states.append((30000., 40000., 120., 55., 93718)) states = np.rec.fromrecords(states, names=cols) dea0 = 25. pin0 = 35. times = np.arange(states['tstart'][0], states['tstop'][-1], 32.8) T_pin, T_dea = twodof.calc_twodof_model(states, pin0, dea0, times, par=char.model_par) clf() plot(times, T_pin, label='1pin1at') plot(times, T_dea, label='1pdeaat') legend()
power = dict((x[0:3], x[3]) for x in characteristics.psmc_power) cols = 'tstart tstop power pitch simpos'.split() pitchs = range(45, 170, 1) settle_temps = [] for pitch in pitchs: states = [] states.append((0., 500000., power[0, 1, 0], pitch, simz_hrcs)) states = np.rec.fromrecords(states, names=cols) dea0 = 25. pin0 = 35. times = np.arange(states['tstart'][0], states['tstop'][-1], 10000.) T_pin, T_dea = twodof.calc_twodof_model(states, pin0, dea0, times, par=characteristics.model_par, dt=10000.) settle_temps.append(T_dea[-1]) figure(1, figsize=(5, 3.75)) clf() plot(pitchs, settle_temps) ylim(0, 15) ylabel('1PDEAAT (degC)') xlabel('Pitch (degrees)') title('Final temp at HRC-S (FEP=0, clock=0, vid=1)') subplots_adjust(bottom=0.14) savefig('scs107_settling.png')
def make_validation_plots(opt, tlm, db): """ Make validation output plots. :param outdir: output directory :param tlm: telemetry :param db: database handle :returns: list of plot info including plot file names """ outdir = opt.outdir states = get_states(tlm[0].date, tlm[-1].date, db) tlm = Ska.Numpy.add_column(tlm, "power", smoothed_power(tlm)) T_dea0 = np.mean(tlm["1pdeaat"][:10]) T_pin0 = np.mean(tlm["1pin1at"][:10]) # Create array of times at which to calculate PSMC temperatures, then do it. logger.info("Calculating PSMC thermal model for validation") T_pin, T_dea = twodof.calc_twodof_model(states, T_pin0, T_dea0, tlm.date, characteristics.model_par) # Interpolate states onto the tlm.date grid state_vals = cmd_states.interpolate_states(states, tlm.date) pred = { "1pdeaat": T_dea, "1pin1at": T_pin, "aosares1": state_vals.pitch, "tscpos": state_vals.simpos, "power": state_vals.power, } labels = { "1pdeaat": "Degrees (C)", "1pin1at": "Degrees (C)", "aosares1": "Pitch (degrees)", "tscpos": "SIM-Z (steps/1000)", "power": "ACIS power (watts)", } scales = {"tscpos": 1000.0} fmts = {"1pdeaat": "%.2f", "1pin1at": "%.2f", "aosares1": "%.3f", "power": "%.2f", "tscpos": "%d"} plots = [] logger.info("Making PSMC model validation plots and quantile table") quantiles = (1, 5, 16, 50, 84, 95, 99) # store lines of quantile table in a string and write out later quant_table = "" quant_head = ",".join(["MSID"] + ["quant%d" % x for x in quantiles]) quant_table += quant_head + "\n" for fig_id, msid in enumerate(sorted(pred)): plot = dict(msid=msid.upper()) fig = plt.figure(10 + fig_id, figsize=(7, 3.5)) fig.clf() scale = scales.get(msid, 1.0) ticklocs, fig, ax = plot_cxctime(tlm.date, tlm[msid] / scale, fig=fig, fmt="-r") ticklocs, fig, ax = plot_cxctime(tlm.date, pred[msid] / scale, fig=fig, fmt="-b") ax.set_title(msid.upper() + " validation") ax.set_ylabel(labels[msid]) filename = msid + "_valid.png" outfile = os.path.join(outdir, filename) logger.info("Writing plot file %s" % outfile) fig.savefig(outfile) plot["lines"] = filename # Make quantiles diff = np.sort(tlm[msid] - pred[msid]) quant_line = "%s" % msid for quant in quantiles: quant_val = diff[(len(diff) * quant) // 100] plot["quant%02d" % quant] = fmts[msid] % quant_val quant_line += "," + fmts[msid] % quant_val quant_table += quant_line + "\n" for histscale in ("log", "lin"): fig = plt.figure(20 + fig_id, figsize=(4, 3)) fig.clf() ax = fig.gca() ax.hist(diff / scale, bins=50, log=(histscale == "log")) ax.set_title(msid.upper() + " residuals: data - model") ax.set_xlabel(labels[msid]) fig.subplots_adjust(bottom=0.18) filename = "%s_valid_hist_%s.png" % (msid, histscale) outfile = os.path.join(outdir, filename) logger.info("Writing plot file %s" % outfile) fig.savefig(outfile) plot["hist" + histscale] = filename plots.append(plot) filename = os.path.join(outdir, "validation_quant.csv") logger.info("Writing quantile table %s" % filename) f = open(filename, "w") f.write(quant_table) f.close() # If run_start_time is specified this is likely for regression testing # or other debugging. In this case write out the full predicted and # telemetered dataset as a pickle. if opt.run_start_time: filename = os.path.join(outdir, "validation_data.pkl") logger.info("Writing validation data %s" % filename) f = open(filename, "w") pickle.dump({"pred": pred, "tlm": tlm}, f, protocol=-1) f.close() return plots
def make_week_predict(opt, tstart, tstop, bs_cmds, tlm, db): # Try to make initial state0 from cmd line options state0 = dict((x, getattr(opt, x)) for x in ("pitch", "simpos", "power", "T_dea", "T_pin")) state0.update( { "tstart": tstart - 30, "tstop": tstart, "datestart": DateTime(tstart - 30).date, "datestop": DateTime(tstart).date, } ) # If cmd lines options were not fully specified then get state0 as last # cmd_state that starts within available telemetry. Update with the # mean temperatures at the start of state0. if None in state0.values(): state0 = cmd_states.get_state0(tlm[-5].date, db, datepar="datestart") ok = (tlm.date >= state0["tstart"] - 150) & (tlm.date <= state0["tstart"] + 150) state0.update({"T_dea": np.mean(tlm["1pdeaat"][ok]), "T_pin": np.mean(tlm["1pin1at"][ok])}) logger.debug("state0 at %s is\n%s" % (DateTime(state0["tstart"]).date, pformat(state0))) if opt.old_cmds: cmds_datestart = DateTime(state0["tstop"]).date cmds_datestop = DateTime(bs_cmds[0]["time"]).date db_cmds = cmd_states.get_cmds(cmds_datestart, cmds_datestop, db) else: # Get the commands after end of state0 through first backstop command time cmds_datestart = state0["datestop"] cmds_datestop = bs_cmds[0]["date"] # *was* DateTime(bs_cmds[0]['time']).date # Get timeline load segments including state0 and beyond. timeline_loads = db.fetchall( """SELECT * from timeline_loads WHERE datestop > '%s' and datestart < '%s'""" % (cmds_datestart, cmds_datestop) ) logger.info("Found %s timeline_loads after %s" % (len(timeline_loads), cmds_datestart)) # Get cmds since datestart within timeline_loads db_cmds = cmd_states.get_cmds(cmds_datestart, db=db, update_db=False, timeline_loads=timeline_loads) # Delete non-load cmds that are within the backstop time span # => Keep if timeline_id is not None or date < bs_cmds[0]['time'] db_cmds = [x for x in db_cmds if (x["timeline_id"] is not None or x["time"] < bs_cmds[0]["time"])] logger.info("Got %d cmds from database between %s and %s" % (len(db_cmds), cmds_datestart, cmds_datestop)) # Get the commanded states from state0 through the end of the backstop commands states = cmd_states.get_states(state0, db_cmds + bs_cmds) states[-1].datestop = bs_cmds[-1]["date"] states[-1].tstop = bs_cmds[-1]["time"] logger.info( "Found %d commanded states from %s to %s" % (len(states), states[0]["datestart"], states[-1]["datestop"]) ) # Add power column based on ACIS commanding in states states = Ska.Numpy.add_column(states, "power", get_power(states)) # Create array of times at which to calculate PSMC temperatures, then do it. times = np.arange(state0["tstart"], tstop, opt.dt) logger.info("Calculating PSMC thermal model") T_pin, T_dea = twodof.calc_twodof_model(states, state0["T_pin"], state0["T_dea"], times, characteristics.model_par) # Make the PSMC limit check plots and data files plt.rc("axes", labelsize=10, titlesize=12) plt.rc("xtick", labelsize=10) plt.rc("ytick", labelsize=10) temps = dict(dea=T_dea, pin=T_pin) plots = make_check_plots(opt, states, times, temps, tstart) viols = make_viols(opt, states, times, temps) write_states(opt, states) write_temps(opt, times, temps) return dict(opt=opt, states=states, times=times, temps=temps, plots=plots, viols=viols)