Index: trunk/tools/drawing.py
===================================================================
--- trunk/tools/drawing.py	(revision 1553)
+++ trunk/tools/drawing.py	(revision 1559)
@@ -50,4 +50,6 @@
 ## e.g. # drawing.py -o draw_bar_line_time -f 'Forcing_rainstats_OKstomate_SESA_DJFmean_norm_meanstd_stat2.nc;time_centered,-1;time_centered;rainnorm;bar:Forcing_rainstats_OKstomate_SACZ_DJFmean_norm_meanstd_stat2.nc;time_centered,-1;time_centered;rainnorm;bar:DiPolo_evapstats_OKstomate_SESA_DJFmean_stat2.nc;time_centered,-1;time_centered;evap_stats;line:DiPolo_evapnustats_OKstomate_SESA_DJFmean_stat2.nc;time_centered,-1;time_centered;evapnu_stats;line:DiPolo_evspsblvegstats_OKstomate_SESA_DJFmean_stat2.nc;time_centered,-1;time_centered;evspsblveg_stats;line:DiPolo_transpirstats_OKstomate_SESA_DJFmean_stat2.nc;time_centered,-1;time_centered;transpir_stats;line:DiPolo_evapstats_OKstomate_SACZ_DJFmean_stat2.nc;time_centered,-1;time_centered;evap_stats;line:DiPolo_evapnustats_OKstomate_SACZ_DJFmean_stat2.nc;time_centered,-1;time_centered;evapnu_stats;line:DiPolo_evspsblvegstats_OKstomate_SACZ_DJFmean_stat2.nc;time_centered,-1;time_centered;evspsblveg_stats;line:DiPolo_transpirstats_OKstomate_SACZ_DJFmean_stat2.nc;time_centered,-1;time_centered;transpir_stats;line' -S 'auto;pr;y;None;1;year|seconds!since!1958-01-01!00:00:00|exct,5,y|%Y;SESA,SACZ,SESA!evap,evapnu,evspblveg,transpir,SACZ!evap,None,None,None;#AA0000,#0000AA;10000000.;#AAAA00,#00AAAA,#AA00AA,#AAAAAA,#AAAA00,#00AAAA,#AA00AA,#AAAAAA;-,-,-,-,--,--,--,--;,;2.;2.;all;normalized!evolution!of!DJFmean!with!WFD3!forcing;0|10;png;bar_time_rain_CRU-NCEP_DJFmean_norm;yes'
 ## e.g. # drawing.py -o draw_time_lag -f 'Forcing_rainstats_OKstomate_SESA_norm_meanstd_stat2.nc;time_centered,-1;time_counter;rainnorm%DiPolo_evapstats_SESA_norm_meanstd_OKstomate.nc;time_counter,-1|stats,2;time_counter;evapnorm:Forcing_rainstats_OKstomate_SACZ_norm_meanstd_stat2.nc;time_centered,-1;time_counter;rainnorm%DiPolo_evapstats_SACZ_norm_meanstd_OKstomate.nc;time_counter,-1|stats,2;time_counter;evapnorm' -S 'auto;centered;pr;1;evap;1;-5,5;-2,2;year|seconds!since!1958-01-01!00:00:00|exct,5,y|%Y;SESA,SACZ;simplepts,1,#AA0000@#0000AA,x,2.;1-month!time-lag!at!SACZ!between!pr!&!evap!normalized!by!mean/stddev;0|12;png;mon1_timelag_pr-evap_SACZ;yes'
+## e.g. # drawing.py -o draw_ensembles_time -f 'Forcing_rain_SESA.nc;time_counter,-1|stats,0;time_centered;rain_stats;ensemble#Forcing_rain_SESA.nc;time_counter,-1|stats,1;time_centered;rain_stats;ensemble#Forcing_rain_SESA.nc;time_counter,-1|stats,2;time_centered;rain_stats;data' -S 'auto;y;tas;mm/d;auto,auto;#0000FF:-:2.:,:2.;year|seconds!since!1958-01-01!00:00:00|exct,5,y|%Y;FromFile;$pr_{space-mean}^{SESA}$;cloud,#AAAADD,auto;$pr_{space-mean}$!evolution!at!SESA;0|10;png;Forcing_pr_spacemean;no;yes'
+
 
 #######
@@ -69,4 +71,5 @@
 # draw_barbs: Function to plot wind barbs
 # draw_basins: Function to plot river basins with their discharge vector and basins id (from 'routing.nc')
+# draw_ensembles_time: Function to plot an ensembles of data following an axis-time
 # draw_lines: Function to draw different lines at the same time from different files
 # draw_lines_time: Function to draw different lines at the same time from different files with times
@@ -100,5 +103,5 @@
   'draw_2D_shad_line',                                                               \
   'draw_2D_shad_line_time', 'draw_bar', 'draw_bar_line', 'draw_bar_line_time',       \
-  'draw_bar_time', 'draw_barbs', 'draw_basins',                                      \
+  'draw_bar_time', 'draw_barbs', 'draw_basins', 'draw_ensembles_time',               \
   'draw_2lines', 'draw_2lines_time', 'draw_lines', 'draw_lines_time',                \
   'draw_Neighbourghood_evol',                                                        \
@@ -6421,5 +6424,5 @@
 def draw_bar(ncfile, values):
     """ Function to plot a bar char figure with multiple sources
-      ncfile = ':' list of [filen];[dimslice];[xvarn];[yvarn]
+      ncfile = '#' list of [filen];[dimslice];[xvarn];[yvarn]
         [filen]: name of the file
         [dimslice]: '|' list of [dimn],[dimrange]
@@ -6495,5 +6498,5 @@
 
     # files and values
-    filedvs = ncfile.split(':')
+    filedvs = ncfile.split('#')
     xvals = []
     yvals = []
@@ -6566,5 +6569,5 @@
 def draw_bar_time(ncfile, values):
     """ Function to plot a bar char figure with multiple sources and time-axis
-      ncfile = ':' list of [filen];[dimslice];[tvarn];[yvarn]
+      ncfile = '#' list of [filen];[dimslice];[tvarn];[yvarn]
         [filen]: name of the file
         [dimslice]: '|' list of [dimn],[dimrange]
@@ -6652,5 +6655,5 @@
 
     # files and values
-    filedvs = ncfile.split(':')
+    filedvs = ncfile.split('#')
     tvals = []
     yvals = []
@@ -6776,5 +6779,5 @@
 def draw_bar_line(ncfile, values):
     """ Function to plot a bar char and lines plot figure with multiple sources
-      ncfile = ':' list of [filen];[dimslice];[xvarn];[yvarn];[kind]
+      ncfile = '#' list of [filen];[dimslice];[xvarn];[yvarn];[kind]
         [filen]: name of the file
         [dimslice]: '|' list of [dimn],[dimrange]
@@ -6872,5 +6875,5 @@
 
     # files and values
-    filedvs = ncfile.split(':')
+    filedvs = ncfile.split('#')
     barxvals = []
     baryvals = []
@@ -6991,5 +6994,5 @@
 def draw_bar_line_time(ncfile, values):
     """ Function to plot a bar char and lines plot figure with multiple sources and time-axis
-      ncfile = ':' list of [filen];[dimslice];[xvarn];[yvarn];[kind]
+      ncfile = '#' list of [filen];[dimslice];[xvarn];[yvarn];[kind]
         [filen]: name of the file
         [dimslice]: '|' list of [dimn],[dimrange]
@@ -7095,5 +7098,5 @@
 
     # files and values
-    filedvs = ncfile.split(':')
+    filedvs = ncfile.split('#')
     bartvals = []
     baryvals = []
@@ -7254,5 +7257,5 @@
 def draw_time_lag(ncfile, values):
     """ Function to plot a time-lag figure with multiple sources (x, previous values; y, future values)
-      ncfile = ':' list of couples ('%' formed) [prefilen];[predslice];[pretvarn];[prevarn]%[futfilen];
+      ncfile = '#' list of couples ('%' formed) [prefilen];[predslice];[pretvarn];[prevarn]%[futfilen];
           [futdslice];[futtvarn];[futvarn]
         'pre': values to act as previous values
@@ -7380,5 +7383,5 @@
 
     # files and values
-    filedvs = ncfile.split(':')
+    filedvs = ncfile.split('#')
     timevalues = []
     prevalues = []
@@ -7547,4 +7550,568 @@
     return
 
+def draw_ensembles_time(ncfile, values):
+    """ Function to plot an ensembles of data following an axis-time
+      ncfile = '#' list of files [filen];[slice];[tvarn];[varn];[kinddata]
+        [filen]: name of the file
+        [dimslice]: '|' list of [dimn],[dimrange]
+          [dimn]: name of the dimension
+          [dimrange]: range for the slice
+            * [integer]: which value of the dimension
+            * -1: all along the dimension
+            * -9: last value of the dimension
+            * [beg]@[end]@[inc] slice from [beg] to [end] every [inc]
+            * NOTE, no dim name all the dimension size
+        [tvarn]: name of the variable time ('WRFtime', for times from WRF)
+        [varn]: name of the variable to use for the values
+        [kinddata]: which kind of use should be done:
+          'data': to be plot as data
+          'ensemble': to be used as a member of the ensemble
+      values = [dimxyfmt];[valaxis];[axislab];[axisunits];[rangevals];[datavals];[timevals];[valskind];
+          [labels];[figenskind];[graphtit];[legvalues];[kindfig];[fign];[outfile];[close]
+        [dimxyfmt]=[dxs],[dxf],[Ndx],[ordx],[dys],[dyf],[Ndy],[ordy]: format of the values at each 
+            axis (or single 'auto' for 'auto','auto')
+          [dxs]: style of x-axis ('auto' for 'pretty')
+            'Nfix', values computed at even 'Ndx'
+            'Stringlist', a given list of strings
+            'Vfix', values computed at even 'Ndx' increments
+            'pretty', values computed following aprox. 'Ndx' at 'pretty' intervals (2.,2.5,4,5,10)
+          [dxf]: format of the labels at the x-axis
+          [Ndx]: Number of ticks at the x-axis (for 'Stringlist', '@' list of labels)
+          [ordx]: angle of orientation of ticks at the x-axis ('auto' for horizontal)
+          [dys]: style of y-axis ('auto' for 'pretty')
+          [dyf]: format of the labels at the y-axis
+          [Ndy]: Number of ticks at the y-axis
+          [ordy]: angle of orientation of ticks at the y-axis ('auto' for horizontal)
+        [valaxis]: which axes should be used for the data
+        [axislab]: Label for the values-axis ('!' for spaces)
+        [axisunits]: units of the values-label
+        [rangevals]: Range of the axis with the values (single 'None' for 'auto','auto')
+         [vmin],[vmax]: minimum and maximum values
+           'auto': the computed minimumm or maximum of the values
+        [datavals]: [color]:[linetype]:[linewidht]:[markertype]:[markersize] values for the data-lines
+          [color]: '@' list of colors of the lines (single value for the same, or 'auto')
+          [linetype]: '@' list of type of the lines (single value for the same, or 'auto')
+          [linewidht]: '@' list of width of the lines (single value for the same, or 'auto')
+          [markertype]: '@' list of type of the markers (single value for the same, or 'auto')
+          [markersize]: '@' list of size of the markers (single value for the same, or 'auto')
+        [timevals]: [timen]|[units]|[kind]|[tfmt] time labels characteristics
+          [timen]; name of the time variable
+          [units]; units string according to CF conventions ([tunits] since 
+            [YYYY]-[MM]-[DD] [[HH]:[MI]:[SS]], '!' for spaces)
+          [kind]; kind of output
+            'Nval': according to a given number of values as 'Nval',[Nval]
+            'exct': according to an exact time unit as 'exct',[tunit]; 
+              tunit= [Nunits],[tu]; [tu]= 'c': centuries, 'y': year, 'm': month,
+               'w': week, 'd': day, 'h': hour, 'i': minute, 's': second, 
+               'l': milisecond
+          [tfmt]; desired format
+        [valskind]: way to compute the values to be plotted
+          'FromFile': from the files with the 'data' flag
+          'FromEns',[usedims],[stats]: from all the files with the 'ensemble' flag with statistics [stats], 
+              throughout dimensions [usedims]
+            [usedims]= '@' list of dimensions to take to compute the statistics
+            [stats]= statistics to use
+              'direct': without statistics
+              'mean': mean value of all files with ensemble flag
+              'median': median value of all files with ensemble flag
+              'weightedmean,[stats],[weights]': weighted mean value all files with ensemble flag
+                [stats] = statistics to use from each member: 'direct', 'mean', 'median'
+                [weights] = '@' list of weights [0,1]
+          'FromIndivEns',[usedims],[stats]: from each files with the 'ensemble' flag with statistics [stats], 
+              throughout dimensions [usedims]
+            [usedims]= '@' list of dimensions to take to compute the statistics
+            [stats]= statistics to use: 
+              'mean': mean value of each file with ensemble flag
+              'median': median value of each file with ensemble flag
+        [labels]: ',' list of labels for the legend ('None' for no-labels, '!' for spaces)
+        [figenskind]=[kindname],[valueskind] kind of ensemble plot
+          'cloud',[color],[limits]: ensembles values as a cloud 
+            [color]: name of the color to use ('auto' for '#DDDDDD')
+            [limits]: which stadistics to use for the plot ('auto' for extremes)
+              '5quantile': 5quantile,95quantile of the values 
+              'direct': below and up values are tacking directly from the provided data
+              'extremes': min,max of the values
+              'stddev': standard deviation fo the values (only available if there is one line)
+          'spaghetti',[color],[width],[type]: a line for each member
+            [color]: name of the color to use for all lines ('auto' for '#DDDDDD')
+            [width]: width of the line to use for all lines ('auto' for 0.5)
+            [type]: type of the line to use for all lines ('auto' for '-')
+        [graphtit]: title of the figure '!' for spaces
+        [legvalues]=[locleg]|[fontsize]: 
+          [locleg]: location of the legend (0, autmoatic)
+            1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right',
+            5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center',
+            9: 'upper center', 10: 'center'
+          [fontsize]: font size for the legend (auto for 12)
+        [kindfig]: kind of output figure (png, pdf, ps)
+        [fign]: name of the figure (without extension)
+        [outfile]: wehter or not a file with the result of the computation of the ensemble should be 
+          written or not (labelled 'ensemble_ofile.nc')
+        [close]: whether figure should be close or not
+    """
+    fname = 'draw_ensembles_time'
+
+    if values == 'h':
+        print fname + '____________________________________________________________'
+        print draw_ensembles_time.__doc__
+        quit()
+
+    expectargs = '[dimxyfmt];[axeskind];[axislab];[axisunits];[rangevals];' +        \
+      '[datavals];[timevals];[valskind];[labels];[figenskind];[graphtit];' +         \
+      '[legvalues];[kindfig];[fign];[outfile];[close]'
+    drw.check_arguments(fname,values,expectargs,';')
+
+    dimxyfmt = values.split(';')[0]
+    valaxis = values.split(';')[1]
+    axislab = values.split(';')[2].replace('!',' ')
+    axisunits = values.split(';')[3]
+    rangevals = values.split(';')[4]
+    datavals = values.split(';')[5]
+    timevals = values.split(';')[6]
+    valskind = values.split(';')[7]
+    labels = gen.str_list(values.split(';')[8].replace('!', ' '), ',')
+    figenskind = values.split(';')[9]
+    graphtit = values.split(';')[10].replace('!',' ')
+    legvalues = values.split(';')[11]
+    kindfig = values.split(';')[12]
+    fign = values.split(';')[13]
+    outfile = gen.Str_Bool(values.split(';')[14])
+    close = gen.Str_Bool(values.split(';')[15])
+
+    ofilen = 'ensemble_ofile.nc'
+
+    # files
+    valfiles = gen.str_list(ncfile, '#')
+
+    # time-values
+    expectargs = '[timen]|[units]|[kind]|[tfmt]'
+    drw.check_arguments('timevals: '+fname,timevals,expectargs,'|')
+
+    timen = timevals.split('|')[0].replace('!',' ')
+    timeunit = timevals.split('|')[1].replace('!',' ')
+    timekind = timevals.split('|')[2]
+    timefmt = timevals.split('|')[3]
+
+    linevals = []
+    ltimevals = []
+    ensvals = []
+    etimevals = []
+
+    # List of files used for the ensemble
+    ensfilens = []
+    # List of files used for the ensemble with its dimension and variable name
+    edv_filens = []
+    # Dictionary with the dimensions of the values from a given file
+    dimsfile = {}
+
+    mintval = gen.fillValueF
+    maxtval = -gen.fillValueF
+    minvval = gen.fillValueF
+    maxvval = -gen.fillValueF
+
+    ensfilens = []
+    ifile = 0
+    dimt = -gen.fillValueF
+    for fdv in valfiles:
+        fvals = fdv.split(';')
+        expectedargs = '[filen];[slice];[tvarn];[varn];[kinddata]'
+        gen.check_arguments('filevals: '+fname, fdv, expectedargs, ';')
+        filen = fvals[0]
+        dimrange = fvals[1]
+        tvarn = fvals[2]
+        yvarn = fvals[3]
+        datatype = fvals[4]
+
+        edv = filen + '#' + dimrange + '#' + yvarn
+
+        if not os.path.isfile(filen):
+            print errormsg
+            print '  ' + fname + ': file "' + filen + '" does not exist !!'
+            quit(-1)
+
+        onc = NetCDFFile(filen, 'r')
+        if tvarn == 'WRFtime':
+            datesv = []
+            if tvarn == 'WRFtime':
+                fdates = onc.variables['Times'][:]
+                for it in range(fdates.shape[0]):
+                    datesv.append(gen.datetimeStr_conversion(fdates[it,:],           \
+                      'WRFdatetime','cfTime,' + timeunit))
+            tvs = np.array(datesv, dtype=np.float)
+            tunitsvar = timeunit
+        else:
+            if not onc.variables.has_key(tvarn):
+                print errormsg
+                print '  ' +fname+ ': file "' +filen+ "' does not have variable '" + \
+                  tvarn + "' for the time-values !!"
+                print '    available ones:', onc.variables.keys()
+                onc.close()
+                quit(-1)
+            otvar = onc.variables[tvarn]
+            tvs, tdims = ncvar.slice_variable(otvar, dimrange.replace(',',':'))
+            tunitsvar = otvar.units
+
+        if not onc.variables.has_key(yvarn):
+            print errormsg
+            print '  ' +fname+ ': file "' +filen+ "' does not have variable '" +     \
+              yvarn + "' for the y-values !!"
+            print '    available ones:', onc.variables.keys()
+            onc.close()
+            quit(-1)
+
+        oyvar = onc.variables[yvarn]
+        yvs, ydims = ncvar.slice_variable(oyvar, dimrange.replace(',',':'))
+        dimsfile[edv] = ydims
+
+        # Referring all times to the same reference time!
+        reftvals = gen.coincident_CFtimes(tvs, timeunit, tunitsvar)
+
+        onc.close()
+
+        if len(reftvals) > dimt: dimt = len(reftvals)
+
+        reftn = reftvals.min()
+        reftx = reftvals.max()
+        if reftn < mintval: mintval = reftn
+        if reftx > maxtval: maxtval = reftx
+        refvn = yvs.min()
+        refvx = yvs.max()
+        if refvn < minvval: minvval = refvn
+        if refvx > maxvval: maxvval = refvx
+
+        if datatype == 'data':
+            if len(tvs.shape) != 1:
+                print errormsg
+                print '  ' +fname+ ': provided slice:', dimrange, "for variable '" +     \
+                 tvarn + "' and '" + datatype + "' does not provide a rank-1 value !!"
+                print '    resultant shape:', tvs.shape, 'dimensions:',tdims,'fix it !!'
+                quit(-1)
+            if len(yvs.shape) != 1:
+                print errormsg
+                print '  ' +fname+ ': provided slice:', dimrange, "for variable '" +     \
+                  yvarn + "' and '" + datatype + "'does not provide a rank-1 value !!"
+                print '    resultant shape:',yvs.shape, 'dimensions:',ydims,'fix it !!'
+                quit(-1)
+            linevals.append(yvs)
+            ltimevals.append(reftvals)
+        else:
+            ensfilens.append(filen)
+            edv_filens.append(edv)
+            ensvals.append(yvs)
+            etimevals.append(reftvals)
+
+        ifile = ifile + 1
+
+    Nens = len(ensvals)
+
+    valskindn = gen.str_list(valskind, ',')[0]
+
+    ENSvals = []
+    TENSvals = []
+
+    # Getting values to draw as lines
+    if valskindn == 'FromFile':
+        if len(linevals) == 0:
+            print errormsg
+            print '  ' + fname + ": you asked to get data '" + valskind +            \
+              "' but no file with flag 'data' was provided !!"
+            print '    provided file characteristics _______'
+            for fdv in prefutdv:
+                print fdv.split(';')
+            quit(-1)
+        ENSvals = list(ensvals)
+        TENSvals = list(etimevals)
+    elif valskindn == 'FromEns':
+        eSvals = valskind.split(',')
+
+        expectedargs='FromEns,[usedims],[stats]'
+        gen.check_arguments('FromEns: '+fname, valskind, expectedargs, ',')
+
+        ensdims = eSvals[1].split('@')
+        ensn = eSvals[2]
+        iedv = 0
+        # Temporal slices for each file/variable
+        Tslices = {}
+        for edv in edv_filens:
+            vvalues = ensvals[iedv]
+            edv_dims = dimsfile[edv]
+            idim = []
+            for dsts in ensdims:
+                idim.append(edv_dims.index(dsts))
+            # Adding each running value as a new member of the ensemble
+            NOTusedim = []
+            for dn in dimsfile[edv]: 
+                if not gen.searchInlist(ensdims, dn): NOTusedim.append(dn)
+            varslices = gen.provide_slices(dimsfile[edv], vvalues.shape, NOTusedim)
+            Tslices[edv] = varslices
+
+            if iedv == 0: dimt = len(varslices)
+            else: 
+                if dimt != len(varslices):
+                    print errormsg
+                    print '  ' + fname + ': this is too complicated !!'
+                    print '    for file:', iedv, ' with ', len(varslices),           \
+                      'and initial dimt:', dimt 
+                    print '    not ready to deal with files with different number '+ \
+                      'of time-steps!!'
+                    quit(-1)
+            # Adding each not runnuning grid-point as a new ensemble member
+            varslices = gen.provide_slices(dimsfile[edv], vvalues.shape, ensdims)
+            for slc in varslices:
+                ENSvals.append(np.squeeze(vvalues[tuple(slc)]))
+                TENSvals.append(np.squeeze(np.array(etimevals[iedv])))
+
+            iedv = iedv + 1
+
+        lvals = np.zeros((dimt), dtype=np.float)
+        for it in range(dimt):
+            iedv = 0     
+            Tvals = []
+            Tvalse = {}
+            for edv in edv_filens:
+                vvalues = ensvals[iedv]
+                varslics = Tslices[edv]
+                Tvals = Tvals + list(np.squeeze(vvalues[tuple(varslics[dimt-it-1])]).flatten())
+                Tvalse[edv] = np.squeeze(vvalues[tuple(varslics[dimt-it-1])])
+
+            aTvals = np.array(Tvals)
+            if ensn == 'mean':
+                lvals[it] = np.mean(aTvals)
+            elif ensn == 'median':
+                lvals[it] = np.median(aTvals)
+            elif ensn[0:12] == 'weightedmean':
+                expectedargs='FromEns,[usedims],weightedmean,[stats],[weights]'
+                gen.check_arguments('FromEns: '+fname, valskind, expectedargs, ',')
+
+                stats = eSvals[3]
+                wgths = eSvals[4].split('@')
+                wghtvs = np.array(wgths, dtype=np.float)
+                if len(wgths) != Nens:
+                    print errormsg
+                    print '  ' + fname + ': the number of members of the ensemble:', \
+                      Nens, 'and the number of provided weights:', len(wghts),       \
+                      'do not coincide !!'
+                    print '    provided weights:', wgths
+                    print '    provided file characteristics _______'
+                    for fdv in prefutdv:
+                        print fdv.split(';')
+                    quit(-1)
+                wgthvals = np.zeros((Nens), dtype=np.float)
+                iedv = 0
+                for edv in edv_filens:
+                    if stats == 'mean':
+                        wgthvals[iedv] = np.mean(Tvalse[edv])
+                    elif stats == 'median':
+                        wgthvals[iedv] = np.median(Tvalse[edv])
+                    else:
+                        print errormsg
+                        print '  ' +fname+ ": statistics '" + eSvals[2] + "' not ready !!"
+                        print '    available ones:', ['mean', 'median']
+                        quit(-1)
+                lvals[it] = np.sum(wghtvals*wghtvs)/np.sum(wghtvs)
+            else:
+                print errormsg
+                print '  ' + fname + ": for '" + valskindn + "'statistics '" +       \
+                  eSvals[2] + "' not ready !!"
+                print '    available ones:', ['mean', 'median', 'weightedmean']
+                quit(-1)
+            iedv = iedv + 1
+        linevals.append(lvals)
+        ltimevals.append(np.squeeze(np.array(etimevals[0], dtype=np.float)))
+    elif valskindn == 'FromIndivEns':
+        indenfns = []
+        eSvals = valskind.split(',')
+
+        expectedargs='FromIndivEns,[usedims],[stats]'
+        gen.check_arguments('FromEns: '+fname, valskind, expectedargs, ',')
+
+        ensdims = eSvals[1].split('@')
+        ensn = eSvals[2]
+        iedv = 0
+        evals = np.zeros((dimt, Nens), dtype=np.float)
+        for edv in edv_filens:
+            vvalues = ensvals[iedv]
+            edv_dims = dimsfile[edv]
+            filen = edv.split('#')[0]
+            indenfns.append(filen)
+
+            idim = []
+            for dsts in ensdims:
+                idim.append(edv_dims.index(dsts))
+            # Adding each running value as a new member of the ensemble
+            NOTusedim = []
+            for dn in dimsfile[edv]: 
+                if not gen.searchInlist(ensdims, dn): NOTusedim.append(dn)
+            varslices = gen.provide_slices(dimsfile[edv], vvalues.shape, ensdims)
+            for varslc in varslices:
+                ENSvals.append(np.squeeze(vvalues[tuple(varslc)]))
+                TENSvals.append(np.squeeze(np.array(etimevals[iedv], dtype=np.float)))
+                # No need to repeat File name len(varslices)0 times
+                # ensfilens.append(filen)
+            if ensn == 'mean':
+                linevals.append(np.mean(vvalues, axis=tuple(idim)))
+            elif ensn == 'median':
+                linevals.append(np.median(vvalues, axis=tuple(idim)))
+            elif ensn == 'direct':
+                if len(values.shape) != 1:
+                    print errormsg
+                    print '  ' +fname+ ': provided slice:', dimrange,                \
+                      "for variable '" + yvarn + "' and '" + datatype +              \
+                      "'does not provide a rank-1 value !!"
+                    print '    resultant shape:', yvs.shape, 'dimensions:', ydims,   \
+                      'fix it !!'
+                    quit(-1)
+                linevals.append(vvalues)
+            else:
+                print errormsg
+                print '  ' + fname + ": for '" + valskindn + "' statistics '" +      \
+                  eSvals[2] + "' not ready !!"
+                print '    available ones:', ['mean', 'median']
+                quit(-1)
+            ltimevals.append(np.squeeze(np.array(etimevals[iedv], dtype=np.float)))
+            iedv = iedv + 1
+    else:
+        print errormsg
+        print '  ' + fname + ": kind of values '" + valskindn + "' not ready !!"
+        print '    available ones:', ['FromFile', 'FromEns', 'FromIndivEns']
+        quit(-1)
+
+    Ndata = len(linevals)
+    Nens = len(ENSvals)
+
+    # Times
+    dtvals = (maxtval - mintval)/10
+    dti = mintval
+    dte = maxtval
+    tvalsticks = np.arange(dti, dte+dtvals, dtvals)
+
+    dtiS = drw.datetimeStr_conversion(str(dti) + ',' + timeunit, 'cfTime',           \
+     'Y/m/d H-M-S')
+    dteS = drw.datetimeStr_conversion(str(dte) + ',' + timeunit, 'cfTime',           \
+     'Y/m/d H-M-S')
+
+    print '  ' + fname + ': plotting from: ' + dtiS + ' to ' + dteS
+
+    timepos, timelabels = drw.CFtimes_plot(tvalsticks, timeunit, timekind, timefmt)
+
+    # ranges
+    if rangevals == 'None':
+        valmin = minvval
+        valmax = maxvval
+    else:
+        if rangevals.split(',')[0] != 'auto':
+            valmin = np.float(rangevals.split(',')[0])
+        else:
+            valmin = minvval
+        if rangevals.split(',')[1] != 'auto':
+            valmax = np.float(rangevals.split(',')[1])
+        else:
+            valmax = maxvval
+
+    xstyl, xaxf, Nxax, xaxor, ystyl, yaxf, Nyax, yaxor = drw.format_axes(dimxyfmt,',')
+    xaxis = [xstyl, xaxf, Nxax, xaxor]
+    yaxis = [ystyl, yaxf, Nyax, yaxor]
+
+    # Legend 
+    legloc, legsiz = drw.legend_values(legvalues,'|')
+
+    valuesvals = [valaxis, axislab, axisunits, [valmin, valmax]]
+
+    # line-characteristics
+    expectargs = '[color]:[linetype]:[linewidht]:[markertype]:[markersize]'
+    Lvals = datavals.split(':')
+    gen.check_arguments('data-line characterisitcs: '+fname,datavals,expectargs,':')
+
+    Lcol = Lvals[0]
+    Ltype = Lvals[1]
+    Lwidth = Lvals[2]
+    Lmarker = Lvals[3]
+    Lsizem = Lvals[4]
+
+    if len(labels) != len(linevals):
+        print errormsg
+        print '  ' + fname + ': There are', len(linevals), 'lines to plot but',      \
+          len(labels), 'labels provided !!'
+        print '    provided labels:', labels
+        quit(-1)
+
+    if outfile:
+        onew = NetCDFFile(ofilen, 'w')
+        Lstr = 250
+    
+        # Dimensions
+        newdim = onew.createDimension('data', Ndata)
+        newdim = onew.createDimension('ensemble', Nens)
+        newdim = onew.createDimension('time', dimt)
+        newdim = onew.createDimension('Lstring', Lstr)
+    
+        # Dimension variables
+        newvar = onew.createVariable('time', 'f8', ('time'))
+        ncvar.basicvardef(newvar, 'time', 'Time', timeunit)
+        newvar.setncattr('axis', 'T')
+        newvar.setncattr('_CoordinateAxisType', 'Time')
+        newvar.setncattr('calendar', 'gregorian')
+        newvar[:] = etimevals[0]
+
+        newvar = onew.createVariable('ensfilen', 'c', ('ensemble', 'Lstring'))
+        ncvar.basicvardef(newvar, 'ensfilen', 'name of files of the ensemble data',  \
+          '-')
+        newvals = ncvar.writing_str_nc(newvar, ensfilens, Lstr)
+        onew.sync()
+    
+        # Global attributes
+        ncvar.add_global_PyNCplot(onew, mainn, fname, '1.0')
+
+    else:
+        onew = None
+
+    drw.plot_TimeEnsembles(linevals, ENSvals, ltimevals, TENSvals, xaxis, yaxis,     \
+      valuesvals, Lcol, Ltype, Lwidth, Lmarker, Lsizem, labels, figenskind, timen,   \
+      timepos, timelabels, graphtit, legloc, legsiz, kindfig, fign, onew, close)
+
+    if onew is not None:
+        ovals = onew.variables['vals']
+        if valskindn == 'FromEns':
+            eSvals = valskind.split(',')
+            ensdims = eSvals[1].split('@')
+            ensn = eSvals[2]
+
+            if ensn != 'weightedmean':
+                txt = 'values retrieved from all ensembles file as ' + ensn +        \
+                  ' along dimensions ' + ','.join(ensdims)
+            else:
+                newvar = onew.createVariable('weight', 'f', ('ensemble'))
+                ncvar.basicvardef(newvar, 'weight', 'weights for the weighted mean', \
+                  '-')
+                newvar[:] = wghtvs
+                txt = 'values retrieved from all ensembles file as weighted mean' +  \
+                  ' along dimensions ' + ','.join(ensdims)
+
+            newattr = ncvar.set_attribute(ovals, 'data_origin', txt)
+
+            onew.sync()
+
+        elif valskindn == 'FromIndivEns':
+            eSvals = valskind.split(',')
+            ensdims = eSvals[1].split('@')
+            ensn = eSvals[2]
+
+            txt = 'values retrieved from each ensembles file as ' + ensn +           \
+              ' along dimensions ' + ','.join(ensdims)
+            newattr = ncvar.set_attribute(ovals, 'data_origin', txt)
+
+            newvar = onew.createVariable('datafilen', 'c', ('data', 'Lstring'))
+            ncvar.basicvardef(newvar, 'datafilen', 'name of files for the ensemble ',\
+              'data', '-')
+            newvals = ncvar.writing_str_nc(newvar, indenfns, Lstr)
+            onew.sync()
+
+        onew.sync()
+        onew.close()
+
+        print fname + ": succesful writting of output file '" + ofilen + "' !!"
+
+    return
+
 #quit()
 
@@ -7569,5 +8136,6 @@
   'draw_2D_shad_cont_time',                                                          \
   'draw_2D_shad_line', 'draw_2D_shad_line_time', 'draw_2lines', 'draw_2lines_time',  \
-  'draw_bar', 'draw_bar_line', 'draw_bar_line_time', 'draw_bar_time', 'draw_lines',  \
+  'draw_bar', 'draw_bar_line', 'draw_bar_line_time', 'draw_bar_time',                \
+  'draw_ensembles_time', 'draw_lines',                                               \
   'draw_lines_time', 'draw_points', 'draw_Taylor', 'draw_time_lag',                  \
   'draw_topo_geogrid_boxes',                                                         \
@@ -7638,4 +8206,6 @@
     elif oper == 'draw_basins':
         draw_basins(opts.ncfile, opts.values)
+    elif oper == 'draw_ensembles_time':
+        draw_ensembles_time(opts.ncfile, opts.values)
     elif oper == 'draw_Neighbourghood_evol':
         draw_Neighbourghood_evol(opts.ncfile, opts.values, opts.varname)
