def test_multiple_LEs():
    positions = (np.random.random(
        (10000, 2)) * ((grid.max_long - grid.min_long),
                       (grid.max_long - grid.min_long)) +
                 (grid.min_long, grid.min_lat)).astype(np.float32)
    mass = np.ones((positions.shape[0], ), np.float32)
    flags = np.zeros_like(mass, dtype=np.uint8)
    #    flags = np.array( (16, 0) , dtype=np.uint8) # 16 is the notOnSurface: flag

    start = time.time()
    mass_grid1 = comp_volume(positions, mass, flags, grid)
    py_time = time.time() - start
    print "python version took %s seconds" % py_time

    start = time.time()
    mass_grid2 = cy_comp_volume(positions, mass, flags, grid)
    cy_time = time.time() - start
    print "cython version took %s seconds" % cy_time

    print "speedup ratio:", py_time / cy_time

    if np.array_equal(mass_grid1, mass_grid2):
        print "They are the same"
    else:
        print "No match!!! somethign is wrong!!!"
def test_multiple_LEs():
    positions = ( np.random.random((10000, 2)) * 
                  ( (grid.max_long-grid.min_long), (grid.max_long-grid.min_long)) +
                  ( grid.min_long, grid.min_lat)
                  ).astype(np.float32)
    mass = np.ones( (positions.shape[0],), np.float32)
    flags = np.zeros_like(mass, dtype=np.uint8)
#    flags = np.array( (16, 0) , dtype=np.uint8) # 16 is the notOnSurface: flag
    
    start = time.time()
    mass_grid1 = comp_volume(positions, mass, flags, grid)
    py_time = time.time() - start
    print "python version took %s seconds"%py_time

    start = time.time()
    mass_grid2 = cy_comp_volume(positions, mass, flags, grid)
    cy_time = time.time() - start
    print "cython version took %s seconds"%cy_time

    print "speedup ratio:", py_time / cy_time

    if np.array_equal(mass_grid1, mass_grid2):
        print "They are the same"
    else:
        print "No match!!! somethign is wrong!!!"
Esempio n. 3
0
import os, sys
import numpy as np

from batch_gnome import tap_mod, tap_comp_volume

# some test data
infilename = "micro_test.bin"

(Trajectory, (NumTimesteps, NumLEs), HeaderData,
 Flags) = tap_mod.ReadTrajectory(infilename)

print Flags
print Flags.dtype

max = Trajectory.max(axis=0).max(axis=0)
min = Trajectory.min(axis=0).min(axis=0)

# set up a grid:
grid = tap_mod.Grid(min[0], max[0], min[1], max[1], 10, 10)
print grid

# loop through the time steps:
for t_ind in xrange(NumTimesteps):
    #for t_ind in [10]:
    print "timestep :", t_ind
    Positions = Trajectory[t_ind]
    flags = Flags[t_ind]
    masses = np.ones_like(flags, dtype=np.float32)
    Volumes = tap_comp_volume.comp_volume(Positions, masses, flags, grid)
    print Volumes
Esempio n. 4
0
def CompThicknessCube_mf(FileList, OutputTimes, Grid, Weather=None):
    """
    CompThicknessCube computes the average thickness of the oil over
    each receptor site. It only works for grid receptors.

    Filelist is a list of netcdf file names: one for each trajectory

    OutputTimes is a sequence of output times, in hours, from the beginning of the run.

    Grid is a Grid object, specifying the grid parameters

    If Weather is not None it must be a tap_comp_volume.weather_curve object

    If Weather is None, then there is no change in volume.

    """

    ## read the header of the first trajectory file: It is assumed that
    ## the others will match..no check is made to assure this, but it
    ## will crash if anything is very wrong.
    # (junk, junk, HeaderData, junk) = ReadTrajectory(FileList[0],2)

    # read the trajectory data from the first netcdf file
    # print "**************"
    # print "getting header info from file:", FileList[0]
    traj_file = nc_particles.Reader(FileList[0])

    if traj_file.get_units('age') != 'seconds':
        raise ValueError("particle age units in netcdf file must be in seconds")

    NumTimesteps = len(traj_file.times)
    MaxNumLEs = traj_file.particle_count[:].max()

    TimeStep = traj_file.times[1] - traj_file.times[0]  # assume constant timestep!
    traj_file.close()

    TimeStepHours = TimeStep.total_seconds() / 3600.00
    ## OutputTimes should already be in hours
    OutputSteps = (np.array([0] + OutputTimes) / TimeStepHours).astype(np.int32)  # in integer units of time step
    # Allocate the Cube
    NumSpills, NumSites, NumTimes = len(FileList), Grid.num_cells, len(OutputTimes)
    ## fixme: need to make this a float cube!
    Cube = np.zeros((NumTimes, NumSites, NumSpills), np.float32)
    Cube_mf = np.zeros((NumTimes, NumSites, NumSpills), np.float32)

    start = time.time()  # just for timing how long it takes to run
    print OutputSteps

    ## Loop through each individual trajectory
    for SpillNum in range(NumSpills):
        # print "computing spill number %i"%(SpillNum,)
        # read new trajectory file:
        print "working with file:", FileList[SpillNum]
        traj_file = nc_particles.Reader(FileList[SpillNum])
        VolTable = np.zeros((NumSites), np.float32)  # this will store the Maximum volume in each grid box.
        VolTable_mf = np.zeros((NumSites), np.float32)  # this will store the Maximum volume in each grid box.

        ## Step through the Cube output time steps
        for step in xrange(len(OutputSteps) - 1):
            ## step through the Trajectory time steps between each Cube Timestep
            for t in xrange(OutputSteps[step], OutputSteps[step + 1]):
                LE_vars = traj_file.get_timestep(t, variables=['longitude', 'latitude', 'age', 'status_codes'])
                LE_lat = LE_vars['latitude']
                LE_long = LE_vars['longitude']
                LE_positions = np.column_stack((LE_long, LE_lat))
                NumLEs = LE_positions.shape[0]
                LE_age = LE_vars['age'].astype(np.float32) / 3600.00
                # NOTE: for TAP -- we assume that are the particles have unit mass at the start
                #      so we don't read it from the file
                LE_mass = np.ones((NumLEs,), dtype=np.float32)
                if Weather:
                    # print "weathering the LEs"
                    LE_mass = Weather.weather(LE_mass, LE_age)
                flags = LE_vars['status_codes'].astype(np.uint8)
                Vol = tap_comp_volume.comp_volume(LE_positions, LE_mass, flags, Grid)
                Vol_mf = comp_volume_mf(LE_positions, LE_mass, flags, Grid)
                # keep the largest volume computed between output timesteps
                VolTable = np.maximum(Vol.flat, VolTable)
                VolTable_mf = np.maximum(Vol_mf.flat, VolTable_mf)
            ## put the max volume in the Cube at this Cube time step
            # Cube[step,:,SpillNum] = transform(VolTable, MaxNumLEs)
            Cube[step, :, SpillNum] = VolTable
            Cube_mf[step, :, SpillNum] = VolTable_mf
        traj_file.close()
    # print "cube took %s seconds to generate"%(time.time() - start)
    return Cube, Cube_mf
Esempio n. 5
0

from batch_gnome import tap_mod, tap_comp_volume


# some test data
infilename = "micro_test.bin"
                                                
(Trajectory,(NumTimesteps,NumLEs),HeaderData,Flags) = tap_mod.ReadTrajectory(infilename)

print Flags
print Flags.dtype

max = Trajectory.max(axis=0).max(axis=0)
min = Trajectory.min(axis=0).min(axis=0)

# set up a grid:
grid = tap_mod.Grid(min[0], max[0], min[1], max[1], 10, 10)
print grid

# loop through the time steps:
for t_ind in xrange(NumTimesteps):
#for t_ind in [10]:
    print "timestep :", t_ind
    Positions = Trajectory[t_ind]
    flags = Flags[t_ind]
    masses = np.ones_like(flags, dtype=np.float32)
    Volumes = tap_comp_volume.comp_volume(Positions, masses, flags, grid)
    print Volumes