Changeset 1071 in lmdz_wrf for trunk


Ignore:
Timestamp:
Aug 31, 2016, 6:12:37 PM (9 years ago)
Author:
lfita
Message:

Getting on `draw_2lines'

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/drawing.py

    r1070 r1071  
    41154115
    41164116    return
     4117
     4118def draw_2lines(values,ncfiles,varnames):
     4119    """ Fucntion to plot two lines in different axes (x/x2 or y/y2)
     4120      values= [varnA]:[varnB]:[commonvardim]:[varangeA]:[varangeB]:[varangeaxis]:[axisvals]:[figvarns]:[colors]:
     4121       [widths]:[styles]:[sizemarks]:[marks]:[graphtitle]:[labelaxis]:[lloc]:[figname]:[figkind]
     4122        [varnA]: name of the variable A in fileA
     4123        [varnB]: name of the variable B in fileB
     4124        [commonvardim]: name of the common variable-dimension
     4125        [varangeA]: ',' separated list of range (min,max) for A values ('None', automatic; 'Extrs' from values extremes)
     4126        [varangeB]: ',' separated list of range (min,max) for B values ('None', automatic; 'Extrs' from values extremes)
     4127        [varangeaxis]: ',' separated list of range (min,max) for common axis values ('None', automatic; 'Extrs' from
     4128          values extremes)
     4129        [axisvals]: which is the axis to plot the values ('x' or 'y')
     4130        [figvarns]: ',' separated list of names of the variables in the  plot
     4131        [colors]: ',' list with color names of the lines for the variables ('None', automatic)
     4132        [widths]: ',' list with widths of the lines for the variables ('None', automatic)
     4133        [styles]: ',' list with the styles of the lines ('None', automatic)
     4134        [sizemarks]: ',' list with the size of the markers of the lines ('None', automatic)
     4135        [marks]: ',' list with the markers of the lines ('None', automatic)
     4136        [graphtitle]: title of the figure ('!' for spaces)
     4137        [labelaxis]: label in the figure of the common axis ('!' for spaces)
     4138        [lloc]: location of the legend (0, automatic)
     4139          1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right',
     4140          5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center',
     4141          9: 'upper center', 10: 'center'      kfig= kind of figure
     4142        [figname]: name of the figure
     4143        [figkind]: kind of figure
     4144      ncfiles= ',' separated list of files to use
     4145      varnames=  ',' separated list of variables names in the files to plot
     4146    """
     4147    function = 'draw_2lines'
     4148
     4149    if values == 'h':
     4150        print fname + '_____________________________________________________________'
     4151        print draw_2lines.__doc__
     4152        quit()
     4153
     4154    expectargs = '[varnA]:[varnB]:[commonvardim]:[varangeA]:[varangeB]:' +           \
     4155     '[varangeaxis]:[axisvals]:[figvarns]:[colors]:[widths]:[styles]:[sizemarks]:' + \
     4156     '[marks]:[graphtitle]:[labelaxis]:[lloc]:[figname]:[figkind]'
     4157 
     4158    drw.check_arguments(fname,values,expectargs,':')
     4159
     4160    varns = [values.split(':')[0], values.split(':')[1]]
     4161    commonvardim = values.split(':')[2]
     4162    varangeA0 = values.split(':')[3]
     4163    varangeB0 = values.split(':')[4]
     4164    varangeaxis0 = values.split(':')[5]
     4165    axisvals = values.split(':')[6]
     4166    figvarns = values.split(':')[7].split(',')
     4167    colors = gen.str_list(values.split(':')[8],',')
     4168    widths = gen.str_list(values.split(':')[9],',')
     4169    styles = gen.str_list(values.split(':')[10],',')
     4170    sizemarks = gen.str_list(values.split(':')[11],',')
     4171    marks = gen.str_list(values.split(':')[12],',')
     4172    graphtitle = values.split(':')[13].replace('!',' ')
     4173    labelaxis = values.split(':')[14].replace('!',' ')
     4174    lloc = np.int(values.split(':')[15])
     4175    figname = values.split(':')[16]
     4176    figkind = values.split(':')[17]
     4177
     4178    files = ncfiles.split(',')
     4179    invarns = varnames.split(',')
     4180
     4181    varunits = []
     4182
     4183    # Values line A
     4184    if not os.path.isfile(files[0]):
     4185        print errormsg
     4186        print '  ' + fname + ": file '" + files[0] + "' does not exist !!"
     4187        quit(-1)
     4188    oncA = NetCDFFile(files[0], 'r')
     4189
     4190    if not gen.searchInfile(oncA.variables.kesy(), invarns[0]):
     4191        print errormsg
     4192        print '  ' + fname + ": A file '" + files[0] + "' does not have variable '" +\
     4193          invarns[0] + "' !!"
     4194        quit(-1)
     4195    objvA = oncA.variables[invarns[0]]
     4196    varvalsA = objvA[:]
     4197    varangeA = []
     4198
     4199    if gen.searchInfile(objvA.ncattrs(), 'units'):
     4200        varunits.append(objva.getncattr('units'))
     4201    else:
     4202        valsA = gen.variables_values(invarns[0])
     4203        varunits.append(valsA[5])
     4204        if varangeA0 == 'None':
     4205            varangeA = [valsA[2], valsA[3]]
     4206        elif varangeA0 == 'Extrs':
     4207            varangeA = [np.min(varvalsA), np.max(varvalsA)]
     4208        else:
     4209            for iv in range(2): varangeA[iv] = np.float(varangeA0[iv])
     4210
     4211    if not gen.searchInfile(oncA.variables.kesy(), commvardim):
     4212        print errormsg
     4213        print '  ' + fname + ": A file '" + files[0] + "' does not have common " +   \
     4214          "dimvar '" + commonvardim + "' !!"
     4215        quit(-1)
     4216    objvd = oncA.variables[commonvardim]
     4217    varvalsaxisA = objvd[:]   
     4218
     4219    oncA.close()
     4220
     4221    # Values line B
     4222    if not os.path.isfile(files[1]):
     4223        print errormsg
     4224        print '  ' + fname + ": file '" + files[1] + "' does not exist !!"
     4225        quit(-1)
     4226    oncB = NetCDFFile(files[1], 'r')
     4227
     4228    if not gen.searchInfile(oncB.variables.keys(), invarns[1]):
     4229        print errormsg
     4230        print '  ' + fname + ": B file '" + files[1] + "' does not have variable '" +\
     4231          invarns[1] + "' !!"
     4232        quit(-1)
     4233    objvB = oncB.variables[invarns[1]]
     4234    varvalsB = objvB[:]
     4235    varangeB = []
     4236
     4237    if gen.searchInfile(objvB.ncattrs(), 'units'):
     4238        varunits.append(objvB.getncattr('units'))
     4239    else:
     4240        valsB = gen.variables_values(invarns[1])
     4241        varunits.append(valsB[5])
     4242        if varangeB0 == 'None':
     4243            varangeB = [valsB[2], valsB[3]]
     4244        elif varangeB0 == 'Extrs':
     4245            varangeA = [np.min(varvalsA), np.max(varvalsA)]
     4246        else:
     4247            for iv in range(2): varangeB[iv] = np.float(varangeB0[iv])
     4248
     4249    if not gen.searchInfile(oncB.variables.keys(), commvardim):
     4250        print errormsg
     4251        print '  ' + fname + ": B file '" + files[1] + "' does not have common " +   \
     4252          "dimvar '" + commonvardim + "' !!"
     4253        quit(-1)
     4254    objvd = oncB.variables[commonvardim]
     4255    varvalsaxisB = objvd[:]
     4256
     4257    # Range of the axis
     4258    varangeaxis = []
     4259    if gen.searchInfile(objvd.ncattrs(), 'units'):
     4260        dimvarunits.append(objvd.getncattr('units'))
     4261    else:
     4262        valsVD = gen.variables_values(commonvardim)
     4263        varunits.append(valsVD[5])
     4264        if varangeaxis0 == 'None':
     4265            varangeaxis = [valsVD[2], valsVD[3]]
     4266        elif varangeaxis0 == 'Extrs':
     4267            varangeaxis[0] = np.min([np.min(varvalsaxisA), np.min(varvalsaxisB)])
     4268            varangeaxis[1] = np.max([np.max(varvalsaxisA), np.max(varvalsaxisB)])
     4269        else:
     4270            for iv in range(2): varangeaxis0[iv] = np.float(varangeaxis[iv])
     4271   
     4272    oncB.close()
     4273
     4274    plot_2lines(varvalsA, varvalsB, varvalsaxisA, varvalsaxisB, varangeA, varrangeB, \
     4275      varangeaxis, axisvals, varns, varunits, colors, widths, styles, sizemarks,     \
     4276      marks, graphtitle, labelaxis, lloc, figname, figkind):
    41174277
    41184278#quit()
  • trunk/tools/drawing_tools.py

    r1070 r1071  
    74437443    return
    74447444
     7445def plot_2lines(valsA, valsB, valsaxisA, valsaxisB, rangeA, rangeB, rangeaxis,       \
     7446  axisvals, varns, varus, cols, wdths, styls, szmks, marks, gtitle, axisn, gloc,     \
     7447  fign, figk):
     7448    """ Fucntion to plot two lines in different axes (x/x2 or y/y2)
     7449      valsA= values to be plotted on axis x or y
     7450      valsB= values to be plotted on axis x2 or y2
     7451      valsaxisA= values at the common axis for valsA
     7452      valsaxisB= values at the common axis for valsB
     7453      rangeA= range of values for valsA
     7454      rangeB= range of values for valsB
     7455      rangeaxis= range of values for the common axis
     7456      axisvals= which is the axis to plot the values ('x' or 'y')
     7457      varns= names of the variables in the  plot
     7458      varus= units of the variables
     7459      cols= list with color names of the lines for the variables
     7460      wdths= list with widths of the lines for the variables
     7461      styls= list with the styles of the lines
     7462      szmks= list with the size of the markers of the lines
     7463      marks= list with the markers of the lines
     7464      gtitle= title of the figure
     7465      axisn= label in the figure of the common axis
     7466      gloc= location of the legend
     7467      fign= name of the figure
     7468      figk= kind of figure
     7469    """
     7470    function = 'plot_2lines'
     7471
     7472    if axisvals == 'x':
     7473        titX = axisn
     7474        titX2 = axisn
     7475        titY = varns[0] + ' ' + varus[0]
     7476        titY2 = varns[1] + ' (' + varus[1] + ')'
     7477        xmin = rangeaxis[0]
     7478        xmax = rangeaxis[1]
     7479        xmin2 = rangeaxis[0]
     7480        xmax2 = rangeaxis[1]
     7481        ymin = rangeA[0]
     7482        ymax = rangeA[1]
     7483        ymin2 = rangeB[0]
     7484        ymax2 = rangeB[1]
     7485    else:
     7486        titX = varns[0] + ' ' + varus[0]
     7487        titX2 = varns[1] + ' (' + varus[1] + ')'
     7488        titY = axisn
     7489        titY2 = axisn
     7490        xmin = rangeA[0]
     7491        xmax = rangeA[1]
     7492        xmin2 = rangeB[0]
     7493        xmax2 = rangeB[1]
     7494        ymin = rangeaxis[0]
     7495        ymax = rangeaxis[1]
     7496        ymin2 = rangeaxis[0]
     7497        ymax2 = rangeaxis[1]
     7498
     7499# From: http://matplotlib.org/examples/api/two_scales.html
     7500    fig, ax1 = plt.subplots()
     7501
     7502    # line A
     7503    if axisvals == 'x':
     7504        il=0
     7505        ax1.plot(valsaxisA, valsA, styls[il], marker=marks[il], linewidth=wdths[il], \
     7506          markersize=szmks[il], label=varns[il], color=cols[il])
     7507        ax1.set_xlabel(titX, color='black')
     7508        ax1.set_ylabel(titY, color=cols[il])
     7509        ax1.set_xlim(xmin,xmax)
     7510        ax1.set_ylim(ymin,ymax)
     7511
     7512        ax2 = ax1.twiny()
     7513        il = il + 1
     7514        ax2.plot(valsaxisB, valsB, styls[il], marker=marks[il], linewidth=wdths[il], \
     7515          markersize=szmks[il], label=varns[il], color=cols[il])
     7516        ax2.set_ylabel(titY2, color=cols[il])
     7517        ax2.set_ylim(ymin2,ymax2)
     7518    else:
     7519        ax1.plot(valsA, valsaxisA, styls[il], marker=marks[il], linewidth=wdths[il], \
     7520          markersize=szmks[il], label=varns[il], color=cols[il])
     7521        ax1.set_xlabel(titX, color=cols[il])
     7522        ax1.set_ylabel(titY, color='black')
     7523        ax1.set_xlim(xmin,xmax)
     7524        ax1.set_ylim(ymin,ymax)
     7525        ax2 = ax1.twinx()
     7526        il = il + 1
     7527        ax2.plot(valsB, valsaxisB, styls[il], marker=marks[il], linewidth=wdths[il], \
     7528          markersize=szmks[il], label=varns[il], color=cols[il])
     7529        ax2.set_xlabel(titX2, color=cols[il])
     7530        ax2.set_xlim(xmin2,xmax2)
     7531       
     7532    graphtit = gtitle.replace('_','\_').replace('&','\&')
     7533
     7534    plt.set_title(graphtit)
     7535    plt.legend(loc=gloc)
     7536   
     7537    output_kind(figk, fign, True)
     7538
     7539    return
Note: See TracChangeset for help on using the changeset viewer.