Code Monkey home page Code Monkey logo

Comments (3)

jarq6c avatar jarq6c commented on June 23, 2024

This issue is related to #113

from hydrotools.

jarq6c avatar jarq6c commented on June 23, 2024

Parking this example here:

from hydrotools.nwis_client.iv import IVDataService
import numpy as np
import pint

def convert(values, from_units, to_units):
    """Convert scientific units of values.
    
    Parameters
    ==========
    values: npt.ArrayLike
        Values to convert
    from_units: pint.UnitRegistry.Quantity compatible object
        Original units of values.
    to_units: pint.UnitRegistry.Quantity compatible object
        Desired conversion units.
        
    Returns
    =======
    result: npt.ArrayLike
        Same shape as values containing converted values.
    """
    # Setup registry
    u = pint.UnitRegistry()

    # Pint Array
    quantities = u.Quantity(np.asarray(values), from_units)

    # Convert units
    return quantities.to(to_units).magnitude

def main():
    # Setup client
    client = IVDataService(value_time_label="value_time")

    # Retrieve some data
    df = client.get(
        sites="01013500",
        startDT="20211001T00:00",
        endDT="20211002T00:00"
    )

    # Convert to cubic meters per second
    df["value"] = convert(df["value"], "ft^3/s", "m^3/s")
    df["measurement_unit"] = "m^3/s"

    # Look at the data
    print(df.head())

if __name__ == "__main__":
    main()

Output

           value_time variable_name usgs_site_code measurement_unit      value qualifiers series
0 2021-10-01 00:00:00    streamflow       01013500            m^3/s  16.112286      ['P']      0
1 2021-10-01 00:15:00    streamflow       01013500            m^3/s  16.112286      ['P']      0
2 2021-10-01 00:30:00    streamflow       01013500            m^3/s  16.112286      ['P']      0
3 2021-10-01 00:45:00    streamflow       01013500            m^3/s  16.112286      ['P']      0
4 2021-10-01 01:00:00    streamflow       01013500            m^3/s  16.112286      ['P']      0

from hydrotools.

jarq6c avatar jarq6c commented on June 23, 2024

Minimal example to compute discharge per unit area (AKA total runoff)

# Set up registry
u = pint.UnitRegistry()

# Parameters
catchment_area = u.Quantity(25.0, "miles^2").to("ft^2")
period = u.Quantity(15.0, "minutes").to("s")
discharge = u.Quantity(500.0, "ft^3/s")

# Compute discharge per unit area
discharge_per_unit_area = discharge * period / catchment_area

# Display in inches
print(discharge_per_unit_area.to("inches"))

Output

0.007747933884297518 inch

from hydrotools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.