Ejemplo n.º 1
0
    def project_array(self, data):
        """Project an array *data* along the given Projector object.
        """

        if self.mode == "nearest":
            if not 'valid_index' in self._cache:
                self._cache['valid_index'] = self._file_cache['valid_index']
                self._cache['valid_output_index'] = \
                    self._file_cache['valid_output_index']
                self._cache['index_array'] = self._file_cache['index_array']

            valid_index, valid_output_index, index_array = \
                (self._cache['valid_index'],
                 self._cache['valid_output_index'],
                 self._cache['index_array'])

            res = kd_tree.get_sample_from_neighbour_info('nn',
                                                         self.out_area.shape,
                                                         data,
                                                         valid_index,
                                                         valid_output_index,
                                                         index_array,
                                                         fill_value=None)

        elif self.mode == "quick":
            if not 'row_idx' in self._cache:
                self._cache['row_idx'] = self._file_cache['row_idx']
                self._cache['col_idx'] = self._file_cache['col_idx']
            row_idx, col_idx = self._cache['row_idx'], self._cache['col_idx']
            img = image.ImageContainer(data, self.in_area, fill_value=None)
            res = np.ma.array(img.get_array_from_linesample(row_idx, col_idx),
                              dtype=data.dtype)

        return res
Ejemplo n.º 2
0
    def _project_array_quick(self, data):
        """Project array *data* using quick interpolation"""
        if 'row_idx' not in self._cache:
            self._cache['row_idx'] = self._file_cache['row_idx']
            self._cache['col_idx'] = self._file_cache['col_idx']
        row_idx, col_idx = self._cache['row_idx'], self._cache['col_idx']
        img = image.ImageContainer(data, self.in_area, fill_value=None)
        res = np.ma.array(img.get_array_from_linesample(row_idx, col_idx),
                          dtype=data.dtype)

        return res
Ejemplo n.º 3
0
 def test_nearest_neighbour_multi_preproc(self):
     data1 = numpy.fromfunction(lambda y, x: y * x * 10 ** -6, (3712, 3712))
     data2 = numpy.fromfunction(
         lambda y, x: y * x * 10 ** -6, (3712, 3712)) * 2
     data = numpy.dstack((data1, data2))
     msg_con = image.ImageContainer(data, self.msg_area)
     #area_con = msg_con.resample_area_nearest_neighbour(self.area_def, 50000)
     row_indices, col_indices = \
         utils.generate_nearest_neighbour_linesample_arrays(self.msg_area,
                                                            self.area_def,
                                                            50000)
     res = msg_con.get_array_from_linesample(row_indices, col_indices)
     cross_sum1 = res[:, :, 0].sum()
     expected1 = 399936.783062
     self.assertAlmostEqual(cross_sum1, expected1,
                            msg='ImageContainer resampling nearest neighbour multi preproc failed')
     cross_sum2 = res[:, :, 1].sum()
     expected2 = 399936.783062 * 2
     self.assertAlmostEqual(cross_sum2, expected2,
                            msg='ImageContainer resampling nearest neighbour multi preproc failed')
Ejemplo n.º 4
0
    def pyresample_method(self, ref_grid_lim, grid_lim, data):
        """
        Method which uses the library "pyresample" which is must faster
        """
        # FIXME: Understand how Proj_Dict influences the result

        X_MAX, Y_MAX = ref_grid_lim
        x_max, y_max = grid_lim

        N, M = data.shape

        proj_dict = {
            'a': '6371228.0',
            'units': 'm',
            'lon_0': '0',
            'proj': 'laea',
            'lat_0': '-90'
        }
        ref_area = geometry.AreaDefinition(
            area_id='REF',
            name='reference',
            proj_id='A',
            proj_dict=proj_dict,
            x_size=N,
            y_size=M,
            area_extent=[-X_MAX, -Y_MAX, X_MAX, Y_MAX])

        init_area = geometry.AreaDefinition(
            area_id='INIT',
            name='initial',
            proj_id='A',
            proj_dict=proj_dict,
            x_size=N,
            y_size=M,
            area_extent=[-x_max, -y_max, x_max, y_max])

        base = image.ImageContainer(data, init_area)
        row_indices, col_indices = utils.generate_quick_linesample_arrays(
            init_area, ref_area)
        result = base.get_array_from_linesample(row_indices, col_indices)
        return result
Ejemplo n.º 5
0
def readE_P():
    x_size = 251
    y_size = 251
    description = 'Arctic EASE grid'
    proj_id = 'ease_nh'
    from pyresample import geometry, utils, image
    area_id = 'ease_nh'
    area_extent = (-7326849.0625,-7326849.0625,7326849.0625,7326849.0625)
    proj_dict = {'a': '6371228.0', 'units': 'm', 'lon_0': '0', \
                 'proj': 'laea', 'lat_0': '90'}
    area_def = geometry.AreaDefinition(area_id, description, proj_id, \
                                       proj_dict, x_size, y_size, area_extent)
    e_p=np.zeros((20,241,480),float)
    nc=Dataset('evap_precip201411.nc','r')
    t=nc.variables['time'][:][20*4+2:30*4+2]
    t0=nc.variables['time'][:][0:1]
    e=nc.variables['e'][21*4+2:31*4+2,::-1,:]
    p=nc.variables['tp'][21*4+2:31*4+2,::-1,:]
    for i in range(1,e.shape[0],2):
        e[i,:,:]=e[i,:,:]-e[i-1,:,:]
        p[i,:,:]=p[i,:,:]-p[i-1,:,:]
        
    print datetime.datetime(1900,1,1)+datetime.timedelta(hours=int(t[0]))
    print datetime.datetime(1900,1,1)+datetime.timedelta(hours=int(t0[0]))
    print datetime.datetime(1900,1,1)+datetime.timedelta(hours=int(t[-1]))
    e_p=(e+p)
    lon,lat=np.meshgrid(0+np.arange(480)*0.75,-90+np.arange(241)*0.75)
    lon[lon>180]-=360.
    grid_def = geometry.GridDefinition(lons=lon, lats=lat)
    row_indices, \
        col_indices = \
                      utils.generate_nearest_neighbour_linesample_arrays(grid_def, area_def, 200000)
    
    msg_con = image.ImageContainer(e_p.sum(axis=0), grid_def)
    e_p_grid = msg_con.get_array_from_linesample(row_indices, col_indices)
    return e_p_grid
Ejemplo n.º 6
0
area_id = 'ease_nh'
area_extent = (-7326849.0625, -7326849.0625, 7326849.0625, 7326849.0625)
proj_dict = {'a': '6371228.0', 'units': 'm', 'lon_0': '0', \
             'proj': 'laea', 'lat_0': '90'}
area_def = geometry.AreaDefinition(area_id, description, proj_id, \
                                   proj_dict, x_size, y_size, area_extent)
import numpy as np

lon, lat = np.meshgrid(-180 + 0.75 / 2 + np.arange(480) * 0.75,
                       -90 + np.arange(241) * 0.75)
grid_def = geometry.GridDefinition(lons=lon, lats=lat)
row_indices, \
    col_indices = \
                  utils.generate_nearest_neighbour_linesample_arrays(grid_def, area_def, 200000)

msg_con = image.ImageContainer(e_p.sum(axis=0), grid_def)
e_p_grid = msg_con.get_array_from_linesample(row_indices, col_indices)
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams.update({'font.size': 13})

m = Basemap(projection='npstere', boundinglat=20, lon_0=0, resolution='l')
plt.suptitle('Moisture Transport from Lagrangian Analysis', fontsize=14)
plt.subplot(111)

m.drawcoastlines()
m.drawparallels(np.arange(-80., 81., 20.), labels=[True, False, False, False])
m.drawmeridians(np.arange(-180., 181., 20.), labels=[False, False, True, True])

xx, yy = area_def.get_lonlats()
x2, y2 = m(xx, yy)