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


Ignore:
Timestamp:
Oct 25, 2016, 7:56:03 PM (8 years ago)
Author:
lfita
Message:

Generlizing, improving and adding to the web `2D_shadow_line'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/drawing.py

    r1219 r1231  
    2121## e.g. # drawing.py -o draw_2D_shad_cont -f wrfout_d01_2001-11-11_00:00:00 -S 'huss,tas:west_east|-1,south_north|-1,Time|2:Time|2:XLONG:XLAT:auto:Blues,auto,auto:fixc,r:%3g:Srange,Srange:260,300,9:huss|&|tas|at|2001-11-11|02|UTC:png:None:cyl,c:True' -v Q2,T2
    2222## e.g. # drawing.py -o draw_2D_shad_cont_time -f wrfout_d01_2001-11-11_00:00:00 -S 'hfls,tas;west_east|-1,south_north|27,Time|-1;south_north|27;XLONG;WRFtime;auto;BuPu,auto,auto;fixc,y;%3g;Srange,Srange;260,300,9;huss|&|tas|evolution|at|south_north=27;png;None;WRFtime|hours!since!1949-12-01|exct,1,h|$%d^{%H}$|date!($[DD]^{[HH]}$);True' -v LH,T2
     23## e.g. # drawing.py -o draw_2D_shad_line -f wrfout_d01_2001-11-11_00:00:00,wrfout_d01_2001-11-11_00:00:00 -S 'hus,hgt:west_east|-1,south_north|96,Time|2,bottom_top|-1:XLONG:ZNU:auto:rainbow,auto,horizontal:Srange,Srange:k,0.,4000.,auto,auto,auto,45.:vert.|sec.|hus|at|y=96|on|2001-11-11|02|UTC:png:flip@y:None:True' -v QVAPOR,HGT
    2324## e.g. # drawing.py -f ~/etudes/domains/MEDCORDEX/geo_em.d01.nc -o draw_2D_shad_cont -S 'height,landmask:Time|0:Time|0:XLONG_M:XLAT_M:terrain:fixc,k:None:0.,3000.:0,1,10:MEDCORDEX height & landmask:pdf:False:lcc,i' -v HGT_M,LANDMASK
    2425## e.g. # drawing.py -o draw_2D_shad_line -f 'mean_dtcon-pluc-pres_lat.nc,mean_dtcon-pluc-pres_lat.nc' -S 'dtcon,prc:bottom_top|-1,south_north|-1:latmean:presmean:seismic,k:-5.,5.:monthly|dtcon|&|prc:pdf:flip@y:None:True' -v 'dtconmean,prcmean'
     
    774775    """ plotting two fields, one with shading and the other with contour lines being
    775776    one of the dimensions of time characteristics
    776     draw_2D_shad_cont(ncfile, values, varn)
     777    draw_2D_shad_cont_time(ncfile, values, varn)
    777778      ncfile= [ncfilevars],[ncfilevarc] files to use (one value, same file)
    778779      values=[vnamefs];[dimvals];[dimvalc];[dimxvn];[dimyvn];[dimxyf];[colorbarvals];[ckind];[clabfmt];[sminv],[smaxv];[sminc],[smaxv],[Nlev];[figt];[kindfig];[reverse];[timevals];[close]
     
    10611062    draw_2D_shad_line(ncfile, values, varn)
    10621063      ncfile= [ncfiles],[ncfilel] file to use for the shading and for the line
    1063       values=[vnamefs],[vnamefl]:[dimvals]:[dimxvn]:[dimyvn]:[colorbar],[colline]:[sminv],[smaxv]:[figt]:
     1064      values=[vnamefs],[vnamefl]:[dimvals]:[dimxvn]:[dimyvn]:[dimxyfmt]:[colorbarvals]:[smin/axv]:[linevalues]:[figt]:
    10641065       [kindfig]:[reverse]:[mapv]:[close]
    10651066        [vnamefs]: Name in the figure of the variable to be shaded
     
    10681069          variable a given value is required (-1, all the length)
    10691070        [dimx/yvn]: name of the variables with the values of the final dimensions (x,y)
    1070         [colorbar]: name of the color bar
    1071         [colline]: name of the color for the line
     1071        [dimxyfmt]=[dxs],[dxf],[Ndx],[ordx],[dys],[dyf],[Ndy],[ordx]: format of the values at each axis (or 'auto')
     1072          [dxs]: style of x-axis ('auto' for 'pretty')
     1073            'Nfix', values computed at even 'Ndx'
     1074            'Vfix', values computed at even 'Ndx' increments
     1075            'pretty', values computed following aprox. 'Ndx' at 'pretty' intervals (2.,2.5,4,5,10)
     1076          [dxf]: format of the labels at the x-axis ('auto' for '%5g')
     1077          [Ndx]: Number of ticks at the x-axis ('auto' for 5)
     1078          [ordx]: angle of orientation of ticks at the x-axis ('auto' for horizontal)
     1079          [dys]: style of y-axis ('auto' for 'pretty')
     1080          [dyf]: format of the labels at the y-axis ('auto' for '%5g')
     1081          [Ndy]: Number of ticks at the y-axis ('auto' for 5)
     1082          [ordy]: angle of orientation of ticks at the y-axis ('auto' for horizontal)
     1083        [colorbarvals]=[colbarn],[fmtcolorbar],[orientation]
     1084          [colorbarn]: name of the color bar
     1085          [fmtcolorbar]: format of the numbers in the color bar 'C'-like ('auto' for %6g)
     1086          [orientation]: orientation of the colorbar ('vertical' (default, by 'auto'), 'horizontal')
     1087        [smin/axv]: minimum and maximum value for the shading or:
     1088          'Srange': for full range
     1089          'Saroundmean@val': for mean-xtrm,mean+xtrm where xtrm = np.min(mean-min@val,max@val-mean)
     1090          'Saroundminmax@val': for min*val,max*val
     1091          'Saroundpercentile@val': for median-xtrm,median+xtrm where xtrm = np.min(median-percentile_(val),
     1092            percentile_(100-val)-median)
     1093          'Smean@val': for -xtrm,xtrm where xtrm = np.min(mean-min*@val,max*@val-mean)
     1094          'Smedian@val': for -xtrm,xtrm where xtrm = np.min(median-min@val,max@val-median)
     1095          'Spercentile@val': for -xtrm,xtrm where xtrm = np.min(median-percentile_(val),
     1096             percentile_(100-val)-median)
     1097        [linevalues]=[colline],[sminl],[smaxl],[dls],[dlf],[Ndl],[ordl]
     1098          [colline]: name of the color for the line
     1099          [smin/axv]: minimum and maximum value for the line or:
     1100            'Srange': for full range
     1101             'Saroundmean@val': for mean-xtrm,mean+xtrm where xtrm = np.min(mean-min@val,max@val-mean)
     1102            'Saroundminmax@val': for min*val,max*val
     1103            'Saroundpercentile@val': for median-xtrm,median+xtrm where xtrm = np.min(median-percentile_(val),
     1104              percentile_(100-val)-median)
     1105            'Smean@val': for -xtrm,xtrm where xtrm = np.min(mean-min*@val,max*@val-mean)
     1106            'Smedian@val': for -xtrm,xtrm where xtrm = np.min(median-min@val,max@val-median)
     1107            'Spercentile@val': for -xtrm,xtrm where xtrm = np.min(median-percentile_(val),
     1108               percentile_(100-val)-median)
     1109          [dls]: style of line-axis ('auto' for 'pretty')
     1110          [dlf]: format of the labels at the line-axis ('auto' for '%5g')
     1111          [Ndl]: Number of ticks at the line-axis ('auto' for 5)
     1112          [ordl]: angle of orientation of ticks at the line-axis ('auto' for horizontal)
    10721113        [smin/axv]: minimum and maximum value for the shading
    10731114        [figt]: title of the figure ('|' for spaces)
     
    10871128            * 'h', high
    10881129            * 'f', full
     1130        [close]: Whether figure should be finished or not
    10891131      valules= 'rh:z|-1,x|-1:z|-1,x|-1:lat:pressure:BuPu:0.,100.:rh:pdf:flip@y:None'
    10901132      varn= [varsn],[varnl] name of the variable to plot with shading and with line
     
    10981140
    10991141    farguments = '[vnamefs],[vnamefl]:[dimvals]:[dimxvn]:[dimyvn]:' +                \
    1100       '[colorbar],[colline]:[sminv],[smaxv]:[figt]:[kindfig]:[reverse]:' +           \
    1101       '[mapv]:[close]'
     1142      '[dimxyfmt]:[colorbarvals]:[smin/axv]:[linevalues]:[figt]:[kindfig]:' +        \
     1143      '[reverse]:[mapv]:[close]'
    11021144    drw.check_arguments(fname,values,farguments,':')
    11031145
     
    11061148    vdimxn = values.split(':')[2]
    11071149    vdimyn = values.split(':')[3]
    1108     colbarn = values.split(':')[4].split(',')[0]
    1109     shadminmax = values.split(':')[5]
    1110     figtitle = values.split(':')[6].replace('|',' ')
    1111     figkind = values.split(':')[7]
    1112     revals = values.split(':')[8]
    1113     mapvalue = values.split(':')[9]
    1114 #    varn = values.split(':')[10]
     1150    dimxyf = values.split(':')[4]
     1151    colorbarvals = values.split(':')[5]
     1152    shadminmax = values.split(':')[6]
     1153    linevalues = values.split(':')[7]
     1154    figtitle = values.split(':')[8].replace('|',' ')
     1155    figkind = values.split(':')[9]
     1156    revals = values.split(':')[10]
     1157    mapvalue = values.split(':')[11]
     1158    close = gen.Str_Bool(values.split(':')[12])
    11151159
    11161160    ncfiles = ncfile.split(',')[0]
     
    11371181
    11381182# Dimensions names
    1139 ##    print fname + ' obj dimnames: ', objvars.dimensions, dimvals, len(dimvals.split(','))
    1140 ##    dimnamesv = []
    1141 ##    for idd in range(len(objvars.dimensions)):
    1142 ##        cutdim = False
    1143 ##        for idc in range(len(dimvals.split(','))):
    1144 ##            dimcutn = dimvals.split(',')[idc].split(':')[0]
    1145 ##            print objvars.dimensions[idd], dimcutn
    1146 ##            if objvars.dimensions[idd] == dimcutn:
    1147 ##                cutdim = True
    1148 ##                break
    1149 ##        if not cutdim: dimnamesv.append(objvars.dimensions[idd])
    11501183    dimnamesv = [vdimxn, vdimyn]
    11511184
     
    11651198    objdimx = objsf.variables[vdimxn]
    11661199    objdimy = objsf.variables[vdimyn]
    1167     odimxu = objdimx.getncattr('units')
    1168     odimyu = objdimy.getncattr('units')
    1169 
    1170     if len(objdimx.shape) <= 2:
    1171 #        odimxv = objdimx[valshad.shape]
    1172 #        odimyv = objdimy[valshad.shape]
    1173         odimxv = objdimx[:]
    1174         odimyv = objdimy[:]
    1175 
    1176     elif len(objdimx.shape) == 3:
    1177 #        dimcut = [0, slice(0,valshad.shape[0]), slice(0,valshad.shape[1])]
    1178 #        odimxv = objdimx[tuple(dimcut)]
    1179 #        odimyv = objdimy[tuple(dimcut)]
    1180         odimxv = objdimx[0,:]
    1181         odimyv = objdimy[0,:]
    1182     else:
    1183         print errormsg
    1184         print '  ' + fname + ': shape of dimension variable:', objdimx.shape,        \
    1185           ' not ready!!'
    1186         quit(-1)
    1187 
    1188     shading_nx = np.zeros((2), dtype=np.float)
    1189     shading_nx[0] = np.float(shadminmax.split(',')[0])
    1190     shading_nx[1] = np.float(shadminmax.split(',')[1])
    1191 
    1192     if mapvalue == 'None': mapvalue = None
     1200    if drw.searchInlist(objdimx.ncattrs(),'units'):
     1201        odimxu = objdimx.getncattr('units')
     1202    else:
     1203        print warnmsg
     1204        print '  ' + fname + ": variable dimension '" + vdimxn + "' without units!!"
     1205        odimxu = '-'
     1206
     1207    if drw.searchInlist(objdimy.ncattrs(),'units'):
     1208        odimyu = objdimy.getncattr('units')
     1209    else:
     1210        print warnmsg
     1211        print '  ' + fname + ": variable dimension '" + vdimyn + "' without units!!"
     1212        odimyu = '-'
     1213
     1214    odimxv, odimyv = drw.dxdy_lonlatDIMS(objdimx[:], objdimy[:], objdimx.dimensions,     \
     1215      objdimy.dimensions, dimvals.replace(':','|').split(','))
     1216
     1217    shading_nx = []
     1218    if shadminmax.split(',')[0][0:1] != 'S':
     1219            shading_nx.append(np.float(shadminmax.split(',')[0]))
     1220    else:
     1221        shading_nx.append(shadminmax.split(',')[0])
     1222
     1223    if shadminmax.split(',')[1][0:1] != 'S':
     1224        shading_nx.append(np.float(shadminmax.split(',')[1]))
     1225    else:
     1226        shading_nx.append(shadminmax.split(',')[1])
    11931227
    11941228# line plot
    11951229##
     1230    linearg = linevalues.split(',')
     1231    if linevalues.split(',')[1][0:1] != 'S':
     1232        linearg[1] = np.float(linevalues.split(',')[1])
     1233    if linevalues.split(',')[2][0:1] != 'S':
     1234        linearg[2] = np.float(linevalues.split(',')[2])
     1235    if linearg[3] == 'auto': linearg[3] = 'pretty'
     1236    if linearg[4] == 'auto': linearg[4] = '5g'
     1237    if linearg[5] == 'auto': linearg[5] = 5
     1238    if linearg[6] == 'auto': linearg[6] = 0.
     1239
    11961240    ncfilel = ncfile.split(',')[1]
    11971241    vnamelfig = values.split(':')[0].split(',')[1]
    11981242    varnl = varn.split(',')[1]
    1199     colline = values.split(':')[4].split(',')[1]
    1200 
     1243
     1244    if not os.path.isfile(ncfilel):
     1245        print errormsg
     1246        print '  ' + fname + ': file for line "' + ncfilel + '" does not exist !!'
     1247        quit(-1)
    12011248    objlf = NetCDFFile(ncfilel,'r')
     1249
     1250    if  not objlf.variables.has_key(varnl):
     1251        print errormsg
     1252        print '  ' + fname + ': line file "' + ncfilel +                            \
     1253          '" does not have variable "' +  varnl + '" !!'
     1254        quit(-1)
    12021255    objlvar = objlf.variables[varnl]
    1203 
    1204     linevals = objlvar[:]
     1256    linevals, dimsline = drw.slice_variable(objlvar, dimvals.replace(',','|'))
    12051257    varlunits = objlvar.units
    12061258
     1259    if mapvalue == 'None': mapvalue = None
     1260
     1261    colbarn, fmtcolbar, colbaror = drw.colorbar_vals(colorbarvals,',')
     1262    colormapv = [colbarn, fmtcolbar, colbaror]
     1263
     1264    xstyl, xaxf, Nxax, xaxor, ystyl, yaxf, Nyax, yaxor = drw.format_axes(dimxyf,',')
     1265    xaxis = [xstyl, xaxf, Nxax, xaxor]
     1266    yaxis = [ystyl, yaxf, Nyax, yaxor]
     1267
     1268    if revals == 'None':
     1269        revals = None
     1270
    12071271    drw.plot_2D_shadow_line(valshad, linevals, vnamesfig, vnamelfig, odimxv, odimyv, \
    1208       odimxu, odimyu, dimnamesv, colbarn, colline, shading_nx, varunits, varlunits,  \
    1209       figtitle, figkind, revals, mapvalue, True)
     1272      odimxu, odimyu, dimnamesv, xaxis, yaxis, colormapv, linearg, shading_nx,       \
     1273      varunits, varlunits, figtitle, figkind, revals, mapvalue, close)
    12101274
    12111275    objsf.close()
Note: See TracChangeset for help on using the changeset viewer.