Ejemplo n.º 1
0
 def plot(self):
     temp_pset = ParticleSet(fieldset=self.fieldset,
                             pclass=ArgoParticle,
                             lon=0,
                             lat=0,
                             depth=0)
     temp_pset.show(field=self.fieldset.U, with_particles=False)
Ejemplo n.º 2
0
class virtualfleet:
    """
    USAGE:
    lat,lon,depth,time : numpy arrays describing initial set of floats
    vfield : velocityfield object
    """
    def __init__(self, **kwargs):
        #props
        self.lat = kwargs['lat']
        self.lon = kwargs['lon']
        self.depth = kwargs['depth']
        self.time = kwargs['time']
        vfield = kwargs['vfield']
        #define parcels particleset
        self.pset = ParticleSet(fieldset=vfield.fieldset,
                                pclass=ArgoParticle,
                                lon=self.lon,
                                lat=self.lat,
                                depth=self.depth,
                                time=self.time)
        if vfield.isglobal:
            # combine Argo vertical movement kernel with Advection kernel + boundaries
            self.kernels = ArgoVerticalMovement + self.pset.Kernel(
                AdvectionRK4) + self.pset.Kernel(periodicBC)
        else:
            self.kernels = ArgoVerticalMovement + self.pset.Kernel(
                AdvectionRK4)

    def plotfloat(self):
        #using parcels psel builtin show function for now
        self.pset.show()

    def simulate(self, **kwargs):
        """
        USAGE:
        duration : number of days (365)
        dt_run : time step in hours for the computation (1/12)        
        dt_out : time step in hours for writing the output (24)
        output_file
        """
        duration = kwargs['duration']
        dt_run = kwargs['dt_run']
        dt_out = kwargs['dt_out']
        output_file = kwargs['output_file']
        self.run_params = {
            'duration': duration,
            'dt_run': dt_run,
            'dt_out': dt_out,
            'output_file': output_file
        }

        # Now execute the kernels for X days, saving data every Y minutes
        self.pset.execute(
            self.kernels,
            runtime=timedelta(days=duration),
            dt=timedelta(hours=dt_run),
            output_file=self.pset.ParticleFile(
                name=output_file, outputdt=timedelta(hours=dt_out)),
            recovery={ErrorCode.ErrorOutOfBounds: DeleteParticle})
Ejemplo n.º 3
0
def main(gc_dir, output_file, num_paths, runtime, dt):
    filepaths = "%s/*.nc" % gc_dir
    filenames = {'U': filepaths, 'V': filepaths}
    variables = {
        'U': 'eastward_eulerian_current_velocity',
        'V': 'northward_eulerian_current_velocity'
    }
    dimensions = {'lat': 'lat', 'lon': 'lon', 'time': 'time'}
    fieldset = FieldSet.from_netcdf(filenames,
                                    variables,
                                    dimensions,
                                    allow_time_extrapolation=True)

    output = {}

    for (loc_name, loc) in locations.iteritems():
        lat_range = get_range(loc[0], second_largest_divisor(num_paths), 100.0)
        lon_range = get_range(loc[1],
                              num_paths / second_largest_divisor(num_paths),
                              100.0)
        lats, lons = np.meshgrid(lat_range, lon_range)

        pset = ParticleSet(fieldset=fieldset,
                           pclass=JITParticle,
                           lon=lons,
                           lat=lats)

        #pset.show()

        paths = [[] for i in range(num_paths)]
        for d in range(runtime / dt):
            pset.execute(
                AdvectionRK4,
                runtime=timedelta(days=dt),
                dt=timedelta(days=dt),
                recovery={ErrorCode.ErrorOutOfBounds: delete_particle})

            for (i, particle) in enumerate(pset.particles):
                paths[i].append(
                    (trunc_float(particle.lat), trunc_float(particle.lon)))
                i += 1

        output[loc_name] = paths

        pset.show(savefile=posixpath.join("../path_images", loc_name))

    out = open(output_file, 'w')
    out.write(json.dumps(output))
Ejemplo n.º 4
0
class virtualfleet:
    """Argo Virtual Fleet simulator."""

    def __init__(self, **kwargs):
        """
        Parameters
        ----------
        lat,lon,depth,time : numpy arrays describing where Argo floats are deployed
        vfield : :class:`virtualargofleet.velocityfield`
        mission : dictionary {'parking_depth':parking_depth, 'profile_depth':profile_depth, 'vertical_speed':vertical_speed, 'cycle_duration':cycle_duration}
        """
        # props
        self.lat = kwargs['lat']
        self.lon = kwargs['lon']
        self.depth = kwargs['depth']
        self.time = kwargs['time']
        vfield = kwargs['vfield']
        mission = kwargs['mission']
        # Pass mission parameters to simulation through fieldset
        vfield.fieldset.add_constant('parking_depth', mission['parking_depth'])
        vfield.fieldset.add_constant('profile_depth', mission['profile_depth'])
        vfield.fieldset.add_constant('v_speed', mission['vertical_speed'])
        vfield.fieldset.add_constant(
            'cycle_duration', mission['cycle_duration'])

        # Define parcels :class:`parcels.particleset.particlesetsoa.ParticleSetSOA`
        self.pset = ParticleSet(fieldset=vfield.fieldset,
                                pclass=ArgoParticle,
                                lon=self.lon,
                                lat=self.lat,
                                depth=self.depth,
                                time=self.time)
        if vfield.isglobal:
            # combine Argo vertical movement kernel with Advection kernel + boundaries
            self.kernels = ArgoVerticalMovement + \
                self.pset.Kernel(AdvectionRK4) + self.pset.Kernel(periodicBC)
        else:
            self.kernels = ArgoVerticalMovement + \
                self.pset.Kernel(AdvectionRK4)

    @property
    def ParticleSet(self):
        """Container class for storing particle and executing kernel over them.

        Returns
        -------
        :class:`parcels.particleset.particlesetsoa.ParticleSetSOA`
        """
        return self.pset

    def show_deployment(self):
        """Method to show where Argo Floats have been deployed

        Using parcels psel builtin show function for now
        """
        self.pset.show()

    def plotfloat(self):
        warnings.warn("'plotfloat' has been replaced by 'show_deployment'", category=DeprecationWarning, stacklevel=2)
        return self.show_deployment()

    def simulate(self, **kwargs):
        """Execute Virtual Fleet simulation

        Inputs
        ------
        duration: int,
            Number of days (365)
        dt_run: float
            Time step in hours for the computation (1/12)
        dt_out: float
            Time step in hours for writing the output (24)
        output_file: str
            Name of the netcdf file where to store simulation results
        """
        duration = kwargs['duration']
        dt_run = kwargs['dt_run']
        dt_out = kwargs['dt_out']
        output_path = kwargs['output_file']

        if ((os.path.exists(output_path)) | (output_path == '')):
            temp_name = next(tempfile._get_candidate_names())+'.nc'
            while os.path.exists(temp_name):
                temp_name = next(tempfile._get_candidate_names())+'.nc'
            output_path = temp_name
            print(
                "Filename empty of file already exists, simulation will be saved in : " + output_path)
        else:
            print("Simulation will be saved in : " + output_path)
        self.run_params = {'duration': duration, 'dt_run': dt_run,
                           'dt_out': dt_out, 'output_file': output_path}

        output_file = self.pset.ParticleFile(
            name=output_path, outputdt=timedelta(hours=dt_out))
        # Now execute the kernels for X days, saving data every Y minutes
        self.pset.execute(self.kernels,
                          runtime=timedelta(days=duration),
                          dt=timedelta(hours=dt_run),
                          output_file=output_file,
                          recovery={ErrorCode.ErrorOutOfBounds: DeleteParticle})
        output_file.export()
        output_file.close()