Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruff in pre-commit for Python formatting and linting #64

Merged
merged 11 commits into from
May 8, 2024
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ repos:
rev: v0.1.4 # Ruff version.
hooks:
- id: ruff # Run the linter for Python.
args: ["--ignore=E402"] # Ignore E402 error "Module level import not at top of file"
- id: ruff-format # Run the formatter for Python.
53 changes: 27 additions & 26 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
"""
----- CLEO -----
File: conf.py
Project: source
Expand All @@ -16,7 +16,7 @@
-----
File Description:
configuration for CLEO documentation made using Sphinx
'''
"""


# Configuration file for the Sphinx documentation builder.
Expand All @@ -32,17 +32,18 @@

import pathlib
import sys

# sys.path.insert(0, os.path.abspath('../..'))
sys.path.insert(0, pathlib.Path(__file__).parents[2].resolve().as_posix())

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'CLEO'
license = 'BSD 3-Clause'
copyright = '(2023) MPI-M, Clara Bayley'
author = 'Clara Bayley & Other Developers'
release = '0.1.0'
project = "CLEO"
license = "BSD 3-Clause"
copyright = "(2023) MPI-M, Clara Bayley"
author = "Clara Bayley & Other Developers"
release = "0.1.0"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand All @@ -51,21 +52,21 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.duration',
'sphinx_copybutton',
'sphinx.ext.intersphinx',
'sphinxcontrib.bibtex',
'breathe',
"sphinx.ext.autodoc",
"sphinx.ext.duration",
"sphinx_copybutton",
"sphinx.ext.intersphinx",
"sphinxcontrib.bibtex",
"breathe",
"sphinx.ext.viewcode",
]

# configuration of citations using bibtex file(s)
bibtex_bibfiles = ['./references.bib']
bibtex_reference_style = 'label'
bibtex_bibfiles = ["./references.bib"]
bibtex_reference_style = "label"

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
Expand All @@ -74,30 +75,30 @@

# Integrate doxygen with sphinx via breathe
breathe_projects = {
"libs" : "../build/doxygen/xml/",
"runcleo" : "../build/doxygen/xml/",
"superdrops" : "../build/doxygen/xml/",
"zarr" : "../build/doxygen/xml/",
"observers" : "../build/doxygen/xml/",
"initialise" : "../build/doxygen/xml/",
"libs": "../build/doxygen/xml/",
"runcleo": "../build/doxygen/xml/",
"superdrops": "../build/doxygen/xml/",
"zarr": "../build/doxygen/xml/",
"observers": "../build/doxygen/xml/",
"initialise": "../build/doxygen/xml/",
}

breathe_default_project = 'proj'
breathe_default_project = "proj"

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'furo'
html_theme = "furo"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]

# To include the date of the last visit in Sphinx documentation, use the last
# updated feature of Sphinx. This feature automatically adds the last modification
# date of the source file to the rendered HTML output.
html_last_updated_fmt = '%d %B %Y'
html_last_updated_fmt = "%d %B %Y"
4 changes: 3 additions & 1 deletion docs/source/usage/good_coding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ by automated tools such as `Cocogitto-bot <https://github.com/apps/cocogitto-bot

Code Style / Formatting
-----------------------
For Python, we obey the PEP 8 style guide.
For Python, we use `ruff <https://docs.astral.sh/ruff/>`_` for formatting and linting. Ruff checks
are something like the combination of several Python linters (Flake8, isort, pydocstyle etc.) and
the black formatter. We obey the default settings of ruff except we ignore E402 errors.

For C++ we obey the Google C++ Style Guide with:
| IndentWidth: 2
Expand Down
153 changes: 79 additions & 74 deletions examples/adiabaticparcel/as2017.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
"""
Copyright (c) 2024 MPI-M, Clara Bayley


'''
----- CLEO -----
File: as2017.py
Project: adiabaticparcel
Created Date: Friday 17th November 2023
Author: Clara Bayley (CB)
Additional Contributors:
-----
Last Modified: Thursday 18th April 2024
Last Modified: Tuesday 7th May 2024
Modified By: CB
-----
License: BSD 3-Clause "New" or "Revised" License
https://opensource.org/licenses/BSD-3-Clause
-----
Copyright (c) 2023 MPI-M, Clara Bayley
-----
File Description:
Script generate input files, runs CLEO adia0D executable to
create data and then creates plots for adiabatic parcel example
similar to Figure 5 of "On the CCN (de)activation nonlinearities"
S. Arabas and S. Shima 2017 to show
Script generate input files, runs CLEO adia0D executable to create data and
then creates plots for adiabatic parcel example similar to Figure 5 of "On
the CCN (de)activation nonlinearities" S. Arabas and S. Shima 2017 to show
example of adaibatic parcel expansion and contraction.
Note: SD(M) = superdroplet (model)
'''
"""

import os
import sys
Expand All @@ -36,25 +34,26 @@
configfile = sys.argv[3]

sys.path.append(path2CLEO) # for imports from pySD package
sys.path.append(path2CLEO+"/examples/exampleplotting/") # for imports from example plotting package
sys.path.append(
path2CLEO + "/examples/exampleplotting/"
) # for imports from example plotting package

from plotssrc import pltsds, as2017fig
from plotssrc import as2017fig
from pySD import editconfigfile
from pySD.sdmout_src import sdtracing
from pySD.sdmout_src import *
from pySD.initsuperdropsbinary_src import *
from pySD.sdmout_src import pyzarr, pysetuptxt, pygbxsdat, sdtracing
from pySD.initsuperdropsbinary_src import rgens, dryrgens, probdists, attrsgen
from pySD.initsuperdropsbinary_src import create_initsuperdrops as csupers
from pySD.initsuperdropsbinary_src import read_initsuperdrops as rsupers
from pySD.gbxboundariesbinary_src import read_gbxboundaries as rgrid
from pySD.gbxboundariesbinary_src import create_gbxboundaries as cgrid

############### INPUTS ##################
# path and filenames for creating SD initial conditions and for running model
constsfile = path2CLEO+"/libs/cleoconstants.hpp"
binpath = path2build+"/bin/"
sharepath = path2build+"/share/"
initSDsfile = sharepath+"as2017_dimlessSDsinit.dat"
gridfile = sharepath+"as2017_dimlessGBxboundaries.dat"
constsfile = path2CLEO + "/libs/cleoconstants.hpp"
binpath = path2build + "/bin/"
sharepath = path2build + "/share/"
initSDsfile = sharepath + "as2017_dimlessSDsinit.dat"
gridfile = sharepath + "as2017_dimlessGBxboundaries.dat"

# booleans for [making, saving] initialisation figures
isfigures = [True, True]
Expand Down Expand Up @@ -103,42 +102,41 @@
paramslist = [params1, params2, params3]
lwdths = [2, 1, 0.5]


def displacement(time, w_avg, thalf):
'''displacement z given velocity, w, is sinusoidal
"""displacement z given velocity, w, is sinusoidal
profile: w = w_avg * pi/2 * np.sin(np.pi * t/thalf)
where wmax = pi/2*w_avg and tauhalf = thalf/pi.'''
where wmax = pi/2*w_avg and tauhalf = thalf/pi."""

zmax = w_avg / 2 * thalf
z = zmax * (1 - np.cos(np.pi * time / thalf))
return z


############### RUN EXAMPLE ##################
### delete any existing datasets
for run_num in range(len(monors)*len(paramslist)):
dataset = binpath+"as2017_sol"+str(run_num)+".zarr"
os.system("rm -rf "+dataset)
for run_num in range(len(monors) * len(paramslist)):
dataset = binpath + "as2017_sol" + str(run_num) + ".zarr"
os.system("rm -rf " + dataset)

### ensure build, share and bin directories exist
if path2CLEO == path2build:
raise ValueError("build directory cannot be CLEO")
raise ValueError("build directory cannot be CLEO")
else:
Path(path2build).mkdir(exist_ok=True)
Path(sharepath).mkdir(exist_ok=True)
Path(binpath).mkdir(exist_ok=True)
Path(path2build).mkdir(exist_ok=True)
Path(sharepath).mkdir(exist_ok=True)
Path(binpath).mkdir(exist_ok=True)

### create file (and plot) for gridbox boundaries
os.system("rm "+gridfile)
cgrid.write_gridboxboundaries_binary(gridfile, zgrid, xgrid,
ygrid, constsfile)
os.system("rm " + gridfile)
cgrid.write_gridboxboundaries_binary(gridfile, zgrid, xgrid, ygrid, constsfile)
rgrid.print_domain_info(constsfile, gridfile)
if isfigures[0]:
rgrid.plot_gridboxboundaries(constsfile, gridfile,
binpath, isfigures[1])
rgrid.plot_gridboxboundaries(constsfile, gridfile, binpath, isfigures[1])
plt.close()

runnum = 0
for i in range(len(monors)):

### create file (and plots) for initial SDs conditions
monor, numconc = monors[i], numconcs[i]
# all SDs have the same dryradius = monor [m]
Expand All @@ -147,78 +145,84 @@ def displacement(time, w_avg, thalf):
# monodisperse droplet radii probability distribution
xiprobdist = probdists.DiracDelta(monor)

initattrsgen = attrsgen.AttrsGenerator(radiigen, dryradiigen, xiprobdist,
coord3gen, coord1gen, coord2gen)
os.system("rm "+initSDsfile)
csupers.write_initsuperdrops_binary(initSDsfile, initattrsgen,
configfile, constsfile,
gridfile, nsupers, numconc)
initattrsgen = attrsgen.AttrsGenerator(
radiigen, dryradiigen, xiprobdist, coord3gen, coord1gen, coord2gen
)
os.system("rm " + initSDsfile)
csupers.write_initsuperdrops_binary(
initSDsfile, initattrsgen, configfile, constsfile, gridfile, nsupers, numconc
)
rsupers.print_initSDs_infos(initSDsfile, configfile, constsfile, gridfile)

if isfigures[0]:
rsupers.plot_initGBxs_distribs(configfile, constsfile, initSDsfile,
gridfile, binpath, isfigures[1], "all")
rsupers.plot_initGBxs_distribs(
configfile, constsfile, initSDsfile, gridfile, binpath, isfigures[1], "all"
)
plt.close()

fig, axs = plt.subplots(nrows=3, ncols=1, figsize=(5, 16))
for params, lwdth in zip(paramslist, lwdths):

### edit relevant setup file parameters
params["zarrbasedir"] = binpath+"as2017_sol"+str(runnum)+".zarr"
params["setup_filename"] = binpath+"as2017_setup.txt"
params["zarrbasedir"] = binpath + "as2017_sol" + str(runnum) + ".zarr"
params["setup_filename"] = binpath + "as2017_setup.txt"
editconfigfile.edit_config_params(configfile, params)

### delete any existing dataset
os.system("rm -rf "+params["zarrbasedir"])
os.system("rm "+params["setup_filename"])
os.system("rm -rf " + params["zarrbasedir"])
os.system("rm " + params["setup_filename"])

### run model
os.chdir(path2build)
executable = path2build+"/examples/adiabaticparcel/src/adia0D"
print('Executable: '+executable)
print('Config file: '+configfile)
executable = path2build + "/examples/adiabaticparcel/src/adia0D"
print("Executable: " + executable)
print("Config file: " + configfile)
os.system(executable + " " + configfile)

### load results
setupfile = binpath+"as2017_setup.txt"
dataset = binpath+"as2017_sol"+str(runnum)+".zarr"
setupfile = binpath + "as2017_setup.txt"
dataset = binpath + "as2017_sol" + str(runnum) + ".zarr"

### read in constants and intial setup from setup .txt file
config = pysetuptxt.get_config(setupfile, nattrs=3, isprint=True)
consts = pysetuptxt.get_consts(setupfile, isprint=True)
gbxs = pygbxsdat.get_gridboxes(gridfile, consts["COORD0"],
isprint=True)
gbxs = pygbxsdat.get_gridboxes(gridfile, consts["COORD0"], isprint=True)

# read in output Xarray data
thermo = pyzarr.get_thermodata(dataset, config["ntime"],
gbxs["ndims"], consts)
thermo = pyzarr.get_thermodata(dataset, config["ntime"], gbxs["ndims"], consts)
supersat = thermo.supersaturation()
time = pyzarr.get_time(dataset).secs
sddata = pyzarr.get_supers(dataset, consts)
zprof = displacement(time, config["W_avg"], config["TAU_half"])

attrs = ["radius", "xi", "msol"]
sd0 = sdtracing.attributes_for1superdroplet(sddata, 0, attrs)
numconc = np.sum(sddata["xi"][0])/gbxs["domainvol"]/1e6 # [/cm^3]
numconc = np.sum(sddata["xi"][0]) / gbxs["domainvol"] / 1e6 # [/cm^3]

### plot results
wlab = "<w> = {:.1f}".format(config["W_avg"]*100)+"cm s$^{-1}$"
axs = as2017fig.condensation_validation_subplots(axs, time, sd0["radius"],
supersat[:, 0, 0, 0],
zprof,
lwdth=lwdth,
lab=wlab)
wlab = "<w> = {:.1f}".format(config["W_avg"] * 100) + "cm s$^{-1}$"
axs = as2017fig.condensation_validation_subplots(
axs, time, sd0["radius"], supersat[:, 0, 0, 0], zprof, lwdth=lwdth, lab=wlab
)

runnum += 1

### save figure
as2017fig.plot_kohlercurve_with_criticalpoints(axs[1], sd0["radius"],
sd0["msol"][0],
thermo.temp[0, 0, 0, 0],
sddata.IONIC, sddata.MR_SOL)

textlab = "N = "+str(numconc)+"cm$^{-3}$\n" +\
"r$_{dry}$ = "+"{:.2g}\u03BCm\n".format(sd0["radius"][0])
as2017fig.plot_kohlercurve_with_criticalpoints(
axs[1],
sd0["radius"],
sd0["msol"][0],
thermo.temp[0, 0, 0, 0],
sddata.IONIC,
sddata.MR_SOL,
)

textlab = (
"N = "
+ str(numconc)
+ "cm$^{-3}$\n"
+ "r$_{dry}$ = "
+ "{:.2g}\u03BCm\n".format(sd0["radius"][0])
)
axs[0].legend(loc="lower right", fontsize=10)
axs[1].legend(loc="upper left")
axs[0].text(0.03, 0.85, textlab, transform=axs[0].transAxes)
Expand All @@ -233,8 +237,9 @@ def displacement(time, w_avg, thalf):

fig.tight_layout()

savename = "as2017fig_"+str(i)+".png"
fig.savefig(binpath+savename, dpi=400,
bbox_inches="tight", facecolor='w', format="png")
print("Figure .png saved as: "+binpath+savename)
savename = "as2017fig_" + str(i) + ".png"
fig.savefig(
binpath + savename, dpi=400, bbox_inches="tight", facecolor="w", format="png"
)
print("Figure .png saved as: " + binpath + savename)
plt.show()
Loading
Loading