Changeset 584
- Timestamp:
- Mar 15, 2012, 10:47:35 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UTIL/PYTHON/sparse.py
r573 r584 17 17 from optparse import OptionParser ### to be replaced by argparse 18 18 from os import system,path 19 from scipy.io.idl import readsav20 19 import numpy as np 21 from myplot import separatenames, definesubplot, errormess, defcolorb, fmtvar, polarinterv, simplinterv, define_proj, readdata, makeplotres 22 from mymath import min, max, writeascii 23 import matplotlib as mpl 24 from matplotlib.pyplot import subplot, figure, plot, scatter, colorbar, show, title, close, legend, xlabel, ylabel, axis, hist 25 from matplotlib.cm import get_cmap 26 from mpl_toolkits.mplot3d import Axes3D 27 parser = OptionParser() 28 20 from sparse import sparse 21 22 parser = OptionParser() 23 29 24 ############################# 30 25 ### Options … … 51 46 parser.add_option('--blon', action='store',dest='blon', type="int", default=None, help='reference lon [computed]') 52 47 parser.add_option('-b', '--back', action='store',dest='back', type="string", default=None, help='background image [None]') 53 parser.add_option('-m', '--min', action='store',dest='vmin', type="float", default=None, help='bounding minimum value [min]') 54 parser.add_option('-M', '--max', action='store',dest='vmax', type="float", default=None, help='bounding maximum value [max]') 48 parser.add_option('-m', '--min', action='store',dest='vmin', type="float", default=None, help='bounding minimum value [min]') 49 parser.add_option('-M', '--max', action='store',dest='vmax', type="float", default=None, help='bounding maximum value [max]') 55 50 parser.add_option('--div', action='store',dest='ndiv', type="int", default=10, help='number of divisions in histogram [10]') 56 51 parser.add_option('-S', '--save', action='store',dest='save', type="string", default="gui", help='save mode (gui,png,eps,svg,pdf,txt,html,avi) [gui]') … … 61 56 (opt,args) = parser.parse_args() 62 57 58 sparse(oplot=None,file=opt.file,ftype=opt.ftype,xvar=opt.xvar,yvar=opt.yvar,zvar=opt.zvar,cvar=opt.cvar,xmin=opt.xmin,xmax=opt.xmax,ymin=opt.ymin,ymax=opt.ymax,zmin=opt.zmin,zmax=opt.zmax,cmin=opt.cmin,cmax=opt.cmax,cond=opt.cond,merge=opt.merge,clb=opt.clb,trans=opt.trans,proj=opt.proj,blat=opt.blat,blon=opt.blon,back=opt.back,vmin=opt.vmin,vmax=opt.vmax,ndiv=opt.ndiv,save=opt.save) 59 60 def sparse(oplot=None,file=None,ftype=None,xvar=None,yvar=None,zvar=None,cvar=None,xmin=None,xmax=None,ymin=None,ymax=None,zmin=None,zmax=None,cmin=None,cmax=None,cond=None,merge=False,clb="def",trans=1.,proj=None,blat=None,blon=None,back=None,vmin=None,vmax=None,ndiv=10,save="gui"): 61 62 import sys 63 from os import system,path 64 from scipy.io.idl import readsav 65 import numpy as np 66 from myplot import separatenames, definesubplot, errormess, defcolorb, fmtvar, polarinterv, simplinterv, define_proj, readdata, makeplotres 67 from mymath import min, max, writeascii 68 import matplotlib as mpl 69 from matplotlib.pyplot import subplot, figure, plot, scatter, colorbar, show, title, close, legend, xlabel, ylabel, axis, hist 70 from matplotlib.cm import get_cmap 71 from mpl_toolkits.mplot3d import Axes3D 72 73 63 74 ############################# 64 75 ### Load and check data … … 67 78 ### Load and check data 68 79 69 if opt.file is None:80 if file is None: 70 81 print "You must specify at least a file to process with -f." 71 82 exit() 72 if opt.xvar is None:83 if xvar is None: 73 84 print "You must specify at least a 1st field with -X." 74 85 exit() 75 if opt.proj is not None and opt.yvar is None:86 if proj is not None and yvar is None: 76 87 print "Why did you ask a projection with only one variable?" 77 88 exit() 78 89 79 filename=separatenames( opt.file)80 81 #print 'conditions', opt.cond82 83 print opt.vmax84 print opt.vmin85 86 if opt.cond is None: nslices = 187 else: nslices = len( opt.cond)90 filename=separatenames(file) 91 92 #print 'conditions', cond 93 94 print vmax 95 print vmin 96 97 if cond is None: nslices = 1 98 else: nslices = len(cond) 88 99 numplot = nslices 89 if opt.merge is False: numplot = numplot*len(filename)100 if merge is False: numplot = numplot*len(filename) 90 101 91 102 print ' ' 92 if opt.merge is False:103 if merge is False: 93 104 print nslices, 'condition(s) and', len(filename), 'file(s) without merging ---->', numplot, 'plot(s)' 94 105 else: … … 111 122 112 123 ##### Find file type 113 if opt.ftype is None:124 if ftype is None: 114 125 if filename[k].find('.sav') is not -1: 115 126 all_type[k] ='sav' … … 126 137 errormess('Not suported') 127 138 else: 128 all_type[k] = opt.ftype139 all_type[k] = ftype 129 140 130 141 ##### Read file … … 138 149 all_data[k] = np.loadtxt(filename[k]) 139 150 140 all_x[k] = readdata(all_data,all_type[k],k, opt.xvar)141 if opt.yvar is not None: all_y[k] = readdata(all_data,all_type[k],k,opt.yvar)151 all_x[k] = readdata(all_data,all_type[k],k,xvar) 152 if yvar is not None: all_y[k] = readdata(all_data,all_type[k],k,yvar) 142 153 else: all_y[k] = None 143 if opt.zvar is not None: all_z[k] = readdata(all_data,all_type[k],k,opt.zvar)154 if zvar is not None: all_z[k] = readdata(all_data,all_type[k],k,zvar) 144 155 else: all_z[k] = None 145 if opt.cvar is not None: all_c[k] = readdata(all_data,all_type[k],k,opt.cvar)156 if cvar is not None: all_c[k] = readdata(all_data,all_type[k],k,cvar) 146 157 else: all_c[k] = None 147 158 148 if opt.merge is True and k >=1 :159 if merge is True and k >=1 : 149 160 all_x[0] = np.concatenate((all_x[0],all_x[k])) 150 if opt.yvar is not None: all_y[0] = np.concatenate((all_y[0],all_y[k]))151 if opt.zvar is not None: all_z[0] = np.concatenate((all_z[0],all_z[k]))152 if opt.cvar is not None: all_c[0] = np.concatenate((all_c[0],all_c[k]))161 if yvar is not None: all_y[0] = np.concatenate((all_y[0],all_y[k])) 162 if zvar is not None: all_z[0] = np.concatenate((all_z[0],all_z[k])) 163 if cvar is not None: all_c[0] = np.concatenate((all_c[0],all_c[k])) 153 164 154 165 … … 158 169 159 170 ## Open a figure and set subplots 160 fig = figure() 171 if plot is not None: fig=oplot 172 else: fig = figure() 161 173 subv,subh = definesubplot(numplot,fig) 162 palette = get_cmap(name= opt.clb)174 palette = get_cmap(name=clb) 163 175 164 176 … … 168 180 169 181 ###### Find which data and which file for plot nplot 170 if opt.merge is False:182 if merge is False: 171 183 index_s = ((nplot)//len(filename))%nslices 172 184 index_f = ((nplot-1)//nslices)%len(filename) … … 179 191 ###### Select point under conditions defined in -w option 180 192 index = np.isfinite(all_x[index_f]) 181 if opt.cond is not None:182 zecond = separatenames( opt.cond[index_s])183 #print 'hello', opt.cond[index_s], zecond193 if cond is not None: 194 zecond = separatenames(cond[index_s]) 195 #print 'hello', cond[index_s], zecond 184 196 for i in range(len(zecond)): 185 197 zecondi = zecond[i] … … 187 199 variable = zecondi[0:zecondi.find('>')] 188 200 value = float(zecondi[zecondi.find('>')+1:len(zecondi)]) 189 if opt.merge is True: # ultra moche de reconcatener a chaque fois, mais bon on s'en fout c'est du python201 if merge is True: # ultra moche de reconcatener a chaque fois, mais bon on s'en fout c'est du python 190 202 zedata = [] 191 203 for k in range(len(filename)): … … 198 210 variable = zecondi[0:zecondi.find('<')] 199 211 value = float(zecondi[zecondi.find('<')+1:len(zecondi)]) 200 if opt.merge is True:212 if merge is True: 201 213 zedata = [] 202 214 for k in range(len(filename)): … … 227 239 228 240 ###### Projection 229 if opt.proj is not None: # Nota : NEVER TRY TO DO A MESHGRID ON SPARSE DATA. WAY TOO MUCH POINTS.241 if proj is not None: # Nota : NEVER TRY TO DO A MESHGRID ON SPARSE DATA. WAY TOO MUCH POINTS. 230 242 wlon = [min(all_x[index_f][index]),max(all_x[index_f][index])] 231 243 wlat = [min(all_y[index_f][index]),max(all_y[index_f][index])] 232 m = define_proj( opt.proj,wlon,wlat,back=opt.back,blat=opt.blat,blon=opt.blon)244 m = define_proj(proj,wlon,wlat,back=back,blat=blat,blon=blon) 233 245 x, y = m(all_x[index_f][index],all_y[index_f][index]) 234 246 else: 235 247 x = all_x[index_f][index] 236 if opt.yvar is not None: y = all_y[index_f][index]248 if yvar is not None: y = all_y[index_f][index] 237 249 238 250 ###### Plot: 1D histogram 239 if opt.yvar is None:240 hist(x,bins= opt.ndiv,histtype='step',linewidth=2)241 if opt.save == 'txt':251 if yvar is None: 252 hist(x,bins=ndiv,histtype='step',linewidth=2) 253 if save == 'txt': 242 254 print 'saving file profile'+str(nplot+1)+'.txt' 243 255 writeascii(np.transpose(x),'profile'+str(nplot+1)+'.txt') 244 256 ###### Plot: 2D cloud 245 elif opt.zvar is None and opt.cvar is None :257 elif zvar is None and cvar is None : 246 258 plot(x,y,'.b') 247 if opt.save == 'txt':259 if save == 'txt': 248 260 print 'saving file profile'+str(nplot+1)+'.txt' 249 261 writeascii(np.transpose(np.array([x,y])),'profile'+str(nplot+1)+'.txt') 250 262 ###### Plot: 2D cloud with color 251 elif opt.zvar is None and opt.cvar is not None :252 if opt.save == 'txt':263 elif zvar is None and cvar is not None : 264 if save == 'txt': 253 265 print 'saving file profile'+str(nplot+1)+'.txt' 254 266 writeascii(np.transpose(np.array([x,y,all_c[index_f][index]])),'profile'+str(nplot+1)+'.txt') 255 267 scatter(x,y,c=all_c[index_f][index],\ 256 marker='o', edgecolor='None',cmap = palette, alpha= opt.trans, vmin = opt.vmin,vmax = opt.vmax)268 marker='o', edgecolor='None',cmap = palette, alpha=trans, vmin = vmin,vmax = vmax) 257 269 ###### Plot: 3D cloud 258 elif opt.zvar is not None and opt.cvar is None :270 elif zvar is not None and cvar is None : 259 271 ax = fig.add_subplot(subv,subh,nplot+1, projection='3d') 260 272 ax.plot(x,y,all_z[index_f][index],'.b') … … 263 275 ax = fig.add_subplot(subv,subh,nplot+1, projection='3d') 264 276 ax.scatter(x,y,all_z[index_f][index],c=all_c[index_f][index],\ 265 marker='o', edgecolor='None', cmap = palette, alpha= opt.trans,vmin = opt.vmin,vmax = opt.vmax)277 marker='o', edgecolor='None', cmap = palette, alpha=trans,vmin = vmin,vmax = vmax) 266 278 267 279 ###### Colorbar: http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps?action=AttachFile&do=get&target=colormaps3.png 268 if opt.clb != 'nobar' and all_c[index_f] is not None and all_z[index_f] is None: # pourquoi la colorbar marche pas en 3d?280 if clb != 'nobar' and all_c[index_f] is not None and all_z[index_f] is None: # pourquoi la colorbar marche pas en 3d? 269 281 colorbar( fraction=0.05,pad=0.03,format='%0.2f',\ 270 282 extend='neither',spacing='proportional' ) 271 283 272 284 ###### Plot limits (options --xmin, --xmax, etc ..) 273 if opt.proj is None:274 xlabel( opt.xvar)275 ylabel( opt.yvar)276 if opt.zvar is None :277 if opt.xmin is not None: mpl.pyplot.xlim(xmin=opt.xmin)285 if proj is None: 286 xlabel(xvar) 287 ylabel(yvar) 288 if zvar is None : 289 if xmin is not None: mpl.pyplot.xlim(xmin=xmin) 278 290 else: mpl.pyplot.xlim(xmin=min(all_x[index_f][index])) 279 if opt.xmax is not None: mpl.pyplot.xlim(xmax=opt.xmax)291 if xmax is not None: mpl.pyplot.xlim(xmax=xmax) 280 292 else: mpl.pyplot.xlim(xmax=max(all_x[index_f][index])) 281 if opt.yvar is not None:282 if opt.ymin is not None: mpl.pyplot.ylim(ymin=opt.ymin)293 if yvar is not None: 294 if ymin is not None: mpl.pyplot.ylim(ymin=ymin) 283 295 else: mpl.pyplot.ylim(ymin=min(all_y[index_f][index])) 284 if opt.ymax is not None: mpl.pyplot.ylim(ymax=opt.ymax)296 if ymax is not None: mpl.pyplot.ylim(ymax=ymax) 285 297 else: mpl.pyplot.ylim(ymax=max(all_y[index_f][index])) 286 298 287 299 288 if opt.cond is not None:289 title( opt.cond[index_s])300 if cond is not None: 301 title(cond[index_s]) 290 302 else: 291 303 title('all point selected') 292 304 293 305 zeplot = "output" 294 if opt.save in ['png','eps','svg','pdf']: makeplotres(zeplot,ext=opt.save)#,res=resolution,pad_inches_value=pad_inches_value,disp=False,ext=save) 295 elif opt.save == 'gui': show() 306 if save in ['png','eps','svg','pdf']: makeplotres(zeplot,ext=save)#,res=resolution,pad_inches_value=pad_inches_value,disp=False,ext=save) 307 elif save == 'gui': show() 308 elif save == 'return': return mpl.pyplot 296 309 else: print "INFO: save mode not supported. using gui instead." ; show() 297 310 print ''
Note: See TracChangeset
for help on using the changeset viewer.