ENVI-met powered by python

Visualizing netCDF outputs in python offers a quick and flexible solution for exploring ENVI-met's model output files. By using python libraries such as matplotlib, you can seamlessly load and display netCDF files.

Here is some sample code:

  1. import matplotlib.pyplot as plt
  2. import netCDF4
  3.  
  4. def plot_map(netCDFfile, varName):
  5. nc = netCDF4.Dataset(netCDFfile) # path to your ENVI-met netCDF output file e.g. "T:/enviprojects/Berlin/output/NetCDF/Berlin_12.5_AT_2021-07-07_06.00.01.nc"
  6.  
  7. print(nc.variables.keys()) # shows all available variables e.g. "Potential_Air_Temperature", "Relative_Humidity"
  8. print(len(nc.variables['Time'])) # shows how many timesteps are inside the netCDF output
  9.  
  10. dataAr = nc.variables[varName][5][1][:][:] # first index is the output timestep (here 5), second index is the height level (here 1)
  11. gridJAr = nc.variables['GridsJ'][:]
  12. gridIAr = nc.variables['GridsI'][:]
  13.  
  14. fig, ax = plt.subplots(figsize=(10*len(gridIAr) / len(gridJAr), 10)) # determines the resolution of the figure
  15. ax.set_aspect(1) # scales the aspect ratio
  16. contour = plt.contourf(gridIAr, gridJAr, dataAr, cmap=twilight_shifted, levels=100) # cmap defines the colorramp, levels defines the number of color gradients
  17.  
  18. plt.colorbar(contour, ax=ax, label=varName, location='right', shrink=0.4) # defines the legend
  19.  
  20. plt.title('ENVI-met powered by python') # adds a title
  21. plt.xlabel('meters') # adds the x label
  22. plt.ylabel('meters') # adds the y label
  23.  
  24. #plt.figtext(0.5, 0.01, 'footnote center', ha='center', va='center', fontsize='small') # option to add a centered footnote
  25. #plt.figtext(0.98, 0.02, 'footnote right', ha='right', va='bottom', fontsize='small') # option to add a footnote on the lower right corner
  26.  
  27. # Show the plot
  28. plt.show() # shows the plot
  29.  
  30. #plt.savefig('D:/_images/' + varName + '.png', bbox_inches=0) # option to save the figure
  31. #plt.close()
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies