source: lmdz_wrf/trunk/tools/model_graphics.bash @ 928

Last change on this file since 928 was 927, checked in by lfita, 9 years ago

Working version with the right values for the configuration
Including the skip of the plot when there is not the file with the necessary variable

  • Property svn:executable set to *
File size: 72.3 KB
Line 
1#!/bin/bash
2# L. Fita, June 2016. Generic script to plot model outputs
3
4main='model_graphics.bash'
5
6# All purpouse
7######## ###### ##### #### ### ## #
8
9function uploadvars() {
10# Function to upload variables to the system from an ASCII file as:
11#   [varname] = [value]
12  fileval=$1
13  errormsg='ERROR -- error -- ERROR -- error'
14
15  if test ! -f ${fileval}; then
16    echo ${errormsg}
17    echo "  "${fname}": file '"${fileval}"' does not exist!!"
18    exit
19  fi
20
21  Nlin=`wc -l ${fileval} | awk '{print $1}'`
22
23  ilin=1
24  while test ${ilin} -le ${Nlin}; do
25    line=`head -n ${ilin} ${fileval} | tail -n 1`
26    varname=`echo ${line} | tr '=' ' ' | awk '{print $1}'`
27    value=`echo ${line} | tr '=' ' ' | awk '{print $2}'`
28    Lvarname=`expr length ${varname}'0'`
29
30    if test ${Lvarname} -gt 1 && test ! ${varname:0:1} = '#'; then
31      export ${varname}=${value}
32    fi
33    ilin=`expr ${ilin} + 1`
34  done
35}
36
37function isInlist() {
38# Function to check whether a value is in a list
39  list=$1
40  value=$2
41 
42  is=`echo ${list} | tr ':' '\n' | awk '{print "@"$1"@"}' | grep '@'${value}'@' | wc -w`
43  if test ${is} -eq 1
44  then
45    true
46  else
47    false
48  fi
49}
50
51function ferrmsg() {
52# Function to exit and write an error message
53#  comdexit: code number exit from the last execution
54#  ref: script/reference from which error occurs
55#  msg: message to write ('!' for spaces, '#' for end of line)
56  comdexit=$1
57  ref=$2
58  msg=$3
59
60  if test ${comdexit} -ne 0; then
61    echo "ERROR -- error -- ERROR -- error"
62    echo "  "${ref}": "$(echo ${msg} | tr '!' ' ' | tr '#' '\n')" !!"
63    exit
64  fi
65}
66
67function ferrmsgF() {
68# Function to exit and write an error message and remove a file
69#  comdexit: code number exit from the last execution
70#  ref: script/reference from which error occurs
71#  msg: message to write ('!' for spaces, '#' for end of line)
72#  FileName: name of the file to remove in case of error
73  comdexit=`expr $1 + 0`
74  ref=$2
75  msg=$3
76  FileName=$4
77
78  if test ${comdexit} -ne 0; then
79    echo "ERROR -- error -- ERROR -- error"
80    echo "  "${ref}": "$(echo ${msg} | tr '!' ' ' | tr '\#' '\n')" !!"
81    rm ${FileName} >& /dev/null
82    exit
83  fi
84}
85
86function list_add_label(){
87# Function to add the label to a list
88#   list= ':' separated list to wchich add a ticket
89#   label= label to add to the list
90#   ch= character to separate list values and new label
91#   pos= position of the label ('beg': beginning, 'end': ending)
92# $ list_add_label 1:2:3:4:5 abc # beg
93# abc#1:abc#2:abc#3:abc#4:abc#5
94
95  fname='list_add_label'
96
97  list=$1
98  label=$2
99  ch=$3
100  pos=$4
101
102  ival=1
103  lvs=`echo ${list} | tr ':' ' '`
104 
105  newlist=''
106  for listv in ${lvs}; do
107    if test ${pos} = 'beg'; then
108      nlv=${label}${ch}${listv}
109    else
110      nlv=${listv}${ch}${label}
111    fi
112    if test ${ival} -eq 1; then
113      newlist=${nlv}
114    else
115      newlist=${newlist}':'${nlv}
116    fi
117    ival=`expr ${ival} + 1`
118  done
119
120  echo ${newlist}
121}
122
123function stats_filename() {
124# Function to provide the name of a given statisitcs file
125#   CFvarn= CF variable name
126#   fkind= kind of the file
127#   headf= head of the file
128# $ stats_filename ta xmean wrfout
129# ta_wrfout_xmean.nc
130# $ stats_filename ta pinterp@last@xmean wrfout
131# ta_wrfout_pinterp_last_xmean.nc
132  fname='stats_filename'
133
134  CFvarn=$1
135  fkind=$2
136  headf=$3
137
138  # Number of operations in a combo file they are '@' separated
139  nopers=`echo ${fkind} | tr '@' ' ' | wc -w | awk '{print $1}'`
140
141  if test $(index_string ${fkind} pinterp) -ne -1; then
142    fhead=${headf}'p'
143  else
144    fhead=${headf}
145  fi
146
147  if test ${nopers} -eq 1; then
148    filename=${CFvarn}'_'${fhead}'_'${fkind}
149  else
150    opers=`echo ${fkind} | tr '@' ' '`
151    filename=${CFvarn}'_'${fhead}
152    for op in ${opers}; do
153      filename=${filename}'_'${op}
154    done
155  fi
156
157  echo ${filename}.nc
158}
159
160function index_string(){
161# Function to provide the index of a given word inside a string
162#   string= string to find in
163#   word= word to find
164#  $ index_string 123abc456ab789bca0 bca
165#  14
166  fname='index_string'
167  string=$1
168  word=$2
169
170  Lstring=`expr length ${string}`
171  Lword=`expr length ${word}`
172 
173  Lfinalstring=`expr ${Lstring} - ${Lword}`
174
175  iw=0
176  index=-1
177  while test ${iw} -le ${Lfinalstring}; do
178    if test ${string:${iw}:${Lword}} = ${word}; then
179      index=${iw}
180      break
181    fi
182    iw=`expr ${iw} + 1`
183  done
184
185  echo ${index}
186  return
187}
188
189function list_filter() {
190# Function to filter by a value a list
191#   list: list to filter
192#   value: value to filter with
193#  $ list_filter '1|i:2|ii:3|iii:4|iv:5|v:1|I:2|II:3|III:4|iv:5|v' 3
194#  3|iii:3|III
195  fname='list_filter'
196
197  list=$1
198  varn=$2
199
200  lvs=`echo ${list} | tr ':' ' '`
201
202  newlist=''
203  il=1
204  for lv in ${lvs}; do
205    if test $(index_string ${lv} ${varn}) -ne -1; then
206      if test ${il} -eq 1; then
207        newlist=${lv}
208      else
209        newlist=${newlist}':'${lv}
210      fi
211      il=`expr ${il} + 1`
212    fi
213  done
214
215  echo ${newlist}
216
217  return
218}
219
220
221# Specific
222######## ###### ##### #### ### ## #
223
224function variable_compute(){
225# Function to retrieve the computation way of a given variable using a series of
226#   test files
227#   iwdir= directory with the test files
228#   var= name of the variable to test
229#   filtests= '@' separated list of '[head]|[file] test files
230  fname='variable_compute'
231
232  iwdir=$1
233  var=$2
234  filtests=$3
235
236  ftsts=`echo ${filtests} |tr '@' ' '`
237  cancompute=true
238
239  for ftest in ${ftsts}; do
240    headerf=`echo ${ftest} | tr '|' ' ' | awk '{print $1}'`
241    filen=`echo ${ftest} | tr '|' ' ' | awk '{print $2}'`
242    compute=`${pyHOME}/nc_var.py -o computevar_model -f ${iwdir}/${filen} -S ${var}`
243    ferrmsg $? ${main} "python!'computevar_model'!failed!#"$(echo ${compute} |       \
244      tr ' ' '!')
245    varmod=`echo ${compute} | tr ' ' '#' | tr '|' ' ' | awk '{print $2}' |           \
246      tr '@' ' ' | awk '{print $2}' | tr '=' ' ' | awk '{print $2}'`
247    vardiag=`echo ${compute} | tr ' ' '#' | tr '|' ' ' | awk '{print $2}' |          \
248      tr '@' ' ' | awk '{print $3}' | tr '=' ' ' | awk '{print $2}'`
249    echo "  "${var}" mod:"${varmod}" diag: "${vardiag}
250    if test ${varmod} = 'None' && test ${vardiag} = 'None'; then 
251      cancompute=false
252    else
253      cancompute=true
254    # Should be considered that variable can also be computed by both ways?
255      break
256    fi
257  done
258  if ! ${cancompute}; then
259    msg="there!is!no!way!to!compute!'"${var}"'!for!model!"${mod}
260# Too extrict!
261#    ferrmsg 1 ${main} ${msg}
262    echo ${warnmsg}
263    echo $(echo $msg | tr '!' ' ')
264  fi
265
266  # a ';' list 'varcompute' it is created for each variable giving:
267  #   [var]|[vark]|[headerf][varmod]|[vardiag]
268  # This list will be used to compute a new file for each variable
269  varcomp=${var}'|'${vark}'|'${headerf}'|'${varmod}'|'${vardiag}
270
271  echo "varcomp= "${varcomp}
272}
273
274function WRF_toCF() {
275# Function to pass a WRF original file to CF-conventions
276#   wrff= WRF file
277#   wrfvl= WRF longitude var name (it might come from WPS!)
278#   wrfvL= WRF latitude var name (it might come from WPS!)
279
280  wrff=$1
281  wrfvl=$2
282  wrfvL=$3
283
284  fdims=`${pyHOME}/nc_var.py -o idims -f ${wrff} | grep alldims | tr '=' ' ' |       \
285    awk '{print $3}'`
286
287  CFdims='west_east@lon:south_north@lat:Time@time'
288  CFds=`echo ${CFdims} | tr ':' ' '`
289 
290  pyout=`${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes        \
291    -f ${wrff} -v time`
292  pyn=$?
293  Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
294  ferrmsgF ${pyn} ${fname} "python!'WRF_CFtime_creation'!failed"${Spyout} ${wrff}
295 
296  pyout=`${pyHOME}/nc_var.py -o WRF_CFlonlat_creation -v ${wrfvl},${wrfvL}           \
297    -f ${wrff} -S lon,lat`
298  pyn=$?
299  Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
300  ferrmsgF ${pyn} ${fname} "python!'WRF_CFlonlat_creation'!failed"${Spyout} ${wrff}
301
302  for CFd in ${CFds}; do
303    cd=`echo ${CFd} | tr '@' ' ' | awk '{print $2}'`
304    wd=`echo ${CFd} | tr '@' ' ' | awk '{print $1}'`
305    if $(isInlist ${fdims} ${wd}); then
306      pyout=`${pyHOME}/nc_var.py -o chdimname -f ${wrff} -S ${wd}':'${cd}`
307      pyn=$?
308      Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
309      ferrmsgF ${pyn} ${fname} "python!'chdimname'!'"${wd}"'!failed#"${Spyout} ${wrff}
310    fi
311  done
312
313  # CF attributes
314  dimvars='lon:lat:time:Times'
315
316  allfilevars=`python ${pyHOME}/nc_var.py -o ivars -f ${wrff} | grep allvars | awk '{print $3}'`
317  allvs=`echo ${allfilevars} | tr ':' ' '`
318  for vn in ${allvs}; do
319    if ! $(isInlist ${dimvars} ${vn}); then
320      varattrs=`python ${pyHOME}/generic.py -o variables_values -S ${vn}`
321      stn=`echo ${varattrs} | tr ':' ' ' | awk '{print $2}'`
322      lon=`echo ${varattrs} | tr ':' ' ' | awk '{print $5}' | tr '|' '!'`
323      un=`echo ${varattrs} | tr ':' ' ' | awk '{print $6}' | tr '|' '!'`
324
325      pyout=`python ${pyHOME}/nc_var.py -o varaddattr -f ${wrff} -S 'standard_name|'${stn} -v ${vn}`
326      pyn=$?
327      Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
328      ferrmsgF ${pyn} ${fname} "python!'varaddattr'!'standard_name'!failed#"${Spyout} ${wrff}
329      pyout=`python ${pyHOME}/nc_var.py -o varaddattr -f ${wrff} -S 'long_name|'${lon} -v ${vn}`
330      pyn=$?
331      Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
332      ferrmsgF ${pyn} ${fname} "python!'varaddattr'!'long_name'!failed#"${Spyout} ${wrff}
333      pyout=`python ${pyHOME}/nc_var.py -o varaddattr -f ${wrff} -S 'units|'${un} -v ${vn}`
334      pyn=$?
335      Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
336      ferrmsgF ${pyn} ${fname} "python!'varaddattr'!'units'!failed#"${Spyout} ${wrff}
337    fi
338  done
339}
340
341function cleaning_varsfile() {
342# Function to keep only a list of variables from a file
343#   filen= file to clean
344#   keepvars= ':' separated list of variables to keep
345  fname='cleaning_varsfile'
346
347  filen=$1
348  keepvars=$2
349
350  fvars=`${pyHOME}/nc_var.py -o ivars -f ${filen} | grep allvars | tr '=' ' ' |      \
351    awk '{print $3}'`
352  fvs=`echo ${fvars} | tr ':' ' '`
353
354  for fv in ${fvs}; do
355    if ! $(isInlist ${keepvars} ${fv}); then
356      pyout=`python ${pyHOME}/nc_var.py -o varrm -v ${fv} -f ${filen}`
357      pyn=$?
358      Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
359      ferrmsg ${pyn} ${fname} "python!'varrm'!'${fv}'!failed#"${Spyout}
360    fi
361  done
362}
363
364function compute_variable() {
365# Function to compute a variable
366#   idir: directory with the input files
367#   usefiles: ',' list of files as [headerf]@[file1]|...|[fileN]
368#   odir: directory to write the output files
369#   cvar: [CFvarn]|[varkind]|[headerf]|[varmod]|[vardiag]
370#     [CFvarn]: CF name of the variable
371#     [vark]: kind of variable:
372#        acc: temporal accumulated values
373#        diff: differences between models
374#        direct: no statistics
375#        last: last temporal value
376#        Lmean: latitudinal mean values
377#        Lsec: latitudinal section (latitudinal value must be given, [var]@[lat])
378#        lsec: longitudinal section (longitudinal value must be given, [var]@[lat])
379#        lmean: longitudinal mean values
380#        pinterp: pressure interpolation (to the given $plevels)
381#        tmean: temporal mean values
382#        tmean: temporal mean values
383#        xmean: x-axis mean values
384#        ymean: y-axis mean values
385#        zsum: vertical aggregated values
386#     [headerf]: header of the files to use
387#     [modvar]: variable to use from the file
388#     [diagvar]: variable computed from the diagnostics (diagnostic.py)
389#   mdim: name of the dimensions in the model
390#   mvdim: name of the vaariable-dimensions in the model
391#   scratch: whether is has to be done from the scratch or not
392  fname='compute_variable'
393  idir=$1
394  usefiles=$2
395  odir=$3
396  cvar=$4
397  mdim=$5
398  mvdim=$6
399  scratch=$7
400
401  CFvarn=`echo ${cvar} | tr '|' ' ' | awk '{print $1}'`
402  vark=`echo ${cvar} | tr '|' ' ' | awk '{print $2}'`
403  headerf=`echo ${cvar} | tr '|' ' ' | awk '{print $3}'`
404  modvar=`echo ${cvar} | tr '|' ' ' | awk '{print $4}'`
405  diagvar=`echo ${cvar} | tr '|' ' ' | awk '{print $5}'`
406
407  cfiles=`echo ${usefiles} | tr ',' '\n' | grep ${headerf} | tr '|' ' ' | \
408    awk '{print $2}' | tr '@' ' '`
409
410# dimensions
411  dnx=`echo ${mdim} | tr ',' ' ' | awk '{print $1}'`
412  dny=`echo ${mdim} | tr ',' ' ' | awk '{print $2}'`
413# var-dimensions
414  vdnx=`echo ${mvdim} | tr ',' ' ' | awk '{print $1}'`
415  vdny=`echo ${mvdim} | tr ',' ' ' | awk '{print $2}'`
416
417  cd ${odir}
418
419  # Computing separately and then joinging for all files
420  Ntotfiles=`echo ${cfiles} | wc -w | awk '{print $1}'`
421  ifile=1
422
423  for cf in ${cfiles}; do
424    ifS=`printf "%0${Ntotfiles}d" ${ifile}`
425
426# Computing variable
427    # Changing file head when it is a pressure-interpolated variable
428    if test ${vark} == 'pinterp'; then
429      fhead=${headerf}p
430    else
431      fhead=${headerf}
432    fi
433
434    filen=${odir}/${CFvarn}_${fhead}_${ifile}-${Ntotfiles}.nc
435    if ${scratch}; then 
436      rm ${filen} >& /dev/null
437      rm ${odir}/${CFvarn}_${fhead}.nc >& /dev/null
438    fi
439
440    if test ! -f ${filen} && test ! -f ${odir}/${CFvarn}_${fhead}.nc; then
441# Since model direct values are retrieved from `variables_valules.dat' which was initially coincived
442#   as a way to only give variable attributes, range and color bars, if a variable has a diagnostic
443#   way to be computed, the later one will be preferred
444
445      if test ! ${modvar} = 'None' && test ${diagvar} = 'None'; then
446        # model variable
447        values=${modvar}',0,-1,-1'
448        vs=${modvar},${vdnx},${vdny},${vdnz},${vdnt}
449        pyout=`${pyHOME}/nc_var.py -f ${cf} -o DataSetSection_multivars -v ${vs}     \
450          -S ${values} | grep succesfull | awk '{print $6}' | tr '"' ' '`
451        pyn=$?
452        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
453        ferrmsg ${pyn} ${fname} "python!'DataSetSection_multivars'!failed"${Spyout}
454        mv ${pyout} ${filen}
455
456        # Keeping the operations
457        pyins=${pyHOME}"/nc_var.py -f "${cf}" -o DataSetSection_multivars -v "${vs}
458        pyins=${pyins}" -S "${values}
459        echo " " >> ${odir}/all_computevars.inf
460        echo "# ${CFvarn}" "${modvar}" >> ${odir}/all_computevars.inf
461        echo ${pyins} >> ${odir}/all_computevars.inf
462
463        pyout=`${pyHOME}/nc_var.py -f ${filen} -o chvarname -v ${modvar} -S ${CFvarn}`
464        pyn=$?
465        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
466        ferrmsgF ${pyn} ${fname} "python!'chvarname'!failed"${Spyout} ${filen}
467      else
468        # diagnostic variable
469        dims=${dnt}@${vdnt},${dnz}@${vdnz},${dny}@${vdny},${dnx}@${vdnx}
470        diagn=`echo ${diagvar} | tr ':' '\n' | head -n 1`
471        Ndiagvars=`echo ${diagvar} | tr ':' ' ' | wc -w | awk '{print $1}'`
472        idiagv=2
473        diagc=''
474        while test ${idiagv} -le ${Ndiagvars}; do
475          diag1=`echo ${diagvar} | tr ':' '\n' | head -n ${idiagv} | tail -n 1`
476          if test ${idiagv} -eq 2; then
477            diagc=${diag1}
478          else
479            diagc=${diagc}'@'${diag1}
480          fi
481          idiagv=`expr ${idiagv} + 1`
482        done
483        pyout=`python ${pyHOME}/diagnostics.py -f ${cf} -d ${dims} -v ${diagn}'|'${diagc}`
484        pyn=$?
485        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
486        ferrmsg ${pyn} ${fname} "python!'diagnostics'!failed#"${Spyout}
487        mv diagnostics.nc ${filen}
488
489        # Keeping the operations
490        pyins=${pyHOME}"/diagnostics.py -f "${cf}" -d "${dims}" -v '"${diagn}"|"
491        pyins=${pyins}${diagc}"'"
492        echo " " >> ${odir}/all_computevars.inf
493        echo "# ${CFvarn}" "${diagvar}" >> ${odir}/all_computevars.inf
494        echo ${pyins} >> ${odir}/all_computevars.inf
495      fi
496
497      # adding CF lon,lat,time in WRF files
498      if test ${headerf:0:3} = 'wrf'; then
499        WRF_toCF ${filen} ${vdnx} ${vdny}
500      fi
501      # Attaching necessary variables for the pressure interpolation
502      if test ${vark} == 'pinterp'; then
503        requiredinterpvars='P:PB:PSFC:PH:PHB:HGT:T:QVAPOR:'
504        rqvs=`echo ${requiredinterpvars} | tr ':' ' '`
505        echo "  "${fname}": adding variables: "${rqvs}" to allow pressure interpolation"
506        for rqv in ${rqvs}; do
507          pyout=`${pyHOME}/nc_var.py -o fvaradd -S ${cf},${rqv} -f ${filen}`
508          pyn=$?
509          Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
510          ferrmsgF ${pyn} ${fname} "python!'fvaradd'!failed#"${Spyout} ${filen}
511        done
512      fi
513    fi
514
515    ifile=`expr ${ifile} + 1`
516# End of files
517  done
518
519  # Joining variable files
520  filen=${odir}/${CFvarn}_${fhead}.nc
521  if ${scratch}; then rm ${filen} >& /dev/null; fi
522
523  if test ! -f ${filen}; then
524    pyout=`python ${pyHOME}/nc_var.py -f ${CFvarn}'_'${fhead}'_,-,.nc'               \
525      -o netcdf_fold_concatenation_HMT -S ./,time -v all`
526    pyn=$?
527    Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
528    ferrmsg ${pyn} ${fname} "python!'netcdf_fold_concatenation_HMT'!failed#"${Spyout}
529    mv netcdf_fold_concatenated_HMT.nc ${filen}
530    if test -f ${filen}; then
531      rm ${CFvarn}_${fhead}_*-*.nc
532    fi
533  fi
534}
535
536function compute_statistics(){
537# Function to compute different statistics
538#   idir: directory with the input files
539#   usefiles: ',' list of files to use [file1],...,[fileN]
540#   odir: directory to write the output files
541#   cvar: [CFvarn]|[varkind]|[headerf]|[varmod]|[vardiag]
542#     [CFvarn]: CF name of the variable
543#     [vark]: kind of variable:
544#        acc: temporal accumulated values
545#        diff: differences between models
546#        direct: no statistics
547#        last: last temporal value
548#        Lmean: latitudinal mean values
549#        Lsec: latitudinal section (latitudinal value must be given, [var]@[lat])
550#        lmean: longitudinal mean values
551#        lsec: longitudinal section (longitudinal value must be given, [var]@[lat])
552#        pinterp: pressure interpolation (to the given $plevels)
553#        tmean: temporal mean values
554#        xmean: x-axis mean values
555#        ymean: y-axis mean values
556#        zsum: vertical aggregated values
557#     [headerf]: header of the files to use
558#     [modvar]: variable to use from the file
559#     [diagvar]: variable computed from the diagnostics (diagnostic.py)
560#   mdim: name of the dimensions in the model
561#   mvdim: name of the vaariable-dimensions in the model
562#   scratch: whether is has to be done from the scratch or not
563  fname='compute_statistics'
564
565  idir=$1
566  usefiles=$2
567  odir=$3
568  cvar=$4
569  mdim=$5
570  mvdim=$6
571  scratch=$7
572
573  # Getting a previous file name to continue with(for combinations)
574  if test $# -eq 8; then
575    echo ${warnmsg}
576    echo "  "${fname}": using a previous file '"$8"' to continue with!!"
577    prevfile=$8
578    usefiles=$8
579  fi
580
581  CFvarn=`echo ${cvar} | tr '|' ' ' | awk '{print $1}'`
582  vark=`echo ${cvar} | tr '|' ' ' | awk '{print $2}'`
583  headerf=`echo ${cvar} | tr '|' ' ' | awk '{print $3}'`
584  modvar=`echo ${cvar} | tr '|' ' ' | awk '{print $4}'`
585  diagvar=`echo ${cvar} | tr '|' ' ' | awk '{print $5}'`
586
587  cfiles=`echo ${usefiles} | tr ',' ' '`
588
589# dimensions
590  dnx=`echo ${mdim} | tr ',' ' ' | awk '{print $1}'`
591  dny=`echo ${mdim} | tr ',' ' ' | awk '{print $2}'`
592# var-dimensions
593  vdnx=`echo ${mvdim} | tr ',' ' ' | awk '{print $1}'`
594  vdny=`echo ${mvdim} | tr ',' ' ' | awk '{print $2}'`
595
596  cd ${odir}
597
598  if test $# -ne 8; then
599    if test ${vark} == 'pinterp'; then
600      fhead=${headerf}p
601    else
602      fhead=${headerf}
603    fi
604    filen=${CFvarn}_${fhead}_${vark}.nc
605  else
606    Lprevfile=`expr length ${prevfile}`
607    Lprevfile3=`expr ${Lprevfile} - 3`
608    filen=${prevfile:0:$Lprevfile3}_${vark}.nc
609  fi
610  if ${scratch}; then rm ${filen} >& /dev/null; fi
611
612  if test ! -f ${filen}; then
613    # Computing stats
614    case ${vark} in
615       # temporal accumulated values
616      'acc') 
617        echo "  "${fname}": kind '"${vark}"' not ready !!"
618        exit
619      ;;
620      # differences between models
621      'diff')
622        echo "  "${fname}": kind '"${vark}"' not ready !!"
623        exit
624      ;;
625      # no statistics
626      'direct')
627        cp ${cfiles} ${filen}
628      ;;
629      # last temporal value
630      'last')
631        Lcfiles=`expr length ${cfiles}`
632        Lcfiles3=`expr ${Lcfiles} - 3`
633        vals='time,-9,0,0'
634        pyout=`python ${pyHOME}/nc_var.py -o DataSetSection -S ${vals} -f ${cfiles}  \
635          -v ${CFvarn}`
636        pyn=$?
637        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
638        ferrmsg ${pyn} ${fname} "python!'DataSetSection'!'last'!failed#"${Spyout}
639        mv ${cfiles:0:${Lcfiles3}}_time_B-9-E0-I0.nc ${filen}
640
641        # Keeping the operations
642        pyins=${pyHOME}"/nc_var.py -o DataSetSection -S "${vals}" -f "${cfiles}
643        pyins=${pyins}"-v "${CFvarn}
644        echo " " >> ${odir}/all_statsvars.inf
645        echo "# ${CFvarn}" "${vark}" >> ${odir}/all_statsvars.inf
646        echo ${pyins} >> ${odir}/all_statsvars.inf
647      ;;
648      # latitudinal mean values
649      'Lmean')
650        echo "  "${fname}": kind '"${vark}"' not ready !!"
651        exit
652      ;;
653      # latitudinal section (latitudinal value must be given, [var]@[lat])
654      'Lsec')
655        echo "  "${fname}": kind '"${vark}"' not ready !!"
656        exit
657      ;;
658      # longitudinal section (longitudinal value must be given, [var]@[lat])
659      'lsec')
660        echo "  "${fname}": kind '"${vark}"' not ready !!"
661        exit
662      ;;
663      # longitudinal mean values
664      'lmean')
665        echo "  "${fname}": kind '"${vark}"' not ready !!"
666        exit
667      ;;
668      # pinterp: pressure interpolation (to the given $plevels)
669      'pinterp')
670        vals=${plevels}',1,1'
671        pyout=`python $pyHOME/nc_var.py -o pinterp -f ${cfiles} -S ${vals}           \
672          -v ${CFvarn}`
673        pyn=$?
674        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
675        ferrmsg ${pyn} ${fname} "python!'pinterp'!failed#"${Spyout}
676        cp pinterp.nc ${filen}
677
678        # Keeping the operations
679        pyins=${pyHOME}"/nc_var.py -o pinterp -S "${vals}" -f "${cfiles}
680        pyins=${pyins}"-v "${CFvarn}
681        echo " " >> ${odir}/all_statsvars.inf
682        echo "# ${CFvarn}" "${vark}" >> ${odir}/all_statsvars.inf
683        echo ${pyins} >> ${odir}/all_statsvars.inf
684
685        # adding CF lon,lat,time in WRF files
686        if test ${headerf:0:3} = 'wrf'; then
687          WRF_toCF ${filen} ${vdnx} ${vdny}
688        fi
689      ;;
690      # temporal mean values
691      'tmean')
692        vals='time|-1,time,mean,lon:lat:'${vdnz}':time'
693        dims='time@time,'${dnz}'@'${vdnz}',lat@lat,lon@lon'
694
695        pyout=`python ${pyHOME}/nc_var.py -o file_oper_alongdims -S ${vals}          \
696          -f ${cfiles} -v ${CFvarn}`
697        pyn=$?
698        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
699        ferrmsg ${pyn} ${fname} "python!'file_oper_alongdims'!'tmean'!failed#"${Spyout}
700        mv file_oper_alongdims_mean.nc ${filen}
701
702        # Keeping the operations
703        pyins=${pyHOME}"/nc_var.py -o file_oper_alongdims -S '"${vals}"' -f "${cfiles}
704        pyins=${pyins}" -v "${CFvarn}
705        echo " " >> ${odir}/all_statsvars.inf
706        echo "# ${CFvarn}" "${vark}" >> ${odir}/all_statsvars.inf
707        echo ${pyins} >> ${odir}/all_statsvars.inf
708
709        varkeep=':'${CFvarn}'mean:timestats'
710      ;;
711      # x-axis mean values
712      'xmean')
713        vals='lon|-1,lon,mean,lon:lat:'${vdnz}':time'
714        dims='time@time,'${dnz}'@'${vdnz}',lat@lat,lon@lon'
715
716        pyout=`python ${pyHOME}/nc_var.py -o file_oper_alongdims -S ${vals}         \
717          -f ${cfiles} -v ${CFvarn}`
718        pyn=$?
719        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
720        ferrmsg ${pyn} ${fname} "python!'file_oper_alongdims'!'tmean'!failed#"${Spyout}
721        mv file_oper_alongdims_mean.nc ${filen}
722
723        # Keeping the operations
724        pyins=${pyHOME}"/nc_var.py -o file_oper_alongdims -S '"${vals}"' -f "${cfiles}
725        pyins=${pyins}" -v "${CFvarn}
726        echo " " >> ${odir}/all_statsvars.inf
727        echo "# ${CFvarn}" "${vark}" >> ${odir}/all_statsvars.inf
728        echo ${pyins} >> ${odir}/all_statsvars.inf
729
730        varkeep=':'${CFvarn}'mean:lonstats'
731      ;;
732      # y-axis mean values
733      'ymean')
734        vals='lat|-1,lat,mean,lon:lat:'${vdnz}':time'
735        dims='time@time,'${dnz}'@'${vdnz}',lat@lat,lon@lon'
736
737        pyout=`python ${pyHOME}/nc_var.py -o file_oper_alongdims -S ${vals}         \
738          -f ${cfiles} -v ${CFvarn}`
739        pyn=$?
740        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
741        ferrmsg ${pyn} ${fname} "python!'file_oper_alongdims'!'tmean'!failed#"${Spyout}
742        mv file_oper_alongdims_mean.nc ${filen}
743
744        # Keeping the operations
745        pyins=${pyHOME}"/nc_var.py -o file_oper_alongdims -S '"${vals}"' -f "${cfiles}
746        pyins=${pyins}" -v "${CFvarn}
747        echo " " >> ${odir}/all_statsvars.inf
748        echo "# ${CFvarn}" "${vark}" >> ${odir}/all_statsvars.inf
749        echo ${pyins} >> ${odir}/all_statsvars.inf
750
751        varkeep=':'${CFvarn}'mean:latstats'
752      ;;
753      # vertical aggregated values
754      'zsum')
755        echo "  "${fname}": kind '"${vark}"' not ready !!"
756        exit
757      ;;
758      *)
759        echo ${errmsg}
760        echo "  "${fname}": kind '"${vark}"' not ready !!"
761        exit
762      ;;
763    esac
764    cleaning_varsfile ${filen} ${CFvarn}':lon:lat:pres:time'${varkeep}
765  fi
766
767}
768
769function draw_plot(){
770# Function to carry out the drawing of the plots
771#   plot= [kplot]+[varn1]|[kind1]#[varn2]|[kind2]#....
772#     diffmap2Dsfc: 2D map of surface differences values of 1 variable
773#     diffmap2Dz: 2D map of 3D differences values of 1 variable
774#     hovmsfc: Hovmoeller diagrams of 1 variable at the surface
775#     map2Dsfc: 2D map of surface values of 1 variable
776#     map3D: 2D map of 3D values of 1 variable
777#     shadcount2D: 2D map of shadow (1st variable) and countour (2nd variable) [stvar1]#[stvar2]
778#
779#     ':' separated list of statitsics variable values are given as: [var]|[kind(including combo values)]
780#     in figures with more than 1 variable, use '#' to separate them
781#   odir= working directory
782#   headf= head of the file
783  fname='draw_plot'
784
785  plot=$1
786  odir=$2
787  headf=$3
788  scratch=$4
789
790  cd ${odir}
791
792  kplot=`echo ${plot} | tr '+' ' ' | awk '{print $1}'`
793  plotvals=`echo ${plot} | tr '+' ' ' | awk '{print $2}'`
794
795  # Assuming that multiple variables will be separated by '#'
796  nvars=`echo ${plotvals} | tr '#' ' ' | wc -w | awk '{print $1}'`
797
798  figfiles=''
799  varinfilen=''
800  CFvars=''
801  figvarparam=''
802  varkinds=''
803  if test ${nvars} -ge 1; then
804    figname=${kplot}_${headf}
805    iv=1
806    while test $iv -le ${nvars}; do
807      varkind=`echo ${plotvals} | tr '#' '\n' | head -n ${iv} | tail -n 1`
808      varn=`echo ${varkind} | tr '|' ' ' | awk '{print $1}'`
809      kind=`echo ${varkind} | tr '|' ' ' | awk '{print $2}'`
810      # Do we have processed the given variable?
811      nfiles=`ls -1 ${odir}/${varn}_${headf}*.nc | wc -l | awk '{print $1}'`
812      if test ${nfiles} -eq 0; then
813        echo "  "${fname}": there are no files for variable '"${varn}"' skiping it !!"
814        return
815      fi
816
817      if $(isInlist ${varmeanname} ${kind}); then 
818        vn=${varn}'mean'
819      else
820        vn=${varn}
821      fi
822      filen=`stats_filename ${varn} ${kind} ${headf}`
823      if test ${iv} -eq 1; then
824        varkinds=${varkind}
825        CFvars=${varn}
826        varinfilen=${vn}
827        figfiles=${filen}
828      else
829        varkinds=${varkinds}':'${varkind}
830        CFvars=${CFvars}':'${varn}
831        varinfilen=${varinfilen}':'${vn}
832        figfiles=${figfiles}':'${filen}
833      fi
834
835      figname=${figname}'_'${vn}'-'${kind}
836
837      # Getting plot variable configuration
838      specific=false
839      if $(isInlist ${specificvarplot} ${varn}); then
840        vals=`listfilter ${specificvarplot} ${varn}`
841        kvals=`listfilter ${specificvarplot} ${kind}`
842        Lkvals=`expr length ${kvals}'0'`
843        if test ${Lkvals} -gt 1; then
844          fkvals=`listfilter ${kvals} ${kplot}`
845          Lfkvals=`expr length ${fkvals}'0'`
846          if test ${Lfkvals} -gt 1; then
847            specific=true
848            minval=`echo ${fkvals} | tr '|' ' ' | awk '{print $4}'`
849            maxval=`echo ${fkvals} | tr '|' ' ' | awk '{print $5}'`
850            colorbar=`echo ${fkvals} | tr '|' ' ' | awk '{print $6}'`
851            cntformat=`echo ${fkvals} | tr '|' ' ' | awk '{print $7}'`
852            colorcnt=`echo ${fkvals} | tr '|' ' ' | awk '{print $8}'`
853          fi
854        fi
855      fi
856      if ! ${specific}; then
857        allvals=`python ${pyHOME}/drawing.py -o variable_values -S ${varn} |         \
858          grep all_values | awk '{print $3}'`
859        pyn=$?
860        ferrmsg ${pyn} ${fname} "python!'variable_values'!'${varn}'!failed#"
861        minval=`echo ${allvals} | tr ',' ' ' | awk '{print $3}'`
862        maxval=`echo ${allvals} | tr ',' ' ' | awk '{print $4}'`
863        colorbar=`echo ${allvals} | tr ',' ' ' | awk '{print $7}'`
864        cntformat='%g'
865        colorcnt='black'
866      fi
867
868      # Graphical paramters for each variable in the plot
869      if test ${iv} -eq 1; then
870        figvarparam=${minval}'|'${maxval}'|'${colorbar}'|'${cntformat}'|'${colorcnt}
871      else
872        figvarparam=${figvarparam}':'${minval}'|'${maxval}'|'${colorbar}'|'
873        figvarparam=${figvarparam}${cntformat}'|'${colorcnt}
874      fi
875
876      iv=`expr ${iv} + 1`
877    # End of number of variables of the plot
878    done
879  fi
880  figname=${figname}.${kindfig}
881
882  if ${scratch}; then rm ${figname} >& /dev/null; fi
883  if test ! -f ${figname}; then
884    case ${kplot} in
885      'diffmap2Dsfc')
886        echo "  "${fname}": kind of plot '"${kplot}"' not ready !!"
887        exit
888      ;;
889      'diffmap2Dz')
890        echo "  "${fname}": kind of plot '"${kplot}"' not ready !!"
891        exit
892      ;;
893      'hovmsfc')
894        echo "  "${fname}": kind of plot '"${kplot}"' not ready !!"
895        exit
896      ;;
897      'map2Dsfc')
898        echo "  "${fname}": kind of plot '"${kplot}"' not ready !!"
899        exit
900      ;;
901      'map3D')
902        echo "  "${fname}": kind of plot '"${kplot}"' not ready !!"
903        exit
904      ;;
905      'shadcount2Dsfc')
906        figtit=`echo ${varkinds} | tr ':' '|'`
907        shdstdn=`echo ${CFvars} | tr ':' ' ' | awk '{print $1}'`
908        cntstdn=`echo ${CFvars} | tr ':' ' ' | awk '{print $2}'`
909        figfs=`echo ${figfiles} | tr ':' ','`
910        srange=`echo ${figvarparam} | tr ':' ' ' | awk '{print $1}' | tr '|' ' ' |   \
911          awk '{print $1","$2}'`
912        cbar=`echo ${figvarparam} | tr ':' ' ' | awk '{print $1}' | tr '|' ' ' |     \
913          awk '{print $3}'`
914        crange=`echo ${figvarparam} | tr ':' ' ' | awk '{print $2}' | tr '|' ' ' |   \
915          awk '{print $1","$2}'`
916        cline=`echo ${figvarparam} | tr ':' ' ' | awk '{print $2}' | tr '|' ' ' |    \
917          awk '{print $5}'`
918        cfmt=`echo ${figvarparam} | tr ':' ' ' | awk '{print $2}' | tr '|' ' ' |     \
919          awk '{print $4}'`
920
921        graphvals=$(echo ${CFvars} | tr ':' ',')
922        graphvals=${graphvals}':lon|-1,lat|-1:lon|-1,lat|-1:lon:lat:'${cbar}':fixc,'
923        graphvals=${graphvals}${cline}':'${cfmt}':'${srange}':'${crange}',9:'
924        graphvals=${graphvals}${figtit}':'${kindfig}':False:'${mapval}
925 
926        plotins="python "${pyHOME}"/drawing.py -f "${figfs}" -o draw_2D_shad_cont "
927        plotins=${plotins}"-S '"${graphvals}"' -v "$(echo ${varinfileb} | tr ':' ',')
928        echo " " >> ${odir}/all_figures.inf
929        echo "#"$(echo $f{igtit} | tr '|' ' ') >> ${odir}/all_figures.inf
930        echo ${plotins} >> ${odir}/all_figures.inf
931        pyout=`python ${pyHOME}/drawing.py -f ${figfs} -o draw_2D_shad_cont          \
932          -S ${graphvals} -v $(echo ${varinfilen} | tr ':' ',')`
933        pyn=$?
934        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
935        ferrmsg ${pyn} ${fname} "python!'draw_2D_shad_cont'!failed#"${Spyout}
936        mv 2Dfields_shadow-contour.${kindfig} ${figname}
937#        exit
938      ;;
939      *)
940        echo ${errormsg}
941        echo "  "${fname}": plot kind '"${kplot}"' not ready !!"
942        exit
943      ;;
944    esac
945  fi
946
947#LLUIS
948
949}
950
951#######    #######
952## MAIN
953    #######
954rootsh=`pwd`
955
956# Uploading environment
957uploadvars model_graphics.dat
958
959if test ${scratch} = 'true'; then
960  scratch=true
961  echo ${warnmsg}
962  echo "  "${main}": starting from the SCRATCH !!"
963  echo "    10 seconds left!!"
964  filescratch=true
965  figscratch=true
966  sleep 10
967else
968  scratch=false
969  if test ${filescratch} = 'true'; then
970    filescratch=true
971    echo ${warnmsg}
972    echo "  "${main}": files starting from the SCRATCH !!"
973    echo "    5 seconds left!!"
974    sleep 5
975  else
976    filescratch=false
977  fi
978  if test ${figscratch} = 'true'; then
979    figscratch=true
980    echo ${warnmsg}
981    echo "  "${main}": figures starting from the SCRATCH !!"
982    echo "    5 seconds left!!"
983    sleep 5
984  else
985    figscratch=false
986  fi
987fi
988
989if test ${debug} = 'true'; then
990  dbg=true
991else
992  dbg=false
993fi
994
995timeval='tstep'
996zval='null'
997dimval='lon,lat,pressure,time'
998
999compute=0
1000computetlon=false
1001plot=false
1002single2Dplot=false
1003lonZsec=false
1004differences=true
1005
1006combosfile=${HOMEpy}/diagnostics.inf
1007
1008####### ###### ##### #### ### ## #
1009
1010mods=`echo ${models} | tr ':' ' '`
1011varks=`echo ${varkinds} | tr ':' ' '`
1012
1013# Models loop
1014##
1015for mod in ${mods}; do
1016  case ${mod} in
1017    'WRF')
1018      exps=`echo ${WRFexps} | tr ':' ' '`
1019      fheaders=`echo ${WRFheaders} | tr ':' ' '`
1020    ;;
1021    'LMDZ')
1022      exps=`echo ${LMDZexps} | tr ':' ' '`
1023      fheaders=`echo ${LMDZheaders} | tr ':' ' '`
1024    ;;
1025    'WRF_LMDZ')
1026      exps=`echo ${WRF_LMDZexps} | tr ':' ' '`
1027      fheaders=`echo ${WRF_LMDZheaders} | tr ':' ' '`
1028    ;;
1029    '*')
1030      echo ${errmsg}
1031      echo "  "${main}": model '"${mod}"' not ready!!"
1032      exit
1033    ;;
1034  esac 
1035
1036  modinf=`$pyHOME/nc_var.py -o model_characteristics -f None -S ${mod} | \
1037    grep singleline | awk '{print $2}'`
1038  ferrmsg $? $main "python!'model_characteristics'!failed"
1039  dnx=`echo ${modinf} | tr ';' '\n' | grep dimxn | tr '=' ' ' | awk '{print $2}'`
1040  dny=`echo ${modinf} | tr ';' '\n' | grep dimyn | tr '=' ' ' | awk '{print $2}'`
1041  dnz=`echo ${modinf} | tr ';' '\n' | grep dimzn | tr '=' ' ' | awk '{print $2}'`
1042  dnt=`echo ${modinf} | tr ';' '\n' | grep dimtn | tr '=' ' ' | awk '{print $2}'`
1043  vdnx=`echo ${modinf} | tr ';' '\n' | grep vardxn | tr '=' ' ' | awk '{print $2}'`
1044  vdny=`echo ${modinf} | tr ';' '\n' | grep vardyn | tr '=' ' ' | awk '{print $2}'`
1045  vdnz=`echo ${modinf} | tr ';' '\n' | grep vardzn | tr '=' ' ' | awk '{print $2}'`
1046  vdnt=`echo ${modinf} | tr ';' '\n' | grep vardtn | tr '=' ' ' | awk '{print $2}'`
1047  echo ${mod}
1048  echo "  dims: "${dnx}", "${dny}", "${dnz}", "${dnt}
1049  echo "  var dims: "${vdnx}", "${vdny}", "${vdnz}", "${vdnt}
1050  moddims=${dnx}','${dny}','${dnz}','${dnt}
1051  modvdims=${vdnx}','${vdny}','${vdnz}','${vdnt}
1052
1053# Experiments loop
1054##
1055  for exp in ${exps}; do
1056    echo "  "${exp}"..."
1057    iwdir=${ifold}/${mod}/${exp}
1058
1059    # Does input folder exist?
1060    if test ! -d ${iwdir}; then
1061      echo ${errmsg}
1062      echo "  "${main}": folder '"${iwdir}"' does not exist !!"
1063      exit
1064    fi
1065
1066    owdir=${ofold}/${mod}/${exp}
1067    mkdir -p ${owdir}
1068
1069    # Need to pass to analyze all the data?
1070    if ${filescratch}; then 
1071      rm ${owdir}/varcompute.inf >& /dev/null
1072      rm ${owdir}/all_computevars.inf >& /dev/null
1073      rm ${owdir}/all_statsvars.inf >& /dev/null
1074
1075      echo "## Computation of variables " > ${owdir}/all_computevars.inf
1076      echo "## Computation of statistics " > ${owdir}/all_statsvars.inf
1077
1078    fi
1079
1080
1081    if test ! -f ${owdir}/varcompute.inf; then
1082
1083      # Does input folder has header files?
1084      cd ${iwdir}
1085      files=''
1086      testfiles=''
1087      ih=1
1088      for fh in ${fheaders}; do
1089        if ${filescratch}; then rm ${owdir}/*_${fh}*.nc >& /dev/null; fi
1090
1091        files1h=`ls -1 ${fh}* | tr '\n' '@'`
1092        Lfiles1h=`expr length ${files1h}'0'`
1093        if test ${Lfiles1h} -lt 2; then
1094          ferrmsg 1 $main "folder!:!"${iwdir}"!does!not!contain!files!"${fh}"*"
1095        fi
1096        testfiles1h=`ls -1 ${fh}* | head -n 1`
1097        if test ${ih} -eq 1; then
1098          files=${fh}'|'${files1h}
1099          testfiles=${fh}'|'${testfiles1h}
1100        else
1101          files=${files}','${fh}'|'${files1h}
1102          testfiles=${testfiles}'@'${fh}'|'${testfiles1h}
1103        fi
1104        ih=`expr ${ih} + 1`
1105      done
1106      testfs=`echo ${testfiles} | tr '@' ' '`
1107      cd ${rootsh}
1108
1109# Kind variables loop
1110##
1111      combo=false
1112      ik=1
1113      itotv=1
1114      for vark in ${varks}; do
1115        echo "    "${vark}" ..."
1116        case ${vark} in
1117          'acc')
1118            varvks=${varacc}
1119          ;;
1120          'combo')
1121            combo=true
1122            varvcombo=${varcombo}
1123            varcos=`echo ${varvcombo} | tr ':' ' '`
1124            varvks=''
1125            ivco=1
1126            for vco in ${varcos}; do
1127              vn=`echo ${vco} | tr '@' ' ' | awk '{print $1}'`
1128              if test ${ivco} -eq 1; then
1129                varvks=${vco}
1130              else
1131                varvks=${varvks}':'${vco}
1132              fi
1133              ivco=`expr ${ivco} + 1`
1134            done
1135          ;;
1136          'diff')
1137            varvks=${vardiff}
1138          ;;
1139          'direct')
1140            varvks=${vardirect}
1141          ;;
1142          'last')
1143            varvks=${varlast}
1144          ;;
1145          'Lmean')
1146            varvks=${varLmean}
1147          ;;
1148          'Lsec')
1149            varvks=${varLsec}
1150          ;;
1151          'lmean')
1152            varvks=${varlmean}
1153          ;;
1154          'lsec')
1155            varvks=${varlsec}
1156          ;;
1157          'pinterp')
1158            varvks=${varpinterp}
1159          ;;
1160          'tmean')
1161            varvks=${vartmean}
1162          ;;
1163          'xmean')
1164            varvks=${varxmean}
1165          ;;
1166          'ymean')
1167            varvks=${varymean}
1168          ;;
1169          'zsum')
1170            varvks=${varzsum}
1171          ;;
1172          '*')
1173            echo ${errmsg}
1174            echo "  "${main}": variable kind '"${vark}"' not ready!!"
1175            exit
1176          ;;
1177        esac
1178
1179# Do we have variables for this kind?
1180        Lvarvks=`expr length ${varvks}'0'`
1181        if test ${Lvarvks} -lt 2; then
1182          ferrmsg 1 ${main} "variable!kind!"${vark}"!without!variables"
1183        fi
1184        if test ${ik} -eq 1; then
1185          allvars=${varvks}
1186        else
1187          allvars=${allvars}':'${varvks}
1188        fi
1189        ik=`expr ${ik} + 1`
1190        vars=`echo ${varvks} | tr ':' ' '`
1191
1192# Variables loop
1193##
1194        iv=1
1195        for var in ${vars}; do
1196          echo "      "${var}
1197          # How to compute it?
1198          varcomp=`variable_compute ${iwdir} ${var} ${testfiles} | grep varcomp |    \
1199            awk '{print $2}'`
1200
1201          if test ${itotv} -eq 1; then
1202            varcompute=${varcomp}
1203          else
1204            varcompute=${varcompute}';'${varcomp}
1205          fi
1206
1207          iv=`expr ${iv} + 1`
1208          itotv=`expr ${itotv} + 1`
1209
1210# end loop of variables
1211        done
1212# end of kind of variables
1213      done
1214
1215      # Outwritting the varcompute to avoid next time (if it is not filescratch!)
1216      cat << EOF > ${owdir}/varcompute.inf
1217files: ${files}
1218varcompute: ${varcompute}
1219itotv: ${itotv}
1220testfs: ${testfiles}
1221EOF
1222    else
1223      echo $warnmsg
1224      echo "  "${main}": getting already data information from the experiment!"
1225      files=`cat ${owdir}/varcompute.inf | grep files | awk '{print $2}'`
1226      varcompute=`cat ${owdir}/varcompute.inf | grep varcompute | awk '{print $2}'`
1227      itotv=`cat ${owdir}/varcompute.inf | grep itotv | awk '{print $2}'`
1228      testfiles=`cat ${owdir}/varcompute.inf | grep testfs | awk '{print $2}'`
1229# End of avoiding to repeat all the experiment search
1230    fi
1231
1232    echo "  For experiment '"${exp}"' is required to compute: "${itotv}" variables"
1233# Computing files for each variable
1234##
1235    echo "      Computing all variables ..."
1236    cd $owdir
1237    ic=1
1238    isc=1
1239    cvars=`echo ${varcompute} | tr ';' ' '`
1240    for cvar in ${cvars}; do
1241      CFv=`echo ${cvar} | tr '|' ' ' | awk '{print $1}'`
1242      vark=`echo ${cvar} | tr '|' ' ' | awk '{print $2}'`
1243      fileh=`echo ${cvar} | tr '|' ' ' | awk '{print $3}'`
1244      modv=`echo ${cvar} | tr '|' ' ' | awk '{print $4}'`
1245      diagv=`echo ${cvar} | tr '|' ' ' | awk '{print $5}'`
1246      if ${dbg}; then echo "        "${CFv}"; "${modv}" "${diagv}; fi
1247
1248      if test ! ${modv} = 'None' || test ! ${diagv} = 'None'; then
1249        compute_variable ${iwdir} ${files} ${owdir} ${cvar} ${moddims} ${modvdims}   \
1250          ${filescratch}
1251        ic=`expr ${ic} + 1`
1252
1253        if test ! ${vark} = 'diff' && test ! ${vark} = 'combo'; then
1254          if test ${vark} = 'pinterp'; then
1255            fhead=${fileh}'p'
1256          else
1257            fhead=${fileh}
1258          fi
1259          compute_statistics ${iwdir} ${CFv}_${fhead}.nc ${owdir} ${cvar}            \
1260            ${moddims} ${modvdims} ${filescratch}
1261          isc=`expr ${isc} + 1`
1262        else
1263          if test ${vark} = 'diff'; then
1264            echo "  "${main}": differences will be calculated when all the "         \
1265              "model/experiments will be done !"
1266          elif test ${vark} = 'diff'; then
1267            echo "  "${main}": combos will be calculated later when all the "        \
1268              "variables will be done !"
1269          fi
1270        fi
1271      else
1272        if ${dbg}; then echo "        not '"${CFv}"' for model '"${mod}"' !!"; fi
1273      fi
1274
1275      # exit
1276# end of computing vars
1277    done
1278
1279    # Computing combos
1280    if ${combo}; then
1281      echo "      Computing combos: "${varcombo}
1282      varcos=`echo ${varcombo} | tr ':' ' '`
1283      for vco in ${varcos}; do
1284        echo "        "${vco}
1285        CFvarn=`echo ${vco} | tr ';' ' ' | awk '{print $1}'`
1286        cvar=`variable_compute ${iwdir} ${CFvarn} ${testfiles} | grep varcomp |      \
1287          awk '{print $2}'`
1288        CFv=`echo ${cvar} | tr '|' ' ' | awk '{print $1}'`
1289        vark=`echo ${cvar} | tr '|' ' ' | awk '{print $2}'`
1290        fileh=`echo ${cvar} | tr '|' ' ' | awk '{print $3}'`
1291        modv=`echo ${cvar} | tr '|' ' ' | awk '{print $4}'`
1292        diagv=`echo ${cvar} | tr '|' ' ' | awk '{print $5}'`
1293
1294        combs=`echo ${vco} | tr ';' ' ' | awk '{print $2}' | tr '@' ' '`
1295        ifile=${CFv}_${fhead}
1296        for comb in ${combs}; do
1297          combcvar=${CFv}'|'${comb}'|'${fileh}'|'${modv}'|'${diagv}
1298          compute_statistics ${iwdir} ${ifile}.nc ${owdir} ${combcvar} ${moddims}    \
1299            ${modvdims} ${filescratch} ${ifile}.nc
1300          ifile=${ifile}_${comb}
1301          isc=`expr ${isc} + 1`
1302        # End of combo
1303        done
1304      # End of combo variables
1305      done
1306    fi
1307
1308    echo "  "${main}": "${ic}" variables has been computed"
1309    echo "  "${main}": "${isc}" statistics has been computed"
1310
1311# Direct plotting
1312##
1313    echo "  "${main}": Plotting direct figures ..."
1314    if ${figscratch}; then rm directplotsdraw.inf; fi
1315    if test ! -f directplotsdraw.inf; then
1316      drwplts=`echo ${drawplots} | tr ':' ' '`
1317      ikp=1
1318      plots=''
1319      for drw in ${drwplts}; do
1320        if $(isInlist ${directplots} ${drw}); then
1321          case ${drw} in
1322            'hovmsfc')
1323              plts=${plothovmsfc}
1324            ;;
1325            'map2Dsfc')
1326              plts=${pltmap2Dsfc}
1327            ;;
1328            'map3D')
1329              plts=${pltmap3D}
1330            ;;
1331            'shadcount2Dsfc')
1332              plts=${pltshadcount2Dsfc}
1333            ;;
1334            *)
1335              echo ${errmsg}
1336              echo "  "${main}": plot kind '"${drw}"' not ready !!"
1337              exit
1338          esac
1339          # Do we have plots for this kind?
1340          Lkplots=`expr length ${plts}'0'`
1341          if test ${Lkplots} -lt 2; then
1342            ferrmsg 1 ${main} "plot!kind!"${drw}"!without!variables"
1343          fi
1344          kplots=`list_add_label ${plts} ${drw} + beg`
1345          if test ${ikp} -eq 1; then
1346            plots=${kplots}
1347          else
1348            plots=${plots}':'${kplots}
1349          fi
1350          ikp=`expr ${ikp} + 1`
1351        fi
1352# End of coincident direct plots
1353      done
1354      cat << EOF > directplotsdraw.inf
1355plots= ${plots}
1356EOF
1357    else
1358      echo ${warnmsg}
1359      echo "  "${main}": retrieving direct plots from existing file 'directplotsdraw.inf' !!"
1360      plots=`cat directplotsdraw.inf | grep plots | awk '{print $2}'`
1361    fi
1362
1363    if ${figscratch}; then rm ${owdir}/all_figures.inf; fi
1364    if test ! -f ${owdir}/all_figures.inf; then
1365      echo "###### List of all figures" > ${owdir}/all_figures.inf
1366    fi
1367
1368    pts=`echo ${plots} | tr ':' ' '`
1369    idp=1
1370    for pt in ${pts}; do
1371      echo "        "${pt}
1372      draw_plot ${pt} ${owdir} ${fileh} ${figscratch}
1373      idp=`expr ${idp} + 1`
1374    done
1375
1376    echo "  "${main}": "${idp}" direct plots hav been computed"
1377# LLUIS
1378    cd ${rootsh}
1379    exit
1380# end of experiments
1381  done
1382# end of models
1383done
1384
1385exit
1386
1387istep=0
1388
1389if test ${compute} -eq 1; then
1390  for exp in ${exps}; do
1391    echo "exp: "${exp}
1392    if test ${exp} == 'AR40'; then
1393      var2Dtmeans=${var2Dtmeans_AR40}
1394    else
1395      var2Dtmeans=${var2Dtmeans_NPv31}
1396    fi
1397    for vark in ${varks}; do
1398      echo "  vark: "${vark}
1399
1400      if test ${vark} = 'mean'; then
1401        fileorig=${ifoldmean}/${exp}/${filenmean}
1402        if test ! -f ${fileorig}; then
1403          fileworig=${filensfc}
1404          dvals='T:Time,Z:bottom_top,Y:south_north,X:west_east'
1405          dimnames='T:Time,Z:bottom_top,Y:south_north,X:west_east'
1406          python ${HOMEpy}/vertical_interpolation.py                            \
1407            -f ${ofold}/${exp}/${filensfc} -o WRFp -i ${plevels} -k 'lin'            \
1408            -v WRFt,U,V,WRFrh,WRFght -d ${dimnames} -D T:Times,Z:ZNU,Y:XLAT,X:XLONG
1409          if test $? -ne 0; then
1410            echo ${errormsg}
1411            echo "  python failed!"
1412            echo "  python ${HOMEpy}/vertical_interpolation.py"                            \
1413            "-f ${ofold}/${exp}/${filensfc} -o WRFp -i ${plevels} -k 'lin'"            \
1414            "-v WRFt,U,V,WRFrh,WRFght -d ${dimnames} -D T:Times,Z:ZNU,Y:XLAT,X:XLONG"
1415            rm ${ofold}/${exp}/vertical_interpolation_WRFp.nc >& /dev/null
1416            exit
1417          fi
1418          mv vertical_interpolation_WRFp.nc ${ofold}/${exp}
1419        fi
1420        vars=`echo ${mean_variables} | tr ':' ' '`
1421        ofile='mean_variables'
1422      elif test ${vark} = 'sfc'; then
1423        fileorig=${ifoldsfc}/${exp}/${filensfc}
1424        if test ! -f ${fileorig}; then
1425          fileworig=${filensfc}
1426          cp ${ifoldsfc}/${exp}/${fileworig} ${ifoldsfc}/${exp}/${filensfc}
1427          python ${HOMEpy}/nc_var.py -o WRF_CFxtime_creation -f ${ifoldsfc}/${exp}/${filensfc} \
1428            -S 19491201000000,hours -v time
1429          python ${HOMEpy}/nc_var.py -o WRF_CFlonlat_creation -f ${ifoldsfc}/${exp}/${filensfc}\
1430            -S longitude,latitude -v XLONG,XLAT
1431        fi
1432        if test ${exp} = 'AR40'; then
1433          vars=`echo ${sfc_variables_AR40} | tr ':' ' '`
1434        else
1435          vars=`echo ${sfc_variables_NPv31} | tr ':' ' '`
1436        fi
1437        ofile='sfc_variables'
1438      elif test ${vark} = 'diag'; then
1439        fileorig=${ifolddiag}/${exp}/${filendiag}
1440        vars=`echo ${diag_variables} | tr ':' ' '`
1441        if test ! -f ${fileorig}; then
1442          values='Time@XTIME,bottom_top@ZNU,south_north@XLAT,west_east@XLONG'
1443          ivar=0
1444          varcombos=''
1445          for var in ${vars}; do
1446            echo "    var:"${var}
1447            varn=`echo ${var} | tr '_' ' ' | awk '{print $2}'`
1448            combo=`python ${HOMEpy}/diagnostics.py -f ${combosfile} -d variable_combo -v ${varn}\
1449              | grep COMBO | awk '{print $2}'`
1450            if test ${combo} = 'ERROR'; then
1451              echo ${errormsg}
1452              echo "  No variable '"${varn}"' in '"${combosfile}"' !!"
1453              exit
1454            fi
1455            if test ${ivar} -eq 0; then
1456              varcombos=${varn}'|'${combo}
1457            else
1458              varcombos=${varcombos}','${varn}'|'${combo}
1459            fi
1460            ivar=`expr ${ivar} + 1`
1461          done
1462          python ${HOMEpy}/diagnostics.py -d ${values} -f ${ifoldsfc}/${exp}/${filensfc} -v \
1463            ${varcombos}
1464          if test $? -ne 0; then
1465            echo ${errormsg}
1466            echo "  python failed!!"
1467            echo python ${HOMEpy}/diagnostics.py -d ${values} -f ${ifoldsfc}/${exp}/${filensfc}\
1468              -v ${varcombos}
1469            exit
1470          fi
1471          python ${HOMEpy}/nc_var.py -o WRF_CFxtime_creation -f diagnostics.nc                 \
1472            -S 19491201000000,hours -v time
1473           python ${HOMEpy}/nc_var.py -o WRF_CFlonlat_creation -f diagnostics.nc               \
1474            -S longitude,latitude -v XLONG,XLAT
1475          mv diagnostics.nc ${ifolddiag}/${exp}
1476        fi
1477        ofile='diag_variables'
1478      elif test ${vark} = 'z'; then
1479        fileorig=${ifoldmean}/${exp}/${filenmean}
1480        vars=`echo ${z_variables} | tr ':' ' '`
1481        ofile='z_variables'
1482      fi
1483
1484# Averaging
1485##
1486      echo "  averaging..."
1487      ivar=0
1488      for varn in ${vars}; do
1489        file=${fileorig}
1490        echo "    var: "${varn}
1491        var=`echo ${varn} | tr '|' ' ' | awk '{print $2}'`
1492        varval=`python ${HOMEpy}/drawing.py -o variable_values -S ${var} | grep all_values | awk '{print $3}'`
1493        varname=`echo ${varval} | tr ',' ' ' | awk '{print $1}'`
1494
1495        echo "      std name: "${varname}
1496        varhead=`echo ${varname} | tr '_' ' ' | awk '{print $1}'`
1497        vartail=`echo ${varname} | tr '_' ' ' | awk '{print $2}'`
1498
1499        if test ${vark} = 'mean'; then
1500          vals='time:-1|z:-1|y:-1|x:-1,time:x,mean,pressure:lat'
1501          python ${HOMEpy}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${file} -v ${var}
1502          pyexec=$?
1503          if test ${pyexec} -ne 0; then
1504            echo ${errormsg}
1505            echo "  "${main}": python fai1ls!"
1506            echo "python ${HOMEpy}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${file} -v ${var}"
1507            exit
1508          else
1509            oper=`echo ${vals} | tr ',' ' ' | awk '{print $3}'`
1510            mv file_oper_alongdims_mean.nc ${ofold}/${exp}/${varname}${oper}.nc
1511          fi
1512        elif test ${vark} = 'z'; then
1513          vals='time:-1|z:-1|y:-1|x:-1,time:x,mean,pressure:lat'
1514          echo "NOT FINISHED!!!!"
1515          exit
1516        else
1517          vals='Time:-1|south_north:-1|west_east:-1,west_east,mean,time:XLAT'
1518          if test ${var} = 'ACRAINTOT'; then
1519            files='add|'${file}'|RAINC,add|'${file}'|RAINNC'
1520            python ${HOMEpy}/nc_var.py -S 'time|XLAT|XLONG@'${files} -o compute_opersvarsfiles \
1521              -v ${var}
1522            mv opersvarsfiles_${var}.nc ${ofold}/${exp}
1523            file=${ofold}/${exp}/opersvarsfiles_${var}.nc
1524          elif test ${var} = 'RAINTOT'; then
1525            dims='Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG'
1526            python ${HOMEpy}/diagnostics.py -d ${dims} -v 'RAINTOT|RAINC@RAINNC@time' -f ${file}
1527            mv diagnostics.nc ${ofold}/${exp}/diagnostics_${varname}.nc
1528            file=${ofold}/${exp}/diagnostics_${varname}.nc
1529            var='pr'
1530          elif test ${var} = 'cllmh'; then
1531            dims='Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG'
1532            clouds='cll:clm:clh'
1533            cls=`echo ${clouds} | tr ':' ' '`
1534            for cl in ${cls}; do
1535              file=${ofold}/${exp}/diagnostics.nc
1536              var=${cl}
1537              echo "    var: "${var}
1538              python ${HOMEpy}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${file} -v ${cl}
1539              pyexec=$?
1540              if test ${pyexec} -ne 0; then
1541                echo ${errormsg}
1542                echo "  "${main}": python fails!"
1543                echo python ${HOMEpy}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${file} -v ${cl}
1544                exit
1545              fi
1546              mv file_oper_alongdims_mean.nc ${ofold}/${exp}/${cl}${oper}.nc
1547              vals='Time:-1|south_north:-1|west_east:-1,Time,mean,XLONG:XLAT'
1548              dims='Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG'
1549              python ${HOMEpy}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${file} -v ${cl}
1550              pyexec=$?
1551              if test ${pyexec} -ne 0; then
1552                echo ${errormsg}
1553                echo "  "${main}": python fails!"
1554                exit
1555              else
1556                oper=`echo ${vals} | tr ',' ' ' | awk '{print $3}'`
1557                mv file_oper_alongdims_mean.nc ${ofold}/${exp}/${cl}t${oper}.nc
1558              fi
1559            done
1560#            break
1561          elif test ${var} = 'WRFbils' && test ! -f ${ofold}/${exp}/diagnostics_${varname}.nc; then
1562            dims='Time@time,south_north@XLAT,west_east@XLONG'
1563            python ${HOMEpy}/diagnostics.py -d ${dims} -v 'WRFbils|HFX@LH' -f ${file}
1564            mv diagnostics.nc ${ofold}/${exp}/diagnostics_${varname}.nc
1565            file=${ofold}/${exp}/diagnostics_${varname}.nc
1566            var='bils'
1567          elif test ${var} = 'WRFprw' && test ! -f ${ofold}/${exp}/diagnostics_${varname}.nc; then
1568            dims='Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG'
1569            python ${HOMEpy}/diagnostics.py -d ${dims} -v 'WRFprw|WRFdens@QVAPOR' -f ${file}
1570            mv diagnostics.nc ${ofold}/${exp}/diagnostics_${varname}.nc
1571            file=${ofold}/${exp}/diagnostics_${varname}.nc
1572            var='prw'
1573          elif test ${var} = 'WRFrhs' && test ! -f ${ofold}/${exp}/diagnostics_${varname}.nc; then
1574            dims='Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG'
1575            python ${HOMEpy}/diagnostics.py -d ${dims} -v 'WRFrhs|PSFC@T2@Q2' -f ${file}
1576            mv diagnostics.nc ${ofold}/${exp}/diagnostics_${varname}.nc
1577            file=${ofold}/${exp}/diagnostics_${varname}.nc
1578            var='huss'
1579          elif test ${var} = 'WRFprc'; then
1580            vals='Time:-1|south_north:-1|west_east:-1,west_east,mean,time:XLAT'
1581            dims='Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG'
1582            var='prc'
1583          elif test ${var} = 'WRFprls'; then
1584            vals='Time:-1|south_north:-1|west_east:-1,west_east,mean,time:XLAT'
1585            dims='Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG'
1586            var='prls'
1587          fi
1588          if test ! -f ${ofold}/${exp}/${varname}${oper}.nc; then
1589            python ${HOMEpy}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${file} -v ${var}
1590            pyexec=$?
1591            if test ${pyexec} -ne 0; then
1592              echo ${errormsg}
1593              echo "  "${main}": python fails!"
1594              exit
1595            else
1596              mv file_oper_alongdims_mean.nc ${ofold}/${exp}/${varname}${oper}.nc
1597            fi
1598          fi
1599        fi
1600
1601# Time means
1602        if $(isin_list ${var2Dtmeans} ${var}); then 
1603          echo "Computing time means!!"
1604          if test ${var} = 'cllmh'; then
1605            clouds='cll:clm:clh'
1606            clds=`echo ${clouds} | tr ':' ' '`
1607            for cld in ${clds}; do
1608              vals='Time:-1|south_north:-1|west_east:-1,Time,mean,XLONG:XLAT'
1609              dims='Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG'
1610              python ${HOMEpy}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${file} -v ${cld}
1611              pyexec=$?
1612              if test ${pyexec} -ne 0; then
1613                echo ${errormsg}
1614                echo "  "${main}": python fails!"
1615                exit
1616              else
1617                oper=`echo ${vals} | tr ',' ' ' | awk '{print $3}'`
1618                mv file_oper_alongdims_mean.nc ${ofold}/${exp}/${cld}t${oper}.nc
1619              fi
1620            done
1621          else
1622            vals='Time:-1|south_north:-1|west_east:-1,Time,mean,XLONG:XLAT'
1623            dims='Time@time,bottom_top@ZNU,south_north@XLAT,west_east@XLONG'
1624            python ${HOMEpy}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${file} -v ${var}
1625            pyexec=$?
1626            if test ${pyexec} -ne 0; then
1627              echo ${errormsg}
1628              echo "  "${main}": python fails!"
1629              exit
1630            else
1631              oper=`echo ${vals} | tr ',' ' ' | awk '{print $3}'`
1632              mv file_oper_alongdims_mean.nc ${ofold}/${exp}/${varname}t${oper}.nc
1633            fi
1634          fi
1635        fi
1636        if test ${var} = 'cllmh'; then exit; fi
1637        ivar=`expr ${ivar} + 1`
1638#        exit
1639# end of vars
1640      done
1641
1642# end of kind vars
1643    done
1644
1645  done
1646# end of compute
1647fi
1648# Longitudinal means of tmeans
1649##
1650if ${computetlon}; then
1651  echo "Computing Longitudinal means of temporal means..."
1652  for exp in ${exps}; do
1653    echo "  exp:"${exp}
1654    for tmean in ${ofold}/${exp}/*tmean.nc; do
1655      fname=`basename ${tmean}`
1656      var=`ncdump -h ${tmean} | grep float | grep mean | tr '(' ' ' | awk '{print $2}'`
1657      vals='south_north:-1|west_east:-1,west_east,mean,XLAT'
1658      dims='bottom_top@bottom_top,south_north@south_north,west_east@west_east'
1659      if test ! -f ${ofold}/${exp}/${var}t-lonmean.nc; then
1660        python ${HOMEpy}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${tmean} -v ${var}
1661        pyexec=$?
1662        if test ${pyexec} -ne 0; then
1663          echo ${errormsg}
1664          echo "  "${main}": python fails!"
1665          exit
1666        else
1667          mv file_oper_alongdims_mean.nc ${ofold}/${exp}/${var}t-lonmean.nc
1668        fi
1669#      exit
1670      fi
1671    done
1672
1673  done
1674fi
1675
1676if ${plot}; then
1677# Plots
1678##
1679
1680for exp in ${exps}; do
1681  echo "exp: "${exp}
1682  for vark in ${varks}; do
1683    echo "  vark: "${vark}
1684    if test ${vark} = 'mean'; then
1685      gcoups=`echo ${graph_couples_mean} | tr ':' ' '`
1686    elif test ${vark} = 'sfc'; then
1687      if test ${exp} = 'AR40'; then
1688        gcoups=`echo ${graph_couples_sfc_AR40} | tr ':' ' '`
1689      else
1690        gcoups=`echo ${graph_couples_sfc_NPv31} | tr ':' ' '`
1691      fi
1692    elif test ${vark} = 'diag'; then
1693      gcoups=`echo ${graph_couples_diag} | tr ':' ' '`
1694    fi
1695
1696    for gpair in ${gcoups}; do
1697      shdvar=`echo ${gpair} | tr '@' ' ' | awk '{print $1}'`
1698      cntvar=`echo ${gpair} | tr '@' ' ' | awk '{print $2}'`
1699      echo "  couple: "${shdvar}'-'${cntvar}
1700      shdvals=`python ${HOMEpy}/drawing.py -o variable_values -S ${shdvar} | grep all_values | awk '{print $3}'`
1701      cntvals=`python ${HOMEpy}/drawing.py -o variable_values -S ${cntvar} | grep all_values | awk '{print $3}'`
1702      files=${ofold}'/'${exp}'/'${shdvar}'mean.nc,'${ofold}'/'${exp}'/'${cntvar}'mean.nc'
1703
1704      Lshdvals=`expr length ${shdvals}0`
1705      if test ${Lshdvals} -lt 2; then
1706        echo ${errormsg}
1707        echo "  Error in drawing_tools.py 'variables_values'!!"
1708        echo "    variable '"${shdvar}"' NOT found!"
1709        exit
1710      fi
1711
1712      Lcntvals=`expr length ${cntvals}0`
1713      if test ${Lcntvals} -lt 2; then
1714        echo ${errormsg}
1715        echo "  Error in drawing_tools.py 'variables_values'!!"
1716        echo "    variable '"${cntvar}"' NOT found!"
1717        exit
1718      fi
1719
1720      shdstdn=`echo ${shdvals} | tr ',' ' ' | awk '{print $1}'`
1721      shdcbar=`echo ${shdvals} | tr ',' ' ' | awk '{print $7}'`
1722      shdmin=`echo ${shdvals} | tr ',' ' ' | awk '{print $3}'`
1723      shdmax=`echo ${shdvals} | tr ',' ' ' | awk '{print $4}'`
1724
1725      cntstdn=`echo ${cntvals} | tr ',' ' ' | awk '{print $1}'`
1726      cntmin=`echo ${cntvals} | tr ',' ' ' | awk '{print $3}'`
1727      cntmax=`echo ${cntvals} | tr ',' ' ' | awk '{print $4}'`
1728
1729      if test ${shdstdn} = 'ERROR' || test ${cntstdn} = 'ERROR'; then
1730        echo ${errmsg}
1731        echo "  "${main}": wrong variable names!!!"
1732        echo "    shdvals: "${shdvals}
1733        echo "    cntvals: "${cntvals}
1734        echo "    shdvar: "${shdvar}" cntvar: "${cntvar}
1735        exit
1736      fi
1737
1738      cntcolor='black'
1739      cntfmt='%g'
1740
1741      if test ${gpair} = 'va@ua'; then
1742        shdmin=`echo ${shdmin} | awk '{print $1/4.}'`
1743        shdmax=`echo ${shdmax} | awk '{print $1/4.}'`
1744        cntmin=`echo ${cntmin} | awk '{print $1/3.}'`
1745        cntmax=`echo ${cntmax} | awk '{print $1 + 20.}'`
1746      elif test ${gpair} = 'tas@ps'; then
1747        shdmin=`echo ${shdmin} | awk '{print $1 + 50.}'`
1748        shdmax=`echo ${shdmax} | awk '{print $1 - 15.}'`
1749        cntmin=`echo ${cntmin} | awk '{print $1 + 15000.}'`
1750        cntmax=`echo ${cntmax} | awk '{print $1 - 2000.}'`
1751      elif test ${gpair} = 'uas@vas'; then
1752        cntmin=`echo ${cntmin} | awk '{print $1 * 0.1}'`
1753        cntmax=`echo ${cntmax} | awk '{print $1 * 0.1}'`
1754      elif test ${gpair} = 'pr@rsds'; then
1755#        shdmax=`echo ${shdmax} | awk '{print $1 / 20.}'`
1756        cntmax=`echo ${cntmax} | awk '{print $1 / 3.}'`
1757      elif test ${gpair} = 'clt@cll' || test ${gpair} = 'clh@clm'; then
1758        cntcolor='red'
1759        cntfmt='%.1g'
1760      elif test ${gpair} = 'clh@clm'; then
1761        cntmax=`echo ${cntmax} | awk '{print $1}'`
1762      elif test ${gpair} = 'prw@huss'; then
1763        shdmax=`echo ${shdmax} | awk '{print $1*4}'`
1764      elif test ${gpair} = 'prls@prc'; then
1765        shdmax=`echo ${shdmax} | awk '{print $1/1.}'`
1766        cntmax=`echo ${cntmax} | awk '{print $1 * 0.5}'`
1767      elif test ${gpair} = 'hfls@hfss'; then
1768        cntmin='-50.'
1769        cntmax='50.'
1770      fi
1771 
1772      if test ${vark} = 'mean'; then
1773        graphvals=${shdstdn}','${cntstdn}':z|-1,y|-1:z|-1,y|-1:lat:pressure:'
1774        graphvals=${graphvals}${shdcbar}':fixsigc,'${cntcolor}':'${cntfmt}':'${shdmin}','
1775        graphvals=${graphvals}${shdmax}':'${cntmin}','${cntmax}',9:LMDZ+WRF|'${exp}
1776        graphvals=${graphvals}'|meridional|monthly|average|of|'${shdstdn}'|&|'
1777        graphvals=${graphvals}${cntstdn}':pdf:flip@y:None'
1778      else
1779        graphvals=${shdstdn}','${cntstdn}':y|-1,time|-1:y|-1,time|-1:time:XLAT:'
1780        graphvals=${graphvals}${shdcbar}':fixsigc,'${cntcolor}':'${cntfmt}':'${shdmin}','
1781        graphvals=${graphvals}${shdmax}':'${cntmin}','${cntmax}',9:WRF+LMDZ|'${exp}
1782        graphvals=${graphvals}'|mean|meridional|evolution|of|'${shdstdn}'|&|'
1783        graphvals=${graphvals}${cntstdn}':pdf:None:time|hours!since!1949-12-01|'
1784        graphvals=${graphvals}'exct,5,d|%d|date!([DD]):None'
1785      fi
1786
1787      echo ${files}
1788      echo ${graphvals}
1789      echo ${shdstdn}'mean,'${cntstdn}'mean'
1790
1791      if test ${vark} = 'sfc' || test ${vark} = 'diag' && 
1792       ! $(isin_list ${coup2D} ${gpair}); then
1793        python ${HOMEpy}/drawing.py -f ${files} -o draw_2D_shad_cont_time -S ${graphvals} -v   \
1794          ${shdstdn}'mean,'${cntstdn}'mean'
1795        pyexc=$?
1796      else
1797        python ${HOMEpy}/drawing.py -f ${files} -o draw_2D_shad_cont -S ${graphvals} -v        \
1798          ${shdstdn}'mean,'${cntstdn}'mean'
1799        pyexc=$?
1800      fi
1801      if test ${pyexc} -ne 0; then
1802        echo ${errormsg}
1803        echo "  "${main}": drawing.py fails!"
1804        exit
1805      else
1806        mv 2Dfields_shadow-contour.pdf ${ofold}/${exp}/${shdvar}mean_${cntvar}mean.pdf
1807        evince ${ofold}/${exp}/${shdvar}mean_${cntvar}mean.pdf &
1808      fi
1809
1810      if $(isin_list ${coup2D} ${gpair}); then
1811        graphvals=${shdstdn}','${cntstdn}':south_north|-1,west_east|-1:'
1812        graphvals=${graphvals}'south_north|-1,west_east|-1:longitude:latitude:'
1813        graphvals=${graphvals}${shdcbar}':fixsigc,'${cntcolor}':'${cntfmt}':'${shdmin}','
1814        graphvals=${graphvals}${shdmax}':'${cntmin}','${cntmax}',9:LMDZ+WRF|'${exp}
1815        graphvals=${graphvals}'|monthly|average|of|'${shdstdn}'|&|'
1816        graphvals=${graphvals}${cntstdn}':pdf:flip@y:None'
1817        echo '  '${shdstdn}'mean,'${cntstdn}'mean'
1818
1819        python ${HOMEpy}/drawing.py -f ${files} -o draw_2D_shad_cont -S ${graphvals} -v        \
1820          ${shdstdn}'mean,'${cntstdn}'mean'
1821        pyexc=$?
1822        if test ${pyexc} -ne 0; then
1823          echo ${errormsg}
1824          echo "  "${main}": drawing.py fails!"
1825          exit
1826        else
1827          mv 2Dfields_shadow-contour.pdf ${ofold}/${exp}/${shdvar}tmean_${cntvar}tmean.pdf
1828          evince ${ofold}/${exp}/${shdvar}tmean_${cntvar}tmean.pdf &
1829        fi
1830      fi
1831#      exit
1832
1833# end of couples
1834    done
1835# end of kinds
1836  done
1837
1838done
1839fi
1840
1841# 2D single plots
1842##
1843if ${single2Dplot}; then
1844  echo "2D single plots"
1845  for exp in ${exps}; do
1846    echo "  exp:"${exp}
1847    for tmean in ${ofold}/${exp}/*tmean.nc ;do
1848      fname=`basename ${tmean}`
1849      var=`ncdump -h ${tmean} | grep float | grep mean | tr '(' ' ' | awk '{print $2}'`
1850      if test ! ${var} = 'cltmean'; then
1851        shdvals=`python ${HOMEpy}/drawing.py -o variable_values -S ${var} |          \
1852          grep all_values | awk '{print $3}'`
1853        cbar=`echo ${shdvals} | tr ',' ' ' | awk '{print $7}'`
1854        min=`echo ${shdvals} | tr ',' ' ' | awk '{print $3}'`
1855        max=`echo ${shdvals} | tr ',' ' ' | awk '{print $4}'`
1856
1857        if test ${var} = 'prlsmean'; then xtrms='0,0.00015';
1858        elif test ${var} = 'prwmean'; then xtrms='0,40';
1859        elif test ${var} = 'psmean'; then xtrms='99000,102500';
1860        elif test ${var} = 'r2mean'; then xtrms='0,0.025';
1861        elif test ${var} = 'tasmean'; then xtrms='275,310';
1862        elif test ${var} = 'tmlamean'; then xtrms='260,310';
1863        elif test ${var} = 'uasmean'; then xtrms='-20,20';
1864        elif test ${var} = 'vasmean'; then xtrms='-20,20';
1865        else xtrms=${min}','${max}; fi
1866
1867#        vals=${var}':XLONG|-1,XLAT|-1:XLONG:XLAT:'${cbar}':'${xtrms}':monthly|mean:pdf:'
1868#        vals=${vals}'None:None:true'
1869#        dims='south_north@XLAT,west_east@XLONG'
1870        python ${HOMEpy}/nc_var.py -o WRF_CFlonlat_creation -S longitude,latitude    \
1871          -f ${tmean} -v XLONG,XLAT
1872        vals=${var}':longitude|-1,latitude|-1:longitude:latitude:'${cbar}':'${xtrms}':monthly|mean:pdf:'
1873        vals=${vals}'None:None:true'
1874        dims='south_north@latitude,west_east@longitude'
1875        python ${HOMEpy}/drawing.py -o draw_2D_shad -S ${vals} -f ${tmean} -v ${var}
1876        pyexec=$?
1877        if test ${pyexec} -ne 0; then
1878          echo ${errormsg}
1879          echo "  "${main}": python fails!"
1880          exit
1881        else
1882          mv 2Dfields_shadow.pdf ${ofold}/${exp}/${var}tmean.pdf
1883#          evince ${ofold}/${exp}/${var}tmean.pdf &
1884#          exit
1885        fi
1886      fi
1887    done
1888  done
1889fi
1890
1891# lon 2 vertical section
1892##
1893echo "lon 2 vertical section ..."
1894vars=`echo ${lon2DZsecvars} | tr ':' ' '`
1895pts=`echo ${lon2DZsecpts} | tr ':' ' '`
1896
1897if ${lonZsec}; then
1898  for exp in ${exps}; do
1899    file=${ofold}/${exp}/vertical_interpolation_WRFp.nc
1900#    python ${HOMEpy}/nc_var.py -o WRF_CFlonlat_creation -S longitude,latitude        \
1901#     -f ${file} -v lon,lat
1902## ALREADY done!
1903#    python ${HOMEpy}/nc_var.py -o valmod -S lowthres@oper,0.,sumc,360. -f ${file}    \
1904#       -v lon
1905    if test ${exp} = 'AR40'; then labexp='wlmdza'
1906    else labexp='wlmdzb'; fi
1907
1908    for pt in ${pts}; do
1909      echo "  pt: "${pt}
1910      vals='x:15|y:'${pt}'|time:23'
1911      lonval=`python ${HOMEpy}/nc_var.py -o varout -f ${file} -S ${vals} -v lon |    \
1912        awk '{print $2}'`
1913      latval=`python ${HOMEpy}/nc_var.py -o varout -f ${file} -S ${vals} -v lat |    \
1914        awk '{print $2}'`
1915      tval=`python ${HOMEpy}/nc_var.py -o varout -f ${file} -S ${vals} -v time |     \
1916        awk '{print $2}'`
1917      for var in ${vars}; do
1918        echo "    var: "${var}
1919
1920        shdvals=`python ${HOMEpy}/drawing.py -o variable_values -S ${var} |          \
1921          grep all_values | awk '{print $3}'`
1922        cbar=`echo ${shdvals} | tr ',' ' ' | awk '{print $7}'`
1923        min=`echo ${shdvals} | tr ',' ' ' | awk '{print $3}'`
1924        max=`echo ${shdvals} | tr ',' ' ' | awk '{print $4}'`
1925
1926# WRFt,U,V,WRFrh,WRFght
1927        if test ${var} = 'WRFght'; then xtrms='0,40000';
1928        elif test ${var} = 'WRFt'; then xtrms='200,300';
1929        elif test ${var} = 'U'; then xtrms='-40,40';
1930        elif test ${var} = 'V'; then xtrms='-20,20';
1931        else xtrms=${min}','${max}; fi
1932
1933# Plotting
1934        vals=${var}':x|-1,y|'${pt}',z|-1,time|24:lon:pressure:'${cbar}':'
1935        vals=${vals}${xtrms}':'${labexp}'|vertical|longitudinal|section|('${latval}
1936        vals=${vals}'$^{\circ}$):pdf:flip@y:None:true'
1937        python ${HOMEpy}/drawing.py -o draw_2D_shad -S ${vals} -f ${file} -v ${var}
1938        pyexec=$?
1939        if test ${pyexec} -ne 0; then
1940          echo ${errormsg}
1941          echo "  "${main}": python fails!"
1942          exit
1943        else
1944          mv 2Dfields_shadow.pdf ${ofold}/${exp}/${var}_lonZsec_${pt}pt.pdf
1945          evince ${ofold}/${exp}/${var}_lonZsec_${pt}pt.pdf &
1946        fi
1947
1948        cntcolor='black'
1949        cntfmt='%g'
1950
1951        if test ${var} = 'WRFght'; then cxtrms='0,40000';
1952        elif test ${var} = 'WRFt'; then cxtrms='200,300';
1953        elif test ${var} = 'U'; then cxtrms='-40,40';
1954        elif test ${var} = 'V'; then cxtrms='-20,20';
1955        else cxtrms=${min}','${max}; fi
1956
1957        graphvals=${var}','${var}':x|-1,y|'${pt}',z|-1,time|24:'
1958        graphvals=${graphvals}'x|-1,y|'${pt}',z|-1,time|24:lon:pressure:'
1959        graphvals=${graphvals}${cbar}':fixsigc,'${cntcolor}':'${cntfmt}':'${xtrms}':'
1960        graphvals=${graphvals}${cxtrms}',15:'${labexp}
1961        graphvals=${graphvals}'|vertical|zonal|section|('${latval}'$^{\circ}$):pdf:'
1962        graphvals=${graphvals}'flip@y:None'
1963
1964        python ${HOMEpy}/drawing.py -o draw_2D_shad_cont -S ${graphvals}             \
1965          -f ${file},${file} -v ${var},${var}
1966        pyexec=$?
1967        if test ${pyexec} -ne 0; then
1968          echo ${errormsg}
1969          echo "  "${main}": python fails!"
1970          exit
1971        else
1972          mv 2Dfields_shadow-contour.pdf ${ofold}/${exp}/${var}_lonZsec-cnt_${pt}pt.pdf
1973          evince ${ofold}/${exp}/${var}_lonZsec-cnt_${pt}pt.pdf &
1974        fi
1975
1976#        exit
1977      done
1978    done
1979  done
1980fi
1981
1982echo "Computing differences"
1983exp1=`echo ${experiments} | tr ':' ' ' | awk '{print $1}'`
1984exp2=`echo ${experiments} | tr ':' ' ' | awk '{print $2}'`
1985
1986diffks=`echo ${diffkinds} | tr ':' ' '`
1987
1988if test ${differences}; then
1989  for kind in ${diffks}; do
1990    echo ${kind}
1991    case ${kind} in
1992      'diffZ')
1993        vars=`echo ${diffZvars} | tr ':' ' '`
1994        fileorigd='mean.nc'
1995      ;;
1996      'diffH')
1997        vars=`echo ${diffHvars} | tr ':' ' '`
1998        fileorigd='tmean.nc'
1999      ;;
2000    esac
2001
2002    echo "  "${var}
2003    for var in ${vars}; do
2004      if test ! -f ${ofold}/${var}_diff.nc; then
2005#        cdo sub ${ofold}/${exp1}/${var}${fileorigd} ${ofold}/${exp2}/${var}${fileorigd} ${ofold}/${var}_diff.nc
2006        if test ${kind} = 'diffZ'; then
2007          values='pressure|lat@add|'${ofold}'/'${exp1}'/'${var}${fileorigd}'|'${var}'mean,'
2008          values=${values}'sub|'${ofold}'/'${exp2}'/'${var}${fileorigd}'|'${var}'mean'
2009          python ${HOMEpy}/nc_var.py -o compute_opersvarsfiles -S ${values} -v ${var}
2010          pyexec=$?
2011          if test ${pyexec} -ne 0; then
2012            echo ${errormsg}
2013            echo "  "${main}": python fails!"
2014            exit
2015          fi
2016          mv opersvarsfiles_${var}.nc ${ofold}/${var}_diff.nc
2017        else
2018          values='longitude|latitude@add|'${ofold}'/'${exp1}'/'${var}${fileorigd}'|'${var}'mean,'
2019          values=${values}'sub|'${ofold}'/'${exp2}'/'${var}${fileorigd}'|'${var}'mean'
2020          python ${HOMEpy}/nc_var.py -o compute_opersvarsfiles -S ${values} -v ${var}
2021          pyexec=$?
2022          if test ${pyexec} -ne 0; then
2023            echo ${errormsg}
2024            echo "  "${main}": python fails!"
2025            exit
2026          fi
2027          mv opersvarsfiles_${var}.nc ${ofold}/${var}_diff.nc
2028        fi
2029      fi
2030    done
2031
2032    if test ${kind} = 'diffZ'; then
2033      coups=`echo ${graph_couples_mean} | tr ':' ' '`
2034      for coup in ${coups}; do
2035        shdvar=`echo ${coup} | tr '@' ' ' | awk '{print $1}'`
2036        cntvar=`echo ${coup} | tr '@' ' ' | awk '{print $2}'`
2037
2038        echo "  couple: "${shdvar}'-'${cntvar}
2039
2040        shdvals=`python ${HOMEpy}/drawing.py -o variable_values -S ${shdvar} | grep all_values | awk '{print $3}'`
2041        cntvals=`python ${HOMEpy}/drawing.py -o variable_values -S ${cntvar} | grep all_values | awk '{print $3}'`
2042        files=${ofold}'/'${shdvar}'_diff.nc,'${ofold}'/'${cntvar}'_diff.nc'
2043
2044        shdstdn=`echo ${shdvals} | tr ',' ' ' | awk '{print $1}'`
2045        shdcbar=`echo ${shdvals} | tr ',' ' ' | awk '{print $7}'`
2046        shdmin=`echo ${shdvals} | tr ',' ' ' | awk '{print $3}'`
2047        shdmax=`echo ${shdvals} | tr ',' ' ' | awk '{print $4}'`
2048
2049        cntstdn=`echo ${cntvals} | tr ',' ' ' | awk '{print $1}'`
2050        cntmin=`echo ${cntvals} | tr ',' ' ' | awk '{print $3}'`
2051        cntmax=`echo ${cntvals} | tr ',' ' ' | awk '{print $4}'`
2052
2053        shdcbar='seismic'
2054        if test ${coup} = 'va@ua'; then
2055          shdmin=-4.
2056          shdmax=4.
2057          cntmin=-10.
2058          cntmax=10.
2059        elif test ${coup} = 'hus@ta'; then 
2060          shdmin=-0.2
2061          shdmax=0.2
2062          cntmin=-2.
2063          cntmax=2.
2064        fi
2065
2066        cntcolor='black'
2067        cntfmt='%g'
2068
2069        graphvals=${shdstdn}','${cntstdn}':x|-1,y|-1,x_2|-1:x|-1,y|-1,x_2|-1:lat:'
2070        graphvals=${graphvals}'pressure:'${shdcbar}':fixsigc,'${cntcolor}':'${cntfmt}':'
2071        graphvals=${graphvals}${shdmin}','${shdmax}':'${cntmin}','${cntmax}',9:'
2072        graphvals=${graphvals}${exp1}'-'${exp2}'|meridional|monthly|average|differences|of|'
2073        graphvals=${graphvals}${shdstdn}'|&|'${cntstdn}':pdf:flip@y:None'
2074
2075        python ${HOMEpy}/drawing.py -o draw_2D_shad_cont -S ${graphvals}             \
2076          -f ${files} -v ${shdvar},${cntvar}
2077        pyexec=$?
2078        if test ${pyexec} -ne 0; then
2079          echo ${errormsg}
2080          echo "  "${main}": python fails!"
2081          exit
2082        else
2083          mv 2Dfields_shadow-contour.pdf ${ofold}/${shdvar}-${cntvar}_diff.pdf
2084          evince ${ofold}/${shdvar}-${cntvar}_diff.pdf &
2085        fi
2086      done
2087#      exit
2088    else
2089      for var in ${vars}; do
2090        echo "  "${var}
2091        vals=`python ${HOMEpy}/drawing.py -o variable_values -S ${var} | grep all_values | awk '{print $3}'`
2092        file=${ofold}/${var}_diff.nc
2093
2094        stdn=`echo ${vals} | tr ',' ' ' | awk '{print $1}'`
2095        cbar='seismic'
2096
2097        if test ${var} = 'uas'; then xtrms='-10.,10.';
2098        elif test ${var} = 'vas'; then xtrms='-5,5';
2099        elif test ${var} = 'ps'; then xtrms='-500,500';
2100        elif test ${var} = 'pr'; then xtrms='-0.001,0.001';
2101        else xtrms=${min}','${max}; fi
2102
2103        vals=${var}':x|-1,y|-1:longitude:latitude:'${cbar}':'
2104        vals=${vals}${xtrms}':'${exp1}'-'${exp2}'|meridional|monthly|average|differences:'
2105        vals=${vals}'pdf:None:None:true'
2106        python ${HOMEpy}/drawing.py -o draw_2D_shad -S ${vals} -f ${file} -v ${var}
2107        pyexec=$?
2108        if test ${pyexec} -ne 0; then
2109          echo ${errormsg}
2110          echo "  "${main}": python fails!"
2111          exit
2112        else
2113          mv 2Dfields_shadow.pdf ${ofold}/${var}_diff.pdf
2114          evince ${ofold}/${var}_diff.pdf &
2115        fi
2116#        exit
2117      done
2118    fi
2119  done
2120fi
Note: See TracBrowser for help on using the repository browser.