Index: sodarplot/trunk/sodarplot/scintec/winddist.py =================================================================== --- (revision ) +++ sodarplot/trunk/sodarplot/scintec/winddist.py (revision 70) @@ -1,0 +1,87 @@ +import os,datetime,glob +import numpy as np +import matplotlib as mpl +mpl.use("Agg") +from windrose import WindroseAxes +from matplotlib import pyplot as plt +import pycdf + +ncDir = '/seacoos/data/nccoos/level1/billymitchell/sodar1' +ncFileGlob = 'billymitchell_sfas_*.nc' + +pngDir = '/seacoos/data/nccoos/level1/billymitchell/sodar1/plots/winddist' +if not os.path.exists(pngDir): + os.mkdir(pngDir) + +pngExt = 'png' +htmlExt = 'html' + +ncFilePattern = os.path.join(ncDir,ncFileGlob) +files = glob.glob(ncFilePattern) +previous = [files[-1]] + files[:-1] +next = files[1:] + [files[0]] +files = zip(previous,files,next) + +first = True +for previous,ncFile,next in files: + print 'Processing',ncFile + ncFileName = os.path.splitext(os.path.basename(ncFile))[0] + year = ncFileName[19:23] + month = ncFileName[24:26] + month = datetime.datetime(int(year),int(month),1).strftime('%B') + if previous: + previous = os.path.splitext(os.path.basename(previous))[0] + previousYear = previous[19:23] + previousMonth = previous[24:26] + previousMonth = datetime.datetime(int(previousYear),int(previousMonth),1).strftime('%B') + previous = previous + os.extsep + htmlExt + if next: + next = os.path.splitext(os.path.basename(next))[0] + nextYear = next[19:23] + nextMonth = next[24:26] + nextMonth = datetime.datetime(int(nextYear),int(nextMonth),1).strftime('%B') + next = next + os.extsep + htmlExt + + nc = pycdf.CDF(ncFile) + + t = nc.var('time')[:] + z = nc.var('z')[:] + u = nc.var('u')[:] + v = nc.var('v')[:] + w = nc.var('w')[:] + + mu = np.ma.masked_invalid(u) + mv = np.ma.masked_invalid(v) + mw = np.ma.masked_invalid(w) + + mumean = mu.mean(axis=0) + mvmean = mv.mean(axis=0) + mwmean = mw.mean(axis=0) + + ucount = mu.count(axis=0) + vcount = mv.count(axis=0) + wcount = mw.count(axis=0) + + mrho = np.sqrt((mu**2) + (mv**2)) + rho = mrho.T.data + mtheta = (180 * np.arctan2(mv, mu)/np.pi) + theta = np.piecewise(mtheta, + (mtheta <= 90, mtheta > 90), + (lambda x: 90 - x, lambda x: 450 - x)) + theta = theta.T + + for x in range(0,rho.shape[0]): + if rho[x].any(): + fig = plt.figure(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='w') + rect = [0.1, 0.1, 0.8, 0.8] + ax = WindroseAxes(fig, rect, axisbg='w') + fig.add_axes(ax) + ax.bar(theta[x], rho[x], normed=True, opening=0.8, edgecolor='white') + l = ax.legend(axespad=-0.10) + plt.setp(l.get_texts(), fontsize=8) + if not os.path.exists(os.path.join(pngDir,ncFileName)): + os.mkdir(os.path.join(pngDir,ncFileName)) + outFile = os.path.join(pngDir,ncFileName,('%dm' % z[x]) + os.extsep + pngExt) + print 'Saving', outFile + fig.savefig(outFile) +