Skip to content

Instantly share code, notes, and snippets.

@guziy
Last active December 21, 2015 03:29
Show Gist options
  • Save guziy/6242624 to your computer and use it in GitHub Desktop.
Save guziy/6242624 to your computer and use it in GitHub Desktop.
Correcting files using NCO utilities (namely: ncatted and ncap2)
#!/bin/bash
folder=.
prefix=ANUSPLIN_latlon_stmn_
var_name=daily_minimum_temperature
for x in ${folder}/${prefix}*.nc
do
x_name=$(basename $x)
part=$(echo $x_name | cut -d"." -f 1)
#get the year and month from the file name
y=$(echo $part | cut -d"_" -f 4)
m=$(echo $part | cut -d"_" -f 5)
#change time units
ncatted -O -a units,time,m,c,"days since ${y}-${m}-01" $x
# decrement the time values so they start from 0
ncap2 -O -s "time=time-1;" $x -o $x
#replace NaNs with a value specified in the missing_value attribute
ncap2 -O -s "where(${var_name} != ${var_name}){${var_name}=${var_name}@missing_value;}" $x -o $x
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment