1 | #! /usr/bin/env python |
---|
2 | from ppclass import pp |
---|
3 | from netCDF4 import Dataset |
---|
4 | from numpy import * |
---|
5 | import numpy as np |
---|
6 | import matplotlib.pyplot as mpl |
---|
7 | from matplotlib.cm import get_cmap |
---|
8 | import pylab |
---|
9 | import matplotlib.colors as mcolors |
---|
10 | import colorsys |
---|
11 | |
---|
12 | ############################ |
---|
13 | filename1="diagfi2015_Av.nc" |
---|
14 | var="u" #variable |
---|
15 | tint=["30,32"] #Time must be as written in the input file |
---|
16 | xarea="-180,179" |
---|
17 | |
---|
18 | nc1=Dataset(filename1) |
---|
19 | |
---|
20 | lat=nc1.variables["lat"][:] |
---|
21 | lon=nc1.variables["lon"][:] |
---|
22 | alt=nc1.variables["altitude"][:] |
---|
23 | ############################ |
---|
24 | |
---|
25 | def getvar(filename,var,tint,xarea): |
---|
26 | myvar = pp(file=filename,var=var,t=tint,x=xarea,compute="mean").getf() |
---|
27 | print((shape(myvar))) |
---|
28 | return myvar |
---|
29 | |
---|
30 | def make_colormap(seq): |
---|
31 | """Return a LinearSegmentedColormap |
---|
32 | seq: a sequence of floats and RGB-tuples. The floats should be increasing |
---|
33 | and in the interval (0,1). |
---|
34 | """ |
---|
35 | |
---|
36 | seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3] |
---|
37 | print(seq) |
---|
38 | cdict = {'red': [], 'green': [], 'blue': []} |
---|
39 | for i, item in enumerate(seq): |
---|
40 | if isinstance(item, float): |
---|
41 | r1, g1, b1 = seq[i - 1] |
---|
42 | r2, g2, b2 = seq[i + 1] |
---|
43 | cdict['red'].append([item, r1, r2]) |
---|
44 | cdict['green'].append([item, g1, g2]) |
---|
45 | cdict['blue'].append([item, b1, b2]) |
---|
46 | print(cdict) |
---|
47 | return mcolors.LinearSegmentedColormap('CustomMap', cdict) |
---|
48 | |
---|
49 | def diverge_map(high, low): |
---|
50 | ''' |
---|
51 | low and high are colors that will be used for the two |
---|
52 | ends of the spectrum. they can be either color strings |
---|
53 | or rgb color tuples |
---|
54 | ''' |
---|
55 | c = mcolors.ColorConverter().to_rgb |
---|
56 | if isinstance(low, str): low = c(low) #si low=string (color) |
---|
57 | if isinstance(high, str): high = c(high) |
---|
58 | print(high) |
---|
59 | return make_colormap([low, c('white'), 0.55, c('white'),0.65, c('white'), high]) |
---|
60 | |
---|
61 | |
---|
62 | hh=(255,99,71) |
---|
63 | hh=(hh[0]/255,hh[1]/255,hh[2]/255) |
---|
64 | print(hh) |
---|
65 | h=hh #(0.565, 0.392, 0.173) |
---|
66 | l=(0.094, 0.310, 0.635) |
---|
67 | rvb1=diverge_map(h,l) |
---|
68 | |
---|
69 | myvar=getvar(filename1,var,tint,xarea) |
---|
70 | |
---|
71 | mpl.figure(figsize=(20, 10)) |
---|
72 | |
---|
73 | font=26 |
---|
74 | |
---|
75 | #pal=rvb1 #get_cmap(name="RdYlBu_r") |
---|
76 | #pal=get_cmap(name="Spectral_r") |
---|
77 | pal=get_cmap(name="rainbow") |
---|
78 | lev=np.linspace(-20,10,31) |
---|
79 | |
---|
80 | xticks=[-90,-60,-30,0,30,60,90] |
---|
81 | #yticks=np.linspace(0,240,9) |
---|
82 | alt=alt/1000. |
---|
83 | |
---|
84 | CF=mpl.contourf(lat,alt,myvar,lev,cmap=pal,extend='both') |
---|
85 | cbar=mpl.colorbar(CF,shrink=1, format="%1.2f") |
---|
86 | #cbar.ax.set_title("[K]",y=1.04,fontsize=font) |
---|
87 | for t in cbar.ax.get_yticklabels(): |
---|
88 | t.set_fontsize(font) |
---|
89 | |
---|
90 | vect=lev |
---|
91 | CS=mpl.contour(lat,alt,myvar,vect,colors='k',linewidths=0.5) |
---|
92 | #### inline=1 : values over the line |
---|
93 | mpl.clabel(CS, inline=1, fontsize=20, fmt='%1.0f',inline_spacing=1) |
---|
94 | |
---|
95 | #mpl.title('Latitude ='+str(tintstr[i]),fontsize=font) |
---|
96 | mpl.ylabel('Altitude (km)',labelpad=10,fontsize=font) |
---|
97 | mpl.xlabel('Latitude (deg)',labelpad=10, fontsize=font) |
---|
98 | mpl.xticks(xticks,fontsize=font) |
---|
99 | #mpl.xticks(fontsize=font) |
---|
100 | #mpl.yticks(yticks,fontsize=font) |
---|
101 | mpl.yticks(fontsize=font) |
---|
102 | pylab.ylim([-4,10]) |
---|
103 | |
---|
104 | mpl.savefig('meanzonalwind_zoom.eps',dpi=200) |
---|
105 | mpl.savefig('meanzonalwind_zoom.png',dpi=200) |
---|
106 | mpl.show() |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|