[43] | 1 | #!/bin/sh |
---|
| 2 | # -*- Mode: sh -*- |
---|
| 3 | # |
---|
[44] | 4 | # Script to build 1mm and full record annual mean from monthly time serie |
---|
[43] | 5 | # |
---|
| 6 | # Uses nco tools |
---|
| 7 | # |
---|
| 8 | usage() { |
---|
| 9 | echo |
---|
[44] | 10 | echo "usage : make_1mm [-d] [-v <field1>,<field2>...] [-t <record name>] [-m] <file> <nyears>" |
---|
| 11 | echo " -d : toggle debug mode" |
---|
| 12 | echo " -v : specify fields to work on" |
---|
| 13 | echo " -t : specify time record name (should be detected automatically)" |
---|
| 14 | echo " -m : do not make full record time mean" |
---|
| 15 | echo " <file> : netcdf post-it format file" |
---|
| 16 | echo " <nyears> : number of years in file" |
---|
[43] | 17 | echo |
---|
| 18 | } |
---|
| 19 | # |
---|
| 20 | # |
---|
| 21 | # -------------------------------------------------------------- |
---|
| 22 | # E. Guilyardi - LODYC - July 1999 - Nov 2007 |
---|
| 23 | # |
---|
| 24 | # |
---|
| 25 | # Inits |
---|
| 26 | # |
---|
| 27 | |
---|
[44] | 28 | while getopts dhv:t:m name |
---|
[43] | 29 | do |
---|
| 30 | case $name in |
---|
| 31 | d) set -x ;; |
---|
| 32 | v) fields="$OPTARG" ;; |
---|
| 33 | t) timef="$OPTARG" ;; |
---|
| 34 | m) nomean=1;; |
---|
| 35 | h) usage |
---|
| 36 | echo |
---|
| 37 | exit 1 |
---|
| 38 | ;; |
---|
| 39 | esac |
---|
| 40 | done |
---|
| 41 | |
---|
| 42 | # |
---|
[44] | 43 | # decode file name |
---|
[43] | 44 | # |
---|
| 45 | shift `expr $OPTIND - 1` |
---|
| 46 | file=$1 |
---|
| 47 | |
---|
| 48 | enam=`echo $file | awk -F\_ '{print $1}'` |
---|
| 49 | ave=`echo $file | awk -F\_ '{print $2}'` |
---|
| 50 | date1=`echo $file | awk -F\_ '{print $3}'` |
---|
| 51 | year1=`echo $date1 | sed 's/\(.*\)../\1/'` |
---|
| 52 | date2=`echo $file | awk -F\_ '{print $4}'` |
---|
| 53 | year2=`echo $date2 | sed 's/\(.*\)../\1/'` |
---|
| 54 | ndates=$2 |
---|
| 55 | |
---|
| 56 | root=${enam}_${ave}_${date1}_${date2} |
---|
| 57 | |
---|
| 58 | type=`echo $file | awk -F\_ '{print $5}'` |
---|
| 59 | type2=`echo $file | awk -F\_ '{print $6}'` |
---|
| 60 | type3=`echo $file | awk -F\_ '{print $7}'` |
---|
| 61 | type4=`echo $file | awk -F\_ '{print $8}'` |
---|
| 62 | type=${type}_${type2}_${type3}_${type4} |
---|
| 63 | type=`echo $type | sed 's/\(.*\)\.nc.*/\1/'` |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | type=${type:-"auto"} |
---|
| 67 | nomean=${nomean:-0} |
---|
| 68 | |
---|
| 69 | echo "" |
---|
| 70 | echo "Processing: $enam $ave $year1 $year2 $ndates $type" |
---|
| 71 | |
---|
[44] | 72 | # detect time dimension |
---|
| 73 | |
---|
| 74 | time=`ncdump -h ${file} | grep UNLIMITED | awk '{print $1}'` |
---|
[43] | 75 | time=${timef:-$time} |
---|
| 76 | fields=${fields:-"-x -v $long,$lati"} |
---|
| 77 | |
---|
| 78 | # |
---|
| 79 | case $ave in |
---|
| 80 | 1m) # output file name |
---|
| 81 | month1=`echo $date1 | sed 's/.*\(..\)/\1/'` |
---|
| 82 | month2=`echo $date2 | sed 's/.*\(..\)/\1/'` |
---|
| 83 | |
---|
| 84 | aveoum=1mm |
---|
| 85 | ;; |
---|
| 86 | |
---|
| 87 | *) echo " ERROR : case ave=$ave not relevant " |
---|
| 88 | exit 1 |
---|
| 89 | ;; |
---|
| 90 | esac |
---|
| 91 | |
---|
| 92 | # |
---|
| 93 | out1m=${enam}_${aveoum}_01_12_${year1}-${year2}_$type.nc |
---|
| 94 | outmean=${enam}_${ndates}y_${year1}_${year2}_$type.nc |
---|
| 95 | # |
---|
| 96 | optg="-O" |
---|
| 97 | |
---|
| 98 | m_list="00 01 02 03 04 05 06 07 08 09 10 11" |
---|
| 99 | |
---|
| 100 | # average every month |
---|
| 101 | |
---|
| 102 | for m in ${m_list} |
---|
| 103 | do |
---|
| 104 | m1=`expr $m + 1` |
---|
| 105 | # echo " averaging month $m1 " |
---|
| 106 | ncks -O -d $time,$m,,12 ${file} ${root}_${type}_mean_month.nc || exit |
---|
| 107 | ncra -O -F -d $time,1,$ndates ${root}_${type}_mean_month.nc ${root}_${type}_mean_month_${m}.nc || exit |
---|
| 108 | rm ${root}_${type}_mean_month.nc |
---|
| 109 | done |
---|
| 110 | |
---|
| 111 | # build monthly means |
---|
| 112 | |
---|
| 113 | echo " Building ${enam}_${aveoum}_01_12_${year1}-${year2}_$type.nc" |
---|
| 114 | |
---|
| 115 | ncrcat ${optg} ${root}_${type}_mean_month_??.nc $out1m |
---|
| 116 | |
---|
| 117 | if [ $nomean -eq 0 ] ; then |
---|
| 118 | |
---|
| 119 | echo " Building ${outmean}" |
---|
| 120 | |
---|
| 121 | ncra ${optg} $out1m $outmean |
---|
| 122 | |
---|
| 123 | fi |
---|
| 124 | |
---|
| 125 | rm -f ${root}_${type}_mean_*.nc |
---|