def grid_geometric(xdim=200, ydim=100):
    """ Standard earth grid with U and V equivalent to lon/lat in m. """
    lon = np.linspace(-180, 180, xdim, dtype=np.float32)
    lat = np.linspace(-90, 90, ydim, dtype=np.float32)
    U, V = np.meshgrid(lat, lon)
    U *= 1000. * 1.852 * 60.
    V *= 1000. * 1.852 * 60.
    grid = Grid.from_data(np.array(U, dtype=np.float32), lon, lat,
                          np.array(V, dtype=np.float32), lon, lat)
    grid.U.units = Geographic()
    grid.V.units = Geographic()
    return grid
def fieldset_geometric(xdim=200, ydim=100):
    """ Standard earth fieldset with U and V equivalent to lon/lat in m. """
    lon = np.linspace(-180, 180, xdim, dtype=np.float32)
    lat = np.linspace(-90, 90, ydim, dtype=np.float32)
    U, V = np.meshgrid(lat, lon)
    U *= 1000. * 1.852 * 60.
    V *= 1000. * 1.852 * 60.
    data = {'U': U, 'V': V}
    dimensions = {'lon': lon, 'lat': lat}
    fieldset = FieldSet.from_data(data, dimensions, transpose=True)
    fieldset.U.units = Geographic()
    fieldset.V.units = Geographic()
    return fieldset
def test_fieldKh_SpatiallyVaryingBrownianMotion(mesh,
                                                mode,
                                                xdim=200,
                                                ydim=100):
    """Test SpatiallyVaryingDiffusion on a non-uniform diffusivity field
    with a linear gradient in one direction"""
    mesh_conversion = 1 / 1852. / 60 if mesh is 'spherical' else 1
    fieldset = zeros_fieldset(mesh=mesh,
                              xdim=xdim,
                              ydim=ydim,
                              mesh_conversion=mesh_conversion)

    Kh = np.zeros((ydim, xdim), dtype=np.float32)
    for x in range(xdim):
        Kh[:, x] = np.tanh(fieldset.U.lon[x] / fieldset.U.lon[-1] *
                           10.) * xdim / 2. + xdim / 2. + 100.

    grid = RectilinearZGrid(lon=fieldset.U.lon, lat=fieldset.U.lat, mesh=mesh)
    fieldset.add_field(Field('Kh_zonal', Kh, grid=grid))
    fieldset.add_field(Field('Kh_meridional', Kh, grid=grid))

    dKh_zonal_dx, _ = fieldset.Kh_zonal.gradient()
    _, dKh_meridional_dy = fieldset.Kh_meridional.gradient()
    fieldset.add_field(Field('dKh_zonal_dx', dKh_zonal_dx, grid=grid))
    fieldset.add_field(Field('dKh_meridional_dy', dKh_meridional_dy,
                             grid=grid))
    if mesh is 'spherical':
        fieldset.dKh_zonal_dx.units = GeographicPolar()
        fieldset.dKh_meridional_dy.units = Geographic()

    npart = 100
    runtime = delta(days=1)

    random.seed(1234)
    pset = ParticleSet(fieldset=fieldset,
                       pclass=ptype[mode],
                       lon=np.zeros(npart),
                       lat=np.zeros(npart))
    pset.execute(pset.Kernel(SpatiallyVaryingBrownianMotion2D),
                 runtime=runtime,
                 dt=delta(hours=1))

    lats = np.array([p.lat for p in pset])
    lons = np.array([p.lon for p in pset])
    tol = 2000 * mesh_conversion  # effectively 2000 m errors (because of low numbers of particles)
    assert np.allclose(np.mean(lons), 0, atol=tol)
    assert np.allclose(np.mean(lats), 0, atol=tol)
    assert (stats.skew(lons) > stats.skew(lats))
Exemple #4
0
    'borV': {
        'time': 'time',
        'lat': 'lat',
        'lon': 'lon'
    },
}
#%%
#Create the fieldset with the periodic halo and time extrapolation for the EKE
print 'Creating the fieldset'
fieldset = FieldSet.from_netcdf(filenames,
                                variables,
                                dimensions,
                                allow_time_extrapolation=True)
fieldset.add_periodic_halo(zonal=True)
fieldset.uuss.units = GeographicPolar()
fieldset.vuss.units = Geographic()
#The starting coordinates of the Particles, for the North Pacific. They are generated
#by the code NAgrid.py, graciously send to me by David.
lons = np.load(
    '/home/students/4056094/Desktop/Thesis/ParcelsOutput/North Atlantic/InputDistribution/LonsTestgrid0_5.npy'
)
lats = np.load(
    '/home/students/4056094/Desktop/Thesis/ParcelsOutput/North Atlantic/InputDistribution/LatsTestgrid0_5.npy'
)
#lons, lats = np.meshgrid(lon,lat)
lons[lons < 0] += 360


#And now we define what sort of particles we are actually dealing with
class SampleParticle(JITParticle):
    #    #Now the part to determine the age of the particle
Exemple #5
0
                               'lon': 'lon_rho',
                               'lat': 'lat_rho'
                           },
                           interp_method='linear',
                           mesh='spherical',
                           allow_time_extrapolation=True)

fieldset.add_field(iso_psi_all)
fieldset.add_field(source_id_psi)
fieldset.add_field(sink_id_psi)
fieldset.add_field(cdist)
fieldset.add_field(cnormx)
fieldset.add_field(cnormy)

fieldset.cnormx_rho.units = GeographicPolar()
fieldset.cnormy_rho.units = Geographic()

# ADD THE PERIODIC BOUNDARY
fieldset.add_constant('halo_west', -180.)
fieldset.add_constant('halo_east', 180.)
fieldset.add_periodic_halo(zonal=True)

# ADD DIFFUSION
if param['Kh']:
    fieldset.add_constant_field('Kh_zonal', param['Kh'], mesh='spherical')
    fieldset.add_constant_field('Kh_meridional', param['Kh'], mesh='spherical')

# ADD MAXIMUM PARTICLE AGE (IF LIMITED AGE)
fieldset.add_constant('max_age', param['max_age'])

# SET SEED
Exemple #6
0
        'lon': 'longitude',
        'time': 'time'
    },
}

fieldset = FieldSet.from_netcdf(filenames,
                                variables,
                                dimensions,
                                allow_time_extrapolation=True)

###############################################################################
# Adding the Stokes drift to the HYCOM currents                               #
###############################################################################
if stokes == 0:
    fieldset.Ust.units = GeographicPolar()
    fieldset.Vst.units = Geographic()
    fieldset = FieldSet(
        U=fieldset.U + fieldset.Ust,
        V=fieldset.V + fieldset.Vst,
    )

###############################################################################
# Adding the border current, which applies for all scenarios except for 0     #
###############################################################################
datasetBor = Dataset(dataInputdirec + 'boundary_velocities_HYCOM.nc')
borU = datasetBor.variables['MaskUvel'][:]
borU[borU != 0] = borU[borU != 0] / abs(borU[borU != 0])
borV = datasetBor.variables['MaskVvel'][:]
borV[borV != 0] = borV[borV != 0] / abs(borV[borV != 0])
borMag = np.sqrt(np.square(borU) + np.square(borV))
nonzeroMag = borMag > 0
Exemple #7
0
	          'borU':'MaskUvel',
	          'borV':'MaskVvel'}
dimensions = {'U':{'time':'time','lat':'lat','lon':'lon'},
	           'V':{'time':'time','lat':'lat','lon':'lon'},
	           'uwnd':{'time':'time','lat':'latitude','lon':'longitude'},
              'vwnd':{'time':'time','lat':'latitude','lon':'longitude'},
	           'borU':{'time':'time','lat':'lat','lon':'lon'},
	           'borV': {'time':'time','lat':'lat','lon':'lon'},
	           }
#%%
#Create the fieldset with the periodic halo and time extrapolation for the EKE
print 'Creating the fieldset'
fieldset = FieldSet.from_netcdf(filenames, variables, dimensions,allow_time_extrapolation=True)
fieldset.add_periodic_halo(zonal=True)
fieldset.uwnd.units=GeographicPolar()
fieldset.vwnd.units=Geographic()

#The starting coordinates of the Particles, for the North Pacific. They are generated
#by the code NAgrid.py, graciously send to me by David.
lons=np.load('/home/students/4056094/Desktop/Thesis/ParcelsOutput/North Atlantic/InputDistribution/LonsTestgrid0_5.npy')
lats=np.load('/home/students/4056094/Desktop/Thesis/ParcelsOutput/North Atlantic/InputDistribution/LatsTestgrid0_5.npy')
#lons, lats = np.meshgrid(lon,lat)
lons[lons<0]+=360


#And now we define what sort of particles we are actually dealing with
class SampleParticle(JITParticle):
#    #Now the part to determine the age of the particle
    Age=Variable('Age',initial=0.,dtype=np.float32)#agr is gonna be in seconds
    prev_time=Variable('prev_time',initial=attrgetter('time'),to_write=False)
    #Now the part to track the distance covered