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

Improve "code quality" #60

Merged
merged 7 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bandit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exclude_dirs:
tests/**
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pyDeltaRCM_WMT
.. image:: https://coveralls.io/repos/github/DeltaRCM/pyDeltaRCM_WMT/badge.svg?branch=develop
:target: https://coveralls.io/github/DeltaRCM/pyDeltaRCM_WMT?branch=develop

.. image:: https://app.codacy.com/project/badge/Grade/1c137d0227914741a9ba09f0b00a49a7
:target: https://www.codacy.com/gh/DeltaRCM/pyDeltaRCM_WMT?utm_source=github.com&utm_medium=referral&utm_content=DeltaRCM/pyDeltaRCM_WMT&utm_campaign=Badge_Grade

pyDeltaRCM is the Python version of the `Matlab deltaRCM <https://csdms.colorado.edu/wiki/Model:DeltaRCM>`_ model by Man Liang.
The pyDeltaRCM scripts in this repository can be run as a stand-alone model following the instructions below.
Expand Down
25 changes: 2 additions & 23 deletions pyDeltaRCM/deltaRCM_tools.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
#! /usr/bin/env python

import sys
import os
import re
import string
import logging
import time
import warnings

from math import floor, sqrt, pi
import numpy as np
from random import shuffle

import matplotlib
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt

from scipy.sparse import lil_matrix, csc_matrix, hstack
from scipy import ndimage

from netCDF4 import Dataset

from .sed_tools import sed_tools
from .water_tools import water_tools
Expand Down Expand Up @@ -46,7 +34,6 @@ def run_one_timestep(self):
RuntimeError
If model has already been finalized via :meth:`finalize`.
"""

timestep = self._time

self.logger.info('-' * 4 + ' Timestep ' +
Expand All @@ -70,7 +57,7 @@ def run_one_timestep(self):
self.sed_route()

def finalize_timestep(self):
"""
"""Finalize timestep.

Clean up after sediment routing. This includes a correction for
flooded cells that are not "wet" (via :meth:`flooding_correction`).
Expand All @@ -84,7 +71,6 @@ def finalize_timestep(self):
-------

"""

self.flooding_correction()
self.stage[:] = np.maximum(self.stage, self.H_SL)
self.depth[:] = np.maximum(self.stage - self.eta, 0)
Expand All @@ -104,7 +90,6 @@ def expand_stratigraphy(self):
-------

"""

_msg = 'Expanding stratigraphy arrays'
self.logger.info(_msg)
if self.verbose >= 2:
Expand Down Expand Up @@ -139,7 +124,6 @@ def record_stratigraphy(self):
-------

"""

timestep = self._time

if self.save_strata and (timestep % self.save_dt == 0):
Expand Down Expand Up @@ -213,7 +197,6 @@ def apply_subsidence(self):
-------

"""

if self.toggle_subsidence:

timestep = self._time
Expand Down Expand Up @@ -241,7 +224,6 @@ def output_data(self):
-------

"""

timestep = self._time

if timestep % self.save_dt == 0:
Expand Down Expand Up @@ -331,7 +313,6 @@ def output_strata(self):
-------

"""

if self.save_strata:

_msg = 'Saving final stratigraphy to netCDF file'
Expand Down Expand Up @@ -404,7 +385,6 @@ def save_figure(self, path, ext='png', close=True):
-------

"""

directory = os.path.split(path)[0]
filename = "%s.%s" % (os.path.split(path)[1], ext)
if directory == '':
Expand Down Expand Up @@ -442,7 +422,6 @@ def save_grids(self, var_name, var, ts):
-------

"""

try:
self.output_netcdf.variables[var_name][ts, :, :] = var
except:
Expand Down
33 changes: 5 additions & 28 deletions pyDeltaRCM/init_tools.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@

import sys
import os
import re
import string
import logging
# import time
import warnings

from math import floor, sqrt, pi
import numpy as np
from random import shuffle

import matplotlib
from matplotlib import pyplot as plt

from scipy.sparse import lil_matrix, csc_matrix, hstack
from scipy.sparse import lil_matrix
from scipy import ndimage

from netCDF4 import Dataset
import time as time_lib
from scipy.sparse import lil_matrix, csc_matrix, hstack
import logging
# import time
import yaml

from . import shared_tools
Expand All @@ -44,13 +33,10 @@ def init_output_infrastructure(self):

def init_logger(self):
"""Initialize a logger.

The logger is initialized regardless of the value of ``self.verbose``.
The level of information printed to the log depends on the verbosity
setting.

"""

self.logger = logging.getLogger('driver')
self.logger.setLevel(logging.INFO)

Expand Down Expand Up @@ -180,11 +166,10 @@ def create_other_variables(self):
Creates variables for model implementation, from specified boundary
condition variables. This method is run during initial model
instantition, but it is also run any time an inlet flow condition
variable is changed, including ``channel_flow_velocity``,
``channel_width``, ``channel_flow_depth``, and
``influx_sediment_concentration``.
variable is changed, including `channel_flow_velocity`,
`channel_width`, `channel_flow_depth`, and
`influx_sediment_concentration`.
"""

self.init_Np_water = self.Np_water
self.init_Np_sed = self.Np_sed

Expand Down Expand Up @@ -258,19 +243,16 @@ def create_domain(self):
"""
Creates the model domain
"""

self.logger.info('Creating model domain')

# ---- empty arrays ----
self.x, self.y = np.meshgrid(
np.arange(0, self.W), np.arange(0, self.L))

self.cell_type = np.zeros((self.L, self.W), dtype=np.int)

self.eta = np.zeros((self.L, self.W)).astype(np.float32)
self.stage = np.zeros((self.L, self.W)).astype(np.float32)
self.depth = np.zeros((self.L, self.W)).astype(np.float32)

self.qx = np.zeros((self.L, self.W))
self.qy = np.zeros((self.L, self.W))
self.qxn = np.zeros((self.L, self.W))
Expand All @@ -279,11 +261,9 @@ def create_domain(self):
self.ux = np.zeros((self.L, self.W))
self.uy = np.zeros((self.L, self.W))
self.uw = np.zeros((self.L, self.W))

self.qs = np.zeros((self.L, self.W))
self.Vp_dep_sand = np.zeros((self.L, self.W))
self.Vp_dep_mud = np.zeros((self.L, self.W))

self.free_surf_flag = np.zeros((self.Np_water,), dtype=np.int)
self.looped = np.zeros((self.Np_water,))
self.indices = np.zeros((self.Np_water, self.size_indices),
Expand Down Expand Up @@ -342,8 +322,7 @@ def create_domain(self):
self.clim_eta = (-self.h0 - 1, 0.05)

def init_stratigraphy(self):
"""Creates sparse array to store stratigraphy data.
"""
"""Creates sparse array to store stratigraphy data."""
if self.save_strata:

self.strata_counter = 0
Expand All @@ -365,7 +344,6 @@ def init_output_grids(self):
.. warning:: Overwrites an existing netcdf file with the same name.

"""

if (self.save_eta_grids or
self.save_depth_grids or
self.save_stage_grids or
Expand Down Expand Up @@ -456,7 +434,6 @@ def init_subsidence(self):

Modify the equations for self.subsidence_mask and self.sigma as desired
"""

if self.toggle_subsidence:

R1 = 0.3 * self.L
Expand Down
28 changes: 10 additions & 18 deletions pyDeltaRCM/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class DeltaModel(Tools):
Finally, after all ``update`` steps are complete, the model should be
finalized (:meth:`finalize`), such that files and data are appropriately
closed and written to disk.

"""

def __init__(self, input_file=None):
"""Creates an instance of the pyDeltaRCM model.

Expand All @@ -47,7 +45,6 @@ def __init__(self, input_file=None):
:doc:`../../guides/userguide`.

"""

self._time = 0.
self._time_step = 1.

Expand Down Expand Up @@ -91,7 +88,6 @@ def update(self):
-------

"""

self.run_one_timestep()

self.apply_subsidence()
Expand All @@ -116,9 +112,7 @@ def finalize(self):
-------

"""

self.logger.info('Finalize model run')

self.output_strata()

try:
Expand All @@ -141,7 +135,6 @@ def time_step(self):
UserWarning
If a very small timestep is configured.
"""

return self._time_step

@time_step.setter
Expand All @@ -160,7 +153,7 @@ def time_step(self, new_dt):

@property
def channel_flow_velocity(self):
""" Get channel flow velocity """
"""Get channel flow velocity."""
return self.u0

@channel_flow_velocity.setter
Expand All @@ -170,7 +163,7 @@ def channel_flow_velocity(self, new_u0):

@property
def channel_width(self):
""" Get channel width """
"""Get channel width."""
return self.N0_meters

@channel_width.setter
Expand All @@ -180,7 +173,7 @@ def channel_width(self, new_N0):

@property
def channel_flow_depth(self):
""" Get channel flow depth """
"""Get channel flow depth."""
return self.h0

@channel_flow_depth.setter
Expand All @@ -190,7 +183,7 @@ def channel_flow_depth(self, new_d):

@property
def sea_surface_mean_elevation(self):
""" Get sea surface mean elevation """
"""Get sea surface mean elevation."""
return self.H_SL

@sea_surface_mean_elevation.setter
Expand All @@ -199,17 +192,16 @@ def sea_surface_mean_elevation(self, new_se):

@property
def sea_surface_elevation_change(self):
""" Get rate of change of sea surface elevation, per timestep"""
"""Get rate of change of sea surface elevation, per timestep."""
return self.SLR

@sea_surface_elevation_change.setter
def sea_surface_elevation_change(self, new_SLR):
""" Set rate of change of sea surface elevation, per timestep"""
self.SLR = new_SLR

@property
def bedload_fraction(self):
""" Get bedload fraction """
"""Get bedload fraction."""
return self.f_bedload

@bedload_fraction.setter
Expand All @@ -218,7 +210,7 @@ def bedload_fraction(self, new_u0):

@property
def influx_sediment_concentration(self):
""" Get influx sediment concentration """
"""Get influx sediment concentration."""
return self.C0_percent

@influx_sediment_concentration.setter
Expand All @@ -228,15 +220,15 @@ def influx_sediment_concentration(self, new_u0):

@property
def sea_surface_elevation(self):
""" Get stage """
"""Get stage."""
return self.stage

@property
def water_depth(self):
""" Get depth """
"""Get depth."""
return self.depth

@property
def bed_elevation(self):
""" Get bed elevation """
"""Get bed elevation."""
return self.eta
Loading