-
Notifications
You must be signed in to change notification settings - Fork 2
/
nctenohist
executable file
·93 lines (69 loc) · 2.39 KB
/
nctenohist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/tcsh
# This weirdness is necessary because when the script is invoked via
# xargs, the argument list gets passed in as a single string, and it
# needs to be split back up. Wack!
set argv = ($*)
if ($#argv < 4 || $#argv > 5) then
echo " usage: nctimeextend N offset infile outfile [varname] \
\
nctimeextend adds N timesteps filled with _FillValue to the main\
variable of a netcdf file. If N is positive, they are appended at\
the end of the data, while if N is negative, the are prepended to\
the beginning.\
\
The added timesteps are created by copying the last (first) N\
timesteps of infile and setting them to _FillValue, then adjusting\
the time coordinate by offset*N. N.B.: Errors will occur if N is\
greater than the number of timesteps in the file, or if the time\
coordinate is not uniformly spaced.\
\
If varname is not supplied, nctimeextend assumes it's everything\
up to the first underscore in infile."
exit
endif
echo nctimeextend $1 $2 $3 $4
set n = $1
set offset = $2
set in = $3
set out = $4
if ($#argv == 5) then
set var = $5
else
set var = `basename $in | cut -f 1 -d _`
endif
set torig = $out.$$.tmp.orig
set ttemp = $out.$$.tmp.temp
set tfill = $out.$$.tmp.fill
set boundsvar = `ncdump -h $in | grep time:bounds | cut -f 2 -d \"`
cp $in $out
if ($n < 0 ) then
@ m = - $n
set first = 0
@ last = $m - 1
cp $out $torig
ncks -O -h -a -d time,$first,$last $out $tfill
ncap2 -O -h -s "$var = $var * 0 + $var@_FillValue" $tfill $ttemp
ncap2 -O -h -s "time=time-$offset*$m" $ttemp $tfill
if ($boundsvar != "") then
mv $tfill $ttemp
ncap2 -O -h -s "$boundsvar=$boundsvar-$offset*$m" $ttemp $tfill
endif
ncrcat -O -h $tfill $torig $out
endif
if ($n > 0 ) then
cp $out $torig
set nt = `ncdump -h $torig | perl -ne 'if (m|time = UNLIMITED ; // \((\d+) currently\)|) {print ($1-1); exit;}'`
@ first = $nt - $n
@ last = $nt - 1
ncks -O -h -a -d time,$first,$last $out $tfill
ncap2 -O -h -s "$var = $var * 0 + $var@_FillValue" $tfill $ttemp
ncap2 -O -h -s "time=time+$offset*($n+1)" $ttemp $tfill
if ($boundsvar != "") then
mv $tfill $ttemp
ncap2 -O -h -s "$boundsvar=$boundsvar+$offset*($n+1)" $ttemp $tfill
endif
ncrcat -O -h $torig $tfill $out
endif
rm -f $torig $ttemp $tfill
# ncatted -h -a history,global,a,c,"\
# `date`: nctimeextend $*" $out