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

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

testing : compare output to reference outputs

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