Example #1
0
    def test_resize_complex_alg4(self):
        n = Nansat(self.test_file_complex,
                   log_level=40,
                   mapper=self.default_mapper)
        n.resize(0.5, resample_alg=4)

        self.assertTrue(np.any(n[1].imag != 0))
Example #2
0
    def test_resize_by_height(self):
        n = Nansat(self.test_file_gcps,
                   log_level=40,
                   mapper=self.default_mapper)
        n.resize(height=500, resample_alg=1)

        self.assertEqual(type(n[1]), np.ndarray)
Example #3
0
    def test_undo(self):
        n1 = Nansat(self.test_file_stere, logLevel=40)
        shape1 = n1.shape()
        n1.resize(10)
        n1.undo()
        shape2 = n1.shape()

        self.assertEqual(shape1, shape2)
Example #4
0
 def test_resize_complex_alg_average(self):
     n = Nansat(self.test_file_complex, log_level=40, mapper=self.default_mapper)
     with warnings.catch_warnings(record=True) as w:
         n.resize(0.5, resample_alg=-1)
         self.assertEqual(len(w), 1)
         self.assertTrue(issubclass(w[-1].category, UserWarning))
         self.assertIn('The imaginary parts of complex numbers '
                         'are lost when resampling by averaging ', str(w[-1].message))
Example #5
0
    def test_undo(self):
        n1 = Nansat(self.test_file_stere, logLevel=40)
        shape1 = n1.shape()
        n1.resize(10)
        n1.undo()
        shape2 = n1.shape()

        self.assertEqual(shape1, shape2)
Example #6
0
    def test_undo(self):
        n1 = Nansat(self.test_file_stere, log_level=40, mapper=self.default_mapper)
        shape1 = n1.shape()
        n1.resize(10)
        n1.undo()
        shape2 = n1.shape()

        self.assertEqual(shape1, shape2)
Example #7
0
    def feature_tracking(self,
                         bandName='sigma0_HV',
                         factor=0.5,
                         vmin=0,
                         vmax=0.013,
                         domainMargin=10,
                         maxDrift=20,
                         denoise=False,
                         dB=False,
                         **kwargs):
        ''' Find starting and ending point of drift using feature tracking '''
        if denoise:
            # open, denoise and reduce size
            n1 = get_denoised_object(self.filename1, bandName, factor,
                                     **kwargs)
            n2 = get_denoised_object(self.filename2, bandName, factor,
                                     **kwargs)
        else:
            # open and reduce size
            n1 = Nansat(self.filename1)
            n2 = Nansat(self.filename2)
            n1.resize(factor, eResampleAlg=-1)
            n2.resize(factor, eResampleAlg=-1)

        # increase accuracy of coordinate transfomation
        n1 = reproject_gcp_to_stere(n1)
        n2 = reproject_gcp_to_stere(n2)

        # get matrices with data
        img1 = n1[bandName]
        img2 = n2[bandName]

        if not denoise and dB:
            img1 = 10 * np.log10(img1)
            img2 = 10 * np.log10(img2)

        # convert to 0 - 255
        img1 = get_uint8_image(img1, vmin, vmax)
        img2 = get_uint8_image(img2, vmin, vmax)

        # find many key points
        kp1, descr1 = find_key_points(img1, **kwargs)
        kp2, descr2 = find_key_points(img2, **kwargs)

        # filter keypoints by Domain
        kp1, descr1 = domain_filter(n1, kp1, descr1, n2, domainMargin)
        kp2, descr2 = domain_filter(n2, kp2, descr2, n1, domainMargin)

        # find coordinates of matching key points
        x1, y1, x2, y2 = get_match_coords(kp1, descr1, kp2, descr2, **kwargs)

        # filter out pair with too high drift
        x1, y1, x2, y2 = max_drift_filter(n1, x1, y1, n2, x2, y2, maxDrift)

        # filter out inconsistent pairs
        x1, y1, x2, y2 = lstsq_filter(x1, y1, x2, y2, **kwargs)

        return n1, img1, x1, y1, n2, img2, x2, y2
Example #8
0
    def test_resize_resize(self):
        n = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper)
        n.resize(0.1)
        n.resize(10)
        tmpfilename = os.path.join(self.tmp_data_path,
                                   'nansat_resize_resize.png')
        n.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(type(n[1]), np.ndarray)
Example #9
0
    def test_resize_resize(self):
        n = Nansat(self.test_file_gcps, logLevel=40)
        n.resize(0.1)
        n.resize(10)
        tmpfilename = os.path.join(ntd.tmp_data_path,
                                   'nansat_resize_resize.png')
        n.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(type(n[1]), np.ndarray)
Example #10
0
    def test_reproject_gcps_resize(self):
        n1 = Nansat(self.test_file_stere, logLevel=40)
        n2 = Nansat(self.test_file_gcps, logLevel=40)
        n1.reproject(n2)
        n1.resize(2)
        tmpfilename = os.path.join(ntd.tmp_data_path,
                                   'nansat_reproject_gcps_resize.png')
        n1.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(n1.shape()[0], n2.shape()[0]*2)
        self.assertEqual(n1.shape()[1], n2.shape()[1]*2)
        self.assertEqual(type(n1[1]), np.ndarray)
Example #11
0
    def test_resize_complex_algAverage(self):
        n = Nansat(self.test_file_complex, logLevel=40)

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            n.resize(0.5, eResampleAlg=-1)

            self.assertTrue(len(w) == 1)
            self.assertTrue(issubclass(w[-1].category, UserWarning))
            self.assertTrue(
                'The imaginary parts of complex numbers '
                'are lost when resampling by averaging ' in str(w[-1].message))
Example #12
0
    def test_reproject_gcps_resize(self):
        n1 = Nansat(self.test_file_stere, log_level=40, mapper=self.default_mapper)
        n2 = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper)
        n1.reproject(n2)
        n1.resize(2)
        tmpfilename = os.path.join(self.tmp_data_path,
                                   'nansat_reproject_gcps_resize.png')
        n1.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(n1.shape()[0], n2.shape()[0] * 2)
        self.assertEqual(n1.shape()[1], n2.shape()[1] * 2)
        self.assertEqual(type(n1[1]), np.ndarray)
Example #13
0
    def test_resize_complex_algAverage(self):
        n = Nansat(self.test_file_complex, logLevel=40)

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            n.resize(0.5, eResampleAlg=-1)

            self.assertTrue(len(w)==1)
            self.assertTrue(issubclass(w[-1].category, UserWarning))
            self.assertTrue(
                        'The imaginary parts of complex numbers ' \
                        'are lost when resampling by averaging '
                            in str(w[-1].message))
Example #14
0
def save_ice_map(inp_filename, raw_filename, classifier_filename, threads,
                 source, quicklook, force):
    """ Load texture features, apply classifier and save ice map """
    # get filenames
    out_filename = inp_filename.replace('_texture_features.npz',
                                        '_classified_%s.tif' % source)
    if os.path.exists(out_filename) and not force:
        print('Processed file %s already exists.' % out_filename)
        return out_filename

    # import classifier
    clf = pickle.load(open(classifier_filename, "rb"))
    clf.n_jobs = threads

    # get texture features
    npz = np.load(inp_filename)
    features = np.vstack([
        npz['textureFeatures'].item()['HH'],
        npz['textureFeatures'].item()['HV'],
    ])
    imgSize = features.shape[1:]
    features = features.reshape((26, np.prod(imgSize))).T
    gpi = np.isfinite(features.sum(axis=1))
    result = clf.predict(features[gpi, :])
    classImage = np.ones(np.prod(imgSize)) * 255
    classImage[gpi] = result
    classImage = classImage.reshape(imgSize)
    img_shape = classImage.shape

    # open original file to get geometry
    raw_nansat = Nansat(raw_filename)
    # crop and resize original Nansat to match the ice map
    raw_shape = raw_nansat.shape()
    crop = [rshape % ishape for (rshape, ishape) in zip(raw_shape, img_shape)]
    raw_nansat.crop(0, 0, raw_shape[1] - crop[1], raw_shape[0] - crop[0])
    raw_nansat.resize(height=img_shape[0])
    raw_nansat.reproject_gcps()

    # create new Nansat object and add ice map
    ice_map = Nansat.from_domain(domain=raw_nansat,
                                 array=classImage.astype(np.uint8))
    ice_map.set_metadata(raw_nansat.get_metadata())
    ice_map.set_metadata('entry_title', 'S1_SAR_ICE_MAP')
    ice_map = add_colortable(ice_map, source)
    ice_map.export(out_filename, bands=[1], driver='GTiff')

    if quicklook:
        rgb = colorcode_array(classImage, source)
        plt.imsave(out_filename.replace('.tif', '.png'), rgb)

    return out_filename
Example #15
0
    def test_add_latlon_grids_number(self):
        ''' Should create figure with lon/lat gridlines given manually '''
        tmpfilename = os.path.join(self.tmp_data_path,
                                   'figure_latlon_grids_number.png')
        n = Nansat(self.test_file_gcps, mapper=self.default_mapper)
        n.resize(3)
        b = n[1]
        lon, lat = n.get_geolocation_grids()

        f = Figure(b)
        f.process(cmax=100, lonGrid=lon, latGrid=lat, lonTicks=7, latTicks=7)
        f.save(tmpfilename)

        self.assertEqual(type(f), Figure)
        self.assertTrue(os.path.exists(tmpfilename))
Example #16
0
def get_n(filename,
          bandName='sigma0_HV',
          factor=0.5,
          vmin=-30,
          vmax=-5,
          denoise=False,
          dB=True,
          **kwargs):
    ''' Get Nansat object with image data scaled to UInt8
    Parameters
    ----------
        filename : str - input file name
        bandName : str - name of band in the file
        factor : float - subsampling factor
        vmin : float - minimum allowed value in the band
        vmax : float - maximum allowed value in the band
        denoise : bool - apply denoising of sigma0 ?
        dB : bool - apply conversion to dB ?
        **kwargs : parameters for get_denoised_object()
    Returns
    -------
        n : Nansat object with one band scaled to UInt8
    '''
    if denoise:
        # run denoising
        n = get_denoised_object(filename, bandName, factor, **kwargs)
    else:
        # open data with Nansat and downsample
        n = Nansat(filename)
        if factor != 1:
            n.resize(factor, resample_alg=-1)
    # get matrix with data
    img = n[bandName]
    # convert to dB
    if not denoise and dB:
        img = 10 * np.log10(img)
    # convert to 0 - 255
    img = get_uint8_image(img, vmin, vmax)

    nout = Nansat.from_domain(n, img, parameters={'name': bandName})
    nout.set_metadata(n.get_metadata())
    # improve geolocation accuracy
    if len(nout.vrt.dataset.GetGCPs()) > 0:
        nout.reproject_gcps()
        nout.vrt.tps = True
    return nout
Example #17
0
    def test_add_latlon_grids_number(self):
        ''' Should create figure with lon/lat gridlines given manually '''
        tmpfilename = os.path.join(self.tmp_data_path, 'figure_latlon_grids_number.png')
        n = Nansat(self.test_file_gcps, mapper=self.default_mapper)
        n.resize(3)
        b = n[1]
        lon, lat = n.get_geolocation_grids()

        f = Figure(b)
        f.process(cmax=100, lonGrid=lon,
                               latGrid=lat,
                               lonTicks=7,
                               latTicks=7)
        f.save(tmpfilename)

        self.assertEqual(type(f), Figure)
        self.assertTrue(os.path.exists(tmpfilename))
Example #18
0
def get_n(filename,
          bandName='sigma0_HV',
          factor=0.5,
          denoise=False,
          dB=True,
          mask_invalid=True,
          landmask_border=20,
          correct_hh=False,
          correct_hh_factor=-0.27,
          remove_spatial_mean=False,
          vmin=None,
          vmax=None,
          pmin=10,
          pmax=99,
          **kwargs):
    """ Get Nansat object with image data scaled to UInt8
    Parameters
    ----------
    filename : str
        input file name
    bandName : str
        name of band in the file
    factor : float
        subsampling factor
    denoise : bool
        apply denoising of sigma0 ?
    dB : bool
        apply conversion to dB ?
    mask_invalid : bool
        mask invalid pixels (land, inf, etc) with 0 ?
    landmask_border : int
        border around landmask
    correct_hh : bool
        perform angular correction of sigma0_HH ?
    correct_hh_factor : float
        coefficient in the correction factor sigma0_HH_cor = sigma0_HH + correct_hh_factor * incidence_angle
    remove_spatial_mean : bool
        remove spatial mean from image ?
    vmin : float or None
        minimum value to convert to 1
    vmax : float or None
        maximum value to convert to 255
    pmin : float
        lower percentile for data scaling if vmin is None
    pmax : float
        upper percentile for data scaling if vmax is None
    **kwargs : dummy parameters for
        get_denoised_object()

    Returns
    -------
        n : Nansat object with one band scaled to UInt8

    """
    if denoise:
        # run denoising
        n = get_denoised_object(filename, bandName, factor, **kwargs)
    else:
        # open data with Nansat and downsample
        n = Nansat(filename)
        if factor != 1:
            n.resize(factor, resample_alg=-1)
    # get matrix with data
    img = n[bandName]
    # convert to dB
    if not denoise and dB:
        img[img <= 0] = np.nan
        img = 10 * np.log10(img)
    if correct_hh:
        img = hh_angular_correction(n, img, bandName, correct_hh_factor)
    if mask_invalid:
        mask = get_invalid_mask(img, n, landmask_border)
        img[mask] = np.nan
    if remove_spatial_mean:
        img -= get_spatial_mean(img)
    # convert to 0 - 255
    img = get_uint8_image(img, vmin, vmax, pmin, pmax)
    # create Nansat with one band only
    nout = Nansat.from_domain(n, img, parameters={'name': bandName})
    nout.set_metadata(n.get_metadata())
    # improve geolocation accuracy
    if len(nout.vrt.dataset.GetGCPs()) > 0:
        nout.reproject_gcps()
        nout.vrt.tps = True

    return nout
Example #19
0
# Fill gaps: extrapolate valid values into 2 pixels border using nearest neighbour
#     get distance and indices of nearest neighbours
dst, ind = nd.distance_transform_edt(np.isnan(sssOrig),
                                     return_distances=True,
                                     return_indices=True)
#     erase row,col indeces further than 2 pixels
ind[0][dst > 2] = 0
ind[1][dst > 2] = 0
#    fill gaps
sssExtra = sssOrig[tuple(ind)]

# Create a Nansat object from matrix with extrapolated product
nExtra = Nansat(domain=nOrig, array=sssExtra)

# Increase resolution
nExtra.resize(10, eResampleAlg=1)

# Get upscaled SSS
sssUpscaled = nExtra[1]

# Plot all SSS for comparison
for sss, sssName in [(sssOrig, 'sss0.png'),
                     (sssExtra, 'sss1.png'),
                     (sssUpscaled, 'sss2.png')]:
    f = plt.figure(figsize=(10,5))
    plt.imshow(sss, vmin=20, vmax=38, interpolation='nearest')
    plt.gca().get_xaxis().set_visible(False)
    plt.gca().get_yaxis().set_visible(False)
    plt.savefig(sssName, bbox_inches='tight', pad_inches=0.0, dpi=300)
    plt.close('all')
Example #20
0
                                   'info':  'copy from the 1st band array'})
# print band list
n.list_bands()
# get GDAL raster band (2nd band)
band = n.get_GDALRasterBand(bandID=2)

# Get band data and do some operations
# -- Get data from 1st band as numpy array
a = n[1]
# -- Plot the array (pyplot image is save to a PNG file)
plt.imshow(a);plt.colorbar();plt.savefig(oFileName + '01_imshow.png');plt.close()
# -- Save as Matlab file
savemat(oFileName + '01.mat', {'band_1': a})

# Resize the data to 50%
n.resize(0.5)
# make simple indexed image from 1st band with default colormap
n.write_figure(oFileName + '02.png', clim='hist')
# undo resize
n.resize()

# Resize the data to 50% using CubicSpline
n.resize_lite(0.5, eResampleAlg=3)
# make simple indexed image from 1st band with default colormap
n.write_figure(oFileName + '02CubicSpline.png', clim='hist')
# undo resize
n.resize()


# make image with map of the file location
n.write_map(oFileName + '04_map.png')
Example #21
0
savemat(oPath + fileName + '.mat', {'band_1': a})

# make simple indexed image from 1st band
n.write_figure(oPath + fileName + '.png')

# make RGB image from bands 6,5,2 with brightness correction
n.write_figure(oPath + fileName + '_rgb.png', bands=[6,5,2], clim='hist', ratio=0.7, logarithm=True, gamma=3)

# make indexed image with legend
n.write_figure(oPath + fileName + '_legend.png', bands='radiance_900', clim='hist', ratio=0.7, legend=True, titleString='NANSAT Tutorial', fontSize=40)

# make small preview in three steps:
# 1. Reduce size of the Nansat object ten times
# 2. Make simple grayscaled image with brightness correction
# 3. Resize back to original resolution 
n.resize(0.1)
n.write_figure(oPath + fileName + '_preview.png', clim='hist', ratio=0.7, cmapName='gray')
n.resize()

# make KML file with image borders (to be opened in Googe Earth)
n.write_kml(kmlFileName=oPath + fileName + '_preview.kml')

# make image with map of the file location
n.write_map(oPath + fileName + '_map.png')

# Make image, reprojected onto map of Northern Europe in three steps:
# 1. Create Domain object. It describes the desired grid of reprojected image:
# projection, resolution, size, etc. In this case it is geographic projection;
# -10 - 30 E, 50 - 70 W; 2000 x 2000 pixels
# 2. Reproject the Nansat object
# 3. Make simple image
Example #22
0
               gamma=3)

# make indexed image with legend
n.write_figure(oPath + fileName + '_legend.png',
               bands='radiance_900',
               clim='hist',
               ratio=0.7,
               legend=True,
               titleString='NANSAT Tutorial',
               fontSize=40)

# make small preview in three steps:
# 1. Reduce size of the Nansat object ten times
# 2. Make simple grayscaled image with brightness correction
# 3. Resize back to original resolution
n.resize(0.1)
n.write_figure(oPath + fileName + '_preview.png',
               clim='hist',
               ratio=0.7,
               cmapName='gray')
n.resize()

# make KML file with image borders (to be opened in Googe Earth)
n.write_kml(kmlFileName=oPath + fileName + '_preview.kml')

# make image with map of the file location
n.write_map(oPath + fileName + '_map.png')

# Make image, reprojected onto map of Northern Europe in three steps:
# 1. Create Domain object. It describes the desired grid of reprojected image:
# projection, resolution, size, etc. In this case it is geographic projection;
Example #23
0
 def test_resize_eResampleAlg_is_given(self):
     n = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper)
     n.resize(pixelsize=500, eResampleAlg=5)
Example #24
0
# Fill gaps: extrapolate valid values into 2 pixels border using nearest neighbour
#     get distance and indices of nearest neighbours
dst, ind = nd.distance_transform_edt(np.isnan(sssOrig),
                                     return_distances=True,
                                     return_indices=True)
#     erase row,col indeces further than 2 pixels
ind[0][dst > 2] = 0
ind[1][dst > 2] = 0
#    fill gaps
sssExtra = sssOrig[tuple(ind)]

# Create a Nansat object from matrix with extrapolated product
nExtra = Nansat(domain=nOrig, array=sssExtra)

# Increase resolution
nExtra.resize(10, eResampleAlg=1)

# Get upscaled SSS
sssUpscaled = nExtra[1]

# Plot all SSS for comparison
for sss, sssName in [(sssOrig, 'sss0.png'), (sssExtra, 'sss1.png'),
                     (sssUpscaled, 'sss2.png')]:
    f = plt.figure(figsize=(10, 5))
    plt.imshow(sss, vmin=20, vmax=38, interpolation='nearest')
    plt.gca().get_xaxis().set_visible(False)
    plt.gca().get_yaxis().set_visible(False)
    plt.savefig(sssName, bbox_inches='tight', pad_inches=0.0, dpi=300)
    plt.close('all')
Example #25
0
    def test_resize_complex_alg4(self):
        n = Nansat(self.test_file_complex, logLevel=40)
        n.resize(0.5, eResampleAlg=4)

        self.assertTrue(np.any(n[1].imag != 0))
Example #26
0
    def test_resize_by_height(self):
        n = Nansat(self.test_file_gcps, logLevel=40)
        n.resize(height=500, eResampleAlg=1)

        self.assertEqual(type(n[1]), np.ndarray)
Example #27
0
    def test_resize_by_factor(self):
        n = Nansat(self.test_file_gcps, logLevel=40)
        n.resize(0.5, eResampleAlg=1)

        self.assertEqual(type(n[1]), np.ndarray)
Example #28
0
    def test_resize_by_factor(self):
        n = Nansat(self.test_file_gcps, logLevel=40)
        n.resize(0.5, eResampleAlg=1)

        self.assertEqual(type(n[1]), np.ndarray)
Example #29
0
    def test_resize_complex_alg4(self):
        n = Nansat(self.test_file_complex, logLevel=40)
        n.resize(0.5, eResampleAlg=4)

        self.assertTrue(np.any(n[1].imag!=0))
Example #30
0
    def test_resize_by_height(self):
        n = Nansat(self.test_file_gcps, logLevel=40)
        n.resize(height=500, eResampleAlg=1)

        self.assertEqual(type(n[1]), np.ndarray)
Example #31
0
# add logo to image to the lower left corner
# (make sure file is in the current folder)
n.write_figure(oFileName + '_logo.png', logoFileName='nansat_logo_s.png', logoLocation=[10, -10], logoSize=[70, 70])

# write figure with lat/lon grid
# 1. Get lat/lon arrays from Nansat object (may take some time)
# 2. Make figure with lat/lon grids
lonGrid, latGrid = n.get_geolocation_grids()
n.write_figure(oFileName + '_latlon.png', latGrid=latGrid, lonGrid=lonGrid, latlonGridSpacing=10, latlonLabels=10)

# make small preview
# 1. Reduce size of the Nansat object ten times
# 2. Make simple grayscaled image with brightness correction
# 3. Resize back to original resolution
n.resize(0.1)
n.write_figure(oFileName + '_preview.png', clim='hist', cmapName='gray')
n.resize()

# enlarge the image with bicubic-spline interpolation
n.resize(2, eResampleAlg=3)
n.write_figure(oFileName + '_large.png', clim='hist', cmapName='gray')
n.resize()

# make KML file with image borders (to be opened in Googe Earth)
n.write_kml(kmlFileName=oFileName + '_preview.kml')

# make image with map of the file location
n.write_map(oFileName + '_map.png')