smmregrid 0.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.
- smmregrid-0.0.1/LICENSE +201 -0
- smmregrid-0.0.1/PKG-INFO +61 -0
- smmregrid-0.0.1/README.md +43 -0
- smmregrid-0.0.1/pyproject.toml +55 -0
- smmregrid-0.0.1/setup.cfg +4 -0
- smmregrid-0.0.1/smmregrid/__init__.py +7 -0
- smmregrid-0.0.1/smmregrid/cdo_weights.py +260 -0
- smmregrid-0.0.1/smmregrid/checker.py +67 -0
- smmregrid-0.0.1/smmregrid/dimension.py +103 -0
- smmregrid-0.0.1/smmregrid/esmf_weights.py +96 -0
- smmregrid-0.0.1/smmregrid/regrid.py +447 -0
- smmregrid-0.0.1/smmregrid/util.py +17 -0
- smmregrid-0.0.1/smmregrid/weights.py +123 -0
- smmregrid-0.0.1/smmregrid.egg-info/PKG-INFO +61 -0
- smmregrid-0.0.1/smmregrid.egg-info/SOURCES.txt +16 -0
- smmregrid-0.0.1/smmregrid.egg-info/dependency_links.txt +1 -0
- smmregrid-0.0.1/smmregrid.egg-info/requires.txt +16 -0
- smmregrid-0.0.1/smmregrid.egg-info/top_level.txt +1 -0
smmregrid-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
smmregrid-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: smmregrid
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Regridding based on sparse matrix multiplication
|
|
5
|
+
Author-email: Jost von Hardenberg <jost.hardenberg@polito.it>, Paolo Davini <p.davini@isac.cnr.it>, Scott Wales <scott.wales@unimelb.edu.au>
|
|
6
|
+
Project-URL: Homepage, https://github.com/jhardenberg/smmregrid
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/jhardenberg/smmregrid/issues
|
|
8
|
+
Project-URL: Repository, https://github.com/oloapinivad/ECmean4
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.8
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
Provides-Extra: tests
|
|
16
|
+
Provides-Extra: all
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
|
|
19
|
+
[](https://github.com/jhardenberg/smmregrid/graphs/commit-activity)
|
|
20
|
+
[](https://github.com/jhardenberg/smmregrid/actions/workflows/mambatest.yml)
|
|
21
|
+
[](https://coveralls.io/github/jhardenberg/smmregrid?branch=main)
|
|
22
|
+
[](https://badge.fury.io/py/smmregrid)
|
|
23
|
+
|
|
24
|
+
# smmregrid
|
|
25
|
+
A compact regridder using sparse matrix multiplication
|
|
26
|
+
|
|
27
|
+
This repository represents a modification of the regridding routines in [climtas](https://github.com/ScottWales/climtas) by Scott Wales, which already implements efficiently this idea and has no other significant dependencies.
|
|
28
|
+
The regridder uses efficiently sparse matrix multiplication with dask + some manipulation of the coordinates.
|
|
29
|
+
|
|
30
|
+
Please note that this tool is not thought as "another interpolation tool", but rather a method to apply pre-computed weights (with CDO, which is currently tested, and with ESMF, which is not yet supported) within the python environment.
|
|
31
|
+
The speedup is estimated to be about ~1.5 to ~5 times, slightly lower if then files are written to the disk. 2D and 3D data are supported on all the grids supported by CDO, both xarray.Dataset and xarray.DataArray can be used. Masks are treated in a simple way but are correctly transfered. Attributes are kept.
|
|
32
|
+
|
|
33
|
+
The tool works for python versions >=3.8. It is safer to run it through conda/mamba. Install with:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
mamba env create -f environment.yml
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
then activate the environment:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
mamba activate smmregrid
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
and install smmregrid in editable mode:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
pip install -e .
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Alternatively - if you have in your environment/machine the required dependencies, mostly CDO - you can install smmregrid directly via pypi with:
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
pip install smmregrid
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Cautionary notes:
|
|
58
|
+
- It does not work correctly if the Xarray.Dataset includes fields with time-varying missing points
|
|
59
|
+
- It works only with interpolation methods/grids supported by CDO
|
|
60
|
+
- It does not support ESMF weigths.
|
|
61
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[](https://github.com/jhardenberg/smmregrid/graphs/commit-activity)
|
|
2
|
+
[](https://github.com/jhardenberg/smmregrid/actions/workflows/mambatest.yml)
|
|
3
|
+
[](https://coveralls.io/github/jhardenberg/smmregrid?branch=main)
|
|
4
|
+
[](https://badge.fury.io/py/smmregrid)
|
|
5
|
+
|
|
6
|
+
# smmregrid
|
|
7
|
+
A compact regridder using sparse matrix multiplication
|
|
8
|
+
|
|
9
|
+
This repository represents a modification of the regridding routines in [climtas](https://github.com/ScottWales/climtas) by Scott Wales, which already implements efficiently this idea and has no other significant dependencies.
|
|
10
|
+
The regridder uses efficiently sparse matrix multiplication with dask + some manipulation of the coordinates.
|
|
11
|
+
|
|
12
|
+
Please note that this tool is not thought as "another interpolation tool", but rather a method to apply pre-computed weights (with CDO, which is currently tested, and with ESMF, which is not yet supported) within the python environment.
|
|
13
|
+
The speedup is estimated to be about ~1.5 to ~5 times, slightly lower if then files are written to the disk. 2D and 3D data are supported on all the grids supported by CDO, both xarray.Dataset and xarray.DataArray can be used. Masks are treated in a simple way but are correctly transfered. Attributes are kept.
|
|
14
|
+
|
|
15
|
+
The tool works for python versions >=3.8. It is safer to run it through conda/mamba. Install with:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
mamba env create -f environment.yml
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
then activate the environment:
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
mamba activate smmregrid
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
and install smmregrid in editable mode:
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
pip install -e .
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Alternatively - if you have in your environment/machine the required dependencies, mostly CDO - you can install smmregrid directly via pypi with:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
pip install smmregrid
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Cautionary notes:
|
|
40
|
+
- It does not work correctly if the Xarray.Dataset includes fields with time-varying missing points
|
|
41
|
+
- It works only with interpolation methods/grids supported by CDO
|
|
42
|
+
- It does not support ESMF weigths.
|
|
43
|
+
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "smmregrid"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Jost von Hardenberg", email="jost.hardenberg@polito.it" },
|
|
10
|
+
{ name="Paolo Davini", email="p.davini@isac.cnr.it" },
|
|
11
|
+
{ name="Scott Wales", email="scott.wales@unimelb.edu.au"}
|
|
12
|
+
]
|
|
13
|
+
description = "Regridding based on sparse matrix multiplication"
|
|
14
|
+
readme = "README.md"
|
|
15
|
+
requires-python = ">=3.8"
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Intended Audience :: Science/Research",
|
|
19
|
+
"License :: OSI Approved :: Apache Software License",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
]
|
|
22
|
+
dependencies = [
|
|
23
|
+
'numpy',
|
|
24
|
+
'xarray',
|
|
25
|
+
'dask',
|
|
26
|
+
'netcdf4',
|
|
27
|
+
'cfgrib',
|
|
28
|
+
'dask',
|
|
29
|
+
'sparse',
|
|
30
|
+
'cdo'
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
tests = [
|
|
35
|
+
"coverage",
|
|
36
|
+
"coveralls",
|
|
37
|
+
"pytest"
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
#docs = [
|
|
41
|
+
# "sphinx",
|
|
42
|
+
# "sphinx-rtd-theme"
|
|
43
|
+
#]
|
|
44
|
+
|
|
45
|
+
all = [
|
|
46
|
+
"scriptengine[tests]"
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[project.urls]
|
|
50
|
+
"Homepage" = "https://github.com/jhardenberg/smmregrid"
|
|
51
|
+
"Bug Tracker" = "https://github.com/jhardenberg/smmregrid/issues"
|
|
52
|
+
"Repository" = "https://github.com/oloapinivad/ECmean4"
|
|
53
|
+
|
|
54
|
+
[tool.setuptools.dynamic]
|
|
55
|
+
version = {attr = "smmregrid.__version__"}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"""CDO-based generation of weights"""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
import logging
|
|
6
|
+
import tempfile
|
|
7
|
+
import subprocess
|
|
8
|
+
from multiprocessing import Process, Manager
|
|
9
|
+
import numpy
|
|
10
|
+
import xarray
|
|
11
|
+
from .util import find_vert_coord
|
|
12
|
+
from .weights import compute_weights_matrix3d, compute_weights_matrix, mask_weights, check_mask
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def worker(wlist, nnn, *args, **kwargs):
|
|
16
|
+
"""Run a worker process"""
|
|
17
|
+
wlist[nnn] = cdo_generate_weights2d(*args, **kwargs).compute()
|
|
18
|
+
|
|
19
|
+
def cdo_generate_weights(source_grid, target_grid, method="con", extrapolate=True,
|
|
20
|
+
remap_norm="fracarea", remap_area_min=0.0, icongridpath=None,
|
|
21
|
+
gridpath=None, extra=None, vert_coord=None, cdo="cdo", nproc=1):
|
|
22
|
+
"""Generate the weights using CDO, handling both 2D and 3D cases"""
|
|
23
|
+
|
|
24
|
+
# Check if there is a vertical coordinate for 3d oceanic data
|
|
25
|
+
if not vert_coord:
|
|
26
|
+
vert_coord = find_vert_coord(source_grid)
|
|
27
|
+
logging.warning('vert_coord is %s',str(vert_coord))
|
|
28
|
+
|
|
29
|
+
if not vert_coord: # Are we 2D? Use default method
|
|
30
|
+
weights =cdo_generate_weights2d(
|
|
31
|
+
source_grid,
|
|
32
|
+
target_grid,
|
|
33
|
+
method=method,
|
|
34
|
+
extrapolate=extrapolate,
|
|
35
|
+
remap_norm=remap_norm,
|
|
36
|
+
remap_area_min=remap_area_min,
|
|
37
|
+
icongridpath=icongridpath,
|
|
38
|
+
gridpath=gridpath,
|
|
39
|
+
extra=extra,
|
|
40
|
+
cdo=cdo,
|
|
41
|
+
nproc=nproc)
|
|
42
|
+
|
|
43
|
+
# Precompute destination weights mask
|
|
44
|
+
weights_matrix = compute_weights_matrix(weights)
|
|
45
|
+
weights = mask_weights(weights, weights_matrix, vert_coord)
|
|
46
|
+
masked = int(check_mask(weights, vert_coord))
|
|
47
|
+
masked_xa = xarray.DataArray(masked, name="dst_grid_masked")
|
|
48
|
+
|
|
49
|
+
return xarray.merge([weights, masked_xa])
|
|
50
|
+
|
|
51
|
+
else: # we are 3D
|
|
52
|
+
if extra:
|
|
53
|
+
# make sure extra is a flat list if it is not already
|
|
54
|
+
if not isinstance(extra, list):
|
|
55
|
+
extra = [extra]
|
|
56
|
+
else:
|
|
57
|
+
extra = []
|
|
58
|
+
|
|
59
|
+
if isinstance(source_grid, str):
|
|
60
|
+
sgrid = xarray.open_dataset(source_grid)
|
|
61
|
+
else:
|
|
62
|
+
sgrid = source_grid
|
|
63
|
+
|
|
64
|
+
nvert = sgrid[vert_coord].values.size
|
|
65
|
+
#print(nvert)
|
|
66
|
+
|
|
67
|
+
# for lev in range(0, nvert):
|
|
68
|
+
mgr = Manager()
|
|
69
|
+
|
|
70
|
+
# dictionaries are shared, so they have to be passed as functions
|
|
71
|
+
wlist= mgr.list(range(nvert))
|
|
72
|
+
|
|
73
|
+
num_blocks, remainder = divmod(nvert, nproc)
|
|
74
|
+
num_blocks = num_blocks + (0 if remainder == 0 else 1)
|
|
75
|
+
|
|
76
|
+
blocks = numpy.array_split(numpy.arange(nvert), num_blocks)
|
|
77
|
+
for block in blocks:
|
|
78
|
+
processes = []
|
|
79
|
+
for lev in block:
|
|
80
|
+
logging.info("Generating level: %s", str(lev))
|
|
81
|
+
extra2 = [f"-sellevidx,{lev+1}"]
|
|
82
|
+
ppp = Process(target=worker,
|
|
83
|
+
args=(wlist, lev, source_grid, target_grid),
|
|
84
|
+
kwargs=dict(method=method,
|
|
85
|
+
extrapolate=extrapolate,
|
|
86
|
+
remap_norm=remap_norm,
|
|
87
|
+
remap_area_min=remap_area_min,
|
|
88
|
+
icongridpath=icongridpath,
|
|
89
|
+
gridpath=gridpath,
|
|
90
|
+
extra=extra + extra2,
|
|
91
|
+
cdo=cdo,
|
|
92
|
+
nproc=nproc))
|
|
93
|
+
ppp.start()
|
|
94
|
+
processes.append(ppp)
|
|
95
|
+
|
|
96
|
+
for proc in processes:
|
|
97
|
+
proc.join()
|
|
98
|
+
|
|
99
|
+
weights = weightslist_to_3d(wlist, vert_coord)
|
|
100
|
+
|
|
101
|
+
# Precompute destination weights mask
|
|
102
|
+
weights_matrix = compute_weights_matrix3d(weights, vert_coord)
|
|
103
|
+
weights = mask_weights(weights, weights_matrix, vert_coord)
|
|
104
|
+
masked = check_mask(weights, vert_coord)
|
|
105
|
+
masked = [int(x) for x in masked] # convert to list of int
|
|
106
|
+
masked_xa = xarray.DataArray(masked, coords={vert_coord: range(0, len(masked))}, name="dst_grid_masked")
|
|
107
|
+
|
|
108
|
+
return xarray.merge([weights, masked_xa])
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def cdo_generate_weights2d(source_grid, target_grid, method="con", extrapolate=True,
|
|
112
|
+
remap_norm="fracarea", remap_area_min=0.0, icongridpath=None,
|
|
113
|
+
gridpath=None, extra=None, cdo="cdo", nproc=1):
|
|
114
|
+
"""
|
|
115
|
+
Generate weights for regridding using CDO
|
|
116
|
+
|
|
117
|
+
Available weight generation methods are:
|
|
118
|
+
|
|
119
|
+
* bic: SCRIP Bicubic
|
|
120
|
+
* bil: SCRIP Bilinear
|
|
121
|
+
* con: SCRIP First-order conservative
|
|
122
|
+
* con2: SCRIP Second-order conservative
|
|
123
|
+
* dis: SCRIP Distance-weighted average
|
|
124
|
+
* laf: YAC Largest area fraction
|
|
125
|
+
* ycon: YAC First-order conservative
|
|
126
|
+
* nn: Nearest neighbour
|
|
127
|
+
|
|
128
|
+
Run ``cdo gen${method} --help`` for details of each method
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
source_grid (xarray.DataArray): Source grid
|
|
132
|
+
target_grid (xarray.DataArray): Target grid
|
|
133
|
+
description
|
|
134
|
+
method (str): Regridding method - default conservative
|
|
135
|
+
extrapolate (bool): Extrapolate output field
|
|
136
|
+
remap_norm (str): Normalisation method for conservative methods
|
|
137
|
+
remap_area_min (float): Minimum destination area fraction
|
|
138
|
+
gridpath (str): where to store downloaded grids
|
|
139
|
+
icongridpath (str): location of ICON grids (e.g. /pool/data/ICON)
|
|
140
|
+
extra: command(s) to apply to source grid before weight generation (can be a list)
|
|
141
|
+
cdo: the command to launch cdo ["cdo"]
|
|
142
|
+
nproc: number of processes to use for weight generation (NOT USED!)
|
|
143
|
+
|
|
144
|
+
cdo: path to cdo binary
|
|
145
|
+
Returns:
|
|
146
|
+
:obj:`xarray.Dataset` with regridding weights
|
|
147
|
+
"""
|
|
148
|
+
|
|
149
|
+
supported_methods = ["bic", "bil", "con", "con2", "dis", "laf", "nn", "ycon"]
|
|
150
|
+
if method not in supported_methods:
|
|
151
|
+
raise ValueError('The remap method provided is not supported!')
|
|
152
|
+
if remap_norm not in ["fracarea", "destarea"]:
|
|
153
|
+
raise ValueError('The remap normalization provided is not supported!')
|
|
154
|
+
|
|
155
|
+
# Make some temporary files that we'll feed to CDO
|
|
156
|
+
weight_file = tempfile.NamedTemporaryFile()
|
|
157
|
+
|
|
158
|
+
if isinstance(source_grid, str):
|
|
159
|
+
sgrid = source_grid
|
|
160
|
+
else:
|
|
161
|
+
source_grid_file = tempfile.NamedTemporaryFile()
|
|
162
|
+
source_grid.to_netcdf(source_grid_file.name)
|
|
163
|
+
sgrid = source_grid_file.name
|
|
164
|
+
|
|
165
|
+
if isinstance(target_grid, str):
|
|
166
|
+
tgrid = target_grid
|
|
167
|
+
else:
|
|
168
|
+
target_grid_file = tempfile.NamedTemporaryFile()
|
|
169
|
+
target_grid.to_netcdf(target_grid_file.name)
|
|
170
|
+
tgrid = target_grid_file.name
|
|
171
|
+
|
|
172
|
+
# Setup environment
|
|
173
|
+
env = os.environ
|
|
174
|
+
if extrapolate:
|
|
175
|
+
env["REMAP_EXTRAPOLATE"] = "on"
|
|
176
|
+
else:
|
|
177
|
+
env["REMAP_EXTRAPOLATE"] = "off"
|
|
178
|
+
|
|
179
|
+
env["CDO_REMAP_NORM"] = remap_norm
|
|
180
|
+
env["REMAP_AREA_MIN"] = "%f" % (remap_area_min)
|
|
181
|
+
|
|
182
|
+
if gridpath:
|
|
183
|
+
env["CDO_DOWNLOAD_PATH"] = gridpath
|
|
184
|
+
if icongridpath:
|
|
185
|
+
env["CDO_ICON_GRIDS"] = icongridpath
|
|
186
|
+
|
|
187
|
+
try:
|
|
188
|
+
# Run CDO
|
|
189
|
+
if extra:
|
|
190
|
+
# make sure extra is a flat list if it is not already
|
|
191
|
+
if not isinstance(extra, list):
|
|
192
|
+
extra = [extra]
|
|
193
|
+
|
|
194
|
+
subprocess.check_output(
|
|
195
|
+
[
|
|
196
|
+
cdo,
|
|
197
|
+
"gen%s,%s" % (method, tgrid)
|
|
198
|
+
] + extra +
|
|
199
|
+
[
|
|
200
|
+
sgrid,
|
|
201
|
+
weight_file.name,
|
|
202
|
+
],
|
|
203
|
+
stderr=subprocess.PIPE,
|
|
204
|
+
env=env,
|
|
205
|
+
)
|
|
206
|
+
else:
|
|
207
|
+
subprocess.check_output(
|
|
208
|
+
[
|
|
209
|
+
cdo,
|
|
210
|
+
"gen%s,%s" % (method, tgrid),
|
|
211
|
+
sgrid,
|
|
212
|
+
weight_file.name,
|
|
213
|
+
],
|
|
214
|
+
stderr=subprocess.PIPE,
|
|
215
|
+
env=env,
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
# Grab the weights file it outputs as a xarray.Dataset
|
|
219
|
+
weights = xarray.open_dataset(weight_file.name, engine="netcdf4")
|
|
220
|
+
return weights
|
|
221
|
+
|
|
222
|
+
except subprocess.CalledProcessError as err:
|
|
223
|
+
# Print the CDO error message
|
|
224
|
+
logging.error(err.output.decode(), file=sys.stderr)
|
|
225
|
+
raise
|
|
226
|
+
|
|
227
|
+
finally:
|
|
228
|
+
# Clean up the temporary files
|
|
229
|
+
if not isinstance(source_grid, str):
|
|
230
|
+
source_grid_file.close()
|
|
231
|
+
if not isinstance(target_grid, str):
|
|
232
|
+
target_grid_file.close()
|
|
233
|
+
weight_file.close()
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def weightslist_to_3d(ds_list, vert_coord='lev'):
|
|
237
|
+
"""
|
|
238
|
+
Function to combine a list of 2D cdo weights into a 3D one adding a vertical coordinate lev
|
|
239
|
+
"""
|
|
240
|
+
# CDO 2.2.0 fix
|
|
241
|
+
if "numLinks" in ds_list[0].dims:
|
|
242
|
+
links_dim = "numLinks"
|
|
243
|
+
else:
|
|
244
|
+
links_dim = "num_links"
|
|
245
|
+
|
|
246
|
+
dim_values = range(len(ds_list))
|
|
247
|
+
nl = [ds.src_address.size for ds in ds_list]
|
|
248
|
+
nl0 = max(nl)
|
|
249
|
+
nlda = xarray.DataArray(nl, coords={vert_coord: range(0, len(nl))}, name="link_length")
|
|
250
|
+
new_array = []
|
|
251
|
+
varlist = ["src_address", "dst_address", "remap_matrix", "src_grid_imask", "dst_grid_imask"]
|
|
252
|
+
ds0 = ds_list[0].drop_vars(varlist)
|
|
253
|
+
for x, d in zip(ds_list, dim_values):
|
|
254
|
+
nl1 = x.src_address.size
|
|
255
|
+
# xplist = [x[vname].pad(num_links=(0, nl0-nl1), mode='constant', constant_values=0)
|
|
256
|
+
xplist = [x[vname].pad(**{links_dim: (0, nl0-nl1), "mode": 'constant', "constant_values": 0})
|
|
257
|
+
for vname in varlist ]
|
|
258
|
+
xp = xarray.merge(xplist)
|
|
259
|
+
new_array.append(xp.assign_coords({vert_coord: d}))
|
|
260
|
+
return xarray.merge([nlda, ds0, xarray.concat(new_array, vert_coord)])
|