def plotdata(path='\\ARCHIVESB::DMD10195:CH1', treename='sandbox', shot=5, server=None): from MDSplus import Connection from pylab import plot, title, xlabel, ylabel if server is not None: Con = Connection(server) Con.openTree(treename, shot) name = Con.get('{}:$NAME'.format(path)) data = Con.get('DATA({})'.format(path)) time = Con.get('DIM_OF({})'.format(path)) unit = Con.get('UNITS_OF({})'.format(path)) else: with Tree(treename, shot) as tree: Node = tree.getNode(path) Data = Node.getData() name = Node.getNode('$NAME').data() data = Data.data() time = Data.dim_of().data() unit = Data.getUnits() plot(time, data) xlabel('time (s)') ylabel(unit) title(name)
def _start_mdsip(self, server, port, logname, protocol=MDSIP_PROTOCOL): if port > 0: from subprocess import Popen, STDOUT logfile = '%s-%s%d.log' % (self.module, logname, self.index) log = open(logfile, 'w') if iswin else None try: hosts = '%s/mdsip.hosts' % self.root params = [ 'mdsip', '-s', '-p', str(port), '-P', protocol, '-h', hosts ] print(' '.join(params + ['&>', logfile])) if not log: params.extend(['2>&1', '|', 'tee', logfile]) mdsip = Popen(params) except: if log: log.close() raise try: self._waitIdle(server, 10) # allow mdsip to launch except Exception: mdsip.kill() raise else: mdsip, log = None, None if server: c = Connection(server) for envpair in self.envx.items(): checkStatus(c.get('setenv($//"="//$)', *envpair)) c.get('tcl($)', 'set verify') return mdsip, log
def get_data(signal, shot, tree): cn = CN('mds.ipp.ac.cn') cn.openTree(tree, shot) x = cn.get('\\' + signal).copy() t = cn.get('dim_of(' + '\\' + signal + ')').copy() cn.closeAllTrees() t = np.array(t, dtype=np.float64) x = np.array(x, dtype=np.float64) return t, x
def read_EAST_TS(shot): from MDSplus import Connection as CN conn = CN('202.127.204.12') conn.openTree('analysis', shot) R = conn.get('\\' + 'R_coreTS ').copy() Z = conn.get('\\' + 'Z_coreTS ').copy() time = conn.get('dim_of(' + '\\' + 'Te_maxTS' + ')').copy() Te = conn.get('\\' + 'Te_coreTS ').copy() Ne = conn.get('\\' + 'ne_coreTS').copy() conn.closeAllTrees() return R, Z, time, Te, Ne
def data(signal, shot, tree): import sys sys.path.append("D:\\Program Files\\MDSplus\\python") from MDSplus import Connection as CN cn = CN('mds.ipp.ac.cn') cn.openTree(tree, shot) x = cn.get('\\' + signal).copy() t = cn.get('dim_of(' + '\\' + signal + ')').copy() cn.closeAllTrees() t = np.array(t, dtype=np.float64) x = np.array(x, dtype=np.float64) return t, x
def io(self): connection = Connection("thread://io") """ mdsconnect """ self.assertEqual(connection.get('_a=1').tolist(), 1) self.assertEqual(connection.get('_a').tolist(), 1) self.assertEqual(connection.getObject('1:3:1').__class__, Range) g = GetMany(connection) g.append('a', '1') g.append('b', '$', 2) g.append('c', '$+$', 1, 2) g.execute() self.assertEqual(g.get('a'), 1) self.assertEqual(g.get('b'), 2) self.assertEqual(g.get('c'), 3)
def fast_read(sig, shot): from MDSplus import Connection as CN conn = CN('mds.ipp.ac.cn') conn.openTree('east', shot) x = conn.get('\\' + sig).copy() t = conn.get('dim_of(' + '\\' + sig + ')').copy() conn.closeAllTrees() ind = np.argmin(np.abs(t - 0)) t = t[ind:] x = x[ind:] t = np.array(t, dtype=np.float64) x = np.array(x, dtype=np.float64) return t, x
def threadsTcp(self): server, server_port = self._setup_mdsip('ACTION_SERVER', 'ACTION_PORT', 7100 + self.index, True) svr = svr_log = None try: svr, svr_log = self._start_mdsip(server, server_port, 'tcp') try: if svr is not None: time.sleep(1) def requests(c, idx): args = [Float32(i / 10 + idx) for i in range(10)] for i in range(10): self.assertEqual( c.get("[$,$,$,$,$,$,$,$,$,$]", *args).tolist(), args) c = Connection(server) """ mdsconnect """ self.assertEqual(c.get('_a=1').tolist(), 1) self.assertEqual(c.get('_a').tolist(), 1) self.assertEqual(c.getObject('1:3:1').__class__, Range) g = GetMany(c) g.append('a', '1') g.append('b', '$', 2) g.append('c', '$+$', 1, 2) g.execute() self.assertEqual(g.get('a'), 1) self.assertEqual(g.get('b'), 2) self.assertEqual(g.get('c'), 3) threads = [ Thread(name="C%d" % i, target=requests, args=(c, i)) for i in range(10) ] for thread in threads: thread.start() for thread in threads: thread.join() finally: if svr and svr.poll() is None: svr.terminate() svr.wait() finally: if svr_log: svr_log.close()
def loadpaths(host, treename): """Get the path environment variables for a tree being served by a remote system and define them in this process context. The remote system's getpaths function is responsible for fixing up the tree_path values, prefixing hostnames and replacing any shortened hostnames with fully qualified ip address names.""" from MDSplus import Connection import os c = Connection(host) paths = c.get("getpaths('%s')" % treename).deserialize() for i in range(len(paths['path'])): os.environ[paths['tree'][i].rstrip().lower() + '_path'] = paths['path'][i].rstrip()
def get(self, expr, *args, **kwargs): print 'expression'+expr return Connection.get(self, expr, *args, **kwargs)
def load_solps_from_mdsplus(mds_server, ref_number): """ Load a SOLPS simulation from a MDSplus server. :param str mds_server: Server address. :param int ref_number: Simulation reference number. :rtype: SOLPSSimulation """ from MDSplus import Connection as MDSConnection # Setup connection to server conn = MDSConnection(mds_server) conn.openTree('solps', ref_number) # Load SOLPS mesh geometry and lookup arrays mesh = load_mesh_from_mdsplus(conn) sim = SOLPSSimulation(mesh) ni = mesh.nx nj = mesh.ny ########################## # Magnetic field vectors # raw_b_field = np.swapaxes(conn.get('\SOLPS::TOP.SNAPSHOT.B').data(), 0, 2) b_field_vectors_cartesian = np.zeros((ni, nj, 3)) b_field_vectors = np.zeros((ni, nj, 3)) for i in range(ni): for j in range(nj): bparallel = raw_b_field[i, j, 0] bradial = raw_b_field[i, j, 1] btoroidal = raw_b_field[i, j, 2] b_field_vectors[i, j] = (bparallel, bradial, btoroidal) pv = mesh.poloidal_grid_basis[i, j, 0] # parallel basis vector rv = mesh.poloidal_grid_basis[i, j, 1] # radial basis vector bx = pv.x * bparallel + rv.x * bradial # component of B along poloidal x by = pv.y * bparallel + rv.y * bradial # component of B along poloidal y b_field_vectors_cartesian[i, j] = (bx, btoroidal, by) sim._b_field_vectors = b_field_vectors sim._b_field_vectors_cartesian = b_field_vectors_cartesian # Load electron species sim._electron_temperature = np.swapaxes( conn.get('\SOLPS::TOP.SNAPSHOT.TE').data(), 0, 1) # (32, 98) => (98, 32) sim._electron_density = np.swapaxes( conn.get('\SOLPS::TOP.SNAPSHOT.NE').data(), 0, 1) # (32, 98) => (98, 32) ############################ # Load each plasma species # ############################ # Master list of species, e.g. ['D0', 'D+1', 'C0', 'C+1', ... species_list = conn.get('\SOLPS::TOP.IDENT.SPECIES').data() try: species_list = species_list.decode('UTF-8') except AttributeError: # Already a string pass sim._species_list = species_list.split() sim._species_density = np.swapaxes( conn.get('\SOLPS::TOP.SNAPSHOT.NA').data(), 0, 2) sim._rad_par_flux = np.swapaxes( conn.get('\SOLPS::TOP.SNAPSHOT.FNAY').data(), 0, 2) # radial particle flux sim._radial_area = np.swapaxes( conn.get('\SOLPS::TOP.SNAPSHOT.SY').data(), 0, 1) # radial contact area # Load the neutral atom density from B2 dab2 = conn.get('\SOLPS::TOP.SNAPSHOT.DAB2') if isinstance(dab2, np.ndarray): sim._b2_neutral_densities = np.swapaxes(dab2.data(), 0, 2) sim._velocities_parallel = np.swapaxes( conn.get('\SOLPS::TOP.SNAPSHOT.UA').data(), 0, 2) sim._velocities_radial = np.zeros((ni, nj, len(sim.species_list))) sim._velocities_toroidal = np.zeros((ni, nj, len(sim.species_list))) sim._velocities_cartesian = np.zeros((ni, nj, len(sim.species_list), 3), dtype=np.float64) ################################################ # Calculate the species' velocity distribution # b2_neutral_i = 0 # counter for B2 neutrals for k, sp in enumerate(sim.species_list): # Identify the species based on its symbol symbol, charge = re.match(_SPECIES_REGEX, sp).groups() charge = int(charge) # If neutral and B" atomic density available, use B2 density, otherwise use fluid species density. if charge == 0 and (sim._b2_neutral_densities != None): species_dens_data = sim.b2_neutral_densities[:, :, b2_neutral_i] b2_neutral_i += 1 else: species_dens_data = sim.species_density[:, :, k] for i in range(ni): for j in range(nj): # Load grid basis vectors pv = mesh.poloidal_grid_basis[i, j, 0] # parallel basis vector rv = mesh.poloidal_grid_basis[i, j, 1] # radial basis vector # calculate field component ratios for velocity conversion bparallel = b_field_vectors[i, j, 0] btoroidal = b_field_vectors[i, j, 2] bplane = sqrt(bparallel**2 + btoroidal**2) parallel_to_toroidal_ratio = bparallel * btoroidal / (bplane** 2) # Calculate toroidal and radial velocity components v_parallel = sim.velocities_parallel[ i, j, k] # straight from SOLPS 'UA' variable v_toroidal = v_parallel * parallel_to_toroidal_ratio sim.velocities_toroidal[i, j, k] = v_toroidal # Special case for edge of mesh, no radial velocity expected. try: if species_dens_data[i, j] == 0: v_radial = 0.0 else: v_radial = sim.radial_particle_flux[ i, j, k] / sim.radial_area[i, j] / species_dens_data[i, j] except IndexError: v_radial = 0.0 sim.velocities_radial[i, j, k] = v_radial # Convert velocities to cartesian coordinates vx = pv.x * v_parallel + rv.x * v_radial # component of v along poloidal x vy = pv.y * v_parallel + rv.y * v_radial # component of v along poloidal y sim.velocities_cartesian[i, j, k, :] = (vx, v_toroidal, vy) # Make Mesh Interpolator function for inside/outside mesh test. inside_outside_data = np.ones(mesh.num_tris) inside_outside = AxisymmetricMapper( Discrete2DMesh(mesh.vertex_coords, mesh.triangles, inside_outside_data, limit=False)) sim._inside_mesh = inside_outside ############################### # Load extra data from server # ############################### #################### # Integrated power # vol = np.swapaxes(conn.get('\SOLPS::TOP.SNAPSHOT.VOL').data(), 0, 1) # TODO - this should be a mesh property linerad = np.swapaxes(conn.get('\SOLPS::TOP.SNAPSHOT.RQRAD').data(), 0, 2) linerad = np.sum(linerad, axis=2) brmrad = np.swapaxes(conn.get('\SOLPS::TOP.SNAPSHOT.RQBRM').data(), 0, 2) brmrad = np.sum(brmrad, axis=2) neurad = conn.get('\SOLPS::TOP.SNAPSHOT.ENEUTRAD').data() if neurad is not None: # need to cope with fact that neurad may not be present!!! if len(neurad.shape) == 3: neurad = np.swapaxes(np.abs(np.sum(neurad, axis=2)), 0, 1) else: neurad = np.swapaxes(np.abs(neurad), 0, 1) else: neurad = np.zeros(brmrad.shape) total_rad_data = np.zeros(vol.shape) ni, nj = vol.shape for i in range(ni): for j in range(nj): total_rad_data[i, j] = (linerad[i, j] + brmrad[i, j] + neurad[i, j]) / vol[i, j] sim._total_rad = total_rad_data return sim
class JobRunner(object): def __init__(self): self.use_mdsconnect = False self.connection_str = '' self.connection = None self.g = {} self._shot = -1 def run(self, jobset, skip_init): import MDSplus from MDSplus import Connection self.error = [] jobs, names = jobset self.r = {} self.r['error message'] = [''] try: for k, job in enumerate(jobs): for j in job: if j.command == 'connection_mode': if self.connection_str != j.params[0]: self.connection_str = j.params[0] server, port, tree = parse_server_string( self.connection_str) self.use_mdsconnect = (False if server.upper() == 'DIRECT' else True) if self.use_mdsconnect: if self.connection is not None: del self.connection try: if port != '': server = server + ':' + port self.connection = Connection(server) res = 'ok' except: res = ('connection error' + traceback.format_exc()) else: if self.connection is not None: del self.connection self.connection = None res = 'ok' else: res = 'ok' else: if j.command == 'init': if skip_init: continue else: j.command = 'value' if self.use_mdsconnect: res = self._run_job_mdsconnect(j) if len(self.error) != 0: self.r['error message'] = self.error self.r[names[k]] = res return self.r else: res = self._run_job(j) if len(self.error) != 0: self.r['error message'] = self.error self.r[names[k]] = res return self.r self.r[names[k]] = res except: self.error = ['run error', traceback.format_exc()] self.r['error message'] = self.error return self.r def _run_script_txt(self, job): expr = job.params[0] try: code = compile(expr, '<string>', 'exec') g = {} l = {} exec(code, self.g, self.r) return 'ok' except: self.error = ['Scrip Error', expr, sys.exc_info()[0]] return None def _run_job_mdsconnect(self, job): com = job.command if com == 'open': tree = job.params[0] shot = job.params[1] try: self.connection.openTree(tree, int(shot)) return 'ok' except: self.error = ['run error', traceback.format_exc()] return None self._shot = shot elif com == 'defnode': node = job.params[0] try: if node.strip() != '': self.connection.setDefault(node) return 'ok' except: self.error = ['run error', traceback.format_exc()] return None elif com == 'value': try: expr = job.params[0] r = self.connection.get(expr).data() return r except: self.error = ['run error', expr, traceback.format_exc()] return None elif com == 'script_txt': return self._run_script_txt(job) def _run_job(self, job): import MDSplus from MDSplus import Connection com = job.command if com == 'open': tree = job.params[0] shot = job.params[1] try: self.t = MDSplus.Tree(tree, int(shot)) return 'ok' except: self.error = ['run error', traceback.format_exc()] return None self._shot = shot elif com == 'defnode': node = job.params[0] try: if node.strip() != '': tn = self.t.getNode(node) self.t.setDefault(tn) return 'ok' except: self.error = ['run error', traceback.format_exc()] return None elif com == 'value': try: expr = job.params[0] r = MDSplus.Data.compile(expr).evaluate().data() return r except: self.error = ['run error', expr, traceback.format_exc()] return None elif com == 'script_txt': return self._run_script_txt(job)
def thick(self): def testnci(thick, local, con, nci): l = local.S.__getattribute__(nci) t = thick.S.__getattribute__(nci) if nci.endswith("_nids"): l, t = str(l), str(t) try: c = str(con.get("getnci(getnci(S,$),'nid_number')", nci)) except TreeNNF: c = '[]' else: c = con.get("getnci(S,$)", nci) try: self.assertEqual(t, c) self.assertEqual(t, l) except: print(nci, t, l, c) raise server, server_port = self._setup_mdsip('ACTION_SERVER', 'ACTION_PORT', 7000 + self.index, True) svr, svr_log = self._start_mdsip(server, server_port, 'thick') try: con = Connection(server) self.assertEqual( con.get("zero([1,1,1,1,1,1,1,1],1)").tolist(), [[[[[[[[0]]]]]]]]) with Tree(self.tree, -1, "new") as local: local.addNode(self.treesub, "SUBTREE") s = local.addNode("S", "SIGNAL") s.addTag("tagS") s.record = ADD(Float32(1), Float32(2)) t = local.addNode("T", "TEXT") t.addNode("TT", "TEXT").addTag("tagTT") t.record = t.TT t.TT = "recTT" local.write() with Tree(self.treesub, -1, "new") as sub: sub.addNode("OK") sub.write() local.normal() Tree.setCurrent(self.tree, 7) setenv("%s_path" % self.tree, "%s::" % server) print(con.get("getenv($//'_path')", self.tree)) con.get("TreeShr->TreeOpen(ref($),val($),val(1))", self.tree, -1) thick = Tree(self.tree, -1) thick.createPulse(1) thick1 = Tree(self.tree, 1) self.assertEqual( getattr(local, self.treesub.upper()).OK.nid, getattr(thick1, self.treesub.upper()).OK.nid) local_filename = local.getFileName() thick_filename = thick.getFileName() self.assertTrue("::" in thick_filename, thick_filename) self.assertTrue(local_filename, thick_filename.split("::", 1)[1]) """ TreeTurnOff / TreeTurnOn """ thick.S.on = False self.assertEqual(local.S.on, False) thick.S.on = True self.assertEqual(local.S.on, True) """ TreeSetCurrentShotId / TreeGetCurrentShotId """ Tree.setCurrent(self.tree, 1) self.assertEqual(Tree.getCurrent(self.tree), 1) """ TreeGetRecord / TreeSetRecord """ self.assertEqual(str(local.S.record), "1. + 2.") self.assertEqual(str(thick.S.record), "1. + 2.") thick.S.record = ADD(Float32(2), Float32(4)) self.assertEqual(str(local.S.record), "2. + 4.") self.assertEqual(str(thick.S.record), "2. + 4.") self.assertEqual(str(local.T.record), str(thick.T.record)) """ GetDefaultNid / SetDefaultNid """ self.assertEqual(thick.getDefault(), thick.top) thick.setDefault(thick.S) self.assertEqual(thick.getDefault(), thick.top.S) thick.setDefault(thick.top) """ FindNodeWildRemote """ self.assertEqual(str(thick.getNodeWild("T*")), str(local.getNodeWild("T*"))) """ FindTagWildRemote """ self.assertEqual(thick.findTags("*"), local.findTags("*")) """ nci """ thick.S.write_once = True self.assertEqual(thick.S.write_once, True) for nci in ( 'on', 'depth', 'usage_str', 'dtype', 'length', 'rlength', 'fullpath', 'minpath', 'member_nids', 'children_nids', 'rfa', 'write_once', ): testnci(thick, local, con, nci) """ new stuff """ self.assertEqual(local.getFileName(), con.get("treefilename($,-1)", self.tree)) finally: if svr_log: svr_log.close() self._stop_mdsip((svr, server))
def get(self, expr, *args, **kwargs): print 'expression' + expr return Connection.get(self, expr, *args, **kwargs)
def test(): from time import sleep hosts = '%s/mdsip.hosts' % self.root def testDispatchCommand(mdsip, command, stdout=None, stderr=None): self.assertEqual( tcl( 'dispatch/command/nowait/server=%s %s' % (mdsip, command), 1, 1, 1), (None, None)) def setup_mdsip(server_env, port_env, default_port, fix0): host = getenv(server_env, '') if len(host) > 0: return host, 0 port = int(getenv(port_env, default_port)) if port == 0: if fix0: port = default_port else: return None, 0 return 'localhost:%d' % (port, ), port def start_mdsip(server, port, logname, env=None): if port > 0: from subprocess import Popen, STDOUT logfile = '%s_%d.log' % (logname, self.index) log = open(logfile, 'w') try: params = ['mdsip', '-s', '-p', str(port), '-h', hosts] print(' '.join(params + ['&>', logfile])) mdsip = Popen(params, env=env, stdout=log, stderr=STDOUT) except: log.close() raise return mdsip, log if server: for envpair in self.envx.items(): testDispatchCommand(server, 'env %s=%s' % envpair) return None, None monitor, monitor_port = setup_mdsip('ACTION_MONITOR', 'MONITOR_PORT', 8700 + self.index, False) monitor_opt = "/monitor=%s" % monitor if monitor_port > 0 else "" server, server_port = setup_mdsip('ACTION_SERVER', 'ACTION_PORT', 8800 + self.index, True) shot = self.shot + 1 Tree('pytree', -1, 'ReadOnly').createPulse(shot) show_server = "Checking server: %s\n[^,]+, [^,]+, logging enabled, Inactive\n" % server pytree = Tree('pytree', shot) pytree.TESTDEVICE.ACTIONSERVER.no_write_shot = False pytree.TESTDEVICE.ACTIONSERVER.record = server """ using dispatcher """ mon, mon_log, svr, svr_log = (None, None, None, None) try: mon, mon_log = start_mdsip(monitor, monitor_port, 'monitor') svr, svr_log = start_mdsip(server, server_port, 'server', self.env) try: sleep(3) if mon: self.assertEqual(mon.poll(), None) if svr: self.assertEqual(svr.poll(), None) """ mdsconnect """ c = Connection(server) self.assertEqual(c.get('1').tolist(), 1) self.assertEqual(c.getObject('1:3:1').__class__, Range) if not sys.platform.startswith( 'win'): # Windows does not support timeout yet try: # currently the connection needs to be closed after a timeout Connection(server).get("wait(1)", timeout=100) self.fail( "Connection.get(wait(1)) should have timed out." ) except Exc.MDSplusException as e: self.assertEqual(e.__class__, Exc.TdiTIMEOUT) g = GetMany(c) g.append('a', '1') g.append('b', '$', 2) g.append('c', '$+$', 1, 2) g.execute() self.assertEqual(g.get('a'), 1) self.assertEqual(g.get('b'), 2) self.assertEqual(g.get('c'), 3) """ tcl dispatch """ self._doTCLTest('show server %s' % server, out=show_server, re=True) testDispatchCommand(server, 'set verify') testDispatchCommand(server, 'type test') self._doTCLTest('set tree pytree/shot=%d' % shot) self._doTCLTest('dispatch/build%s' % monitor_opt) self._doTCLTest('dispatch/phase%s INIT' % monitor_opt) sleep(1) self._doTCLTest('show server %s' % server, out=show_server, re=True) self._doTCLTest('dispatch/phase%s PULSE' % monitor_opt) sleep(1) self._doTCLTest('show server %s' % server, out=show_server, re=True) self._doTCLTest('dispatch/phase%s STORE' % monitor_opt) sleep(1) self._doTCLTest('show server %s' % server, out=show_server, re=True) """ tcl exceptions """ self._doExceptionTest( 'dispatch/command/server=%s ' % server, Exc.MdsdclIVVERB) """ tcl check if still alive """ if mon: self.assertEqual(mon.poll(), None) if svr: self.assertEqual(svr.poll(), None) finally: try: self._doTCLTest( 'dispatch/command/wait/server=%s close/all' % server) finally: if svr and svr.poll() is None: svr.terminate() svr.wait() if mon and mon.poll() is None: mon.terminate() mon.wait() finally: if svr_log: svr_log.close() if mon_log: mon_log.close() self._doTCLTest('close/all') pytree = Tree('pytree', shot, 'ReadOnly') self.assertTrue(pytree.TESTDEVICE.INIT1_DONE.record <= pytree.TESTDEVICE.INIT2_DONE.record)
class ForkingTCPRequestHandler(socketserver.StreamRequestHandler): # def __init__(self, *args, **kargs): name = '/tmp/mdsplus_gateway_' + str(os.getpid()) + '.log' self.use_mdsconnect = False self.connection_str = '' self.connection = None if write_handler_log: self.fid = open(name, 'w') else: self.fid = None socketserver.StreamRequestHandler.__init__(self, *args, **kargs) def write_log(self, txt): if self.fid is not None: self.fid.write(txt + '\n') def handle(self): try: while True: self.loop_handler() except: pass def loop_handler(self): message = self.rfile.readline().strip() if self.fid is not None: self.fid.write('handling input \n' + message + '\n') asmessage = binascii.a2b_hex(message) # logging.basicConfig(level = logging.DEBUG) try: data = pickle.loads(asmessage) except ValueError as EOFError: if self.fid is not None: self.fid.write('picke error \n') self.fid.flush() # logging.exception('picke load error', len(message)) # response = None # return #cur_thread = threading.current_thread() if self.fid is not None: self.fid.write('handling request \n' + str(data) + '\n') self.fid.flush() r = {} for name, commands in data: for command in commands: com = command[0] param = command[1:] #cur_thread = threading.current_thread() if self.fid is not None: self.fid.write(com + ':' + param + '\n') self.fid.flush() if com == 's': server, port, tree = parse_server_string(param) self.use_mdsconnect = (False if server.upper() == 'DIRECT' else True) if self.use_mdsconnect: try: if self.connection_str != param: del self.connection if port != '': server = server + ':' + port self.connection = Connection(server) self.connection_str = param r[name] = 'ok' except: r[name] = 'connection error' self.error = [ 'connection error', self.connection_str ] else: if self.connection is not None: del self.connection self.connection = None self.connection_str = '' r[name] = 'ok' else: self.error = [] if self.use_mdsconnect: r[name] = self._handle_mdsconnect(com, param) else: r[name] = self._handle(com, param) if len(self.error) != 0: if self.fid is not None: self.fid.write(str(self.error)) self.fid.flush() if self.fid is not None: self.fid.write('return variables \n' + str(list(r.keys())) + '\n') self.fid.flush() sr = pickle.dumps(r) response = binascii.b2a_hex(sr) if self.fid is not None: self.fid.write('sending data (length) \n' + str(len(response)) + '\n') self.fid.flush() self.request.sendall(response + '\n') if self.fid is not None: self.fid.flush() def _handle_mdsconnect(self, com, param): response = '' self.write_log(str(com) + ':' + str(param)) if com == 'c': # connection request arr = param.split(',') tree = ','.join(arr[:-1]) shot = arr[-1] try: if tree != '': self.write_log('opening tree :' + str(tree) + ':' + str(shot)) self.connection.openTree(tree, int(shot)) response = 'ok' except Exception: self.error = ['run error', traceback.format_exc()] response = None elif com == 'd': node = param self.write_log('setting def node ' + str(node)) try: if node.strip() != '': self.connection.setDefault(node) response = 'ok' except Exception: self.error = ['run error', traceback.format_exc()] response = None elif com == 'v': # mdsvalue self.write_log('mdsvalue ' + str(param)) try: response = self.connection.get(param).data() except Exception: self.error = ['run error', param, traceback.format_exc()] response = None elif com == 'u': # mdsvalue self.write_log('mdsvalue ' + str(param)) try: response = self.connection.get('_piscopevar=execute($)', param).data() except Exception: self.error = ['run error', param, traceback.format_exc()] response = None elif com == 'w': # dim_of (this is not used anymore) self.write_log('dim_of ' + str(param)) expr = param[1:] dim = int(param[0]) try: # response =MDSplus.Data.compile(param).evaluate().data() response = self.connection.get(expr).dim_of(dim).data() except Exception: self.error = ['run error', param, traceback.format_exc()] response = None # self.write_log(response.__repr__()) return response def _handle(self, com, param): response = '' self.write_log(str(com) + ' ' + str(param)) if com == 'c': # connection request arr = param.split(',') tree = ','.join(arr[:-1]) shot = arr[-1] try: if tree != '': self.write_log('opening tree ' + str(tree) + ' ' + str(shot)) self.t = MDSplus.Tree(tree, int(shot)) self.write_log('opening tree ' + str(self.t)) response = 'ok' except Exception: self.error = ['run error', traceback.format_exc()] response = None elif com == 'd': node = param self.write_log('setting def node ' + str(node)) try: if node.strip() != '': tn = self.t.getNode(node) self.t.setDefault(tn) response = 'ok' except Exception: self.error = ['run error', traceback.format_exc()] response = None elif com == 'f': # connection request a = param.split(',') tree = a[0] shot = a[1] node = a[2] expr = ','.join(a[3:]) try: self.t = MDSplus.Tree(tree, int(shot)) if node.strip() != '': tn = self.t.getNode(node) self.t.setDefault(tn) response = MDSplus.Data.compile(expr).evaluate().data() except Exception: self.error = ['run error', expr, traceback.format_exc()] response = None elif com == 'v': # mdsvalue self.write_log('mdsvalue ' + str(param)) try: response = MDSplus.Data.compile(param).evaluate().data() except Exception: self.error = ['run error', param, traceback.format_exc()] response = None elif com == 'u': # mdsvalue self.write_log('mdsvalue ' + str(param)) try: response = MDSplus.Data.execute('_piscopevar=execute($)', param).data() except Exception: self.error = ['run error', param, traceback.format_exc()] response = None elif com == 'w': # dim_of (this is not used anymore) self.write_log('dim_of ' + str(param)) expr = param[1:] dim = int(param[0]) try: # response =MDSplus.Data.compile(param).evaluate().data() response = MDSplus.Data.execute(expr).dim_of(dim).data() except Exception: self.error = ['run error', param, traceback.format_exc()] response = None return response
class JobRunner(object): def __init__(self): self.use_mdsconnect = False self.connection_str = '' self.connection = None self.g = {} self._shot = -1 self.gjob = None # global tdi (executed when shot number changed) self._tree = '' self._defnode = '' def __del__(self): if self.connection is not None: del self.connection def run(self, jobset, skip_init): # def _evaluate_sig(res, sig_names, dim_names): # for name in dim_names: # try: # src, dim = res[name][0:3].split(',') # res[name] = res[src].dim_of(long(dim)).data() # except: # pass # for name in sig_names: # try: # res[name] = res[name].data() # except: # pass # return res self.error = [] jobs, names = jobset self.r = {'error message': []} sig_names = [] dim_names = [] try: for k, job in enumerate(jobs): for j in job: # print j if j.command == 'connection_mode': # print 'connection_mode param', self.connection_str, j.params, self.connection # if self.connection is not None: # del self.connection # self.connection = None if self.connection_str != j.params[0]: # if True: server, port, tree = parse_server_string( j.params[0]) self.use_mdsconnect = (False if server.upper() == 'DIRECT' else True) if self.use_mdsconnect: if self.connection is not None: del self.connection self.connection = None try: if port != '': server = server + ':' + port self.connection = Connection(server) self.connection_str = j.params[0] res = 'ok' except: self.r['error message'] = [ 'connection error' + traceback.format_exc() ] self.r[names[k]] = None return self.r else: if self.connection is not None: del self.connection self.connection = None res = 'ok' else: res = 'ok' self._connection_flag = True else: if j.command == 'init': if skip_init: continue else: j.command = 'value' if self.use_mdsconnect: # res = self._run_job_mdsconnect(j, sig_names, dim_names, names[k]) #ttt = time.time() res = self._run_job_mdsconnect(j) #print time.time() - ttt, j if len(self.error) != 0: self.r['error message'] = self.error[:] self.r[names[k]] = res return self.r # return _evaluate_sig(self.r, sig_names, dim_names) else: res = self._run_job(j) if len(self.error) != 0: self.r['error message'] = self.error[:] self.r[names[k]] = res return self.r # return _evaluate_sig(self.r, sig_names, dim_names) self._connection_flag = False self.r[names[k]] = res except: self.error = ['run error', traceback.format_exc()] # self.r = _evaluate_sig(self.r, sig_names, dim_names) self.r['error message'] = self.error return self.r def set_globaljob(self, job): self.gjob = job def terminate(self): if self.connection is not None: del self.connection self.connection = None def _run_script_txt(self, job): expr = job.params[0] try: code = compile(expr, '<string>', 'exec') g = {} l = {} exec code in self.g, self.r return 'ok' except: self.error = ['Scrip Error', expr, sys.exc_info()[0]] return None # def _run_job_mdsconnect(self, job, sig_names,dim_names, name): def _run_job_mdsconnect(self, job): com = job.command if com == 'novalue': return '' if debug_runner: print(job) #print threading.current_thread().name, com, job.params if com == 'open': tree = job.params[0] shot = job.params[1] if self._tree != tree or self._shot != shot: try: #print tree, #print long(shot) if tree != '': self.connection.openTree(tree, long(shot)) except: self.error = ['run error', traceback.format_exc()] return None try: t = tree.split(',')[0].strip() node = '\\' + t + '::TOP' if node != self._defnode: self.connection.setDefault(node) self._defnode = node except: pass try: if shot != self._shot: expr = 'reset_private();reset_public();1' r = self.connection.get(expr).data() if self._connection_flag: if self.gjob is not None: expr = self.gjob.params[0] #print 'performing global tdi', expr r = self.connection.get(expr).data() self._connection_flag = False self._shot = shot self._tree = tree return 'ok' except: self.error = ['run error', traceback.format_exc()] return None elif com == 'defnode': node = job.params[0] try: if node.strip() != '': if node != self._defnode: self.connection.setDefault(node) self._defnode = node return 'ok' except: print(node) self.error = ['run error', traceback.format_exc()] return None elif com == 'value': try: expr = job.params[0] r = self.connection.get(expr).data() return r except: self.error = ['run error', expr, traceback.format_exc()] return None elif com == 'valuesig': try: expr = job.params[0] r = self.connection.get('_piscopevar=execute($)', expr).data() return r except: self.error = ['run error', expr, traceback.format_exc()] return None # elif com == 'dim_of':# this is not used anymore # expr = job.params[0] # dim_names.append(name) # return expr elif com == 'script_txt': return self._run_script_txt(job) # def _run_job(self, job, sig_names, dim_names, name): def _run_job(self, job): import MDSplus from MDSplus import Connection com = job.command if com == 'novalue': return '' if com == 'open': tree = job.params[0] shot = job.params[1] try: if tree != '': self.t = MDSplus.Tree(tree, long(shot)) if shot != self._shot: expr = 'reset_private();reset_public();1' # r =MDSplus.Data.compile(expr).evaluate().data() r = MDSplus.Data.execute(expr).data() if self._connection_flag: if self.gjob is not None: expr = self.gjob.params[0] r = MDSplus.Data.execute(expr).data() self._connection_flag = False self._shot = shot return 'ok' except: # print traceback.format_exc() self.error = ['run error', traceback.format_exc()] return None elif com == 'defnode': node = job.params[0] try: if node.strip() != '': tn = self.t.getNode(node) self.t.setDefault(tn) return 'ok' except: self.error = ['run error', traceback.format_exc()] return None elif com == 'value': try: expr = job.params[0] r = MDSplus.Data.execute(expr).data() # r =MDSplus.Data.execute(expr).data() # sig_names.append(name) return r except: self.error = ['run error', expr, traceback.format_exc()] return None elif com == 'valuesig': try: expr = job.params[0] r = MDSplus.Data.execute('_piscopevar=execute($)', expr).data() # r =MDSplus.Data.execute(expr).data() # sig_names.append(name) return r except: self.error = ['run error', expr, traceback.format_exc()] return None # elif com == 'dim_of': # this is not used anymore # expr = job.params[0] ### expr should be y, 0 (= dim_of(y, 0)) # dim_names.append(name) # return expr elif com == 'script_txt': return self._run_script_txt(job)
class JobRunner(object): def __init__(self): self.use_mdsconnect = False self.connection_str = '' self.connection = None self.g = {} self._shot = -1 def run(self, jobset, skip_init): import MDSplus from MDSplus import Connection self.error = [] jobs, names = jobset self.r = {} self.r['error message'] = [''] try: for k, job in enumerate(jobs): for j in job: if j.command == 'connection_mode': if self.connection_str != j.params[0]: self.connection_str = j.params[0] server, port, tree = parse_server_string(self.connection_str) self.use_mdsconnect = (False if server.upper() == 'DIRECT' else True) if self.use_mdsconnect: if self.connection is not None: del self.connection try: if port != '': server = server + ':' + port self.connection = Connection(server) res = 'ok' except: res = ('connection error' + traceback.format_exc()) else: if self.connection is not None: del self.connection self.connection = None res = 'ok' else: res = 'ok' else: if j.command == 'init': if skip_init: continue else: j.command = 'value' if self.use_mdsconnect: res = self._run_job_mdsconnect(j) if len(self.error) != 0: self.r['error message'] = self.error self.r[names[k]] = res return self.r else: res = self._run_job(j) if len(self.error) != 0: self.r['error message'] = self.error self.r[names[k]] = res return self.r self.r[names[k]] = res except: self.error = ['run error', traceback.format_exc()] self.r['error message'] = self.error return self.r def _run_script_txt(self, job): expr = job.params[0] try: code = compile(expr, '<string>', 'exec') g = {} l = {} exec code in self.g, self.r return 'ok' except: self.error = ['Scrip Error', expr, sys.exc_info()[0]] return None def _run_job_mdsconnect(self, job): com = job.command if com == 'open': tree = job.params[0] shot = job.params[1] try: self.connection.openTree(tree, long(shot)) return 'ok' except: self.error = ['run error', traceback.format_exc()] return None self._shot = shot elif com == 'defnode': node = job.params[0] try: if node.strip() != '': self.connection.setDefault(node) return 'ok' except: self.error = ['run error', traceback.format_exc()] return None elif com == 'value': try: expr = job.params[0] r =self.connection.get(expr).data() return r except: self.error = ['run error', expr,traceback.format_exc()] return None elif com == 'script_txt': return self._run_script_txt(job) def _run_job(self, job): import MDSplus from MDSplus import Connection com = job.command if com == 'open': tree = job.params[0] shot = job.params[1] try: self.t = MDSplus.Tree(tree, long(shot)) return 'ok' except: self.error = ['run error', traceback.format_exc()] return None self._shot = shot elif com == 'defnode': node = job.params[0] try: if node.strip() != '': tn = self.t.getNode(node) self.t.setDefault(tn) return 'ok' except: self.error = ['run error', traceback.format_exc()] return None elif com == 'value': try: expr = job.params[0] r =MDSplus.Data.compile(expr).evaluate().data() return r except: self.error = ['run error', expr, traceback.format_exc()] return None elif com == 'script_txt': return self._run_script_txt(job)
start = time() d = c.Data.execute('random($)', samples).data() end = time() print( """ Speed test for connection to %s Using dill for pickling: %s Floating point samples retrieved: %d MBytes received: %g Transaction time in seconds: %g Transfer speed in MB/s: %g """ % (host, usingDill, samples, d.nbytes * 1E-6, end - start, d.nbytes * 1E-6 / (end - start))) c.connection.close() if mdsip is not None: from MDSplus import Connection c = Connection(mdsip) start = time() d = c.get('random($)', samples).data() end = time() print(""" Speed test for mdsip connection to %s Floating point samples retrieved: %d MBytes received: %g Transaction time in seconds: %g Transfer speed in MB/s: %g """ % (mdsip, samples, d.nbytes * 1E-6, end - start, d.nbytes * 1E-6 / (end - start)))
def get_input(): shots = [] signals = [] trees = [] labels = [] processes = [] parameters = [] for i in range(1, 6): shot = names['shot' + str(i)].get() if len(shot) > 0: shot = int(shot) shots.append(shot) else: if i == 1: from MDSplus import Connection as CN cn = CN('mds.ipp.ac.cn') shot = cn.get("current_shot('east')") shot = int(shot) shots.append(shot) for i in range(1, 8): signal = names['signal' + str(i)].get() tree = names['tree' + str(i)].get() label = names['label' + str(i)].get() process = names['process' + str(i)].get() parameter = names['parameter' + str(i)].get() if len(signal) > 0: signals.append(signal) if len(tree) == 0: trees.append('east_1') else: trees.append(tree) if len(label) == 0: labels.append(signal) else: labels.append(label) if len(process) == 0: processes.append('None') parameters.append('None') else: processes.append(process) if len(parameter) == 0: parameters.append('None') else: parameter_list = parameter.split(",") parameter = [ float(parameter_list[i]) for i in range(len(parameter_list)) ] parameters.append(parameter) begin_time = begin.get() end_time = end.get() if len(begin_time) == 0: begin_time = 0 if len(end_time) == 0: end_time = 10 begin_time = float(begin_time) end_time = float(end_time) low_frequency = low_filter.get() if len(low_frequency) == 0: low_frequency = 0 low_frequency = int(low_frequency) up_frequency = up_filter.get() if len(up_frequency) == 0: up_frequency = 0 up_frequency = int(up_frequency) return shots, signals, trees, begin_time, end_time, low_frequency, up_frequency, labels, processes, parameters
def get_input(): signals1 = [] trees1 = [] labels1 = [] zooms1 = [] signals2 = [] trees2 = [] labels2 = [] zooms2 = [] signals3 = [] trees3 = [] labels3 = [] zooms3 = [] units = [] shot = names['shot1'].get() if len(shot) > 0: shot = int(shot) else: from MDSplus import Connection as CN cn = CN('mds.ipp.ac.cn') shot = cn.get("current_shot('east')") shot = int(shot) for i in range(1, 8): signal1 = names['signal1' + str(i)].get() tree1 = names['tree1' + str(i)].get() label1 = names['label1' + str(i)].get() zoom1 = names['zoom1' + str(i)].get() signal2 = names['signal2' + str(i)].get() tree2 = names['tree2' + str(i)].get() label2 = names['label2' + str(i)].get() zoom2 = names['zoom2' + str(i)].get() signal3 = names['signal3' + str(i)].get() tree3 = names['tree3' + str(i)].get() label3 = names['label3' + str(i)].get() zoom3 = names['zoom3' + str(i)].get() unit = names['unit' + str(i)].get() if len(signal1) > 0: signals1.append(signal1) if len(tree1) == 0: trees1.append('east_1') else: trees1.append(tree1) if len(label1) == 0: labels1.append(signal1) else: labels1.append(label1) if len(zoom1) == 0: zooms1.append(1) else: zooms1.append(float(zoom1)) if len(signal2) > 0: signals2.append(signal2) if len(tree2) == 0: trees2.append('east_1') else: trees2.append(tree2) if len(label2) == 0: labels2.append(signal2) else: labels2.append(label2) if len(zoom2) == 0: zooms2.append(1) else: zooms2.append(float(zoom2)) else: signals2.append('0') trees2.append('0') labels2.append('0') zooms2.append(0) if len(signal3) > 0: signals3.append(signal3) if len(tree3) == 0: trees3.append('east_1') else: trees3.append(tree3) if len(label3) == 0: labels3.append(signal3) else: labels3.append(label3) if len(zoom3) == 0: zooms3.append(1) else: zooms3.append(float(zoom3)) else: signals3.append('0') trees3.append('0') labels3.append('0') zooms3.append(0) if len(unit) > 0: units.append(unit) else: units.append('0') begin_time = begin.get() end_time = end.get() if len(begin_time) == 0: begin_time = 0 if len(end_time) == 0: end_time = 10 begin_time = float(begin_time) end_time = float(end_time) low_frequency = low_filter.get() if len(low_frequency) == 0: low_frequency = 0 low_frequency = int(low_frequency) up_frequency = up_filter.get() if len(up_frequency) == 0: up_frequency = 0 up_frequency = int(up_frequency) return shot, signals1, trees1, labels1, zooms1, signals2, trees2, labels2, zooms2, signals3, trees3, labels3, zooms3, units, begin_time, end_time, low_frequency, up_frequency
class JobRunner(object): def __init__(self): self.use_mdsconnect = False self.connection_str = '' self.connection = None self.g = {} self._shot = -1 self.gjob = None # global tdi (executed when shot number changed) self._tree = '' self._defnode = '' def __del__(self): if self.connection is not None: del self.connection def run(self, jobset, skip_init): # def _evaluate_sig(res, sig_names, dim_names): # for name in dim_names: # try: # src, dim = res[name][0:3].split(',') # res[name] = res[src].dim_of(long(dim)).data() # except: # pass # for name in sig_names: # try: # res[name] = res[name].data() # except: # pass # return res self.error = [] jobs, names = jobset self.r = {'error message': []} sig_names = [] dim_names = [] try: for k, job in enumerate(jobs): for j in job: # print j if j.command == 'connection_mode': # print 'connection_mode param', self.connection_str, j.params, self.connection # if self.connection is not None: # del self.connection # self.connection = None if self.connection_str != j.params[0]: # if True: server, port, tree = parse_server_string(j.params[0]) self.use_mdsconnect = (False if server.upper() == 'DIRECT' else True) if self.use_mdsconnect: if self.connection is not None: del self.connection self.connection = None try: if port != '': server = server + ':' + port self.connection = Connection(server) self.connection_str = j.params[0] res = 'ok' except: self.r['error message'] = ['connection error' + traceback.format_exc()] self.r[names[k]] = None return self.r else: if self.connection is not None: del self.connection self.connection = None res = 'ok' else: res = 'ok' self._connection_flag = True else: if j.command == 'init': if skip_init: continue else: j.command = 'value' if self.use_mdsconnect: # res = self._run_job_mdsconnect(j, sig_names, dim_names, names[k]) #ttt = time.time() res = self._run_job_mdsconnect(j) #print time.time() - ttt, j if len(self.error) != 0: self.r['error message'] = self.error[:] self.r[names[k]] = res return self.r # return _evaluate_sig(self.r, sig_names, dim_names) else: res = self._run_job(j) if len(self.error) != 0: self.r['error message'] = self.error[:] self.r[names[k]] = res return self.r # return _evaluate_sig(self.r, sig_names, dim_names) self._connection_flag = False self.r[names[k]] = res except: self.error = ['run error', traceback.format_exc()] # self.r = _evaluate_sig(self.r, sig_names, dim_names) self.r['error message'] = self.error return self.r def set_globaljob(self, job): self.gjob = job def terminate(self): if self.connection is not None: del self.connection self.connection = None def _run_script_txt(self, job): expr = job.params[0] try: code = compile(expr, '<string>', 'exec') g = {} l = {} exec code in self.g, self.r return 'ok' except: self.error = ['Scrip Error', expr, sys.exc_info()[0]] return None # def _run_job_mdsconnect(self, job, sig_names,dim_names, name): def _run_job_mdsconnect(self, job): com = job.command if com == 'novalue': return '' if debug_runner: print(job) #print threading.current_thread().name, com, job.params if com == 'open': tree = job.params[0] shot = job.params[1] if self._tree != tree or self._shot != shot: try: #print tree, #print long(shot) if tree != '': self.connection.openTree(tree, long(shot)) except: self.error = ['run error', traceback.format_exc()] return None try: t = tree.split(',')[0].strip() node = '\\'+ t +'::TOP' if node != self._defnode: self.connection.setDefault(node) self._defnode = node except: pass try: if shot != self._shot: expr = 'reset_private();reset_public();1' r =self.connection.get(expr).data() if self._connection_flag: if self.gjob is not None: expr = self.gjob.params[0] #print 'performing global tdi', expr r =self.connection.get(expr).data() self._connection_flag = False self._shot = shot self._tree = tree return 'ok' except: self.error = ['run error', traceback.format_exc()] return None elif com == 'defnode': node = job.params[0] try: if node.strip() != '': if node != self._defnode: self.connection.setDefault(node) self._defnode = node return 'ok' except: print(node) self.error = ['run error', traceback.format_exc()] return None elif com == 'value': try: expr = job.params[0] r =self.connection.get(expr).data() return r except: self.error = ['run error', expr,traceback.format_exc()] return None elif com == 'valuesig': try: expr = job.params[0] r =self.connection.get('_piscopevar=execute($)', expr).data() return r except: self.error = ['run error', expr,traceback.format_exc()] return None # elif com == 'dim_of':# this is not used anymore # expr = job.params[0] # dim_names.append(name) # return expr elif com == 'script_txt': return self._run_script_txt(job) # def _run_job(self, job, sig_names, dim_names, name): def _run_job(self, job): import MDSplus from MDSplus import Connection com = job.command if com == 'novalue': return '' if com == 'open': tree = job.params[0] shot = job.params[1] try: if tree != '': self.t = MDSplus.Tree(tree, long(shot)) if shot != self._shot: expr = 'reset_private();reset_public();1' # r =MDSplus.Data.compile(expr).evaluate().data() r =MDSplus.Data.execute(expr).data() if self._connection_flag: if self.gjob is not None: expr = self.gjob.params[0] r =MDSplus.Data.execute(expr).data() self._connection_flag = False self._shot = shot return 'ok' except: # print traceback.format_exc() self.error = ['run error', traceback.format_exc()] return None elif com == 'defnode': node = job.params[0] try: if node.strip() != '': tn = self.t.getNode(node) self.t.setDefault(tn) return 'ok' except: self.error = ['run error', traceback.format_exc()] return None elif com == 'value': try: expr = job.params[0] r =MDSplus.Data.execute(expr).data() # r =MDSplus.Data.execute(expr).data() # sig_names.append(name) return r except: self.error = ['run error', expr, traceback.format_exc()] return None elif com == 'valuesig': try: expr = job.params[0] r =MDSplus.Data.execute('_piscopevar=execute($)', expr).data() # r =MDSplus.Data.execute(expr).data() # sig_names.append(name) return r except: self.error = ['run error', expr, traceback.format_exc()] return None # elif com == 'dim_of': # this is not used anymore # expr = job.params[0] ### expr should be y, 0 (= dim_of(y, 0)) # dim_names.append(name) # return expr elif com == 'script_txt': return self._run_script_txt(job)