source: trunk/LMDZ.COMMON/libf/evolution/deftank/output_layering.py @ 3321

Last change on this file since 3321 was 3300, checked in by jbclement, 7 months ago

PEM:
Update of "output_layering.py" in the deftank.
JBC

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