tracmass.seed#
- tracmass.seed.ijk(i1, i2, j1, j2, k1, k2, grid=None, use_subset_coordinates=False, **sub_kw)[source]#
Seed particles in every cell of an i/j/k index box.
- Parameters:
i1 (
int) – 1-based TRACMASS cell index ranges (inclusive). By default i and j are absolute, full-grid indices – the frame of saved trajectories – whateversubsetthe grid was loaded with.i2 (
int) – 1-based TRACMASS cell index ranges (inclusive). By default i and j are absolute, full-grid indices – the frame of saved trajectories – whateversubsetthe grid was loaded with.j1 (
int) – 1-based TRACMASS cell index ranges (inclusive). By default i and j are absolute, full-grid indices – the frame of saved trajectories – whateversubsetthe grid was loaded with.j2 (
int) – 1-based TRACMASS cell index ranges (inclusive). By default i and j are absolute, full-grid indices – the frame of saved trajectories – whateversubsetthe grid was loaded with.k1 (
int) – 1-based TRACMASS cell index ranges (inclusive). By default i and j are absolute, full-grid indices – the frame of saved trajectories – whateversubsetthe grid was loaded with.k2 (
int) – 1-based TRACMASS cell index ranges (inclusive). By default i and j are absolute, full-grid indices – the frame of saved trajectories – whateversubsetthe grid was loaded with.grid (
GridFieldsorNone, optional) – The run grid. Itsi_offset/j_offsetconvert absolute indices to the subset-relative ones particles use while integrating, cells outside the grid are dropped, and land cells (kmt == 0) are skipped. Without a grid the indices are used as given (no offset, no masking).use_subset_coordinates (
bool, optional) – If True, i/j are already subset-relative (cell 1 is the subset’s first cell) and no offset is applied. Default False. From settings:use_subset_coordinatesin[env.seed].**sub_kw – Sub-cell options (
sub_dist,i_sub_num, …).
- Returns:
ParticleState– Particles in subset-relative indices.- Raises:
ValueError – If grid is given and no cell of the box lies inside it.
- Return type:
Examples
>>> # glorys subset i1 = 2600, j1 = 500: absolute cells 2611..2621 >>> state = seed.ijk(2611, 2621, 511, 521, 1, 1, grid=grid) >>> # the same cells in subset coordinates >>> state = seed.ijk(11, 21, 11, 21, 1, 1, grid=grid, ... use_subset_coordinates=True)
- tracmass.seed.from_settings(grid=None, cfg=None)[source]#
Seed particles as described by the
[env.seed]settings table.- Parameters:
- Returns:
ParticleState- Raises:
ValueError – If there is no seed table, or its
approachis not a key ofSETTINGS_APPROACHES.- Return type:
Notes
The table names an
approachand gives its arguments in aboxsub-table; every other key (sub_dist,i_sub_num, …) is passed on as a sub-cell seeding option:[omi.seed] approach = "ijk" sub_dist = "rect_xyz" i_sub_num = 40 j_sub_num = 40 k_sub_num = 1 box = { i1 = 10, i2 = 20, j1 = 10, j2 = 20, k1 = 1, k2 = 1 }
i/j/kinboxare 1-based TRACMASS cell indices, withi/jabsolute (full-grid) unless the table setsuse_subset_coordinates = true(seeijk()). Withapproach = "latlon_box"the box is geographic instead (seelatlon_box()):box = { lat1 = -64, lat2 = -62, lon1 = -70, lon2 = -68, depth1 = 0, depth2 = 100 }
with
depth1/depth2depths in metres, andapproach = "latlon_point"takeslat,lon,dlat(and optionallydlon,depth1,depth2).approach = "latlon_list"seeds one particle per point, from a table file (seed_file = "points.csv"withlat/lonand optionallydepthcolumns) or inlinelat = [...]/lon = [...]/depth = [...]lists (seelatlon_list()).Examples
>>> tracmass.settings.setenv("omi") >>> state = seed.from_settings(grid)
- tracmass.seed.latlon_box(lat1, lat2, lon1, lon2, depth1=0.0, depth2=None, grid=None, *, gcm=None, **sub_kw)[source]#
Seed every ocean cell whose centre lies in a lat/lon/depth box.
- Parameters:
lat1 (
float) – Latitude range in degrees (inclusive).lat2 (
float) – Latitude range in degrees (inclusive).lon1 (
float) – Longitude range in degrees (inclusive), eastward from lon1 to lon2: any longitude convention works, andlon1 > lon2gives a box across the dateline (e.g.170→-170).lon2 (
float) – Longitude range in degrees (inclusive), eastward from lon1 to lon2: any longitude convention works, andlon1 > lon2gives a box across the dateline (e.g.170→-170).depth1 (
float, optional) – Depth range in metres below the surface (positive down); depth2 defaults to depth1. Every level of a column that overlaps the range is seeded, so a single depth (depth1 == depth2) picks the level that contains it. Defaultdepth1 = 0: the surface level. Levels below the local bottom are skipped.depth2 (
float, optional) – Depth range in metres below the surface (positive down); depth2 defaults to depth1. Every level of a column that overlaps the range is seeded, so a single depth (depth1 == depth2) picks the level that contains it. Defaultdepth1 = 0: the surface level. Levels below the local bottom are skipped.grid (
GridFields) – The run grid (required): selects the window of the GCM’s lon/lat and depths (for asubset) and itskmtmasks out land.gcm (
strorNone, optional) – Adapter providinglonlat_gridanddepth_grid. Default:[env.gcm] name.**sub_kw – Sub-cell options (
sub_dist,i_sub_num, …), seeijk().
- Returns:
ParticleState- Raises:
ValueError – If grid is missing, the GCM has no geographic grid,
depth1 > depth2, or the box contains no ocean cell of the grid.TypeError – If given the old arguments
k1/k2/kpos/z1/z2.
- Return type:
Examples
>>> state = seed.latlon_box(-64, -62, -70, -68, 0, 100, grid=grid, ... i_sub_num=10, j_sub_num=10)
From settings:
[omi.seed] approach = "latlon_box" i_sub_num = 10 box = { lat1 = -64, lat2 = -62, lon1 = -70, lon2 = -68, depth1 = 0, depth2 = 100 }
- tracmass.seed.latlon_point(lat, lon, dlat, dlon=None, depth1=0.0, depth2=None, grid=None, **kw)[source]#
Seed the ocean cells within dlat / dlon degrees of a point.
A
latlon_box()oflat ± dlatbylon ± dlon(dlon defaults to dlat) over depths depth1 to depth2 in metres (default: the surface level); other keywords (gcm, sub-cell options) are passed on.- Parameters:
- Return type:
- tracmass.seed.latlon_list(df=None, grid=None, *, seed_file=None, lat=None, lon=None, depth=None, lat_col='lat', lon_col='lon', depth_col='depth', gcm=None, **_ignored)[source]#
Seed one particle at each lat/lon/depth position of a table.
Each row becomes a particle at the exact fractional grid position of its point (not snapped to the cell centre), found with
tracmass.trajs.coords.lonlat_to_xy()on the adapter’slonlat_grid(grid), and at the fractional level of its depth in the adapter’sdepth_grid(grid). A point on land (or outside the grid) is moved to the centre of the nearest ocean cell, keeping its depth.- Parameters:
df (
pd.DataFrameorstrorNone) – Table with lat_col / lon_col columns (and optionally depth_col, the depth of each point in metres). A str is read as a file, like seed_file.grid (
GridFields) – The run grid (required): selects the GCM’s lon/lat and depth window (for asubset) and itskmtmasks out land.seed_file (
strorNone, optional) – Path to a.csvor.parquetfile to read df from.lat (
listoffloatorNone, optional) – Positions given directly instead of df / seed_file.lon (
listoffloatorNone, optional) – Positions given directly instead of df / seed_file.depth (
listoffloatorfloatorNone, optional) – Depth in metres below the surface (positive down), per point or one for all, used when the table has no depth_col column. Default0: at the surface.lat_col (
str, optional) – Column names. Defaults"lat","lon","depth".lon_col (
str, optional) – Column names. Defaults"lat","lon","depth".depth_col (
str, optional) – Column names. Defaults"lat","lon","depth".gcm (
strorNone, optional) – Adapter providinglonlat_gridanddepth_grid. Default:[env.gcm] name.**_ignored – Sub-cell options from a settings table are accepted and ignored: each point is seeded exactly once.
- Returns:
ParticleState– Particles in subset-relative coordinates, in row order (rows whose depth lies outside the column, above its top or at or below the local bottom, are dropped).- Raises:
ValueError – If grid or the positions are missing, a column is absent, or no point lies in an ocean cell deep enough for its depth.
TypeError – If given the old arguments
kpos/k_col/z.
- Return type:
Examples
>>> df = pd.DataFrame({"lat": [-63.0, -62.5], "lon": [-69.0, -68.2], ... "depth": [5.0, 50.0]}) >>> state = seed.latlon_list(df, grid=grid)
From settings:
[omi.seed] approach = "latlon_list" seed_file = "stations.csv" # columns lat, lon (and optionally depth)
or with the positions inline:
[omi.seed] approach = "latlon_list" lat = [-63.0, -62.5] lon = [-69.0, -68.2] depth = [5.0, 50.0]
- tracmass.seed.SETTINGS_APPROACHES = {'ijk': <function ijk>, 'latlon_box': <function latlon_box>, 'latlon_list': <function latlon_list>, 'latlon_point': <function latlon_point>}#
Seeding approaches usable from a
[env.seed]settings table, by name. Each takes the table’sboxentries and sub-cell keys as keyword arguments plusgrid=.
- tracmass.seed.mask(mask, kpos=1, **sub_kw)[source]#
Seed particles at every True cell of a raw
(j, i)-ordered mask.maskmust be in the grid’s native row-major orientation — the same orientation grid fields come in straight from xarray/NetCDF (e.g.seagrid.ecco2.grid_definition().kmt.valuesorseagrid.glorys’s rawkmt): latitude (j) first, longitude (i) second. This is the opposite ofGridFields.kmt’s(imt, jmt)convention — do not passgrid.kmthere directly; useijk()for that.- Parameters:
mask (
np.ndarray) – Boolean array, shape(jmt, imt)for a 2-D mask or(jmt, imt, kmt)for a 3-D one. True marks a cell to seed.kpos (
int, optional) – 1-based vertical level for seeded particles whenmaskis 2-D. Ignored (and superseded by the mask’s own k-axis) whenmaskis 3-D. Default 1.**sub_kw – Forwarded to
_expand_sub()(e.g.sub_dist,i_sub_num).
- Returns:
ParticleState– One particle per True cell, placed at the cell centre, at levelkpos(2-D mask) or the cell’s own k (3-D mask).- Warns:
UserWarning – If
mask.shape[0] > mask.shape[1](more j than i). Ocean grids are normally near-global in longitude but span at most the poles in latitude, sojmt > imtusually meansmaskwas passed already transposed to(imt, jmt)instead of the expected raw(jmt, imt).- Return type:
Examples
>>> kmt = seagrid.ecco2.grid_definition().kmt.values # (jmt, imt) >>> state = seed.mask(kmt > 0, kpos=1)