Skip to content

Commit

Permalink
fix codacy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
OnnoEbbens committed Aug 25, 2021
1 parent 1f697fd commit f8a1400
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 89 deletions.
3 changes: 2 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
from nlmod import __version__
import os
import sys

sys.path.insert(0, os.path.abspath('.'))
from nlmod import __version__



# -- Project information -----------------------------------------------------
Expand Down
13 changes: 6 additions & 7 deletions nlmod/mdims/mbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from .. import util

def get_empty_model_ds(model_name, model_ws, mfversion="mf6",

def get_empty_model_ds(model_name, model_ws, mfversion="mf6",
exe_name=None):
""" get an empty model dataset
Expand Down Expand Up @@ -34,24 +35,22 @@ def get_empty_model_ds(model_name, model_ws, mfversion="mf6",
model_ds.attrs['model_name'] = model_name
model_ds.attrs['mfversion'] = mfversion
model_ds.attrs['model_dataset_created_on'] = dt.datetime.now().strftime("%Y%m%d_%H:%M:%S")

if exe_name is None:
exe_name = os.path.join(os.path.dirname(__file__),
'..', '..', 'bin', model_ds.mfversion)

# if working on Windows add .exe extension
if sys.platform.startswith('win'):
exe_name += ".exe"

model_ds.attrs["exe_name"] = exe_name
# add some directories

# add some directories
if model_ws is not None:
figdir, cachedir = util.get_model_dirs(model_ws)
model_ds.attrs['model_ws'] = model_ws
model_ds.attrs['figdir'] = figdir
model_ds.attrs['cachedir'] = cachedir



return model_ds
2 changes: 1 addition & 1 deletion nlmod/mdims/mtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def set_model_ds_time(model_ds, start_time, steady_state,
steady_start=False, time_units='DAYS',
transient_timesteps=0,
perlen=1.0, steady_perlen=3650,
perlen=1.0, steady_perlen=3650,
nstp=1, tsmult=1.0):
"""Set timing for a model dataset.
Expand Down
24 changes: 12 additions & 12 deletions nlmod/mdims/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def resample_dataarray_to_structured_grid(da_in, extent=None, delr=None, delc=No
Also flips the y-coordinates to make them descending instead of ascending.
This makes it easier to export array to flopy. In other words, make sure
that both lines of code create the same plot::
da_in['top'].sel(layer=b'Hlc').plot()
plt.imshow(da_in['top'].sel(layer=b'Hlc').data)
Expand Down Expand Up @@ -635,7 +635,7 @@ def fillnan_dataarray_unstructured_grid(xar_in, gridprops=None,


def resample_unstr_2d_da_to_struc_2d_da(da_in, model_ds, cellsize=25,
method ='nearest'):
method='nearest'):
"""resample a 2d dataarray (xarray) from an unstructured grid to a new
dataaraay from a structured grid.
Expand All @@ -657,22 +657,22 @@ def resample_unstr_2d_da_to_struc_2d_da(da_in, model_ds, cellsize=25,
"""
points_unstr = np.array([model_ds.x.values, model_ds.y.values]).T
modelgrid_x = np.arange(model_ds.x.values.min(),
model_ds.x.values.max(),
modelgrid_x = np.arange(model_ds.x.values.min(),
model_ds.x.values.max(),
cellsize)
modelgrid_y = np.arange(model_ds.y.values.max(),
model_ds.y.values.min(),
modelgrid_y = np.arange(model_ds.y.values.max(),
model_ds.y.values.min(),
-cellsize)
mg = np.meshgrid(modelgrid_x, modelgrid_y)
points = np.vstack((mg[0].ravel(), mg[1].ravel())).T
points = np.vstack((mg[0].ravel(), mg[1].ravel())).T

arr_out_1d = griddata(points_unstr, da_in.values, points, method=method)
arr_out2d = arr_out_1d.reshape(len(modelgrid_y),
arr_out2d = arr_out_1d.reshape(len(modelgrid_y),
len(modelgrid_x))

da_out = xr.DataArray(arr_out2d,
dims=('y','x'),
dims=('y', 'x'),
coords={'y': modelgrid_y,
'x': modelgrid_x})

return da_out
4 changes: 2 additions & 2 deletions nlmod/mfpackages/surface_water.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def estimate_polygon_length(gdf):
def distribute_cond_over_lays(cond, cellid, rivbot, laytop, laybot,
idomain=None, kh=None, stage=None):

if isinstance(rivbot, np.ndarray) or isinstance(rivbot, xr.DataArray):
if isinstance(rivbot, (np.ndarray, xr.DataArray)):
rivbot = float(rivbot[cellid])
if len(laybot.shape) == 3:
# the grid is structured grid
Expand Down Expand Up @@ -435,5 +435,5 @@ def build_spd(celldata, pkg, model_ds):
spd.append([cid, stage, cond, rbot] + auxlist)
elif pkg in ["DRN", "GHB"]:
spd.append([cid, stage, cond] + auxlist)

return spd
4 changes: 1 addition & 3 deletions nlmod/read/geotop.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_geotop_dataset(extent, delr, delc,
geotop_ds = mdims.get_resampled_ml_layer_ds_struc(raw_ds=geotop_ds_raw,
extent=extent,
delr=delr, delc=delc)

for datavar in geotop_ds:
geotop_ds[datavar].attrs['source'] = 'Geotop'
geotop_ds[datavar].attrs['url'] = geotop_url
Expand All @@ -98,8 +98,6 @@ def get_geotop_raw_within_extent(extent, url):
slices geotop netcdf.
"""



geotop_ds_raw = xr.open_dataset(url)

# set x and y dimensions to cell center
Expand Down
3 changes: 1 addition & 2 deletions nlmod/read/jarkus.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,11 @@ def bathymetry_to_model_dataset(model_ds,

model_ds_out['bathymetry'] = xr.where(
model_ds['northsea'], da_bathymetry, np.nan)

for datavar in model_ds_out:
model_ds_out[datavar].attrs['source'] = 'Jarkus'
model_ds_out[datavar].attrs['url'] = url
model_ds_out[datavar].attrs['source'] = dt.datetime.now().strftime('%Y%m%d')


return model_ds_out

Expand Down
2 changes: 1 addition & 1 deletion nlmod/read/knmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def add_knmi_to_model_dataset(model_ds,

elif model_ds.gridtype == 'unstructured':
model_ds_out['recharge'].loc[loc_sel.index, :] = model_recharge.values

for datavar in model_ds_out:
model_ds_out[datavar].attrs['source'] = 'KNMI'
model_ds_out[datavar].attrs['date'] = dt.datetime.now().strftime('%Y%m%d')
Expand Down
13 changes: 2 additions & 11 deletions nlmod/read/regis.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def get_layer_models(extent, delr, delc,
def get_combined_layer_models(extent, delr, delc,
regis_botm_layer=b'AKc',
use_regis=True, use_geotop=True,
remove_nan_layers=True,
cachedir=None, use_cache=False):
remove_nan_layers=True):
"""combine layer models into a single layer model.
Possibilities so far include:
Expand Down Expand Up @@ -110,12 +109,6 @@ def get_combined_layer_models(extent, delr, delc,
if True regis and geotop layers with only nans are removed from the
model. if False nan layers are kept which might be usefull if you want
to keep some layers that exist in other models. The default is True.
cachedir : str
directory to store cached values, if None a temporary directory is
used. default is None
use_cache : bool, optional
if True the cached resampled regis dataset is used.
The default is False.
Raises
------
Expand Down Expand Up @@ -216,7 +209,7 @@ def get_regis_dataset(extent, delr, delc, botm_layer=b'AKc'):
regis_ds = mdims.get_resampled_ml_layer_ds_struc(raw_ds=regis_ds_raw2,
extent=extent,
delr=delr, delc=delc)

for datavar in regis_ds:
regis_ds[datavar].attrs['source'] = 'REGIS'
regis_ds[datavar].attrs['url'] = regis_url
Expand Down Expand Up @@ -332,8 +325,6 @@ def add_geotop_to_regis_hlc(regis_ds, geotop_ds,
regis_geotop_ds[key].attrs['regis_url'] = regis_ds[key].url
regis_geotop_ds[key].attrs['geotop_url'] = geotop_ds[key].url
regis_geotop_ds[key].attrs['date'] = dt.datetime.now().strftime('%Y%m%d')



return regis_geotop_ds

Expand Down
4 changes: 2 additions & 2 deletions nlmod/read/rws.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def surface_water_to_model_dataset(model_ds, modelgrid, da_name):
area = xr.zeros_like(model_ds['top'])
cond = xr.zeros_like(model_ds['top'])
peil = xr.zeros_like(model_ds['top'])
for i, row in gdf.iterrows():
for _, row in gdf.iterrows():
area_pol = mdims.polygon_to_area(modelgrid, row['geometry'],
xr.ones_like(model_ds['top']),
model_ds.gridtype)
Expand All @@ -106,7 +106,7 @@ def surface_water_to_model_dataset(model_ds, modelgrid, da_name):
model_ds_out[f'{da_name}_area'] = area
model_ds_out[f'{da_name}_cond'] = cond
model_ds_out[f'{da_name}_peil'] = peil

for datavar in model_ds_out:
model_ds_out[datavar].attrs['source'] = 'RWS'
model_ds_out[datavar].attrs['date'] = dt.datetime.now().strftime('%Y%m%d')
Expand Down
42 changes: 16 additions & 26 deletions nlmod/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@
from shapely.geometry import box
from shutil import copyfile

import __main__


logger = logging.getLogger(__name__)



def write_and_run_model(gwf, model_ds, write_model_ds=True,
nb_path=None):
""" write modflow files and run the model. 2 extra options:
1. write the model dataset to cache
2. copy the modelscript (typically a Jupyter Notebook) to the model
workspace with a timestamp.
Parameters
----------
Expand All @@ -49,29 +45,26 @@ def write_and_run_model(gwf, model_ds, write_model_ds=True,
of a Jupyter Notebook from within the notebook itself.
"""
if not nb_path is None:

if nb_path is not None:
new_nb_fname = f'{dt.datetime.now().strftime("%Y%m%d")}' + os.path.split(nb_path)[-1]
dst = os.path.join(model_ds.model_ws, new_nb_fname)
dst = os.path.join(model_ds.model_ws, new_nb_fname)
logger.info(f'write script {new_nb_fname} to model workspace')
copyfile(nb_path, dst)



if write_model_ds:
logger.info('write model dataset to cache')
model_ds.to_netcdf(os.path.join(model_ds.attrs['cachedir'],
model_ds.to_netcdf(os.path.join(model_ds.attrs['cachedir'],
'full_model_ds.nc'))
model_ds.attrs['model_dataset_written_to_disk_on'] = dt.datetime.now().strftime("%Y%m%d_%H:%M:%S")

logger.info('write modflow files to model workspace')
gwf.simulation.write_simulation()
model_ds.attrs['model_data_written_to_disk_on'] = dt.datetime.now().strftime("%Y%m%d_%H:%M:%S")

logger.info('run model')
assert gwf.simulation.run_simulation()[0]
model_ds.attrs['model_ran_on'] = dt.datetime.now().strftime("%Y%m%d_%H:%M:%S")




def get_model_dirs(model_ws):
Expand Down Expand Up @@ -105,7 +98,6 @@ def get_model_dirs(model_ws):
os.mkdir(cachedir)

return figdir, cachedir



def get_model_ds_empty(model_ds):
Expand Down Expand Up @@ -160,7 +152,7 @@ def check_delr_delc_extent(dic, model_ds):
if 'extent' in dic.keys():
key_check = (np.array(dic['extent']) == np.array(
model_ds.attrs['extent'])).all()

if key_check:
logger.info('extent of current grid is the same as cached grid')
else:
Expand Down Expand Up @@ -698,6 +690,7 @@ def getmfexes(pth='.', version='', pltfrm=None):

return


def add_heads_to_model_ds(model_ds, fname_hds=None):
"""reads the heads from a modflow .hds file and returns an xarray
DataArray.
Expand All @@ -717,18 +710,16 @@ def add_heads_to_model_ds(model_ds, fname_hds=None):

if fname_hds is None:
fname_hds = os.path.join(model_ds.model_ws, model_ds.model_name + '.hds')

head_filled = get_heads_array(fname_hds, gridtype=model_ds.gridtype)



if model_ds.gridtype == 'unstructured':
head_ar = xr.DataArray(data=head_filled[:, :, :],
dims=('time', 'layer', 'cid'),
coords={'cid': model_ds.cid,
'layer': model_ds.layer,
'time': model_ds.time})
elif model_ds.gridtype =='structured':
elif model_ds.gridtype == 'structured':
head_ar = xr.DataArray(data=head_filled,
dims=('time', 'layer', 'y', 'x'),
coords={'x': model_ds.x,
Expand All @@ -738,6 +729,7 @@ def add_heads_to_model_ds(model_ds, fname_hds=None):

return head_ar


def get_heads_array(fname_hds, gridtype='structured',
fill_nans=True):
"""reads the heads from a modflow .hds file and returns a numpy array.
Expand Down Expand Up @@ -767,7 +759,7 @@ def get_heads_array(fname_hds, gridtype='structured',

if gridtype == 'unstructured':
head_filled = np.ones((head.shape[0], head.shape[1], head.shape[3])) * np.nan

for t in range(head.shape[0]):
for lay in range(head.shape[1] - 1, -1, -1):
head_filled[t][lay] = head[t][lay][0]
Expand All @@ -777,7 +769,7 @@ def get_heads_array(fname_hds, gridtype='structured',
head_filled[t][lay + 1],
head_filled[t][lay])

elif gridtype =='structured':
elif gridtype == 'structured':
head_filled = np.zeros_like(head)
for t in range(head.shape[0]):
for lay in range(head.shape[1] - 1, -1, -1):
Expand All @@ -789,7 +781,7 @@ def get_heads_array(fname_hds, gridtype='structured',
head_filled[t][lay])
else:
raise ValueError('wrong gridtype')

return head_filled


Expand All @@ -811,5 +803,3 @@ def download_mfbinaries(binpath=None, version='6.0'):
pltfrm = get_platform(None)
# Download and unpack mf6 exes
getmfexes(pth=binpath, version=version, pltfrm=pltfrm)


Loading

0 comments on commit f8a1400

Please sign in to comment.