1 | from netCDF4 import Dataset |
---|
2 | import numpy as np |
---|
3 | from numpy import ma |
---|
4 | import argparse |
---|
5 | import matplotlib.pyplot as plt |
---|
6 | |
---|
7 | parser = argparse.ArgumentParser() |
---|
8 | parser.add_argument("-f" , metavar='file_name' , help="names of input files" , type=str , nargs="+", required=True ) |
---|
9 | parser.add_argument("-v" , metavar='var_name' , help="variable list" , type=str , nargs=1 , required=True ) |
---|
10 | args = parser.parse_args() |
---|
11 | |
---|
12 | # read mesh_mask |
---|
13 | ncid = Dataset('mesh_mask.nc') |
---|
14 | lat2d = ncid.variables['gphit' ][ :,:].squeeze() |
---|
15 | lon2d = ncid.variables['glamt' ][ :,:].squeeze() |
---|
16 | msk = ncid.variables['tmaskutil'][0,:,:].squeeze() |
---|
17 | ncid.close() |
---|
18 | |
---|
19 | plt.figure(figsize=np.array([210,210]) / 25.4) |
---|
20 | |
---|
21 | # read psi.nc |
---|
22 | ncid = Dataset(args.f[0]) |
---|
23 | var2d = ncid.variables[args.v[0]][-1,:,:].squeeze() |
---|
24 | var2dm = ma.masked_where(msk==0.0,var2d) |
---|
25 | # convert in Sv |
---|
26 | var2dm = var2dm / 1e6 |
---|
27 | ncid.close() |
---|
28 | |
---|
29 | # define colorbar |
---|
30 | vlevel=np.arange(0.00,0.36,0.02) |
---|
31 | pcol = plt.contourf(lon2d,lat2d,var2dm,levels=vlevel) |
---|
32 | plt.clf() |
---|
33 | |
---|
34 | # plot contour |
---|
35 | ax = plt.subplot(1, 1, 1) |
---|
36 | ax.contour(lon2d,lat2d,var2dm,levels=vlevel) |
---|
37 | ax.grid() |
---|
38 | ax.set_title('PSI ISOMIP (Sv)') |
---|
39 | ax.set_ylabel('Latitude',fontsize=14) |
---|
40 | ax.set_xlabel('Longitude',fontsize=14) |
---|
41 | |
---|
42 | # plot colorbar |
---|
43 | plt.subplots_adjust(left=0.1,right=0.89, bottom=0.1, top=0.89, wspace=0.1, hspace=0.1) |
---|
44 | cax = plt.axes([0.91, 0.1, 0.02, 0.79]) |
---|
45 | cbar= plt.colorbar(pcol, ticks=vlevel, cax=cax) |
---|
46 | cbar.ax.tick_params(labelsize=14) |
---|
47 | |
---|
48 | # save figure |
---|
49 | plt.savefig('psi.png', format='png', dpi=300) |
---|
50 | |
---|
51 | plt.show() |
---|
52 | |
---|