<commit_before>#
# Copyright (c) 2014, <NAME> (<EMAIL>)
#
# For the Tri-state EPSCoR Track II WC-WAVE Project
#
"""
Tools for working with IPW binary data and running the iSNOBAL model
"""

import os
import logging
import pandas as pd
import numpy as np
import subprocess
import struct

from collections import namedtuple, defaultdict

#: IPW standard. assumed unchanging since they've been the same for 20 years
BAND_TYPE_LOC = 1
BAND_INDEX_LOC = 2

#: Container for ISNOBAL Global Band information
GlobalBand = namedtuple("GlobalBand", 'byteorder nLines nSamps nBands')

#: Check if a header is starting
IsHeaderStart = lambda headerLine: headerLine.split()[0] == "!<header>"

#: ISNOBAL variable names to be looked up to make dataframes and write metadata
VARNAME_DICT = \
    {
        'in': ["I_lw", "T_a", "e_a", "u", "T_g", "S_n"],
        'em': ["R_n", "H", "L_v_E", "G", "M", "delta_Q", "E_s", "melt",
               "ro_predict", "cc_s"],
        'snow': ["z_s", "rho", "m_s", "h2o", "T_s_0", "T_s_l", "T_s",
                 "z_s_l", "h2o_sat"]
    }

#: Convert number of bytes to struct package code for unsigned integer type
PACK_DICT = \
    {
        1: 'B',
        2: 'H',
        4: 'I'
    }


def isnobal(data_tstep=60, nsteps=8754, init_img="data/init.ipw",
            precip_file="data/ppt_desc", mask_file="tl2p5mask.ipw",
            input_prefix="in", output_frequency=1):
    """
    Wrapper for running the ISNOBAL
    (http://cgiss.boisestate.edu/~hpm/software/IPW/man1/isnobal.html) model.
    """
    isnobalcmd = "".join(["isnobal ",
                          " -t " + data_tstep,
                          " -n " + nsteps,
                          " -I " + init_img,
                          " -p " + precip_file,
                          " -m " + mask_file,
                          " -i " + input_prefix,
                          " -O " + output_frequency,
                          " -e em ",
                          " -s snow"])
    logging.debug("ISNOBAL shell command: " + isnobalcmd)

    output = subprocess.check_output(isnobalcmd, shell=True)

    logging.debug("ISNOBAL process output: " + output)

<commit_msg>updated isnobal to be more correct and allow for more flexible output directory formats<commit_after>#
# Copyright (c) 2014, <NAME> (<EMAIL>)
#
# For the Tri-state EPSCoR Track II WC-WAVE Project
#
"""
Tools for working with IPW binary data and running the iSNOBAL model
"""

import os
import logging
import pandas as pd
import numpy as np
import subprocess
import struct

from collections import namedtuple, defaultdict

#: IPW standard. assumed unchanging since they've been the same for 20 years
BAND_TYPE_LOC = 1
BAND_INDEX_LOC = 2

#: Container for ISNOBAL Global Band information
GlobalBand = namedtuple("GlobalBand", 'byteorder nLines nSamps nBands')

#: Check if a header is starting
IsHeaderStart = lambda headerLine: headerLine.split()[0] == "!<header>"

#: ISNOBAL variable names to be looked up to make dataframes and write metadata
VARNAME_DICT = \
    {
        'in': ["I_lw", "T_a", "e_a", "u", "T_g", "S_n"],
        'em': ["R_n", "H", "L_v_E", "G", "M", "delta_Q", "E_s", "melt",
               "ro_predict", "cc_s"],
        'snow': ["z_s", "rho", "m_s", "h2o", "T_s_0", "T_s_l", "T_s",
                 "z_s_l", "h2o_sat"]
    }

#: Convert number of bytes to struct package code for unsigned integer type
PACK_DICT = \
    {
        1: 'B',
        2: 'H',
        4: 'I'
    }


def isnobal(data_tstep=60, nsteps=8758, init_img="data/init.ipw",
            precip_file="data/ppt_desc", mask_file="data/tl2p5mask.ipw",
            input_prefix="data/inputs/in", output_frequency=1,
            em_prefix="data/outputs/em", snow_prefix="data/outputs/snow"):
    """
    Wrapper for running the ISNOBAL
    (http://cgiss.boisestate.edu/~hpm/software/IPW/man1/isnobal.html) model.
    """
    isnobalcmd = "".join(["isnobal ",
                          " -t " + str(data_tstep),
                          " -n " + str(nsteps),
                          " -I " + init_img,
                          " -p " + precip_file,
                          " -m " + mask_file,
                          " -i " + input_prefix,
                          " -O " + str(output_frequency),
                          " -e " + em_prefix,
                          " -s " + snow_prefix])
    logging.debug("ISNOBAL shell command: " + isnobalcmd)

    output = subprocess.check_output(isnobalcmd, shell=True)

    logging.debug("ISNOBAL process output: " + output)


{'fraction_non_alphanumeric': 0.13414364640883977, 'fraction_numerical': 0.011049723756906077, 'mean_word_length': 2.2282453637660486, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5288107', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1495, 'n_words': 401}