Ejemplo n.º 1
0
 def _load_netcdf4(self, path, variable, lon0, lat0, pressure=None):
     with netCDF4.Dataset(path) as dataset:
         try:
             var = dataset.variables[variable]
         except KeyError:
             return {"x": [], "y": []}
         lons = geo.to_180(self._longitudes(dataset, var))
         lats = self._latitudes(dataset, var)
         i = np.argmin(np.abs(lons - lon0))
         j = np.argmin(np.abs(lats - lat0))
         times = self._times(dataset, var)
         values = var[..., j, i]
         if (("pressure" in var.coordinates)
                 or ("pressure" in var.dimensions)):
             pressures = self._pressures(dataset, var)
             if len(var.dimensions) == 3:
                 pts = self.search(pressures, pressure)
                 values = values[pts]
                 if isinstance(times, dt.datetime):
                     times = [times]
                 else:
                     times = times[pts]
             else:
                 mask = self.search(pressures, pressure)
                 values = values[:, mask][:, 0]
     return {"x": times, "y": values}
Ejemplo n.º 2
0
    def _load_cube(self, path, variable, lon0, lat0, pressure=None):
        import iris
        cube = iris.load_cube(path, iris.Constraint(variable))
        coord = cube.coord('time')
        times = netCDF4.num2date(coord.points, units=str(coord.units))
        lons = cube.coord('longitude').points
        lats = cube.coord('latitude').points
        if lons.ndim == 2:
            lons = lons[0, :]
        if lats.ndim == 2:
            lats = lats[:, 0]
        lons = geo.to_180(lons)
        i = np.argmin(np.abs(lons - lon0))
        j = np.argmin(np.abs(lats - lat0))
        values = cube.data

        # Index array based on coordinate ordering
        pts = []
        for c in cube.coords():
            if c.name() == 'longitude':
                pts.append(i)
            elif c.name() == 'latitude':
                pts.append(j)
            else:
                pts.append(slice(None))
        pts = tuple(pts)
        values = values[pts]

        # Filter time/pressure axes to select correct pressure
        if self._has_dim(cube, 'pressure'):
            pressures = cube.coord('pressure').points
            if len(cube.ndim) == 3:
                pts = self.search(pressures, pressure)
                values = values[pts]
                if isinstance(times, dt.datetime):
                    times = [times]
                else:
                    times = times[pts]
            else:
                mask = self.search(pressures, pressure)
                values = values[:, mask][:, 0]
        return times, values