Skip to content

Commit

Permalink
Merge 88fd00b into d6f062a
Browse files Browse the repository at this point in the history
  • Loading branch information
scotthavens committed Jul 1, 2020
2 parents d6f062a + 88fd00b commit a282efb
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 70 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
coloredlogs==6.1
coloredlogs
Cython>=0.28.4
inicheck>=0.9.0,<0.10.0
mysql-connector-python-rf==2.2.2
Expand Down
6 changes: 0 additions & 6 deletions smrf/distribute/air_temp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@

# import numpy as np
import logging

from smrf.distribute import image_data
from smrf.utils import utils

Expand Down Expand Up @@ -48,11 +44,9 @@ def __init__(self, taConfig):

# extend the base class
image_data.image_data.__init__(self, self.variable)
self._logger = logging.getLogger(__name__)

# check and assign the configuration
self.getConfig(taConfig)

self._logger.debug('Created distribute.air_temp')

def initialize(self, topo, data):
Expand Down
3 changes: 0 additions & 3 deletions smrf/distribute/albedo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import logging

import numpy as np

from smrf.distribute import image_data
Expand Down Expand Up @@ -61,7 +59,6 @@ def __init__(self, albedoConfig):

# extend the base class
image_data.image_data.__init__(self, self.variable)
self._logger = logging.getLogger(__name__)

# Get the veg values for the decay methods. Date method uses self.veg
# Hardy2000 uses self.litter
Expand Down
3 changes: 0 additions & 3 deletions smrf/distribute/cloud_factor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import logging

from smrf.distribute import image_data
from smrf.utils import utils

Expand Down Expand Up @@ -45,7 +43,6 @@ def __init__(self, config):

# extend the base class
image_data.image_data.__init__(self, self.variable)
self._logger = logging.getLogger(__name__)

# check and assign the configuration
self.getConfig(config)
Expand Down
2 changes: 1 addition & 1 deletion smrf/distribute/image_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, variable):

self.gridded = False

self._base_logger = logging.getLogger(__name__)
self._logger = logging.getLogger(self.__class__.__module__)

def getConfig(self, cfg):
"""
Expand Down
3 changes: 0 additions & 3 deletions smrf/distribute/precipitation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

import logging

import netCDF4 as nc
import numpy as np

Expand Down Expand Up @@ -106,7 +104,6 @@ def __init__(self, pptConfig, start_date, time_step=60):

# extend the base class
image_data.image_data.__init__(self, self.variable)
self._logger = logging.getLogger(__name__)

# check and assign the configuration
self.getConfig(pptConfig)
Expand Down
4 changes: 0 additions & 4 deletions smrf/distribute/soil_temp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

import logging

import numpy as np

from smrf.distribute import image_data
Expand Down Expand Up @@ -44,7 +41,6 @@ def __init__(self, soilConfig):

# extend the base class
image_data.image_data.__init__(self, self.variable)
self._logger = logging.getLogger(__name__)

self.config = soilConfig

Expand Down
3 changes: 0 additions & 3 deletions smrf/distribute/solar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import logging

from smrf.distribute import image_data
from smrf.envphys.constants import IR_WAVELENGTHS, VISIBLE_WAVELENGTHS
from smrf.envphys.solar import cloud, toporad, vegetation
Expand Down Expand Up @@ -211,7 +209,6 @@ def __init__(self, config, topo):

# extend the base class
image_data.image_data.__init__(self, self.variable)
self._logger = logging.getLogger(__name__)

self.config = config["solar"]
self.albedoConfig = config["albedo"]
Expand Down
3 changes: 0 additions & 3 deletions smrf/distribute/thermal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import logging

import numpy as np

from smrf.distribute import image_data
Expand Down Expand Up @@ -194,7 +192,6 @@ def __init__(self, thermalConfig):

# extend the base class
image_data.image_data.__init__(self, self.variable)
self._logger = logging.getLogger(__name__)
self.getConfig(thermalConfig)

self.min = thermalConfig['min']
Expand Down
3 changes: 0 additions & 3 deletions smrf/distribute/vapor_pressure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

import logging

import numpy as np

from smrf.distribute import image_data
Expand Down Expand Up @@ -67,7 +65,6 @@ def __init__(self, vpConfig, precip_temp_method):

# extend the base class
image_data.image_data.__init__(self, self.variable)
self._logger = logging.getLogger(__name__)

# check and assign the configuration
self.getConfig(vpConfig)
Expand Down
60 changes: 60 additions & 0 deletions smrf/framework/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import logging.config
import os

import coloredlogs


class SMRFLogger():
"""Setup the root logger for SMRF to either the console or
to a file
"""

FMT = '%(levelname)s:%(name)s:%(message)s'

def __init__(self, config):

self.log_level = config.get('log_level', 'info').upper()
self.log_file = config.get('log_file', None)

# https://docs.python.org/3/library/logging.config.html#configuration-dictionary-schema
log_config = {
'version': 1,
'formatters': {
'standard': {
'format': self.FMT
}
},
'handlers': {
'default': {
'level': self.log_level,
'formatter': 'standard',
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout', # Default is stderr
}
},
'loggers': {
'': { # root logger
'handlers': ['default'],
'level': self.log_level,
'propagate': False
}
}
}

if self.log_file is not None:
os.makedirs(os.path.dirname(self.log_file), exist_ok=True)

log_config['handlers']['log_file'] = {
'level': self.log_level,
'formatter': 'standard',
'class': 'logging.FileHandler',
'filename': self.log_file,
'mode': 'a'
}

log_config['loggers']['']['handlers'] = ['log_file']

logging.config.dictConfig(log_config)

if self.log_file is None:
coloredlogs.install(level=self.log_level, fmt=self.FMT)
42 changes: 4 additions & 38 deletions smrf/framework/model_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import os
import sys
from datetime import datetime
from os.path import abspath, dirname, join
from os.path import abspath, join
from threading import Thread

import coloredlogs
import numpy as np
import pandas as pd
import pytz
Expand All @@ -40,10 +39,10 @@
from topocalc.shade import shade

from smrf import data, distribute
from smrf.output import output_netcdf, output_hru
from smrf.framework import art
from smrf.envphys import sunang
from smrf.envphys.solar import model
from smrf.framework import art, logger
from smrf.output import output_hru, output_netcdf
from smrf.utils import queue
from smrf.utils.utils import backup_input, check_station_colocation, getqotw

Expand Down Expand Up @@ -118,40 +117,7 @@ def __init__(self, config, external_logger=None):
' UserConfig instance')
# start logging
if external_logger is None:

if 'log_level' in ucfg.cfg['system']:
loglevel = ucfg.cfg['system']['log_level'].upper()
else:
loglevel = 'INFO'

numeric_level = getattr(logging, loglevel, None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % loglevel)

# setup the logging
logfile = None
if ucfg.cfg['system']['log_file'] is not None:
logfile = ucfg.cfg['system']['log_file']
os.makedirs(dirname(logfile), exist_ok=True)

fmt = '%(levelname)s:%(name)s:%(message)s'
if logfile is not None:
# From the python3 docs on basicConfig
# "This function does nothing if the root logger already has
# handlers configured"
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)

logging.basicConfig(filename=logfile,
level=numeric_level,
filemode='a',
format=fmt)
else:
logging.basicConfig(level=numeric_level)
coloredlogs.install(level=numeric_level, fmt=fmt)

self._loglevel = numeric_level

self.smrf_logger = logger.SMRFLogger(ucfg.cfg['system'])
self._logger = logging.getLogger(__name__)
else:
self._logger = external_logger
Expand Down
2 changes: 1 addition & 1 deletion smrf/tests/smrf_test_case.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
import shutil
import unittest
import logging

import netCDF4 as nc
import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion smrf/tests/smrf_test_case_lakes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import os
import shutil
import logging

from inicheck.tools import get_user_config

Expand Down

0 comments on commit a282efb

Please sign in to comment.