Example #1
0
def mask_isce_file(in_file, mask_file, out_file=None):
    if not in_file:
        return

    # read mask_file
    print('read mask from {}'.format(mask_file))
    mask = readfile.read(mask_file)[0]

    # mask isce file
    atr = readfile.read_attribute(in_file)
    length, width = int(atr['LENGTH']), int(atr['WIDTH'])
    interleave = atr['scheme'].upper()
    num_band = int(atr['number_bands'])

    # default short name for data type from ISCE
    dataTypeDict = {
        'byte': 'bool_',
        'float': 'float32',
        'double': 'float64',
        'cfloat': 'complex64',
    }
    data_type = atr['DATA_TYPE'].lower()
    if data_type in dataTypeDict.keys():
        data_type = dataTypeDict[data_type]

    print('read {}'.format(in_file))
    print('setting the (phase) value on the masked out pixels to zero')
    fbase, ext = os.path.splitext(in_file)
    if ext == '.unw':
        amp = readfile.read_binary(in_file,
                                   data_type=data_type,
                                   num_band=num_band,
                                   band_interleave=interleave,
                                   band=1)[0]
        pha = readfile.read_binary(in_file,
                                   data_type=data_type,
                                   num_band=num_band,
                                   band_interleave=interleave,
                                   band=2)[0]
        pha[mask == 0] = 0
        data = np.hstack((amp, pha)).flatten()
    elif ext == '.int':
        data = np.fromfile(in_file, dtype=data_type,
                           count=length * width).reshape(-1, width)
        #data[mask == 0] = np.abs(data[mask == 0])  #set the angle of complex data to zero
        data[mask == 0] = 0
    elif ext in ['.cor', '.conncomp']:
        data = readfile.read(
            in_file
        )[0]  #, data_type=data_type, num_band=num_band, band_interleave=interleave, band=1)[0]
        data[mask == 0] = 0
    else:
        raise ValueError('unsupported ISCE file: {}'.format(in_file))

    # output filename
    if not out_file:
        if ext in ['.int', '.cor', '.unw']:
            out_file = '{}_msk{}'.format(fbase, ext)
        elif in_file.endswith('.unw.conncomp'):
            out_file = '{}_msk.unw.conncomp'.format(
                in_file.split('.unw.conncomp')[0])
        else:
            raise ValueError(
                'unrecognized input file type: {}'.format(in_file))

    data.tofile(out_file)
    print('finished writing to file {}'.format(out_file))

    # prepare ISCE metadata file by
    # 1. copy and rename metadata files
    # 2. update file path inside files
    for ext in ['xml', 'vrt']:
        # copy
        cmd = 'cp {i}.{e} {o}.{e}'.format(i=in_file, o=out_file, e=ext)
        os.system(cmd)

        msg = cmd
        msg += ' and update the corresponding filename'
        print(msg)

        # update file path
        meta_file = '{o}.{e}'.format(o=out_file, e=ext)
        with open(meta_file, 'r') as f:
            s = f.read()
        s = s.replace(os.path.basename(in_file), os.path.basename(out_file))
        with open(meta_file, 'w') as f:
            f.write(s)
    return out_file
Example #2
0
def mask_isce_file(in_file, mask_file, out_file=None):
    if not in_file:
        return    

    # read mask_file
    print('read mask from {}'.format(mask_file))
    mask = readfile.read(mask_file)[0]

    # mask isce file
    atr = readfile.read_attribute(in_file)
    length, width = int(atr['LENGTH']), int(atr['WIDTH'])
    interleave = atr['scheme'].upper()
    num_band = int(atr['number_bands'])

    # default short name for data type from ISCE
    dataTypeDict = {
        'byte': 'bool_',
        'float': 'float32',
        'double': 'float64',
        'cfloat': 'complex64',
    }
    data_type = atr['DATA_TYPE'].lower()
    if data_type in dataTypeDict.keys():
        data_type = dataTypeDict[data_type]

    print('read {}'.format(in_file))
    print('setting the (phase) value on the masked out pixels to zero')
    fbase, ext = os.path.splitext(in_file)
    if ext == '.unw':
        amp = readfile.read_binary(in_file, data_type=data_type, num_band=num_band, band_interleave=interleave, band=1)[0]
        pha = readfile.read_binary(in_file, data_type=data_type, num_band=num_band, band_interleave=interleave, band=2)[0]
        pha[mask == 0] = 0
        data = np.hstack((amp, pha)).flatten()
    elif ext == '.int':
        data = np.fromfile(in_file, dtype=data_type, count=length*width).reshape(-1, width)
        #data[mask == 0] = np.abs(data[mask == 0])  #set the angle of complex data to zero
        data[mask == 0] = 0
    elif ext in ['.cor','.conncomp']:
        data = readfile.read(in_file)[0] #, data_type=data_type, num_band=num_band, band_interleave=interleave, band=1)[0]
        data[mask == 0] = 0
    else:
        raise ValueError('unsupported ISCE file: {}'.format(in_file))

    # output filename
    if not out_file:
        if ext in ['.int', '.cor', '.unw']:
            out_file = '{}_msk{}'.format(fbase, ext)
        elif in_file.endswith('.unw.conncomp'):
            out_file = '{}_msk.unw.conncomp'.format(in_file.split('.unw.conncomp')[0])
        else:
            raise ValueError('unrecognized input file type: {}'.format(in_file))

    data.tofile(out_file)
    print('finished writing to file {}'.format(out_file))

    # prepare ISCE metadata file by
    # 1. copy and rename metadata files
    # 2. update file path inside files
    for ext in ['xml', 'vrt']:
        # copy
        cmd = 'cp {i}.{e} {o}.{e}'.format(i=in_file, o=out_file, e=ext)
        os.system(cmd)

        msg = cmd
        msg += ' and update the corresponding filename'
        print(msg)

        # update file path
        meta_file = '{o}.{e}'.format(o=out_file, e=ext)
        with open(meta_file, 'r') as f:
            s = f.read()
        s = s.replace(os.path.basename(in_file),
                      os.path.basename(out_file))
        with open(meta_file, 'w') as f:
            f.write(s)
    return out_file