Esempio n. 1
0
    def _subdivide_observations(self, dsObs):  # {{{
        '''
        Subdivide each segment of the transect so the horizontal resolution
        approximately matches the requested resolution
        '''

        lat = dsObs.lat.values
        lon = dsObs.lon.values

        # compute the great circle distance between these points
        dxIn = self._haversine(lon[0:-1], lat[0:-1], lon[1:], lat[1:])

        nSegments = numpy.maximum(
            (dxIn / self.horizontalResolution + 0.5).astype(int), 1)

        xIn = numpy.zeros(lat.shape)
        xIn[1:] = numpy.cumsum(dxIn)

        outIndex = []
        for index in range(len(xIn) - 1):
            n = nSegments[index]
            outIndex.extend(index + numpy.arange(0, n) / n)
        outIndex.append(len(xIn) - 1)

        xOut = numpy.interp(outIndex, numpy.arange(len(xIn)), xIn)

        dsObs['xIn'] = (('nPoints', ), xIn)
        dsObs['xOut'] = (('nPointsOut', ), xOut)

        # interpolate fields without and with vertical dimension
        dsObs = interp_1d(dsObs,
                          inInterpDim='nPoints',
                          inInterpCoord='xIn',
                          outInterpDim='nPointsOut',
                          outInterpCoord='xOut')
        dsObs = dsObs.drop(['xIn'])
        dsObs = dsObs.rename({'nPointsOut': 'nPoints', 'xOut': 'x'})
        return dsObs  # }}}
Esempio n. 2
0
    def _vertical_interp(self, ds, transectIndex, dsObs, outFileName,
                         outObsFileName):
        '''
        Vertically interpolate a transect and write it to a unique file

        Parameters
        ----------
        ds : ``xarray.Dataset``
            The data set containing all transects before vertical interpolation

        transectIndex : int
            The index of the transect to extract

        dsObs : ``xarray.Dataset``
            The obs dataset used if verticalComparisonGridName is 'obs'

        outFileName : str
            The name of the file to which the resulting data set should be
            written

        outObsFileName : str
            The name of the file to which the resulting obs data set should be
            written if it is interpolated
        '''
        # Authors
        # -------
        # Xylar Asay-Davis

        if os.path.exists(outFileName):
            return

        ds = ds.where(ds.transectNumber == transectIndex, drop=True)

        if self.verticalComparisonGridName == 'mpas':
            z = ds.zMid
            z = z.rename({'nVertLevels': 'nzOut'})
        elif self.verticalComparisonGridName == 'obs':
            z = dsObs.z
            z = z.rename({'nz': 'nzOut'})
        else:
            # a defined vertical grid
            z = (('nzOut', ), self.verticalComparisonGrid)

        if self.verticalComparisonGridName == 'mpas':
            ds = ds.rename({'zMid': 'z', 'nVertLevels': 'nz'})
        else:
            ds['z'] = z
            # remap each variable
            ds = interp_1d(ds,
                           inInterpDim='nVertLevels',
                           inInterpCoord='zMid',
                           outInterpDim='nzOut',
                           outInterpCoord='z')
            ds = ds.rename({'nzOut': 'nz'})

        if self.verticalComparisonGridName != 'obs' and 'nz' in dsObs.dims:
            dsObs['zOut'] = z
            # remap each variable
            dsObs = interp_1d(dsObs,
                              inInterpDim='nz',
                              inInterpCoord='z',
                              outInterpDim='nzOut',
                              outInterpCoord='zOut')
            dsObs = dsObs.rename({'nzOut': 'nz'})
            write_netcdf(dsObs, outObsFileName)

        ds = ds.drop(['validMask', 'transectNumber'])
        write_netcdf(ds, outFileName)  # }}}