<jupyter_start><jupyter_text>Develop this so that you can call USGS.IV(guage_number, start_date, end_date) to get results.1. Can set lat and long coordinates to grab all water gauges in an area. Collect all data, save and json&txt.2. Check USGS, NOAA, USACE, and National Weather Servicehttps://water.weather.gov/ahps2/hydrograph.php?wfo=lix&gage=bbsl1https://waterdata.usgs.gov/nwis/inventory/?site_no=07374578Noaa tides and currents2. check that results exist.3. Save as json and txt.  Scrape USGS Water Level Gauge APIAvailable APIs: https://waterservices.usgs.gov/rest/Map to USGS Water Level Gauges:https://maps.waterdata.usgs.gov/mapper/index.htmlGauge Elevation statement: https://waterdata.usgs.gov/wa/nwis/current/?type=datum DescriptionIn this notebook the user defines USGS stations to access for instantaneous or daily value water information. The result is a directory of TXT files with the requested information.<jupyter_code>import requests
import os
# Function to build API url. 

def check_dir(dir_path):
    # Check that file was created.
    if not os.path.exists(dir_path):
        print(dir_path)
        os.mkdir(dir_path)
        print("Directory did not exist and was created")

def build_gauge(website_url, Gauge):
    response = requests.get(website_url, params=Gauge)
    if response.status_code == 200:
        output = response.content.decode('utf-8')
        return output
    else:
        return response.status_code
    
def save_gauge(website_url, save_loc, Gauge, file_name):
    check_dir(save_loc)    
    file_path = os.path.join(save_loc, file_name)
    
    with open(file_path, 'w') as infile:
        infile.write(build_gauge(website_url, Gauge))
    return file_path<jupyter_output><empty_output><jupyter_text>Build Dictionary of Gauges*Note: One gauge per cell*<jupyter_code>Gauges = {}

# Set output defaults, can be adjusted for each gauge as necessary
agency = 'USGS'
start_date = '2018-01-01'
end_date = '2018-12-31'
fmt = 'json' # [rdb, json]
file_ext = '.json' # [.txt, .json]

# Gauge 1
gauge_key = 'Calcasieu River at Cameron Gauge'

gauge = {
    'site': '08017118', # Calcasieu River at Cameron Gauge
    'agencyCd': agency,
    'startDT': start_date,
    'endDT': end_date,
    'format': fmt, # Tab delimited format    
}

# Desired output type [(y)es or (n)o]
output = {'iv': 'y',
          'peak': 'y',
          'dv': 'y'}

Gauges[gauge_key]=[gauge, output]
# Gauge 2
gauge_key = 'Calcasieu River at Hackberry'

gauge = {
    'site': '08017095', # Calcasieu River at Cameron Gauge
    'agencyCd': agency,
    'startDT': start_date,
    'endDT': end_date,
    'format': fmt, # Tab delimited format    
}

# Desired output type [(y)es or (n)o]
output = {'iv': 'y',
          'peak': 'y',
          'dv': 'y'}

Gauges[gauge_key]=[gauge, output]
# Gauge 3
gauge_key = 'Bayou Bonfouca'

gauge = {
    'site': '07374578', # Calcasieu River at Cameron Gauge
    'agencyCd': agency,
    'startDT': start_date,
    'endDT': end_date,
    'format': fmt, # Tab delimited format    
}

# Desired output type [(y)es or (n)o]
output = {'iv': 'y',
          'peak': 'y',
          'dv': 'y'}

Gauges[gauge_key]=[gauge, output]
"""# Check Gauge has valid results

check_url = 'https://waterservices.usgs.gov/nwis/iv/?'
    
for gauge in Gauges:
    check = build_gauge(check_url, gauge)
    print(check)"""<jupyter_output>Calcasieu River at Cameron Gauge
400
Calcasieu River at Hackberry
400
Bayou Bonfouca
400<jupyter_text>Instantaneous Values (IV) Web ServiceAcquire near real-time water data. Readers are usually made very 15 minutes and transmitted hourly. First date available, typically, October 1, 2007.Note: Certain operation data are retricted to total time-frame of 120 days.<jupyter_code>iv_url = "http://waterservices.usgs.gov/nwis/iv/?"

for gauge in Gauges:
    save_loc = os.path.join(os.getcwd(), "iv")
    iv_save_file = gauge +'_iv' + file_ext 
    
    if Gauges[gauge][1]['iv'].lower() == 'y':      
        save_gauge(iv_url, save_loc, Gauges[gauge][0], iv_save_file)
    elif Gauges[gauge][1]['iv'].lower() != 'n':
        print("'Check that input for variable output['iv'] \
        is 'y' or 'n'")<jupyter_output><empty_output><jupyter_text>Daily Values (DV) Web ServiceDaily maximum, minimu, and mean values for each gauge recording.<jupyter_code>dv_url = "http://waterservices.usgs.gov/nwis/dv/?"

for gauge in Gauges:
    save_loc = os.path.join(os.getcwd(), "dv")
    dv_save_file = gauge +'_dv' + file_ext
    
    if Gauges[gauge][1]['dv'].lower() == 'y':
        save_gauge(dv_url, save_loc, Gauges[gauge][0], dv_save_file)
    elif Gauges[gauge][1]['dv'].lower() != 'n':
        print("'Check that input for variable output['dv'] \
        is 'y' or 'n'")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12964908594242489, 'fraction_numerical': 0.01807102332422778, 'mean_word_length': 3.8129423660262893, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '183838', 'license': 'MIT', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1647, 'n_words': 454, 'path': 'notebooks/USGS Water Service API.ipynb', 'repo_name': 'duke-moose/jupyter', 'size': '4759'}