def loadFile(self):
     if (DEBUG):
         self.printTime("Loading file " + self.filename)
     self.minutesOfTheHour = [0]
     self.undef = 9.969209968386869e+36
     self.reader = pyfimex0.createFileReader('nc', self.filename)
     self.interpolator = pyfimex0.createInterpolator(self.reader)
     self.filetime = self.getFileTime(self.filename)
    def test_Proj4(self):
        test_ncfile = os.path.join(test_srcdir,
                                   'erai.sfc.40N.0.75d.200301011200.nc')
        r = pyfimex0.createFileReader('netcdf', test_ncfile)

        inter_ll = pyfimex0.createInterpolator(r)

        inter_ll.changeProjection(
            pyfimex0.InterpolationMethod.BILINEAR,
            "+proj=utm +zone=32 +datum=WGS84 +no_defs",
            range(250000, 280000, 10000),  # 3 x-axis values
            range(6630000, 6680000, 10000),  # 6 y-axis values
            "m",  # x-axis unit
            "m"  # y-axis unit
        )

        values = inter_ll.getDataSlice('ga_skt', 0).values()
        for v in values:
            self.assertTrue((v < 281.1) and (v > 266))
    def test_LonLatPoints(self):
        test_ncfile = os.path.join(test_srcdir,
                                   'erai.sfc.40N.0.75d.200301011200.nc')
        r = pyfimex0.createFileReader('netcdf', test_ncfile)

        inter_ll = pyfimex0.createInterpolator(r)

        lats = [
            59.109, 59.052, 58.994, 58.934, 58.874, 58.812, 58.749, 58.685,
            58.62, 64.
        ]
        lons = [
            4.965, 5.13, 5.296, 5.465, 5.637, 5.81, 5.986, 6.164001, 6.344, 3.
        ]
        inter_ll.changeProjection(pyfimex0.InterpolationMethod.BILINEAR, lons,
                                  lats)

        i_cdm = inter_ll.getCDM()
        self.assertEqual(len(lons), i_cdm.getDimension('x').getLength())
        self.assertEqual(1, i_cdm.getDimension('y').getLength())

        for v in inter_ll.getDataSlice('ga_skt', 0).values():
            self.assertTrue((v < 281.1) and (v > 266))
Ejemplo n.º 4
0
    def interpolateFile(self):
        """ Method for interpolating selected bands to desired resolution"""

        # Parsing interpolation method nearestneighbor to nearest_neighbor
        if self.interpolateMethod == 'nearestneighbor':
            self.interpolateMethod = 'nearest_neighbor'

        validInterpolationMethods = {
            'BICUBIC': pyfimex0.InterpolationMethod.BICUBIC,
            'BILINEAR': pyfimex0.InterpolationMethod.BILINEAR,
            'COORD_NN': pyfimex0.InterpolationMethod.COORD_NN,
            'COORD_NN_KD': pyfimex0.InterpolationMethod.COORD_NN_KD,
            'FORWARD_MAX': pyfimex0.InterpolationMethod.FORWARD_MAX,
            'FORWARD_MEAN': pyfimex0.InterpolationMethod.FORWARD_MEAN,
            'FORWARD_MEDIAN': pyfimex0.InterpolationMethod.FORWARD_MEDIAN,
            'FORWARD_MIN': pyfimex0.InterpolationMethod.FORWARD_MIN,
            'FORWARD_SUM': pyfimex0.InterpolationMethod.FORWARD_SUM,
            'FORWARD_UNDEF_MAX':
            pyfimex0.InterpolationMethod.FORWARD_UNDEF_MAX,
            'FORWARD_UNDEF_MEAN':
            pyfimex0.InterpolationMethod.FORWARD_UNDEF_MEAN,
            'FORWARD_UNDEF_MEDIAN':
            pyfimex0.InterpolationMethod.FORWARD_UNDEF_MEDIAN,
            'FORWARD_UNDEF_MIN':
            pyfimex0.InterpolationMethod.FORWARD_UNDEF_MIN,
            'FORWARD_UNDEF_SUM':
            pyfimex0.InterpolationMethod.FORWARD_UNDEF_SUM,
            'NEAREST_NEIGHBOR': pyfimex0.InterpolationMethod.NEAREST_NEIGHBOR
        }

        interpolator = pyfimex0.createInterpolator(self.extractor)

        if self.interpolateProjString:
            if (self.interpolateXAxisValues and self.interpolateXAxisUnit) or (
                    self.interpolateYAxisValues and self.interpolateYAxisUnit):
                interpolate = True
            else:
                interpolate = False
        else:
            interpolate = False

        if interpolate:  # NOTE - should have two options, i.e. change proj and extr lat/lon array
            print('INTERPOLATING file')

            # Parse input values
            if self.interpolateXAxisValues:
                xAxisValues = self.interpolateXAxisValues.split(',')
                x0 = round(float(xAxisValues[0]))
                x1 = round(float(xAxisValues[-1]))
                dx = round(float(xAxisValues[1]) - x0)
            else:
                cdm = interpolator.getCDM()
                # Get dimensions
                dims = {}
                for dimension in cdm.getDimensionNames():
                    try:
                        dims[dimension] = interpolator.getDataSlice(
                            dimension, 0)
                    except RuntimeError as e:
                        print(e)
                        dims[dimension] = cdm.getDimension(dimension)

                # Get geo transform for output datasets
                x = dims[self.east_dim].values()
                #x0 = round(float(x[0]))
                #x1 = round(float(x[-1]))
                #dx = round(float(abs(x[0]-x[1])))
                x0 = (float(x[0]))
                x1 = (float(x[-1]))
                dx = (float(abs(x[0] - x[1])))
                nx = round((x1 - x0) / dx)

            if self.interpolateYAxisValues:
                yAxisValues = self.interpolateYAxisValues.split(',')
                y0 = (float(yAxisValues[0]))
                y1 = (float(yAxisValues[-1]))
                dy = (float(yAxisValues[1]) - y0)
                ny = round((y1 - y0) / dy)

            else:
                cdm = interpolator.getCDM()
                # Get dimensions
                dims = {}
                for dimension in cdm.getDimensionNames():
                    try:
                        dims[dimension] = interpolator.getDataSlice(
                            dimension, 0)
                    except RuntimeError as e:
                        print(e)
                        dims[dimension] = cdm.getDimension(dimension)

                # Get geo transform for output datasets
                y = dims[self.east_dim].values()
                y0 = (float(y[0]))
                y1 = (float(y[-1]))
                dy = (float(abs(y[0] - y[1])))
                ny = round((y1 - y0) / dy)

            try:
                #print(x0,x1,dx,y0,y1,dy)
                interpolator.changeProjection(
                    validInterpolationMethods[self.interpolateMethod.upper()],
                    self.interpolateProjString,
                    #range(x0,x1+dx,dx),   # x-axis values
                    #range(y0,y1+dy,dy), # y-axis values
                    np.linspace(x0, x1 + dx, nx),  # x-axis values
                    np.linspace(y0, y1 + dy, ny),  # y-axis values
                    self.interpolateXAxisUnit,  # x-axis unit
                    self.interpolateYAxisUnit  # y-axis unit
                )
            except Exception as e:
                print(e)
        return interpolator