0

I have a directory of NetCDF files with names of the following format:

foo_d1_t1.nc
foo_d1_t2.nc
foo_d1_t3.nc
foo_d1_t4.nc
foo_d2_t1.nc
foo_d2_t2.nc
foo_d2_t3.nc
foo_d2_t4.nc
...

where "d" refers to the day and "t" to the timestep.

The files contain accumulated values since the start of the day, which I need to convert to an average rate per timestep by subtracting t1 from t2, t2 from t3 and t3 from t4 for each day. Individually this can be achieved through ncdiff, which for one day would look like this:

cp foo_d1_t1.nc bar_d1_t1.nc     #t1 needs no modification
ncdiff foo_d1_t2.nc foo_d1_t1.nc bar_d1_t2.nc
ncdiff foo_d1_t3.nc foo_d1_t2.nc bar_d1_t3.nc
ncdiff foo_d1_t4.nc foo_d1_t3.nc bar_d1_t4.nc

Can anyone assist with a shell script to automate this for all days present in a directory? Cheers.

4

1 回答 1

4

重击

# capture the files into an array
files=(*.nc)

# do something with the first one
echo cp "${files[0]}" "${files[0]/#foo/bar}"

# do stuff with the remaining ones
for ((i=1; i < ${#files[@]}; i++)); do
    echo ncdiff "${files[i]}" "${files[i-1]}" "${files[i]/#foo/bar}"
done

如果您满意,请删除“回声”

于 2015-04-17T15:48:26.290 回答