-
Notifications
You must be signed in to change notification settings - Fork 0
Fvcom reader to handle non-geographic coordinates #36
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
Open
mikebedington
wants to merge
7
commits into
main
Choose a base branch
from
fvcom_reader_cartesian
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd0cae6
Add reading cartesian coordinate files to FVCOM reader
bcd8a30
Add error handling for missing CoordinateProjection code
6fea554
Slightly cleaner way of handling the projection code in fvcom_reader
01a579c
Raise error if cartesian coordinates and no projection code available
ccb1846
Merge branch 'main' into fvcom_reader_cartesian
wathen 53a3d3c
Merge branch 'main' into fvcom_reader_cartesian
wathen 0af37e7
Bug fix, missing return argument type
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,11 +30,13 @@ class FVCOMReader: | |
| DAYS_PER_MILLISECOND = 1.0 / (1000.0 * 60.0 * 60.0 * 24.0) | ||
|
|
||
| def __init__(self, | ||
| file_paths: Union[str, List[str]]): | ||
| file_paths: Union[str, List[str]], | ||
| projection: str=None): | ||
| """Initialize the FVCOMReader with the path to the netCDF file. | ||
|
|
||
| Args: | ||
| file_paths (str, list): Path to the FVCOM netCDF file. | ||
| projection (str, optional): EPSG projection code, as either EPSG:xxxx or xxxx | ||
| """ | ||
| # Handle single file path or list of file paths | ||
| if isinstance(file_paths, str): | ||
|
|
@@ -45,13 +47,18 @@ def __init__(self, | |
| # Load only the first file initially for metadata and time-independent data | ||
| print(f'Accessing FVCOM metadata from: {self.file_paths[0]}') | ||
| self._metadata_dataset = Dataset(self.file_paths[0]) | ||
|
|
||
| self._grid = None # Lazy initialization | ||
| self._z_level_cache = {} # Cache for time-dependent z levels | ||
|
|
||
| # Build the time index mapping for multiple files | ||
| self._build_time_index_mapping() | ||
|
|
||
| if projection is None and hasattr(self._metadata_dataset,'CoordinateProjection'): | ||
| self._projection = self._metadata_dataset.CoordinateProjection.split(':')[-1] | ||
| else: | ||
| self._projection = projection.split(':')[-1] | ||
|
|
||
|
|
||
| def _build_time_index_mapping(self): | ||
| """Build a mapping from datetime to (file_path, local_time_index)""" | ||
| self._time_to_file_map = {} | ||
|
|
@@ -142,9 +149,9 @@ def grid(self) -> Grid: | |
| Grid: The grid object containing mesh structure and open boundaries. | ||
| """ | ||
| if self._grid is None: | ||
| mesh_data = self._extract_mesh_data() | ||
| mesh_data, coordinate_system = self._extract_mesh_data() | ||
| sigma_data = self._extract_sigma_data() | ||
| self._grid = Grid(mesh_data, sigma_data, "geographic") | ||
| self._grid = Grid(mesh_data, sigma_data, coordinate_system, epsg_code=self._projection) | ||
| return self._grid | ||
|
|
||
| @property | ||
|
|
@@ -450,7 +457,7 @@ def get_interpolation_coordinates(self, horizontal_position: str, | |
| vertical_coordinate_system=vertical_coordinate_system, | ||
| dates=dates) | ||
|
|
||
| def _extract_mesh_data(self) -> MeshData: | ||
| def _extract_mesh_data(self) -> tuple[MeshData, str]: | ||
| """Extract mesh data from FVCOM output file. | ||
|
|
||
| Returns: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also update the docstring for this output |
||
|
|
@@ -459,14 +466,26 @@ def _extract_mesh_data(self) -> MeshData: | |
| # Extract basic mesh components | ||
| nodes = np.arange(1, self._metadata_dataset.dimensions['node'].size+1) # TBC zero based indexing kept here. | ||
| triangles = self._metadata_dataset.variables['nv'][:].T - 1 # Convert to 0-based indexing, transpose to (n_elem, 3) | ||
| x1 = self._metadata_dataset.variables['lon'][:] | ||
| x2 = self._metadata_dataset.variables['lat'][:] | ||
| x3 = self._return_grid_variable_data('h')[:] | ||
|
|
||
| if getattr(self._metadata_dataset, 'CoordinateSystem', None) == 'Cartesian': | ||
| x1 = self._metadata_dataset.variables['x'][:] | ||
| x2 = self._metadata_dataset.variables['y'][:] | ||
| if self._projection is not None: | ||
| # This assumes the coordinate projection in the netCDF is either EPSG:xxxxx or just xxxxxx | ||
| coordinate_system = 'cartesian' | ||
| else: | ||
| raise PyFVCOM2ValueError(f"Cartesian coordinates specificed but no CoordinateProjection provided in file or passed") | ||
|
|
||
| else: | ||
| x1 = self._metadata_dataset.variables['lon'][:] | ||
| x2 = self._metadata_dataset.variables['lat'][:] | ||
| coordinate_system = 'geographic' | ||
|
|
||
| x3 = self._return_grid_variable_data('h')[:] | ||
| open_bdy_node_lists = None | ||
| bdy_types = None | ||
|
|
||
| return MeshData(triangles, nodes, x1, x2, x3, bdy_types, open_bdy_node_lists) | ||
| return MeshData(triangles, nodes, x1, x2, x3, bdy_types, open_bdy_node_lists), coordinate_system | ||
|
mikebedington marked this conversation as resolved.
|
||
|
|
||
| def _extract_sigma_data(self) -> SigmaData: | ||
| """Extract sigma coordinate data from FVCOM output file. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.