source: configs/testing/python/common.py @ 740

Last change on this file since 740 was 740, checked in by dubos, 6 years ago

testing : small improvements

File size: 4.6 KB
Line 
1import numpy as np
2import netCDF4 as cdf
3# select non-interactive backend, cf http://stackoverflow.com/questions/4931376/generating-matplotlib-graphs-without-a-running-x-server
4import matplotlib
5matplotlib.use('Agg') 
6import matplotlib.pyplot as plt
7
8import matplotlib.style
9import matplotlib as mpl
10mpl.style.use('classic')
11
12def getdims(nc, *names): return [len(nc.dimensions[name]) for name in names]
13def getvars(nc, *names): return [nc.variables[name] for name in names]
14
15def axis_longitude():
16    plt.xlim((0,360))
17    plt.xlabel('longitude (degrees)')
18    plt.xticks(np.arange(0,361,30))
19
20def axis_latitude():
21    plt.ylim((-90,90))
22    plt.ylabel('latitude (degrees)')
23    plt.yticks(np.arange(-90,91,30))
24
25def slice_lon(nlon,llm,lon,Phi):
26    lon2, z = np.zeros((llm,nlon)), np.zeros((llm,nlon))
27    for lev in range(llm):
28        # average from interfaces to full levels
29        z[lev,:] = (.5/g)*(Phi[lev,:]+Phi[lev+1,:]) 
30        lon2[lev,:] = lon[:]
31    return lon2, z
32
33#--------------------------- DCMIP21 ---------------------------
34
35def post_DCMIP21():
36    def plot_var(nlon,nlat,llm, lon,Phi,var,varname):
37        # vertical slice at final time
38        print 'Reading data ...'
39        var, Phi = var[-1,:,nlat/2,:], Phi[-1,:,nlat/2,:]
40        print '... done.'
41        lon2, z = slice_lon(nlon,llm,lon,Phi)
42        plt.figure(figsize=(12,6))
43        plt.contourf(lon2,z,var)
44        plt.colorbar() 
45        plt.title('%s at final time'%varname)
46        axis_longitude()
47        plt.ylabel('z (m)')
48        #        plt.yticks(np.arange(0, 10001, 1000))
49        plt.savefig('%s.png'%varname)
50        plt.close()
51    lon, lat, Omega,T,u,Phi = getvars(nc, 'lon','lat','OMEGA', 'T', 'U', 'PHI')
52    plot_var(nlon,nlat,llm, lon,Phi,Omega,'Omega')
53    plot_var(nlon,nlat,llm, lon,Phi,u,'u')
54    plot_var(nlon,nlat,llm, lon,Phi,T,'T')
55
56#--------------------------- DCMIP31 ---------------------------
57
58def post_DCMIP31():
59   
60    def plot_T850(lon,lat,T850):  # T850 at final time
61        print 'Reading data ...'
62        lon, lat, T850 = lon[:], lat[:], T850[-1, :, :]
63        print '... done.'
64        plt.figure(figsize=(12,6))
65        plt.contourf(lon,lat,T850)
66        plt.colorbar() 
67        plt.title('T850')
68        axis_longitude()
69        axis_latitude()
70        plt.savefig('T850.png')
71        plt.close()
72       
73    def plot_dT(nlon,nlat,llm, lon,T_ref,T,p,Phi): # perturbation temp, final time
74        # vertical slice at final time
75        print 'Reading data ...'
76        T_ref,T,p,Phi = [ x[-1,:,nlat/2,:] for x in T_ref,T,p,Phi]
77        print '... done.'
78        N, Teq, peq = 0.01, 300., 1e5
79        N2, g2 = N*N, g*g
80        G = g2/(N2*Cpd)
81
82        lon2, z = slice_lon(nlon,llm,lon,Phi)
83        theta = T*((peq/p)**kappa)
84        Thetab = Teq*np.exp(N2*z/g)
85        plt.figure(figsize=(12,6))
86        plt.contourf(lon2,z,theta-Thetab, levels=np.arange(-0.12,0.12,0.02) )
87        plt.colorbar() 
88        plt.title('$\\Theta\'$')
89        axis_longitude()
90        plt.ylabel('z (m)')
91        plt.yticks(np.arange(0, 10001, 1000))
92        plt.savefig('dT.png')
93        plt.close()
94
95        plt.figure(figsize=(12,6))
96        plt.contourf(lon2,z,T-T_ref)
97        plt.colorbar() 
98        plt.title('$T-T_{ref}$')
99        axis_longitude()
100        plt.ylabel('z (m)')
101        plt.yticks(np.arange(0, 10001, 1000))
102        plt.savefig('dT_ref.png')
103        plt.close()
104
105    lon, lat, T850, T, Phi, p = getvars(nc, 'lon','lat','T850', 'T', 'PHI','P')
106    T_ref, = getvars(nc_ref, 'T')
107    plot_dT(nlon,nlat,llm, lon,T_ref,T,p,Phi)
108#    plot_dT_ref(nlon,nlat,llm, lon,T_ref,T,p,Phi)
109    plot_T850(lon,lat,T850)
110
111#--------------------------- DCMIP41 ---------------------------
112
113def post_DCMIP41():
114    def plot_T850(day, lon,lat,T850):  # T850 at final time
115        print 'Reading data ...'
116        lon, lat, T850 = lon[:], lat[:], T850[day-1, :, :]
117        print '... done.'
118        plt.figure(figsize=(12,5))
119        plt.contourf(lon,lat,T850)
120        plt.colorbar() 
121        plt.title('T850 at day %d'%day)
122        axis_longitude()
123        axis_latitude()
124        plt.savefig('T850_day%02d.png'%day)
125
126    lon, lat, T850, T, Phi = getvars(nc, 'lon','lat','T850', 'T', 'PHI')   
127    plot_T850(7,lon,lat,T850)
128    plot_T850(10,lon,lat,T850)
129    plot_T850(30,lon,lat,T850)
130
131#------------------------ Held & Suarez ----------------------
132
133post_held_suarez = post_DCMIP41
134   
135#--------------------------- MAIN ----------------------------
136
137filename = 'output_dcmip2016_regular.nc'
138nc = cdf.Dataset('netcdf/%s'%filename, "r")
139nc_ref = cdf.Dataset('netcdf_ref/%s'%filename, "r")
140
141llm, nlon, nlat, ntime = getdims(nc, 'lev','lon','lat','time_counter')
142Cpd, kappa, g = 1004.5, 0.2857143, 9.80616
143
144# Now call a routine post_XXX()
Note: See TracBrowser for help on using the repository browser.