Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify role of getAvailableInventoriesandYears #150

Open
dt-woods opened this issue Nov 20, 2023 · 5 comments
Open

Clarify role of getAvailableInventoriesandYears #150

dt-woods opened this issue Nov 20, 2023 · 5 comments
Milestone

Comments

@dt-woods
Copy link

The table in the README says RCRAInfo has 2013, 2017 and 2019 data available, but I don't see them with I run the following code snippet (using stewi 1.1.2). I only see 2015 data.

>>> import stewi 
>>> stewi.getAvailableInventoriesandYears()
{'TRI': ['2016', '2017'],
 'NEI': ['2016', '2017'],
 'eGRID': ['2016', '2018', '2020'],
 'RCRAInfo': ['2015']}

Are these just the data files that have been downloaded locally and not the ones available for download?

def getAvailableInventoriesandYears(stewiformat='flowbyfacility'):

@bl-young
Copy link
Collaborator

Are these just the data files that have been downloaded locally and not the ones available for download?

Correct. This function predates storage of data objects on data commons so does not serve all that much purpose anymore.

@dt-woods
Copy link
Author

dt-woods commented Nov 21, 2023 via email

@bl-young bl-young changed the title Mismatch of available inventories in getAvailableInventoriesandYears dictionary Clarify role of getAvailableInventoriesandYears Nov 27, 2023
@dt-woods
Copy link
Author

I wrote this to provide the functionality I was talking about above. You just need to update the global dictionary.

STEWI_DATA_VINTAGES = {
    'DMR': [x for x in range(2011, 2023, 1)],
    'GHGRP': [x for x in range(2011, 2023, 1)],
    'eGRID': [2014, 2016, 2018, 2019, 2020, 2021],
    'NEI': [2011, 2014, 2017, 2020],
    'RCRAInfo': [x for x in range(2011, 2023, 2)],
    'TRI': [x for x in range(2011, 2023, 1)],
}
'''dict : A dictionary of StEWI inventories and their available vintages.'''


def linear_search(lst, target):
    """Backwards search for the value less than or equal to a given value.

    Parameters
    ----------
    lst : list
        A list of numerically sorted data (lowest to highest).
    target : int, float
        A target value (e.g., year).

    Returns
    -------
    int
        The index of the search list associated with the value equal to or
        less than the target, else -1 for a target out-of-range (i.e., smaller than the smallest entry in the list).

    Examples
    --------
    >>> NEI_YEARS = [2011, 2014, 2017, 2020]
    >>> linear_search(NEI_YEARS, 2020)
    3
    >>> linear_search(NEI_YEARS, 2019)
    2
    >>> linear_search(NEI_YEARS, 2018)
    2
    >>> linear_search(NEI_YEARS, 2010)
    -1
    """
    for i in range(len(lst) - 1, -1, -1):
        if lst[i] <= target:
            return i
    return -1


def get_stewi_invent_years(year):
    """Helper function to return inventory years of interest from StEWI.
    See https://github.com/USEPA/standardizedinventories for inventory names
    and years.

    Parameters
    ----------
    year : int
        An inventory vintage (e.g., 2020).

    Returns
    -------
    dict
        A dictionary of inventory codes and their most recent year of
        data available (less than or equal to the year provided).

    Examples
    --------
    >>> get_stewi_invent_years(2019)

    """
    r_dict = {}
    for key in STEWI_DATA_VINTAGES.keys():
        avail_years = STEWI_DATA_VINTAGES[key]
        y_idx = linear_search(avail_years, year)
        if y_idx != -1:
            r_dict[key] = STEWI_DATA_VINTAGES[key][y_idx]
    return r_dict

@bl-young bl-young added this to the v1.2.0 milestone Jan 17, 2025
@bl-young
Copy link
Collaborator

@dt-woods it'd be great if you could do a PR of these new functions into develop. my suggestion might be to add get_stewi_invent_years() to stewi/init.py and linear_search() and STEWI_DATA_VINTAGES to stewi/globals.py

@dt-woods
Copy link
Author

dt-woods commented Jan 18, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants