1 | #################################################################################### |
---|
2 | ### Python script to output the stratification data from the "startpem.nc" files ### |
---|
3 | #################################################################################### |
---|
4 | |
---|
5 | import netCDF4 as nc |
---|
6 | import numpy as np |
---|
7 | import matplotlib.pyplot as plt |
---|
8 | import sys |
---|
9 | import os.path |
---|
10 | |
---|
11 | ############################## |
---|
12 | ### Parameters to fill in |
---|
13 | filename = 'startpem9.nc' # File name |
---|
14 | igrid = 1 # Grid point |
---|
15 | islope = 1 # Slope number |
---|
16 | istr = 4 # Stratum number |
---|
17 | ############################## |
---|
18 | |
---|
19 | ### Open the NetCDF file |
---|
20 | if os.path.isfile(filename): |
---|
21 | nc_file = nc.Dataset(filename,'r') |
---|
22 | else: |
---|
23 | sys.exit('The file \"' + filename + '\" does not exist!') |
---|
24 | |
---|
25 | ### Get the dimensions |
---|
26 | Time = len(nc_file.dimensions['Time']) |
---|
27 | ngrid = len(nc_file.dimensions['physical_points']) |
---|
28 | nslope = len(nc_file.dimensions['nslope']) |
---|
29 | nb_str_max = len(nc_file.dimensions['nb_str_max']) |
---|
30 | if igrid > ngrid or igrid < 1: |
---|
31 | sys.exit('Asked grid point is not possible!') |
---|
32 | if islope > nslope or islope < 1: |
---|
33 | sys.exit('Asked slope number is not possible!') |
---|
34 | if istr > nb_str_max or istr < 1: |
---|
35 | sys.exit('Asked stratum number is not possible!') |
---|
36 | |
---|
37 | ### Get the stratification properties |
---|
38 | stratif_thickness = [] |
---|
39 | stratif_top_elevation = [] |
---|
40 | stratif_co2ice_volfrac = [] |
---|
41 | stratif_h2oice_volfrac = [] |
---|
42 | stratif_dust_volfrac = [] |
---|
43 | stratif_air_volfrac = [] |
---|
44 | for i in range(1,nslope + 1): |
---|
45 | stratif_thickness.append(nc_file.variables['stratif_slope' + str(i).zfill(2) + '_thickness'][:]) |
---|
46 | stratif_top_elevation.append(nc_file.variables['stratif_slope' + str(i).zfill(2) + '_top_elevation'][:]) |
---|
47 | stratif_co2ice_volfrac.append(nc_file.variables['stratif_slope' + str(i).zfill(2) + '_co2ice_volfrac'][:]) |
---|
48 | stratif_h2oice_volfrac.append(nc_file.variables['stratif_slope' + str(i).zfill(2) + '_h2oice_volfrac'][:]) |
---|
49 | stratif_dust_volfrac.append(nc_file.variables['stratif_slope' + str(i).zfill(2) + '_dust_volfrac'][:]) |
---|
50 | stratif_air_volfrac.append(nc_file.variables['stratif_slope' + str(i).zfill(2) + '_air_volfrac'][:]) |
---|
51 | |
---|
52 | ### Display the data |
---|
53 | igrid = igrid - 1 |
---|
54 | islope = islope - 1 |
---|
55 | labels = 'CO2 ice', 'H2O ice', 'Dust', 'Air' |
---|
56 | contents = np.zeros([4,len(stratif_top_elevation[islope][0,:,igrid]) + 1]) |
---|
57 | height = np.zeros(len(stratif_top_elevation[islope][0,:,igrid]) + 1) |
---|
58 | height[0] = stratif_top_elevation[islope][0,0,:] - stratif_thickness[islope][0,0,:] |
---|
59 | height[1:] = stratif_top_elevation[islope][0,:,igrid] |
---|
60 | for i in range(len(stratif_top_elevation[islope][0,:,igrid])): |
---|
61 | contents[0,1 + i] = stratif_co2ice_volfrac[islope][0,i,igrid] |
---|
62 | contents[1,1 + i] = stratif_h2oice_volfrac[islope][0,i,igrid] |
---|
63 | contents[2,1 + i] = stratif_dust_volfrac[islope][0,i,igrid] |
---|
64 | contents[3,1 + i] = stratif_air_volfrac[islope][0,i,igrid] |
---|
65 | contents[:,0] = contents[:,1] |
---|
66 | |
---|
67 | # Simple subplots for a layering |
---|
68 | fig, (ax1, ax2, ax3, ax4) = plt.subplots(1,4,layout = 'constrained',sharey = True) |
---|
69 | fig.suptitle('Simple content profiles for the layering') |
---|
70 | ax1.step(contents[0,:],height,where = 'post') |
---|
71 | ax2.step(contents[1,:],height,where = 'post') |
---|
72 | ax3.step(contents[2,:],height,where = 'post') |
---|
73 | ax4.step(contents[3,:],height,where = 'post') |
---|
74 | ax1.set_ylabel('Elevation [m]') |
---|
75 | ax1.set_xlabel('Volume fraction [m3/m3]') |
---|
76 | ax2.set_xlabel('Volume fraction [m3/m3]') |
---|
77 | ax3.set_xlabel('Volume fraction [m3/m3]') |
---|
78 | ax4.set_xlabel('Volume fraction [m3/m3]') |
---|
79 | ax1.set_title(labels[0]) |
---|
80 | ax2.set_title(labels[1]) |
---|
81 | ax3.set_title(labels[2]) |
---|
82 | ax4.set_title(labels[3]) |
---|
83 | plt.savefig('layering_simpleprofiles.png') |
---|
84 | |
---|
85 | # Stackplot for a layering |
---|
86 | fig, ax = plt.subplots() |
---|
87 | ax.fill_betweenx(height,0,contents[0,:],label = labels[0],color = 'r',step = 'pre') |
---|
88 | ax.fill_betweenx(height,contents[0,:],sum(contents[0:2,:]),label = labels[1],color = 'b',step = 'pre') |
---|
89 | ax.fill_betweenx(height,sum(contents[0:2,:]),sum(contents[0:3,:]),label = labels[2],color = 'y',step = 'pre') |
---|
90 | ax.fill_betweenx(height,sum(contents[0:3,:]),sum(contents),label = labels[3],color = 'g',step = 'pre') |
---|
91 | plt.vlines(x = 0.,ymin = height[0],ymax = height[len(height) - 1],color = 'k',linestyle = '-') |
---|
92 | plt.vlines(x = 1.,ymin = height[0],ymax = height[len(height) - 1],color = 'k',linestyle = '-') |
---|
93 | for i in range(len(height)): |
---|
94 | plt.hlines(y = height[i],xmin = 0.0,xmax = 1.0,color = 'k',linestyle = '--') |
---|
95 | ax.set_title('Stack content profiles for the layering') |
---|
96 | plt.xlabel('Volume fraction [m3/m3]') |
---|
97 | plt.ylabel('Elevation [m]') |
---|
98 | ax.legend(loc = 'center left',bbox_to_anchor = (1,0.5)) |
---|
99 | plt.savefig('layering_stackprofiles.png') |
---|
100 | |
---|
101 | plt.show() |
---|
102 | |
---|
103 | ### Close the NetCDF file |
---|
104 | nc_file.close() |
---|