From 807a0539db4433533300266519cf2e220ed41b90 Mon Sep 17 00:00:00 2001 From: minghangli-uni Date: Mon, 9 Sep 2024 09:30:20 +1000 Subject: [PATCH] Add standardise_mom6_filenames.sh script (#32) --- payu_config/archive.sh | 1 + .../standardise_mom6_filenames.sh | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100755 payu_config/archive_scripts/standardise_mom6_filenames.sh diff --git a/payu_config/archive.sh b/payu_config/archive.sh index b5d3ff4..f098f15 100644 --- a/payu_config/archive.sh +++ b/payu_config/archive.sh @@ -2,4 +2,5 @@ source $(dirname "$0")/archive_scripts/archive_cice_restarts.sh source $(dirname "$0")/archive_scripts/concat_ice_daily.sh +source $(dirname "$0")/archive_scripts/standardise_mom6_filenames.sh python3 $(dirname "$0")/archive_scripts/build_intake_ds.py diff --git a/payu_config/archive_scripts/standardise_mom6_filenames.sh b/payu_config/archive_scripts/standardise_mom6_filenames.sh new file mode 100755 index 0000000..8c1e0bf --- /dev/null +++ b/payu_config/archive_scripts/standardise_mom6_filenames.sh @@ -0,0 +1,53 @@ +#!/usr/bin/bash +# Copyright 2024 ACCESS-NRI and contributors. See the top-level COPYRIGHT file for details. +# SPDX-License-Identifier: Apache-2.0. +# +# Standardise file naming for MOM6 output files from access-om3. +# 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 + +Help() +{ + # Display help + echo -e "Standardise file naming for MOM6 output files.\n" + echo "Syntax: scriptTemplate [-h|d DIRECTORY]" + echo "options:" + echo "h Print this help message." + echo -e "d Process files in the specified 'DIRECTORY'." +} + +while getopts ":hd:" option; do + case $option in + h) # display help + Help + exit;; + d) # Enter a directory + out_dir=$OPTARG + if [ ! -d $out_dir ]; then + echo $out_dir Does not exist + exit + fi;; + \?) # Invalid option + echo "Error: Invalid option" + exit;; + esac +done + +# if no directory was specified, collect all directories from 'archive' +if [ -z $out_dir ]; then + out_dirs=$(ls -rd archive/output*[0-9] 2>/dev/null) +else + out_dirs=$out_dir +fi + +# process each output directory +for dir in ${out_dirs[@]}; do + # process each mom6 file + for current_file in $dir/access-om3.mom6.*.nc; do + if [ -f $current_file ]; then + new_filename=$(echo $current_file | sed -E 's/_([0-9]{4})\./\1./') + # rename the file without overwriting exisiting files + mv -n $current_file $new_filename + fi + done +done