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

v0.4.0 #57

Merged
merged 15 commits into from
Dec 4, 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
8 changes: 2 additions & 6 deletions .github/workflows/python-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
py-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
py-version: ['3.9', '3.10', '3.11', '3.12', '3.13']

steps:
- uses: actions/checkout@v3
Expand All @@ -49,11 +49,7 @@ jobs:
pip install pytest pytest-cov flake8

- name: Install package and dependencies
if: ${{ matrix.py-version == '3.7' || matrix.py-version == '3.8' }}
run: pip install .

- name: Install package and dependencies
if: ${{ !(matrix.py-version == '3.7' || matrix.py-version == '3.8') }}
# if: ${{ !(matrix.py-version == '3.7' || matrix.py-version == '3.8') }}
run: pip install .[bib]

# linting & pytest
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
global-include *.yaml
graft esupy/data_census
graft esupy/data
2 changes: 1 addition & 1 deletion esupy/context_secondary.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def classify_height(df):
(df['StackHeight'] < 150*m_to_ft), # ~492 ft
(df['StackHeight'] >= 150*m_to_ft)]
cmpt = ['unspecified', 'ground', 'low', 'high', 'very high']
df['cmpt_rh'] = np.select(cond, cmpt)
df['cmpt_rh'] = np.select(cond, cmpt, default='unspecified')
return df

def urb_intersect(df_pt, year):
Expand Down
250 changes: 250 additions & 0 deletions esupy/data/ISO_3166.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
English short name,Alpha-2 code,Alpha-3 code
Afghanistan,AF,AFG
Albania,AL,ALB
Algeria,DZ,DZA
American Samoa,AS,ASM
Andorra,AD,AND
Angola,AO,AGO
Anguilla,AI,AIA
Antarctica,AQ,ATA
Antigua and Barbuda,AG,ATG
Argentina,AR,ARG
Armenia,AM,ARM
Aruba,AW,ABW
Australia,AU,AUS
Austria,AT,AUT
Azerbaijan,AZ,AZE
Bahamas (the),BS,BHS
Bahrain,BH,BHR
Bangladesh,BD,BGD
Barbados,BB,BRB
Belarus,BY,BLR
Belgium,BE,BEL
Belize,BZ,BLZ
Benin,BJ,BEN
Bermuda,BM,BMU
Bhutan,BT,BTN
Bolivia (Plurinational State of),BO,BOL
"Bonaire, Sint Eustatius and Saba",BQ,BES
Bosnia and Herzegovina,BA,BIH
Botswana,BW,BWA
Bouvet Island,BV,BVT
Brazil,BR,BRA
British Indian Ocean Territory (the),IO,IOT
Brunei Darussalam,BN,BRN
Bulgaria,BG,BGR
Burkina Faso,BF,BFA
Burundi,BI,BDI
Cabo Verde,CV,CPV
Cambodia,KH,KHM
Cameroon,CM,CMR
Canada,CA,CAN
Cayman Islands (the),KY,CYM
Central African Republic (the),CF,CAF
Chad,TD,TCD
Chile,CL,CHL
China,CN,CHN
Christmas Island,CX,CXR
Cocos (Keeling) Islands (the),CC,CCK
Colombia,CO,COL
Comoros (the),KM,COM
Congo (the Democratic Republic of the),CD,COD
Congo (the),CG,COG
Cook Islands (the),CK,COK
Costa Rica,CR,CRI
Croatia,HR,HRV
Cuba,CU,CUB
Curaçao,CW,CUW
Cyprus,CY,CYP
Czechia,CZ,CZE
Côte d'Ivoire,CI,CIV
Denmark,DK,DNK
Djibouti,DJ,DJI
Dominica,DM,DMA
Dominican Republic (the),DO,DOM
Ecuador,EC,ECU
Egypt,EG,EGY
El Salvador,SV,SLV
Equatorial Guinea,GQ,GNQ
Eritrea,ER,ERI
Estonia,EE,EST
Eswatini,SZ,SWZ
Ethiopia,ET,ETH
Falkland Islands (the) [Malvinas],FK,FLK
Faroe Islands (the),FO,FRO
Fiji,FJ,FJI
Finland,FI,FIN
France,FR,FRA
French Guiana,GF,GUF
French Polynesia,PF,PYF
French Southern Territories (the),TF,ATF
Gabon,GA,GAB
Gambia (the),GM,GMB
Georgia,GE,GEO
Germany,DE,DEU
Ghana,GH,GHA
Gibraltar,GI,GIB
Greece,GR,GRC
Greenland,GL,GRL
Grenada,GD,GRD
Guadeloupe,GP,GLP
Guam,GU,GUM
Guatemala,GT,GTM
Guernsey,GG,GGY
Guinea,GN,GIN
Guinea-Bissau,GW,GNB
Guyana,GY,GUY
Haiti,HT,HTI
Heard Island and McDonald Islands,HM,HMD
Holy See (the),VA,VAT
Honduras,HN,HND
Hong Kong,HK,HKG
Hungary,HU,HUN
Iceland,IS,ISL
India,IN,IND
Indonesia,ID,IDN
Iran (Islamic Republic of),IR,IRN
Iraq,IQ,IRQ
Ireland,IE,IRL
Isle of Man,IM,IMN
Israel,IL,ISR
Italy,IT,ITA
Jamaica,JM,JAM
Japan,JP,JPN
Jersey,JE,JEY
Jordan,JO,JOR
Kazakhstan,KZ,KAZ
Kenya,KE,KEN
Kiribati,KI,KIR
Korea (the Democratic People's Republic of),KP,PRK
Korea (the Republic of),KR,KOR
Kuwait,KW,KWT
Kyrgyzstan,KG,KGZ
Lao People's Democratic Republic (the),LA,LAO
Latvia,LV,LVA
Lebanon,LB,LBN
Lesotho,LS,LSO
Liberia,LR,LBR
Libya,LY,LBY
Liechtenstein,LI,LIE
Lithuania,LT,LTU
Luxembourg,LU,LUX
Macao,MO,MAC
Madagascar,MG,MDG
Malawi,MW,MWI
Malaysia,MY,MYS
Maldives,MV,MDV
Mali,ML,MLI
Malta,MT,MLT
Marshall Islands (the),MH,MHL
Martinique,MQ,MTQ
Mauritania,MR,MRT
Mauritius,MU,MUS
Mayotte,YT,MYT
Mexico,MX,MEX
Micronesia (Federated States of),FM,FSM
Moldova (the Republic of),MD,MDA
Monaco,MC,MCO
Mongolia,MN,MNG
Montenegro,ME,MNE
Montserrat,MS,MSR
Morocco,MA,MAR
Mozambique,MZ,MOZ
Myanmar,MM,MMR
Namibia,NA,NAM
Nauru,NR,NRU
Nepal,NP,NPL
Netherlands (Kingdom of the),NL,NLD
New Caledonia,NC,NCL
New Zealand,NZ,NZL
Nicaragua,NI,NIC
Niger (the),NE,NER
Nigeria,NG,NGA
Niue,NU,NIU
Norfolk Island,NF,NFK
North Macedonia,MK,MKD
Northern Mariana Islands (the),MP,MNP
Norway,NO,NOR
Oman,OM,OMN
Pakistan,PK,PAK
Palau,PW,PLW
"Palestine, State of",PS,PSE
Panama,PA,PAN
Papua New Guinea,PG,PNG
Paraguay,PY,PRY
Peru,PE,PER
Philippines (the),PH,PHL
Pitcairn,PN,PCN
Poland,PL,POL
Portugal,PT,PRT
Puerto Rico,PR,PRI
Qatar,QA,QAT
Romania,RO,ROU
Russian Federation (the),RU,RUS
Rwanda,RW,RWA
Réunion,RE,REU
Saint Barthélemy,BL,BLM
"Saint Helena, Ascension and Tristan da Cunha",SH,SHN
Saint Kitts and Nevis,KN,KNA
Saint Lucia,LC,LCA
Saint Martin (French part),MF,MAF
Saint Pierre and Miquelon,PM,SPM
Saint Vincent and the Grenadines,VC,VCT
Samoa,WS,WSM
San Marino,SM,SMR
Sao Tome and Principe,ST,STP
Saudi Arabia,SA,SAU
Senegal,SN,SEN
Serbia,RS,SRB
Seychelles,SC,SYC
Sierra Leone,SL,SLE
Singapore,SG,SGP
Sint Maarten (Dutch part),SX,SXM
Slovakia,SK,SVK
Slovenia,SI,SVN
Solomon Islands,SB,SLB
Somalia,SO,SOM
South Africa,ZA,ZAF
South Georgia and the South Sandwich Islands,GS,SGS
South Sudan,SS,SSD
Spain,ES,ESP
Sri Lanka,LK,LKA
Sudan (the),SD,SDN
Suriname,SR,SUR
Svalbard and Jan Mayen,SJ,SJM
Sweden,SE,SWE
Switzerland,CH,CHE
Syrian Arab Republic (the),SY,SYR
Taiwan (Province of China),TW,TWN
Tajikistan,TJ,TJK
"Tanzania, the United Republic of",TZ,TZA
Thailand,TH,THA
Timor-Leste,TL,TLS
Togo,TG,TGO
Tokelau,TK,TKL
Tonga,TO,TON
Trinidad and Tobago,TT,TTO
Tunisia,TN,TUN
Turkmenistan,TM,TKM
Turks and Caicos Islands (the),TC,TCA
Tuvalu,TV,TUV
Türkiye,TR,TUR
Uganda,UG,UGA
Ukraine,UA,UKR
United Arab Emirates (the),AE,ARE
United Kingdom of Great Britain and Northern Ireland (the),GB,GBR
United States Minor Outlying Islands (the),UM,UMI
United States of America (the),US,USA
Uruguay,UY,URY
Uzbekistan,UZ,UZB
Vanuatu,VU,VUT
Venezuela (Bolivarian Republic of),VE,VEN
Viet Nam,VN,VNM
Virgin Islands (British),VG,VGB
Virgin Islands (U.S.),VI,VIR
Wallis and Futuna,WF,WLF
Western Sahara*,EH,ESH
Yemen,YE,YEM
Zambia,ZM,ZMB
Zimbabwe,ZW,ZWE
Åland Islands,AX,ALA
96 changes: 96 additions & 0 deletions esupy/location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# location.py (esupy)
# !/usr/bin/env python3
# coding=utf-8
"""
Functions to facilitate creation of location objects
"""

import bz2
import json
import pandas as pd
from pathlib import Path

from esupy.remote import make_url_request

path = Path(__file__).parent

# %% get GeoJSON
url = 'https://geography.ecoinvent.org/files'
# https://geography.ecoinvent.org/#id12

location_dict = {'states': 'states.geojson.bz2',
'countries': 'countries.geojson.bz2',
'us_electricity': ['usa-electricity.geojson.bz2',
'electricity.geojson.bz2']}

def extract_coordinates(group) -> dict:
"""
Creates a dictinary of locations, where the key is the location code
and the values are 'geometry': geoJSON coordinates and 'properties': dict
"""
file = location_dict.get(group)
if file is None:
raise KeyError(f'Must select group from available '
f'keys: {list(location_dict.keys())}')
if isinstance(file, str):
file = [file]
features = []
for f in file:
response = make_url_request(f'{url}/{f}')
content = bz2.decompress(response.content)
data = json.loads(content)
# extract GeoJSON objects from the FeatureCollection
features.extend(data['features'])

## need to also grab the UUID?
if group in ('states', 'us_electricity'):
d = {f['properties']['shortname']: {'geometry': f['geometry'],
'properties': f['properties']}
for f in features
if f['properties']['shortname'].startswith('US')}
else:
d = {f['properties']['shortname']: {'geometry': f['geometry'],
'properties': f['properties']}
for f in features}
return d

def olca_location_meta():
location_meta = ('https://raw.githubusercontent.com/GreenDelta/'
'data/master/refdata/locations.csv')
df = pd.read_csv(location_meta)
return df


def assign_state_abbrev(df):
"""
Replaces state FIPS with state abbreviations, e.g., "US-AL" in the
"Location" column. Also assigns "00000" to "US".
Requires flowsa
"""
try:
import flowsa
except ImportError:
raise ImportError("assigning state abbreviations currently requires "
"flowsa. Install via pip install "
"git+https://github.com/USEPA/flowsa.git#egg=flowsa")
f = flowsa.location.get_state_FIPS(abbrev=True).drop(columns='County')
f['State'] = f['State'].apply(lambda x: f"US-{x}")
fd = f.set_index('FIPS').to_dict()['State']
fd['00000'] = 'US'
df['Location'] = df['Location'].replace(fd)
return df.dropna(subset='Location')


def read_iso_3166():
# accessed from the ISO online browing platform
# https://www.iso.org/obp/ui/#search
df = pd.read_csv(path / 'data' / 'ISO_3166.csv')
df = df.rename(columns={'English short name': 'Name',
'Alpha-2 code': 'ISO-2d',
'Alpha-3 code': 'ISO-3d'})
return df


if __name__ == "__main__":
# d = extract_coordinates(group='states')
d = extract_coordinates(group='countries')
Loading
Loading