Exemple #1
0
def headall2dtw(heads_file, pre_file, output_root, verbose=False):
    ''' headall2dtw() - Reads IWFM HeadAll.out file, subtracts heads from
        land surface elevation, and writes out as a time series with
        one csv file for each layer

    Parameters
    ----------
    heads_file : str
        name of headall.out file 
    
    pre_file : str
        name of IWFM Preprocessor main input file
    
    output_root : str
        basename of output file
    
    verbose : bool, default=False
        True = command-line output on

    Returns
    -------
    nothing
    
    '''
    import numpy as np
    import os
    import iwfm as iwfm

    pre_path, pre_proc = os.path.split(pre_file)
    pre_dict, _ = iwfm.iwfm_read_preproc(pre_file)

    node_file = os.path.join(pre_path, pre_dict['node_file'])
    node_coords, node_list = iwfm.iwfm_read_nodes(node_file)

    strat_file = os.path.join(pre_path, pre_dict['strat_file'])
    strat, nlayers = iwfm.iwfm_read_strat(strat_file, node_coords)

    elevations = iwfm.iwfm_lse(strat)
    lse = np.asarray([i[1] for i in elevations])

    # -- get heads
    data, layers, dates, nodes = iwfm.headall_read(heads_file)
    heads = np.asarray(data)

    # -- calculate depth from land surface
    dtw = lse - heads

    # -- write to csv files
    iwfm.headall2csv(
        np.around(dtw, 3),
        layers,
        dates,
        nodes,
        output_root,
        verbose=verbose,
    )
    return
Exemple #2
0
def iwfm2shp(main_file, shape_name, verbose=False):
    ''' iwfm2shp() - Takes the IWFM model main preprocessor file name
        and a base name for output files, and create node, element, 
        stream node and stream reach shapefiles

    Parameters
    ----------
    main_file : str
        IWFM Preprocessor main file name
    
    shape_name : str
        output shapefiles base name
    
    verbose : bool, default=False
        True = command-line output on

    Returns
    -------
    nothing
    
    '''

    import os, sys
    import iwfm as iwfm
    import iwfm.gis as gis

    topdir = os.getcwd()

    pre_dict, have_lake = iwfm.iwfm_read_preproc(main_file)
    if verbose:
        print(f'  Read preprocessor file {main_file}')

    elem_ids, elem_nodes, elem_sub = iwfm.iwfm_read_elements(pre_dict['elem_file']) 
    if verbose:
        print(f'  Read nodes of {len(elem_nodes):,} elements from {pre_dict["elem_file"]}')

    node_coords, node_list = iwfm.iwfm_read_nodes(pre_dict['node_file'], factor = 1)
    if verbose:
        print(f'  Read coordinates of {len(node_coords):,} nodes from {pre_dict["node_file"]}')

    node_strat, nlayers = iwfm.iwfm_read_strat(pre_dict['strat_file'], node_coords)
    if verbose:
        print(f'  Read stratigraphy for {len(node_strat):,} nodes from {pre_dict["strat_file"]}')

    if have_lake:
        lake_elems, lakes = iwfm.iwfm_read_lake(pre_dict['lake_file'])  
        if verbose:
            if len(lakes) > 1:
                print(f'  Read info for {len(lakes):,} lakes from {pre_dict["lake_file"]}')
            elif len(lakes) == 1:
                print(f'  Read info for {len(lakes):,} lake from {pre_dict["lake_file"]}')
    else:
        lake_elems, lakes = 0, [0]
        if verbose:
            print('  No lakes file')

    reach_list, stnodes_dict, nsnodes, rating_dict = iwfm.iwfm_read_streams(pre_dict['stream_file'])
    if verbose:
        print(f'  Read info for {len(reach_list):,} stream reaches and {nsnodes:,} stream nodes from {pre_dict["stream_file"]}')

    if verbose:
        print(' ')

    # == Create element shapefile in default UTM 10N (EPSG 26910)
    gis.elem2shp(elem_nodes,node_coords,elem_sub,lake_elems,shape_name,verbose=verbose)

    # == Create node shapefile in default UTM 10N (EPSG 26910)
    gis.nodes2shp(node_coords, node_strat, nlayers, shape_name, verbose=verbose)

    # == Create stream node shapefile in default UTM 10N (EPSG 26910)
    gis.snodes2shp(nsnodes, stnodes_dict, node_coords, shape_name, verbose=verbose)

    # == Create stream reach shapefile in default UTM 10N (EPSG 26910)
    gis.reach2shp(reach_list, stnodes_dict, node_coords, shape_name, verbose=verbose)

    return 
Exemple #3
0
def iwfm_sub_preproc(in_pp_file,
                     elem_pairs_file,
                     out_base_name,
                     verbose=False):
    ''' iwfm_sub_preproc() - Read in the Preprocessor main file of a model and a list
    of element pairs for a submodel. Use existing model Elements, Nodes, Stream
    specification, Lake and Stratigraphy files to produce new preprocessor files for
    the submodel, and pickle files of submodel nodes, stream nodes and lakes

    Parameters
    ----------
    in_pp_file : str
        name of existing preprocessor main input file

    elem_pairs_file : str
        name of file listing elements of existing nmodel and submodel

    out_base_name : str
        root of submodel output file names

    verbose : bool, default=False
        turn command-line output on or off

    Returns
    -------
    pre_dict_new : dict
        dictionary of submodel preprocessor file names

    elem_list : ints
        list of existing model elements in submodel

    new_srs : ints
        list of existing model subregions in submodel

    elem_dict : dict
        dictionary existing model element to submodel element

    node_list : ints
        list of existing model nodes in submodel

    snode_dict : ints
        list of existing model stream nodes in submodel

    lake_info : list
        description of each lake in the submodel

    '''
    import iwfm as iwfm
    import pickle

    # -- get list of file names from preprocessor input file
    pre_dict, have_lake = iwfm.iwfm_read_preproc(in_pp_file)
    if verbose:
        print(f'  Read preprocessor file {in_pp_file}')

    # -- create list of new file names
    pre_dict_new = iwfm.new_pp_dict(out_base_name)

    # -- read submodel elements
    elem_list, new_srs, elem_dict, rev_elem_dict = iwfm.get_elem_list(
        elem_pairs_file)
    if verbose:
        print(f'  Read submodel element pairs file {elem_pairs_file}')

    # -- determine submodel nodes
    node_list = iwfm.sub_pp_nodes(pre_dict['elem_file'], elem_list)
    pickle.dump(node_list, open(out_base_name + '_nodes.bin',
                                'wb'))  # dump node_list to file
    if verbose:
        print('  Compiled list of submodel nodes')

    # -- determine submodel stream nodes
    reach_info, snode_dict, rattab_dict, rating_header, stream_aq = iwfm.sub_pp_streams(
        pre_dict['stream_file'], node_list)
    # dump snode_dict to file
    pickle.dump(snode_dict, open(out_base_name + '_snodes.bin', 'wb'))
    if verbose:
        print('  Compiled list of submodel stream nodes')

    # -- determine submodel lakes
    if have_lake:
        lake_info, have_lake = iwfm.sub_pp_lakes(pre_dict['lake_file'],
                                                 elem_list)
        # dump lake_info to file
        pickle.dump(lake_info, open(out_base_name + '_lakes.bin', 'wb'))
        if have_lake and verbose:
            print('  Compiled list of submodel lakes')

    if verbose:
        print(' ')

    # -- write new node file
    iwfm.sub_pp_node_file(pre_dict['node_file'], pre_dict_new['node_file'],
                          node_list)
    if verbose:
        print(f'  Wrote submodel node file {pre_dict_new["node_file"]}')

    # -- write new element file
    iwfm.sub_pp_elem_file(pre_dict['elem_file'], pre_dict_new['elem_file'],
                          elem_list, new_srs)
    if verbose:
        print(f'  Wrote submodel element file {pre_dict_new["elem_file"]}')

    # -- write new stratigraphy file
    iwfm.sub_pp_strat_file(pre_dict['strat_file'], pre_dict_new['strat_file'],
                           node_list)
    if verbose:
        print(
            f'  Wrote submodel stratigraphy file {pre_dict_new["strat_file"]}')

    # -- write new stream specification file
    iwfm.sub_pp_stream_file(
        pre_dict['stream_file'],
        pre_dict_new['stream_file'],
        snode_dict,
        reach_info,
        rattab_dict,
        rating_header,
        stream_aq,
    )
    if verbose:
        print(
            f'  Wrote submodel stream specification file {pre_dict_new["stream_file"]}'
        )

    # -- write new lake file
    if have_lake:
        iwfm.sub_pp_lake_file(pre_dict['lake_file'], pre_dict_new['lake_file'],
                              lake_info)
        if verbose:
            print(
                f'  Wrote submodel lake specification file {pre_dict_new["lake_file"]}'
            )

    # -- write new preprocesor main input file
    iwfm.sub_pp_file(in_pp_file, pre_dict, pre_dict_new, have_lake)
    if verbose:
        print(f'  Wrote submodel preprocessor file {pre_dict_new["prename"]}')

    return pre_dict_new, elem_list, new_srs, elem_dict, node_list, snode_dict, lake_info