Ejemplo n.º 1
0
def split_filename_into_tokens(include_dir, filename, is_windows):
    # type: (str, str, bool) -> Any
    r"""
    Tokens are the individual components of paths

    Invalid Linux Tokens
    '\0' (NUL)

    Invalid Windows Tokens
    < (less than)
    > (greater than)
    : (colon - sometimes works, but is actually NTFS Alternate Data Streams)
    " (double quote)
    / (forward slash)
    \ (backslash)
    | (vertical bar or pipe)
    ? (question mark)
    * (asterisk)
    All control codes (<= 31)
    """
    if is_windows:
        inc = PureWindowsPath(include_dir)
        pth = PureWindowsPath(filename).parts

        # fails if the comment has stripped out the file (e.g., "INCLUDE '$ENV/model.bdf'")
        pth0 = pth[0]

        # Linux style paths
        # /work/model.bdf
        if len(pth0) == 1 and pth0[0] == '\\':
            # utterly breaks os.path.join
            raise SyntaxError('filename=%r cannot start with / on Windows' %
                              filename)
    else:
        inc = PurePosixPath(include_dir)
        pth = PurePosixPath(filename).parts

        # fails if the comment has stripped out the file (e.g., "INCLUDE '$ENV/model.bdf'")
        pth0 = pth[0]
        if len(pth0) >= 2 and pth0[:2] == r'\\':
            # Windows network paths
            # \\nas3\work\model.bdf
            raise SyntaxError("filename=%r cannot start with \\\\ on Linux" %
                              filename)

    pth2 = split_tokens(pth, is_windows)
    if is_windows:
        pth3 = ntpath.join(*pth2)
    else:
        pth3 = posixpath.join(*pth2)

    pth_out = inc / pth3
    return pth_out
Ejemplo n.º 2
0
def _split_path(abspath, is_windows):
    """
    Takes a path and splits it into the various components.

    This is a helper method for ``write_include``
    """
    if is_windows:
        parts = PureWindowsPath(abspath).parts
    else:
        parts = PurePosixPath(abspath).parts
    return parts
Ejemplo n.º 3
0
def path2unix(path, nojoin=False, fromwinpath=False):
    '''From a path given in any format, converts to posix path format
    fromwinpath=True forces the input path to be recognized as a Windows path (useful on Unix machines to unit test Windows paths)'''
    if fromwinpath:
        pathparts = list(PureWindowsPath(path).parts)
    else:
        pathparts = list(PurePath(path).parts)
    if nojoin:
        return pathparts
    else:
        return posixpath.join(*pathparts)
Ejemplo n.º 4
0
def split_tokens(tokens, is_windows):
    """converts a series of path tokens into a joinable path"""
    tokens2 = []
    is_mac_linux = not is_windows
    for itoken, token in enumerate(tokens):
        # this is technically legal...
        #   INCLUDE '/testdir/dir1/dir2/*/myfile.dat'
        assert '*' not in token, '* in path not supported; tokens=%s' % tokens
        if is_windows:
            assert '$' not in token, '$ in path not supported; tokens=%s' % tokens
        else:
            assert '%' not in token, '%% in path not supported; tokens=%s' % tokens

        if itoken == 0 and is_mac_linux and ':' in token:
            ## no C:/dir/model.bdf on linux/mac
            #raise SyntaxError('token cannot include colons (:); token=%r; tokens=%s' % (
                #token, str(tokens)))

            # this has an environment variable or a drive letter
            #print(token)
            stokens = token.split(':')
            if len(stokens) != 2:
                msg = "len(stokens)=%s must be 2; stokens=%s" % (len(stokens), stokens)
                raise SyntaxError(msg)
            if len(stokens[0]) == 1:
                if len(stokens[1]) not in [0, 1]:
                    raise SyntaxError('tokens=%r token=%r stokens=%s stoken[1]=%r len=%r' % (
                        tokens, token, stokens, stokens[1], len(stokens[1])))

            if len(stokens[0]) < 2:
                raise SyntaxError('token cannot include colons (:); token=%r; tokens=%s' % (
                    token, str(tokens)))
            else:
                # variables in Windows are not case sensitive; not handled?
                token0 = stokens[0]
                if is_windows:
                    assert '$' not in stokens[0], token0
                    assert '%' not in stokens[0], token0

                    if '%' in token0:
                        assert token0[0] == '%', token0
                        assert token0[-1] == '%', token0
                        token0 = '%' + token0 + '%'
                    else:
                        token0 = '$' + token0

                    #tokeni = os.path.expandvars('$' + stokens[0])
                    tokeni = os.path.expandvars(token0)
                    if '$' in tokeni:
                        raise SyntaxError('tokeni=%r has a $ in it after expanding (token0=%r)...\n'
                                          'tokens=%s stokens=%s' % (tokeni, token0, tokens, stokens))

                    tokensi = PureWindowsPath(tokeni).parts
                else:
                    if '$' in token0:
                        assert token0[0] == '$', token0
                    else:
                        token0 = '$' + token0
                    assert '%' not in stokens[0], token0
                    tokeni = os.path.expandvars(token0)
                    tokensi = PurePosixPath(tokeni).parts

                tokens2.extend(tokensi)
                tokens2.append(stokens[1])

        elif ':' in token:
            # Windows

            # this has an environment variable or a drive letter
            stokens = token.split(':')

            if len(stokens[0]) == 1:
                if len(stokens[1]) not in [0, 1]:
                    raise SyntaxError('tokens=%r token=%r stokens=%s stoken[1]=%r len=%r' % (
                        tokens, token, stokens, stokens[1], len(stokens[1])))
                # drive letter
                if itoken != 0:
                    raise SyntaxError('the drive letter is in the wrong place; '
                                      'itoken=%s; token=%r; stoken=%s; tokens=%s' % (
                                          itoken, token, stokens, tokens))
                tokens2.append(token)
            else:

                # variables in Windows are not case sensitive; not handled?
                environment_vars_to_expand = stokens[:-1]
                if len(environment_vars_to_expand) != 1:
                    raise SyntaxError(
                        'Only 1 environment variable can be expanded; '
                        'environment_vars_to_expand = %r' % environment_vars_to_expand)
                for env_var in environment_vars_to_expand:
                    if env_var.strip('$ %') not in os.environ:
                        environment_variables = list(os.environ.keys())
                        environment_variables.sort()
                        raise SyntaxError("Cant find environment variable=%r"
                                          '\nenviron=%s' % (env_var, environment_variables))

                    env_vari = os.path.expandvars('$' + env_var.strip('%'))
                    if '$' in env_vari:
                        raise SyntaxError('env_vari=%r has a $ in it after expanding (token0=%r)...\n'
                                          'tokens=%s stokens=%s' % (env_vari, env_var, tokens, stokens))
                    if is_windows:
                        tokensi = PureWindowsPath(env_vari).parts
                    else:
                        tokensi = PurePosixPath(env_vari).parts
                    tokens2.extend(tokensi)
                tokens2.append(stokens[-1])
        else:
            # standard
            tokens2.append(token)

    return tokens2
Ejemplo n.º 5
0
# Create a txt and write inside
openfile = open('Probe.txt', 'w')
openfile.write('Hello this is a quick test file')
openfile.close()

# Checking the path file

import os

print(os.getcwd())
print('\n')
print(os.listdir())  # Can We get the path of a specific file?

from pathlib2 import PureWindowsPath

print(PureWindowsPath('Probe.txt'))

my_file = open('Probe.txt')
print(my_file.read())
# Once we read it we have to reset the index

my_file.seek(0)
print(my_file.read())

my_file.seek(0)
my_file.close()
print('\n')

my_file = open('Probe.txt', 'w+')
my_file.write('Hello, this is a new line')
my_file.seek(0)
Ejemplo n.º 6
0
    # 20*log10(a/1) = end_gain_in_dB
    end_gain = 10**(end_gain_in_dB / 10)
    gain_curve = np.linspace(1, end_gain, gain_num).reshape(-1, 1)

    gained_mat = input_mat * gain_curve
    # 饱和
    gained_mat[gained_mat > 32767] = 32767
    gained_mat[gained_mat < -32768] = -32768
    return gained_mat


# %% prior map dataloader
# 读取第一次采集的雷达数据
# file = Path(PureWindowsPath(r'F:/20201219_GPS/500M/CAS_S500Y_4-HHf-LGn.bin'))
file = Path(PureWindowsPath(r'F:/20201219_GPS/500M/CAS_S500Y_4.bin'))  # 回波数据
CH1 = Tools.bin2mat(file)  # CH1 np format
CH1 = np.fliplr(CH1)  # 注意数据收集时的雷达运动方向

# 读取每道数据对应的GPS信息
file = Path(PureWindowsPath(r'F:/20201219_GPS/500M/CAS_S500Y_4.GPR'))
CH1_GPS = Tools.GPRGPSReader(file)  # 经度 纬度 高度 3 * n 矩阵
CH1_GPS = np.fliplr(CH1_GPS)

row_start = 111  # 行裁切
colu_end = 25682  # 列裁切
patch_size = 416  # 切片

# 截取行和列数据
##CH1 = CH1[row_start:row_start+patch_size,:colu_end]/np.max(np.abs(CH1))
CH1 = RemoveBackground(CH1)