Skip to content

Commit 39c0e91

Browse files
authored
Merge pull request #9 from ie3-institute/df/#8-sql-coordinates-source
Sql coordinates source
2 parents 619e99d + 03ea639 commit 39c0e91

25 files changed

+2163
-218
lines changed

.github/workflows/simple-ci.yml renamed to .github/workflows/ci.yml

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- uses: actions/checkout@v3
1717
- uses: actions/setup-python@v4
1818
with:
19-
python-version: '3.10'
19+
python-version: '3.12'
2020
#----------------------------------------------
2121
# load pip cache if cache exists
2222
#----------------------------------------------
@@ -40,7 +40,7 @@ jobs:
4040
fail-fast: true
4141
matrix:
4242
os: [ "ubuntu-latest", "macos-latest" ]
43-
python-version: [ "3.10"]
43+
python-version: [ "3.12"]
4444
runs-on: ${{ matrix.os }}
4545
steps:
4646
#----------------------------------------------
@@ -54,13 +54,38 @@ jobs:
5454
with:
5555
python-version: ${{ matrix.python-version }}
5656
#----------------------------------------------
57+
# Install PostgreSQL dependencies
58+
#----------------------------------------------
59+
- name: Install PostgreSQL (Ubuntu)
60+
if: matrix.os == 'ubuntu-latest'
61+
run: |
62+
sudo apt-get update
63+
sudo apt-get install -y libpq-dev postgresql postgresql-contrib
64+
65+
- name: Install PostgreSQL (macOS)
66+
if: matrix.os == 'macos-latest'
67+
run: |
68+
brew install postgresql@14
69+
brew link --force postgresql@14
70+
export PATH="/usr/local/opt/postgresql@14/bin:$PATH"
71+
echo 'export PATH="/usr/local/opt/postgresql@14/bin:$PATH"' >> ~/.bash_profile
72+
73+
#----------------------------------------------
5774
# ----- install & configure poetry -----
5875
#----------------------------------------------
5976
- name: Install Poetry
6077
uses: snok/install-poetry@v1
6178
with:
6279
virtualenvs-create: true
6380
virtualenvs-in-project: true
81+
version: 1.6.1
82+
83+
#----------------------------------------------
84+
# Check Poetry version
85+
#----------------------------------------------
86+
- name: Check Poetry version
87+
run: poetry --version
88+
6489
#----------------------------------------------
6590
# load cached venv if cache exists
6691
#----------------------------------------------
@@ -75,12 +100,10 @@ jobs:
75100
#----------------------------------------------
76101
- name: Install dependencies
77102
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
78-
run: poetry install --no-interaction --no-root
79-
#----------------------------------------------
80-
# install your root project, if required
81-
#----------------------------------------------
82-
- name: Install library
83-
run: poetry install --no-interaction
103+
run: |
104+
# Try with --no-update, fallback if not supported
105+
poetry lock --no-update || poetry lock
106+
poetry install --no-root --no-interaction
84107
#----------------------------------------------
85108
# run test suite
86109
#----------------------------------------------

.idea/misc.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/python-template.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Changelog
2+
23
All notable changes to this project will be documented in this file.
34

45
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
@@ -8,10 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
89

910
### Added
1011
- Implementing auto-merge for dependabot PRs [#12](https://github.com/ie3-institute/copernicusWeather2psdmWeather/issues/12)
12+
- Initialisation of this converter [#1](https://github.com/ie3-institute/copernicusWeather2psdmWeather/issues/1)
1113

1214
### Changed
1315
- Disabled auto-merge for minor Dependabot updates [#20](https://github.com/ie3-institute/copernicusWeather2psdmWeather/issues/20)
1416

17+
### Removed
18+
1519
### Fixed
1620
- Fixed implementation of dependabot auto-merge feature [#14](https://github.com/ie3-institute/copernicusWeather2psdmWeather/issues/14)
1721

README.md

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,36 @@
1-
# python-template
1+
# copernicusWeather2psdmWeather
22

3-
Python template that includes all the basics for your new shiny project.
3+
Converts [copernicus.eu](https://cds.climate.copernicus.eu/) netCDF4 weather data to [PowerSystemDataModel](https://github.com/ie3-institute/PowerSystemDataModel) weather data format.
44

5-
## Setup
5+
## How To Use
66

7-
1. Open a new repository while using this template.
8-
This repository is marked as a template repository.
9-
Github offers you the convenient possibility to open a new repository from a such a template repository.
10-
See [here](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template) for details.
11-
2. Clone your new repository
12-
3. Rename the `python-project` package to your project name (this is where your source code should go).
13-
4. Install the dependencies (preferably within a fresh virtual environment)
14-
5. Run `pre-commit install`
15-
6. Enjoy!
7+
### Config File
8+
This tool can be used by giving parameters through config file `config.yaml`.
169

17-
## What's in here?
10+
Here the parameter of the PostGreSql-Database can be assigned:
11+
- `db_user`: Database username, e.g. `postgres`
12+
- `db_password`: Database password of username, e.g. `postgres`
13+
- `db_port`: Database port, e.g. `5432`
14+
- `db_name`: Name of the database, `westfalia_2023_03_24-2023_03_28`
15+
- `input_dir`: Location of the CDS input files, e.g. `input/samples`
16+
- `file_name_base`: First part of the input files, see comment below. e.g. `westfalia_2023_03_24-2023_03_28`
1817

19-
### Formatting
18+
So far we support the following format of input files:
19+
- netCDF4
2020

21-
To spare you the pain of manually taking care of formatting your code and establish some consistency (remember "äußere Ordnung führt zu innerer Ordnung") it includes [Black](https://black.readthedocs.io/en/stable/) for automated formatting.
22-
Execute `black .` in the terminal at the project's root directory to format your code.
21+
To start converter
22+
- Adapt config file
23+
- Setup PostGreSql database
24+
- Start `main.py`
2325

24-
### Testing
26+
### netCDF4 Input files
27+
Climate Data Store (CDS) provides the weather data in two files, which file names ends on '_accum.nc' and '_instant.nc'. Sample data can be found in `input/samples`.
2528

26-
To make sure your code actually does what you think it does!
27-
The template includes [pytest](https://docs.pytest.org/en/7.1.x/) for that purpose.
28-
Include the tests in the `tests` directory. Running `pytest` in the terminal at the project's root directory executes all tests and hopefully replaces hoping that your code works by knowing (at least so far as your actual test all relevant cases and boundary conditions).
29+
#### File name base
30+
Weather in netCFD format are provided by Copernicus in two files ending with `_accum.nc` and `_instant.nc`.
31+
This parameter allows to set the file_name_base.
32+
E.g. `weather_data_accum.nc` and `weather_data_instant.nc`. file_name_base would be `weather_data`.
2933

30-
### CI
31-
32-
To give you a slap on the wrist if you did not apply the formatter or your tests don't pass there is a [Github Actions](https://github.com/features/actions) workflow at `.github/workflows/simple-ci.yml`.
33-
After pushing to the remote repository it checks out your code, installs all the dependencies then runs `flake8` for any code style issues and `black --check` to check the formatting.
34-
Depending on how that went you will see yourself either confronted with a friendly green tick or rather unfriendly red cross at the code window in github (or the `Actions` section of the github repository.)
35-
36-
### Pre-Commit
37-
38-
To reduce slaps on the wrist there is [pre-commit](https://pre-commit.com/) having your back.
39-
It installs pre-commit hooks that `black` and `flake8` before the CI gets a chance to complain.
40-
The types of git hooks are configured in `.pre-commit-config.yaml`.
41-
To install the hooks run `pre-commit install` once in the terminal at the project's root directory.
42-
43-
### Dependabot
44-
45-
To keep the dust off your dependencies [Dependabot](https://github.blog/2020-06-01-keep-all-your-packages-up-to-date-with-dependabot/) checks for new versions once a day (according to current cofigurations) and opens Pull-Requests in case it finds any newer versions.
46-
The configuration can be changed at `.github/dependabot.yml`
47-
Keep in mind that dependency updates can break your code.
48-
You safeguard yourself against this by making sure you have all your code tested and the dependency pull request does not break any of them.
4934

5035
## Something Missing?
5136

config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
db_user: "postgres"
2+
db_port: 5432
3+
db_password: "postgres"
4+
db_name: "PLACEHOLDER_DB_NAME"
5+
input_dir: "input"
6+
#Weather in netCFD format are provided by Copernicus in two files ending with `_accum.nc` and `_instant.nc`.
7+
#This parameter allows to set the file_name_base.
8+
#E.g. `weather_data_accum.nc` and `weather_data_instant.nc`. file_name_base would be `weather_data`
9+
file_name_base: "WEATHER_DATA_INPUT_FILE_BASE_NAME"

coordinates/coordinates.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy as np
2+
from netCDF4 import Dataset
3+
from pypsdm.db.weather.models import Coordinate
4+
from sqlmodel import Session
5+
6+
7+
def create_coordinates_df(weather: Dataset, session: Session):
8+
lats = np.asarray(weather.variables["latitude"])
9+
lons = np.asarray(weather.variables["longitude"])
10+
11+
coordinates = []
12+
idx_to_id = {}
13+
14+
coord_id = 0
15+
for lat_idx, lat in enumerate(lats):
16+
for lon_idx, lon in enumerate(lons):
17+
coord = Coordinate.from_xy(coord_id, float(lon), float(lat))
18+
coordinates.append(coord)
19+
20+
idx_to_id[(lat_idx, lon_idx)] = coord_id
21+
coord_id += 1
22+
23+
session.add_all(coordinates)
24+
session.commit()
25+
26+
return idx_to_id

create_docker_db.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker run --name PLACEHOLDER_DB_NAME -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=PLACEHOLDER_DB_NAME -v C:\docker\PLACEHOLDER_DB_NAME/var/lib/postgresql/data -p 5432:5432 -d postgis/postgis:17-3.4
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)