Exemple #1
0
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 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
Exemple #4
0
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 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
Exemple #6
0
def get_connection(shot, tree='mst'):

    """Get an MDSplus connection object connected to the appropriate 
    server and having the given tree and shot opened. 
    """
    svr = get_server_for_shot(shot)
    conn, tree_, shot_ = _get_svr_cached(svr)
        
    if conn is None:
        conn = Connection(svr)
        
    if tree != tree_ or shot != shot_:
        try:
            # This throws an exception if there are no open trees. 
            conn.closeAllTrees()
        except:
            pass
        conn.openTree(tree, shot)
        
    _update_svr_cache(svr, conn, tree, shot)
    
    return conn
Exemple #7
0
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)
Exemple #8
0
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
Exemple #9
0
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)
Exemple #11
0
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, 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)
Exemple #13
0
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