Exemple #1
0
def sub_pp_lake_file(lake_file, new_lake_file, lake_info):
    ''' sub_pp_lake_file() - Copy the old lake file and replace the contents with
        those of the new model, and write out the new IWFM lake file

    Parameters
    ----------
    lake_file : str
        name of existing preprocessor node file
    
    new_lake_file : str
        name of submodel preprocessor node file
    
    lake_info : list
        info describing of each lake in the submodel

    Returns
    -------
    nothing

    '''
    import iwfm as iwfm

    lake_lines = open(
        lake_file).read().splitlines()  # open and read input file

    line_index = iwfm.skip_ahead(0, lake_lines, 0)  # skip comments
    lake_lines[line_index] = iwfm.pad_both(str(
        len(lake_info)), f=4, b=35) + ' '.join(
            lake_lines[line_index].split()[1:])

    line_index = iwfm.skip_ahead(line_index + 1, lake_lines, 0)

    new_lake_lines = lake_lines[:line_index]

    for i in range(0, len(lake_info)):
        new_lake_lines.append('\t' + '\t'.join(lake_info[i][0:4]) + '\t' +
                              str(lake_info[i][5][0]) + '\t' + lake_info[i][4])

        for j in range(1, len(lake_info[i][5])):
            new_lake_lines.append('\t\t\t\t\t' + str(lake_info[i][5][j]))

    new_lake_lines.append('')

    with open(new_lake_file, 'w') as outfile:
        outfile.write('\n'.join(new_lake_lines))

    return
Exemple #2
0
def sub_pp_node_file(node_file, new_node_file, node_list):
    ''' sub_pp_node_file() - Copy the original node file, replace the 
        contents with those of the new model, and write out the new file

    Parameters
    ----------
    node_file : str
        name of existing preprocessor node file
    
    new_node_file : str
        name of submodel preprocessor node file
    
    node_list : llist of ints
        list of submodel nodes

    Returns
    -------
    nothing

    '''
    import iwfm as iwfm

    node_lines = open(
        node_file).read().splitlines()  # open and read input file

    line_index = iwfm.skip_ahead(0, node_lines, 0)  # skip comments
    node_lines[line_index] = iwfm.pad_both(str(
        len(node_list)), f=4, b=35) + ' '.join(
            node_lines[line_index].split()[1:])

    line_index = iwfm.skip_ahead(line_index + 2, node_lines, 0)

    new_node_lines = node_lines[:line_index]

    for i in range(line_index, len(node_lines)):
        if int(node_lines[i].split()[0]) in node_list:
            new_node_lines.append(node_lines[i])
    new_node_lines.append('')

    with open(new_node_file, 'w') as outfile:
        outfile.write('\n'.join(new_node_lines))

    return
Exemple #3
0
def sub_pp_stream_file(
    stream_file,
    new_stream_file,
    snode_dict,
    reach_info,
    rattab_dict,
    rating_header,
    stream_aq,
):
    ''' sub_pp_stream_file() - Copy the original stream specification file 
        and replace the contents with those of the new model, and write
        out the new file

    Parameters
    ----------
    stream_file : str
        name of existing preprocessor node file
    
    new_stream_file : str
        name of submodel preprocessor node file
    
    snode_dict : ints
        dictionary of existing model stream nodes in submodel
    
    reach_info : str
        reach info line for reaches in submodel
    
    rattab_dict : dict
        rating tables for stream nodes in submodel
    
    rating_header : str
        header info for rating tables including factors
    
    stream_aq : str
        stream-aquifer section of stream preprocessor file

    Returns
    -------
    nothing

    '''
    import iwfm as iwfm

    stream_lines = open(stream_file).read().splitlines()

    stream_type = stream_lines[0][1:]

    line_index = iwfm.skip_ahead(0, stream_lines, 0)  # skip comments
    sub_stream_lines = stream_lines[:line_index]

    # -- number of stream reaches
    sub_stream_lines.append(
        iwfm.pad_both(str(len(reach_info)), f=4, b=35) +
        ' '.join(stream_lines[line_index].split()[1:]))

    # -- number of points in each rating table
    rattab_sns = [*rattab_dict]
    sub_stream_lines.append(
        iwfm.pad_both(str(len(rattab_dict[rattab_sns[0]])), f=4, b=35) +
        ' '.join(stream_lines[line_index].split()[1:]))

    # -- write stream reach and rating table information
    if stream_type == '4.0':
        # placeholder for add_streams_40()
        # sub_stream_lines = add_streams_40(sub_stream_lines, reach_info, snode_dict, rattab_dict, rating_header, stream_aq)
        exit_now(stream_type)
    elif stream_type == '4.1':
        # placeholder for add_streams_41()
        # sub_stream_lines = add_streams_41(sub_stream_lines, reach_info, snode_dict, rattab_dict, rating_header, stream_aq)
        exit_now(stream_type)
    elif stream_type == '4.2':
        sub_stream_lines = add_streams_42(
            sub_stream_lines,
            reach_info,
            snode_dict,
            rattab_dict,
            rating_header,
            stream_aq,
        )
    else:
        exit_now(stream_type)

    sub_stream_lines.append('')
    # -- write new stream specification file
    with open(new_stream_file, 'w') as outfile:
        outfile.write('\n'.join(sub_stream_lines))

    return
Exemple #4
0
def sub_pp_file(in_pp_file, pre_dict, pre_dict_new, has_lake=False):
    ''' sub_pp_file() - Copy the old preprocessor input file,
        replacing the file names with those of the new model,
        and write out the new file

    Parameters
    ----------
    in_pp_file : str
        name of existing preprocessor main input file
    
    pre_dict : dict
        dictionary of existing model preprocessor file names
    
    pre_dict_new : dict
        dictionary of submodel preprocessor file names
    
    has_lake : bool, default=False
        does the submodel have a lake file?

    Returns:
    nothing

    '''
    import iwfm as iwfm

    # -- read the preprocessor file into array pre_lines
    pre_lines = open(
        in_pp_file).read().splitlines()  # open and read input file

    # -- preproc output file
    line_index = iwfm.skip_ahead(0, pre_lines, 3)  # skip comments
    pre_lines[line_index] = iwfm.pad_both(
        pre_dict_new['preout'], f=4, b=53) + ' '.join(
            pre_lines[line_index].split()[1:])

    # -- element file
    line_index = iwfm.skip_ahead(line_index + 1, pre_lines, 0)
    pre_lines[line_index] = iwfm.pad_both(
        pre_dict_new['elem_file'], f=4, b=53) + ' '.join(
            pre_lines[line_index].split()[1:])

    # -- node file
    line_index = iwfm.skip_ahead(line_index + 1, pre_lines, 0)
    pre_lines[line_index] = iwfm.pad_both(
        pre_dict_new['node_file'], f=4, b=53) + ' '.join(
            pre_lines[line_index].split()[1:])

    # -- stratigraphy file
    line_index = iwfm.skip_ahead(line_index + 1, pre_lines, 0)
    pre_lines[line_index] = iwfm.pad_both(
        pre_dict_new['strat_file'], f=4, b=53) + ' '.join(
            pre_lines[line_index].split()[1:])

    # -- stream file
    line_index = iwfm.skip_ahead(line_index + 1, pre_lines, 0)
    pre_lines[line_index] = iwfm.pad_both(
        pre_dict_new['stream_file'], f=4, b=53) + ' '.join(
            pre_lines[line_index].split()[1:])

    # -- lake file
    line_index = iwfm.skip_ahead(line_index + 1, pre_lines, 0)
    if len(pre_dict['lake_file']) > 1 and has_lake:
        pre_lines[line_index] = iwfm.pad_both(
            pre_dict_new['lake_file'], f=4, b=53) + ' '.join(
                pre_lines[line_index].split()[1:])
    else:
        pre_lines[line_index] = (iwfm.pad_both(' ', f=4, b=53) + '/ ' +
                                 ' '.join(pre_lines[line_index].split()[1:]))

    pre_lines.append('')
    # -- write new preprocessor input file
    with open(pre_dict_new['prename'], 'w') as outfile:
        outfile.write('\n'.join(pre_lines))

    return
Exemple #5
0
def sub_pp_elem_file(elem_file, new_elem_file, elem_list, new_srs):
    ''' sub_pp_elem_file() - Copy the old element file and
        replace the contents with those of the new model,
        and write out the new file

    Parameters
    ----------
    elem_file : str
        name of existing preprocessor element file
    
    new_elem_file : str
        name of submodel preprocessor element file
    
    elem_list : list of ints
        list of submodel elements
    
    new_srs : list of ints
        list of submodel subregions

    Returns
    -------
    nothing

    '''
    import iwfm as iwfm

    comments = ['Cc*#']

    elems = []
    for e in elem_list:
        elems.append(e[0])

    elem_lines = open(
        elem_file).read().splitlines()  # open and read input file

    line_index = iwfm.skip_ahead(0, elem_lines, 0)  # skip comments
    elem_lines[line_index] = iwfm.pad_both(str(
        len(elem_list)), f=4, b=35) + ' '.join(
            elem_lines[line_index].split()[1:])

    line_index = iwfm.skip_ahead(line_index + 1, elem_lines, 0)
    elem_lines[line_index] = iwfm.pad_both(str(
        len(new_srs)), f=4, b=35) + ' '.join(
            elem_lines[line_index].split()[1:])

    line_index = iwfm.skip_ahead(line_index + 1, elem_lines, 0)

    for sr in range(0, len(new_srs)):
        elem_lines[line_index] = iwfm.pad_both(
            'Subregion ' + str(new_srs[sr]), f=4, b=25) + ' '.join(
                elem_lines[line_index].split()[2:])
        line_index = iwfm.skip_ahead(line_index + 1, elem_lines, 0)
    # remove the remaining 'Subregion' lines
    while elem_lines[line_index][0] not in comments:
        del elem_lines[line_index]

    line_index = iwfm.skip_ahead(line_index + 1, elem_lines, 0)

    new_elem_lines = elem_lines[:line_index]

    for i in range(line_index, len(elem_lines)):
        if int(elem_lines[i].split()[0]) in elems:
            new_elem_lines.append(elem_lines[i])

    new_elem_lines.append('')

    with open(new_elem_file, 'w') as outfile:
        outfile.write('\n'.join(new_elem_lines))

    return
Exemple #6
0
def sub_swhed_file(old_filename,
                   new_filename,
                   node_list,
                   snode_list,
                   verbose=False):
    '''sub_swhed_file() - Read original old small watershed file, determine
        which small watersheds are in the submodel, and write out a new file

    Parameters
    ----------
    old_filename : str
        name of existing nmodel small watersheds file
    
    new_filename : str
        name of new subnmodel small watersheds file

    node_list : ints
        list of existing model nodes in submodel
    
    snode_list : ints
        list of existing model stream nodes in submodel
    
    verbose : bool, default=False
        turn command-line output on or off

    Returns
    -------
    nothing

    '''
    import iwfm as iwfm

    swshwd_lines = open(old_filename).read().splitlines()

    line_index = iwfm.skip_ahead(0, swshwd_lines,
                                 2)  # skip factors and comments

    nsw_line, nsw = line_index, int(swshwd_lines[line_index].split()[0])

    line_index = iwfm.skip_ahead(line_index + 4, swshwd_lines, 0)

    sw_list = []
    for sw in range(0, nsw):  # small watershed descriptions
        change, line, items = 0, line_index, swshwd_lines[line_index].split()

        if int(items[4]
               ) in node_list:  # if IWB in submodel keep small watershed
            sw_list.append(int(items[0]))  # ID

            # if IWBTS not in submodel, replace with '0'
            if (int(items[2]) not in snode_list):
                change, items[2] = 1, '0'

            # check that each arc node is in the submodel
            for l in range(1, int(items[3])):
                line_index = iwfm.skip_ahead(line_index, swshwd_lines, 1)

                # remove this arc node and decrement nwb
                if (int(swshwd_lines[line_index].split()[0])
                        not in snode_list):
                    del swshwd_lines[line_index]
                    change, items[3] = 1, str(int(items[3]) - 1)
                    line_index -= 1

            if change:
                swshwd_lines[line] = '\t'.join([i for i in items])

        else:  # remove these lines
            for i in range(0, int(items[3])):
                del swshwd_lines[line_index]
            line_index -= 1
        line_index = iwfm.skip_ahead(line_index, swshwd_lines, 1)

    if verbose:
        print(f'  Submodel uses {len(sw_list)} of {nsw} small watersheds')

    # replace NSW
    swshwd_lines[nsw_line] = iwfm.pad_both(str(
        len(sw_list)), f=6, b=50) + ' '.join(
            swshwd_lines[nsw_line].split()[1:])

    line_index = iwfm.skip_ahead(line_index, swshwd_lines, 6)

    # remove root zone parameters for small watersheds outside submodel
    for sw in range(0, nsw):
        if int(swshwd_lines[line_index].split()
               [0]) not in node_list:  # remove the line
            del swshwd_lines[line_index]
        line_index = iwfm.skip_ahead(line_index, swshwd_lines, 1)

    line_index = iwfm.skip_ahead(line_index, swshwd_lines, 3)

    # remove aquifer parameters for small watersheds outside submodel
    for sw in range(0, nsw):
        if int(swshwd_lines[line_index].split()
               [0]) not in node_list:  # remove the line
            del swshwd_lines[line_index]
        line_index = iwfm.skip_ahead(line_index, swshwd_lines, 1)

    line_index = iwfm.skip_ahead(line_index, swshwd_lines, 1)

    # remove initial conditions for small watersheds outside submodel
    for sw in range(0, nsw):
        if int(swshwd_lines[line_index].split()
               [0]) not in node_list:  # remove the line
            del swshwd_lines[line_index]
        line_index = iwfm.skip_ahead(line_index, swshwd_lines, 0)

    swshwd_lines.append('')
    # -- write submodel small watersheds file
    with open(new_filename, 'w') as outfile:
        outfile.write('\n'.join(swshwd_lines))

    return