Source code for pycif.plugins.models.wrfchem.utilities
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
From ctdas-wrf's wrchem utilities.
"""
import os
import sys
import shutil
import importlib
import glob
import logging
import subprocess
import tempfile
import copy
import datetime as dt
import netCDF4 as nc
import numpy as np
[docs]
class utilities(object):
"""Utilities for wrfchem model"""
def __init__(self):
pass
[docs]
@staticmethod
def check_out_err(process):
"""
Displays stdout and stderr, returns returncode of the process.
Only makes sense if stdout and stderr are directed to PIPE,
else process.communicate doesn't see them (I think).
If verbose, only display errors if there are any.
"""
# Get job messages
out, err = process.communicate()
# Print output
def to_str(str_or_bytestr):
""" If argument is of type str, return argument. If
argument is of type bytes, return decoded str"""
if isinstance(str_or_bytestr, str):
return str_or_bytestr
elif isinstance(str_or_bytestr, bytes):
return str(str_or_bytestr, 'utf-8')
else:
logging.error("type of argument is = %s. Don't know what to do.", type(out))
def write_output(out_err, log_fun):
if out_err is None:
log_fun("No output.")
elif isinstance(out_err, list):
for line in out_err:
log_fun(to_str(line.rstrip()))
else:
log_fun(to_str(out_err.rstrip()))
logging.debug("Subprocess output:")
write_output(out, logging.debug)
# Handle errors
if process.returncode != 0:
logging.error("subprocess error")
logging.error("Returncode: %s", str(process.returncode))
logging.error("Message:")
write_output(err, logging.error)
return process.returncode