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

URL testing #122

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lciafmt/data/methods.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"ReCiPe 2016 - Midpoint/E":"ReCiPe 2016 v1.1 midpoint method, Egalitarian version. The typical ReCiPe midpoint method used is the Hierarchist version."},
"path": "recipe",
"case_insensitivity": "True",
"url": "http://www.rivm.nl/sites/default/files/2018-11/ReCiPe2016_CFs_v1.1_20180117.xlsx",
"url": "https://www.rivm.nl/sites/default/files/2024-10/ReCiPe2016_CFs_v1.1_20180117.xlsx",
"bib_id": "huijbregts_recipe_2017",
"citation": "Huijbregts 2017",
"source_type": "Excel file"
Expand All @@ -49,7 +49,7 @@
"ImpactWorld+ - Endpoint":"This method includes all global endpoint flows and their attributes."},
"path": "impactworld",
"case_insensitivity": "False",
"url": "http://www.impactworldplus.org/en/writeToFile.php",
"url": "https://www.dropbox.com/sh/2sdgbqf08yn91bc/AABIGLlb_OwfNy6oMMDZNrm0a/IWplus_public_v1.3.accdb?dl=1",
"citation": "Bulle, Cecile, Manuele Margni, Laure Patouillard, Anne-Marie Boulay, Guillaume Bourgault, Vincent De Bruille, Viet Cao, et al. IMPACT World+: A Globally Regionalized Life Cycle Impact Assessment Method. The International Journal of Life Cycle Assessment 24, no. 9 (September 2019), 1653–74. https://doi.org/10.1007/s11367-019-01583-0",
"source_type": "Access file"
},
Expand Down
14 changes: 9 additions & 5 deletions lciafmt/iw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import pandas as pd
import lciafmt
import lciafmt.cache as cache
import lciafmt.df as dfutil
from lciafmt.util import log, format_cas
Expand Down Expand Up @@ -38,13 +39,10 @@ def get(file=None, url=None, region=None) -> pd.DataFrame:
"Please install drivers to remotely connect to Access Database. "
"Drivers only available on windows platform. For instructions visit: "
"https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-Microsoft-Access")

method_meta = lciafmt.Method.ImpactWorld.get_metadata()
f = file
if f is None:
fname = "Impact_World.accdb"
if url is None:
url = "https://www.dropbox.com/sh/2sdgbqf08yn91bc/AABIGLlb_OwfNy6oMMDZNrm0a/IWplus_public_v1.3.accdb?dl=1"
f = cache.get_or_download(fname, url)
f = _get_file(method_meta, url)
df = _read(f, region)

# Identify midpoint and endpoint records and differentiate in data frame.
Expand All @@ -58,6 +56,12 @@ def get(file=None, url=None, region=None) -> pd.DataFrame:

return df

def _get_file(method_meta, url=None):
fname = "Impact_World.accdb"
if url is None:
url = method_meta['url']
f = cache.get_or_download(fname, url)
return f

def _read(access_file: str, region) -> pd.DataFrame:
"""Read the Access database at passed access_file into DataFrame."""
Expand Down
16 changes: 11 additions & 5 deletions lciafmt/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pandas as pd
import openpyxl

import lciafmt
import lciafmt.cache as cache
import lciafmt.df as dfutil
import lciafmt.xls as xls
Expand Down Expand Up @@ -52,13 +53,10 @@ def get(add_factors_for_missing_contexts=True, endpoint=True,
:return: DataFrame of method in standard format
"""
log.info("getting method ReCiPe 2016")
method_meta = lciafmt.Method.RECIPE_2016.get_metadata()
f = file
if f is None:
fname = "recipe_2016.xlsx"
if url is None:
url = ("http://www.rivm.nl/sites/default/files/2018-11/" +
"ReCiPe2016_CFs_v1.1_20180117.xlsx")
f = cache.get_or_download(fname, url)
f = _get_file(method_meta, url)
df = _read(f)
if add_factors_for_missing_contexts:
log.info("adding average factors for primary contexts")
Expand Down Expand Up @@ -135,6 +133,14 @@ def get(add_factors_for_missing_contexts=True, endpoint=True,
return df


def _get_file(method_meta, url=None):
fname = "recipe_2016.xlsx"
if url is None:
url = method_meta['url']
f = cache.get_or_download(fname, url)
return f


def _read(file: str) -> pd.DataFrame:
log.info(f"read ReCiPe 2016 from file {file}")
wb = openpyxl.load_workbook(file, read_only=True, data_only=True)
Expand Down
11 changes: 7 additions & 4 deletions lciafmt/traci.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ def get(add_factors_for_missing_contexts=True, file=None,
method_meta = lciafmt.Method.TRACI.get_metadata()
f = file
if f is None:
fname = "traci_2.1.xlsx"
if url is None:
url = method_meta['url']
f = cache.get_or_download(fname, url)
f = _get_file(method_meta, url)
df = _read(f)
if add_factors_for_missing_contexts:
log.info("adding average factors for primary contexts")
Expand Down Expand Up @@ -72,6 +69,12 @@ def get(add_factors_for_missing_contexts=True, file=None,

return df

def _get_file(method_meta, url=None):
fname = "traci_2.1.xlsx"
if url is None:
url = method_meta['url']
f = cache.get_or_download(fname, url)
return f

def _read(xls_file: str) -> pd.DataFrame:
"""Read the data from Excel with given path into a DataFrame."""
Expand Down
7 changes: 7 additions & 0 deletions tests/test_generate_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def test_generate_methods():
assert not error_list


def test_url_access():
import lciafmt.iw as impactworld
f = lciafmt.recipe._get_file(lciafmt.Method.RECIPE_2016.get_metadata())
f = lciafmt.traci._get_file(lciafmt.Method.TRACI.get_metadata())
f = impactworld._get_file(lciafmt.Method.ImpactWorld.get_metadata())

def test_endpoint_method():
method = lciafmt.generate_endpoints('Weidema_valuation',
name='Weidema Valuation',
Expand Down Expand Up @@ -63,4 +69,5 @@ def test_compilation_method():
if __name__ == "__main__":
# test_generate_methods()
# test_method_write_json()
# test_url_access()
test_compilation_method()
Loading