Skip to content

Instantly share code, notes, and snippets.

@trondkr
trondkr / add_halo.sh
Last active March 13, 2022 19:35
Add fake point to NetCDF4 file to wrap globally
for file in era5*.nc
do
 name=${file##*/}
 base=${name%.nc}
newfile="${base}_halo.nc"
 
if [ ! -f "$newfile" ]
then
echo "$0: Creating '${file}'"
  cdo sethalo,0,1 "$file" "$newfile"
@trondkr
trondkr / xarray_wrap_around_pixel_demo.py
Created July 29, 2021 21:00 — forked from brews/xarray_wrap_around_pixel_demo.py
Demo using cartopy.util.add_cyclic_point with an xarray Dataset to add a cyclic or wrap-around pixel to the `lon` dimension. This can be useful for plotting with `cartopy` or regridding with `xesmf`.
"""
Demo using cartopy.util.add_cyclic_point with an xarray Dataset to
add a cyclic or wrap-around pixel to the `lon` dimension. This can be useful
for plotting with `cartopy` or regridding with `xesmf`.
"""
import xarray as xr
from cartopy.util import add_cyclic_point
@trondkr
trondkr / convert_to_netcdf4_classic.sh
Created September 24, 2019 22:44
Convert NetCDF4 to NetCDF4_classic
module load NCO/4.7.7-intel-2018b
for file in *; do
ext=${file##*.}
fname=`basename $file .$ext`
fname=${fname}_classic.nc
echo ${fname}
nccopy -k nc7 ${file} ${fname}
done;
@trondkr
trondkr / addEmptyICEVariablesROMSINIFile.sh
Last active July 9, 2019 20:44
Add empty sea-ice variables for ROMS init file.
# Add empty sea-ice variables for ROMS init file. This is intended to make
# ROMS run by not failining on missing variables when initializing
export FINAL="hunnbunn50_ini_v5.nc"
export TMPFILE="tmp.nc"
rm -f ${FINAL}
rm -f ${TMPFILE}
echo "Copying original file to be modified"
scp "hunnbunn50_ini_v4.nc" ${FINAL}
@trondkr
trondkr / addEmptyICEVariablesROMSToBRYFile.sh
Last active July 8, 2019 17:42
Adds empty ice variables to existing BRY file so that we can run ICE model but with lacking boundary ice forcing
directions=(north south east west)
scp hunnbunn50_bry_v3.nc ${FINAL}
export FINAL=hunnbunn50_bry_v4.nc
export TMPFILE=tmp.nc
echo "Final output file will be named: " ${FINAL}
for dir in "${directions[@]}"
do
echo "Running SOUTH"
@trondkr
trondkr / postProcessROMSFilenames.py
Created July 8, 2019 17:01
This script takes the output of running ROMS where the name of result files are an increasing integer ROMS.1, ROMS.2, ROMS.3 ... ROMS.N. The new filenames uses the startdate (ocean_time ) of the ROMS file as the integer instead. This means that all files have names indicating start date as days since 1948/1/1
import os, shutil
import datetime as datetime
from netCDF4 import Dataset
import numpy as np
import glob
import string
__author__ = 'Trond Kristiansen'
__email__ = 'me (at) trondkristiansen.com'
__created__ = datetime.datetime(2015, 10, 5)
@trondkr
trondkr / deleteTimestepsInNetCDFfiles.py
Last active July 8, 2019 17:00
This script reads netcdf files and investigates whether some timesteps occurr more than one time.
import os
import datetime as datetime
from netCDF4 import Dataset
from subprocess import call
from itertools import chain
__author__ = 'Trond Kristiansen'
__email__ = 'me (at) trondkristiansen.com'
__created__ = datetime.datetime(2014, 4, 2)
__modified__ = datetime.datetime(2014, 4, 2)
@trondkr
trondkr / seltimesteps.sh
Last active April 19, 2019 16:16
CDO seltimesteps
# Select the first 12 timesteps of a file
cdo -seltimestep,1/12 NORESM_ATM.cam2.hmlvl.2006-2100_delta.nc NORESM_ATM.cam2.hmlvl.2006-2100_delta.nc2
@trondkr
trondkr / mergetime.py
Last active April 6, 2017 18:54
Remove duplicates of timesteps in netcdf files
```python
from netCDF4 import Dataset, datetime, date2num,num2date
import numpy as np
import os
from subprocess import call
__author__ = 'Trond Kristiansen'
__email__ = 'trond.kristiansen@niva.no'
__created__ = datetime(2017, 2, 1)
__modified__ = datetime(2017, 2, 1)
ncflint -A -C -v ubar_north,vbar_north,u_north,v_north,ubar_west,vbar_west,u_west,v_west -w 0.0,0.0 ws4km_bry_SODA3_19890115_to_20151215_test.nc ws4km_bry_SODA3_19890115_to_20151215_test.nc test.nc