From 09ba68ae80b304e6b00b099b77f54591f08a9212 Mon Sep 17 00:00:00 2001
From: k204229 <lucio-eceiza@dkrz.de>
Date: Fri, 28 Mar 2025 15:56:12 +0100
Subject: [PATCH] add: installation, simple tools for conversion and check

---
 .gitignore       | 17 +++++++++++++++++
 Makefile         | 25 +++++++++++++++++++++++++
 setup.py         | 25 +++++++++++++++++++++++++
 src/__init__.py  |  0
 src/converter.py | 19 +++++++++++++++++++
 src/validator.py | 10 ++++++++++
 6 files changed, 96 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Makefile
 create mode 100644 setup.py
 create mode 100644 src/__init__.py
 create mode 100644 src/converter.py
 create mode 100644 src/validator.py

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0678e48
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,17 @@
+*.DS*
+.vscode
+.idea
+.ipynb_checkpoints
+*/.ipynb_checkpoints
+*/*/.ipynb_checkpoints
+.vscode
+*.log
+.idea*
+conda_env/
+**/*.sw[pco]
+**/*.*.sw[pco]
+**/.ipynb_checkpoints*
+**/*.py[co]
+**/*~
+**/._README.md
+**/*egg*
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..6007016
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,25 @@
+# makefile used for testing
+#
+#
+ENV_NAME=era5-tables
+.PHONY: all conda install test
+
+all: install test
+
+conda:
+	conda create -n $(ENV_NAME) python=3.11
+
+install: conda
+	@# python3 -m pip install -e .[dev]
+	conda run -n $(ENV_NAME) python -m pip install -e .[dev]
+
+check:
+	conda activate $(ENV_NAME); cfchecks $(file)
+
+clean:
+	conda remove -n $(ENV_NAME) --all -y
+
+lint:
+	mypy --install-types --non-interactive
+	black --check -t py311 -l 79 src
+	flake8 src/rechunk_data --count --max-complexity=15 --max-line-length=88 --statistics --doctests
\ No newline at end of file
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..ea16802
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,25 @@
+from setuptools import setup, find_packages
+
+setup(
+    name='era5-tables',
+    version='0.1',
+    packages=find_packages(where='src'),
+    package_dir={'': 'src'},
+    install_requires=[
+        'pandas>=2.0.0',
+        'openpyxl>=3.1.0',
+        'cfchecker>=4.1.0',
+        'netCDF4>=1.6.0',
+        'numpy>=1.20.0'
+    ],
+    extras_require={
+        'dev': [
+            'pytest',
+            'mypy',
+            'black',
+            'flake8',
+            'ipython'
+        ]
+    },
+    python_requires='>=3.9',
+)
diff --git a/src/__init__.py b/src/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/converter.py b/src/converter.py
new file mode 100644
index 0000000..803128d
--- /dev/null
+++ b/src/converter.py
@@ -0,0 +1,19 @@
+import pandas as pd
+import json
+import csv
+from pathlib import Path
+
+def excel_to_csv(excel_path, csv_path, sheet_name=0, field_separator="|"):
+    df = pd.read_excel(excel_path, sheet_name=sheet_name)
+    df.to_csv(csv_path, sep=field_separator, index=False)
+    return csv_path
+
+def csv_to_json(csv_path, json_path):
+    with open(csv_path, encoding='utf-8') as csvf:
+        csv_reader = csv.DictReader(csvf)
+        data = [row for row in csv_reader]
+
+    with open(json_path, 'w', encoding='utf-8') as jsonf:
+        json.dump(data, jsonf, indent=2)
+    
+    return json_path
diff --git a/src/validator.py b/src/validator.py
new file mode 100644
index 0000000..760b7e6
--- /dev/null
+++ b/src/validator.py
@@ -0,0 +1,10 @@
+import subprocess
+import sys
+
+def cf_check(nc_file):
+    try:
+        result = subprocess.run(['cfchecks', nc_file], capture_output=True, text=True, check=True)
+        print(result.stdout)
+    except subprocess.CalledProcessError as e:
+        print("CF checker error:", e.stderr, file=sys.stderr)
+        sys.exit(1)
-- 
GitLab