Skip to content

Commit 4dff316

Browse files
added encoding
1 parent 19eb121 commit 4dff316

File tree

7 files changed

+112
-64
lines changed

7 files changed

+112
-64
lines changed

cmethods/CMethods.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
2-
# -*- coding:utf-8 -*-
3-
# Copyright (C) 2023 Benjamin Thomas Schwertfegerr
2+
# -*- coding: utf-8 -*-
3+
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
44
# Github: https://github.com/btschwertfeger
55
#
66

cmethods/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# -*- coding:utf-8 -*-
3-
# Copyright (C) 2023 Benjamin Thomas Schwertfegerr
2+
# -*- coding: utf-8 -*-
3+
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
44
# Github: https://github.com/btschwertfeger
55
#

examples/do_bias_correction.py

Lines changed: 101 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
#!/bin/python3
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
23

3-
__descrption__ = 'Script to adjust climate biases in 3D Climate data'
4-
__author__ = 'Benjamin Thomas Schwertfeger'
4+
__descrption__ = "Script to adjust climate biases in 3D Climate data"
5+
__author__ = "Benjamin Thomas Schwertfeger"
56
__copyright__ = __author__
6-
__email__ = '[email protected]'
7-
__link__ = 'https://b-schwertfeger.de'
8-
__github__ = 'https://github.com/btschwertfeger/Bias-Adjustment-Python';
7+
__email__ = "[email protected]"
8+
__link__ = "https://b-schwertfeger.de"
9+
__github__ = "https://github.com/btschwertfeger/Bias-Adjustment-Python"
910

1011
import argparse
11-
import logging, sys
12+
import logging
13+
import sys
14+
1215
import xarray as xr
1316

1417
from cmethods.CMethods import CMethods
1518

1619
# * ----- L O G G I N G -----
1720
formatter = logging.Formatter(
18-
fmt='%(asctime)s %(module)s,line: %(lineno)d %(levelname)8s | %(message)s',
19-
datefmt='%Y-%m-%d %H:%M:%S'
21+
fmt="%(asctime)s %(module)s,line: %(lineno)d %(levelname)8s | %(message)s",
22+
datefmt="%Y-%m-%d %H:%M:%S",
2023
)
2124

2225
log = logging.getLogger()
@@ -26,76 +29,121 @@
2629
logging.getLogger().addHandler(screen_handler)
2730

2831
# * ----- I N P U T - H A N D L I N G -----
29-
parser = argparse.ArgumentParser(description='Adjust climate data based on bias correction algorithms and magic.')
30-
parser.add_argument('--obs', '--observation', dest='obs_fpath', type=str, help='Observation dataset')
31-
parser.add_argument('--contr', '--control', dest='contr_fpath', type=str, help='Control dataset')
32-
parser.add_argument('--scen', '--scenario', dest='scen_fpath', type=str, help='Scenario dataset (data to adjust)')
32+
parser = argparse.ArgumentParser(
33+
description="Adjust climate data based on bias correction algorithms and magic."
34+
)
35+
parser.add_argument(
36+
"--obs", "--observation", dest="obs_fpath", type=str, help="Observation dataset"
37+
)
38+
parser.add_argument(
39+
"--contr", "--control", dest="contr_fpath", type=str, help="Control dataset"
40+
)
41+
parser.add_argument(
42+
"--scen",
43+
"--scenario",
44+
dest="scen_fpath",
45+
type=str,
46+
help="Scenario dataset (data to adjust)",
47+
)
3348

34-
parser.add_argument('-m', '--method', dest='method', type=str, help='Correction method')
35-
parser.add_argument('-v', '--variable', dest='var', type=str, default='tas', help='Variable to adjust')
36-
parser.add_argument('-u', '--unit', dest='unit', type=str, default='°C', help='Unit of the varible')
49+
parser.add_argument("-m", "--method", dest="method", type=str, help="Correction method")
50+
parser.add_argument(
51+
"-v", "--variable", dest="var", type=str, default="tas", help="Variable to adjust"
52+
)
53+
parser.add_argument(
54+
"-u", "--unit", dest="unit", type=str, default="°C", help="Unit of the varible"
55+
)
3756

38-
parser.add_argument('-g', '--group', dest='group', type=str, default=None, help='Value grouping, default: time, (options: time.month, time.dayofyear, time.year')
39-
parser.add_argument('-k', '--kind', dest='kind', type=str, default='+', help='+ or *, default: +')
40-
parser.add_argument('-n', '--nquantiles', dest='n_quantiles', type=int, default=100, help='Nr. of Quantiles to use')
57+
parser.add_argument(
58+
"-g",
59+
"--group",
60+
dest="group",
61+
type=str,
62+
default=None,
63+
help="Value grouping, default: time, (options: time.month, time.dayofyear, time.year",
64+
)
65+
parser.add_argument(
66+
"-k", "--kind", dest="kind", type=str, default="+", help="+ or *, default: +"
67+
)
68+
parser.add_argument(
69+
"-n",
70+
"--nquantiles",
71+
dest="n_quantiles",
72+
type=int,
73+
default=100,
74+
help="Nr. of Quantiles to use",
75+
)
4176

42-
parser.add_argument('-p', '--processes', dest='p', type=int, default=1, help='Multiprocessing with n processes, default: 1')
77+
parser.add_argument(
78+
"-p",
79+
"--processes",
80+
dest="p",
81+
type=int,
82+
default=1,
83+
help="Multiprocessing with n processes, default: 1",
84+
)
4385
params = vars(parser.parse_args())
4486

45-
obs_fpath = params['obs_fpath']
46-
contr_fpath = params['contr_fpath']
47-
scen_fpath = params['scen_fpath']
87+
obs_fpath = params["obs_fpath"]
88+
contr_fpath = params["contr_fpath"]
89+
scen_fpath = params["scen_fpath"]
90+
91+
method = params["method"]
92+
var = params["var"]
93+
unit = params["unit"]
94+
group = params["group"]
95+
kind = params["kind"]
96+
n_quantiles = params["n_quantiles"]
97+
n_jobs = params["p"]
4898

49-
method = params['method']
50-
var = params['var']
51-
unit = params['unit']
52-
group = params['group']
53-
kind = params['kind']
54-
n_quantiles = params['n_quantiles']
55-
n_jobs = params['p']
5699

57100
# * ----- ----- -----M A I N ----- ----- -----
58101
def main() -> None:
59102
cm = CMethods()
60103

61-
if method not in cm.get_available_methods(): raise ValueError(f'Unknown method {method}. Available methods: {cm.get_available_methods()}')
104+
if method not in cm.get_available_methods():
105+
raise ValueError(
106+
f"Unknown method {method}. Available methods: {cm.get_available_methods()}"
107+
)
62108

63109
ds_obs = xr.open_dataset(obs_fpath)[var]
64110
ds_simh = xr.open_dataset(contr_fpath)[var]
65111
ds_simp = xr.open_dataset(scen_fpath)[var]
66-
log.info('Data Loaded')
112+
log.info("Data Loaded")
67113

68-
ds_obs.attrs['unit'] = unit
69-
ds_simh.attrs['unit'] = unit
70-
ds_simp.attrs['unit'] = unit
114+
ds_obs.attrs["unit"] = unit
115+
ds_simh.attrs["unit"] = unit
116+
ds_simp.attrs["unit"] = unit
71117

72-
start_date: str = ds_simp['time'][0].dt.strftime('%Y%m%d').values.ravel()[0]
73-
end_date: str = ds_simp['time'][-1].dt.strftime('%Y%m%d').values.ravel()[0]
118+
start_date: str = ds_simp["time"][0].dt.strftime("%Y%m%d").values.ravel()[0]
119+
end_date: str = ds_simp["time"][-1].dt.strftime("%Y%m%d").values.ravel()[0]
74120

75-
descr1, descr2 = '', ''
121+
descr1, descr2 = "", ""
76122
if method in cm.DISTRIBUTION_METHODS:
77-
descr1 = f'_quantiles-{n_quantiles}'
123+
descr1 = f"_quantiles-{n_quantiles}"
78124

79125
# ----- Adjustment -----
80-
log.info(f'Starting {method} adjustment')
126+
log.info(f"Starting {method} adjustment")
81127
result = cm.adjust_3d(
82-
method = method,
83-
obs = ds_obs,
84-
simh = ds_simh,
85-
simp = ds_simp,
86-
n_quantiles = n_quantiles,
87-
kind = kind,
88-
group = group,
89-
n_jobs = n_jobs
128+
method=method,
129+
obs=ds_obs,
130+
simh=ds_simh,
131+
simp=ds_simp,
132+
n_quantiles=n_quantiles,
133+
kind=kind,
134+
group=group,
135+
n_jobs=n_jobs,
90136
)
91-
log.info('Saving now')
137+
log.info("Saving now")
92138
result.name = var
93-
result['time'] = ds_simp['time']
94-
result.to_netcdf(f'{method}_result_var-{var}{descr1}_kind-{kind}_group-{group}{descr2}_{start_date}_{end_date}.nc')
95-
log.info('Done')
139+
result["time"] = ds_simp["time"]
140+
result.to_netcdf(
141+
f"{method}_result_var-{var}{descr1}_kind-{kind}_group-{group}{descr2}_{start_date}_{end_date}.nc"
142+
)
143+
log.info("Done")
96144

97145

98-
if __name__ == '__main__':
146+
if __name__ == "__main__":
99147
main()
100148

101149

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#!/usr/bin/python3
2-
# -*- coding:utf-8 -*-
3-
# run via python3 setup.py upload
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
4+
# Github: https://github.com/btschwertfeger
45

56
import io
67
import os
@@ -9,7 +10,6 @@
910

1011
from setuptools import Command, find_packages, setup
1112

12-
# Package meta-data.
1313
NAME = "python-cmethods"
1414
DESCRIPTION = (
1515
"Collection of bias adjustment procedures for multidimensional climate data"

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# -*- coding:utf-8 -*-
2+
# -*- coding: utf-8 -*-
33
# Copyright (C) 2023 Benjamin Thomas Schwertfegerr
44
# Github: https://github.com/btschwertfeger
55
#

tests/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
scikit-learn
21
numpy
2+
scikit-learn
33
xarray

tests/test_methods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# -*- coding:utf-8 -*-
2+
# -*- coding: utf-8 -*-
33
# Copyright (C) 2023 Benjamin Thomas Schwertfegerr
44
# Github: https://github.com/btschwertfeger
55
#

0 commit comments

Comments
 (0)