Code Monkey home page Code Monkey logo

epicsa-climate-api's Introduction

E-PICSA Climate Api

API for accessing E-PICSA climate services

Build with FastAPI

Pre-Requisites

The api requires Python and R runtimes installed. It has been tested with the versions listed below

Relevant documentation should also be followed to ensure runtimes can be executed from PATH environment variable.

Configuration

Environment

Create an environment file from the sample. These will be populated using Pydantic

cp .env.sample .env

Python

The scripts below will create a python virtual environment, activate, install required python and R dependencies and start local server

=== "Windows (powershell)"

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install --upgrade -r requirements.txt
uvicorn app.main:app --reload

=== "Linux (bash)"

python -m venv .venv
source .venv/bin/activate
pip install --upgrade -r requirements.txt
uvicorn app.main:app --reload

R

Once installed you will need to call R from an elevated shell to install dependencies

Windows

Rscript install_packages.R
Rscript install_packages_picsa.R

Linux

sudo Rscript install_packages.R
sudo Rscript install_packages_picsa.R

Authorization File

In order to run the package, you will need to add the following service account file to the main repository folder (i.e. the folder where this README.md file is stored):

service-account.json

Running locally

Once installed, subsequent server starts can skip installation steps

=== "Windows (powershell)"

.\.venv\Scripts\Activate.ps1
uvicorn app.main:app --reload

=== "Linux (bash)"

source .venv/bin/activate
uvicorn app.main:app --reload

The server will start at http://127.0.0.1:8000

Running Tests

pytest

Running locally (docker)

docker compose up --build

Deployment

This repo contains example workflow to build as a docker image and deploy to google cloud run. See action yaml for details

Troubleshooting

Pip won't install dependencies Depending on local versions of R and python (as well as operating system) there may be issues when installing certain packages. Recommend attempting install using the requirements_dev.txt file which pins exact versions of packages shown to be compatible with each other, i.e.

pip install --upgrade -r requirements_dev.txt

R Dependency incompatibility Ensure R installed a per prerequisites, verify version via rscript --version.

If facing issues with a specific package it may help to download RStudio, and using the packages tab to check what version of packages are installed and update any indicated within error logs.

Called endpoint method does not exist The library calls methods from various other git repos where code is hosted both in python and R. These are installed during initial setup, but will need reinstallation whenever new versions of the external repos exist.

Simply repeat the steps above to install dependencies from install_packages_picsa.R

Any other issues should be raised on GitHub

License

This project is licensed under the terms of the MIT license.

epicsa-climate-api's People

Contributors

chrismarsh82 avatar chrismclarke avatar dannyparsons avatar dependabot[bot] avatar istride avatar lloyddewit avatar

epicsa-climate-api's Issues

In README.md file, 'fastapi run' command gives error

@chrismclarke The top-level README.md file contains the following Windows installation instructions:

    python -m venv .venv
    .\.venv\Scripts\Activate.ps1
    pip install -r requirements.txt
    fastapi run

The last line gives me an error.
Should it be something like uvicorn app.main:app --reload?
Or have I missed something?


FYI, the error was 'The term 'fastapi' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'.

I would normally start to investigate the path, but I remembered that for Open CDMS we ran uvicorn so I thought I'd check with you first.

Create endpoints for forecasts

Create endpoints to return short-term and long-term forecasts.

Not fully defined where these are stored or how many there will be so at the moment.

First draft should be a place holder that shows public accessible PDF

Bug(annual_rainfall_summaries): NA_character_ present in metadata

For some specific endpoint requests the metadata comes back with NA_character_ field which cannot be handled by the API.

E.g. using

{
  "country": "zm",
  "station_id": "16",
  "summaries": [
    "annual_rain",
    "start_rains",
    "end_rains",
    "end_season",
    "seasonal_rain",
    "seasonal_length"
  ]
}

Response metadata comes back as:

OrderedDict([('annual_rain', OrderedDict([('annual_rain', 'TRUE'), ('n_rain', 'TRUE'), ('na_rm', 'FALSE')])), ('start_rains', OrderedDict([('threshold', 1), ('s_start_doy', 120), ('start_day', 1), ('end_day', 366), ('total_rainfall', 'TRUE'), ('amount_rain', 25), ('over_days', 3), ('proportion', 'FALSE'), ('number_rain_days', 'FALSE'), ('dry_spell', 'TRUE'), ('spell_max_dry_days', 10), ('spell_interval', 21), ('dry_period', 'FALSE'), ('last_updated', '2022-11-25')])), ('end_rains', OrderedDict([('start_day', 121), ('end_day', 366), ('interval_length', 1), ('min_rainfall', 10), ('s_start_doy', 120)])), ('end_season', OrderedDict([('start_day', 121), ('end_day', 366), ('capacity', 100), ('water_balance_max', 60), ('evaporation', 'value'), ('evaporation_value', 5), ('s_start_doy', 120)])), ('seasonal_rain', OrderedDict([('seasonal_rain', 'TRUE'), ('end_type', 'rains'), ('total_rain', 'TRUE'), ('n_rain', 'TRUE'), ('na_rm', 'FALSE'), ('rain_day', 0.85)])), ('seasonal_length', OrderedDict([('end_type', 'rains')])), (NA_character_, OrderedDict([('start_day', 121), ('end_day', 366), ('interval_length', 1), ('min_rainfall', 10)]))])  

see end of response for OrderedDict([('end_type', 'rains')])), (NA_character_, OrderedDict([('start_day', 121)....

[Bug] Server uptime check fails randomly

The server contains an uptime check which ensures a service-account.json file exists as a quality-control check. This seems to somewhat randomly pass/fail, even when the file is present (see video below).

This only appears to happen when running in google cloud (can't reproducde locally)

E-PICSA.Climate.API.-.Swagger.UI.webm
@router.get("/")
def get_status():
    """
    Check server up and authorized to access data
    """
    checkServiceAccount()
    # TODO - add test within epicsa wrapper to check if R installed and correct version
    return 'Server Up'


def checkServiceAccount():
    # check for service-account.json file relative to project root
    serviceJsonPath = path.abspath('./service-account.json')
    if path.exists(serviceJsonPath):
        return True
    else:
        raise HTTPException(status_code=401,detail="service-account.json missing from local files. Endpoints will not be able to access climate data")

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.