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

Add tests #6

Merged
merged 26 commits into from
May 9, 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
15 changes: 15 additions & 0 deletions .copyright.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Copyright ${years} ${owner}.

This file is part of ${projectname}.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
50 changes: 50 additions & 0 deletions .github/actions/setup/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: 'venv setup'
description: 'configures venv, python and caches'

inputs:
python-version:
description: "Python version to use"
required: true
cache-dependency-path:
description: "Requirement files to install. Can be a glob pattern."
default: '**/requirements*.txt'
venv-dir:
default: '.venv'

outputs:
cache-hit:
description: "A boolean value to indicate if a cache was restored"
value: ${{ steps.cache-venv.outputs.cache-hit }}

runs:
using: 'composite'
steps:
- run: cp .env.example .env
shell: bash

- uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
id: setup-python
with:
python-version: ${{ inputs.python-version }}

- run: echo '::remove-matcher owner=python::'
shell: bash

- uses: actions/cache@13aacd865c20de90d75de3b17ebe84f7a17d57d2 # v4.0.0
id: cache-venv
with:
path: ${{ inputs.venv-dir }}
key: setup-venv-${{ runner.os }}-py-${{ steps.setup-python.outputs.python-version }}-${{ steps.setup-python.outputs.python-path }}-${{ hashFiles(inputs.cache-dependency-path) }}-${{ inputs.install-cmd }}

- run: python3 -m venv ${{ inputs.venv-dir }}
if: steps.cache-venv.outputs.cache-hit != 'true'
shell: bash

- run: |
source ${{ inputs.venv-dir }}/bin/activate
python -m pip install -r requirements.txt
python -m pip install -r requirements-dev.txt
echo "VIRTUAL_ENV=${VIRTUAL_ENV}" >> $GITHUB_ENV
echo "${VIRTUAL_ENV}/bin" >> $GITHUB_PATH

shell: bash
29 changes: 29 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on:
pull_request:
push:
branches: [main]
tags: ['v*']

jobs:

tests:
strategy:
fail-fast: false
matrix:
os: [ "ubuntu-latest" ]
python-version: [ "3.10" ]
runs-on: "${{ matrix.os }}"
steps:
- name: Check out repository
uses: actions/checkout@v3
- uses: ./.github/actions/setup
with:
python-version: "${{ matrix.python-version }}"
venv-id: "tests-${{ runner.os }}"
- name: Run tests
run: |
pytest -r a -v tests
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ inputs
intermediates
experiments
.env
.idea

!outputs/README.md
!inputs/README.md
!intermediates/README.md
.vscode/launch.json
.vscode/settings.json
domain.ipynb

# Virtual environment
venv
.venv
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: update-licenseheaders
update-licenseheaders: ## add or update license headers in all python files
licenseheaders --current-year --owner "The Superpower Institute Ltd" --projname "OpenMethane" --tmpl .copyright.tmpl --ext .py -x "venv/*"
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ Copy the `.env.example` file to `.env` and customise the paths as you need.

### Installation

To avoid conflicts with other Python projects, it is recommended to create a virtual environment for this project. To do this, run:


```console
python -m venv venv
source venv/bin/activate
```

Install required modules:

```console
Expand Down
17 changes: 17 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2024 The Superpower Institute Ltd.
#
# This file is part of OpenMethane.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
26 changes: 19 additions & 7 deletions omAgLulucfWasteEmis.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
"""
omAgLulucfWasteEmis.py
#
# Copyright 2024 The Superpower Institute Ltd.
#
# This file is part of OpenMethane.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Copyright 2023 The Superpower Institute Ltd
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
Process livestock methane emissions
"""

import csv
Expand Down
26 changes: 19 additions & 7 deletions omCreateDomainInfo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
"""
omCreateDomainInfo.py
#
# Copyright 2024 The Superpower Institute Ltd.
#
# This file is part of OpenMethane.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Copyright 2023 The Superpower Institute Ltd
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
Generate domain file from example domain
"""

from omInputs import domainPath, geomFilePath, croFilePath, dotFilePath
Expand Down
61 changes: 44 additions & 17 deletions omDownloadInputs.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
#
# Copyright 2024 The Superpower Institute Ltd.
#
# This file is part of OpenMethane.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
omDownloadInputs.py
Download required input files

Copyright 2023 The Superpower Institute Ltd
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
This downloads the input files that rarely change and can be cached between runs.
"""

from omInputs import electricityPath, fugitivesPath, landUsePath, sectoralEmissionsPath, sectoralMappingsPath, ntlPath, auShapefilePath, livestockDataPath, termitePath, wetlandPath, coalPath, oilGasPath
from omInputs import (electricityPath,
# fugitivesPath,
landUsePath, sectoralEmissionsPath, sectoralMappingsPath, ntlPath, auShapefilePath, livestockDataPath, termitePath, wetlandPath, coalPath, oilGasPath)
import requests
import os
from omUtils import getenv

root_path = os.path.dirname(os.path.realpath(__file__))

remote = getenv("PRIOR_REMOTE")

electricityFile = getenv("CH4_ELECTRICITY")
Expand All @@ -27,6 +45,7 @@
wetlandFile = getenv("WETLANDS")
coalFile = getenv("COAL")
oilGasFile = getenv("OILGAS")

downloads = [
[electricityFile, electricityPath],
[coalFile, coalPath],
Expand All @@ -41,15 +60,23 @@
[wetlandFile, wetlandPath]
]

for filename, filepath in downloads:
url = f"{remote}{filename}"
def download_input_files(root_path, downloads, remote):
for filename, filepath in downloads:
filepath = os.path.join(root_path, filepath)
print(filepath)
url = f"{remote}{filename}"

if not os.path.exists(filepath):
print(f"Downloading {filename} to {filepath} from {url}")

if not os.path.exists(filepath):
print(f"Downloading {filename} to {filepath} from {url}")
with requests.get(url, stream=True) as response:
with open(filepath, mode="wb") as file:
for chunk in response.iter_content(chunk_size=10 * 1024):
file.write(chunk)
else:
print(f"Skipping {filename} beacuse it already exists at {filepath}")

with requests.get(url, stream=True) as response:
with open(filepath, mode="wb") as file:
for chunk in response.iter_content(chunk_size=10 * 1024):
file.write(chunk)
else:
print(f"Skipping {filename} beacuse it already exists at {filepath}")
if __name__ == '__main__':
download_input_files(root_path=root_path,
downloads=downloads,
remote=remote)
26 changes: 19 additions & 7 deletions omElectricityEmis.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
"""
omEelctricityEmis.py
#
# Copyright 2024 The Superpower Institute Ltd.
#
# This file is part of OpenMethane.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Copyright 2023 The Superpower Institute Ltd
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
Process emissions from the electricity sector
"""

import numpy as np
Expand Down
26 changes: 19 additions & 7 deletions omFugitiveEmis.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
"""
omFugitiveEmis.py
#
# Copyright 2024 The Superpower Institute Ltd.
#
# This file is part of OpenMethane.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Copyright 2023 The Superpower Institute Ltd
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
Process fugitive Methane emissions
"""

import numpy as np
Expand Down
28 changes: 22 additions & 6 deletions omGFASEmis.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
#
# Copyright 2024 The Superpower Institute Ltd.
#
# This file is part of OpenMethane.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
omGFASEmis.py
Download and process GFAS data

Copyright 2023 The Superpower Institute Ltd
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
This downloads files from [ADS](https://atmosphere.copernicus.eu/data).
See the project readme for more information about configuring
the required credentials.
"""

import numpy as np
Expand Down
Loading
Loading