Last active
March 23, 2022 07:26
-
-
Save engeir/7ba0e74b2856222ac1cefb105af43b3b to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/sh | |
########################################################################## | |
# The MIT License | |
# | |
# Copyright (c) engeir | |
# | |
# Permission is hereby granted, free of charge, | |
# to any person obtaining a copy of this software and | |
# associated documentation files (the "Software"), to | |
# deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, | |
# merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom | |
# the Software is furnished to do so, | |
# subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice | |
# shall be included in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR | |
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
########################################################################## | |
# | |
# Description: Create file used in the CESM2 that works with the INTERP_MISSING_MONTHS mode, | |
# from a file meant to be run in CYCLE mode. | |
# | |
# Dependencies: NCO - http://research.jisao.washington.edu/data_sets/nco/ | |
# reset_date_variable - https://gist.github.com/engeir/7ba0e74b2856222ac1cefb105af43b3b#file-reset_date_variable-py | |
# | |
# Usage: | |
# 1. Run with some netCDF file as the first argument | |
# `cycle2interp.sh in.nc` | |
# | |
# Needs a python environment with the netcdf4 package installed, and the NCO command line | |
# tools. | |
# | |
# Shell: POSIX compliant | |
# Author: Eirik R. Enger | |
# | |
if ! command -v ncks >/dev/null 2>&1; then | |
echo "Cannot access NCO commands" | |
exit 1 | |
fi | |
ncks -d time,'1850-1-1 0:00:0.0','1851-1-1 0:00:0.0' "$1" 1850.nc | |
ncap2 -s 'time=time+(1-1850)*365' 1850.nc 1.nc | |
ncap2 -s 'time=time+(9999-1850)*365' 1850.nc 9999.nc | |
if ncdump -h "$1" | grep 'time = UNLIMITED' >/dev/null 2>&1; then | |
cp 1.nc 1_un.nc | |
cp 9999.nc 9999_un.nc | |
else | |
ncks --mk_rec_dmn time 1.nc 1_un.nc | |
ncks --mk_rec_dmn time 9999.nc 9999_un.nc | |
fi | |
# Make a backup and save to the original name. | |
mv "$1" "$1".bak | |
ncrcat -h 1_un.nc 9999_un.nc "$1" | |
echo "$1" | reset_date_variable.py | |
# Clean up | |
rm 1850.nc 1.nc 9999.nc 1_un.nc 9999_un.nc |
This file contains 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
#!/bin/python | |
########################################################################## | |
# The MIT License | |
# | |
# Copyright (c) engeir | |
# | |
# Permission is hereby granted, free of charge, | |
# to any person obtaining a copy of this software and | |
# associated documentation files (the "Software"), to | |
# deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, | |
# merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom | |
# the Software is furnished to do so, | |
# subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice | |
# shall be included in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR | |
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
########################################################################## | |
# | |
# Description: Re-write the year of the "date" variable of a netCDF file. The first | |
# 12 elements are set to year 1, the rest to year 9999. | |
# | |
# Dependencies: netCDF4 - https://pypi.org/project/netCDF4/ | |
# | |
# Usage: | |
# 1. Run the script with the filename as stdin | |
# `echo "in.nc" | reset_date_variable.py` | |
# | |
# Author: Eirik R. Enger | |
# | |
import os | |
import sys | |
import netCDF4 as nc | |
def rewrite_date_variable() -> None: | |
# Check file path | |
data = sys.stdin.readlines() | |
new_path = data[0].strip("\n") | |
if not isinstance(new_path, str) or new_path[-3:] != ".nc": | |
raise TypeError(f"Are you sure {new_path} is a valid netCDF file?") | |
if not os.path.exists(new_path): | |
raise FileNotFoundError(f"Cannot find file named {new_path}.") | |
# Open file | |
with nc.Dataset(new_path, "r+") as ncfile: | |
# Fix values | |
for i, d in enumerate(ncfile.variables["date"][:]): | |
n = 1 if i < 12 else 9999 | |
ncfile.variables["date"][i] = d % 10000 + n * 10000 | |
if __name__ == "__main__": | |
rewrite_date_variable() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment