Skip to content

Commit

Permalink
Merge pull request #73 from openearth/fix-logger
Browse files Browse the repository at this point in the history
central logger
  • Loading branch information
DirkEilander authored Jun 29, 2018
2 parents 2c466fe + 91ca333 commit cfe8b90
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 204 deletions.
100 changes: 16 additions & 84 deletions couplingFramework_PCR_to_DFM.ipynb

Large diffs are not rendered by default.

82 changes: 38 additions & 44 deletions couplingFramework_PCR_to_LFP.ipynb

Large diffs are not rendered by default.

17 changes: 6 additions & 11 deletions glofrim-py/glofrim/glofrim_cmf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

import logging
import glob
import shutil
from os import mkdir, unlink
Expand All @@ -15,10 +14,6 @@
from main import BMI_model_wrapper
from utils import subcall

log_fmt = '%(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(level=logging.INFO, format=log_fmt, filemode='w')
logger = logging.getLogger(__name__)

class CMF_model(BMI_model_wrapper):
def __init__(self, engine, config_fn,
model_data_dir, out_dir,
Expand Down Expand Up @@ -81,7 +76,7 @@ def set_model_input_files(self):
shutil.copy(src_fn, dst_path)
# remove *tmp.* files from data_dir
if 'tmp.' in basename(src_fn):
logger.info('removing tmp file {:s} from model data dir'.format(basename(src_fn)))
self.logger.info('removing tmp file {:s} from model data dir'.format(basename(src_fn)))
unlink(src_fn)
# update config_fn
self.update_config(sec, opt, '"./{}/{}"'.format(folder, fn))
Expand All @@ -101,7 +96,7 @@ def set_inpmat_file(self, bounds, res, olat='NtoS'):
# generate inpmat
msg2 = './generate_inpmat {} {} {} {} {} {:s}'.format(
abs(res[0]), westin, eastin, northin, southin, olat)
logger.info(msg2)
self.logger.info(msg2)
subcall(msg2, cwd=ddir)
# set new inpmat and diminfo in config
rel_path = relpath(dirname(self.config_fn), ddir)
Expand Down Expand Up @@ -132,7 +127,7 @@ def get_model_grid(self):
"""
from nb.dd_ops import NextXY

logger.info('Getting CMF model grid parameters.')
self.logger.info('Getting CMF model grid parameters.')
fn_lsmask = join(self.model_data_dir, 'lsmask.tif')
if not isfile(fn_lsmask):
raise IOError("lsmask.tif file not found at {}".format(fn_lsmask))
Expand All @@ -144,10 +139,10 @@ def get_model_grid(self):
self.model_grid_transform = ds.transform
self._fn_landmask = fn_lsmask
msg = 'Model bounds {:s}; width {}, height {}'
logger.debug(msg.format(self.model_grid_bounds, *self.model_grid_shape))
self.logger.debug(msg.format(self.model_grid_bounds, *self.model_grid_shape))

# read drainage direction data
logger.info('Getting CMF model drainage direction')
self.logger.info('Getting CMF model drainage direction')
fn_nextxy = join(self.model_data_dir, 'nextxy.bin')
if not isfile(fn_nextxy):
raise IOError("nextxy.bin file not found at {}".format(fn_nextxy))
Expand Down Expand Up @@ -177,7 +172,7 @@ def model_2d_index(self, xy, **kwargs):
indices : list of tuples
list of (row, col) index tuples
"""
logger.info('Getting CMF model indices for xy coordinates.')
self.logger.info('Getting CMF model indices for xy coordinates.')
fn_catmxy = join(self.model_data_dir, 'hires', 'reg.catmxy.tif')
if not isfile(fn_catmxy):
raise IOError("{} file not found".format(fn_catmxy))
Expand Down
15 changes: 5 additions & 10 deletions glofrim-py/glofrim/glofrim_dfm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

import logging
import glob
import shutil
from os import mkdir
Expand All @@ -13,11 +12,6 @@
from main import BMI_model_wrapper
from utils import ConfigParser

log_fmt = '%(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(level=logging.INFO, format=log_fmt, filemode='w')
logger = logging.getLogger(__name__)


class DFM_model(BMI_model_wrapper):
def __init__(self, engine, config_fn,
model_data_dir, out_dir,
Expand Down Expand Up @@ -76,7 +70,7 @@ def get_model_coords(self):
"""Get DFM model coordinates for 1D and 2D mesh via BMI. The DFM model
should be initialized first in order to access the variables."""

logger.info('Getting DFM model coordinates.')
self.logger.info('Getting DFM model coordinates.')
# define separator between 2D and 1D parts of arrays == lenght of 2d cell points
self._1d2d_idx = len(self.get_var('flowelemnode'))
x_coords = self.get_var('xz') # x-coords of each cell centre point
Expand All @@ -96,12 +90,12 @@ def get_area_1d(self):
def get_model_1d_index(self):
"""Creat a spatial index for the 1d coordinates. A model_1d_index
attribute funtion is created to find the nearest 1d coordinate tuple"""
logger.info('Constructing spatial index for the 1D coordinates of the DFM model.')
self.logger.info('Constructing spatial index for the 1D coordinates of the DFM model.')
# 1d coords
n1d = len(self.model_1d_coords)
self.model_1d_indices = np.arange(n1d, dtype=np.int32) + self._1d2d_idx
# build spatial rtree index of points2
logger.info('Constructing spatial index for 1D vertices of DFM model')
self.logger.info('Constructing spatial index for 1D vertices of DFM model')
self.model_1d_rtree = rtree.index.Index()
for i, xy in enumerate(self.model_1d_coords):
self.model_1d_rtree.insert(i+n1d, xy) # return index including 2d
Expand All @@ -117,7 +111,7 @@ def get_model_2d_index(self):
A model_2d_index attribute funtion is created to find the nearest
2d cell center"""
# build spatial rtree index of 2d coords
logger.info('Constructing spatial index for the 2D mesh of the DFM model')
self.logger.info('Constructing spatial index for the 2D mesh of the DFM model')
self.model_2d_rtree = rtree.index.Index()
for i, xy in enumerate(self.model_2d_coords):
self.model_2d_rtree.insert(i, xy)
Expand All @@ -130,3 +124,4 @@ def model_2d_index(self, xy, n=1):
#TODO: assess validity based on e.g. max distance
valid = np.ones(len(index), dtype=bool)
return index, valid

26 changes: 11 additions & 15 deletions glofrim-py/glofrim/glofrim_lfp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

import logging
import glob
import shutil
import os
Expand All @@ -15,11 +14,6 @@
from bmi.wrapper import BMIWrapper
from main import BMI_model_wrapper

log_fmt = '%(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(level=logging.INFO, format=log_fmt, filemode='w')
logger = logging.getLogger(__name__)


class LFP_model(BMI_model_wrapper):
def __init__(self, engine, config_fn,
model_data_dir, out_dir,
Expand Down Expand Up @@ -80,13 +74,14 @@ def get_model_coords(self):
"""Get LFP model coordinates for 1D and 2D mesh via BMI. The LFP model
should be initialized first in order to access the variables."""

logger.info('Getting LFP model coordinates.')
self.logger.info('Getting LFP model coordinates.')

i_ind, j_ind = np.where(np.logical_and(self.get_var('SGCwidth') > 0., self.get_var('DEM') != -9999))
print i_ind.shape, j_ind.shape
# print i_ind.shape, j_ind.shape

fn_map = join(self.model_data_dir,
self.model_config['dummysection']['DEMfile'])
self.model_config['header1']['DEMfile'])

if not isfile(fn_map):
raise IOError('landmask file not found')
self._fn_landmask = fn_map
Expand All @@ -97,22 +92,23 @@ def get_model_coords(self):
self.model_grid_bounds = ds.bounds
self.model_grid_shape = ds.shape
self.model_grid_transform = ds.transform

list_x_coords, list_y_coords = self.model_grid_transform * (i_ind, j_ind)
list_x_coords, list_y_coords = ds.xy(i_ind, j_ind)

self.model_1d_coords = zip(list_x_coords, list_y_coords)
self.model_1d_indices = zip(i_ind, j_ind)
self.model_1d_indices = np.arange(i_ind.size)
self.model_1d_rc = (i_ind, j_ind)

pass

def get_area_1d(self):
row, col = zip(*self.model_1d_indices)
row, col = self.model_1d_rc
area_1D = self.get_var('dA')[row, col]
return area_1D


class ParConfigParser(ConfigParser):
def __init__(self, **kwargs):
self.optionxform = lambda option:option # keep format with capital/lower letters
defaults = dict(comment_prefixes=('!', '/', '#'),
inline_comment_prefixes=('!'),
delimiters=('='))
Expand All @@ -133,5 +129,5 @@ def _write_section(self, fp, section_name, section_items, delimiter):
for key, value in section_items:
value = self._interpolation.before_write(self, section_name, key, value)
value = ' ' + str(value).replace('\n', '\n\t')
fp.write("{}{}\n".format(key.upper(), value))
fp.write("/\n")
fp.write("{}{}\n".format(key, value))
fp.write("\n")
24 changes: 11 additions & 13 deletions glofrim-py/glofrim/glofrim_pcr.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
# -*- coding: utf-8 -*-

import logging
from os.path import isdir, join, basename, dirname, abspath, isfile, isabs
import numpy as np
import rasterio

from pcrglobwb_bmi_v203 import pcrglobwb_bmi

from main import BMI_model_wrapper
from utils import ConfigParser

log_fmt = '%(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(level=logging.INFO, format=log_fmt, filemode='w')
logger = logging.getLogger(__name__)

class PCR_model(BMI_model_wrapper):
def __init__(self, config_fn,
Expand All @@ -21,6 +14,7 @@ def __init__(self, config_fn,
missing_value=-999, landmask_mv=255, forcing_data_dir=None,
**kwargs):
"""initialize the PCR-GLOBWB (PCR) model BMI class and model configuration file"""
from pcrglobwb_bmi_v203 import pcrglobwb_bmi
# BMIWrapper for PCR model model
pcr_bmi = pcrglobwb_bmi.pcrglobwbBMI()
# set config parser
Expand Down Expand Up @@ -60,7 +54,7 @@ def get_model_grid(self):
model_grid_shape : tuple
model number of rows and cols
"""
logger.info('Getting PCR model grid parameters.')
self.logger.info('Getting PCR model grid parameters.')
fn_map = join(self.model_config['globalOptions']['inputDir'],
self.model_config['globalOptions']['landmask'])
if not isfile(fn_map):
Expand All @@ -73,14 +67,14 @@ def get_model_grid(self):
self.model_grid_shape = ds.shape
self.model_grid_transform = ds.transform
msg = 'Model bounds {:s}; width {}, height {}'
logger.debug(msg.format(self.model_grid_bounds, *self.model_grid_shape))
self.logger.debug(msg.format(self.model_grid_bounds, *self.model_grid_shape))
pass

def get_drainage_direction(self):
from nb.nb_io import read_dd_pcraster
# read file with pcr readmap
nodata = self.options.get('landmask_mv', 255)
logger.info('Getting PCR LDD map.')
self.logger.info('Getting PCR LDD map.')
fn_ldd = self.model_config['routingOptions']['lddMap']
if not isabs(fn_ldd):
ddir = self.model_config['globalOptions']['inputDir']
Expand All @@ -105,14 +99,18 @@ def model_2d_index(self, xy, **kwargs):
"""
import pcraster as pcr
nodata = self.options.get('landmask_mv', 255)
logger.info('Getting PCR model indices of xy coordinates.')
self.logger.info('Getting PCR model indices of xy coordinates.')
r, c = self.grid_index(*zip(*xy), **kwargs)
r = np.array(r).astype(int)
c = np.array(c).astype(int)
# check if inside domain
nrows, ncols = self.model_grid_shape
inside = np.logical_and.reduce((r>=0, r<nrows, c>=0, c<ncols))
# get valid cells (inside landmask)
lm_map = pcr.readmap(str(self._fn_landmask))
lm_data = pcr.pcr2numpy(lm_map, nodata)
valid = lm_data[r, c] == 1
valid = np.zeros_like(inside, dtype=bool)
valid[inside] = lm_data[r[inside], c[inside]] == 1
return zip(r, c), valid

# TODO: this should be in the setting file. not changing the actual ldd
Expand All @@ -139,7 +137,7 @@ def deactivate_routing(self, coupled_indices=None):
msg = "Deactivating the LDD is only possible before the model is initialized"
raise AssertionError(msg)

logger.info('Editing PCR ldd grid to deactivate routing in coupled cells.')
self.logger.info('Editing PCR ldd grid to deactivate routing in coupled cells.')
# get ldd filename from config
fn_ldd = self.model_config['routingOptions']['lddMap']
if not isabs(fn_ldd):
Expand Down
19 changes: 6 additions & 13 deletions glofrim-py/glofrim/glofrim_wfl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@

#TODO: guess the input dir as in PCR has to be set up to work with path of the WFLOW ini-file instead

import logging
from os.path import isdir, join, basename, dirname, abspath, isfile, isabs
import numpy as np
import rasterio



from main import BMI_model_wrapper
from utils import ConfigParser

log_fmt = '%(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(level=logging.INFO, format=log_fmt, filemode='w')
logger = logging.getLogger(__name__)

class WFL_model(BMI_model_wrapper):

Expand Down Expand Up @@ -67,7 +60,7 @@ def get_model_grid(self):
model_grid_shape : tuple
model number of rows and cols
"""
logger.info('Getting WFLOW model grid parameters.')
self.logger.info('Getting WFLOW model grid parameters.')
fn_map = getattr(self.model_config['model'], 'wflow_subcatch', 'staticmaps/wflow_subcatch.map')
if not isabs(fn_map):
ddir = self.model_data_dir
Expand All @@ -82,14 +75,14 @@ def get_model_grid(self):
self.model_grid_shape = ds.shape
self.model_grid_transform = ds.transform
msg = 'Model bounds {:s}; width {}, height {}'
logger.debug(msg.format(self.model_grid_bounds, *self.model_grid_shape))
self.logger.debug(msg.format(self.model_grid_bounds, *self.model_grid_shape))
pass

def get_drainage_direction(self):
from nb.nb_io import read_dd_pcraster
# read file with pcr readmap
nodata = self.options.get('landmask_mv', 255)
logger.info('Getting WFLOW LDD map.')
self.logger.info('Getting WFLOW LDD map.')
fn_ldd = getattr(self.model_config['model'], 'wflow_ldd', 'staticmaps/wflow_ldd.map')
if not isabs(fn_ldd):
ddir = self.model_data_dir
Expand All @@ -113,7 +106,7 @@ def model_2d_index(self, xy, **kwargs):
indices : list of tuples
list of (row, col) index tuples
"""
logger.info('Getting WFLOW model indices of xy coordinates.')
self.logger.info('Getting WFLOW model indices of xy coordinates.')
r, c = self.grid_index(*zip(*xy), **kwargs)
r = np.array(r).astype(int)
c = np.array(c).astype(int)
Expand Down Expand Up @@ -163,11 +156,11 @@ def update(self, dt=None, **kwargs):
state variables.
"""
if dt is not None: # by default take internally set dt
logger.warning('dt is not used in the wflow bmi update function')
self.logger.warning('dt is not used in the wflow bmi update function')
self.bmi.update()
current_time = self.get_current_time()
time_step = self.get_time_step()
logger.info(
self.logger.info(
"%s -> start_time: %s, current_time %s, timestep %s",
self.name,
self.start_time,
Expand Down
Loading

0 comments on commit cfe8b90

Please sign in to comment.