1 | from netCDF4 import Dataset |
---|
2 | import numpy as np |
---|
3 | import argparse |
---|
4 | import matplotlib.pyplot as plt |
---|
5 | |
---|
6 | # read argument |
---|
7 | parser = argparse.ArgumentParser() |
---|
8 | parser.add_argument("-f" , metavar='file_name' , help="names of input files" , type=str , nargs=1 , 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 | vx2d = ncid.variables['gphit' ][0,:,0].squeeze() |
---|
15 | vx2dv = ncid.variables['gphiv' ][0,:,0].squeeze() |
---|
16 | y2d = ncid.variables['gdepw_0'][0,:,:,1].squeeze()*-1 |
---|
17 | y2dt = ncid.variables['gdept_0'][0,:,:,1].squeeze()*-1 |
---|
18 | msk = ncid.variables['tmask' ][0,:,:,1].squeeze() |
---|
19 | ncid.close() |
---|
20 | |
---|
21 | # build x 2d array |
---|
22 | x2d=y2d*0.0 |
---|
23 | x2dv=y2d*0.0 |
---|
24 | for jk in range(0,y2d.shape[0]): |
---|
25 | x2d[jk,:]=vx2d[:] |
---|
26 | x2dv[jk,:]=vx2d[:] |
---|
27 | |
---|
28 | plt.figure(figsize=np.array([210,210]) / 25.4) |
---|
29 | |
---|
30 | # read data and mask it |
---|
31 | ncid = Dataset(args.f[0]) |
---|
32 | var2d = ncid.variables[args.v[0]][-1,:,:,:].squeeze() |
---|
33 | var2dm = var2d[:,:] |
---|
34 | var2dm[msk==0] = -1 |
---|
35 | ncid.close() |
---|
36 | |
---|
37 | # define colorbar |
---|
38 | vlevel=np.arange(0,0.13,0.01) |
---|
39 | pcol = plt.contourf(x2d,y2d,var2dm,levels=vlevel) |
---|
40 | plt.clf() |
---|
41 | |
---|
42 | # plot contour |
---|
43 | ax = plt.subplot(1, 1, 1) |
---|
44 | ax.contour(x2dv,y2dt,var2dm,levels=vlevel) |
---|
45 | ax.grid() |
---|
46 | ax.set_title('MOC ISOMIP (Sv)') |
---|
47 | ax.set_ylabel('Depth (m)',fontsize=14) |
---|
48 | ax.set_xlabel('Latitude',fontsize=14) |
---|
49 | |
---|
50 | # plot colorbar |
---|
51 | plt.subplots_adjust(left=0.1,right=0.89, bottom=0.1, top=0.89, wspace=0.1, hspace=0.1) |
---|
52 | cax = plt.axes([0.91, 0.1, 0.02, 0.79]) |
---|
53 | cbar= plt.colorbar(pcol, ticks=vlevel, cax=cax) |
---|
54 | cbar.ax.tick_params(labelsize=14) |
---|
55 | |
---|
56 | # save figure |
---|
57 | plt.savefig('moc.png', format='png', dpi=300) |
---|
58 | |
---|
59 | plt.show() |
---|
60 | |
---|