1 | #!/bin/sh |
---|
2 | # -*- Mode: sh -*- |
---|
3 | # |
---|
4 | # Script to build 1mm and annual mean from time serie |
---|
5 | # |
---|
6 | # Uses nco tools |
---|
7 | # |
---|
8 | usage() { |
---|
9 | echo |
---|
10 | echo "usage : make_1mm [-d] [-g] [-v <field1>,<field2>...] [-t <record name>] [-g <grid>] [-m] <file> <nyears>" |
---|
11 | echo |
---|
12 | } |
---|
13 | # |
---|
14 | # |
---|
15 | # -------------------------------------------------------------- |
---|
16 | # E. Guilyardi - LODYC - July 1999 - Nov 2007 |
---|
17 | # |
---|
18 | # |
---|
19 | # Inits |
---|
20 | # |
---|
21 | |
---|
22 | while getopts g:dhv:t:m name |
---|
23 | do |
---|
24 | case $name in |
---|
25 | d) set -x ;; |
---|
26 | g) type="$OPTARG";; |
---|
27 | v) fields="$OPTARG" ;; |
---|
28 | t) timef="$OPTARG" ;; |
---|
29 | m) nomean=1;; |
---|
30 | h) usage |
---|
31 | echo |
---|
32 | exit 1 |
---|
33 | ;; |
---|
34 | esac |
---|
35 | done |
---|
36 | |
---|
37 | # |
---|
38 | # so that $1 is root |
---|
39 | # |
---|
40 | shift `expr $OPTIND - 1` |
---|
41 | file=$1 |
---|
42 | |
---|
43 | enam=`echo $file | awk -F\_ '{print $1}'` |
---|
44 | ave=`echo $file | awk -F\_ '{print $2}'` |
---|
45 | date1=`echo $file | awk -F\_ '{print $3}'` |
---|
46 | year1=`echo $date1 | sed 's/\(.*\)../\1/'` |
---|
47 | date2=`echo $file | awk -F\_ '{print $4}'` |
---|
48 | year2=`echo $date2 | sed 's/\(.*\)../\1/'` |
---|
49 | ndates=$2 |
---|
50 | |
---|
51 | root=${enam}_${ave}_${date1}_${date2} |
---|
52 | |
---|
53 | type=`echo $file | awk -F\_ '{print $5}'` |
---|
54 | type2=`echo $file | awk -F\_ '{print $6}'` |
---|
55 | type3=`echo $file | awk -F\_ '{print $7}'` |
---|
56 | type4=`echo $file | awk -F\_ '{print $8}'` |
---|
57 | type=${type}_${type2}_${type3}_${type4} |
---|
58 | type=`echo $type | sed 's/\(.*\)\.nc.*/\1/'` |
---|
59 | |
---|
60 | |
---|
61 | type=${type:-"auto"} |
---|
62 | nomean=${nomean:-0} |
---|
63 | |
---|
64 | echo "" |
---|
65 | echo "Processing: $enam $ave $year1 $year2 $ndates $type" |
---|
66 | |
---|
67 | case $type in |
---|
68 | t30) long="nav_lon" |
---|
69 | lati="nav_lat" |
---|
70 | time="time_counter" ;; |
---|
71 | lmdz*) long="lon" |
---|
72 | lati="lat" |
---|
73 | time="time_counter" ;; |
---|
74 | reg1) long="lon" |
---|
75 | lati="lat" |
---|
76 | time="time" ;; |
---|
77 | t42) long="lon" |
---|
78 | lati="lat" |
---|
79 | time="time" ;; |
---|
80 | t62) long="lon" |
---|
81 | lati="lat" |
---|
82 | time="time" ;; |
---|
83 | umat) long="longitude" |
---|
84 | lati="latitude" |
---|
85 | time="t" ;; |
---|
86 | umau) long="longitude" |
---|
87 | lati="latitude" |
---|
88 | time="t" ;; |
---|
89 | reg1mtrop) long="longitude" |
---|
90 | lati="latitude" |
---|
91 | time="t" ;; |
---|
92 | *) long="nav_lon" |
---|
93 | lati="nav_lat" |
---|
94 | time=`ncdump -h ${file} | grep UNLIMITED | awk '{print $1}'` ;; |
---|
95 | esac |
---|
96 | time=${timef:-$time} |
---|
97 | fields=${fields:-"-x -v $long,$lati"} |
---|
98 | |
---|
99 | # |
---|
100 | case $ave in |
---|
101 | 1m) # output file name |
---|
102 | month1=`echo $date1 | sed 's/.*\(..\)/\1/'` |
---|
103 | month2=`echo $date2 | sed 's/.*\(..\)/\1/'` |
---|
104 | |
---|
105 | aveoum=1mm |
---|
106 | ;; |
---|
107 | |
---|
108 | *) echo " ERROR : case ave=$ave not relevant " |
---|
109 | exit 1 |
---|
110 | ;; |
---|
111 | esac |
---|
112 | |
---|
113 | # |
---|
114 | PATH=$PATH:/usr/local/netcdf/bin:/u/rech/eee/reee217/bin |
---|
115 | # |
---|
116 | out1m=${enam}_${aveoum}_01_12_${year1}-${year2}_$type.nc |
---|
117 | outmean=${enam}_${ndates}y_${year1}_${year2}_$type.nc |
---|
118 | # |
---|
119 | optg="-O" |
---|
120 | grid=0 |
---|
121 | if [ $grid -eq 1 ] ; then |
---|
122 | # get grid |
---|
123 | ncks -O -v $lati,$long ${file} $out1m || echo 'no grid' |
---|
124 | optg="-A" |
---|
125 | fi |
---|
126 | |
---|
127 | m_list="00 01 02 03 04 05 06 07 08 09 10 11" |
---|
128 | |
---|
129 | # average every month |
---|
130 | |
---|
131 | for m in ${m_list} |
---|
132 | do |
---|
133 | m1=`expr $m + 1` |
---|
134 | # echo " averaging month $m1 " |
---|
135 | ncks -O -d $time,$m,,12 ${file} ${root}_${type}_mean_month.nc || exit |
---|
136 | ncra -O -F -d $time,1,$ndates ${root}_${type}_mean_month.nc ${root}_${type}_mean_month_${m}.nc || exit |
---|
137 | rm ${root}_${type}_mean_month.nc |
---|
138 | done |
---|
139 | |
---|
140 | # build monthly means |
---|
141 | |
---|
142 | echo " Building ${enam}_${aveoum}_01_12_${year1}-${year2}_$type.nc" |
---|
143 | |
---|
144 | ncrcat ${optg} ${root}_${type}_mean_month_??.nc $out1m |
---|
145 | |
---|
146 | if [ $nomean -eq 0 ] ; then |
---|
147 | |
---|
148 | echo " Building ${outmean}" |
---|
149 | |
---|
150 | ncra ${optg} $out1m $outmean |
---|
151 | |
---|
152 | fi |
---|
153 | |
---|
154 | rm -f ${root}_${type}_mean_*.nc |
---|