Example #1
0
def test_check_dataid():
    """test check dataid."""
    datin = [Data(), Data()]

    dat = dataprep.check_dataid(datin)
    assert dat[0].dataid == '(1)'
    assert dat[1].dataid == '(2)'
Example #2
0
def test_normalisation():
    """Tests for normalisation."""

    datin = Data()
    datin.data = np.ma.array([[1., 2.], [1., 2.]])

    tmp = normalisation.Normalisation(None)
    tmp.indata = {'Raster': [datin, datin]}
    tmp.radiobutton_interval.setChecked(True)
    tmp.settings(True)
    datout = np.ma.array([[0., 1.], [0., 1.]])

    np.testing.assert_array_equal(tmp.outdata['Raster'][0].data, datout)

    tmp.radiobutton_mean.setChecked(True)
    tmp.settings(True)
    datout = np.ma.array([[-1., 1.], [-1., 1.]])

    np.testing.assert_array_equal(tmp.outdata['Raster'][0].data, datout)

    tmp.radiobutton_median.setChecked(True)
    tmp.settings(True)
    datout = np.ma.array([[-1., 1.], [-1., 1.]])

    np.testing.assert_array_equal(tmp.outdata['Raster'][0].data, datout)

    tmp.radiobutton_8bit.setChecked(True)
    tmp.settings(True)

    datout = np.ma.array([[0., 255.], [0., 255.]])

    np.testing.assert_array_equal(tmp.outdata['Raster'][0].data, datout)
Example #3
0
def smalldata():
    """Small test dataset."""
    dat = Data()
    dat.data = np.ma.array([[29000., 29000.], [29000., 29000.]],
                           mask=[[0, 0], [0, 0]])
    dat.extent = (25, 25, -28, -27)  # left, right, bottom, top

    return dat
Example #4
0
def test_rtp():
    """test rtp."""
    datin = Data()
    datin.data = np.ma.array([[1, 2], [1, 2]])
    dat2 = [[0.8763800720931049, 2.123619927906895],
            [0.8763800720931049, 2.123619927906895]]

    dat = dataprep.rtp(datin, 60, 30)
    np.testing.assert_array_equal(dat.data, dat2)
Example #5
0
def test_equation():
    """tests equation editor."""
    datin = Data()
    datin.data = np.ma.array([[1., 2.], [1., 2.]])
    datout = datin.data * 2

    tmp = equation_editor.EquationEditor()
    tmp.indata = {'Raster': [datin, datin]}
    tmp.settings('i0+i1')

    np.testing.assert_array_equal(tmp.outdata['Raster'][0].data, datout)
Example #6
0
def test_trimraster():
    """test trim raster."""
    datin = Data()
    datin.data = np.ma.masked_equal(
        [[0, 0, 0, 0], [0, 1, 2, 0], [0, 1, 2, 0], [0, 0, 0, 0]], 0)
    datin.nullvalue = 0

    dat2 = [[1, 2], [1, 2]]

    dat = dataprep.trim_raster([datin])
    np.testing.assert_array_equal(dat[0].data, dat2)
Example #7
0
    def __init__(self):
        Data.__init__(self)

        self.input_type = []
        self.no_clusters = 0
        self.center = []
        self.center_std = []
        self.memdat = []
        self.vrc = 0.0
        self.nce = None
        self.xbi = None
        self.obj_fcn = None
Example #8
0
def test_IGRF(smalldata):
    """Tests IGRF Calculation."""
    datin2 = Data()
    datin2.data = np.ma.array([[0., 0.], [0., 0.]], mask=[[0, 0], [0, 0]])

    datin2.extent = (25, 25, -28, -27)  # left, right, bottom, top

    dat2 = [[940.640983, 864.497698], [1164.106631, 1079.494023]]

    tmp = igrf.IGRF()
    tmp.indata = {'Raster': [smalldata, datin2]}
    tmp.dateedit.setDate(QtCore.QDate(2000, 1, 1))
    tmp.dsb_alt.setValue(0.)
    tmp.settings(True)

    dat = tmp.outdata['Raster'][-1].data

    np.testing.assert_array_almost_equal(dat, dat2)
Example #9
0
    def init_grid(self, data):
        """ Initializes raster variables in the Data class

        Args:
            data (numpy masked array): masked array containing raster data."""

        grid = Data()
        grid.data = data
        grid.cols = self.numx
        grid.rows = self.numy
        grid.xdim = self.dxy
        grid.ydim = self.dxy
        grid.tlx = self.xrange[0]
        grid.tly = self.yrange[1]
        return grid
Example #10
0
    def run(self):
        """ Entrypoint to start this routine """

        bandid = []
        self.proj.set_current(self.indata['Raster'][0].wkt)

        for i in self.indata['Raster']:
            bandid.append(i.dataid)
            self.banddata[i.dataid] = Data()
            tmp = self.banddata[i.dataid]
            self.dataid[i.dataid] = i.dataid
            tmp.tlx = i.tlx
            tmp.tly = i.tly
            tmp.xdim = i.xdim
            tmp.ydim = i.ydim
            tmp.rows = i.rows
            tmp.cols = i.cols
            tmp.nullvalue = i.nullvalue
            tmp.wkt = i.wkt
            tmp.min = i.data.min()
            tmp.max = i.data.max()
            tmp.mean = i.data.mean()
            try:
                tmp.units = i.units
            except AttributeError:
                tmp.units = ''

        self.combobox_bandid.currentIndexChanged.disconnect()
        self.combobox_bandid.addItems(bandid)
        indx = self.combobox_bandid.currentIndex()
        self.oldtxt = self.combobox_bandid.itemText(indx)
        self.combobox_bandid.currentIndexChanged.connect(self.update_vals)

        idata = self.banddata[self.oldtxt]
        self.lbl_cols.setText(str(idata.cols))
        self.lbl_rows.setText(str(idata.rows))
        self.txt_null.setText(str(idata.nullvalue))
        self.dsb_tlx.setText(str(idata.tlx))
        self.dsb_tly.setText(str(idata.tly))
        self.dsb_xdim.setText(str(idata.xdim))
        self.dsb_ydim.setText(str(idata.ydim))
        self.lbl_min.setText(str(idata.min))
        self.lbl_max.setText(str(idata.max))
        self.lbl_mean.setText(str(idata.mean))
        self.led_units.setText(str(idata.units))

        self.update_vals()

        tmp = self.exec_()
        if tmp == 1:
            self.acceptall()
            tmp = True

        return tmp
Example #11
0
def rtp(data, I_deg, D_deg):
    """ Reduction to the Pole """

    datamedian = np.ma.median(data.data)
    ndat = data.data - datamedian
    ndat.data[ndat.mask] = 0

    fftmod = np.fft.fft2(ndat)

    ny, nx = fftmod.shape
    nyqx = 1 / (2 * data.xdim)
    nyqy = 1 / (2 * data.ydim)

    kx = np.linspace(-nyqx, nyqx, nx)
    ky = np.linspace(-nyqy, nyqy, ny)

    KX, KY = np.meshgrid(kx, ky)

    I = np.deg2rad(I_deg)
    D = np.deg2rad(D_deg)
    alpha = np.arctan2(KY, KX)

    filt = 1 / (np.sin(I) + 1j * np.cos(I) * np.cos(D - alpha))**2

    zrtp = np.fft.ifft2(fftmod * filt)
    zrtp = zrtp.real + datamedian

    # Create dataset
    dat = Data()
    dat.data = np.ma.masked_invalid(zrtp)
    dat.data.mask = np.ma.getmaskarray(data.data)
    dat.rows, dat.cols = zrtp.shape
    dat.nullvalue = data.data.fill_value
    dat.dataid = data.dataid
    dat.tlx = data.tlx
    dat.tly = data.tly
    dat.xdim = data.xdim
    dat.ydim = data.ydim

    return dat
Example #12
0
def test_fuzzy():
    """ test fuzzy cluster """

    dat1 = Data()
    dat1.data = np.ma.identity(3)
    dat1.data.mask = np.zeros([3, 3])
    dat1.data.data[0, 0] = 1.1

    dat2 = Data()
    dat2.data = np.ma.ones([3, 3])
    dat2.data.mask = np.zeros([3, 3])

    tmp = fuzzy_clust.FuzzyClust(None)
    tmp.indata = {'Raster': [dat1, dat2]}
    tmp.spinbox_minclusters.setValue(2)
    tmp.spinbox_maxclusters.setValue(2)
    tmp.combobox_alg.setCurrentIndex(0)
    tmp.settings(True)

    datout2 = tmp.outdata['Cluster'][0].data.data
    datout = np.array([[1, 2, 2], [2, 1, 2], [2, 2, 1]])
    if datout2[0, 0] == 2:
        datout = np.abs(datout - 3)

    np.testing.assert_array_equal(datout2, datout)
Example #13
0
    def init_grid(self, data):
        """ Initializes raster variables in the Data class

        Args:
            data (numpy masked array): masked array containing raster data."""

        grid = Data()
        grid.data = data
        grid.cols = self.numx
        grid.rows = self.numy
        grid.xdim = self.dxy
        grid.ydim = self.dxy
        grid.tlx = self.xrange[0]
        grid.tly = self.yrange[1]
        return grid
Example #14
0
    def acceptall(self):
        """ accept """
        dxy = self.dsb_dxy.value()
        data = self.indata['Point'][0]

        newdat = []
        for data in self.pbar.iter(self.indata['Point']):
            if data.dataid != self.dataid.currentText():
                continue
            x = data.xdata
            y = data.ydata
            z = data.zdata

            for i in [x, y, z]:
                filt = np.logical_not(np.isnan(i))
                x = x[filt]
                y = y[filt]
                z = z[filt]

            tmp = quickgrid(x, y, z, dxy, showtext=self.parent.showprocesslog)
            mask = np.ma.getmaskarray(tmp)
            gdat = tmp.data

            # Create dataset
            dat = Data()
            dat.data = np.ma.masked_invalid(gdat[::-1])
            dat.data.mask = mask[::-1]
            dat.rows, dat.cols = gdat.shape
            dat.nullvalue = dat.data.fill_value
            dat.dataid = data.dataid
            dat.tlx = x.min()
            dat.tly = y.max()
            dat.xdim = dxy
            dat.ydim = dxy
            newdat.append(dat)

        self.outdata['Raster'] = newdat
        self.outdata['Point'] = self.indata['Point']
Example #15
0
    def run(self):
        """Entrypoint to start this routine."""
        bandid = []
        self.proj.set_current(self.indata['Raster'][0].wkt)

        for i in self.indata['Raster']:
            bandid.append(i.dataid)
            self.banddata[i.dataid] = Data()
            tmp = self.banddata[i.dataid]
            self.dataid[i.dataid] = i.dataid
            tmp.xdim = i.xdim
            tmp.ydim = i.ydim
            tmp.nullvalue = i.nullvalue
            tmp.wkt = i.wkt
            tmp.extent = i.extent
            tmp.data = i.data
            tmp.units = i.units

        self.combobox_bandid.currentIndexChanged.disconnect()
        self.combobox_bandid.addItems(bandid)
        indx = self.combobox_bandid.currentIndex()
        self.oldtxt = self.combobox_bandid.itemText(indx)
        self.combobox_bandid.currentIndexChanged.connect(self.update_vals)

        idata = self.banddata[self.oldtxt]

        irows = idata.data.shape[0]
        icols = idata.data.shape[1]

        self.lbl_cols.setText(str(icols))
        self.lbl_rows.setText(str(irows))
        self.txt_null.setText(str(idata.nullvalue))
        self.dsb_tlx.setText(str(idata.extent[0]))
        self.dsb_tly.setText(str(idata.extent[-1]))
        self.dsb_xdim.setText(str(idata.xdim))
        self.dsb_ydim.setText(str(idata.ydim))
        self.lbl_min.setText(str(idata.data.min()))
        self.lbl_max.setText(str(idata.data.max()))
        self.lbl_mean.setText(str(idata.data.mean()))
        self.led_units.setText(str(idata.units))

        self.update_vals()

        tmp = self.exec_()
        if tmp == 1:
            self.acceptall()
            tmp = True

        return tmp
Example #16
0
def test_tilt():
    """test tilt depth."""

    datin = Data()
    datin.data = np.ma.array([[0, 0, .1, .5, 1], [0, .1, .5, 1, .5],
                              [.1, .5, 1, .5, .1], [.5, 1, .5, .1, 0],
                              [1, .5, .1, 0, 0]])

    tmp = tiltdepth.TiltDepth(None)
    tmp.indata = {'Raster': [datin]}
    tmp.dsb_dec.setValue(0.)
    tmp.dsb_inc.setValue(90.)
    tmp.settings(True)
    tmp.change_band1()

    datout2 = tmp.depths

    datout = np.array([[3.93612464, -1.99438548, 1., 0.32962923],
                       [3.49438548, -2.49438548, 1., 0.34958333],
                       [2.99438548, -2.99438548, 1., 0.34958333],
                       [1.98888969, -1.98888969, 2., 0.36451351],
                       [2.48888969, -1.48759916, 2., 0.3654272]])

    np.testing.assert_array_almost_equal(datout2, datout)
Example #17
0
def rtp(data, I_deg, D_deg):
    """ Reduction to the Pole """

    datamedian = np.ma.median(data.data)
    ndat = data.data - datamedian
    ndat.data[ndat.mask] = 0

    fftmod = np.fft.fft2(ndat)

    ny, nx = fftmod.shape
    nyqx = 1/(2*data.xdim)
    nyqy = 1/(2*data.ydim)

    kx = np.linspace(-nyqx, nyqx, nx)
    ky = np.linspace(-nyqy, nyqy, ny)

    KX, KY = np.meshgrid(kx, ky)

    I = np.deg2rad(I_deg)
    D = np.deg2rad(D_deg)
    alpha = np.arctan2(KY, KX)

    filt = 1/(np.sin(I)+1j*np.cos(I)*np.cos(D-alpha))**2

    zrtp = np.fft.ifft2(fftmod*filt)
    zrtp = zrtp.real + datamedian

# Create dataset
    dat = Data()
    dat.data = np.ma.masked_invalid(zrtp)
    dat.data.mask = np.ma.getmaskarray(data.data)
    dat.rows, dat.cols = zrtp.shape
    dat.nullvalue = data.data.fill_value
    dat.dataid = data.dataid
    dat.tlx = data.tlx
    dat.tly = data.tly
    dat.xdim = data.xdim
    dat.ydim = data.ydim

    return dat
Example #18
0
    def acceptall(self):
        """ accept """
        dxy = self.dsb_dxy.value()
        data = self.indata['Point'][0]

        newdat = []
        for data in self.pbar.iter(self.indata['Point']):
            if data.dataid != self.dataid.currentText():
                continue
            x = data.xdata
            y = data.ydata
            z = data.zdata

            for i in [x, y, z]:
                filt = np.logical_not(np.isnan(i))
                x = x[filt]
                y = y[filt]
                z = z[filt]

            tmp = quickgrid(x, y, z, dxy, showtext=self.parent.showprocesslog)
            mask = np.ma.getmaskarray(tmp)
            gdat = tmp.data

    # Create dataset
            dat = Data()
            dat.data = np.ma.masked_invalid(gdat[::-1])
            dat.data.mask = mask[::-1]
            dat.rows, dat.cols = gdat.shape
            dat.nullvalue = dat.data.fill_value
            dat.dataid = data.dataid
            dat.tlx = x.min()
            dat.tly = y.max()
            dat.xdim = dxy
            dat.ydim = dxy
            newdat.append(dat)

        self.outdata['Raster'] = newdat
        self.outdata['Point'] = self.indata['Point']
Example #19
0
    def init_grid(self, data):
        """ Initializes raster variables in the Data class

        Args:
            data (numpy masked array): masked array containing raster data."""

        grid = Data()
        grid.data = data
        grid.xdim = self.dxy
        grid.ydim = self.dxy
        grid.extent = [self.xrange[0], self.xrange[1], self.yrange[0],
                       self.yrange[1]]

        return grid
Example #20
0
def test_crisp():
    """ test crisp cluster """

    dat1 = Data()
    dat1.data = np.ma.identity(3)
    dat1.data.mask = np.zeros([3, 3])

    dat2 = Data()
    dat2.data = np.ma.ones([3, 3])
    dat2.data.mask = np.zeros([3, 3])

    tmp = crisp_clust.CrispClust(None)
    tmp.indata = {'Raster': [dat1, dat2]}
    tmp.spinbox_minclusters.setValue(2)
    tmp.spinbox_maxclusters.setValue(2)
    tmp.settings(True)

    datout2 = tmp.outdata['Cluster'][0].data.data
    datout = np.array([[1, 2, 2], [2, 1, 2], [2, 2, 1]])
    if datout2[0, 0] == 2:
        datout = np.abs(datout - 3)

    np.testing.assert_array_equal(datout2, datout)
Example #21
0
    def init_grid(self, data):
        """
        Initializes raster variables in the Data class.

        Parameters
        ----------
        data : numpy array
             Masked array containing raster data.

        Returns
        -------
        grid : PyGMI Data
            PyGMI raster dataset.

        """
        grid = Data()
        grid.data = data
        grid.xdim = self.dxy
        grid.ydim = self.dxy
        grid.extent = [
            self.xrange[0], self.xrange[1], self.yrange[0], self.yrange[1]
        ]

        return grid
Example #22
0
def gdal_to_dat(dest, bandid='Data'):
    """
    GDAL to Data format.

    Parameters
    ----------
    dest - GDAL format
        GDAL format
    bandid - str
        band identity
    """
    dat = Data()
    gtr = dest.GetGeoTransform()

    nbands = dest.RasterCount

    if nbands == 1:
        rtmp = dest.GetRasterBand(1)
        dat.data = rtmp.ReadAsArray()
    else:
        dat.data = []
        for i in range(nbands):
            rtmp = dest.GetRasterBand(i + 1)
            dat.data.append(rtmp.ReadAsArray())
        dat.data = np.array(dat.data)
        dat.data = np.moveaxis(dat.data, 0, -1)

    nval = rtmp.GetNoDataValue()

    dat.data = np.ma.masked_equal(dat.data, nval)
    dat.data.set_fill_value(nval)
    dat.data = np.ma.fix_invalid(dat.data)

    dat.extent_from_gtr(gtr)
    dat.dataid = bandid
    dat.nullvalue = nval
    dat.wkt = dest.GetProjection()

    return dat
Example #23
0
def get_geosoft(hfile):
    """
    Get geosoft file

    Parameters
    ----------
    ifile : str
        filename to import

    Returns
    -------
    dat : PyGMI Data
        Dataset imported
    """
    f = open(hfile, mode='rb')

    es = np.fromfile(f, dtype=np.int32, count=1)[0]  # 4
    sf = np.fromfile(f, dtype=np.int32, count=1)[0]  # signf
    ncols = np.fromfile(f, dtype=np.int32, count=1)[0]  # ncol
    nrows = np.fromfile(f, dtype=np.int32, count=1)[0]  # nrow
    kx = np.fromfile(f, dtype=np.int32, count=1)[0]  # 1

    dx = np.fromfile(f, dtype=np.float64, count=1)[0]  # dx
    dy = np.fromfile(f, dtype=np.float64, count=1)[0]  # dy
    x0 = np.fromfile(f, dtype=np.float64, count=1)[0]  # xllcor
    y0 = np.fromfile(f, dtype=np.float64, count=1)[0]  # yllcor
    rot = np.fromfile(f, dtype=np.float64, count=1)[0]  # rot
    zbase = np.fromfile(f, dtype=np.float64, count=1)[0]  # zbase
    zmult = np.fromfile(f, dtype=np.float64, count=1)[0]  # zmult

    label = np.fromfile(f, dtype='a48', count=1)[0]
    mapno = np.fromfile(f, dtype='a16', count=1)[0]

    proj = np.fromfile(f, dtype=np.int32, count=1)[0]
    unitx = np.fromfile(f, dtype=np.int32, count=1)[0]
    unity = np.fromfile(f, dtype=np.int32, count=1)[0]
    unitz = np.fromfile(f, dtype=np.int32, count=1)[0]
    nvpts = np.fromfile(f, dtype=np.int32, count=1)[0]
    izmin = np.fromfile(f, dtype=np.int32, count=1)[0]
    izmax = np.fromfile(f, dtype=np.int32, count=1)[0]
    izmed = np.fromfile(f, dtype=np.int32, count=1)[0]
    izmea = np.fromfile(f, dtype=np.int32, count=1)[0]

    zvar = np.fromfile(f, dtype=np.float64, count=1)[0]

    prcs = np.fromfile(f, dtype=np.int32, count=1)[0]

    temspc = np.fromfile(f, dtype='a324', count=1)[0]

    if es == 2:
        nval = -32767
        data = np.fromfile(f, dtype=np.int16, count=nrows * ncols)

    elif es == 4:
        data = np.fromfile(f, dtype=np.float32, count=nrows * ncols)
        nval = -1.0E+32
    else:
        return None

    data = np.ma.masked_equal(data, nval)

    data = data / zmult + zbase
    data.shape = (nrows, ncols)
    data = data[::-1]

    f.close()

    dat = []
    dat.append(Data())
    i = 0

    dat[i].data = data
    dat[i].dataid = hfile[:-4]
    dat[i].nullvalue = nval
    dat[i].xdim = dx
    dat[i].ydim = dy

    xmin = x0
    ymax = y0 + dy * nrows
    ymin = y0
    xmax = xmin + ncols * dx

    dat[i].extent = [xmin, xmax, ymin, ymax]

    return dat
Example #24
0
def get_aster_ged_bin(ifile):
    """
    Get ASTER GED binary format

    Emissivity_Mean_Description: Mean Emissivity for each pixel on grid-box
    using all ASTER data from 2000-2010
    Emissivity_SDev_Description: Emissivity Standard Deviation for each pixel
    on grid-box using all ASTER data from 2000-2010
    Temperature_Mean_Description: Mean Temperature (K) for each pixel on
    grid-box using all ASTER data from 2000-2010
    Temperature_SDev_Description: Temperature Standard Deviation for each pixel
    on grid-box using all ASTER data from 2000-2010
    NDVI_Mean_Description: Mean NDVI for each pixel on grid-box using all ASTER
    data from 2000-2010
    NDVI_SDev_Description: NDVI Standard Deviation for each pixel on grid-box
    using all ASTER data from 2000-2010
    Land_Water_Map_LWmap_Description: Land Water Map using ASTER visible bands
    Observations_NumObs_Description: Number of values used in computing mean
    and standard deviation for each pixel.
    Geolocation_Latitude_Description: Latitude
    Geolocation_Longitude_Description: Longitude
    ASTER_GDEM_ASTGDEM_Description: ASTER GDEM resampled to NAALSED

    Parameters
    ----------
    ifile : str
        filename to import

    Returns
    -------
    dat : PyGMI raster Data
        dataset imported
    """

    dat = []
    nval = -9999
    bandid = {}

    bandid[0] = 'Emissivity_mean_band_10'
    bandid[1] = 'Emissivity_mean_band_11'
    bandid[2] = 'Emissivity_mean_band_12'
    bandid[3] = 'Emissivity_mean_band_13'
    bandid[4] = 'Emissivity_mean_band_14'
    bandid[5] = 'Emissivity_std_dev_band_10'
    bandid[6] = 'Emissivity_std_dev_band_11'
    bandid[7] = 'Emissivity_std_dev_band_12'
    bandid[8] = 'Emissivity_std_dev_band_13'
    bandid[9] = 'Emissivity_std_dev_band_14'
    bandid[10] = 'Temperature_mean'
    bandid[11] = 'Temperature_std_dev'
    bandid[12] = 'NDVI_mean'
    bandid[13] = 'NDVI_std_dev'
    bandid[14] = 'Land_water_map'
    bandid[15] = 'Observations'
    bandid[16] = 'Latitude'
    bandid[17] = 'Longitude'
    bandid[18] = 'ASTER GDEM'

    scale = [
        0.001, 0.001, 0.001, 0.001, 0.001, 0.0001, 0.0001, 0.0001, 0.0001,
        0.0001, 0.01, 0.01, 0.01, 0.01, 1, 1, 0.001, 0.001, 1
    ]

    units = [
        '', '', '', '', '', '', '', '', '', '', 'Kelvin', 'Kelvin', '', '', '',
        'Number per pixel', 'degrees', 'degrees', 'meters'
    ]

    data = np.fromfile(ifile, dtype=np.int32)
    rows_cols = int((data.size / 19)**0.5)
    data.shape = (19, rows_cols, rows_cols)

    lats = data[16] * scale[16]
    lons = data[17] * scale[17]

    latsdim = (lats.max() - lats.min()) / (lats.shape[0] - 1)
    lonsdim = (lons.max() - lons.min()) / (lons.shape[0] - 1)

    tlx = lons.min() - abs(lonsdim / 2)
    tly = lats.max() + abs(latsdim / 2)

    for i in range(19):
        dat.append(Data())

        dat[i].data = data[i] * scale[i]

        dat[i].dataid = bandid[i]
        dat[i].nullvalue = nval * scale[i]
        dat[i].xdim = lonsdim
        dat[i].ydim = latsdim
        dat[i].units = units[i]

        rows, cols = dat[i].data.shape
        xmin = tlx
        ymax = tly
        ymin = ymax - rows * dat[i].ydim
        xmax = xmin + cols * dat[i].xdim

        dat[i].extent = [xmin, xmax, ymin, ymax]

    dat.pop(17)
    dat.pop(16)

    return dat
Example #25
0
    def tiltdepth(self, data):
        """ Calculate tilt depth """
        self.pbar.setValue(0)
        self.pbar.setMaximum(4)

        # RTP
        inc = self.dsb_inc.value()
        dec = self.dsb_dec.value()

        zout = dataprep.rtp(data, inc, dec)

        # Tilt
        self.pbar.setValue(1)

        nr, nc = zout.data.shape
        dy, dx = np.gradient(zout.data)
        dxtot = np.sqrt(dx**2 + dy**2)
        dz = cooper.vertical(zout.data)
        t1 = np.arctan(dz / dxtot)

        self.pbar.setValue(2)
        # A negative number implies we are straddling 0

        # Contour tilt
        x = zout.extent[0] + np.arange(nc) * zout.xdim + zout.xdim / 2
        y = zout.extent[-1] - np.arange(nr) * zout.ydim - zout.ydim / 2

        X, Y = np.meshgrid(x, y)
        Z = np.rad2deg(t1)
        self.X = X
        self.Y = Y
        self.Z = Z

        cnt0 = self.axes.contour(X, Y, Z, [0])
        cnt45 = self.axes.contour(X, Y, Z, [45], alpha=0)
        cntm45 = self.axes.contour(X, Y, Z, [-45], alpha=0)

        self.pbar.setValue(3)

        gx0, gy0, cgrad0, cntid0 = vgrad(cnt0)
        gx45, gy45, _, _ = vgrad(cnt45)
        gxm45, gym45, _, _ = vgrad(cntm45)

        g0 = np.transpose([gx0, gy0])

        self.pbar.setValue(4)

        dmin1 = []
        dmin2 = []

        for i, j in self.pbar.iter(g0):
            dmin1.append(distpc(gx45, gy45, i, j, 0))
            dmin2.append(distpc(gxm45, gym45, i, j, 0))

        dx1 = gx45[dmin1] - gx0
        dy1 = gy45[dmin1] - gy0

        dx2 = gxm45[dmin2] - gx0
        dy2 = gym45[dmin2] - gy0

        grad = np.arctan2(dy1, dx1) * 180 / pi
        grad[grad > 90] -= 180
        grad[grad < -90] += 180
        gtmp1 = np.abs(90 - np.abs(grad - cgrad0))

        grad = np.arctan2(dy2, dx2) * 180 / pi
        grad[grad > 90] -= 180
        grad[grad < -90] += 180
        gtmp2 = np.abs(90 - np.abs(grad - cgrad0))

        gtmp = np.logical_and(gtmp1 <= 10, gtmp2 <= 10)

        gx0 = gx0[gtmp]
        gy0 = gy0[gtmp]
        cntid0 = cntid0[gtmp]
        dx1 = dx1[gtmp]
        dy1 = dy1[gtmp]
        dx2 = dx2[gtmp]
        dy2 = dy2[gtmp]

        dist1 = np.sqrt(dx1**2 + dy1**2)
        dist2 = np.sqrt(dx2**2 + dy2**2)

        dist = np.min([dist1, dist2], 0)

        self.x0 = gx0
        self.x1 = dx1 + gx0
        self.x2 = dx2 + gx0
        self.y0 = gy0
        self.y1 = dy1 + gy0
        self.y2 = dy2 + gy0

        self.depths = np.transpose([gx0, gy0, cntid0.astype(int), dist])

        tmp = dataprep.quickgrid(gx0,
                                 gy0,
                                 dist,
                                 data.xdim,
                                 showtext=self.showtext)

        mask = np.ma.getmaskarray(tmp)
        gdat = tmp.data

        dat = Data()
        dat.data = np.ma.masked_invalid(gdat[::-1])
        dat.data.mask = mask[::-1]
        dat.nullvalue = dat.data.fill_value
        dat.dataid = data.dataid
        dat.xdim = data.xdim
        dat.ydim = data.xdim
        dat.extent = [gx0.min(), gx0.max(), gy0.min(), gy0.max()]

        self.outdata['Raster'] = [dat]
Example #26
0
def get_aster(ifile):
    """
    Gets ASTER Data

    Parameters
    ----------
    ifile : str
        filename to import

    Returns
    -------
    dat : PyGMI raster Data
        dataset imported
    """

    dat = []
    ifile = ifile[:]

    dataset = gdal.Open(ifile, gdal.GA_ReadOnly)

    subdata = dataset.GetSubDatasets()

    latentry = [i for i in subdata if 'Latitude' in i[1]]
    subdata.pop(subdata.index(latentry[0]))

    dataset = None
    dataset = gdal.Open(latentry[0][0], gdal.GA_ReadOnly)
    rtmp = dataset.GetRasterBand(1)
    lats = rtmp.ReadAsArray()
    latsdim = ((lats.max() - lats.min()) / (lats.shape[0] - 1)) / 2

    lonentry = [i for i in subdata if 'Longitude' in i[1]]
    subdata.pop(subdata.index(lonentry[0]))

    dataset = None
    dataset = gdal.Open(lonentry[0][0], gdal.GA_ReadOnly)
    rtmp = dataset.GetRasterBand(1)
    lons = rtmp.ReadAsArray()
    lonsdim = ((lons.max() - lons.min()) / (lons.shape[1] - 1)) / 2

    lonsdim = latsdim
    tlx = lons.min() - abs(lonsdim / 2)
    tly = lats.max() + abs(latsdim / 2)
    cols = int((lons.max() - lons.min()) / lonsdim) + 1
    rows = int((lats.max() - lats.min()) / latsdim) + 1

    newx2, newy2 = np.mgrid[0:rows, 0:cols]
    newx2 = newx2 * lonsdim + tlx
    newy2 = tlx - newy2 * latsdim

    subdata = [i for i in subdata if 'ImageData' in i[0]]

    i = -1
    for ifile2, bandid2 in subdata:
        dataset = None
        dataset = gdal.Open(ifile2, gdal.GA_ReadOnly)

        rtmp2 = dataset.ReadAsArray()

        tmpds = gdal.AutoCreateWarpedVRT(dataset)
        rtmp2 = tmpds.ReadAsArray()
        gtr = tmpds.GetGeoTransform()
        tlx, lonsdim, _, tly, _, latsdim = gtr

        nval = 0

        i += 1

        dat.append(Data())
        dat[i].data = rtmp2

        if dat[i].data.dtype.kind == 'i':
            if nval is None:
                nval = 999999
            nval = int(nval)
        elif dat[i].data.dtype.kind == 'u':
            if nval is None:
                nval = 0
            nval = int(nval)
        else:
            if nval is None:
                nval = 1e+20
            nval = float(nval)

        dat[i].data = np.ma.masked_invalid(dat[i].data)
        dat[i].data.mask = dat[i].data.mask | (dat[i].data == nval)
        if dat[i].data.mask.size == 1:
            dat[i].data.mask = (np.ma.make_mask_none(dat[i].data.shape) +
                                dat[i].data.mask)

        dat[i].dataid = bandid2
        dat[i].nullvalue = nval
        dat[i].xdim = abs(lonsdim)
        dat[i].ydim = abs(latsdim)

        rows, cols = dat[i].data.shape
        xmin = tlx
        ymax = tly
        ymin = ymax - rows * dat[i].ydim
        xmax = xmin + cols * dat[i].xdim

        dat[i].extent = [xmin, xmax, ymin, ymax]

        srs = osr.SpatialReference()
        srs.ImportFromWkt(dataset.GetProjection())
        srs.AutoIdentifyEPSG()

        dat[i].wkt = srs.ExportToWkt()

    if dat == []:
        dat = None
    dataset = None
    return dat
Example #27
0
def get_aster_ged(ifile):
    """
    Gets ASTER GED data

    Parameters
    ----------
    ifile : str
        filename to import

    Returns
    -------
    dat : PyGMI raster Data
        dataset imported
    """
    dat = []
    ifile = ifile[:]

    dataset = gdal.Open(ifile, gdal.GA_ReadOnly)

    subdata = dataset.GetSubDatasets()

    latentry = [i for i in subdata if 'Latitude' in i[1]]
    subdata.pop(subdata.index(latentry[0]))
    dataset = None
    dataset = gdal.Open(latentry[0][0], gdal.GA_ReadOnly)
    rtmp = dataset.GetRasterBand(1)
    lats = rtmp.ReadAsArray()
    latsdim = (lats.max() - lats.min()) / (lats.shape[0] - 1)

    lonentry = [i for i in subdata if 'Longitude' in i[1]]
    subdata.pop(subdata.index(lonentry[0]))

    dataset = None
    dataset = gdal.Open(lonentry[0][0], gdal.GA_ReadOnly)
    rtmp = dataset.GetRasterBand(1)
    lons = rtmp.ReadAsArray()
    lonsdim = (lons.max() - lons.min()) / (lons.shape[0] - 1)

    tlx = lons.min() - abs(lonsdim / 2)
    tly = lats.max() + abs(latsdim / 2)

    i = -1
    for ifile2, bandid2 in subdata:
        dataset = None
        dataset = gdal.Open(ifile2, gdal.GA_ReadOnly)
        bandid = bandid2
        units = ''

        if 'ASTER_GDEM' in bandid2:
            bandid = 'ASTER GDEM'
            units = 'meters'
        if 'Land_Water_Map' in bandid2:
            bandid = 'Land_water_map'
        if 'Observations' in bandid2:
            bandid = 'Observations'
            units = 'number per pixel'

        rtmp2 = dataset.ReadAsArray()

        if rtmp2.shape[-1] == min(rtmp2.shape) and rtmp2.ndim == 3:
            rtmp2 = np.transpose(rtmp2, (2, 0, 1))

        nbands = 1
        if rtmp2.ndim == 3:
            nbands = rtmp2.shape[0]

        for i2 in range(nbands):
            nval = -9999
            i += 1

            dat.append(Data())
            if rtmp2.ndim == 3:
                dat[i].data = rtmp2[i2]
            else:
                dat[i].data = rtmp2

            dat[i].data = np.ma.masked_invalid(dat[i].data)
            dat[i].data.mask = (np.ma.getmaskarray(dat[i].data)
                                | (dat[i].data == nval))
            if dat[i].data.mask.size == 1:
                dat[i].data.mask = (np.ma.make_mask_none(dat[i].data.shape) +
                                    np.ma.getmaskarray(dat[i].data))

            dat[i].data = dat[i].data * 1.0
            if 'Emissivity/Mean' in bandid2:
                bandid = 'Emissivity_mean_band_' + str(10 + i2)
                dat[i].data = dat[i].data * 0.001
            if 'Emissivity/SDev' in bandid2:
                bandid = 'Emissivity_std_dev_band_' + str(10 + i2)
                dat[i].data = dat[i].data * 0.0001
            if 'NDVI/Mean' in bandid2:
                bandid = 'NDVI_mean'
                dat[i].data = dat[i].data * 0.01
            if 'NDVI/SDev' in bandid2:
                bandid = 'NDVI_std_dev'
                dat[i].data = dat[i].data * 0.01
            if 'Temperature/Mean' in bandid2:
                bandid = 'Temperature_mean'
                units = 'Kelvin'
                dat[i].data = dat[i].data * 0.01
            if 'Temperature/SDev' in bandid2:
                bandid = 'Temperature_std_dev'
                units = 'Kelvin'
                dat[i].data = dat[i].data * 0.01

            dat[i].dataid = bandid
            dat[i].nullvalue = nval
            dat[i].xdim = abs(lonsdim)
            dat[i].ydim = abs(latsdim)
            dat[i].units = units

            rows, cols = dat[i].data.shape
            xmin = tlx
            ymax = tly
            ymin = ymax - rows * dat[i].ydim
            xmax = xmin + cols * dat[i].xdim

            dat[i].extent = [xmin, xmax, ymin, ymax]

            srs = osr.SpatialReference()
            srs.ImportFromWkt(dataset.GetProjection())
            srs.AutoIdentifyEPSG()

            dat[i].wkt = srs.ExportToWkt()

    dataset = None
    return dat
Example #28
0
def get_raster(ifile, nval=None):
    """
    This function loads a raster dataset off the disk using the GDAL
    libraries. It returns the data in a PyGMI data object.

    Parameters
    ----------
    ifile : str
        filename to import
    nval : float, optional
        No data/null value. The default is None.

    Returns
    -------
    dat : PyGMI raster Data
        dataset imported
    """
    dat = []
    bname = ifile.split('/')[-1].rpartition('.')[0] + ': '
    ifile = ifile[:]
    ext = ifile[-3:]
    custom_wkt = None

    # Envi Case
    if ext == 'hdr':
        ifile = ifile[:-4]
        tmp = glob.glob(ifile + '.dat')
        if tmp:
            ifile = tmp[0]

    if ext == 'ers':
        with open(ifile) as f:
            metadata = f.read()
            if 'STMLO' in metadata:
                clong = metadata.split('STMLO')[1][:2]

                orig = osr.SpatialReference()
                if 'CAPE' in metadata:
                    orig.ImportFromEPSG(4222)
                    orig.SetTM(0., float(clong), 1., 0., 0.)
                    orig.SetProjCS(r'Cape / TM' + clong)
                    custom_wkt = orig.ExportToWkt()
                elif 'WGS84' in metadata:
                    orig.ImportFromEPSG(4148)
                    orig.SetTM(0., float(clong), 1., 0., 0.)
                    orig.SetProjCS(r'Hartebeesthoek94 / TM' + clong)
                    custom_wkt = orig.ExportToWkt()

    dataset = gdal.Open(ifile, gdal.GA_ReadOnly)

    if dataset is None:
        return None

    gtr = dataset.GetGeoTransform()

    for i in range(dataset.RasterCount):
        rtmp = dataset.GetRasterBand(i + 1)
        bandid = rtmp.GetDescription()
        if nval is None:
            nval = rtmp.GetNoDataValue()

        dat.append(Data())
        dat[i].data = rtmp.ReadAsArray()
        if dat[i].data.dtype.kind == 'i':
            if nval is None:
                nval = 999999
            nval = int(nval)
        elif dat[i].data.dtype.kind == 'u':
            if nval is None:
                nval = 0
            nval = int(nval)
        else:
            if nval is None:
                nval = 1e+20
            nval = float(nval)

        if ext == 'ers' and nval == -1.0e+32:
            dat[i].data[np.ma.less_equal(dat[i].data, nval)] = -1.0e+32


# Note that because the data is stored in a masked array, the array ends up
# being double the size that it was on the disk.
        dat[i].data = np.ma.masked_invalid(dat[i].data)
        dat[i].data.mask = (np.ma.getmaskarray(dat[i].data) |
                            (dat[i].data == nval))
        if dat[i].data.mask.size == 1:
            dat[i].data.mask = (np.ma.make_mask_none(dat[i].data.shape) +
                                np.ma.getmaskarray(dat[i].data))

        dat[i].extent_from_gtr(gtr)
        if bandid == '':
            bandid = bname + str(i + 1)
        dat[i].dataid = bandid
        if bandid[-1] == ')':
            dat[i].units = bandid[bandid.rfind('(') + 1:-1]

        dat[i].nullvalue = nval

        if custom_wkt is None:
            srs = osr.SpatialReference()
            srs.ImportFromWkt(dataset.GetProjection())
            srs.AutoIdentifyEPSG()
            dat[i].wkt = srs.ExportToWkt()
        else:
            dat[i].wkt = custom_wkt

    dataset = None
    return dat
Example #29
0
def get_modis(ifile):
    """
    Gets MODIS data

    Parameters
    ----------
    ifile : str
        filename to import

    Returns
    -------
    dat : PyGMI raster Data
        dataset imported
    """
    dat = []
    ifile = ifile[:]

    dataset = gdal.Open(ifile, gdal.GA_ReadOnly)

    subdata = dataset.GetSubDatasets()

    latentry = [i for i in subdata if 'Latitude' in i[1]]
    subdata.pop(subdata.index(latentry[0]))
    dataset = None

    dataset = gdal.Open(latentry[0][0], gdal.GA_ReadOnly)
    rtmp = dataset.GetRasterBand(1)
    lats = rtmp.ReadAsArray()
    latsdim = ((lats.max() - lats.min()) / (lats.shape[0] - 1)) / 2

    lonentry = [i for i in subdata if 'Longitude' in i[1]]
    subdata.pop(subdata.index(lonentry[0]))

    dataset = None
    dataset = gdal.Open(lonentry[0][0], gdal.GA_ReadOnly)
    rtmp = dataset.GetRasterBand(1)
    lons = rtmp.ReadAsArray()
    lonsdim = ((lons.max() - lons.min()) / (lons.shape[1] - 1)) / 2

    lonsdim = latsdim
    tlx = lons.min() - abs(lonsdim / 2)
    tly = lats.max() + abs(latsdim / 2)
    cols = int((lons.max() - lons.min()) / lonsdim) + 1
    rows = int((lats.max() - lats.min()) / latsdim) + 1

    newx2, newy2 = np.mgrid[0:rows, 0:cols]
    newx2 = newx2 * lonsdim + tlx
    newy2 = tlx - newy2 * latsdim

    tmp = []
    for i in subdata:
        if 'HDF4_EOS:EOS_SWATH' in i[0]:
            tmp.append(i)
    subdata = tmp

    i = -1
    for ifile2, bandid2 in subdata:
        dataset = None
        dataset = gdal.Open(ifile2, gdal.GA_ReadOnly)

        rtmp2 = dataset.ReadAsArray()

        if rtmp2.shape[-1] == min(rtmp2.shape) and rtmp2.ndim == 3:
            rtmp2 = np.transpose(rtmp2, (2, 0, 1))

        nbands = 1
        if rtmp2.ndim == 3:
            nbands = rtmp2.shape[0]

        for i2 in range(nbands):
            rtmp = dataset.GetRasterBand(i2 + 1)
            bandid = rtmp.GetDescription()
            nval = rtmp.GetNoDataValue()
            i += 1

            dat.append(Data())
            if rtmp2.ndim == 3:
                dat[i].data = rtmp2[i2]
            else:
                dat[i].data = rtmp2

            newx = lons[dat[i].data != nval]
            newy = lats[dat[i].data != nval]
            newz = dat[i].data[dat[i].data != nval]

            if newx.size == 0:
                dat[i].data = np.zeros((rows, cols)) + nval
            else:
                tmp = quickgrid(newx, newy, newz, latsdim)
                mask = np.ma.getmaskarray(tmp)
                gdat = tmp.data
                dat[i].data = np.ma.masked_invalid(gdat[::-1])
                dat[i].data.mask = mask[::-1]

            if dat[i].data.dtype.kind == 'i':
                if nval is None:
                    nval = 999999
                nval = int(nval)
            elif dat[i].data.dtype.kind == 'u':
                if nval is None:
                    nval = 0
                nval = int(nval)
            else:
                if nval is None:
                    nval = 1e+20
                nval = float(nval)

            dat[i].data = np.ma.masked_invalid(dat[i].data)
            dat[i].data.mask = (np.ma.getmaskarray(dat[i].data) |
                                (dat[i].data == nval))
            if dat[i].data.mask.size == 1:
                dat[i].data.mask = (np.ma.make_mask_none(dat[i].data.shape) +
                                    np.ma.getmaskarray(dat[i].data))

            dat[i].dataid = bandid2 + ' ' + bandid
            dat[i].nullvalue = nval
            dat[i].xdim = abs(lonsdim)
            dat[i].ydim = abs(latsdim)

            rows, cols = dat[i].data.shape
            xmin = tlx
            ymax = tly
            ymin = ymax - rows * dat[i].ydim
            xmax = xmin + cols * dat[i].xdim

            dat[i].extent = [xmin, xmax, ymin, ymax]

            srs = osr.SpatialReference()
            srs.ImportFromWkt(dataset.GetProjection())
            srs.AutoIdentifyEPSG()

            dat[i].wkt = srs.ExportToWkt()

    dataset = None
    return dat
Example #30
0
def get_ascii(ifile):
    """
    Import ascii raster dataset

    Parameters
    ----------
    ifile : str
        filename to import

    Returns
    -------
    dat : PyGMI raster Data
        dataset imported
    """
    isESRI = False

    with open(ifile, 'r') as afile:
        adata = afile.read()

    adata = adata.split()

    if adata[0] == 'ncols':
        isESRI = True

    if isESRI:
        nbands = 1
        ncols = int(adata[1])
        nrows = int(adata[3])
        xdim = float(adata[9])
        ydim = float(adata[9])
        nval = float(adata[11])
        ulxmap = float(adata[5])
        ulymap = float(adata[7]) + ydim * nrows
        if 'center' in adata[4].lower():
            ulxmap = ulxmap - xdim / 2
        if 'center' in adata[6].lower():
            ulymap = ulymap - ydim / 2
        adata = adata[12:]
    else:
        with open(ifile[:-3] + 'hdr', 'r') as hfile:
            tmp = hfile.readlines()

        xdim = float(tmp[0].split()[-1])
        ydim = float(tmp[1].split()[-1])
        ncols = int(tmp[2].split()[-1])
        nrows = int(tmp[3].split()[-1])
        nbands = int(tmp[4].split()[-1])
        ulxmap = float(tmp[5].split()[-1])
        ulymap = float(tmp[6].split()[-1])
        nval = -9999.0

    bandid = ifile[:-4].rsplit('/')[-1]

    adata = np.array(adata, dtype=float)
    adata.shape = (nrows, ncols)

    if nbands > 1:
        warnings.warn('PyGMI only supports single band ASCII files. '
                      'Only first band will be exported.')

    dat = [Data()]
    i = 0

    dat[i].data = np.ma.masked_equal(adata, nval)
    if dat[i].data.mask.size == 1:
        dat[i].data.mask = (np.ma.make_mask_none(dat[i].data.shape) +
                            dat[i].data.mask)

    dat[i].dataid = bandid
    dat[i].nullvalue = nval
    dat[i].xdim = xdim
    dat[i].ydim = ydim

    rows, cols = dat[i].data.shape
    xmin = ulxmap
    ymax = ulymap
    ymin = ymax - rows * ydim
    xmax = xmin + cols * xdim

    dat[i].extent = [xmin, xmax, ymin, ymax]

    return dat
Example #31
0
    def run(self):
        """
        Run.

        Returns
        -------
        None.

        """
        data = copy.copy(self.indata['Raster'])
        self.update_vars()

        cltype = self.cltype
        cov_constr = self.constrain
        no_runs = self.runs
        max_iter = self.max_iter
        term_thresh = self.term_thresh
        no_clust = np.array([self.min_cluster, self.max_cluster])
        de_norm = self.denorm
        expo = self.fexp

        print('Fuzzy Clustering started')

        # #############################################################################
        # Section to deal with different bands having different null values.
        masktmp = data[0].data.mask  # Start with the first entry
        # Add the masks to this.This promotes False values to True if necessary
        for i in data:
            masktmp += i.data.mask
        for i, _ in enumerate(data):  # Apply this to all the bands
            data[i].data.mask = masktmp
# #############################################################################

        dat_in = np.array([i.data.compressed() for i in data]).T

        if self.radiobutton_manual.isChecked() is True:
            ext = ('ASCII matrix (*.txt);;'
                   'ASCII matrix (*.asc);;'
                   'ASCII matrix (*.dat)')
            filename = QtWidgets.QFileDialog.getOpenFileName(
                self.parent, 'Read Cluster Centers', '.', ext)
            if filename == '':
                return False

            os.chdir(os.path.dirname(filename))
            ifile = str(filename)

            dummy_mod = np.ma.array(np.genfromtxt(ifile, unpack=True))
            [row, col] = np.shape(dummy_mod)
            ro1 = np.sum(list(range(no_clust[0], no_clust[1] + 1)))

            if dat_in.shape[1] != col or row != ro1:
                QtWidgets.QMessageBox.warning(self.parent, 'Warning',
                                              ' Incorrect matrix size!',
                                              QtWidgets.QMessageBox.Ok,
                                              QtWidgets.QMessageBox.Ok)

            cnt = -1
            for i in range(no_clust[0], no_clust[1] + 1):
                smtmp = np.zeros(i)
                for j in range(i):
                    cnt = cnt + 1
                    smtmp[j] = dummy_mod[cnt]
                startmdat = {i: smtmp}
                startmfix = {i: []}

            filename = QtWidgets.QFileDialog.getOpenFileName(
                self.parent, 'Read Cluster Center Constraints', '.', ext)
            if filename == '':
                QtWidgets.QMessageBox.warning(
                    self.parent, 'Warning',
                    'Running cluster analysis without constraints',
                    QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
            else:
                ifile = str(filename)
                dummy_mod = np.ma.array(np.genfromtxt(ifile, unpack=True))
                [row, col] = np.shape(dummy_mod)
                ro1 = np.sum(list(range(no_clust[0], no_clust[1] + 1)))
                if dat_in.shape[1] != col or row != ro1:
                    QtWidgets.QMessageBox.warning(self.parent, 'Warning',
                                                  ' Incorrect matrix size!',
                                                  QtWidgets.QMessageBox.Ok,
                                                  QtWidgets.QMessageBox.Ok)
                cnt = -1
                for i in range(no_clust[0], no_clust[1] + 1):
                    smtmp = np.zeros(i)
                    for j in range(i):
                        cnt = cnt + 1
                        smtmp = dummy_mod[cnt]
                    startmfix = {i: smtmp}

        cnt = -1
        dat_out = [Data() for i in range(no_clust[0], no_clust[1] + 1)]

        for i in range(no_clust[0], no_clust[1] + 1):
            print('Number of Clusters:' + str(i))
            cnt = cnt + 1
            if self.radiobutton_datadriven.isChecked() is True:
                print('Initial guess: data driven')

                no_samp = dat_in.shape[0]
                dno_samp = no_samp / i
                idx = np.arange(0, no_samp + dno_samp, dno_samp)
                idx[0] = 1
                startmdat = {i: np.zeros([i, dat_in.shape[1]])}
                dat_in1 = dat_in
                smtmp = np.zeros([i, dat_in.shape[1]])
                for k in range(dat_in.shape[1]):
                    # this is same as matlab sortrows
                    # dat_in1[dat_in1[:, k].argsort()]
                    for j in range(i):
                        smtmp[j, k] = np.median(dat_in1[idx[j]:idx[j + 1], k])
                startmdat = {i: smtmp}
                startmfix = {i: np.array([])}  # inserted 'np.array'
                del dat_in1

                clu, clcent, clobj_fcn, clvrc, clnce, clxbi = self.fuzzy_means(
                    dat_in, i, startmdat[i], startmfix[i], max_iter,
                    term_thresh, expo, cltype, cov_constr)

            elif self.radiobutton_manual.isChecked() is True:
                print('Initial guess: manual')

                clu, clcent, clobj_fcn, clvrc, clnce, clxbi = self.fuzzy_means(
                    dat_in, i, startmdat[i], startmfix[i], max_iter,
                    term_thresh, expo, cltype, cov_constr)

            elif self.radiobutton_random.isChecked() is True:
                print('Initial guess: random')

                clobj_fcn = np.array([np.Inf])
                for j in range(no_runs):
                    print('Run ' + str(j + 1) + ' of' + str(no_runs))

                    xmins = np.minimum(dat_in, 1)
                    xmaxs = np.maximum(dat_in, 1)
                    startm1dat = {
                        i:
                        np.random.uniform(xmins[np.zeros(i, int), :],
                                          xmaxs[np.zeros(i, int), :])
                    }
                    startmfix = {i: np.array([])}
                    clu1, clcent1, clobj_fcn1, clvrc1, clnce1, clxbi1 = \
                        self.fuzzy_means(dat_in, i, startm1dat[i],
                                         startmfix[i], max_iter, term_thresh,
                                         expo, cltype, cov_constr)

                    if clobj_fcn1[-1] < clobj_fcn[-1]:
                        clu = clu1
                        clcent = clcent1
                        clobj_fcn = clobj_fcn1
                        clnce = clnce1
                        clxbi = clxbi1
                        clvrc = clvrc1
                        startmdat = {i: startm1dat[i]}

            clalp = np.array(clu).max(0)  # 0 means row wise max?
            clidx = np.array(clu).argmax(0)
            clalp = clalp - (1.0 / clcent.shape[0])
            clalp = clalp / clalp.max()
            clalp[clalp > 1] = 1
            clalp[clalp < 0] = 0
            zonal = np.ma.zeros(data[0].data.shape) - 9999.0
            alpha = np.ma.zeros(data[0].data.shape) - 9999.0
            alpha1 = (data[0].data.mask == 0)
            zonal[alpha1 == 1] = clidx
            alpha[alpha1 == 1] = clalp
            zonal.mask = data[0].data.mask
            alpha.mask = data[0].data.mask

            cent_std = np.array(
                [np.std(dat_in[clidx == k], 0) for k in range(i)])

            #            den_cent = clcent
            #            den_cent_std = np.array(cent_std, copy=True)
            #            den_cent_std1 = np.array(cent_std, copy=True)

            if de_norm == 1:
                pass

            dat_out[cnt].metadata['Cluster']['input_type'] = []
            for k in data:
                dat_out[cnt].metadata['Cluster']['input_type'].append(k.dataid)

            dat_out[cnt].data = np.ma.array(zonal)
            dat_out[cnt].metadata['Cluster']['no_clusters'] = i
            dat_out[cnt].metadata['Cluster']['center'] = clcent
            dat_out[cnt].metadata['Cluster']['center_std'] = cent_std

            dat_out[cnt].metadata['Cluster']['memdat'] = []
            for k in range(clcent.shape[0]):
                dummy = np.ones(data[0].data.shape) * np.nan
                alpha1 = (data[0].data.mask == 0)
                dummy[alpha1 == 1] = clu[k, :]
                dat_out[cnt].metadata['Cluster']['memdat'].append(dummy)
            dat_out[cnt].metadata['Cluster']['vrc'] = clvrc
            dat_out[cnt].metadata['Cluster']['nce'] = clnce
            dat_out[cnt].metadata['Cluster']['xbi'] = clxbi
            dat_out[cnt].metadata['Cluster']['obj_fcn'] = clobj_fcn

#            dat_out[cnt].type = self.type
#            dat_out[cnt].algorithm = cltype
#            dat_out[cnt].initialization = init_type
#            dat_out[cnt].init_mod = startmdat[i]
#            dat_out[cnt].init_constrains = startmfix[i]
#            dat_out[cnt].runs = no_runs
#            dat_out[cnt].max_iterations = max_iter
#            dat_out[cnt].denormalize = de_norm
#            dat_out[cnt].term_threshold = term_thresh
#            dat_out[cnt].shape_constrain = cov_constr
#            dat_out[cnt].zonal = zonal
#            dat_out[cnt].alpha = alpha
#            dat_out[cnt].memdat = np.ma.array(dat_out[cnt].memdat)
#            dat_out[cnt].memdat.mask = dat_out[cnt].alpha.mask
#            dat_out[cnt].xxx = data[0].xxx
#            dat_out[cnt].yyy = data[0].yyy
#            dat_out[cnt].denorm_center = den_cent
#            dat_out[cnt].denorm_center_stdup = den_cent_std
#            dat_out[cnt].denorm_center_stdlow = den_cent_std1
#            dat_out[cnt].iterations = clobj_fcn.size
#            dat_out[cnt].fuzziness_exp = expo

#            gaugetmp.crisplogtxt.Value += '\n'+logtxt

        for i in dat_out:
            i.xdim = data[0].xdim
            i.ydim = data[0].ydim
            i.dataid = 'Fuzzy Cluster: ' + str(
                i.metadata['Cluster']['no_clusters'])
            i.nullvalue = data[0].nullvalue
            i.extent = data[0].extent
            i.data += 1

        print('Fuzzy Cluster complete' + ' (' + self.cltype + ' ' +
              self.init_type + ')')

        self.outdata['Cluster'] = dat_out
        self.outdata['Raster'] = self.indata['Raster']

        return True
Example #32
0
def get_geopak(hfile):
    """
    GeoPak Import

    Parameters
    ----------
    hfile : str
        filename to import

    Returns
    -------
    dat : PyGMI raster Data
        dataset imported

    Returns
    -------
    dat : PyGMI Data
        PyGMI raster dataset.

    """

    with open(hfile, 'rb') as fin:
        fall = fin.read()

    off = 0
    fnew = []
    while off < len(fall):
        off += 1
        breclen = np.frombuffer(fall, dtype=np.uint8, count=1, offset=off)[0]

        if breclen == 130:
            break

        reclen = breclen

        if breclen == 129:
            reclen = 128

        off += 1

        fnew.append(fall[off:off + reclen])
        off += reclen

    fnew = b''.join(fnew)
    header = np.frombuffer(fnew, dtype=np.float32, count=32, offset=0)

    #     Lines in grid      1
    #     Points per line    2
    #     Grid factor        3
    #     Grid base value    4
    #     Grid X origin      5
    #     Grid Y origin      6
    #     Grid rotation      7
    #     Grid dummy value   8
    #     Map scale          9
    #     Cell size (X)     10
    #     Cell size (Y)     11
    #     Inches/unit       12
    #     Grid X offset     13
    #     Grid Y offset     14
    #     Grid hdr version  15
    #
    #     Lines in grid     17
    #     Points per line   18
    #     Grid factor       21
    #     Grid base value   22
    #     Z maximum         23
    #     Z minimum         24
    #
    #     Grid dummy value  26

    nrows = int(header[0])
    ncols = int(header[1])
    gfactor = header[2]
    gbase = header[3]
    x0 = header[4]
    y0 = header[5]
    #    rotation = header[6]
    nval = header[7]
    #    mapscale = header[8]
    dx = header[9]
    dy = header[10]
    #    inches_per_unit = header[11]
    #    xoffset = header[12]
    #    yoffset = header[13]
    #    hver = header[14]
    #    zmax = header[22]
    #    zmin = header[23]

    data = np.frombuffer(fnew,
                         dtype=np.int16,
                         count=(nrows * ncols),
                         offset=128)

    data = np.ma.masked_equal(data, nval)
    data = data / gfactor + gbase
    data.shape = (nrows, ncols)
    data = data[::-1]

    dat = []
    dat.append(Data())
    i = 0

    dat[i].data = data
    dat[i].dataid = hfile[:-4]

    dat[i].nullvalue = nval
    dat[i].xdim = dx
    dat[i].ydim = dy

    xmin = x0
    ymax = y0 + dy * nrows
    ymin = y0
    xmax = xmin + ncols * dx

    dat[i].extent = [xmin, xmax, ymin, ymax]

    return dat
Example #33
0
def gdal_to_dat(dest, bandid='Data'):
    """
    GDAL to Data format

    Parameters
    ----------
    dest - GDAL format
        GDAL format
    bandid - str
        band identity
    """
    dat = Data()
    gtr = dest.GetGeoTransform()

    rtmp = dest.GetRasterBand(1)
    dat.data = rtmp.ReadAsArray()
    nval = rtmp.GetNoDataValue()

    dat.data = np.ma.masked_equal(dat.data, nval)
    dat.data.set_fill_value(nval)
    dat.data = np.ma.fix_invalid(dat.data)

    dat.nrofbands = dest.RasterCount
    dat.tlx = gtr[0]
    dat.tly = gtr[3]
    dat.dataid = bandid
    dat.nullvalue = nval
    dat.rows = dest.RasterYSize
    dat.cols = dest.RasterXSize
    dat.xdim = abs(gtr[1])
    dat.ydim = abs(gtr[5])
    dat.wkt = dest.GetProjection()
    dat.gtr = gtr

    return dat
Example #34
0
def gdal_to_dat(dest, bandid='Data'):
    """
    GDAL to Data format

    Parameters
    ----------
    dest - GDAL format
        GDAL format
    bandid - str
        band identity
    """
    dat = Data()
    gtr = dest.GetGeoTransform()

    rtmp = dest.GetRasterBand(1)
    dat.data = rtmp.ReadAsArray()
    nval = rtmp.GetNoDataValue()

    dat.data = np.ma.masked_equal(dat.data, nval)
    dat.data.set_fill_value(nval)
    dat.data = np.ma.fix_invalid(dat.data)

    dat.nrofbands = dest.RasterCount
    dat.tlx = gtr[0]
    dat.tly = gtr[3]
    dat.dataid = bandid
    dat.nullvalue = nval
    dat.rows = dest.RasterYSize
    dat.cols = dest.RasterXSize
    dat.xdim = abs(gtr[1])
    dat.ydim = abs(gtr[5])
    dat.wkt = dest.GetProjection()
    dat.gtr = gtr

    return dat