Beispiel #1
0
    def write_full(self, key, data):
        """
        Write an entire object synchronously.

        The object is filled with the provided data. If the object exists,
        it is atomically truncated and then written.

        :param key: name of the object
        :type key: str
        :param data: data to write
        :type data: str

        :raises: :class:`TypeError`
        :raises: :class:`Error`
        :returns: int - 0 on success
        """
        self.require_ioctx_open()
        if not isinstance(key, str):
            raise TypeError('key must be a string')
        if not isinstance(data, str):
            raise TypeError('data must be a string')
        length = len(data)
        ret = self.librados.rados_write_full(self.io, c_char_p(key),
                 c_char_p(data), c_size_t(length))
        if ret == 0:
            return ret
        else:
            raise make_ex(ret, "Ioctx.write(%s): failed to write_full %s" % \
                (self.name, key))
Beispiel #2
0
    def convert_word(self, word, flat=False):
        if word[-1] == ':':
            word = word[0:-1]
        if not word:
            return list()
        if word[0] == '-':
            word = word[1:]

        self.lock.acquire()
        we = word.encode('utf-8')
        st = ctypes.c_char_p(we)
        self.lib.analyse_item(st, 0)
        result = self.lib.first_analysis_result()
        ret = []
        while result:
            if self.lib.get_value_type(result) != 4:
                raise Exception('Unknown libmalaga result type!')
            s = ctypes.c_char_p(self.lib.get_value_string(result))
            ret.append(str(s.value, 'utf-8'))
            self.libc.free(s)
            result = self.lib.next_analysis_result()
        self.lock.release()

	if flat:
            ret = list(set(ret))
            if len(ret) > 0:
                trans = self.FI_TRANSFORMS.get(ret[0])
                if trans and ret == trans[0]:
                    ret = trans[1]
        else:
            if len(ret) == 1:
                ret = ret[0]
        return ret
Beispiel #3
0
 def rename(self, src, dst):
     self.require_state("mounted")
     if not isinstance(src, basestring) or not isinstance(dst, basestring):
         raise TypeError('source and destination must be a string')
     ret = self.libcephfs.ceph_rename(self.cluster, c_char_p(src), c_char_p(dst))
     if ret < 0:
         raise make_ex(ret, "error in rename '%s' to '%s'" % (src, dst))
Beispiel #4
0
def multi_graph(V):
    for i, a in enumerate(F):
        for j, b in enumerate(F):
            if i == j:
                continue
            t = c_sp_align(c_char_p(a), c_char_p(b))
            yield (i, j, t)
Beispiel #5
0
    def mds_command(self, mds_spec, args, input_data):
        """
        :return 3-tuple of output status int, output status string, output data
        """

        cmdarr = (c_char_p * len(args))(*args)

        outbufp = pointer(pointer(c_char()))
        outbuflen = c_long()
        outsp = pointer(pointer(c_char()))
        outslen = c_long()

        ret = self.libcephfs.ceph_mds_command(self.cluster, c_char_p(mds_spec),
                                              cmdarr, len(args),
                                              c_char_p(input_data),
                                              len(input_data), outbufp,
                                              byref(outbuflen), outsp,
                                              byref(outslen))

        my_outbuf = outbufp.contents[:(outbuflen.value)]
        my_outs = outsp.contents[:(outslen.value)]
        if outbuflen.value:
            self.libcephfs.ceph_buffer_free(outbufp.contents)
        if outslen.value:
            self.libcephfs.ceph_buffer_free(outsp.contents)

        return (ret, my_outbuf, my_outs)
Beispiel #6
0
def get_consensus(align, quals=[], cons_thres=-1.0, qual_thres=' ', gapped=False):
  cons_thres_c = ctypes.c_double(cons_thres)
  qual_thres_c = ctypes.c_char(qual_thres)
  n_seqs = len(align)
  if gapped:
    gapped_c = 1
  else:
    gapped_c = 0
  assert not quals or len(quals) == n_seqs, 'Different number of sequences and quals.'
  seq_len = None
  for seq in (align + quals):
    if seq_len is None:
      seq_len = len(seq)
    else:
      assert seq_len == len(seq), 'All sequences in the alignment must be the same length.'
  align_c = (ctypes.c_char_p * n_seqs)()
  for i, seq in enumerate(align):
    align_c[i] = ctypes.c_char_p(seq)
  quals_c = (ctypes.c_char_p * n_seqs)()
  for i, qual in enumerate(quals):
    quals_c[i] = ctypes.c_char_p(qual)
  if not quals:
    quals_c = 0
  return consensus.get_consensus(align_c, quals_c, n_seqs, seq_len, cons_thres_c, qual_thres_c,
                                 gapped_c)
Beispiel #7
0
def get_consensus_duplex(align1, align2, quals1=[], quals2=[], cons_thres=-1.0, qual_thres=' ',
                         method='iupac'):
  assert method in ('iupac', 'freq')
  cons_thres_c = ctypes.c_double(cons_thres)
  qual_thres_c = ctypes.c_char(qual_thres)
  n_seqs1 = len(align1)
  n_seqs2 = len(align2)
  assert (not quals1 and not quals2) or (quals1 and quals2)
  assert not quals1 or len(quals1) == n_seqs1
  assert not quals2 or len(quals2) == n_seqs2
  seq_len = None
  for seq in (align1 + align2 + quals1 + quals2):
    if seq_len is None:
      seq_len = len(seq)
    else:
      assert seq_len == len(seq), 'All sequences in the alignment must be the same length.'
  align1_c = (ctypes.c_char_p * n_seqs1)()
  for i, seq in enumerate(align1):
    align1_c[i] = ctypes.c_char_p(seq)
  align2_c = (ctypes.c_char_p * n_seqs1)()
  for i, seq in enumerate(align2):
    align2_c[i] = ctypes.c_char_p(seq)
  quals1_c = (ctypes.c_char_p * n_seqs1)()
  for i, seq in enumerate(quals1):
    quals1_c[i] = ctypes.c_char_p(seq)
  quals2_c = (ctypes.c_char_p * n_seqs1)()
  for i, seq in enumerate(quals2):
    quals2_c[i] = ctypes.c_char_p(seq)
  if not quals1:
    quals1_c = 0
  if not quals2:
    quals2_c = 0
  return consensus.get_consensus_duplex(align1_c, align2_c, quals1_c, quals2_c, n_seqs1, n_seqs2,
                                        seq_len, cons_thres_c, qual_thres_c, method)
Beispiel #8
0
def move_container(ns_name, cid):
	print "Moving container " + cid + " to another meta2"
	ns_name = ctypes.c_char_p(ns_name)
	cid = ctypes.c_char_p(cid)
	lib = ctypes.cdll.LoadLibrary("librulesmotorpy2c.so")
	rc = lib.motor_move_container(ns_name, cid)
	return bool(rc)
Beispiel #9
0
 def create_yamr(self, stor_file, port, info_filename, 
                 n_fail_max, overdue_resched_fac, overdue_giveup_fac):
     func = self.runlib.rmic_create_yamr
     func.restype = ctypes.c_void_p
     self.obj = func( ctypes.c_char_p(stor_file), 
           ctypes.c_char_p(port), ctypes.c_char_p(info_filename), 
           n_fail_max, ctypes.c_double(overdue_resched_fac), ctypes.c_double(overdue_giveup_fac))
Beispiel #10
0
    def connect(self):
        databroker = self._databroker
        if self._conn is None:
            conn = databroker.create_connection()
            self._conn = conn
            eptTCP = dbrksvrtype.TProtocolType.eptTCP.value
            emtVideo = dbrksvrtype.TMediaType.emtVideo.value
            emtAudio = dbrksvrtype.TMediaType.emtAudio.value
            conn_opt = dbrkhelper.TDataBrokerConnectionOptions()
            conn_opt.pzIPAddr = ctypes.c_char_p(self._camera.url)
            conn_opt.wHttpPort = ctypes.c_ushort(self._camera.http_port)
            conn_opt.pzUID = ctypes.c_char_p(self._camera.username)
            conn_opt.pzPWD = ctypes.c_char_p(self._camera.password)
            conn_opt.dwProtocolType = eptTCP
            conn_opt.pzServerType = "auto"
            conn_opt.dwMediaType = (emtVideo | emtAudio)
            conn_opt.pfStatus = dbrkhelper.FTDataBrokerStatusCallback(self._update_connection_status)
            conn_opt.pfAV = dbrkhelper.FTDataBrokerAVCallback(self._put_one_frame)
            dwFlags = dbrkhelper.TDataBrokerConnOptionFlag.eConOptProtocolAndMediaType.value
            dwFlags |= dbrkhelper.TDataBrokerConnOptionFlag.eConOptHttpPort.value
            conn_opt.dwFlags = ctypes.c_ulong(dwFlags)
            databroker.set_connection_options(conn, conn_opt)
        else:
            conn = self._conn

        return databroker.connect(conn)
Beispiel #11
0
def exists(name):
	"""Validates the name of a netgroup"""

	try:
		## try to switch to 'str' object rather than unicode
		name = name.encode('utf8')
	except Exception as ex:
		## ignore
		pass

	host,user,domain = c_char_p(None),c_char_p(None),c_char_p(None)
	libc = CDLL('libc.so.6')
	libc.setnetgrent(name)

	try:
		while libc.getnetgrent(_byref(host), _byref(user), _byref(domain)):
			libc.endnetgrent()
			return True

		libc.endnetgrent()
		return False

	except Exception as ex:
		libc.endnetgrent()
		return False
Beispiel #12
0
    def aead_decrypt(self, data):
        """
        Decrypt data and authenticate tag

        :param data: cipher text with tag
        :return: plain text
        """
        global buf_size, buf
        cipher_out_len = c_size_t(0)
        plen = len(data) - self._tlen
        if buf_size < plen:
            buf_size = plen * 2
            buf = create_string_buffer(buf_size)
        tag = data[plen:]
        r = libmbedtls.mbedtls_cipher_auth_decrypt(
            byref(self._ctx),
            c_char_p(self._nonce.raw), c_size_t(self._nlen),
            None, c_size_t(0),
            c_char_p(data), c_size_t(plen),
            byref(buf), byref(cipher_out_len),
            c_char_p(tag), c_size_t(self._tlen)
        )
        if r:
            raise Exception('AEAD encrypt failed {0:#x}'.format(r))
        self.cipher_ctx_init()
        return buf.raw[:cipher_out_len.value]
Beispiel #13
0
    def __init__(self, cipher_name, key, iv, op, crypto_path=None):
        if cipher_name[:len('mbedtls:')] == 'mbedtls:':
            cipher_name = cipher_name[len('mbedtls:'):]
        MbedTLSCryptoBase.__init__(self, cipher_name, crypto_path)
        key_ptr = c_char_p(key)
        iv_ptr = c_char_p(iv)
        r = libmbedtls.mbedtls_cipher_setkey(
            byref(self._ctx),
            key_ptr, c_int(len(key) * 8),
            c_int(op)
        )
        if r:
            self.clean()
            raise Exception('can not set cipher key')
        r = libmbedtls.mbedtls_cipher_set_iv(
            byref(self._ctx),
            iv_ptr, c_size_t(len(iv))
        )
        if r:
            self.clean()
            raise Exception('can not set cipher iv')
        r = libmbedtls.mbedtls_cipher_reset(byref(self._ctx))
        if r:
            self.clean()
            raise Exception('can not reset cipher')

        self.encrypt = self.update
        self.decrypt = self.update
Beispiel #14
0
    def mount(self, source=None, target=None, mount_type=None,
              filesystemtype=None, data=None):
        if not [arg for arg in [source, target, filesystemtype, mount_type]
                if arg is not None]:
            return
        func_obj = self.functions["mount"]

        if source is None:
            source = "none"
        if target is None:
            target = c_char_p()
        if filesystemtype is None:
            filesystemtype = c_char_p()
        if mount_type is None:
            mount_type = "unchanged"
        if data is None:
            data = c_void_p()

        flag = func_obj.extra["flag"]
        propagation = func_obj.extra["propagation"]
        mount_flags = propagation[mount_type]
        mount_vals = [flag[k] for k in mount_flags]
        flags = reduce(lambda res, val: res | val, mount_vals, 0)

        self._c_func_mount(source, target, filesystemtype, flags, data)
Beispiel #15
0
    def aead_encrypt(self, data):
        """
        Encrypt data with authenticate tag

        :param data: plain text
        :return: cipher text with tag
        """
        global buf_size, buf
        plen = len(data)
        if buf_size < plen + self._tlen:
            buf_size = (plen + self._tlen) * 2
            buf = create_string_buffer(buf_size)
        cipher_out_len = c_size_t(0)
        tag_buf = create_string_buffer(self._tlen)

        r = libmbedtls.mbedtls_cipher_auth_encrypt(
            byref(self._ctx),
            c_char_p(self._nonce.raw), c_size_t(self._nlen),
            None, c_size_t(0),
            c_char_p(data), c_size_t(plen),
            byref(buf), byref(cipher_out_len),
            byref(tag_buf), c_size_t(self._tlen)
        )
        assert cipher_out_len.value == plen
        if r:
            raise Exception('AEAD encrypt failed {0:#x}'.format(r))
        self.cipher_ctx_init()
        return buf.raw[:cipher_out_len.value] + tag_buf.raw[:self._tlen]
Beispiel #16
0
 def getBoxFilter(self,
         time, point_coords,
         data_set = 'isotropic1024coarse',
         make_modulo = False,
         field = 'velocity',
         filter_width = 7*2*np.pi / 1024):
     if not self.connection_on:
         print('you didn\'t connect to the database')
         sys.exit()
     if not (point_coords.shape[-1] == 3):
         print ('wrong number of values for coordinates in getBoxFilter')
         sys.exit()
         return None
     if not (point_coords.dtype == np.float32):
         print 'point coordinates in getBoxFilter must be floats. stopping.'
         sys.exit()
         return None
     npoints = point_coords.shape[0]
     for i in range(1, len(point_coords.shape)-1):
         npoints *= point_coords.shape[i]
     if make_modulo:
         pcoords = np.zeros(point_coords.shape, np.float64)
         pcoords[:] = point_coords
         np.mod(pcoords, 2*np.pi, point_coords)
     result_array = point_coords.copy()
     self.lib.getBoxFilter(self.authToken,
              ctypes.c_char_p(data_set),
              ctypes.c_char_p(field),
              ctypes.c_float(time),
              ctypes.c_float(filter_width),
              ctypes.c_int(npoints),
              point_coords.ctypes.data_as(ctypes.POINTER(ctypes.POINTER(ctypes.c_float))),
              result_array.ctypes.data_as(ctypes.POINTER(ctypes.POINTER(ctypes.c_float))))
     return result_array
Beispiel #17
0
	def SetValue(self, value):
		# set object type
		otype = None
		size = 8
		if type(value) == str:
			otype = ASN_OCTET_STR
			size = len(value)
			value = ctypes.c_char_p(value)
		elif type(value) == int:
			otype = ASN_INTEGER
			value = ctypes.pointer(ctypes.c_int(value))
		elif type(value) == float:
			otype = ASN_APP_FLOAT
			value = ctypes.pointer(ctypes.c_float(value))
		elif type(value) == SnmpIpAddress:
			otype = ASN_IPADDRESS
			size = 4
			value = ctypes.c_char_p(socket.inet_aton(value))
		elif type(value) == SnmpCounter32:
			otype = ASN_COUNTER32
			value = ctypes.pointer(ctypes.c_uint(value))
		elif type(value) == SnmpGauge32:
			otype = ASN_UNSIGNED
			value = ctypes.pointer(ctypes.c_uint(value))
		axl.snmp_set_var_typed_value(self.__request.requestvb, otype, ctypes.cast(value, ctypes.POINTER(ctypes.c_ubyte)), size)
		self.value = value
Beispiel #18
0
def vasco_verify(data, params, password, challenge="\0" * 16):
    res = Vasco_DLL.AAL2VerifyPassword(pointer(data),
                                           pointer(params),
                                           c_char_p(password),
                                           c_char_p(challenge)
                                          )
    return (res, data)
 def ENopen(self, inpfile=None, rptfile=None, binfile=None):
     """
     Opens EPANET input file & reads in network data
     
     Arguments:
      * inpfile = EPANET .inp input file (default to constructor value)
      * rptfile = Output file to create (default to constructor value)
      * binfile = Binary output file to create (default to constructor value)
     
     """
     if self.fileLoaded: self.ENclose()
     if self.fileLoaded: 
         raise(EPANETException('Fatal error closing previously opened file'))
     
     if inpfile is None: inpfile = self.inpfile
     if rptfile is None: rptfile = self.rptfile
     if binfile is None: binfile = self.binfile
     
     en_open = getattr(self.ENlib, 'ENopen')
     self.errcode = en_open(c_char_p(inpfile.encode()),
                            c_char_p(rptfile.encode()),
                            c_char_p(binfile.encode()))
     self._error()
     if self.errcode < 100:
         self.fileLoaded = True
     return
Beispiel #20
0
def vasco_dpxinit(filename="demovdp.dpx", transportkey=None):
    '''
    This function returns
     - error code
     - filehandle (TDPXHandle)
     - application count
     - application names
     - token counts
    '''
    if not transportkey:
        transportkey = "1" * 32

    c_filename = c_char_p(filename)
    c_transportkey = c_char_p(transportkey)
    # string with 97 bytes
    appl_names = "." * 97
    p_names = c_char_p(appl_names)

    fh = TDPXHandle()
    p_fh = pointer(fh)

    appl_count = c_int(0)
    p_acount = pointer(appl_count)

    token_count = c_int(0)
    p_tcount = pointer(token_count)

    res = Vasco_DLL.AAL2DPXInit(p_fh,
                            c_filename,
                            c_transportkey,
                            p_acount,
                            p_names,
                            p_tcount)

    return (res, fh, appl_count, appl_names, token_count)
Beispiel #21
0
def vasco_genpassword(data, params, challenge="\0" * 16):
    password = "******" * 41
    res = Vasco_DLL.AAL2GenPassword(pointer(data),
                                     pointer(params),
                                     c_char_p(password),
                                     c_char_p(challenge))
    return (res, password)
Beispiel #22
0
def _sem_open(name, value=None):
    """ Construct or retrieve a semaphore with the given name

    If value is None, try to retrieve an existing named semaphore.
    Else create a new semaphore with the given value
    """
    if value is None:
        handle = pthread.sem_open(ctypes.c_char_p(name), 0)
    else:
        handle = pthread.sem_open(ctypes.c_char_p(name), SEM_OFLAG, SEM_PERM,
                                  ctypes.c_int(value))

    if handle == SEM_FAILURE:
        e = ctypes.get_errno()
        if e == errno.EEXIST:
            raise FileExistsError("a semaphore named %s already exists" % name)
        elif e == errno.ENOENT:
            raise FileNotFoundError('cannot find semaphore named %s' % name)
        elif e == errno.ENOSYS:
            raise NotImplementedError('No semaphore implementation on this '
                                      'system')
        else:
            raiseFromErrno()

    return handle
Beispiel #23
0
def _commoncrypto_pbkdf2(data, salt, iterations, digest, keylen):
    """Common Crypto compatibile wrapper
    """
    c_hashfunc = ctypes.c_uint32(_commoncrypto_hashlib_to_crypto_map_get(digest))
    c_pass = ctypes.c_char_p(data)
    c_passlen = ctypes.c_size_t(len(data))
    c_salt = ctypes.c_char_p(salt)
    c_saltlen = ctypes.c_size_t(len(salt))
    c_iter = ctypes.c_uint(iterations)
    c_keylen = ctypes.c_size_t(keylen)
    c_buff = ctypes.create_string_buffer(keylen)

    crypto.CCKeyDerivationPBKDF.restype = ctypes.c_int
    crypto.CCKeyDerivationPBKDF.argtypes = [ctypes.c_uint32,
                                            ctypes.c_char_p,
                                            ctypes.c_size_t,
                                            ctypes.c_char_p,
                                            ctypes.c_size_t,
                                            ctypes.c_uint32,
                                            ctypes.c_uint,
                                            ctypes.c_char_p,
                                            ctypes.c_size_t]
    ret = crypto.CCKeyDerivationPBKDF(2, # hardcoded 2-> PBKDF2
                                           c_pass, c_passlen,
                                           c_salt, c_saltlen,
                                           c_hashfunc,
                                           c_iter,
                                           c_buff,
                                           c_keylen)

    return (1 - ret, c_buff)
Beispiel #24
0
	def setFrom(self, val, name=None):
		"""Create an argument using a value as input.
		
		Arguments:
		Value -- an OpenSCAD value
		string -- the name of the value
		"""
		if name:
			self.name = ctypes.c_char_p(name)
		else:
			self.name = ctypes.c_char_p(0)
			
		if isinstance(val, bool):
			self.type = 'b'
			self.boolValue = ctypes.c_bool(val)
		elif isinstance(val, int) or isinstance(val, float):
			self.type = 'd'
			self.dblValue = ctypes.c_double(val)
		elif isinstance(val, str):
			self.type = 's'
			self.strValue = ctypes.c_char_p(val)
		elif isinstance(val, list) or isinstance(val, tuple):
			self.type = 'v'
			self.vecLen = ctypes.c_int(len(val))
			arr = (ctypes.c_double * len(val))()
			for i, v in enumerate(val):
				arr[i] = ctypes.c_double(v)
			self.vecValue = arr
Beispiel #25
0
    def getPeakProperty(self, p_name):
        """
        Return a numpy array containing the requested property.
        """
        if not p_name in self.peak_properties:
            raise MultiFitterException("No such property '" + p_name + "'")

        # Properties that are calculated from other properties.
        if(self.peak_properties[p_name] == "compound"):

            # Return 0 length array if there are no localizations.
            if(self.getNFit() == 0):
                return numpy.zeros(0, dtype = numpy.float64)
                
            # Peak significance calculation.
            if(p_name == "significance"):
                bg_sum = self.getPeakProperty("bg_sum")
                fg_sum = self.getPeakProperty("fg_sum")
                return fg_sum/numpy.sqrt(bg_sum)
            
        # Floating point properties.
        elif(self.peak_properties[p_name] == "float"):
            values = numpy.ascontiguousarray(numpy.zeros(self.getNFit(), dtype = numpy.float64))
            self.clib.mFitGetPeakPropertyDouble(self.mfit,
                                                values,
                                                ctypes.c_char_p(p_name.encode()))
            return values

        # Integer properties.
        elif(self.peak_properties[p_name] == "int"):
            values = numpy.ascontiguousarray(numpy.zeros(self.getNFit(), dtype = numpy.int32))
            self.clib.mFitGetPeakPropertyInt(self.mfit,
                                             values,
                                             ctypes.c_char_p(p_name.encode()))
            return values
Beispiel #26
0
    def __init__(self, geom_input, srs=None):
        "Initializes Geometry on either WKT or an OGR pointer as input."

        str_instance = isinstance(geom_input, basestring)

        # If HEX, unpack input to to a binary buffer.
        if str_instance and hex_regex.match(geom_input):
            geom_input = buffer(a2b_hex(geom_input.upper()))
            str_instance = False

        # Constructing the geometry,
        if str_instance:
            # Checking if unicode
            if isinstance(geom_input, unicode):
                # Encoding to ASCII, WKT or HEX doesn't need any more.
                geom_input = geom_input.encode('ascii')

            wkt_m = wkt_regex.match(geom_input)
            json_m = json_regex.match(geom_input)
            if wkt_m:
                if wkt_m.group('type').upper() == 'LINEARRING':
                    # OGR_G_CreateFromWkt doesn't work with LINEARRING WKT.
                    #  See http://trac.osgeo.org/gdal/ticket/1992.
                    g = capi.create_geom(OGRGeomType(wkt_m.group('type')).num)
                    capi.import_wkt(g, byref(c_char_p(geom_input)))
                else:
                    g = capi.from_wkt(byref(c_char_p(geom_input)), None, byref(c_void_p()))
            elif json_m:
                if GEOJSON:
                    g = capi.from_json(geom_input)
                else:
                    raise NotImplementedError('GeoJSON input only supported on GDAL 1.5+.')
            else:
                # Seeing if the input is a valid short-hand string
                # (e.g., 'Point', 'POLYGON').
                ogr_t = OGRGeomType(geom_input)
                g = capi.create_geom(OGRGeomType(geom_input).num)
        elif isinstance(geom_input, buffer):
            # WKB was passed in
            g = capi.from_wkb(str(geom_input), None, byref(c_void_p()), len(geom_input))
        elif isinstance(geom_input, OGRGeomType):
            # OGRGeomType was passed in, an empty geometry will be created.
            g = capi.create_geom(geom_input.num)
        elif isinstance(geom_input, self.ptr_type):
            # OGR pointer (c_void_p) was the input.
            g = geom_input
        else:
            raise OGRException('Invalid input type for OGR Geometry construction: %s' % type(geom_input))

        # Now checking the Geometry pointer before finishing initialization
        # by setting the pointer for the object.
        if not g:
            raise OGRException('Cannot create OGR Geometry from input: %s' % str(geom_input))
        self.ptr = g

        # Assigning the SpatialReference object to the geometry, if valid.
        if bool(srs): self.srs = srs

        # Setting the class depending upon the OGR Geometry Type
        self.__class__ = GEO_CLASSES[self.geom_type.num]
Beispiel #27
0
 def set_param(self, name, value):
     """set paramter to the trainer"""
     name = str(name)
     value = str(value)
     cxnlib.CXNNetSetParam(self.handle,
                           ctypes.c_char_p(name.encode('utf-8')),
                           ctypes.c_char_p(value.encode('utf-8')))
Beispiel #28
0
def _openssl_pbkdf2(data, salt, iterations, digest, keylen):
    """OpenSSL compatibile wrapper
    """
    c_hashfunc = ctypes.c_void_p(_openssl_hashlib_to_crypto_map_get(digest))

    c_pass = ctypes.c_char_p(data)
    c_passlen = ctypes.c_int(len(data))
    c_salt = ctypes.c_char_p(salt)
    c_saltlen = ctypes.c_int(len(salt))
    c_iter = ctypes.c_int(iterations)
    c_keylen = ctypes.c_int(keylen)
    c_buff = ctypes.create_string_buffer(keylen)

    # PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
    #                            const unsigned char *salt, int saltlen, int iter,
    #                            const EVP_MD *digest,
    #                       int keylen, unsigned char *out);

    crypto.PKCS5_PBKDF2_HMAC.argtypes = [ctypes.c_char_p, ctypes.c_int,
                                         ctypes.c_char_p, ctypes.c_int,
                                         ctypes.c_int, ctypes.c_void_p,
                                         ctypes.c_int, ctypes.c_char_p]

    crypto.PKCS5_PBKDF2_HMAC.restype = ctypes.c_int
    err = crypto.PKCS5_PBKDF2_HMAC(c_pass, c_passlen,
                            c_salt, c_saltlen,
                            c_iter,
                            c_hashfunc,
                            c_keylen,
                            c_buff)
    return (err, c_buff)
Beispiel #29
0
    def __init__(self, packet, layers=0):
        fields = struct.unpack("!HHIIBBHHH", packet[:self.tcp_min_header_size])
        self.src_port = fields[0]
        self.dst_port = fields[1]
        self.seqnum = fields[2]
        self.acknum = fields[3]
        
        self.data_offset = 4 * (fields[4] >> 4)

        self.urg = fields[5] & 32
        self.ack = fields[5] & 16
        self.psh = fields[5] & 8
        self.rst = fields[5] & 4
        self.syn = fields[5] & 2
        self.fin = fields[5] & 1

        self.win = fields[6]
        self.sum = fields[7]
        urg_offset = 4 * fields[8] # rarely used

        if self.data_offset < 20:
            self.opt = b''
            self.payload = b''
        else:
            self.opt = ctypes.c_char_p(binascii.hexlify(packet[20:self.data_offset]))
            self.payload = ctypes.c_char_p(binascii.hexlify(packet[self.data_offset:]))
Beispiel #30
0
    def defineVariable(self, name='', value=''):
        """used to bind mailmerge variable to a recipient.

        name: string for mailmerge variable's name
        value: string for mailmerge variable's value"""
        if not pmta.PmtaRcptDefineVariable(self.recipient, c_char_p(name), c_char_p(value)):
            raise PmtaRecipientError(self.recipient)
#    os.system('open ' + test_name + '.html')


if __name__ == "__main__":
    if len(sys.argv) == 2 and str(sys.argv[1]) != "docker":
        run_test(str(sys.argv[1]))
    else:
        exe_ver = '???'
        lib_ver = '???'
        response = subprocess.check_output([CAPTION_INSPECTOR_EXE, '-v'], stderr=subprocess.STDOUT)
        if response.split()[0] == b"Version:":
            exe_ver = response.strip().decode('utf-8')
        clib = ctypes.CDLL(CAPTION_INSPECTOR_LIBRARY)
        clib.ExtrnlAdptrGetVersion.restype = ctypes.c_char_p
        response = clib.ExtrnlAdptrGetVersion()
        lib_ver = ctypes.c_char_p(response).value.decode('utf-8')
        if exe_ver != lib_ver:
            print("Version Mismatch! " + exe_ver + " vs. " + lib_ver)
            exit(1)
        date_str = str(datetime.now().strftime('%Y_%m_%d__%H_%M_%S'))
        out_file_name = date_str + '_test_output'
        file = open(out_file_name + '.xml', "w")
        file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
        file.write("<testsuites name=\"Caption Inspector " + exe_ver + " Tests\">\n")
        file.close()
        retval = 0
        for test in py_test_suites:
            if os.system('pytest -o junit_suite_name="' + py_test_suites_names[test] + '" --junitxml ' + date_str + '_' + test + '.xml test__' + test + '.py') != 0 or retval != 0:
                retval = 1
            os.system('cat ' + date_str + '_' + test + '.xml >> ' + out_file_name + '.xml')
#            os.system('rm ' + date_str + '_' + test + '.xml')
    def create_surface(self, x=0, y=0, w=0, h=0, layer=0):
        #Set the viewport position and size
        dst_rect = c_ints((x, y, w, h))
        src_rect = c_ints((x, y, w << 16, h << 16))

        if PLATFORM == PLATFORM_ANDROID:
            self.surface = openegl.eglGetCurrentSurface(EGL_DRAW)
            # Get the width and height of the screen - TODO, this system returns 100x100
            time.sleep(0.2)  #give it a chance to find out the dimensions
            w = c_int()
            h = c_int()
            openegl.eglQuerySurface(self.display, self.surface, EGL_WIDTH,
                                    byref(w))
            openegl.eglQuerySurface(self.display, self.surface, EGL_HEIGHT,
                                    byref(h))
            self.width, self.height = w.value, h.value
        elif PLATFORM == PLATFORM_PI:
            self.dispman_display = bcm.vc_dispmanx_display_open(
                0)  #LCD setting
            self.dispman_update = bcm.vc_dispmanx_update_start(0)
            alpha = c_ints((DISPMANX_FLAGS_ALPHA_PREMULT, 0, 0))
            self.dispman_element = bcm.vc_dispmanx_element_add(
                self.dispman_update, self.dispman_display, layer, dst_rect, 0,
                src_rect, DISPMANX_PROTECTION_NONE, alpha, 0, 0)

            nativewindow = (GLint * 3)(self.dispman_element, w, h + 1)
            bcm.vc_dispmanx_update_submit_sync(self.dispman_update)

            self.nw_p = ctypes.pointer(nativewindow)
            ### NB changing the argtypes to allow passing of bcm native window is
            ### deeply unsatisfactory. But xlib defines Window as c_ulong and ctypes
            ### isn't happy about a pointer being cast to an int
            openegl.eglCreateWindowSurface.argtypes = [
                EGLDisplay, EGLConfig,
                POINTER((GLint * 3)), EGLint
            ]
            self.surface = openegl.eglCreateWindowSurface(
                self.display, self.config, self.nw_p, 0)

        elif pi3d.USE_PYGAME:
            import pygame
            flags = pygame.OPENGL
            wsize = (w, h)
            if w == self.width and h == self.height:  # i.e. full screen
                flags = pygame.FULLSCREEN | pygame.OPENGL
                wsize = (0, 0)
            if self.display_config & DISPLAY_CONFIG_NO_RESIZE:
                flags |= pygame.RESIZABLE
            if self.display_config & DISPLAY_CONFIG_NO_FRAME:
                flags |= pygame.NOFRAME
            if self.display_config & DISPLAY_CONFIG_FULLSCREEN:
                flags |= pygame.FULLSCREEN
            elif self.display_config & DISPLAY_CONFIG_MAXIMIZED:
                flags |= pygame.FULLSCREEN
                wsize = (0, 0)

            self.width, self.height = w, h
            self.d = pygame.display.set_mode(wsize, flags)
            self.window = pygame.display.get_wm_info()["window"]
            self.surface = openegl.eglCreateWindowSurface(
                self.display, self.config, self.window, 0)

        else:  # work on basis it's X11
            # Set some WM info
            self.root = xlib.XRootWindowOfScreen(self.screen)
            if self.use_glx:  # For drawing on X window with transparent background
                numfbconfigs = c_int()
                VisData = c_ints(
                    (glx.GLX_RENDER_TYPE, glx.GLX_RGBA_BIT,
                     glx.GLX_DRAWABLE_TYPE, glx.GLX_WINDOW_BIT,
                     glx.GLX_DOUBLEBUFFER, True, glx.GLX_RED_SIZE, 8,
                     glx.GLX_GREEN_SIZE, 8, glx.GLX_BLUE_SIZE, 8,
                     glx.GLX_ALPHA_SIZE, 8, glx.GLX_DEPTH_SIZE, 16, 0))
                glx_screen = xlib.XDefaultScreen(self.d)
                fbconfigs = glx.glXChooseFBConfig(self.d, glx_screen, VisData,
                                                  byref(numfbconfigs))
                fbconfig = 0
                for i in range(numfbconfigs.value):
                    visual = glx.glXGetVisualFromFBConfig(
                        self.d, fbconfigs[i]).contents
                    if not visual:
                        continue
                    pict_format = glx.XRenderFindVisualFormat(
                        self.d, visual.visual).contents
                    if not pict_format:
                        continue

                    fbconfig = fbconfigs[i]
                    if pict_format.direct.alphaMask > 0:
                        break

                if not fbconfig:
                    print("No matching FB config found")
                #/* Create a colormap - only needed on some X clients, eg. IRIX */
                cmap = xlib.XCreateColormap(self.d, self.root, visual.visual,
                                            AllocNone)
                attr = xlib.XSetWindowAttributes()
                attr.colormap = cmap
                attr.background_pixmap = 0
                attr.border_pixmap = 0
                attr.border_pixel = 0
                attr.event_mask = (StructureNotifyMask | EnterWindowMask
                                   | LeaveWindowMask | ExposureMask
                                   | ButtonPressMask | ButtonReleaseMask
                                   | OwnerGrabButtonMask | KeyPressMask
                                   | KeyReleaseMask)
                attr_mask = (  #  CWBackPixmap|
                    CWColormap | CWBorderPixel | CWEventMask)
                self.window = xlib.XCreateWindow(self.d, self.root, x, y, w, h,
                                                 0, visual.depth, 1,
                                                 visual.visual, attr_mask,
                                                 byref(attr))
            else:  # normal EGL created context
                self.window = xlib.XCreateSimpleWindow(self.d, self.root, x, y,
                                                       w, h, 1, 0, 0)

            s = ctypes.create_string_buffer(b'WM_DELETE_WINDOW')
            self.WM_DELETE_WINDOW = ctypes.c_ulong(
                xlib.XInternAtom(self.d, s, 0))

            # set window title
            title = ctypes.c_char_p(self.window_title)
            title_length = ctypes.c_int(len(self.window_title))
            wm_name_atom = ctypes.c_ulong(
                xlib.XInternAtom(self.d,
                                 ctypes.create_string_buffer(b'WM_NAME'), 0))
            string_atom = ctypes.c_ulong(
                xlib.XInternAtom(self.d,
                                 ctypes.create_string_buffer(b'STRING'), 0))
            xlib.XChangeProperty(self.d, self.window, wm_name_atom,
                                 string_atom, 8, xlib.PropModeReplace, title,
                                 title_length)

            if (w == self.width
                    and h == self.height) or (self.display_config
                                              & DISPLAY_CONFIG_FULLSCREEN):
                # set full-screen. Messy c function calls!
                wm_state = ctypes.c_ulong(
                    xlib.XInternAtom(self.d, b'_NET_WM_STATE', 0))
                fullscreen = ctypes.c_ulong(
                    xlib.XInternAtom(self.d, b'_NET_WM_STATE_FULLSCREEN', 0))
                fullscreen = ctypes.cast(ctypes.pointer(fullscreen),
                                         ctypes.c_char_p)
                XA_ATOM = 4
                xlib.XChangeProperty(self.d, self.window, wm_state, XA_ATOM,
                                     32, xlib.PropModeReplace, fullscreen, 1)

            self.width, self.height = w, h

            if self.display_config & DISPLAY_CONFIG_HIDE_CURSOR:
                black = xlib.XColor()
                black.red = 0
                black.green = 0
                black.blue = 0
                noData = ctypes.c_char_p(bytes([0, 0, 0, 0, 0, 0, 0, 0]))
                bitmapNoData = xlib.XCreateBitmapFromData(
                    self.d, self.window, noData, 8, 8)
                invisibleCursor = xlib.XCreatePixmapCursor(
                    self.d, bitmapNoData, bitmapNoData, black, black, 0, 0)
                xlib.XDefineCursor(self.d, self.window, invisibleCursor)

            #TODO add functions to xlib for these window manager libx11 functions
            #self.window.set_wm_name('pi3d xlib window')
            #self.window.set_wm_icon_name('pi3d')
            #self.window.set_wm_class('draw', 'XlibExample')

            xlib.XSetWMProtocols(self.d, self.window, self.WM_DELETE_WINDOW, 1)
            #self.window.set_wm_hints(flags = Xutil.StateHint,
            #                         initial_state = Xutil.NormalState)

            #self.window.set_wm_normal_hints(flags = (Xutil.PPosition | Xutil.PSize
            #                                         | Xutil.PMinSize),
            #                                min_width = 20,
            #                                min_height = 20)

            xlib.XSelectInput(self.d, self.window,
                              KeyPressMask | KeyReleaseMask)
            xlib.XMapWindow(self.d, self.window)
            #xlib.XMoveWindow(self.d, self.window, x, y) #TODO this has to happen later. Works after rendering first frame. Check when
            if self.use_glx:
                dummy = c_int()
                if not glx.glXQueryExtension(self.d, byref(dummy),
                                             byref(dummy)):
                    print("OpenGL not supported by X server\n")
                dummy_glx_context = ctypes.cast(0, glx.GLXContext)
                self.render_context = glx.glXCreateNewContext(
                    self.d, fbconfig, glx.GLX_RGBA_TYPE, dummy_glx_context,
                    True)
                if not self.render_context:
                    print("Failed to create a GL context\n")
                if not glx.glXMakeContextCurrent(
                        self.d, self.window, self.window, self.render_context):
                    print("glXMakeCurrent failed for window\n")
            else:
                self.surface = openegl.eglCreateWindowSurface(
                    self.display, self.config, self.window, 0)

        if not self.use_glx:
            assert self.surface != EGL_NO_SURFACE and self.surface is not None
            r = openegl.eglMakeCurrent(self.display, self.surface,
                                       self.surface, self.context)
            assert r

        #Create viewport
        opengles.glViewport(GLint(0), GLint(0), GLsizei(w), GLsizei(h))
Beispiel #33
0
def set_pyverdict(queue_handle, packet_id, verdict, buffer_len, buffer):
    set_verdict(queue_handle, packet_id, verdict, buffer_len,
                ctypes.c_char_p(buffer))
Beispiel #34
0
 def genConnectivitySig(self, tdiCsvFile, signatureFileName, fiberFile):
     if self.CoreModule is None:
         return
     res = self.CoreModule.genConnectivitySig(c_char_p(tdiCsvFile),
                                              c_char_p(signatureFileName),
                                              c_char_p(fiberFile))
Beispiel #35
0
 def notify_peer_disconnected(self, peer_id):
     """An existing peer was dropped"""
     self._notify(
         "consensus_notifier_notify_peer_disconnected",
         ctypes.c_char_p(peer_id.encode()))
Beispiel #36
0
 def notify_peer_connected(self, peer_id):
     """A new peer was added"""
     self._notify(
         "consensus_notifier_notify_peer_connected",
         ctypes.c_char_p(peer_id.encode()))
Beispiel #37
0
 def notify_block_invalid(self, block_id):
     """This block cannot be committed successfully"""
     self._notify(
         "consensus_notifier_notify_block_invalid",
         ctypes.c_char_p(block_id.encode()))
Beispiel #38
0
def read_polar(radar_file, moment="ZH", physic_value=False,
               masked_array=False, verbose=False):
    """
    Reads a METRANET polar data file

    Parameters
    ----------
    radar_file : str
        file name
    moment : str
        moment name
    physic_value : boolean
        If true returns the physical value. Otherwise the digital value
    masked_array : boolean
        If true returns a numpy masked array with NaN values masked. Otherwise
        returns a regular masked array with NaN values
    verbose : boolean
        If true prints out extra information

    Returns
    -------
    ret_data : RadarData object
        An object containing the information read from the file. None if
        the file has not been properly read

    """
    c_speed = 299792458  # m/s

    ret_data = RadarData(moment=moment)
    prd_header = {}
    prd_data_level = ret_data.scale

    # uppercase moment
    moment = moment.upper()

    # as big as possible
    max_bins = 3000
    max_azimuths = 500

    if verbose:
        print("Read POLAR file %s " % radar_file)

        # read BINARY data
    prdt_size = max_bins * max_azimuths
    if moment == 'PHI':
        prdt_size *= 2
        prd_data = np.zeros(prdt_size, np.ushort)
    else:
        prd_data = np.zeros(prdt_size, np.ubyte)

    bfile = os.path.basename(radar_file)
    if (bfile.startswith('MS') or bfile.startswith('MH') or
            bfile.startswith('ML')):
        momentms = True
        Header_stru = Header_struPM
    else:
        momentms = False
        Header_stru = Header_struPM

    t_pol_header = (Header_stru * max_azimuths)()
    t_rad_header = (Header_struMS * 1)()

    metranet_lib = get_library(momentms=momentms, verbose=verbose)

    ret = metranet_lib.py_decoder_p2(
        ctypes.c_char_p(radar_file.encode('utf-8')),
        np.ctypeslib.as_ctypes(prd_data), ctypes.c_int(prdt_size),
        ctypes.c_char_p(moment.encode('utf-8')), ctypes.byref(t_pol_header),
        ctypes.byref(t_rad_header), ctypes.c_int(verbose))

    if ret <= max_azimuths:
        return None

    if moment == 'PHI':
        ret *= 0.5

    # reshape matrix data
    bins = t_pol_header[0].num_gates
    nr_az = int(ret / bins)
    if bins < 1:
        # if num_gates is less than 1 (exception)
        bins = ret/360
    if nr_az > 360:
        nr_az = 360

    if nr_az < 360:
        print("WARNING incomplete sweep")

    if verbose:
        print("ret=%d" % ret)
        print("bins=%d" % bins)
        print("nr_az=%d" % nr_az)
        print("len(prd_data)=%d" % len(prd_data))
    prd_data = prd_data[0: nr_az * bins]
    prd_data = np.reshape(prd_data, (nr_az, bins))

    # reorder pol_header
    pol_header = (Header_stru * nr_az)()
    for i in range(0, nr_az):
        angle_start = Selex_Angle(t_pol_header[i].start_angle)
        pol_header[int(angle_start.az)] = t_pol_header[i]

    # Select scale
    prd_data_level = float_mapping(moment, pol_header[0].data_time,
                                   pol_header[0].scan_id,
                                   pol_header[0].ny_quest)


    if verbose:
        print("prd_data shape ", prd_data.shape)
        print("min/max prd_data: ", prd_data.min(), prd_data.max())
        set(string.printable)

        print("prd_data scan_id ", pol_header[0].scan_id)
        print("prd_data host_id ", pol_header[0].host_id)
        print("data level ", prd_data_level[0:10])
        for i in range(0, nr_az, 10):
            angle_start = Selex_Angle(pol_header[i].start_angle)
            angle_end = Selex_Angle(pol_header[i].end_angle)
            x = pol_header[i].num_gates / 2
            print(("pol_header[%3d].num_gates: %d, time %d.%03d start_az/el:"
                   " %6.1f/%4.1f  end_az/el: %6.1f/%4.1f real[%d]=%6.2f"
                   " ( raw=%5d)") %
                  (i, pol_header[i].num_gates, pol_header[i].data_time,
                   pol_header[i].data_time_residue, angle_start.az,
                   angle_start.el, angle_end.az, angle_end.el, x,
                   prd_data_level[prd_data[i, x]], prd_data[i, x]))

    if physic_value:
        ret_data.data = prd_data_level[prd_data]
        if masked_array:
            ret_data.data = np.ma.array(
                ret_data.data, mask=np.isnan(ret_data.data))
            if bfile[1] == 'L':
                if moment in ('ZH', 'ZV', 'ZHC', 'ZVC'):
                    prd_data_level[1] = np.nan
                    ret_data.data = np.ma.masked_where(
                        prd_data == 1, ret_data.data)
    else:
        ret_data.data = prd_data
        if masked_array:
            ret_data.data = np.ma.array(
                ret_data.data, mask=prd_data == 0)
            if bfile[1] == 'L':
                if moment in ('ZH', 'ZV', 'ZHC', 'ZVC'):
                    prd_data_level[1] = np.nan
                    ret_data.data = np.ma.masked_where(
                        prd_data == 1, ret_data.data)

    # header
    prd_header['pid'] = bfile[0:3]
    prd_header['radar'] = bfile[2]
    prd_header['moment'] = moment
    prd_header['column'] = bins
    prd_header['row'] = nr_az
    # prd_header['elevation'] = "%03d" % pol_header[0].current_sweep
    prd_header['volume_time'] = int(
        pol_header[0].data_time - pol_header[0].data_time % 300 + 300)
    prd_header['time'] = time.strftime(
        '%y%j%H%M', time.gmtime(int(prd_header['volume_time'])))
    prd_header["quality"] = bfile[12]

    # if exists extended header, fill header
    if t_rad_header[0].FileId > 0 and t_rad_header[0].GateWidth > 0.:
        prd_header["FileId"] = t_rad_header[0].FileId
        prd_header["Version"] = t_rad_header[0].Version
        # prd_header["Spare1"] = t_rad_header[0].Spare1
        # prd_header["Length"] = t_rad_header[0].Length
        prd_header["RadarName"] = ctypes.string_at(
            t_rad_header[0].RadarName)
        prd_header["ScanName"] = ctypes.string_at(t_rad_header[0].ScanName)
        prd_header["RadarLat"] = t_rad_header[0].RadarLat
        prd_header["RadarLon"] = t_rad_header[0].RadarLon
        prd_header["RadarHeight"] = t_rad_header[0].RadarHeight
        prd_header["SequenceSweep"] = t_rad_header[0].SequenceSweep
        prd_header["CurrentSweep"] = t_rad_header[0].CurrentSweep
        prd_header["TotalSweep"] = t_rad_header[0].TotalSweep
        prd_header["AntMode"] = t_rad_header[0].AntMode
        prd_header["Priority"] = t_rad_header[0].Priority
        # prd_header["Quality"] = t_rad_header[0].Quality
        prd_header["quality"] = t_rad_header[0].Quality
        # prd_header["Spare2"] = t_rad_header[0].Spare2
        prd_header["RepeatTime"] = t_rad_header[0].RepeatTime
        prd_header["NumMoments"] = t_rad_header[0].NumMoments
        prd_header["GateWidth"] = t_rad_header[0].GateWidth
        prd_header["WaveLength"] = t_rad_header[0].WaveLength
        prd_header["Frequency"] = c_speed/(prd_header["WaveLength"]*1e-2)
        prd_header["PulseWidth"] = t_rad_header[0].PulseWidth
        prd_header["StartRange"] = t_rad_header[0].StartRange
        prd_header["MetaDataSize"] = t_rad_header[0].MetaDataSize
        # update with radar sweep radar
        prd_header['radar'] = prd_header["RadarName"][0]
    else:
        # specific radar metadata not available (old PM/PH/PL format)
        # from polar header
        prd_header["CurrentSweep"] = pol_header[0].current_sweep
        prd_header["TotalSweep"] = pol_header[0].total_sweep
        prd_header["AntMode"] = pol_header[0].ant_mode
        prd_header["RepeatTime"] = pol_header[0].repeat_time
        prd_header["GateWidth"] = pol_header[0].gate_width
        prd_header["StartRange"] = pol_header[0].start_range
        prd_header["Priority"] = pol_header[0].priority

        # from hard-coded table
        rname = prd_header['radar']
        radar_def = get_radar_site_info()
        if rname in radar_def.keys():
            prd_header["FileId"] = radar_def[rname]["FileId"]
            prd_header["Version"] = radar_def[rname]["Version"]
            prd_header["RadarName"] = radar_def[rname]["RadarName"]
            prd_header["ScanName"] = radar_def[rname]["ScanName"]
            prd_header["RadarLat"] = radar_def[rname]["RadarLat"]
            prd_header["RadarLon"] = radar_def[rname]["RadarLon"]
            prd_header["RadarHeight"] = radar_def[rname]["RadarHeight"]
            prd_header["Frequency"] = radar_def[rname]["Frequency"]
            prd_header["WaveLength"] = radar_def[rname]["WaveLength"]
            prd_header["PulseWidth"] = radar_def[rname]["PulseWidth"]
            prd_header["SequenceSweep"] = (
                radar_def[rname]['SweepsOrder'].index(
                    pol_header[0].current_sweep))

            if bfile[1] == 'M':
                prd_header["NumMoments"] = radar_def[rname]['NumMomentsPM']
            if bfile[1] == 'H':
                prd_header["NumMoments"] = radar_def[rname]['NumMomentsPH']
            if bfile[1] == 'L':
                prd_header["NumMoments"] = radar_def[rname]['NumMomentsPL']

    ret_data.header = prd_header
    ret_data.scale = prd_data_level
    ret_data.pol_header = pol_header

    # change parameters in header
    for i in range(nr_az):
        ret_data.pol_header[i].total_record = nr_az

    return ret_data
 def onError(fmt, ap):
     msg = ctypes.c_char_p()
     ret = lib.lw_vasprintf(ctypes.byref(msg), fmt, ap)
     SextanteLog.addToLog(SextanteLog.LOG_ERROR, u"FAILURE: liblwgeom error is:\n%s" % msg.value)
Beispiel #40
0
def read_product(radar_file, physic_value=False, masked_array=False,
                 verbose=False):
    """
    Reads a METRANET cartesian data file

    Parameters
    ----------
    radar_file : str
        file name
    physic_value : boolean
        If true returns the physical value. Otherwise the digital value
    masked_array : boolean
        If true returns a numpy masked array with NaN values masked. Otherwise
        returns a regular masked array with NaN values
    verbose : boolean
        If true prints out extra information

    Returns
    -------
    ret_data : RadarData object
        An object containing the information read from the file. None if
        the file has not been properly read

    """
    ret_data = RadarData()
    prd_header = {'row': 0, 'column': 0}

    # read ASCII data
    if verbose:
        print("physic_value: ", physic_value)
        print("File %s: read ASCII" % radar_file)

    try:
        with open(radar_file, 'rb') as data_file:
            lines = data_file.readlines()
    except OSError as ee:
        warn(str(ee))
        print("Unable to read file '%s'" % radar_file)
        return None

    for t_line in lines:
        line = t_line.decode("utf-8").strip('\n')
        if line.find('end_header') == -1:
            data = line.split('=')
            prd_header[data[0]] = data[1]
        else:
            break

    # read BINARY data
    prdt_size = int(prd_header['column']) * int(prd_header['row'])
    if prdt_size < 1:
        print("Error, no size found row=%3d column=%3d" %
              (prd_header['row'], prd_header['column']))
        return None

    if verbose:
        print("File %s: read BINARY data: expected %s bytes, " %
              (radar_file, prdt_size), end='')

    prd_data = np.zeros(
        [int(prd_header['row']), int(prd_header['column'])], np.ubyte)
    prd_data_level = np.zeros(256, np.float32)
    metranet_lib = get_library(verbose=verbose)

    ret = metranet_lib.py_decoder(
        ctypes.c_char_p(radar_file.encode('utf-8')),
        np.ctypeslib.as_ctypes(prd_data), ctypes.c_int(prdt_size),
        np.ctypeslib.as_ctypes(prd_data_level), ctypes.c_int(verbose))

    # convert 0 at end of array with NAN
    conv_zero2nan = True

    i = len(prd_data_level)
    if prd_data_level.max() == prd_data_level.min():
        prd_data_level = np.fromiter(xrange(256), dtype=np.uint32)
    else:
        while conv_zero2nan:
            i -= 1
            if i < 0:
                conv_zero2nan = False
            elif prd_data_level[i] == 0.0:
                prd_data_level[i] = np.nan
            else:
                conv_zero2nan = False

    if verbose:
        print("Found %d bytes" % ret)
        print("prd_data_level[10] = %f" % prd_data_level[10])
        print("min/max prd_data: %d/%d" % (prd_data.min(), prd_data.max()))
        print("first 100 bytes", prd_data[0:100, 0])
        print("data level ", prd_data_level[0:10])

    # ret_data = RadarData(
    #    data=prd_data, header=prd_header, scale=prd_data_level)
    if physic_value:
        ret_data.data = prd_data_level[prd_data]
        if masked_array:
            ret_data.data = np.ma.array(
                ret_data.data, mask=np.isnan(ret_data.data))
            ret_data.data = np.ma.masked_where(prd_data == 0, ret_data.data)
    else:
        ret_data.data = prd_data
        if masked_array:
            ret_data.data = np.ma.array(
                ret_data.data, mask=prd_data == 0)
    ret_data.header = prd_header
    ret_data.scale = prd_data_level

    return ret_data
Beispiel #41
0
 def _set_clipboard_text(self, text):
     SDL_SetClipboardText(ctypes.c_char_p(text.encode()))
Beispiel #42
0
    def init(self):
      if self.restoreInfo() == 0:
          raise mdsExceptions.TclFAILED_ESSENTIAL

      self.saveInfo()

      try:
        self.frames.setCompressOnPut(False)
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot disable automatic compresson on put for frames node')
        raise mdsExceptions.TclFAILED_ESSENTIAL

      try:
        self.frames_metad.setCompressOnPut(False)
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot disable automatic compresson on put for frames_metad node')
        raise mdsExceptions.TclFAILED_ESSENTIAL

###Object Parameters

      try:
        o_refl_temp = c_double(self.object_refl_temp.data())
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid value for object refletive temperature')
        raise mdsExceptions.TclFAILED_ESSENTIAL
      try:
        o_atm_temp = c_double(self.object_atm_temp.data())
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid value for object atmosfere temperature')
        raise mdsExceptions.TclFAILED_ESSENTIAL
      try:
        o_distance = c_double(self.object_distance.data())
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid value for object distance')
        raise mdsExceptions.TclFAILED_ESSENTIAL
      try:
        o_emissivity = c_double(self.object_emissivity.data())
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid value for object emissivity')
        raise mdsExceptions.TclFAILED_ESSENTIAL
      try:
        o_atm_hum = c_double(self.object_atm_hum.data())
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid value for object atmosfere humidity')
        raise mdsExceptions.TclFAILED_ESSENTIAL
      try:
        o_optic_temp = c_double(self.object_optic_temp.data())
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid value for object optic temperature')
        raise mdsExceptions.TclFAILED_ESSENTIAL
      try:
        o_optic_trans = c_double(self.object_optic_trans.data())
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid value for object optic transmission')
        raise mdsExceptions.TclFAILED_ESSENTIAL
      try:
        o_atm_trans = c_double(self.object_atm_trans.data())
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid value for object atmosfere trasmission')
        raise mdsExceptions.TclFAILED_ESSENTIAL


      status = FLIRSC65X.flirLib.setObjectParameters(self.handle, o_refl_temp, o_atm_temp, o_distance, o_emissivity, o_atm_hum , o_optic_temp, o_optic_trans, o_atm_trans )
      if status < 0:
        FLIRSC65X.flirLib.getLastError(self.handle, self.error)
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot Set Object Parameters : ' + self.error.raw)
        raise mdsExceptions.TclFAILED_ESSENTIAL

###Frame Rate
      try:
         frameRate = self.timing_frame_rate.data()
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid frame rate value')
        raise mdsExceptions.TclFAILED_ESSENTIAL
      status = FLIRSC65X.flirLib.setFrameRateNew(self.handle, c_double(frameRate))
      if status < 0:
        FLIRSC65X.flirLib.getLastError(self.handle, self.error)
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot Set Frame Rate : ' + self.error.raw)
        raise mdsExceptions.TclFAILED_ESSENTIAL

###Frame Area
      x=c_int(0)
      y=c_int(0)
      width=c_int(0)
      height=c_int(0)
      status = FLIRSC65X.flirLib.getReadoutArea(self.handle, byref(x), byref(y), byref(width), byref(height))
      if status < 0:
        FLIRSC65X.flirLib.getLastError(self.handle, self.error)
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot Get Readout Area : ' + self.error.raw)
        raise mdsExceptions.TclFAILED_ESSENTIAL

      #write data in mdsplus
      self.frame_x.putData(x.value)
      self.frame_y.putData(y.value)
      self.frame_width.putData(width.value)
      self.frame_height.putData(height.value)

###Focal Length
      try:
         focalLength = self.cam_setup_focal_length.data()
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid Focal Length value')
        raise mdsExceptions.TclFAILED_ESSENTIAL

      if focalLength == '25':
        focalLengthInt=0
      elif focalLength == '41':
        focalLengthInt=3         #offset to select the correct calibration curve using the Measurement Range

###Measurement Range
      try:
         measureRange = self.cam_setup_meas_range.data()
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid measurement range value')
        raise mdsExceptions.TclFAILED_ESSENTIAL

      if measureRange == '-40...150':
        measRangeInt=0
      elif measureRange == '100...650':
        measRangeInt=1
      elif measureRange == '300...2000':
        measRangeInt=2

      status = FLIRSC65X.flirLib.setMeasurementRange(self.handle, c_int(measRangeInt+focalLengthInt))
      if status < 0:
        try:
          FLIRSC65X.flirLib.getLastError(self.handle, self.error)
          Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot Set Measurement Range : ' + self.error.raw)
          raise mdsExceptions.TclFAILED_ESSENTIAL
        except:
          traceback.print_exc()

###Image Temperature
      try:
        frameTempUnit = self.frame_temp_unit.data()
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid image temperature unit (Radiometric, 10mk, 100mk) value')
        raise mdsExceptions.TclFAILED_ESSENTIAL

      if frameTempUnit == 'Radiometric':
        frameTempUnitCode=c_int(0)
      elif frameTempUnit == 'LinearTemperature10mK':
        frameTempUnitCode=c_int(1)
      elif frameTempUnit == 'LinearTemperature100mK':
        frameTempUnitCode=c_int(2)

      status = FLIRSC65X.flirLib.setIrFormat(self.handle, frameTempUnitCode)
      if status < 0:
        FLIRSC65X.flirLib.getLastError(self.handle, self.error)
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot Set Image Temperature unit : ' + self.error.raw)
        raise mdsExceptions.TclFAILED_ESSENTIAL

###Frame Trigger mode
      try:
        burstDuration = self.timing_burst_dur.data()
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid acquisition duration value')
        raise mdsExceptions.TclFAILED_ESSENTIAL

      try:
        triggerMode = self.timing_trig_mode.data()
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid trigger mode value')
        raise mdsExceptions.TclFAILED_ESSENTIAL

      try:
        trigSource = self.timing_trig_source.data()
      except:
        if triggerMode == 'EXTERNAL':
           Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid trigger source value')
           raise mdsExceptions.TclFAILED_ESSENTIAL
        else:
           trigSource = array([0.])

      print("OK " + triggerMode )
      if triggerMode == 'EXTERNAL':   #0=internal  1=external trigger
        trigModeCode=c_int(1)
      else:
        trigSource = array([0.])
        trigModeCode=c_int(0)

      numTrigger = trigSource.size
      print("OK - NUM TRIGGER ", numTrigger)
      print("OK - Trigger Source ", trigSource)


      timeBase = Data.compile(" $ : $ + $ :(zero( size( $ ), 0.) + 1.) * 1./$", trigSource, trigSource, burstDuration, trigSource, frameRate)

      print("Data = " + Data.decompile(timeBase))

      self.timing_time_base.putData(timeBase)
      status = FLIRSC65X.flirLib.setTriggerMode(self.handle, trigModeCode, c_double(burstDuration), numTrigger)

      if status < 0:
        FLIRSC65X.flirLib.getLastError(self.handle, self.error)
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot Set Internal/External Trigger : ' + self.error.raw)
        raise mdsExceptions.TclFAILED_ESSENTIAL

###Calibration
      try:
        calibAuto = self.cam_setup_calib_auto.data()
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid auto calibration setup')
        raise mdsExceptions.TclFAILED_ESSENTIAL

      calibModeCode = c_int(1)
      if calibAuto == 'NO':
        try:
           calibTime = self.cam_setup_calib_time.data()
           calibModeCode = c_int(0)
        except:
           Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid calibration duration value')
           raise mdsExceptions.TclFAILED_ESSENTIAL
        if numTrigger > 1 and (burstDuration + calibTime) > (trigSource[1] - trigSource[0]) :
           Data.execute('DevLogErr($1,$2)', self.nid, 'Calibration executed during acquisition')
           raise mdsExceptions.TclFAILED_ESSENTIAL

      status = FLIRSC65X.flirLib.setCalibMode(self.handle, calibModeCode)
      if status < 0:
        FLIRSC65X.flirLib.getLastError(self.handle, self.error)
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot Set Internal/External Trigger : ' + self.error.raw)
        raise mdsExceptions.TclFAILED_ESSENTIAL

###Streaming
      try:
        streamingMode = self.streaming_mode.data()
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid streaming mode setup')
        raise mdsExceptions.TclFAILED_ESSENTIAL


      if streamingMode == 'Stream and Store':
          streamingEnabled = c_int(1)
          storeEnabled = c_int(1)
      elif streamingMode == 'Only Stream':
          streamingEnabled = c_int(1)
          storeEnabled = c_int(0)
      else: #streamingMode == 'Only Store':
          streamingEnabled = c_int(0)
          storeEnabled = c_int(1)


      if streamingEnabled :
          try:
             if self.streaming_autoscale.data() == 'YES' :
                  autoAdjustLimit = c_int(1)
             else:
                  autoAdjustLimit = c_int(0)
          except:
             Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid streaming autoscale parameter value')
             raise mdsExceptions.TclFAILED_ESSENTIAL

          try:
             lowLim = c_int(self.streaming_lolim.data())
          except:
             Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid streaming low temperature limit parameter value')
             raise mdsExceptions.TclFAILED_ESSENTIAL

          try:
             highLim = c_int(self.streaming_hilim.data())
          except:
             Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid streaming high temperature limit parameter value')
             raise mdsExceptions.TclFAILED_ESSENTIAL

          try:
             streamingPort = c_int(self.streaming_port.data())
          except:
             Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid streaming port parameter value')
             raise mdsExceptions.TclFAILED_ESSENTIAL

          try:
             streamingServer = self.streaming_server.data()
          except:
             Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid streaming server parameter value')
             raise mdsExceptions.TclFAILED_ESSENTIAL

#fede 20161012
#      else:
#          autoAdjustLimit = c_int(0)
#          streamingPort = c_int(8888)
#          lowLim = c_int(0)
#          highLim = c_int(36)
#          streamingServer = "localhost"

          print("lowLim ", lowLim)
          print("highLim ", highLim)
          print("frameTempUnitCode ", frameTempUnitCode)
          print("streamingPort ", streamingPort)
          print("streamingServer ", streamingServer)
          deviceName = str(self).rsplit(":",1)
          deviceName = deviceName[1]
          print("Device Name ", deviceName)     
      
#fede: recover device name and pass it to set streaming to overlay text on frame!!!

          status = FLIRSC65X.flirLib.setStreamingMode(self.handle, frameTempUnitCode, streamingEnabled,  autoAdjustLimit, c_char_p(streamingServer), streamingPort,  lowLim,  highLim, c_char_p(deviceName));
          if status < 0:
            FLIRSC65X.flirLib.getLastError(self.handle, self.error)
            Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot execute streaming setup mode : ' + self.error.raw)
            raise mdsExceptions.TclFAILED_ESSENTIAL


###Acquisition


      try:
        acqSkipFrameNumber = c_int( self.timing_skip_frame.data() )
      except:
        Data.execute('DevLogErr($1,$2)', self.nid, 'Invalid acquisition decimation value')
        raise mdsExceptions.TclFAILED_ESSENTIAL

      status = FLIRSC65X.flirLib.setAcquisitionMode(self.handle, storeEnabled , acqSkipFrameNumber)
      if status < 0:
        FLIRSC65X.flirLib.getLastError(self.handle, self.error)
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot execute acquisition setup mode : ' + self.error.raw)
        raise mdsExceptions.TclFAILED_ESSENTIAL

      try:
       treePtr = c_void_p(0)
       status = FLIRSC65X.mdsLib.camOpenTree(c_char_p(self.getTree().name), c_int(self.getTree().shot), byref(treePtr))
       if status == -1:
         Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot open tree')
         raise mdsExceptions.TclFAILED_ESSENTIAL
      except:
       traceback.print_exc()

      framesNid = self.frames.nid
      timebaseNid = self.timing_time_base.nid
      framesMetadNid = self.frames_metad.nid
      frame0TimeNid = self.frame0_time.nid

      status = FLIRSC65X.flirLib.setTreeInfo( self.handle,  treePtr,  framesNid,  timebaseNid,  framesMetadNid, frame0TimeNid)
      if status < 0:
        FLIRSC65X.flirLib.getLastError(self.handle, self.error)
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot execute set tree info : '+self.error.raw)
        raise mdsExceptions.TclFAILED_ESSENTIAL


###Auto Calibration
      status = FLIRSC65X.flirLib.executeAutoCalib(self.handle)
      if status < 0:
        FLIRSC65X.flirLib.getLastError(self.handle, self.error)
        Data.execute('DevLogErr($1,$2)', self.nid, 'Cannot Execute Auto Calibration : '+self.error.raw)
        raise mdsExceptions.TclFAILED_ESSENTIAL

      print('Init action completed.')
      return
Beispiel #43
0
def inflateInit2(strm, windowBits):
    return _zlib.inflateInit2_(ctypes.addressof(strm), windowBits,
                               ctypes.c_char_p(ZLIB_VERSION),
                               ctypes.sizeof(z_stream))
    def PushOK(self):
        if self.sizeLine.text=='number of words' or self.sizeLine.text()=='number of symbols' or self.countLine.text()=='' or self.numberPuttern.text()=='':
           return
        self.textCombobox.clear()
        textComboboxRes.clear()
        self.sourceText.clear()
        # self.sourceText.clear()
        # self.sourceText.clear()
        resulText.clear()
        countText = self.countLine.text()
        a = int(countText)
        for i in range(a):
            self.textCombobox.addItem("Text " + str(i + 1))
            textComboboxRes.addItem("Text " + str(i + 1))
        text_num = a
        puttern_num=0
        if self.comboBox.currentText()=="Random words":
            puttern_num=self.numberPuttern.text()
        if self.settingWordCombobox.currentText()=="Manual":
            self.pattern = self.enterWordEdit.text()
            c_pattern = ct.c_char_p(self.pattern.encode(encoding))
        else:
            c_pattern=0

        if self.comboBox.currentText() == "Text":

            list_ = []
            for i in range(text_num):
                list_.append(book_parser())
                new_list = (ct.c_char_p * text_num)(*map(str.encode, list_))
            self.text=lib.make_parser_storage(new_list, c_pattern, text_num, len)
        else:
            text_num = a
            text_type = 0
            puttern_num=0
            path=path1

            if self.comboBox.currentText() == "Full Random":
                text_type=0
                path=path1
            if self.comboBox.currentText() == "Random words":
                text_type=1
                path=path2
                puttern_num = self.numberPuttern.text()

            puttern_num=int(puttern_num)
            length = int(self.sizeLine.text())
            print(length)
            if self.settingWordCombobox.currentText() == "Generate":
                self.text = (lib.make_text_storage(text_num, text_type, path, 0, puttern_num, length))
            else:
                self.text = (lib.make_text_storage(text_num, text_type, path, c_pattern, puttern_num, length))
        textik=self.text[0].Text.contents.haystack.decode(encoding)
        self.sourceText.append(self.text[0].Text.contents.haystack.decode(encoding))
        if self.settingWordCombobox.currentText()=="Generate":
            self.enterWordEdit.clear()
            self.enterWordEdit.setStyleSheet("""font: normal; color: black""")
            self.enterWordEdit.setText(self.text[0].Pattern.contents.needle.decode(encoding))
        numAlg=1
        if self.algorithmCombobox.currentText()=="Boyer Moor Horspool":
            numAlg=0
        elif self.algorithmCombobox.currentText()=="Naive Matcher":
            numAlg=1
        elif self.algorithmCombobox.currentText()=="Rabin Karp":
            numAlg=2
        elif self.algorithmCombobox.currentText()=="KMP":
            numAlg=3

        self.algorithm_res = lib.make_result_storage(self.text, numAlg, text_num)
        numPatrern=0
        i=0
        printRes=''
        numPattern = 0
        printRes = ''

        text = self.text[0].Text.contents.haystack.decode(encoding)
        i = 0
        if self.comboBox.currentText() == "Random words":
            if self.algorithm_res[0].numberOfMatches != 0:
                while i < len(text):
                    if i == self.algorithm_res[0].matchedShifts[numPattern]:
                        for j in range(i,  len(self.text[0].Pattern.contents.needle.decode(encoding))+i):
                            redText = "<span style=\" font: bold; color: #ffa500;\">"
                            redText += (text[j])

                            printRes += (redText)
                        numPattern += 1
                        i += len(self.text[0].Pattern.contents.needle.decode(encoding))
                    else:
                        blackText = "<span style=\" font: normal; color: black;\">"
                        blackText += text[i]
                        printRes += (blackText)
                        i += 1
                resulText.append(printRes)
            else:
                resulText.append(self.text[0].Text.contents.haystack.decode(encoding))
        else:
            if self.algorithm_res[0].numberOfMatches != 0:
                while i < len(text):
                    if i == self.algorithm_res[0].matchedShifts[numPattern]:
                        if i+1==self.algorithm_res[0].matchedShifts[numPattern+1]:
                            redText = "<span style=\" font: bold; color: #ffa500;\">"
                            redText += (text[i])
                            printRes += (redText)
                            i+=1
                        else:
                            for j in range(i, i + len(self.text[0].Pattern.contents.needle.decode(encoding))):
                                redText = "<span style=\" font: bold; color: #ffa500;\">"
                                redText += (text[j])
                                printRes += (redText)
                            i+=len(self.text[0].Pattern.contents.needle.decode(encoding))
                        numPattern += 1
                    else:
                        blackText = "<span style=\" font: normal; color: black;\">"
                        blackText += text[i]
                        printRes += (blackText)
                        i += 1
                resulText.append(printRes)
            else:
                resulText.append(self.text[0].Text.contents.haystack.decode(encoding))

        statictic = lib.make_statictic(self.algorithm_res, text_num)

        numberOfMatches.setText("Number of matches: " + str(self.algorithm_res[0].numberOfMatches))
        numOfComparises.setText("Number of comparisons: " + str(self.algorithm_res[0].numOfCompares))
        numOfExtraOps.setText("Number of extra operations: " + str(self.algorithm_res[0].numOfExtraOps))
        workTime.setText("Work time: " + str(self.algorithm_res[0].workTime))
        memoryWaste.setText("Memory waste: " + str(self.algorithm_res[0].memoryWaste))
        genPattern.setText("Pattern: "+ self.enterWordEdit.text())
        genNumOfComparises.setText("Number of comparisons: " + str(statictic.contents.numOfCompares))
        genNumOfExtraOps.setText("Number of extra operations: " + str(statictic.contents.numOfExtraOps))
        genWorkTime.setText("Work time: " + str(statictic.contents.workTime))
        genMemoryWaste.setText("Memory waste: " + str(statictic.contents.memoryWaste))
        self.textCombobox.currentTextChanged.connect(self.ChooseText)
        textComboboxRes.currentTextChanged.connect(self.ChooseTextRes)
Beispiel #45
0
def inflateInit(strm):
    return _zlib.inflateInit_(ctypes.addressof(strm),
                              ctypes.c_char_p(ZLIB_VERSION),
                              ctypes.sizeof(z_stream))
Beispiel #46
0
def deflateInit(strm, level):
    return _zlib.deflateInit_(ctypes.addressof(strm), level,
                              ctypes.c_char_p(ZLIB_VERSION),
                              ctypes.sizeof(z_stream))
Beispiel #47
0
    def make_rdm4(self,
                  state,
                  norb,
                  nelec,
                  dt=numpy.dtype('Float64'),
                  filetype="binary",
                  link_index=None,
                  **kwargs):
        import os

        if self.has_fourpdm == False:
            self.twopdm = False
            self.threepdm = False
            self.extraline.append('threepdm\n')
            self.extraline.append('fourpdm\n')

            writeDMRGConfFile(self, nelec, False)
            if self.verbose >= logger.DEBUG1:
                inFile = self.configFile
                #inFile = os.path.join(self.scratchDirectory,self.configFile)
                logger.debug1(self, 'Block Input conf')
                logger.debug1(self, open(inFile, 'r').read())
            executeBLOCK(self)
            if self.verbose >= logger.DEBUG1:
                outFile = self.outputFile
                #outFile = os.path.join(self.scratchDirectory,self.outputFile)
                logger.debug1(self, open(outFile).read())
            self.has_fourpdm = True
            self.has_threepdm = True
            self.extraline.pop()

        # The binary files coming from STACKBLOCK and BLOCK are different:
        # - STACKBLOCK does not have 4RDM
        #   If it had, it would probably come in a 8-fold symmetr which must unpacked
        #   using "libunpack.unpackE4" (see lib/icmpspt/icmpspt.c)
        # - BLOCK just writes a list of all values, this is directly read
        #   using "unpackE4_BLOCK" (see below)
        if (filetype == "binary"):
            fname = os.path.join(self.scratchDirectory, "node0",
                                 "spatial_fourpdm.%d.%d.bin" % (state, state))
            if 'stackblock' in settings.BLOCKEXE:
                print 'Reading binary 4RDM from STACKBLOCK'
                fnameout = os.path.join(
                    self.scratchDirectory, "node0",
                    "spatial_fourpdm.%d.%d.bin.unpack" % (state, state))
                libunpack.unpackE4(ctypes.c_char_p(fname),
                                   ctypes.c_char_p(fnameout),
                                   ctypes.c_int(norb))
                E4 = numpy.fromfile(fnameout, dtype=numpy.dtype('Float64'))
                E4 = numpy.reshape(
                    E4, (norb, norb, norb, norb, norb, norb, norb, norb),
                    order='F')
            else:
                print 'Reading binary 4RDM from BLOCK'
                E4 = DMRGCI.unpackE4_BLOCK(self, fname, norb)

        # The 4RDMs written by "Fourpdm_container::save_spatial_npdm_text" in BLOCK and STACKBLOCK
        # are written as E4[i1,j2,k3,l4,m4,n3,o2,p1]
        # and are stored here as E4[i1,j2,k3,l4,p1,o2,n3,m4]
        # This is done with SQA in mind.
        else:
            print 'Reading text-file 4RDM'
            fname = os.path.join(self.scratchDirectory, "node0",
                                 "spatial_fourpdm.%d.%d.txt" % (state, state))
            f = open(fname, 'r')
            lines = f.readlines()
            E4 = numpy.zeros(shape=(norb, norb, norb, norb, norb, norb, norb,
                                    norb),
                             dtype=dt,
                             order='F')
            assert (int(lines[0]) == norb)
            for line in lines[1:]:
                linesp = line.split()
                if (len(linesp) != 9):
                    continue
                a, b, c, d, e, f, g, h, integral = int(linesp[0]), int(
                    linesp[1]), int(linesp[2]), int(linesp[3]), int(
                        linesp[4]), int(linesp[5]), int(linesp[6]), int(
                            linesp[7]), float(linesp[8])
                if (False):
                    up_indexes = [a, b, c, d]
                    dn_indexes = [h, g, f, e]
                    for i in range(4):
                        for j in range(4):
                            if (i == j):
                                continue
                            for k in range(4):
                                if ((i == k) or (j == k)):
                                    continue
                                for l in range(4):
                                    if ((i == l) or (j == l) or (k == l)):
                                        continue
                                    E4[up_indexes[i],up_indexes[j],up_indexes[k],up_indexes[l],\
                                       dn_indexes[i],dn_indexes[j],dn_indexes[k],dn_indexes[l]] = integral
                else:
                    self.populate(E4, [a, b, c, d, h, g, f, e], integral)
        print ''
        return E4
Beispiel #48
0
def deflateInit2(strm, level, method, windowBits, memLevel, strategy):
    return _zlib.deflateInit2_(ctypes.addressof(strm), level, method,
                               windowBits, memLevel, strategy,
                               ctypes.c_char_p(ZLIB_VERSION),
                               ctypes.sizeof(z_stream))
Beispiel #49
0
def _make_atomic_symbol_function(handle):
    """Create an atomic symbol function by handle and funciton name."""
    name = ctypes.c_char_p()
    desc = ctypes.c_char_p()
    key_var_num_args = ctypes.c_char_p()
    num_args = mx_uint()
    arg_names = ctypes.POINTER(ctypes.c_char_p)()
    arg_types = ctypes.POINTER(ctypes.c_char_p)()
    arg_descs = ctypes.POINTER(ctypes.c_char_p)()

    check_call(
        _LIB.MXSymbolGetAtomicSymbolInfo(handle, ctypes.byref(name),
                                         ctypes.byref(desc),
                                         ctypes.byref(num_args),
                                         ctypes.byref(arg_names),
                                         ctypes.byref(arg_types),
                                         ctypes.byref(arg_descs),
                                         ctypes.byref(key_var_num_args)))
    param_str = ctypes2docstring(num_args, arg_names, arg_types, arg_descs)
    key_var_num_args = py_str(key_var_num_args.value)
    func_name = py_str(name.value)

    desc = py_str(desc.value)
    if key_var_num_args:
        desc += '\nThis function support variable length of positional input.'
    doc_str = ('%s\n\n' + '%s\n' + 'name : string, optional.\n' +
               '    Name of the resulting symbol.\n\n' + 'Returns\n' +
               '-------\n' + 'symbol: Symbol\n' + '    The result symbol.')
    doc_str = doc_str % (desc, param_str)

    def creator(*args, **kwargs):
        """Activation Operator of Neural Net.
        The parameters listed below can be passed in as keyword arguments.

        Parameters
        ----------
        name : string, required.
            Name of the resulting symbol.

        Returns
        -------
        symbol: Symbol
            the resulting symbol
        """
        param_keys = []
        param_vals = []
        symbol_kwargs = {}
        name = kwargs.pop('name', None)

        if key_var_num_args and key_var_num_args not in kwargs:
            param_keys.append(c_str(key_var_num_args))
            param_vals.append(c_str(str(len(args))))

        for k, v in kwargs.items():
            if isinstance(v, Symbol):
                symbol_kwargs[k] = v
            else:
                param_keys.append(c_str(k))
                param_vals.append(c_str(str(v)))
        # create atomic symbol
        param_keys = c_array(ctypes.c_char_p, param_keys)
        param_vals = c_array(ctypes.c_char_p, param_vals)
        sym_handle = SymbolHandle()
        check_call(
            _LIB.MXSymbolCreateAtomicSymbol(handle, mx_uint(len(param_keys)),
                                            param_keys, param_vals,
                                            ctypes.byref(sym_handle)))

        if len(args) != 0 and len(symbol_kwargs) != 0:
            raise TypeError(
                '%s can only accept input'
                'Symbols either as positional or keyword arguments, not both' %
                func_name)
        if key_var_num_args and len(symbol_kwargs) != 0:
            raise ValueError(
                'This function support variable length of Symbol arguments.\n'
                +
                'Please pass all the input Symbols via positional arguments' +
                ' instead of keyword arguments.')

        s = Symbol(sym_handle)
        hint = func_name.lower()
        name = NameManager.current.get(name, hint)
        s._compose(*args, name=name, **symbol_kwargs)
        return s

    creator.__name__ = func_name
    creator.__doc__ = doc_str
    return creator
Beispiel #50
0
    def compile(self, **options):
        """Perform Compliation

        The valid compiler options are

         *   - -g (enable generation of debugging information)
         *   - -opt=
         *     - 0 (disable optimizations)
         *     - 3 (default, enable optimizations)
         *   - -arch=
         *     - compute_20 (default)
         *     - compute_30
         *     - compute_35
         *   - -ftz=
         *     - 0 (default, preserve denormal values, when performing
         *          single-precision floating-point operations)
         *     - 1 (flush denormal values to zero, when performing
         *          single-precision floating-point operations)
         *   - -prec-sqrt=
         *     - 0 (use a faster approximation for single-precision
         *          floating-point square root)
         *     - 1 (default, use IEEE round-to-nearest mode for
         *          single-precision floating-point square root)
         *   - -prec-div=
         *     - 0 (use a faster approximation for single-precision
         *          floating-point division and reciprocals)
         *     - 1 (default, use IEEE round-to-nearest mode for
         *          single-precision floating-point division and reciprocals)
         *   - -fma=
         *     - 0 (disable FMA contraction)
         *     - 1 (default, enable FMA contraction)
         *
         """

        # stringify options
        opts = []
        if 'debug' in options:
            if options.pop('debug'):
                opts.append('-g')

        if options.get('opt'):
            opts.append('-opt=%d' % options.pop('opt'))

        if options.get('arch'):
            opts.append('-arch=%s' % options.pop('arch'))

        other_options = (
            'ftz',
            'prec_sqrt',
            'prec_div',
            'fma',
        )

        for k in other_options:
            if k in options:
                v = int(bool(options.pop(k)))
                opts.append('-%s=%d' % (k.replace('_', '-'), v))

        # If there are any option left
        if options:
            optstr = ', '.join(map(repr, options.keys()))
            raise NvvmError("unsupported option {0}".format(optstr))

        # compile
        c_opts = (c_char_p *
                  len(opts))(*[c_char_p(x.encode('utf8')) for x in opts])
        err = self.driver.nvvmCompileProgram(self._handle, len(opts), c_opts)
        self._try_error(err, 'Failed to compile\n')

        # get result
        reslen = c_size_t()
        err = self.driver.nvvmGetCompiledResultSize(self._handle,
                                                    byref(reslen))

        self._try_error(err, 'Failed to get size of compiled result.')

        ptxbuf = (c_char * reslen.value)()
        err = self.driver.nvvmGetCompiledResult(self._handle, ptxbuf)
        self._try_error(err, 'Failed to get compiled result.')

        # get log
        self.log = self.get_log()

        return ptxbuf[:]
Beispiel #51
0
def _cu_check_error(result):
    if result != CUDA_SUCCESS:
        _error_str = c_char_p()
        _cuda_lib.cuGetErrorString(result, byref(_error_str))
        raise NVError('Device API Error %d: %s' %
                      (result, _error_str.value.decode()))
Beispiel #52
0
    def make_rdm3(self,
                  state,
                  norb,
                  nelec,
                  dt=numpy.dtype('Float64'),
                  filetype="binary",
                  link_index=None,
                  **kwargs):
        import os

        if self.has_threepdm == False:
            self.twopdm = False
            self.extraline.append('threepdm\n')

            writeDMRGConfFile(self, nelec, False)
            if self.verbose >= logger.DEBUG1:
                inFile = self.configFile
                #inFile = os.path.join(self.scratchDirectory,self.configFile)
                logger.debug1(self, 'Block Input conf')
                logger.debug1(self, open(inFile, 'r').read())
            executeBLOCK(self)
            if self.verbose >= logger.DEBUG1:
                outFile = self.outputFile
                #outFile = os.path.join(self.scratchDirectory,self.outputFile)
                logger.debug1(self, open(outFile).read())
            self.has_threepdm = True
            self.extraline.pop()

        # The binary files coming from STACKBLOCK and BLOCK are different
        # - STACKBLOCK uses the 6-fold symmetry, this must be unpacked
        #   using "libunpack.unpackE3" (see lib/icmpspt/icmpspt.c)
        # - BLOCK just writes a list of all values, this is directly read
        #   using "unpackE3_BLOCK" (see below)
        if (filetype == "binary"):
            fname = os.path.join(self.scratchDirectory, "node0",
                                 "spatial_threepdm.%d.%d.bin" % (state, state))
            if 'stackblock' in settings.BLOCKEXE:
                print 'Reading binary 3RDM from STACKBLOCK'
                fnameout = os.path.join(
                    self.scratchDirectory, "node0",
                    "spatial_threepdm.%d.%d.bin.unpack" % (state, state))
                libunpack.unpackE3(ctypes.c_char_p(fname),
                                   ctypes.c_char_p(fnameout),
                                   ctypes.c_int(norb))
                E3 = numpy.fromfile(fnameout, dtype=numpy.dtype('Float64'))
                E3 = numpy.reshape(E3, (norb, norb, norb, norb, norb, norb),
                                   order='F')
            else:
                print 'Reading binary 3RDM from BLOCK'
                E3 = DMRGCI.unpackE3_BLOCK(self, fname, norb)

        # The 3RDMs written by "Threepdm_container::save_spatial_npdm_text" in BLOCK and STACKBLOCK
        # are written as E3[i1,j2,k3,l3,m2,n1]
        # and are stored here as E3[i1,j2,k3,n1,m2,l3]
        # This is done with SQA in mind.
        else:
            print 'Reading text-file 3RDM'
            fname = os.path.join(self.scratchDirectory, "node0",
                                 "spatial_threepdm.%d.%d.txt" % (state, state))
            f = open(fname, 'r')
            lines = f.readlines()
            E3 = numpy.zeros(shape=(norb, norb, norb, norb, norb, norb),
                             dtype=dt,
                             order='F')
            assert (int(lines[0]) == norb)
            for line in lines[1:]:
                linesp = line.split()
                if (len(linesp) != 7):
                    continue
                a, b, c, d, e, f, integral = int(linesp[0]), int(
                    linesp[1]), int(linesp[2]), int(linesp[3]), int(
                        linesp[4]), int(linesp[5]), float(linesp[6])
                if (False):
                    E3[a, b, c, f, e, d] = integral
                    E3[a, c, b, f, d, e] = integral
                    E3[b, a, c, e, f, d] = integral
                    E3[b, c, a, e, d, f] = integral
                    E3[c, a, b, d, f, e] = integral
                    E3[c, b, a, d, e, f] = integral
                else:
                    self.populate(E3, [a, b, c, f, e, d], integral)
        print ''
        return E3
Beispiel #53
0
def get_string(target, addr):
    if target is None:
        return ctypes.c_char_p(addr).value.decode("latin1")
    return target.read_string(addr)
Beispiel #54
0
 def _arg_to_ctypes(cls, *value):
     if value and isinstance(value[0], bytes):
         return ctypes.c_char_p(value[0])
     else:
         return super(CTypesPtr, cls)._arg_to_ctypes(*value)
Beispiel #55
0
 def read_model(self, path):
     path_char = ctypes.c_char_p(path.encode())
     model = _lib.ffm_load_model_c_string(path_char)
     self._model = model
     return self
Beispiel #56
0
def c_str(string):
	return ctypes.c_char_p(string)
Beispiel #57
0
 def connect(self, address, rack, slot):
     logger.info("connecting to %s rack %s slot %s" % (address, rack, slot))
     return clib.Cli_ConnectTo(self.pointer, c_char_p(address), c_int(rack),
                               c_int(slot))
Beispiel #58
0
 def save_model(self, path):
     model = self._model
     path_char = ctypes.c_char_p(path.encode())
     _lib.ffm_save_model_c_string(model, path_char)
Beispiel #59
0
 def set_session_password(self, password):
     """Send the password to the PLC to meet its security level."""
     assert len(password) <= 8, 'maximum password length is 8'
     return clib.Cli_SetSessionPassword(self.pointer, c_char_p(password))
Beispiel #60
0
    def __init__(self, options=None):

        if options is None:
            options = DetectorOptions()

        self.options = options

        # detect OS to get extension for DLL
        uname0 = os.uname()[0]
        if uname0 == 'Darwin':
            extension = '.dylib'
        else:
            extension = '.so'  # TODO test on windows?

        filename = searchpath = Path(__file__).parent / ('lib/libapriltag' +
                                                         extension)
        print(filename)

        # load the C library and store it as a class variable
        # note: prefer OS install to local!
        try:
            self.libc = ctypes.CDLL(filename)
        except OSError:
            selfdir = os.path.dirname(__file__)
            relpath = os.path.join(selfdir, '../build/lib/', filename)
            if not os.path.exists(relpath):
                raise
            self.libc = ctypes.CDLL(relpath)

        # declare return types of libc function
        self._declare_return_types()

        # create the c-_apriltag_detector object
        self.tag_detector = self.libc.apriltag_detector_create()
        self.tag_detector.contents.nthreads = int(options.nthreads)
        self.tag_detector.contents.quad_decimate = float(options.quad_decimate)
        self.tag_detector.contents.quad_sigma = float(options.quad_sigma)
        self.tag_detector.contents.refine_edges = int(options.refine_edges)
        self.tag_detector.contents.refine_decode = int(options.refine_decode)
        self.tag_detector.contents.refine_pose = int(options.refine_pose)
        self.tag_detector.contents.debug = int(options.debug)
        print("In Detector.__init__")
        print(self.tag_detector.contents.debug)

        if options.quad_contours:
            self.libc.apriltag_detector_enable_quad_contours(
                self.tag_detector, 1)

        self.inverse = options.inverse

        self.families = []

        flist = self.libc.apriltag_family_list()

        for i in range(flist.contents.size):
            ptr = ctypes.c_char_p()
            self.libc.zarray_get(flist, i, ctypes.byref(ptr))
            self.families.append(ctypes.string_at(ptr))
            #print(ptr.value.decode("utf-8"))

        if options.families == 'all':
            families_list = self.families
        elif isinstance(options.families, list):
            families_list = options.families
        else:
            families_list = [
                n for n in re.split(r'\W+', options.families) if n
            ]

        #print("##"+repr(families_list))

        # add tags
        for family in families_list:
            self.add_tag_family(family)