-
Notifications
You must be signed in to change notification settings - Fork 7
/
colorbarImg.py
51 lines (45 loc) · 1.6 KB
/
colorbarImg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
'''
A script that dynamically generates log scaled color bar images from a NetCDF dataset for each step with data.
Requires matplotlib and Scientific python.
Use as follows:
import colorbarImg
colorbarImg.getImages('wrfout.nc','FGRNHFX')
'''
from Scientific.IO import NetCDF
import pylab
import scipy
from numpy import *
from matplotlib import pyplot, mpl
from matplotlib.colors import LogNorm
from matplotlib.colorbar import ColorbarBase
from matplotlib.ticker import LogFormatter
import shutil,os
def getImages(filename,vname):
file=NetCDF.NetCDFFile(filename,'r')
vdata=file.variables[vname]
vsize=vdata.shape[0]
# create empty files subdirectory for output images
try:
shutil.rmtree('colorbarImages')
except:
pass
os.makedirs('colorbarImages')
# go through the whole dataset and generate a color bar image for each step
for i in range(vsize):
varray = vdata[i,:,:,]
data=pylab.flipud(varray)
pylab.imshow(data, norm=LogNorm())
imgNum = 'TimeStep_'+ str(i)
if len(data[data>0])>0:
#make a new figure that contains the colorbar
fig=pylab.figure(figsize=(2,5))
ax1 = fig.add_axes([0.35, 0.03, 0.1, 0.9])
vmin=data[data>0].min()
vmax=data.max()
norm = LogNorm(vmin,vmax)
#make the colorbar in log scale
logFormatter=LogFormatter(10, labelOnlyBase=False)
cb1 = ColorbarBase(ax1,norm=norm,format=logFormatter,spacing='proportional', orientation='vertical')
imgName='colorbarImages/%s.png' %imgNum
fig.savefig(imgName, bbox_inches='tight')