n97fit 1.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
n97fit-1.0.1/AUTHORS ADDED
@@ -0,0 +1,9 @@
1
+ The following people contributed to the code:
2
+
3
+ Takakiyo Nakazawa - Center for Atmospheric and Oceanic Studies (CAOS), Tohoku University
4
+
5
+ Misa Ishizawa - Environment and Climate Change Canada
6
+
7
+ Taku Umezawa - National Institute for Environmental Studies (NIES)
8
+
9
+ Hisashi Yashiro - National Institute for Environmental Studies (NIES)
n97fit-1.0.1/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 1991-2024, n97fit developers
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright notice,
7
+ this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright notice,
9
+ this list of conditions and the following disclaimer in the documentation
10
+ and/or other materials provided with the distribution.
11
+ * Neither the name of the copyright holder nor the names of its contributors
12
+ may be used to endorse or promote products derived from this software
13
+ without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+
@@ -0,0 +1 @@
1
+ include AUTHORS
n97fit-1.0.1/PKG-INFO ADDED
@@ -0,0 +1,181 @@
1
+ Metadata-Version: 2.1
2
+ Name: n97fit
3
+ Version: 1.0.1
4
+ Summary: This is a program of data selection and curve fitting by a digital filtering technique including Reinsch-type cubic spline, Fourier harmonics and linear interpolation. It is to derive the fitted curve to the observed data ( CO2, d13C,..), as well as to separate the seasonal, short- and long-term variations from the data.
5
+ Author: Takakiyo Nakazawa
6
+ Maintainer-email: Hisashi Yashio <yashiro.hisashi@nies.go.jp>
7
+ License: Copyright (c) 1991-2024, n97fit developers
8
+ All rights reserved.
9
+
10
+ Redistribution and use in source and binary forms, with or without
11
+ modification, are permitted provided that the following conditions are met:
12
+ * Redistributions of source code must retain the above copyright notice,
13
+ this list of conditions and the following disclaimer.
14
+ * Redistributions in binary form must reproduce the above copyright notice,
15
+ this list of conditions and the following disclaimer in the documentation
16
+ and/or other materials provided with the distribution.
17
+ * Neither the name of the copyright holder nor the names of its contributors
18
+ may be used to endorse or promote products derived from this software
19
+ without specific prior written permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+
32
+
33
+ Classifier: Development Status :: 4 - Beta
34
+ Classifier: License :: OSI Approved :: BSD License
35
+ Classifier: Operating System :: OS Independent
36
+ Classifier: Programming Language :: Python :: 3
37
+ Classifier: Programming Language :: Python :: 3.8
38
+ Classifier: Programming Language :: Python :: 3.9
39
+ Classifier: Programming Language :: Python :: 3.10
40
+ Classifier: Programming Language :: Python :: 3.11
41
+ Classifier: Programming Language :: Python :: 3.12
42
+ Classifier: Topic :: Scientific/Engineering :: Atmospheric Science
43
+ Requires-Python: >=3.8
44
+ Description-Content-Type: text/markdown
45
+ License-File: LICENSE
46
+ License-File: AUTHORS
47
+ Requires-Dist: numpy
48
+ Requires-Dist: scipy>=1.10
49
+ Requires-Dist: pandas<3,>=2.0
50
+ Provides-Extra: dev
51
+ Requires-Dist: build; extra == "dev"
52
+
53
+ n97fit: Curve fitting tool for trace gas observations
54
+ ======
55
+
56
+ As known as "Nakazawa-fitting"
57
+
58
+ ## Reference Paper
59
+
60
+ Nakazawa, T., Ishizawa, M., Higuchi, K. And Trivett, N.B.A. (1997), Two Curve Fitting Methods Applied to CO2 Flask Data. Environmetrics, 8: 197-218. [URL]
61
+
62
+ [URL]: https://doi.org/10.1002/(SICI)1099-095X(199705)8:3<197::AID-ENV248>3.0.CO;2-C
63
+
64
+ ## Authors
65
+
66
+ - Takakiyo Nakazawa - Original work
67
+
68
+ See the AUTHORS file for details
69
+
70
+ ## License
71
+
72
+ This project is licensed under the BSD 3-clause License - see the LICENSE file for details
73
+
74
+ ## Acknowledgments
75
+
76
+ We would like to thank Prof. Shinji Morimoto and the staff and students of the Center for Atmospheric and Oceanic Studies in Tohoku University, who contributed to the improvement of this tool.
77
+
78
+ # How to use
79
+
80
+ ## Prerequisites
81
+
82
+ - python >= 3.8
83
+ - numpy
84
+ - pandas
85
+
86
+ ## Running as a module
87
+
88
+ $> python -m n97fit [input_file] [config_file] [output_dir]
89
+
90
+ ## Input data format
91
+
92
+ YYYYMMDD obsID Lat Lon Hgt Value Anl.Date QF
93
+ 19891011 2135 13 144 2 131.17 19891023 -1
94
+ 19891012 2137 15 152 2 167.82 19891023 0
95
+ ...
96
+
97
+ YYYY : Observation year
98
+ MM : Observation month
99
+ DD : Observation day
100
+ obsID : Observation ID (e.g. Flask number)
101
+ Lat : Latitude
102
+ Log : Longtitude
103
+ Hgt : Altitude
104
+ Value : Time series of concentration, isotope ratio, etc.
105
+ Anl.Date : Analyzing/extracting day
106
+ QF : Quality flag
107
+
108
+ ## Outputs
109
+
110
+ - data\_all.txt : All of the input data
111
+ - data\_eff\_ave.txt : Input data, averaged good quality data (flag=0) by "Range of day"
112
+ - data\_eq\_ave.txt : Input data, averaged and aligned to the output interval of fitting results
113
+ - data\_rej.txt : Input data, outliers
114
+ - fit\_all.txt : Fitting results
115
+ - fit\_annual.txt : Fitting results, annual mean
116
+ - fit\_monthly.txt : Fitting results, monthly mean
117
+ - fit\_multimonth.txt : Fitting results, multi-monthly mean
118
+ - fit\_onemonth.txt : Fitting results, one harmonics
119
+ - fit\_period.txt : Fitting results, mean of the whole period
120
+
121
+ # Release Notes
122
+
123
+ ## 2024-04-01
124
+
125
+ - Version 1.0.1
126
+ - Initial release of python version
127
+
128
+ ## Original Release Notes (FORTRAN version)
129
+
130
+
131
+ Program : lsfbf4-9-2.f
132
+
133
+ This is a program of data selection and curve fitting
134
+ by a digital filtering technique including Reinsch-type cubic
135
+ spline, Fourier harmonics and linear interpolation. It is to
136
+ derive the fitted curve to the observed data ( CO2, d13C,..),
137
+ as well as to separate the seasonal, short- and long-term
138
+ variations from the data.
139
+
140
+ The average seasonal cycle is represented by harmonics
141
+ (NH=0-6), but if you give NH, the average cycle with NH+1 is
142
+ first calculated and then smoothed by a Butterworth filter
143
+ which attenuates signals with a period of NH+1 to 50 %.
144
+ Linear interpolation is used to calculate equally spaced
145
+ data from residuals which are obtained by extracting the
146
+ average seasonal cycle from actual data.
147
+ The long-term trend and irregular parts of the seasonal
148
+ cycle are driven by smoothing equally spaced data ,obtained
149
+ above, with the Butterworth filter. The extrapolated data
150
+ for the trend and overall fit are given by the fit to the
151
+ actual data with a Reinsch spline (T5=1825days), which are
152
+ altered by an iterative procedure.
153
+ Prior to use of this program, the number of harmonics has
154
+ to be determined by the program 'PERI'. The digital filter
155
+ form of Reinsch spline is shown explicitly by the programs
156
+ 'COX1' or 'COX' in which the transfer function is plotted.
157
+
158
+ KD must be sufficiently larger than the value of ((910
159
+ +2920)/(mean data interval expressed in day)+data number of
160
+ the record). KO must be sufficiently larger than the value
161
+ of (910+2920+length of the data record expressed in day).
162
+ If you want to change KD, you must change KD of subroutine
163
+ or function Fourier, Frc1, Reinsch, Setup, Chol1d, Smooth,
164
+ Extrad1 and Judge1,too. If you want to change KO, you must
165
+ change N or KO in subroutines Bwf, Scycle, Yac and Judge1,
166
+ too.
167
+
168
+ By T. Nakazawa (Mar. 8, 1991)
169
+
170
+ * Before digital filtering, data values with more than 3 standard
171
+ errors of estimated fit are identified as outliers and rejected
172
+ from further analysis. (dafault setting)
173
+
174
+ * The order of Butterworth filter is set to be 26th for long-term
175
+ and to be 16th for seasonal cycle. (dafault setting)
176
+
177
+ Last modified by M. Ishizawa
178
+ (Feb. 4, 2002)
179
+
180
+ Fortran 90 version refactoring
181
+ Developed by Hisashi Yashio (2008)
n97fit-1.0.1/README.md ADDED
@@ -0,0 +1,129 @@
1
+ n97fit: Curve fitting tool for trace gas observations
2
+ ======
3
+
4
+ As known as "Nakazawa-fitting"
5
+
6
+ ## Reference Paper
7
+
8
+ Nakazawa, T., Ishizawa, M., Higuchi, K. And Trivett, N.B.A. (1997), Two Curve Fitting Methods Applied to CO2 Flask Data. Environmetrics, 8: 197-218. [URL]
9
+
10
+ [URL]: https://doi.org/10.1002/(SICI)1099-095X(199705)8:3<197::AID-ENV248>3.0.CO;2-C
11
+
12
+ ## Authors
13
+
14
+ - Takakiyo Nakazawa - Original work
15
+
16
+ See the AUTHORS file for details
17
+
18
+ ## License
19
+
20
+ This project is licensed under the BSD 3-clause License - see the LICENSE file for details
21
+
22
+ ## Acknowledgments
23
+
24
+ We would like to thank Prof. Shinji Morimoto and the staff and students of the Center for Atmospheric and Oceanic Studies in Tohoku University, who contributed to the improvement of this tool.
25
+
26
+ # How to use
27
+
28
+ ## Prerequisites
29
+
30
+ - python >= 3.8
31
+ - numpy
32
+ - pandas
33
+
34
+ ## Running as a module
35
+
36
+ $> python -m n97fit [input_file] [config_file] [output_dir]
37
+
38
+ ## Input data format
39
+
40
+ YYYYMMDD obsID Lat Lon Hgt Value Anl.Date QF
41
+ 19891011 2135 13 144 2 131.17 19891023 -1
42
+ 19891012 2137 15 152 2 167.82 19891023 0
43
+ ...
44
+
45
+ YYYY : Observation year
46
+ MM : Observation month
47
+ DD : Observation day
48
+ obsID : Observation ID (e.g. Flask number)
49
+ Lat : Latitude
50
+ Log : Longtitude
51
+ Hgt : Altitude
52
+ Value : Time series of concentration, isotope ratio, etc.
53
+ Anl.Date : Analyzing/extracting day
54
+ QF : Quality flag
55
+
56
+ ## Outputs
57
+
58
+ - data\_all.txt : All of the input data
59
+ - data\_eff\_ave.txt : Input data, averaged good quality data (flag=0) by "Range of day"
60
+ - data\_eq\_ave.txt : Input data, averaged and aligned to the output interval of fitting results
61
+ - data\_rej.txt : Input data, outliers
62
+ - fit\_all.txt : Fitting results
63
+ - fit\_annual.txt : Fitting results, annual mean
64
+ - fit\_monthly.txt : Fitting results, monthly mean
65
+ - fit\_multimonth.txt : Fitting results, multi-monthly mean
66
+ - fit\_onemonth.txt : Fitting results, one harmonics
67
+ - fit\_period.txt : Fitting results, mean of the whole period
68
+
69
+ # Release Notes
70
+
71
+ ## 2024-04-01
72
+
73
+ - Version 1.0.1
74
+ - Initial release of python version
75
+
76
+ ## Original Release Notes (FORTRAN version)
77
+
78
+
79
+ Program : lsfbf4-9-2.f
80
+
81
+ This is a program of data selection and curve fitting
82
+ by a digital filtering technique including Reinsch-type cubic
83
+ spline, Fourier harmonics and linear interpolation. It is to
84
+ derive the fitted curve to the observed data ( CO2, d13C,..),
85
+ as well as to separate the seasonal, short- and long-term
86
+ variations from the data.
87
+
88
+ The average seasonal cycle is represented by harmonics
89
+ (NH=0-6), but if you give NH, the average cycle with NH+1 is
90
+ first calculated and then smoothed by a Butterworth filter
91
+ which attenuates signals with a period of NH+1 to 50 %.
92
+ Linear interpolation is used to calculate equally spaced
93
+ data from residuals which are obtained by extracting the
94
+ average seasonal cycle from actual data.
95
+ The long-term trend and irregular parts of the seasonal
96
+ cycle are driven by smoothing equally spaced data ,obtained
97
+ above, with the Butterworth filter. The extrapolated data
98
+ for the trend and overall fit are given by the fit to the
99
+ actual data with a Reinsch spline (T5=1825days), which are
100
+ altered by an iterative procedure.
101
+ Prior to use of this program, the number of harmonics has
102
+ to be determined by the program 'PERI'. The digital filter
103
+ form of Reinsch spline is shown explicitly by the programs
104
+ 'COX1' or 'COX' in which the transfer function is plotted.
105
+
106
+ KD must be sufficiently larger than the value of ((910
107
+ +2920)/(mean data interval expressed in day)+data number of
108
+ the record). KO must be sufficiently larger than the value
109
+ of (910+2920+length of the data record expressed in day).
110
+ If you want to change KD, you must change KD of subroutine
111
+ or function Fourier, Frc1, Reinsch, Setup, Chol1d, Smooth,
112
+ Extrad1 and Judge1,too. If you want to change KO, you must
113
+ change N or KO in subroutines Bwf, Scycle, Yac and Judge1,
114
+ too.
115
+
116
+ By T. Nakazawa (Mar. 8, 1991)
117
+
118
+ * Before digital filtering, data values with more than 3 standard
119
+ errors of estimated fit are identified as outliers and rejected
120
+ from further analysis. (dafault setting)
121
+
122
+ * The order of Butterworth filter is set to be 26th for long-term
123
+ and to be 16th for seasonal cycle. (dafault setting)
124
+
125
+ Last modified by M. Ishizawa
126
+ (Feb. 4, 2002)
127
+
128
+ Fortran 90 version refactoring
129
+ Developed by Hisashi Yashio (2008)
@@ -0,0 +1,66 @@
1
+ [build-system]
2
+ requires = ["setuptools>=64"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "n97fit"
7
+ version = "1.0.1"
8
+ license = {file = "LICENSE"}
9
+
10
+ authors = [
11
+ {name = "Takakiyo Nakazawa"},
12
+ ]
13
+
14
+ maintainers = [
15
+ {name = "Hisashi Yashio", email = "yashiro.hisashi@nies.go.jp"}
16
+ ]
17
+
18
+ description = """
19
+ This is a program of data selection and curve fitting \
20
+ by a digital filtering technique including Reinsch-type cubic \
21
+ spline, Fourier harmonics and linear interpolation. It is to \
22
+ derive the fitted curve to the observed data ( CO2, d13C,..), \
23
+ as well as to separate the seasonal, short- and long-term \
24
+ variations from the data. \
25
+ """
26
+ readme = "README.md"
27
+
28
+ dependencies = [
29
+ "numpy",
30
+ "scipy>=1.10",
31
+ "pandas>=2.0,<3"
32
+ ]
33
+
34
+ requires-python = ">=3.8"
35
+
36
+ classifiers = [
37
+ "Development Status :: 4 - Beta",
38
+ "License :: OSI Approved :: BSD License",
39
+ "Operating System :: OS Independent",
40
+ "Programming Language :: Python :: 3",
41
+ "Programming Language :: Python :: 3.8",
42
+ "Programming Language :: Python :: 3.9",
43
+ "Programming Language :: Python :: 3.10",
44
+ "Programming Language :: Python :: 3.11",
45
+ "Programming Language :: Python :: 3.12",
46
+ "Topic :: Scientific/Engineering :: Atmospheric Science",
47
+ ]
48
+
49
+ keywords = [""]
50
+
51
+ [project.urls]
52
+ #Homepage = ""
53
+ #Documentation = ""
54
+ #Repository = ""
55
+
56
+ [project.scripts]
57
+ n97fit = "n97fit:cli_main"
58
+
59
+ [project.optional-dependencies]
60
+ dev = [
61
+ "build"
62
+ ]
63
+
64
+ [tool.setuptools]
65
+ package-dir = {"" = "src"}
66
+ include-package-data = true
n97fit-1.0.1/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,6 @@
1
+ from .n97fit import N97Fit, logger
2
+ from .__main__ import cli_main
3
+
4
+ from .io import read_n97format, write_fixedformat
5
+
6
+ __all__ = ["N97Fit", "logger", "read_n97format", "write_fixedformat", "cli_main"]
@@ -0,0 +1,194 @@
1
+ from typing import List
2
+ from pathlib import Path
3
+
4
+ from pandas import read_fwf
5
+
6
+ from .io import read_n97format, write_fixedformat, logger
7
+ from . import N97Fit
8
+
9
+
10
+ def readcfg(file, nrows: int = None) -> List:
11
+ """
12
+ Read configuration file and return a numpy array.
13
+
14
+ Parameters:
15
+ n (int): Number of rows to read from the file.
16
+ filename (os.PathLike): Path to the configuration file.
17
+
18
+ Returns:
19
+ np.ndarray: Numpy array containing the configuration data.
20
+ """
21
+
22
+ logger.info("")
23
+ logger.info(" Config file : %s", file)
24
+ logger.info(" +Number of list : %d", nrows)
25
+
26
+ dtypes = {
27
+ "label": str,
28
+ "value": int,
29
+ }
30
+
31
+ conf = read_fwf(
32
+ file,
33
+ widths=[34, 4],
34
+ dtype=dtypes,
35
+ names=dtypes.keys(),
36
+ nrows=nrows,
37
+ skiprows=0,
38
+ header=None,
39
+ )
40
+
41
+ for i, row in conf.iterrows():
42
+ logger.info(" +%-34s %3d", row["label"], row["value"])
43
+
44
+ return conf["value"].values.tolist()
45
+
46
+
47
+ def getargs():
48
+ import argparse
49
+
50
+ parser = argparse.ArgumentParser(
51
+ prog="n97fit",
52
+ description="A program of data selection and curve fitting.",
53
+ )
54
+ parser.add_argument("input", type=Path, help="input data file")
55
+ parser.add_argument("config", type=Path, help="config file")
56
+ parser.add_argument("outdir", type=Path, help="output directory")
57
+ args, _ = parser.parse_known_args()
58
+
59
+ return args.input, args.config, args.outdir
60
+
61
+
62
+ def cli_main():
63
+ input, config, outdir = getargs()
64
+
65
+ [avgday, sigma, outday, nhm, cutlo, orderlo, orderhi, sw_iteration] = readcfg(
66
+ file=config, nrows=8
67
+ )
68
+
69
+ # Read input data
70
+ data = read_n97format(file=input)
71
+
72
+ # create N97Fit instance
73
+ n97fit = N97Fit(
74
+ avgday=avgday,
75
+ sigma=sigma,
76
+ nhm=nhm,
77
+ cutlo=cutlo,
78
+ orderlo=orderlo,
79
+ orderhi=orderhi,
80
+ sw_iteration=sw_iteration,
81
+ loglevel="INFO",
82
+ )
83
+
84
+ # exec fitting
85
+ n97fit.fit(data)
86
+
87
+ # fetch results
88
+ all_data = n97fit.data_all()
89
+ rej_data = n97fit.data_rej()
90
+ eff_ave = n97fit.data_eff_ave()
91
+ eq_ave = n97fit.data_eq_ave()
92
+ fit_all = n97fit.all(interval=outday)
93
+ fit_annual = n97fit.annual()
94
+ fit_monthly = n97fit.monthly()
95
+ fit_multimonth = n97fit.multimonth()
96
+ fit_period = n97fit.period()
97
+ fit_onemonth = n97fit.onemonth()
98
+
99
+ # write output
100
+ logger.info("")
101
+ logger.info(" * * * * * START Output * * * * *")
102
+
103
+ outdir = Path(outdir)
104
+ outdir.mkdir(parents=True, exist_ok=True)
105
+ logger.info(" Output directory : %s", outdir)
106
+
107
+ write_fixedformat(
108
+ all_data,
109
+ Path(outdir, "data_all.txt"),
110
+ format={"flg": "%3d"},
111
+ float_format="%12.5f",
112
+ )
113
+
114
+ write_fixedformat(
115
+ rej_data,
116
+ Path(outdir, "data_rej.txt"),
117
+ format={"flg": "%3d"},
118
+ float_format="%12.5f",
119
+ )
120
+
121
+ write_fixedformat(
122
+ eff_ave,
123
+ Path(outdir, "data_eff_ave.txt"),
124
+ float_format="%12.5f",
125
+ date_format="%(excel)11d",
126
+ )
127
+
128
+ write_fixedformat(
129
+ eq_ave,
130
+ Path(outdir, "data_eq_ave.txt"),
131
+ float_format="%12.5f",
132
+ date_format="%(excel)11d",
133
+ )
134
+
135
+ write_fixedformat(
136
+ fit_all,
137
+ Path(outdir, "fit_all.txt"),
138
+ format={"year": "%9.5f"},
139
+ float_format="%13.7f",
140
+ date_format="%(excel)7d",
141
+ )
142
+
143
+ write_fixedformat(
144
+ fit_annual,
145
+ Path(outdir, "fit_annual.txt"),
146
+ format={"year": "%4d"},
147
+ float_format="%13.7f",
148
+ fillna={
149
+ "ave_fit": -999.9999999,
150
+ "stdev_fit": -99.9999999,
151
+ "ave_trend": -999.9999999,
152
+ "stdev_trend": -99.9999999,
153
+ },
154
+ )
155
+
156
+ write_fixedformat(
157
+ fit_monthly,
158
+ Path(outdir, "fit_monthly.txt"),
159
+ format={"year": "%4d", "mn": "%2d"},
160
+ float_format="%13.7f",
161
+ fillna={
162
+ "ave_fit": -999.9999999,
163
+ "stdev_fit": -99.9999999,
164
+ "ave_trend": -999.9999999,
165
+ "stdev_trend": -99.9999999,
166
+ },
167
+ )
168
+
169
+ write_fixedformat(
170
+ fit_onemonth,
171
+ Path(outdir, "fit_onemonth.txt"),
172
+ float_format="%13.7f",
173
+ )
174
+
175
+ write_fixedformat(
176
+ fit_multimonth,
177
+ Path(outdir, "fit_multimonth.txt"),
178
+ format={"period": "%10s", "mn": "%2d"},
179
+ float_format="%13.7f",
180
+ )
181
+
182
+ write_fixedformat(
183
+ fit_period,
184
+ Path(outdir, "fit_period.txt"),
185
+ format={"period": "%10s"},
186
+ float_format="%13.7f",
187
+ )
188
+
189
+ logger.info("")
190
+ logger.info(" ************** E N D ***************")
191
+
192
+
193
+ if __name__ == "__main__":
194
+ cli_main()
@@ -0,0 +1,8 @@
1
+ from .cday import date2cday, cday2date
2
+ from .ijmapping import ijmapping
3
+
4
+ __all__ = [
5
+ "date2cday",
6
+ "cday2date",
7
+ "ijmapping",
8
+ ]
@@ -0,0 +1,54 @@
1
+ from typing import Union
2
+ from datetime import date, timedelta
3
+
4
+ import pandas as pd
5
+
6
+ # start date
7
+ ys = 1904
8
+ sdate = date(ys, 1, 1)
9
+
10
+ # cache dictionaries
11
+ cday_dict = dict()
12
+ date_dict = dict()
13
+
14
+
15
+ def date2cday(dt: Union[date, pd.Timestamp]) -> int:
16
+ """Converts a date to a day count from 1904-01-01
17
+
18
+ Args:
19
+ year (int): year
20
+ month (int): month
21
+ day (int): day
22
+
23
+ Returns:
24
+ int: day count
25
+ """
26
+ if isinstance(dt, pd.Timestamp):
27
+ dt = dt.date()
28
+
29
+ if dt in cday_dict:
30
+ return cday_dict[dt]
31
+
32
+ cday = dt.toordinal() - sdate.toordinal()
33
+ cday_dict[dt] = cday
34
+
35
+ return cday
36
+
37
+
38
+ def cday2date(cday: int) -> date:
39
+ """Converts a day count from 1904-01-01 to a date
40
+
41
+ Args:
42
+ cday (int): day count
43
+
44
+ Returns:
45
+ date: date
46
+ """
47
+
48
+ if cday in date_dict:
49
+ return date_dict[cday]
50
+
51
+ cdate = sdate + timedelta(days=int(cday))
52
+ date_dict[cday] = cdate
53
+
54
+ return cdate