Skip to content

Conversation

einola
Copy link
Member

@einola einola commented Oct 8, 2025

Improved scripts to create initial conditions files

Fixes #945

Task List

  • Defined the tests that specify a complete and functioning change (It may help to create a design specification & test specification)
  • Implemented the source code change that satisfies the tests
  • Documented the feature by providing worked example
  • Updated the README or other documentation
  • Completed the pre-Request checklist below

Change Description

Adds the functionality to make_init_base.py to make generic geographic grids, in addition to idealised ones. The user must supply lat/lon coordinates of the grid and may provide a mask and initial fields. The program can create nextsim grids from a grid of lat/lon coordinates or lat/lon coordinates of a NEMO C-grid (which also provides coordinates of the upper right corner of the grid box).

Usage:
 0. Import make_init_base
  >> from make_init_base import initMaker
 1. Create an initialiser object with a given filename, e.g.
  >> init = initMaker("test")
     2a. Create a cartesian grid from dimensions and resolution, e.g.
      >> init.make_cartesian_grid(128, 128, 3e3)
     2b. Create a geographic grid from a file containing coordinates. We need to define type of grid:
        * "p": only centre points of the grid are given
        * "ur": the centre and upper right corner of the grid is given
        * "ll": the centre and lower left corner of the grid is given
      init.make_geographic_grid("coordinates.nc", "ur", plat_name="gphit", plon_name="glamt", qlat_name="gphif", qlon_name="glamf")
 3. Modify any variables needed, e.g.
  >> init.cice = 1
  >> init.hice = 3
  >> init.hice[10:20, 10:20] = 4
 4. The init file is written when the initialiser object goes out of scope (e.g. when the program ends or at the end of a loop).

I also modified and renamed make_initNH.py to use make_init_base.py


Test Description

I used the script to recreate the grid of the integration test, as well as the grid from our NEMO-coupled setup and a few others. All of which run successfully.


Documentation Impact

The functionality of make_init_base.py should be documented in the usage section of the manual, but it is not.


Other Details

N/A


Pre-Request Checklist

  • The requirements of this pull request are fully captured in an issue or design specification and are linked and summarised in the description of this PR
  • No new warnings are generated
  • The documentation has been updated (or an issue has been created to track the corresponding change)
  • Methods and Tests are commented such that they can be understood without having to obtain additional context
  • This PR/Issue is labelled as a bug/feature/enhancement/breaking change
  • This change conforms to the conventions described in the README

Just rewrite it a bit so that you have to call either
make_cartesian_grid or make_geographic_grid before setting your
variables. Only make_cartesian_grid implemented so far.
make_init_base.py can now handle NEMO files and produce vertex
coordinates and the grid azimuth angle on a displaced pole.
Doc strings and miscellaneous cleaning. I also added two functions to
get the grid centre longitude and latitude.
The script is now much shorter and also more generalised. But it expects
 the grid file to follow NEMO naming conventions for the coordinates.
This belongs with the commit before last.
make_init_base.py can now handle grids that only have lat/lon
information at the grid cell centre. I do this by assuming everything is
regular and that the vertices are +/- dx and dy away from the grid cell
centre. I also updated make_init_geographic.py to be able to handle such
grids.
The grid is up-side-down and we also need the reverse of the angle. Two
minuses that make a plus, but now it's clearer why it all works (I
hope).
The velocities need to have zeros on land, but the rest can have missing
values.
einola added 3 commits October 8, 2025 08:06
# Conflicts:
#	run/interpolators.py
#	run/make_initNH.py
#	run/make_init_base.py
I did "ruff format --config ruff.toml ./" in the project base directory.
This modified a lot of files, but I just picked the ones relevant for
this PR.
This time, I did "ruff check --diff ./ > patch.diff" in the run
directory, followed by "git apply patch.diff". That gave a different
result to "ruff format".
.
@einola
Copy link
Member Author

einola commented Oct 8, 2025

I got into a fight with Ruff, and this resulted in a lot of unrelated formatting changes - sorry.

I won over Ruff in the end!

einola added 3 commits October 8, 2025 08:43
This reverts commit 6ec6694.
by doing "ruff check --diff ./ > patch.diff" in the run directory and
then "git apply patch.diff".
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 this pull request may close these issues.

Improve the initfile creation for geographic grids

1 participant