-
Notifications
You must be signed in to change notification settings - Fork 1
Replace standardise_mom6_filenames.sh with a new script #133
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
dougiesquire
wants to merge
2
commits into
main
Choose a base branch
from
mom-filenames
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.
+245
−233
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright 2024 ACCESS-NRI and contributors. See the top-level COPYRIGHT file for details. | ||
| # SPDX-License-Identifier: Apache-2.0. | ||
| # | ||
| # Standardise file naming for MOM output files in access-om by removing the underscore | ||
| # before date/time suffixes and replacing subsequent underscores with hyphens, e.g., | ||
| # replacing '_YYYY_MM' with 'YYYY-MM'. | ||
| # This was written assuming it would be used as a payu "userscript" at the "archive" stage, | ||
| # but alternatively a path to an "archive" directory can be provided. | ||
| # For more details, see https://github.com/COSIMA/om3-scripts/issues/32 | ||
|
|
||
| import argparse | ||
| import glob | ||
| import os | ||
| import re | ||
| import sys | ||
|
|
||
|
|
||
| def standardised_filename(path): | ||
| """Return the standardised filename for a MOM output file, or None if no rename is needed. | ||
|
|
||
| Files ending in *_<digits>[_<digits>...].nc[...] are renamed so that the first | ||
| underscore before the digit groups is removed and subsequent ones are replaced with '-'. | ||
| For example: | ||
| file._2023.nc -> file.2023.nc | ||
| file._2023_01.nc -> file.2023-01.nc | ||
| file._2023_01_15.nc -> file.2023-01-15.nc | ||
| """ | ||
| dirpath = os.path.dirname(path) | ||
| basename = os.path.basename(path) | ||
|
|
||
| # Split the .nc extension | ||
| ext_match = re.match(r"^(.*?)(\.nc.*)$", basename) | ||
| if not ext_match: | ||
| return None | ||
| stem, ext = ext_match.groups() | ||
|
|
||
| # Match the trailing block of underscore-separated digit groups | ||
| suffix_match = re.match(r"^(.*?)(_\d+(?:_\d+)*)$", stem) | ||
| if not suffix_match: | ||
| return None | ||
| base, suffix = suffix_match.groups() | ||
|
|
||
| # Remove the leading underscore and replace any remaining ones with '-' | ||
| new_suffix = suffix[1:].replace("_", "-") | ||
|
|
||
| new_basename = base + new_suffix + ext | ||
| if new_basename == basename: | ||
| return None | ||
|
|
||
| return os.path.join(dirpath, new_basename) | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser( | ||
| description="Standardise file naming for MOM output files." | ||
| ) | ||
| parser.add_argument( | ||
| "-d", | ||
| metavar="DIRECTORY", | ||
| dest="out_dir", | ||
| help="Process files in the specified experiment 'DIRECTORY'.", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| if args.out_dir: | ||
| if not os.path.isdir(args.out_dir): | ||
| print(f"{args.out_dir} does not exist") | ||
| sys.exit(1) | ||
| out_dirs = [args.out_dir] | ||
| else: | ||
| out_dirs = sorted(glob.glob("archive/output*"), reverse=True) | ||
|
|
||
| # Support ACCESS-OM3 and ACCESS-OM2 output files | ||
| file_patterns = [ | ||
| "access-om3.mom6.*.nc*", | ||
| "ocean/access-om2.mom5.*.nc*", | ||
| ] | ||
|
|
||
| for dir_path in out_dirs: | ||
| for pattern in file_patterns: | ||
| for current_file in glob.glob(f"{dir_path}/{pattern}"): | ||
| if not os.path.isfile(current_file): | ||
| continue | ||
| new_file = standardised_filename(current_file) | ||
| if new_file is None: | ||
| continue | ||
| if os.path.exists(new_file): | ||
| print(f"Skipping {current_file}: {new_file} already exists") | ||
| continue | ||
| os.rename(current_file, new_file) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the matches against things where there is anything(
.*) after.nc, is that needed ?Probably harmless even if not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we also want to match uncollated files e.g.
*.nc.0001