Skip to content

pybdy package

Subpackages

Submodules

pybdy.nemo_bdy_chunk module

Created on Thu Dec 19 10:39:46 2024.

@author James Harle @author Benjamin Barton

pybdy.nemo_bdy_chunk.chunk_bdy(bdy)

Master chunking function.

Takes the boundary indicies and turns them into a list of boundary chunks. The boundary is first split at natural breaks like land or the east-west wrap. The chunks are then split near corners. The chunks are then optionally split in the middle if they’re above a certain size after attempting to split at corners.

Parameters

bdy (obj) : organised as bdy_i[point, i/j grid] and rim width bdy_r[point]
logger : log error and messages

Returns

chunk_number (numpy.array) : array of chunk numbers

pybdy.nemo_bdy_chunk.chunk_corner(ibdy, jbdy, rbdy, chunk_number, rw)

Find corners and split along the change in direction.

To do this we look for a change in direction along each rim.

Parameters

ibdy (numpy.array) : index in i direction
jbdy (numpy.array) : index in j direction
rbdy (numpy.array) : rim index
chunk_number (numpy.array) : array of chunk numbers. -1 means an unassigned chunk number
rw (int) : rimwidth

Returns

chunk_number (numpy.array) : array of chunk numbers

pybdy.nemo_bdy_chunk.chunk_land(ibdy, jbdy, chunk_number, rw)

Find natural breaks in the boundary looking for gaps in i and j.

Parameters

ibdy (numpy.array) : index in i direction
jbdy (numpy.array) : index in j direction
chunk_number (numpy.array) : array of chunk numbers. -1 means an unassigned chunk number
rw (int) : rimwidth

Returns

chunk_number (numpy.array) : array of chunk numbers

pybdy.nemo_bdy_chunk.chunk_large(ibdy, jbdy, chunk_number)

Split boundaries that have too much white space and are too large.

Parameters

ibdy (numpy.array) : index in i direction
jbdy (numpy.array) : index in j direction
chunk_number (numpy.array) : array of chunk numbers. -1 means an unassigned chunk number

Returns

chunk_number (numpy.array) : array of chunk numbers

pybdy.nemo_bdy_dst_coord module

class pybdy.nemo_bdy_dst_coord.DstCoord

Bases: object

Object is currently empty and has data bound to it externally.

Equivalent to Matlab dst_coord.

pybdy.nemo_bdy_extr_assist module

Created on Thu Dec 21 17:34:00 2024.

@author James Harle @author Benjamin Barton

pybdy.nemo_bdy_extr_assist.check_wrap(imin, imax, sc_lon)

Check if source domain wraps and dst spans the wrap.

Parameters

imin (int) : minimum i index
imax (int) : maximum i index
sc_lon (np.array) : the longitude of the source grid

Returns

wrap_flag (bool) : if true the sc wraps and dst spans wrap

pybdy.nemo_bdy_extr_assist.distance_weights(sc_bdy, dist_tot, sc_z_len, r0, logger)

Find the distance weightings for averaging source data to destination.

Parameters

sc_bdy (numpy.array) : source data
dist_tot (numpy.array) : distance from dst point to 9 nearest sc points
sc_z_len (int) : the number of depth levels
r0 (float) : correlation distance
logger : log of statements

Returns

dist_wei (numpy.array) : weightings for averaging
dist_fac (numpy.array) : total weighting factor

pybdy.nemo_bdy_extr_assist.flood_fill(sc_bdy, isslab, logger)

Fill the data horizontally then downwards to remove nans before interpolation.

Parameters

sc_bdy (np.array) : souce data [nz_sc, nbdy, 9]
isslab (bool) : if true data has vertical cells for vertical flood fill
logger : log of statements

Returns

sc_bdy (np.array) : souce data [nz_sc, nbdy, 9]

pybdy.nemo_bdy_extr_assist.get_ind(dst_lon, dst_lat, sc_lon, sc_lat)

Calculate indicies of max and min for data extraction.

Parameters

dst_lon (np.array) : the longitude of the destination grid
dst_lat (np.array) : the latitude of the destination grid
sc_lon (np.array) : the longitude of the source grid
sc_lat (np.array) : the latitude of the source grid

Returns

imin (int) : minimum i index
imax (int) : maximum i index
jmin (int) : minimum j index
jmax (int) : maximum j index

pybdy.nemo_bdy_extr_assist.get_vertical_weights(dst_dep, dst_len_z, num_bdy, sc_z, sc_z_len, ind, zco)

Determine 3D depth vertical weights for the linear interpolation onto Dst grid.

Selects 9 source points horizontally around a destination grid point. Calculated the distance from each source point to the destination to be used in weightings. The resulting arrays are [nz * nbdy * 9, 2].

Parameters

dst_dep (np.array) : the depth of the destination grid chunk [nz, nbdy]
dst_len_z (int) : the length of depth axis of the destination grid
num_bdy (int) : number of boundary points in chunk
sc_z (np.array) : the depth of the source grid [k, j, i]
sc_z_len (int) : the length of depth axis of the source grid
ind (np.array) : indices of bdy and 9 nearest neighbours flattened “F” j,i [nbdy, 9]
zco (bool) : if True z levels are not spatially varying

Returns

z9_dist (np.array) : the distance weights of the selected points
z9_ind (np.array) : the indices of the sc depth above and below bdy

pybdy.nemo_bdy_extr_assist.get_vertical_weights_zco(dst_dep, dst_len_z, num_bdy, sc_z, sc_z_len)

Determine vertical weights for the linear interpolation onto Dst grid.

Calculated the vertical distance from each source point to the destination to be used in weightings. The resulting arrays are [nbdy * nz, 2].

Note: z_dist and z_ind are [nbdy*nz, 2] where [:, 0] is the nearest vertical index
and [:, 1] is the index above or below i.e. the vertical index -1 for sc_z > dst_z
and vertical index +1 for sc_z \<= dst_z

Parameters

dst_dep (np.array) : the depth of the destination grid chunk [nz, nbdy]
dst_len_z (int) : the length of depth axis of the destination grid
num_bdy (int) : number of boundary points in chunk
sc_z (np.array) : the depth of the source grid [k, j, i]
sc_z_len (int) : the length of depth axis of the source grid

Returns

z_dist (np.array) : the distance weights of the selected points
z_ind (np.array) : the indices of the sc depth above and below bdy

pybdy.nemo_bdy_extr_assist.interp_horizontal(sc_bdy, dist_wei, dist_fac, logger)

Interpolate the source data to the destination grid using weighted average.

Parameters

sc_bdy (numpy.array) : source data
dist_wei (numpy.array) : weightings for interpolation
dist_fac (numpy.array) : sum of weightings
logger : log of statements

Returns

dst_bdy (numpy.array) : destination bdy points with data from source grid

pybdy.nemo_bdy_extr_assist.interp_vertical(sc_bdy, dst_dep, bdy_bathy, z_ind, z_dist, num_bdy, zinterp=True)

Interpolate source data onto destination vertical levels.

Parameters

sc_bdy (np.array) : souce data [nz_sc, nbdy, 9]
dst_dep (np.array) : the depth of the destination grid chunk [nz, nbdy]
bdy_bathy (np.array): the destination grid bdy points bathymetry
z_ind (np.array) : the indices of the sc depth above and below bdy
z_dist (np.array) : the distance weights of the selected points
num_bdy (int) : number of boundary points in chunk
zinterp (bool) : vertical interpolation flag

Returns

data_out (np.array) : source data on destination depth levels

pybdy.nemo_bdy_extr_assist.valid_index(sc_bdy, logger)

Find an array of valid indicies.

Parameters

sc_bdy (numpy.array) : source data
logger : log of statements

Returns

data_ind (numpy.array) : indicies of max depth of valid data
nan_ind (numpy.array) : indicies where source is land

pybdy.nemo_bdy_extr_tm3 module

Created on Wed Sep 12 08:02:46 2012.

This Module defines the extraction of the data from the source grid and does the interpolation onto the destination grid.

@author James Harle @author John Kazimierz Farey

@author: Mr. Srikanth Nagella

class pybdy.nemo_bdy_extr_tm3.Extract(setup, SourceCoord, DstCoord, Grid, var_nam, grd, pair)

Bases: object

method __init__(setup, SourceCoord, DstCoord, Grid, var_nam, grd, pair)

Initialise the Extract object.

Parent grid to child grid weights are defined along with rotation weightings for vector quantities.

Parameters

setup (list) : settings for bdy
SourceCoord (obj) : source grid information
DstCoord (obj) : destination grid information
Grid (dict) : containing grid type ‘t’, ‘u’, ‘v’ and source time
var_name (list) : netcdf file variable names (str)
years (list) : years to extract (default [1979])
months (list) : months to extract (default [11])

Returns

Extract (obj) : Object with indexing arrays and weightings ready for interpolation

method cal_trans(source, dest, year, month)

Translate between calendars and return scale factor and number of days in month.

Parameters

source : source calendar
dest : destination calendar
year : input year
month : input month

Returns

sf : scale factor
ed : number of days in month

method extract_month(year, month)

Extract monthly data and interpolates onto the destination grid.

Parameters

year : year of data to be extracted
month : month of the year to be extracted

Returns

self.data_out : data from source on bdy locations and depths

method time_delta(time_counter)

Get time delta and number of time steps per day.

Calculates difference between time steps for time_counter and checks these are uniform. Then retrives the number of time steps per day.

Parameters

time_counter : model time coordinate

Returns

deltaT : length of time step
dstep : number of time steps per day

method time_interp(year, month)

Perform a time interpolation of the BDY data to daily frequency.

This method performs a time interpolation (if required). This is necessary if the time frequency is not a factor of monthly output or the input and output calendars differ. CF compliant calendar options

accepted: gregorian | standard, proleptic_gregorian, noleap | 365_day,
360_day or julian.*

method write_out(year, month, ind, unit_origin)

Write monthy BDY data to netCDF file.

This method writes out all available variables for a given grid along with any asscoaied metadata. Currently data are only written out as monthly files.

Parameters

year (int) : year to write out
month (int) : month to write out
ind (dict): dictionary holding grid information
unit_origin (str) : time reference ‘%d 00:00:00’ %date_origin

Returns

None

pybdy.nemo_bdy_gen_c module

NEMO Boundary module.

Creates indices for t, u and v points, plus rim gradient. The variable names have been renamed to keep consistent with python standards and generalizing the variable names eg. bdy_i is used instead of bdy_t

Ported from Matlab code by James Harle

@author: John Kazimierz Farey
@author: Srikanth Nagella.

class pybdy.nemo_bdy_gen_c.Boundary(boundary_mask, settings, grid)

Bases: object

Class for boundary definitions.

method __init__(boundary_mask, settings, grid)

Generate the indices for NEMO Boundary and returns a Grid object with indices.

Parameters

boundary_mask : boundary mask
settings : dictionary of setting values
grid : type of the grid ‘t’, ‘u’, ‘v’

Returns

Boundary (object) : where bdy_i is index and bdy_r is the r index

method fill(mask, ref, brg)

method find_bdy(igrid, jgrid, mask, brg)

Find the border indexes by checking the change from ocean to land.

Returns the i and j index array where the shift happens.

Parameters

igrid : I x direction indexes
jgrid : J y direction indexes
mask : mask data
brg : mask index range

Returns

bdy_i : bdy indexes
bdy_r : bdy rim values.

method remove_duplicate_points(bdy_i, bdy_r)

Remove the duplicate points in the bdy_i and return the bdy_i and bdy_r.

Parameters

bdy_i : bdy indexes
bdy_r : bdy rim values.

Returns

bdy_i : bdy indexes
bdy_r : bdy rim values.

method remove_landpoints_open_ocean(mask, bdy_i, bdy_r)

Remove the land points and open ocean points.

method unique_rows(t)

Find indexes of unique rows in the input 2D array.

Parameters

t : input 2D array.

Returns

indx : indexes of unique rows

pybdy.nemo_bdy_grid_angle module

class pybdy.nemo_bdy_grid_angle.GridAngle(hgr, imin, imax, jmin, jmax, cd_type)

Bases: object

Class to get orientation of grid from I and J offsets for different grid types.

method __init__(hgr, imin, imax, jmin, jmax, cd_type)

Get sin and cosin files for the grid angle from North.

Parameters

hgr : grid object
imin : minimum model zonal indices
imax : maximum model zonal indices
jmin : minimum model meridional indices
jmax : maximum model meridional indices
cd_type: define the nature of pt2d grid points

Returns

None : object

method get_lam_phi(map=False, i=0, j=0, single=False)

Get lam/phi in (offset) i/j range for init grid type.

Data must be converted to float64 to prevent dementation of later results.

method get_north_dir()

Find North pole direction and modulus of some point.

method get_seg_dir(north_n)

Find segmentation direction of some point.

method get_sin_cos(nx, ny, nn, sx, sy, sn)

Get sin and cos from lat and lon using using scaler/vectorial products.

method trig_eq(x, eq, z_one, z_two)

Calculate long winded equation of two vars; some lam and phi.

pybdy.nemo_bdy_ice module

class pybdy.nemo_bdy_ice.BoundaryIce

Bases: object

method __init__()

pybdy.nemo_bdy_ncgen module

Create a Nemo Bdy netCDF file ready for population.

Written by John Kazimierz Farey, started August 30, 2012 Port of Matlab code of James Harle

pybdy.nemo_bdy_ncgen.CreateBDYNetcdfFile(filename, xb_len, x_len, y_len, depth_len, rw, h, orig, fv, calendar, grd)

Create a template of bdy netcdf files. A common for T, I, U, V, E grid types.

pybdy.nemo_bdy_ncpop module

Created on 3 Oct 2014.

@author: Mr. Srikanth Nagella
Netcdf writer for the bdy output

pybdy.nemo_bdy_ncpop.write_data_to_file(filename, variable_name, data)

Write the data to the netcdf templete file.

Parameters

filename – output filename variable_name – variable name into which the data is written to. data – data that will be written to variable in netcdf.

pybdy.nemo_bdy_scr_coord module

class pybdy.nemo_bdy_scr_coord.ScrCoord

Bases: object

method __init__()

pybdy.nemo_bdy_setup module

Created on Wed Sep 12 08:02:46 2012.

Parses a file to find out which nemo boundary settings to use

@author John Kazimierz Farey @author James Harle

class pybdy.nemo_bdy_setup.Setup(setfile)

Bases: object

Invoke with a text file location, class init reads and deciphers variables.

This class holds the settings information

method __init__(setfile)

Set up the constructor.

This constructor reads the settings file and sets the dictionary with setting name/key and it’s value.

Parameters

setfile (str) : settings file

method refresh()

Reload the settings from file.

method variable_info_reader(filename)

Read the variable description data from the ‘variable.info’ file.

This method reads the variable description data from ‘variable.info’ file in the pybdy installation path if it can’t find the file with the same name as input bdy file with extension .info

Parameters

filename – filename of the variables information returns a dictionary with variable name and its description.

Returns

variable_info : dict

method write()

Write backs the variable data back into the file.

pybdy.nemo_bdy_setup.strip_comments(line)

Strip the comments in the line. removes text after !.

pybdy.nemo_bdy_source_coord module

class pybdy.nemo_bdy_source_coord.SourceCoord

Bases: object

method __init__()

Initialise the source coordinates attributes of the object.

pybdy.nemo_bdy_zgrv2 module

Created.

@author John Kazimierz Farey @author Benjamin Barton.

pybdy.nemo_bdy_zgrv2.get_bdy_depths(DstCoord, bdy_i, grd)

Depth levels from the nearest neighbour on the source grid.

Parameters

DstCoord (object) : Object containing destination grid info
bdy_i (np.array) : indices of the i, j bdy points [bdy, 2]
grd (str) : grid type t, u, v

Returns

bdy_tz (array) : sc depths on bdy points on t levels
bdy_wz (array) : sc depths on bdy points on w levels
bdy_e3 (array) : sc level thickness on bdy points on t levels

pybdy.nemo_bdy_zgrv2.get_bdy_depths_old(bdy_t, bdy_u, bdy_v, DstCoord, settings)

Generate Depth information.

Written by John Kazimierz Farey, Sep 2012 Port of Matlab code of James Harle

Generates depth points for t, u and v in one loop iteration. Initialise with bdy t, u and v grid attributes (Grid.bdy_i) and settings dictionary.

pybdy.nemo_bdy_zgrv2.get_bdy_sc_depths(SourceCoord, DstCoord, grd)

Depth levels from the nearest neighbour on the source grid.

Parameters

SourceCoord (object) : Object containing source grid info
DstCoord (object) : Object containing destination grid info
grd (str) : grid type t, u, v

Returns

bdy_tz (array) : sc depths on bdy points on t levels
bdy_wz (array) : sc depths on bdy points on w levels
bdy_e3 (array) : sc level thickness on bdy points on t levels

pybdy.nemo_coord_gen_pop module

Module that combines matlab coord gen and pop.

Initialise with netcdf file name and dictionary containing all bdy grids (objects).

class pybdy.nemo_coord_gen_pop.Coord(fname, bdy_ind)

Bases: object

Class for writing boundayr coordinate data to netcdf file.

method __init__(fname, bdy_ind)

Create Nemo bdy indices for t, u, v points.

Parameters

fname (str) : file name of coords file for output
bdy_ind (numpy.array) : indicies of bdy points

Returns

None : object

method add_vars(dim, grd, unt)

Create a var w/ attributes.

method build_dict(dim, units)

Set up a grid dictionary.

method closeme()

method create_dims()

Create dims and returns a dictionary of them.

method populate(hgr)

Populate the file with indices, lat, lon, and e dimensions.

method set_lenvar(vardic, hgr=None, unt=None)

Set the len var of each array in the var dictionary.

Use by specifying hgr and unt which pulls data from loaded grid data. Otherwise pull it from the class dict.

pybdy.profiler module

Created on Wed Sep 12 08:02:46 2012.

The main application script for the NRCT.

@author James Harle @author John Kazimierz Farey @author Srikanth Nagella

class pybdy.profiler.Grid

Bases: object

A Grid object that stores bdy grid information.

method __init__()

pybdy.profiler.process_bdy(setup_filepath=0, mask_gui=False)

Handle all the calls to generate open boundary conditions for a given regional domain.

This is main entry for processing BDY lateral boundary conditions. This is the main script that handles all the calls to generate open boundary conditions for a given regional domain. Input options are handled in a NEMO style namelist (namelist.bdy). There is an optional GUI allowing the user to create a mask that defines the extent of the regional model.

Parameters

setup_filepath (str) : file path to find namelist.bdy
mask_gui (bool): whether use of the GUI is required

Returns

None : bdy data is written out to NetCDF file

pybdy.profiler.write_tidal_data(setup_var, dst_coord_var, grid, tide_cons, cons)

Write the tidal data to a NetCDF file.

Parameters

setup_var (list): Description of arg1
dst_coord_var (obj) : Description of arg1
grid (dict): Description of arg1
tide_cons (list): Description of arg1
cons (data): cosz, sinz, cosu, sinu, cosv, sinv

Returns

None : tidal data is written to NetCDF file

pybdy.pybdy_exe module

Entry for the project.

@author: Mr. Srikanth Nagella

pybdy.pybdy_exe.main()

Run main function.

Checks the command line parameters and passes them to the profile module for processing.

pybdy.pybdy_ncml_generator module

Created on 2 Jul 2015.

The main application object for hosting the pybdy ncml editor. Used for development purposes to display the ncml editor dialog.

@author: Shirley Crompton, UK Science and Technology Facilities Council

pybdy.pybdy_ncml_generator.main()

Command line execution method.

Checks the input arguments and passes on to method to open the ncml generator window.

pybdy.pybdy_settings_editor module

Created on 7 Jan 2015.

@author: Mr. Srikanth Nagella

pybdy.pybdy_settings_editor.main()

Command line execution method.

Checks the input arguments and passes on to method to open the settings window.

pybdy.pybdy_settings_editor.open_settings_dialog(setup)

Start the settings window using the setup settings provided in the input.

On clicking the cancel button it doesn’t shutdown the applicaiton but carries on with the execution.

pybdy.pybdy_settings_editor.open_settings_window(fname)

Start a Qt application.

This method gives the user the option to pick a namelist.bdy file to edit. Once user selects it it will open a dialog box where users can edit the parameters.

pybdy.version module

Module contents

a Python based regional NEMO model configuration toolbox.