1 | #!/bin/bash |
---|
2 | # Shell script to split variables from a LMDZ output and give the right time-axis values |
---|
3 | # L. Fita, LMD. March 2015 |
---|
4 | |
---|
5 | # input folder |
---|
6 | infold=. |
---|
7 | |
---|
8 | # output folder |
---|
9 | outfold=. |
---|
10 | |
---|
11 | # intput file |
---|
12 | infile=NPv5.12a_SE_1981_1983_1M_histmth.nc |
---|
13 | |
---|
14 | # Location of the python scripts |
---|
15 | HOMEpy=/home/lfita/etudes/WRF_LMDZ/svn/LMD_WRF/tools |
---|
16 | |
---|
17 | # Time variables' name |
---|
18 | tnames='time_centered:time_instant' |
---|
19 | |
---|
20 | # Variables not to change |
---|
21 | NOvars='time_counter:time_counter_bnds:lon:lat:presnivs' |
---|
22 | |
---|
23 | ### END of MODIFICATIONS ### |
---|
24 | ####### ###### ##### #### ### ## # |
---|
25 | timedim='time_counter' |
---|
26 | |
---|
27 | function isin_list() { |
---|
28 | # Function to check whether a value is in a list |
---|
29 | list=$1 |
---|
30 | value=$2 |
---|
31 | |
---|
32 | is=`echo ${list} | tr ':' '\n' | awk '{print "@"$1"@"}' | grep '@'${value}'@' | wc -w` |
---|
33 | if test ${is} -eq 1 |
---|
34 | then |
---|
35 | true |
---|
36 | else |
---|
37 | false |
---|
38 | fi |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | ####### ####### |
---|
43 | ## MAIN |
---|
44 | ####### |
---|
45 | |
---|
46 | Linfile=`expr length ${infile}` |
---|
47 | Linfile3=`expr $Linfile - 3` |
---|
48 | headfile=${infile:0:Linfile3} |
---|
49 | |
---|
50 | ifile=${infold}/${infile} |
---|
51 | |
---|
52 | # Cleaning original file from accents in the attributes |
---|
53 | python ${HOMEpy}/nc_var.py -o varrmattr -S long_name -f ${ifile} -v random_notrig |
---|
54 | python ${HOMEpy}/nc_var.py -o varrmattr -S long_name -f ${ifile} -v proba_notrig |
---|
55 | python ${HOMEpy}/nc_var.py -o varaddattr -S 'long_name|Tirage!aleatoire!de!non-declanchement' \ |
---|
56 | -v random_notrig -f ${ifile} |
---|
57 | python ${HOMEpy}/nc_var.py -o varaddattr -S 'long_name|Probabilite!de!non-declanchement' \ |
---|
58 | -v proba_notrig -f ${ifile} |
---|
59 | |
---|
60 | # Preparing time variables values |
---|
61 | tns=`echo ${tnames} | tr ':' ' '` |
---|
62 | for tn in ${tns}; do |
---|
63 | python ${HOMEpy}/nc_var.py -o varout -f ${ifile} -S ${timedim}:-1 -v ${tn} | \ |
---|
64 | awk '{print $2}' >& ${tn}.dat |
---|
65 | done |
---|
66 | |
---|
67 | # Getting all variables in the file |
---|
68 | allvariables=`python ${HOMEpy}/nc_var.py -o ivars -f ${ifile} | grep allvars | \ |
---|
69 | awk '{print $3}'` |
---|
70 | |
---|
71 | allvars=` echo ${allvariables} | tr ':' ' '` |
---|
72 | for var in ${allvars}; do |
---|
73 | if ! $(isin_list ${NOvars} ${var}); then |
---|
74 | vfile=${headfile}_${var}.nc |
---|
75 | vmfile=${headfile}_${var}mean.nc |
---|
76 | |
---|
77 | # Which time_coordinate |
---|
78 | coord=`python ${HOMEpy}/nc_var.py -o ivattrs -f ${ifile} -v ${var} | grep -v '#' \ |
---|
79 | | grep coordinates | awk '{print $3}'` |
---|
80 | # Working in that variables which have an attribute called 'coordinates' wich content |
---|
81 | # any of the tnames on the original file, CDO remove this attribute... |
---|
82 | Lcoord=`expr length ${coord}'0'` |
---|
83 | if test ${Lcoord} -gt 1 && test ! -f ${vmfile}; then |
---|
84 | timevar=`echo ${coord} | tr '!' ' ' | awk '{print $1}'` |
---|
85 | echo " "${var}" time variable:"${timevar}"..." |
---|
86 | |
---|
87 | tunits=`python ${HOMEpy}/nc_var.py -o ivattrs -f ${ifile} -v ${timevar} | \ |
---|
88 | grep -v '#' | grep units | awk '{print $3}'` |
---|
89 | tcal=`python ${HOMEpy}/nc_var.py -o ivattrs -f ${ifile} -v ${timevar} | \ |
---|
90 | grep -v '#' | grep calendar | awk '{print $3}'` |
---|
91 | |
---|
92 | # Individual variable file |
---|
93 | cdo selvar,${var} ${ifile} ${vfile} |
---|
94 | # Changing variable names |
---|
95 | python ${HOMEpy}/nc_var.py -o chvarname -S ${timevar} -f ${vfile} -v ${timedim} |
---|
96 | # Adding values to the file |
---|
97 | python ${HOMEpy}/nc_var.py -o setvar_asciivalues -S ${timevar}.dat -f \ |
---|
98 | ${vfile} -v ${timevar} |
---|
99 | # Adding attributes |
---|
100 | python ${HOMEpy}/nc_var.py -o varaddattr -S 'standard_name|time' -v ${timevar} \ |
---|
101 | -f ${vfile} |
---|
102 | python ${HOMEpy}/nc_var.py -o varaddattr -S 'long_name|time' -v ${timevar} -f \ |
---|
103 | ${vfile} |
---|
104 | python ${HOMEpy}/nc_var.py -o varaddattr -S 'units|'${tunits} -v ${timevar} -f \ |
---|
105 | ${vfile} |
---|
106 | python ${HOMEpy}/nc_var.py -o varaddattr -S 'calendar|'${tcal} -v ${timevar} -f\ |
---|
107 | ${vfile} |
---|
108 | python ${HOMEpy}/nc_var.py -o varaddattr -S 'coordinates|'${timevar}'!lat!lon' -v \ |
---|
109 | ${var} -f ${vfile} |
---|
110 | |
---|
111 | if test -f ${vmfile}; then rm ${vmfile}; fi |
---|
112 | cdo seasmean ${vfile} ${vmfile} |
---|
113 | # exit |
---|
114 | fi |
---|
115 | fi |
---|
116 | done |
---|