示例#1
0
 def Kernel(self, pyfunc, c_include=""):
     """Wrapper method to convert a `pyfunc` into a :class:`parcels.kernel.Kernel` object
     based on `fieldset` and `ptype` of the ParticleSet"""
     return Kernel(self.fieldset,
                   self.ptype,
                   pyfunc=pyfunc,
                   c_include=c_include)
示例#2
0
 def Kernel(self, pyfunc, c_include="", delete_cfiles=True):
     """Wrapper method to convert a `pyfunc` into a :class:`parcels.kernel.Kernel` object
     based on `fieldset` and `ptype` of the ParticleSet
     :param delete_cfiles: Boolean whether to delete the C-files after compilation in JIT mode (default is True)
     """
     return Kernel(self.fieldset, self.ptype, pyfunc=pyfunc, c_include=c_include,
                   delete_cfiles=delete_cfiles)
示例#3
0
    def density(self, field_name=None, particle_val=None, relative=False, area_scale=False):
        """Method to calculate the density of particles in a ParticleSet from their locations,
        through a 2D histogram.

        :param field: Optional :mod:`parcels.field.Field` object to calculate the histogram
                      on. Default is `fieldset.U`
        :param particle_val: Optional numpy-array of values to weigh each particle with,
                             or string name of particle variable to use weigh particles with.
                             Default is None, resulting in a value of 1 for each particle
        :param relative: Boolean to control whether the density is scaled by the total
                         weight of all particles. Default is False
        :param area_scale: Boolean to control whether the density is scaled by the area
                           (in m^2) of each grid cell. Default is False
        """

        field_name = field_name if field_name else "U"
        field = getattr(self.fieldset, field_name)

        f_str = """
def search_kernel(particle, fieldset, time):
    x = fieldset.{}[time, particle.depth, particle.lat, particle.lon]
        """.format(field_name)

        k = Kernel(
            self.fieldset,
            self.ptype,
            funcname="search_kernel",
            funcvars=["particle", "fieldset", "time", "x"],
            funccode=f_str,
        )
        self.execute(k, runtime=0)

        if isinstance(particle_val, str):
            particle_val = self.particle_data[particle_val]
        else:
            particle_val = particle_val if particle_val else np.ones(self.size)
        density = np.zeros((field.grid.lat.size, field.grid.lon.size), dtype=np.float32)

        p = self.data_accessor()

        for i in range(self.size):
            p.set_index(i)
            try:  # breaks if either p.xi, p.yi, p.zi, p.ti do not exist (in scipy) or field not in fieldset
                if p.ti[field.igrid] < 0:  # xi, yi, zi, ti, not initialised
                    raise('error')
                xi = p.xi[field.igrid]
                yi = p.yi[field.igrid]
            except:
                _, _, _, xi, yi, _ = field.search_indices(p.lon, p.lat, p.depth, 0, 0, search2D=True)
            density[yi, xi] += particle_val[i]

        if relative:
            density /= np.sum(particle_val)

        if area_scale:
            density /= field.cell_areas()

        return density
示例#4
0
 def Kernel(self, pyfunc):
     """Wrapper method to convert a `pyfunc` into a :class:`parcels.kernel.Kernel` object
     based on `grid` and `ptype` of the ParticleSet"""
     return Kernel(self.grid, self.ptype, pyfunc=pyfunc)