Changeset 1559 in lmdz_wrf for trunk/tools/drawing.py


Ignore:
Timestamp:
May 9, 2017, 3:51:13 PM (8 years ago)
Author:
lfita
Message:

Adding:

`draw_ensembles_time': Function to plot an ensembles of data following an axis-time

Replacing:

'%' character of separation by '#' in list of files for `bar' plots

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/drawing.py

    r1553 r1559  
    5050## 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'
    5151## 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'
     52## 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'
     53
    5254
    5355#######
     
    6971# draw_barbs: Function to plot wind barbs
    7072# draw_basins: Function to plot river basins with their discharge vector and basins id (from 'routing.nc')
     73# draw_ensembles_time: Function to plot an ensembles of data following an axis-time
    7174# draw_lines: Function to draw different lines at the same time from different files
    7275# draw_lines_time: Function to draw different lines at the same time from different files with times
     
    100103  'draw_2D_shad_line',                                                               \
    101104  'draw_2D_shad_line_time', 'draw_bar', 'draw_bar_line', 'draw_bar_line_time',       \
    102   'draw_bar_time', 'draw_barbs', 'draw_basins',                                      \
     105  'draw_bar_time', 'draw_barbs', 'draw_basins', 'draw_ensembles_time',               \
    103106  'draw_2lines', 'draw_2lines_time', 'draw_lines', 'draw_lines_time',                \
    104107  'draw_Neighbourghood_evol',                                                        \
     
    64216424def draw_bar(ncfile, values):
    64226425    """ Function to plot a bar char figure with multiple sources
    6423       ncfile = ':' list of [filen];[dimslice];[xvarn];[yvarn]
     6426      ncfile = '#' list of [filen];[dimslice];[xvarn];[yvarn]
    64246427        [filen]: name of the file
    64256428        [dimslice]: '|' list of [dimn],[dimrange]
     
    64956498
    64966499    # files and values
    6497     filedvs = ncfile.split(':')
     6500    filedvs = ncfile.split('#')
    64986501    xvals = []
    64996502    yvals = []
     
    65666569def draw_bar_time(ncfile, values):
    65676570    """ Function to plot a bar char figure with multiple sources and time-axis
    6568       ncfile = ':' list of [filen];[dimslice];[tvarn];[yvarn]
     6571      ncfile = '#' list of [filen];[dimslice];[tvarn];[yvarn]
    65696572        [filen]: name of the file
    65706573        [dimslice]: '|' list of [dimn],[dimrange]
     
    66526655
    66536656    # files and values
    6654     filedvs = ncfile.split(':')
     6657    filedvs = ncfile.split('#')
    66556658    tvals = []
    66566659    yvals = []
     
    67766779def draw_bar_line(ncfile, values):
    67776780    """ Function to plot a bar char and lines plot figure with multiple sources
    6778       ncfile = ':' list of [filen];[dimslice];[xvarn];[yvarn];[kind]
     6781      ncfile = '#' list of [filen];[dimslice];[xvarn];[yvarn];[kind]
    67796782        [filen]: name of the file
    67806783        [dimslice]: '|' list of [dimn],[dimrange]
     
    68726875
    68736876    # files and values
    6874     filedvs = ncfile.split(':')
     6877    filedvs = ncfile.split('#')
    68756878    barxvals = []
    68766879    baryvals = []
     
    69916994def draw_bar_line_time(ncfile, values):
    69926995    """ Function to plot a bar char and lines plot figure with multiple sources and time-axis
    6993       ncfile = ':' list of [filen];[dimslice];[xvarn];[yvarn];[kind]
     6996      ncfile = '#' list of [filen];[dimslice];[xvarn];[yvarn];[kind]
    69946997        [filen]: name of the file
    69956998        [dimslice]: '|' list of [dimn],[dimrange]
     
    70957098
    70967099    # files and values
    7097     filedvs = ncfile.split(':')
     7100    filedvs = ncfile.split('#')
    70987101    bartvals = []
    70997102    baryvals = []
     
    72547257def draw_time_lag(ncfile, values):
    72557258    """ Function to plot a time-lag figure with multiple sources (x, previous values; y, future values)
    7256       ncfile = ':' list of couples ('%' formed) [prefilen];[predslice];[pretvarn];[prevarn]%[futfilen];
     7259      ncfile = '#' list of couples ('%' formed) [prefilen];[predslice];[pretvarn];[prevarn]%[futfilen];
    72577260          [futdslice];[futtvarn];[futvarn]
    72587261        'pre': values to act as previous values
     
    73807383
    73817384    # files and values
    7382     filedvs = ncfile.split(':')
     7385    filedvs = ncfile.split('#')
    73837386    timevalues = []
    73847387    prevalues = []
     
    75477550    return
    75487551
     7552def draw_ensembles_time(ncfile, values):
     7553    """ Function to plot an ensembles of data following an axis-time
     7554      ncfile = '#' list of files [filen];[slice];[tvarn];[varn];[kinddata]
     7555        [filen]: name of the file
     7556        [dimslice]: '|' list of [dimn],[dimrange]
     7557          [dimn]: name of the dimension
     7558          [dimrange]: range for the slice
     7559            * [integer]: which value of the dimension
     7560            * -1: all along the dimension
     7561            * -9: last value of the dimension
     7562            * [beg]@[end]@[inc] slice from [beg] to [end] every [inc]
     7563            * NOTE, no dim name all the dimension size
     7564        [tvarn]: name of the variable time ('WRFtime', for times from WRF)
     7565        [varn]: name of the variable to use for the values
     7566        [kinddata]: which kind of use should be done:
     7567          'data': to be plot as data
     7568          'ensemble': to be used as a member of the ensemble
     7569      values = [dimxyfmt];[valaxis];[axislab];[axisunits];[rangevals];[datavals];[timevals];[valskind];
     7570          [labels];[figenskind];[graphtit];[legvalues];[kindfig];[fign];[outfile];[close]
     7571        [dimxyfmt]=[dxs],[dxf],[Ndx],[ordx],[dys],[dyf],[Ndy],[ordy]: format of the values at each
     7572            axis (or single 'auto' for 'auto','auto')
     7573          [dxs]: style of x-axis ('auto' for 'pretty')
     7574            'Nfix', values computed at even 'Ndx'
     7575            'Stringlist', a given list of strings
     7576            'Vfix', values computed at even 'Ndx' increments
     7577            'pretty', values computed following aprox. 'Ndx' at 'pretty' intervals (2.,2.5,4,5,10)
     7578          [dxf]: format of the labels at the x-axis
     7579          [Ndx]: Number of ticks at the x-axis (for 'Stringlist', '@' list of labels)
     7580          [ordx]: angle of orientation of ticks at the x-axis ('auto' for horizontal)
     7581          [dys]: style of y-axis ('auto' for 'pretty')
     7582          [dyf]: format of the labels at the y-axis
     7583          [Ndy]: Number of ticks at the y-axis
     7584          [ordy]: angle of orientation of ticks at the y-axis ('auto' for horizontal)
     7585        [valaxis]: which axes should be used for the data
     7586        [axislab]: Label for the values-axis ('!' for spaces)
     7587        [axisunits]: units of the values-label
     7588        [rangevals]: Range of the axis with the values (single 'None' for 'auto','auto')
     7589         [vmin],[vmax]: minimum and maximum values
     7590           'auto': the computed minimumm or maximum of the values
     7591        [datavals]: [color]:[linetype]:[linewidht]:[markertype]:[markersize] values for the data-lines
     7592          [color]: '@' list of colors of the lines (single value for the same, or 'auto')
     7593          [linetype]: '@' list of type of the lines (single value for the same, or 'auto')
     7594          [linewidht]: '@' list of width of the lines (single value for the same, or 'auto')
     7595          [markertype]: '@' list of type of the markers (single value for the same, or 'auto')
     7596          [markersize]: '@' list of size of the markers (single value for the same, or 'auto')
     7597        [timevals]: [timen]|[units]|[kind]|[tfmt] time labels characteristics
     7598          [timen]; name of the time variable
     7599          [units]; units string according to CF conventions ([tunits] since
     7600            [YYYY]-[MM]-[DD] [[HH]:[MI]:[SS]], '!' for spaces)
     7601          [kind]; kind of output
     7602            'Nval': according to a given number of values as 'Nval',[Nval]
     7603            'exct': according to an exact time unit as 'exct',[tunit];
     7604              tunit= [Nunits],[tu]; [tu]= 'c': centuries, 'y': year, 'm': month,
     7605               'w': week, 'd': day, 'h': hour, 'i': minute, 's': second,
     7606               'l': milisecond
     7607          [tfmt]; desired format
     7608        [valskind]: way to compute the values to be plotted
     7609          'FromFile': from the files with the 'data' flag
     7610          'FromEns',[usedims],[stats]: from all the files with the 'ensemble' flag with statistics [stats],
     7611              throughout dimensions [usedims]
     7612            [usedims]= '@' list of dimensions to take to compute the statistics
     7613            [stats]= statistics to use
     7614              'direct': without statistics
     7615              'mean': mean value of all files with ensemble flag
     7616              'median': median value of all files with ensemble flag
     7617              'weightedmean,[stats],[weights]': weighted mean value all files with ensemble flag
     7618                [stats] = statistics to use from each member: 'direct', 'mean', 'median'
     7619                [weights] = '@' list of weights [0,1]
     7620          'FromIndivEns',[usedims],[stats]: from each files with the 'ensemble' flag with statistics [stats],
     7621              throughout dimensions [usedims]
     7622            [usedims]= '@' list of dimensions to take to compute the statistics
     7623            [stats]= statistics to use:
     7624              'mean': mean value of each file with ensemble flag
     7625              'median': median value of each file with ensemble flag
     7626        [labels]: ',' list of labels for the legend ('None' for no-labels, '!' for spaces)
     7627        [figenskind]=[kindname],[valueskind] kind of ensemble plot
     7628          'cloud',[color],[limits]: ensembles values as a cloud
     7629            [color]: name of the color to use ('auto' for '#DDDDDD')
     7630            [limits]: which stadistics to use for the plot ('auto' for extremes)
     7631              '5quantile': 5quantile,95quantile of the values
     7632              'direct': below and up values are tacking directly from the provided data
     7633              'extremes': min,max of the values
     7634              'stddev': standard deviation fo the values (only available if there is one line)
     7635          'spaghetti',[color],[width],[type]: a line for each member
     7636            [color]: name of the color to use for all lines ('auto' for '#DDDDDD')
     7637            [width]: width of the line to use for all lines ('auto' for 0.5)
     7638            [type]: type of the line to use for all lines ('auto' for '-')
     7639        [graphtit]: title of the figure '!' for spaces
     7640        [legvalues]=[locleg]|[fontsize]:
     7641          [locleg]: location of the legend (0, autmoatic)
     7642            1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right',
     7643            5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center',
     7644            9: 'upper center', 10: 'center'
     7645          [fontsize]: font size for the legend (auto for 12)
     7646        [kindfig]: kind of output figure (png, pdf, ps)
     7647        [fign]: name of the figure (without extension)
     7648        [outfile]: wehter or not a file with the result of the computation of the ensemble should be
     7649          written or not (labelled 'ensemble_ofile.nc')
     7650        [close]: whether figure should be close or not
     7651    """
     7652    fname = 'draw_ensembles_time'
     7653
     7654    if values == 'h':
     7655        print fname + '____________________________________________________________'
     7656        print draw_ensembles_time.__doc__
     7657        quit()
     7658
     7659    expectargs = '[dimxyfmt];[axeskind];[axislab];[axisunits];[rangevals];' +        \
     7660      '[datavals];[timevals];[valskind];[labels];[figenskind];[graphtit];' +         \
     7661      '[legvalues];[kindfig];[fign];[outfile];[close]'
     7662    drw.check_arguments(fname,values,expectargs,';')
     7663
     7664    dimxyfmt = values.split(';')[0]
     7665    valaxis = values.split(';')[1]
     7666    axislab = values.split(';')[2].replace('!',' ')
     7667    axisunits = values.split(';')[3]
     7668    rangevals = values.split(';')[4]
     7669    datavals = values.split(';')[5]
     7670    timevals = values.split(';')[6]
     7671    valskind = values.split(';')[7]
     7672    labels = gen.str_list(values.split(';')[8].replace('!', ' '), ',')
     7673    figenskind = values.split(';')[9]
     7674    graphtit = values.split(';')[10].replace('!',' ')
     7675    legvalues = values.split(';')[11]
     7676    kindfig = values.split(';')[12]
     7677    fign = values.split(';')[13]
     7678    outfile = gen.Str_Bool(values.split(';')[14])
     7679    close = gen.Str_Bool(values.split(';')[15])
     7680
     7681    ofilen = 'ensemble_ofile.nc'
     7682
     7683    # files
     7684    valfiles = gen.str_list(ncfile, '#')
     7685
     7686    # time-values
     7687    expectargs = '[timen]|[units]|[kind]|[tfmt]'
     7688    drw.check_arguments('timevals: '+fname,timevals,expectargs,'|')
     7689
     7690    timen = timevals.split('|')[0].replace('!',' ')
     7691    timeunit = timevals.split('|')[1].replace('!',' ')
     7692    timekind = timevals.split('|')[2]
     7693    timefmt = timevals.split('|')[3]
     7694
     7695    linevals = []
     7696    ltimevals = []
     7697    ensvals = []
     7698    etimevals = []
     7699
     7700    # List of files used for the ensemble
     7701    ensfilens = []
     7702    # List of files used for the ensemble with its dimension and variable name
     7703    edv_filens = []
     7704    # Dictionary with the dimensions of the values from a given file
     7705    dimsfile = {}
     7706
     7707    mintval = gen.fillValueF
     7708    maxtval = -gen.fillValueF
     7709    minvval = gen.fillValueF
     7710    maxvval = -gen.fillValueF
     7711
     7712    ensfilens = []
     7713    ifile = 0
     7714    dimt = -gen.fillValueF
     7715    for fdv in valfiles:
     7716        fvals = fdv.split(';')
     7717        expectedargs = '[filen];[slice];[tvarn];[varn];[kinddata]'
     7718        gen.check_arguments('filevals: '+fname, fdv, expectedargs, ';')
     7719        filen = fvals[0]
     7720        dimrange = fvals[1]
     7721        tvarn = fvals[2]
     7722        yvarn = fvals[3]
     7723        datatype = fvals[4]
     7724
     7725        edv = filen + '#' + dimrange + '#' + yvarn
     7726
     7727        if not os.path.isfile(filen):
     7728            print errormsg
     7729            print '  ' + fname + ': file "' + filen + '" does not exist !!'
     7730            quit(-1)
     7731
     7732        onc = NetCDFFile(filen, 'r')
     7733        if tvarn == 'WRFtime':
     7734            datesv = []
     7735            if tvarn == 'WRFtime':
     7736                fdates = onc.variables['Times'][:]
     7737                for it in range(fdates.shape[0]):
     7738                    datesv.append(gen.datetimeStr_conversion(fdates[it,:],           \
     7739                      'WRFdatetime','cfTime,' + timeunit))
     7740            tvs = np.array(datesv, dtype=np.float)
     7741            tunitsvar = timeunit
     7742        else:
     7743            if not onc.variables.has_key(tvarn):
     7744                print errormsg
     7745                print '  ' +fname+ ': file "' +filen+ "' does not have variable '" + \
     7746                  tvarn + "' for the time-values !!"
     7747                print '    available ones:', onc.variables.keys()
     7748                onc.close()
     7749                quit(-1)
     7750            otvar = onc.variables[tvarn]
     7751            tvs, tdims = ncvar.slice_variable(otvar, dimrange.replace(',',':'))
     7752            tunitsvar = otvar.units
     7753
     7754        if not onc.variables.has_key(yvarn):
     7755            print errormsg
     7756            print '  ' +fname+ ': file "' +filen+ "' does not have variable '" +     \
     7757              yvarn + "' for the y-values !!"
     7758            print '    available ones:', onc.variables.keys()
     7759            onc.close()
     7760            quit(-1)
     7761
     7762        oyvar = onc.variables[yvarn]
     7763        yvs, ydims = ncvar.slice_variable(oyvar, dimrange.replace(',',':'))
     7764        dimsfile[edv] = ydims
     7765
     7766        # Referring all times to the same reference time!
     7767        reftvals = gen.coincident_CFtimes(tvs, timeunit, tunitsvar)
     7768
     7769        onc.close()
     7770
     7771        if len(reftvals) > dimt: dimt = len(reftvals)
     7772
     7773        reftn = reftvals.min()
     7774        reftx = reftvals.max()
     7775        if reftn < mintval: mintval = reftn
     7776        if reftx > maxtval: maxtval = reftx
     7777        refvn = yvs.min()
     7778        refvx = yvs.max()
     7779        if refvn < minvval: minvval = refvn
     7780        if refvx > maxvval: maxvval = refvx
     7781
     7782        if datatype == 'data':
     7783            if len(tvs.shape) != 1:
     7784                print errormsg
     7785                print '  ' +fname+ ': provided slice:', dimrange, "for variable '" +     \
     7786                 tvarn + "' and '" + datatype + "' does not provide a rank-1 value !!"
     7787                print '    resultant shape:', tvs.shape, 'dimensions:',tdims,'fix it !!'
     7788                quit(-1)
     7789            if len(yvs.shape) != 1:
     7790                print errormsg
     7791                print '  ' +fname+ ': provided slice:', dimrange, "for variable '" +     \
     7792                  yvarn + "' and '" + datatype + "'does not provide a rank-1 value !!"
     7793                print '    resultant shape:',yvs.shape, 'dimensions:',ydims,'fix it !!'
     7794                quit(-1)
     7795            linevals.append(yvs)
     7796            ltimevals.append(reftvals)
     7797        else:
     7798            ensfilens.append(filen)
     7799            edv_filens.append(edv)
     7800            ensvals.append(yvs)
     7801            etimevals.append(reftvals)
     7802
     7803        ifile = ifile + 1
     7804
     7805    Nens = len(ensvals)
     7806
     7807    valskindn = gen.str_list(valskind, ',')[0]
     7808
     7809    ENSvals = []
     7810    TENSvals = []
     7811
     7812    # Getting values to draw as lines
     7813    if valskindn == 'FromFile':
     7814        if len(linevals) == 0:
     7815            print errormsg
     7816            print '  ' + fname + ": you asked to get data '" + valskind +            \
     7817              "' but no file with flag 'data' was provided !!"
     7818            print '    provided file characteristics _______'
     7819            for fdv in prefutdv:
     7820                print fdv.split(';')
     7821            quit(-1)
     7822        ENSvals = list(ensvals)
     7823        TENSvals = list(etimevals)
     7824    elif valskindn == 'FromEns':
     7825        eSvals = valskind.split(',')
     7826
     7827        expectedargs='FromEns,[usedims],[stats]'
     7828        gen.check_arguments('FromEns: '+fname, valskind, expectedargs, ',')
     7829
     7830        ensdims = eSvals[1].split('@')
     7831        ensn = eSvals[2]
     7832        iedv = 0
     7833        # Temporal slices for each file/variable
     7834        Tslices = {}
     7835        for edv in edv_filens:
     7836            vvalues = ensvals[iedv]
     7837            edv_dims = dimsfile[edv]
     7838            idim = []
     7839            for dsts in ensdims:
     7840                idim.append(edv_dims.index(dsts))
     7841            # Adding each running value as a new member of the ensemble
     7842            NOTusedim = []
     7843            for dn in dimsfile[edv]:
     7844                if not gen.searchInlist(ensdims, dn): NOTusedim.append(dn)
     7845            varslices = gen.provide_slices(dimsfile[edv], vvalues.shape, NOTusedim)
     7846            Tslices[edv] = varslices
     7847
     7848            if iedv == 0: dimt = len(varslices)
     7849            else:
     7850                if dimt != len(varslices):
     7851                    print errormsg
     7852                    print '  ' + fname + ': this is too complicated !!'
     7853                    print '    for file:', iedv, ' with ', len(varslices),           \
     7854                      'and initial dimt:', dimt
     7855                    print '    not ready to deal with files with different number '+ \
     7856                      'of time-steps!!'
     7857                    quit(-1)
     7858            # Adding each not runnuning grid-point as a new ensemble member
     7859            varslices = gen.provide_slices(dimsfile[edv], vvalues.shape, ensdims)
     7860            for slc in varslices:
     7861                ENSvals.append(np.squeeze(vvalues[tuple(slc)]))
     7862                TENSvals.append(np.squeeze(np.array(etimevals[iedv])))
     7863
     7864            iedv = iedv + 1
     7865
     7866        lvals = np.zeros((dimt), dtype=np.float)
     7867        for it in range(dimt):
     7868            iedv = 0     
     7869            Tvals = []
     7870            Tvalse = {}
     7871            for edv in edv_filens:
     7872                vvalues = ensvals[iedv]
     7873                varslics = Tslices[edv]
     7874                Tvals = Tvals + list(np.squeeze(vvalues[tuple(varslics[dimt-it-1])]).flatten())
     7875                Tvalse[edv] = np.squeeze(vvalues[tuple(varslics[dimt-it-1])])
     7876
     7877            aTvals = np.array(Tvals)
     7878            if ensn == 'mean':
     7879                lvals[it] = np.mean(aTvals)
     7880            elif ensn == 'median':
     7881                lvals[it] = np.median(aTvals)
     7882            elif ensn[0:12] == 'weightedmean':
     7883                expectedargs='FromEns,[usedims],weightedmean,[stats],[weights]'
     7884                gen.check_arguments('FromEns: '+fname, valskind, expectedargs, ',')
     7885
     7886                stats = eSvals[3]
     7887                wgths = eSvals[4].split('@')
     7888                wghtvs = np.array(wgths, dtype=np.float)
     7889                if len(wgths) != Nens:
     7890                    print errormsg
     7891                    print '  ' + fname + ': the number of members of the ensemble:', \
     7892                      Nens, 'and the number of provided weights:', len(wghts),       \
     7893                      'do not coincide !!'
     7894                    print '    provided weights:', wgths
     7895                    print '    provided file characteristics _______'
     7896                    for fdv in prefutdv:
     7897                        print fdv.split(';')
     7898                    quit(-1)
     7899                wgthvals = np.zeros((Nens), dtype=np.float)
     7900                iedv = 0
     7901                for edv in edv_filens:
     7902                    if stats == 'mean':
     7903                        wgthvals[iedv] = np.mean(Tvalse[edv])
     7904                    elif stats == 'median':
     7905                        wgthvals[iedv] = np.median(Tvalse[edv])
     7906                    else:
     7907                        print errormsg
     7908                        print '  ' +fname+ ": statistics '" + eSvals[2] + "' not ready !!"
     7909                        print '    available ones:', ['mean', 'median']
     7910                        quit(-1)
     7911                lvals[it] = np.sum(wghtvals*wghtvs)/np.sum(wghtvs)
     7912            else:
     7913                print errormsg
     7914                print '  ' + fname + ": for '" + valskindn + "'statistics '" +       \
     7915                  eSvals[2] + "' not ready !!"
     7916                print '    available ones:', ['mean', 'median', 'weightedmean']
     7917                quit(-1)
     7918            iedv = iedv + 1
     7919        linevals.append(lvals)
     7920        ltimevals.append(np.squeeze(np.array(etimevals[0], dtype=np.float)))
     7921    elif valskindn == 'FromIndivEns':
     7922        indenfns = []
     7923        eSvals = valskind.split(',')
     7924
     7925        expectedargs='FromIndivEns,[usedims],[stats]'
     7926        gen.check_arguments('FromEns: '+fname, valskind, expectedargs, ',')
     7927
     7928        ensdims = eSvals[1].split('@')
     7929        ensn = eSvals[2]
     7930        iedv = 0
     7931        evals = np.zeros((dimt, Nens), dtype=np.float)
     7932        for edv in edv_filens:
     7933            vvalues = ensvals[iedv]
     7934            edv_dims = dimsfile[edv]
     7935            filen = edv.split('#')[0]
     7936            indenfns.append(filen)
     7937
     7938            idim = []
     7939            for dsts in ensdims:
     7940                idim.append(edv_dims.index(dsts))
     7941            # Adding each running value as a new member of the ensemble
     7942            NOTusedim = []
     7943            for dn in dimsfile[edv]:
     7944                if not gen.searchInlist(ensdims, dn): NOTusedim.append(dn)
     7945            varslices = gen.provide_slices(dimsfile[edv], vvalues.shape, ensdims)
     7946            for varslc in varslices:
     7947                ENSvals.append(np.squeeze(vvalues[tuple(varslc)]))
     7948                TENSvals.append(np.squeeze(np.array(etimevals[iedv], dtype=np.float)))
     7949                # No need to repeat File name len(varslices)0 times
     7950                # ensfilens.append(filen)
     7951            if ensn == 'mean':
     7952                linevals.append(np.mean(vvalues, axis=tuple(idim)))
     7953            elif ensn == 'median':
     7954                linevals.append(np.median(vvalues, axis=tuple(idim)))
     7955            elif ensn == 'direct':
     7956                if len(values.shape) != 1:
     7957                    print errormsg
     7958                    print '  ' +fname+ ': provided slice:', dimrange,                \
     7959                      "for variable '" + yvarn + "' and '" + datatype +              \
     7960                      "'does not provide a rank-1 value !!"
     7961                    print '    resultant shape:', yvs.shape, 'dimensions:', ydims,   \
     7962                      'fix it !!'
     7963                    quit(-1)
     7964                linevals.append(vvalues)
     7965            else:
     7966                print errormsg
     7967                print '  ' + fname + ": for '" + valskindn + "' statistics '" +      \
     7968                  eSvals[2] + "' not ready !!"
     7969                print '    available ones:', ['mean', 'median']
     7970                quit(-1)
     7971            ltimevals.append(np.squeeze(np.array(etimevals[iedv], dtype=np.float)))
     7972            iedv = iedv + 1
     7973    else:
     7974        print errormsg
     7975        print '  ' + fname + ": kind of values '" + valskindn + "' not ready !!"
     7976        print '    available ones:', ['FromFile', 'FromEns', 'FromIndivEns']
     7977        quit(-1)
     7978
     7979    Ndata = len(linevals)
     7980    Nens = len(ENSvals)
     7981
     7982    # Times
     7983    dtvals = (maxtval - mintval)/10
     7984    dti = mintval
     7985    dte = maxtval
     7986    tvalsticks = np.arange(dti, dte+dtvals, dtvals)
     7987
     7988    dtiS = drw.datetimeStr_conversion(str(dti) + ',' + timeunit, 'cfTime',           \
     7989     'Y/m/d H-M-S')
     7990    dteS = drw.datetimeStr_conversion(str(dte) + ',' + timeunit, 'cfTime',           \
     7991     'Y/m/d H-M-S')
     7992
     7993    print '  ' + fname + ': plotting from: ' + dtiS + ' to ' + dteS
     7994
     7995    timepos, timelabels = drw.CFtimes_plot(tvalsticks, timeunit, timekind, timefmt)
     7996
     7997    # ranges
     7998    if rangevals == 'None':
     7999        valmin = minvval
     8000        valmax = maxvval
     8001    else:
     8002        if rangevals.split(',')[0] != 'auto':
     8003            valmin = np.float(rangevals.split(',')[0])
     8004        else:
     8005            valmin = minvval
     8006        if rangevals.split(',')[1] != 'auto':
     8007            valmax = np.float(rangevals.split(',')[1])
     8008        else:
     8009            valmax = maxvval
     8010
     8011    xstyl, xaxf, Nxax, xaxor, ystyl, yaxf, Nyax, yaxor = drw.format_axes(dimxyfmt,',')
     8012    xaxis = [xstyl, xaxf, Nxax, xaxor]
     8013    yaxis = [ystyl, yaxf, Nyax, yaxor]
     8014
     8015    # Legend
     8016    legloc, legsiz = drw.legend_values(legvalues,'|')
     8017
     8018    valuesvals = [valaxis, axislab, axisunits, [valmin, valmax]]
     8019
     8020    # line-characteristics
     8021    expectargs = '[color]:[linetype]:[linewidht]:[markertype]:[markersize]'
     8022    Lvals = datavals.split(':')
     8023    gen.check_arguments('data-line characterisitcs: '+fname,datavals,expectargs,':')
     8024
     8025    Lcol = Lvals[0]
     8026    Ltype = Lvals[1]
     8027    Lwidth = Lvals[2]
     8028    Lmarker = Lvals[3]
     8029    Lsizem = Lvals[4]
     8030
     8031    if len(labels) != len(linevals):
     8032        print errormsg
     8033        print '  ' + fname + ': There are', len(linevals), 'lines to plot but',      \
     8034          len(labels), 'labels provided !!'
     8035        print '    provided labels:', labels
     8036        quit(-1)
     8037
     8038    if outfile:
     8039        onew = NetCDFFile(ofilen, 'w')
     8040        Lstr = 250
     8041   
     8042        # Dimensions
     8043        newdim = onew.createDimension('data', Ndata)
     8044        newdim = onew.createDimension('ensemble', Nens)
     8045        newdim = onew.createDimension('time', dimt)
     8046        newdim = onew.createDimension('Lstring', Lstr)
     8047   
     8048        # Dimension variables
     8049        newvar = onew.createVariable('time', 'f8', ('time'))
     8050        ncvar.basicvardef(newvar, 'time', 'Time', timeunit)
     8051        newvar.setncattr('axis', 'T')
     8052        newvar.setncattr('_CoordinateAxisType', 'Time')
     8053        newvar.setncattr('calendar', 'gregorian')
     8054        newvar[:] = etimevals[0]
     8055
     8056        newvar = onew.createVariable('ensfilen', 'c', ('ensemble', 'Lstring'))
     8057        ncvar.basicvardef(newvar, 'ensfilen', 'name of files of the ensemble data',  \
     8058          '-')
     8059        newvals = ncvar.writing_str_nc(newvar, ensfilens, Lstr)
     8060        onew.sync()
     8061   
     8062        # Global attributes
     8063        ncvar.add_global_PyNCplot(onew, mainn, fname, '1.0')
     8064
     8065    else:
     8066        onew = None
     8067
     8068    drw.plot_TimeEnsembles(linevals, ENSvals, ltimevals, TENSvals, xaxis, yaxis,     \
     8069      valuesvals, Lcol, Ltype, Lwidth, Lmarker, Lsizem, labels, figenskind, timen,   \
     8070      timepos, timelabels, graphtit, legloc, legsiz, kindfig, fign, onew, close)
     8071
     8072    if onew is not None:
     8073        ovals = onew.variables['vals']
     8074        if valskindn == 'FromEns':
     8075            eSvals = valskind.split(',')
     8076            ensdims = eSvals[1].split('@')
     8077            ensn = eSvals[2]
     8078
     8079            if ensn != 'weightedmean':
     8080                txt = 'values retrieved from all ensembles file as ' + ensn +        \
     8081                  ' along dimensions ' + ','.join(ensdims)
     8082            else:
     8083                newvar = onew.createVariable('weight', 'f', ('ensemble'))
     8084                ncvar.basicvardef(newvar, 'weight', 'weights for the weighted mean', \
     8085                  '-')
     8086                newvar[:] = wghtvs
     8087                txt = 'values retrieved from all ensembles file as weighted mean' +  \
     8088                  ' along dimensions ' + ','.join(ensdims)
     8089
     8090            newattr = ncvar.set_attribute(ovals, 'data_origin', txt)
     8091
     8092            onew.sync()
     8093
     8094        elif valskindn == 'FromIndivEns':
     8095            eSvals = valskind.split(',')
     8096            ensdims = eSvals[1].split('@')
     8097            ensn = eSvals[2]
     8098
     8099            txt = 'values retrieved from each ensembles file as ' + ensn +           \
     8100              ' along dimensions ' + ','.join(ensdims)
     8101            newattr = ncvar.set_attribute(ovals, 'data_origin', txt)
     8102
     8103            newvar = onew.createVariable('datafilen', 'c', ('data', 'Lstring'))
     8104            ncvar.basicvardef(newvar, 'datafilen', 'name of files for the ensemble ',\
     8105              'data', '-')
     8106            newvals = ncvar.writing_str_nc(newvar, indenfns, Lstr)
     8107            onew.sync()
     8108
     8109        onew.sync()
     8110        onew.close()
     8111
     8112        print fname + ": succesful writting of output file '" + ofilen + "' !!"
     8113
     8114    return
     8115
    75498116#quit()
    75508117
     
    75698136  'draw_2D_shad_cont_time',                                                          \
    75708137  'draw_2D_shad_line', 'draw_2D_shad_line_time', 'draw_2lines', 'draw_2lines_time',  \
    7571   'draw_bar', 'draw_bar_line', 'draw_bar_line_time', 'draw_bar_time', 'draw_lines',  \
     8138  'draw_bar', 'draw_bar_line', 'draw_bar_line_time', 'draw_bar_time',                \
     8139  'draw_ensembles_time', 'draw_lines',                                               \
    75728140  'draw_lines_time', 'draw_points', 'draw_Taylor', 'draw_time_lag',                  \
    75738141  'draw_topo_geogrid_boxes',                                                         \
     
    76388206    elif oper == 'draw_basins':
    76398207        draw_basins(opts.ncfile, opts.values)
     8208    elif oper == 'draw_ensembles_time':
     8209        draw_ensembles_time(opts.ncfile, opts.values)
    76408210    elif oper == 'draw_Neighbourghood_evol':
    76418211        draw_Neighbourghood_evol(opts.ncfile, opts.values, opts.varname)
Note: See TracChangeset for help on using the changeset viewer.