Esempio n. 1
0
def write_ecoflow_file_drainageareaxml(file_list, dir_name, file_name):
    """    
    Write a csv file containing a label (basin id number) and its corresponding area.

    Parameters
    ----------
    file_list : list
        List of WATERSimulation.xml files to process
    dir_name : string
        String name for output directory
    file_name : string
        String name for output file
    """
    area_data = {}
    for f in file_list:

        filedir, filename = helpers.get_file_info(f)

        ecoflow_dir = helpers.make_directory(path=filedir,
                                             directory_name=dir_name)

        helpers.print_input_output_info(
            input_dict={"input_file": f},
            output_dict={"output_directory": ecoflow_dir})

        waterapputils_logging.initialize_loggers(output_dir=ecoflow_dir)

        # read xml file
        waterxml_tree = waterxml.read_file(f)

        # get area from each region from the xml file and sum for a total area
        project, study, simulation = waterxml.get_xml_data(
            waterxml_tree=waterxml_tree)

        # get the project name which is the same as the stationid
        stationid = project["ProjName"]

        # get the area means for each region
        areas = waterxml.get_study_unit_areas(simulation_dict=simulation)

        # calculate total area
        total_area = waterxml.calc_total_study_unit_areas(areas)

        # fill area_data with total area
        area_data[stationid] = total_area

    # convert from km**2 to mi**2
    area_data = helpers.convert_area_values(area_data,
                                            in_units="km2",
                                            out_units="mi2")

    # write timeseries of dishcarge + water use for ecoflow program
    watertxt.write_drainagearea_file(area_data=area_data,
                                     save_path=ecoflow_dir,
                                     filename=file_name)

    waterapputils_logging.remove_loggers()
Esempio n. 2
0
def write_ecoflow_file_drainageareashp(file_list, dir_name, file_name,
                                       label_field, query_field):
    """    
    Write a csv file containing a label (basin id number) and its corresponding area.
    Two methods to get the area from each respective shapefile:

        1. if shapefile(s) has an area field and user specifies it in user_settings.py 
        under the *basin_shapefile_area_field* variable then get the area for each
        basin using the specified area field name (query_field)
        
        2. if shapefile(s) do not have an area field or user does not specify is in
        user_settings.py, then calculate it using osgeo and label each basin according
        to *basin_shapefile_id_field* in user_settings.py 

    Parameters
    ----------
    file_list : list
        List of files to process; files are shapefiles
    settings : dictionary
        Dictionary of user settings
    label_field : string
        String name of an id field (basin id number) to associate with a basin
    query_field : string
        String name of an area field

    Notes
    -----
    Uses settings set in user_settings.py  
    """
    for f in file_list:

        filedir, filename = helpers.get_file_info(f)

        ecoflow_dir = helpers.make_directory(path=filedir,
                                             directory_name=dir_name)

        waterapputils_logging.initialize_loggers(output_dir=ecoflow_dir)

        helpers.print_input_output_info(
            input_dict={"input_file": f},
            output_dict={"output_directory": ecoflow_dir})

        basin_shapefile = osgeo.ogr.Open(f)

        # get the areas for each region
        areas = spatialvectors.get_areas_dict(shapefile=basin_shapefile,
                                              id_field=label_field,
                                              query_field=query_field)

        # write timeseries of dishcarge + water use for ecoflow program
        watertxt.write_drainagearea_file(area_data=areas,
                                         save_path=ecoflow_dir,
                                         filename=file_name)

    waterapputils_logging.remove_loggers()
def write_ecoflow_file_drainageareaxml(file_list, dir_name, file_name):
    """    
    Write a csv file containing a label (basin id number) and its corresponding area.

    Parameters
    ----------
    file_list : list
        List of WATERSimulation.xml files to process
    dir_name : string
        String name for output directory
    file_name : string
        String name for output file
    """   
    area_data = {}
    for f in file_list:
               
        filedir, filename = helpers.get_file_info(f)       

        ecoflow_dir = helpers.make_directory(path = filedir, directory_name = dir_name)

        helpers.print_input_output_info(input_dict = {"input_file": f}, output_dict = {"output_directory": ecoflow_dir})

        waterapputils_logging.initialize_loggers(output_dir = ecoflow_dir) 

        # read xml file
        waterxml_tree = waterxml.read_file(f)       

        # get area from each region from the xml file and sum for a total area
        project, study, simulation = waterxml.get_xml_data(waterxml_tree = waterxml_tree)

        # get the project name which is the same as the stationid
        stationid = project["ProjName"]

        # get the area means for each region
        areas = waterxml.get_study_unit_areas(simulation_dict = simulation)

        # calculate total area
        total_area = waterxml.calc_total_study_unit_areas(areas)

        # fill area_data with total area
        area_data[stationid] = total_area

    # convert from km**2 to mi**2
    area_data = helpers.convert_area_values(area_data, in_units = "km2", out_units = "mi2")

    # write timeseries of dishcarge + water use for ecoflow program
    watertxt.write_drainagearea_file(area_data = area_data, save_path = ecoflow_dir, filename = file_name)
            
    waterapputils_logging.remove_loggers()
def write_ecoflow_file_drainageareashp(file_list, dir_name, file_name, label_field, query_field):
    """    
    Write a csv file containing a label (basin id number) and its corresponding area.
    Two methods to get the area from each respective shapefile:

        1. if shapefile(s) has an area field and user specifies it in user_settings.py 
        under the *basin_shapefile_area_field* variable then get the area for each
        basin using the specified area field name (query_field)
        
        2. if shapefile(s) do not have an area field or user does not specify is in
        user_settings.py, then calculate it using osgeo and label each basin according
        to *basin_shapefile_id_field* in user_settings.py 

    Parameters
    ----------
    file_list : list
        List of files to process; files are shapefiles
    settings : dictionary
        Dictionary of user settings
    label_field : string
        String name of an id field (basin id number) to associate with a basin
    query_field : string
        String name of an area field

    Notes
    -----
    Uses settings set in user_settings.py  
    """   
    for f in file_list:
               
        filedir, filename = helpers.get_file_info(f)       

        ecoflow_dir = helpers.make_directory(path = filedir, directory_name = dir_name)

        waterapputils_logging.initialize_loggers(output_dir = ecoflow_dir) 
 
        helpers.print_input_output_info(input_dict = {"input_file": f}, output_dict = {"output_directory": ecoflow_dir})

        basin_shapefile = osgeo.ogr.Open(f)  

        # get the areas for each region
        areas = spatialvectors.get_areas_dict(shapefile = basin_shapefile, id_field = label_field, query_field = query_field)

        # write timeseries of dishcarge + water use for ecoflow program
        watertxt.write_drainagearea_file(area_data = areas, save_path = ecoflow_dir, filename = file_name)
            
    waterapputils_logging.remove_loggers()
def apply_wateruse(settings):
    """    
    Apply water use data to WATER.txt file(s). 

    Parameters
    ----------
    settings : dictionary
        Dictionary of user settings                 

    Notes
    -----
    Uses settings set in user_settings.py  
    """

    # create output directories and files
    info_dir, ecoflow_dir, oasis_dir, info_file = create_output_dirs_files(
        settings)

    # initialize error logging in info_dir
    waterapputils_logging.initialize_loggers(output_dir=info_dir)

    # write all future print strings to the info_file
    sys.stdout = open(info_file, "w")

    # open shapefiles
    centroids_shapefile = osgeo.ogr.Open(
        settings["wateruse_centroids_shapefile"])
    basin_shapefile = osgeo.ogr.Open(
        os.path.join(settings["simulation_directory"],
                     settings["basin_shapefile_name"]))

    # find intersecting points (centroids) based on water basin supplied
    intersecting_centroids_all = spatialvectors.get_intersected_field_values(
        intersector=basin_shapefile,
        intersectee=centroids_shapefile,
        intersectee_field=settings["wateruse_centroids_shapefile_id_field"],
        intersector_field=settings["basin_shapefile_id_field"])

    intersecting_centroids, nonintersecting_centroids = spatialvectors.validate_field_values(
        field_values_dict=intersecting_centroids_all)

    # apply water use
    if intersecting_centroids:
        process_intersecting_centroids(intersecting_centroids, settings,
                                       ecoflow_dir, oasis_dir)

    # if no intersecting centroids, then warn the user and ask user to supply the water use points to a text file that will be contained in the info directory with a name specified in the user_settings.py file
    if nonintersecting_centroids:

        spatialvectors.write_field_values_file(
            filepath=info_dir,
            filename=settings["wateruse_non_intersecting_file_name"],
            field_values_dict=nonintersecting_centroids)

        warn_str = "The following basin(s) do not intersect with the water use centroids shapefile:\n    {}\n\n    centroids shapefile: {}\n    basin shapefile: {}\n".format(
            nonintersecting_centroids,
            settings["wateruse_centroids_shapefile"],
            os.path.join(settings["simulation_directory"],
                         settings["basin_shapefile_name"]))
        instruction_str1 = "Using water use centriod id: 000.  This special water use centroid id specifies 0 cubic feet per second water use.\n"
        instruction_str2 = "Writing the following wateruse non intersecting file that specifies the non intersecting basin(s) with the special water use centroid id:\n    {}\n".format(
            os.path.join(info_dir,
                         settings["wateruse_non_intersecting_file_name"]))
        instruction_str3 = "To apply water use to the non intersecting basin(s), add centroid(s) ids (newhydroid) (separated by commas) that you would like to use for each non intersecting basin to the wateruse non intersecting file."
        logging.warn("\n{}\n{}\n{}\n{}\n".format(warn_str, instruction_str1,
                                                 instruction_str2,
                                                 instruction_str3))

        # apply 0 cfs water use using the special centroid id of 000
        sub_intersecting_centroids = spatialvectors.read_field_values_file(
            filepath=os.path.join(
                info_dir, settings["wateruse_non_intersecting_file_name"]))

        # apply the wateruse
        process_intersecting_centroids(sub_intersecting_centroids, settings,
                                       ecoflow_dir, oasis_dir)

    # get the areas (in square miles) for each region
    areas = spatialvectors.get_areas_dict(
        shapefile=basin_shapefile,
        id_field=settings["basin_shapefile_id_field"],
        query_field=settings["basin_shapefile_area_field"])

    # write the drainage area csv file for ecoflow program
    watertxt.write_drainagearea_file(
        area_data=areas,
        save_path=ecoflow_dir,
        filename=settings["ecoflow_drainage_area_file_name"])

    # create map of study area
    map_processing.create_simulation_map(settings=settings)

    # remove error logger
    waterapputils_logging.remove_loggers()
def apply_wateruse(settings):
    """    
    Apply water use data to WATER.txt file(s). 

    Parameters
    ----------
    settings : dictionary
        Dictionary of user settings                 

    Notes
    -----
    Uses settings set in user_settings.py  
    """   

	# create output directories and files   
    info_dir, ecoflow_dir, oasis_dir, info_file = create_output_dirs_files(settings)

    # initialize error logging in info_dir
    waterapputils_logging.initialize_loggers(output_dir = info_dir) 

    # write all future print strings to the info_file
    sys.stdout = open(info_file, "w")  
    
    # open shapefiles
    centroids_shapefile = osgeo.ogr.Open(settings["wateruse_centroids_shapefile"]) 
    basin_shapefile = osgeo.ogr.Open(os.path.join(settings["simulation_directory"], settings["basin_shapefile_name"])) 

    # find intersecting points (centroids) based on water basin supplied
    intersecting_centroids_all = spatialvectors.get_intersected_field_values(intersector = basin_shapefile, intersectee = centroids_shapefile, intersectee_field = settings["wateruse_centroids_shapefile_id_field"], intersector_field = settings["basin_shapefile_id_field"])

    intersecting_centroids, nonintersecting_centroids = spatialvectors.validate_field_values(field_values_dict = intersecting_centroids_all)     

    # apply water use
    if intersecting_centroids:    
        process_intersecting_centroids(intersecting_centroids, settings, ecoflow_dir, oasis_dir)

    # if no intersecting centroids, then warn the user and ask user to supply the water use points to a text file that will be contained in the info directory with a name specified in the user_settings.py file
    if nonintersecting_centroids:
        
        spatialvectors.write_field_values_file(filepath = info_dir, filename = settings["wateruse_non_intersecting_file_name"], field_values_dict = nonintersecting_centroids)

        warn_str = "The following basin(s) do not intersect with the water use centroids shapefile:\n    {}\n\n    centroids shapefile: {}\n    basin shapefile: {}\n".format(nonintersecting_centroids, 
                                                                                                                                                                     settings["wateruse_centroids_shapefile"] , 
                                                                                                                                                                     os.path.join(settings["simulation_directory"], settings["basin_shapefile_name"]))
        instruction_str1 = "Using water use centriod id: 000.  This special water use centroid id specifies 0 cubic feet per second water use.\n"
        instruction_str2 = "Writing the following wateruse non intersecting file that specifies the non intersecting basin(s) with the special water use centroid id:\n    {}\n".format(os.path.join(info_dir, settings["wateruse_non_intersecting_file_name"]))
        instruction_str3 = "To apply water use to the non intersecting basin(s), add centroid(s) ids (newhydroid) (separated by commas) that you would like to use for each non intersecting basin to the wateruse non intersecting file."
        logging.warn("\n{}\n{}\n{}\n{}\n".format(warn_str, instruction_str1, instruction_str2, instruction_str3)) 
        
        # apply 0 cfs water use using the special centroid id of 000 
        sub_intersecting_centroids = spatialvectors.read_field_values_file(filepath = os.path.join(info_dir, settings["wateruse_non_intersecting_file_name"]))

        # apply the wateruse     
        process_intersecting_centroids(sub_intersecting_centroids, settings, ecoflow_dir, oasis_dir)        

    # get the areas (in square miles) for each region 
    areas = spatialvectors.get_areas_dict(shapefile = basin_shapefile, id_field = settings["basin_shapefile_id_field"], query_field = settings["basin_shapefile_area_field"])

    # write the drainage area csv file for ecoflow program
    watertxt.write_drainagearea_file(area_data = areas, save_path = ecoflow_dir, filename = settings["ecoflow_drainage_area_file_name"])

    # create map of study area
    map_processing.create_simulation_map(settings = settings)

    # remove error logger
    waterapputils_logging.remove_loggers()