Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reinstate fit_extent_to_regis under a different name #260

Closed
OnnoEbbens opened this issue Sep 11, 2023 · 2 comments · Fixed by #266
Closed

reinstate fit_extent_to_regis under a different name #260

OnnoEbbens opened this issue Sep 11, 2023 · 2 comments · Fixed by #266

Comments

@OnnoEbbens
Copy link
Collaborator

The fit_extent_to_regis function resized a random extent in such a way that there will be an integer number of columns and cells given a certain cellsize. This is useful in case you want to quickly create a grid from a random extent.

I would propose to reinstate the function under the name snap_extent in de grid module.

The original fit_extent_to_regis is:

def fit_extent_to_regis(extent, delr, delc, cs_regis=100.,
                        verbose=False):
    """
    redifine extent and calculate the number of rows and columns.

    The extent will be redefined so that the borders os the grid (xmin, xmax, 
    ymin, ymax) correspond with the borders of the regis grid.

    Parameters
    ----------
    extent : list, tuple or np.array
        original extent (xmin, xmax, ymin, ymax)
    delr : int or float,
        cell size along rows, equal to dx
    delc : int or float,
        cell size along columns, equal to dy
    cs_regis : int or float, optional
        cell size of regis grid. The default is 100..

    Returns
    -------
    extent : list, tuple or np.array
        adjusted extent
    nrow : int
        number of rows.
    ncol : int
        number of columns.

    """
    extent = extent.copy()
    
    if verbose:
        print(f'redefining current extent: {extent}, fit to regis raster')

    for d in [delr, delc]:
        if float(d) not in [10., 20., 25., 50., 100., 200., 400., 500., 800.]:
            print(f'you probably cannot run the model with this '
                  f'cellsize -> {delc, delr}')

    # if extents ends with 50 do nothing, otherwise rescale extent to fit regis
    if extent[0] % cs_regis == 0 or not extent[0] % (0.5 * cs_regis) == 0:
        extent[0] -= extent[0] % 100
        extent[0] = extent[0] - 0.5 * cs_regis
    # get number of columns
    ncol = int(np.ceil((extent[1] - extent[0]) / delr))
    extent[1] = extent[0] + (ncol * delr)  # round x1 up to close grid

    # round y0 down to next 50 necessary for regis
    if extent[2] % cs_regis == 0 or not extent[2] % (0.5 * cs_regis) == 0:
        extent[2] -= extent[2] % 100
        extent[2] = extent[2] - 0.5 * cs_regis
    nrow = int(np.ceil((extent[3] - extent[2]) / delc))  # get number of rows
    extent[3] = extent[2] + (nrow * delc)  # round y1 up to close grid

    if verbose:
        print(
            f'new extent is {extent} model has {nrow} rows and {ncol} columns')

    return extent, nrow, ncol
@rubencalje
Copy link
Collaborator

I think snap_extent would be a good name, and maybe return only the extent, and not nrow and ncol anymore.

OnnoEbbens added a commit that referenced this issue Oct 10, 2023
@OnnoEbbens OnnoEbbens linked a pull request Oct 10, 2023 that will close this issue
OnnoEbbens added a commit that referenced this issue Oct 18, 2023
* fix for #260

* align netcdf4 version with ci and rtd

* snap to cell edge and not cell center
@rubencalje
Copy link
Collaborator

Fixed by @OnnoEbbens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants