cedar-graph 2024.10.0__py3-none-any.whl
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.
- cedar_graph/__init__.py +0 -0
- cedar_graph/_version.py +16 -0
- cedar_graph/data/__init__.py +3 -0
- cedar_graph/data/field_info.py +197 -0
- cedar_graph/data/loader.py +36 -0
- cedar_graph/data/operator.py +117 -0
- cedar_graph/data/source.py +132 -0
- cedar_graph/logger.py +5 -0
- cedar_graph/metadata.py +8 -0
- cedar_graph/plots/__init__.py +0 -0
- cedar_graph/plots/cn/__init__.py +0 -0
- cedar_graph/plots/cn/bpli_wind/__init__.py +0 -0
- cedar_graph/plots/cn/bpli_wind/default.py +156 -0
- cedar_graph/plots/cn/cape_wind/__init__.py +0 -0
- cedar_graph/plots/cn/cape_wind/default.py +157 -0
- cedar_graph/plots/cn/cin_wind/__init__.py +0 -0
- cedar_graph/plots/cn/cin_wind/default.py +153 -0
- cedar_graph/plots/cn/div_wind/__init__.py +0 -0
- cedar_graph/plots/cn/div_wind/default.py +175 -0
- cedar_graph/plots/cn/height_500_mslp/__init__.py +0 -0
- cedar_graph/plots/cn/height_500_mslp/default.py +176 -0
- cedar_graph/plots/cn/height_500_wind_850/__init__.py +0 -0
- cedar_graph/plots/cn/height_500_wind_850/default.py +188 -0
- cedar_graph/plots/cn/k_wind/__init__.py +0 -0
- cedar_graph/plots/cn/k_wind/default.py +157 -0
- cedar_graph/plots/cn/prep_24h/__init__.py +0 -0
- cedar_graph/plots/cn/prep_24h/default.py +182 -0
- cedar_graph/plots/cn/pte_wind/__init__.py +0 -0
- cedar_graph/plots/cn/pte_wind/default.py +185 -0
- cedar_graph/plots/cn/qv_div/__init__.py +0 -0
- cedar_graph/plots/cn/qv_div/default.py +128 -0
- cedar_graph/plots/cn/radar_reflectivity/__init__.py +0 -0
- cedar_graph/plots/cn/radar_reflectivity/default.py +133 -0
- cedar_graph/plots/cn/rain_24h/__init__.py +0 -0
- cedar_graph/plots/cn/rain_24h/default.py +134 -0
- cedar_graph/plots/cn/rain_wind_10m/__init__.py +0 -0
- cedar_graph/plots/cn/rain_wind_10m/default.py +225 -0
- cedar_graph/plots/cn/shr/__init__.py +0 -0
- cedar_graph/plots/cn/shr/default.py +183 -0
- cedar_graph/plots/cn/t_2m/__init__.py +0 -0
- cedar_graph/plots/cn/t_2m/default.py +133 -0
- cedar_graph/plots/cn/t_dew_t/__init__.py +0 -0
- cedar_graph/plots/cn/t_dew_t/default.py +184 -0
- cedar_graph/plots/cn/wind_10m/__init__.py +0 -0
- cedar_graph/plots/cn/wind_10m/default.py +155 -0
- cedar_graph/quickplot.py +226 -0
- cedar_graph-2024.10.0.dist-info/LICENSE +202 -0
- cedar_graph-2024.10.0.dist-info/METADATA +146 -0
- cedar_graph-2024.10.0.dist-info/RECORD +51 -0
- cedar_graph-2024.10.0.dist-info/WHEEL +5 -0
- cedar_graph-2024.10.0.dist-info/top_level.txt +1 -0
cedar_graph/__init__.py
ADDED
|
File without changes
|
cedar_graph/_version.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# file generated by setuptools_scm
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
TYPE_CHECKING = False
|
|
4
|
+
if TYPE_CHECKING:
|
|
5
|
+
from typing import Tuple, Union
|
|
6
|
+
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
7
|
+
else:
|
|
8
|
+
VERSION_TUPLE = object
|
|
9
|
+
|
|
10
|
+
version: str
|
|
11
|
+
__version__: str
|
|
12
|
+
__version_tuple__: VERSION_TUPLE
|
|
13
|
+
version_tuple: VERSION_TUPLE
|
|
14
|
+
|
|
15
|
+
__version__ = version = '2024.10.0'
|
|
16
|
+
__version_tuple__ = version_tuple = (2024, 10, 0)
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Union, Optional, Dict
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
@dataclass
|
|
6
|
+
class Parameter:
|
|
7
|
+
eccodes_short_name: Optional[str] = None
|
|
8
|
+
eccodes_keys: Optional[Dict[str, int]] = None
|
|
9
|
+
wgrib2_name: Optional[str] = None
|
|
10
|
+
cemc_name: Optional[str] = None
|
|
11
|
+
|
|
12
|
+
def get_parameter(self) -> Optional[Union[str, Dict[str, int]]]:
|
|
13
|
+
"""
|
|
14
|
+
Return proper item for ``parameter`` param in reki's load_* functions.
|
|
15
|
+
|
|
16
|
+
Returns
|
|
17
|
+
-------
|
|
18
|
+
Optional[Union[str, Dict[str, int]]]
|
|
19
|
+
"""
|
|
20
|
+
if self.eccodes_short_name is not None:
|
|
21
|
+
return self.eccodes_short_name
|
|
22
|
+
if self.eccodes_keys is not None:
|
|
23
|
+
return self.eccodes_keys
|
|
24
|
+
if self.wgrib2_name is not None:
|
|
25
|
+
return self.wgrib2_name
|
|
26
|
+
if self.cemc_name is not None:
|
|
27
|
+
return self.cemc_name
|
|
28
|
+
return None
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class FieldInfo:
|
|
32
|
+
name: str
|
|
33
|
+
parameter: Parameter
|
|
34
|
+
level_type: Optional[Union[str, Dict[str, int]]] = None
|
|
35
|
+
level: Optional[Union[int, float, Dict[str, int]]] = None
|
|
36
|
+
additional_keys: Optional[Dict[str, Union[str, int, float]]] = None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# 2米温度
|
|
40
|
+
t_2m_info = FieldInfo(
|
|
41
|
+
name="t2m",
|
|
42
|
+
parameter=Parameter(
|
|
43
|
+
eccodes_short_name="2t",
|
|
44
|
+
),
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# 温度
|
|
49
|
+
t_info = FieldInfo(
|
|
50
|
+
name="t",
|
|
51
|
+
parameter=Parameter(
|
|
52
|
+
eccodes_short_name="t",
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# 位势高度
|
|
58
|
+
hgt_info = FieldInfo(
|
|
59
|
+
name="h",
|
|
60
|
+
parameter=Parameter(
|
|
61
|
+
eccodes_short_name="gh",
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# 海平面气压
|
|
67
|
+
mslp_info = FieldInfo(
|
|
68
|
+
name="mslp",
|
|
69
|
+
parameter=Parameter(
|
|
70
|
+
eccodes_short_name="prmsl",
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# 东西风
|
|
76
|
+
u_info = FieldInfo(
|
|
77
|
+
name="u",
|
|
78
|
+
parameter=Parameter(
|
|
79
|
+
eccodes_short_name="u",
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
# 南北风
|
|
85
|
+
v_info = FieldInfo(
|
|
86
|
+
name="v",
|
|
87
|
+
parameter=Parameter(
|
|
88
|
+
eccodes_short_name="v",
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# 雷达组合反射率
|
|
94
|
+
cr_info = FieldInfo(
|
|
95
|
+
name="cr",
|
|
96
|
+
parameter=Parameter(
|
|
97
|
+
wgrib2_name="CR",
|
|
98
|
+
# eccodes_keys=dict(
|
|
99
|
+
# disicpline=0,
|
|
100
|
+
# parameterCategory=16,
|
|
101
|
+
# parameterNumber=224,
|
|
102
|
+
# )
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
apcp_info = FieldInfo(
|
|
108
|
+
name="apcp",
|
|
109
|
+
parameter=Parameter(
|
|
110
|
+
wgrib2_name="APCP",
|
|
111
|
+
)
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
asnow_info = FieldInfo(
|
|
116
|
+
name="asnow",
|
|
117
|
+
parameter=Parameter(
|
|
118
|
+
wgrib2_name="ASNOW",
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
# 散度
|
|
124
|
+
div_info = FieldInfo(
|
|
125
|
+
name="div",
|
|
126
|
+
parameter=Parameter(
|
|
127
|
+
wgrib2_name="RELD"
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# K指数
|
|
134
|
+
k_index_info = FieldInfo(
|
|
135
|
+
name="k",
|
|
136
|
+
parameter=Parameter(
|
|
137
|
+
wgrib2_name="KX",
|
|
138
|
+
)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
# CIN
|
|
143
|
+
cin_info = FieldInfo(
|
|
144
|
+
name="cape",
|
|
145
|
+
parameter=Parameter(
|
|
146
|
+
wgrib2_name="CIN"
|
|
147
|
+
)
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
# 最优抬升指数
|
|
152
|
+
bpli_info = FieldInfo(
|
|
153
|
+
name="bpli",
|
|
154
|
+
parameter=Parameter(
|
|
155
|
+
wgrib2_name="BLI"
|
|
156
|
+
)
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
# CAPE
|
|
160
|
+
cape_info = FieldInfo(
|
|
161
|
+
name="cape",
|
|
162
|
+
parameter=Parameter(
|
|
163
|
+
wgrib2_name="CAPE"
|
|
164
|
+
)
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
# 水汽通量散度
|
|
168
|
+
qv_div_info = FieldInfo(
|
|
169
|
+
name="qv_div",
|
|
170
|
+
parameter=Parameter(
|
|
171
|
+
wgrib2_name="FRZR",
|
|
172
|
+
)
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
# 露点温度
|
|
176
|
+
dew_t_info = FieldInfo(
|
|
177
|
+
name="dpt",
|
|
178
|
+
parameter=Parameter(
|
|
179
|
+
wgrib2_name="DPT",
|
|
180
|
+
)
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
# 假相当位温
|
|
184
|
+
pte_info = FieldInfo(
|
|
185
|
+
name="pte",
|
|
186
|
+
parameter=Parameter(
|
|
187
|
+
wgrib2_name="EPOT",
|
|
188
|
+
)
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
# 垂直风切变
|
|
192
|
+
vwsh_info = FieldInfo(
|
|
193
|
+
name="vwsh",
|
|
194
|
+
parameter=Parameter(
|
|
195
|
+
eccodes_short_name="vwsh"
|
|
196
|
+
)
|
|
197
|
+
)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import xarray as xr
|
|
3
|
+
|
|
4
|
+
from .field_info import FieldInfo
|
|
5
|
+
from .source import DataSource
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class DataLoader:
|
|
9
|
+
def __init__(self, data_source: DataSource):
|
|
10
|
+
self.data_source = data_source
|
|
11
|
+
|
|
12
|
+
def load(
|
|
13
|
+
self,
|
|
14
|
+
field_info: FieldInfo,
|
|
15
|
+
start_time: pd.Timestamp,
|
|
16
|
+
forecast_time: pd.Timedelta,
|
|
17
|
+
) -> xr.DataArray or None:
|
|
18
|
+
"""
|
|
19
|
+
Load field from data loader.
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
field_info
|
|
24
|
+
start_time
|
|
25
|
+
forecast_time
|
|
26
|
+
|
|
27
|
+
Returns
|
|
28
|
+
-------
|
|
29
|
+
xr.DataArray or None
|
|
30
|
+
"""
|
|
31
|
+
field = self.data_source.retrieve(
|
|
32
|
+
field_info=field_info,
|
|
33
|
+
start_time=start_time,
|
|
34
|
+
forecast_time=forecast_time,
|
|
35
|
+
)
|
|
36
|
+
return field
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
from dataclasses import fields
|
|
2
|
+
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
import xarray as xr
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
from cedarkit.maps.util import AreaRange
|
|
9
|
+
|
|
10
|
+
from cedar_graph.metadata import BasePlotMetadata
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def prepare_data(plot_data, plot_metadata: BasePlotMetadata, total_area: AreaRange):
|
|
14
|
+
"""
|
|
15
|
+
Process all fields in plot_data according setting in plot_metadata.
|
|
16
|
+
Use generated fields replace those in plot_data.
|
|
17
|
+
|
|
18
|
+
Supported operators:
|
|
19
|
+
|
|
20
|
+
* extract_area: use ``total_area``
|
|
21
|
+
* sample_nearest: use ``plot_metadata.sample_step``
|
|
22
|
+
|
|
23
|
+
Parameters
|
|
24
|
+
----------
|
|
25
|
+
plot_data
|
|
26
|
+
some PlotData object for each plot.
|
|
27
|
+
plot_metadata
|
|
28
|
+
some PlotMetadata object for each plot.
|
|
29
|
+
total_area
|
|
30
|
+
used when auto_extract_area is set.
|
|
31
|
+
|
|
32
|
+
Returns
|
|
33
|
+
-------
|
|
34
|
+
PlotData
|
|
35
|
+
"""
|
|
36
|
+
auto_extract_area = plot_metadata.auto_extract_area
|
|
37
|
+
auto_sample_nearest = plot_metadata.auto_sample_nearest
|
|
38
|
+
|
|
39
|
+
field_names = set([
|
|
40
|
+
f.name for f in fields(plot_data)
|
|
41
|
+
if f.type == xr.DataArray and f.name.index("field_") != -1
|
|
42
|
+
])
|
|
43
|
+
|
|
44
|
+
if auto_extract_area:
|
|
45
|
+
for field_name in field_names:
|
|
46
|
+
field = getattr(plot_data, field_name)
|
|
47
|
+
plot_field = extract_area(field, area=total_area)
|
|
48
|
+
setattr(plot_data, field_name, plot_field)
|
|
49
|
+
|
|
50
|
+
if auto_sample_nearest:
|
|
51
|
+
sample_step = plot_metadata.sample_step
|
|
52
|
+
for field_name in field_names:
|
|
53
|
+
field = getattr(plot_data, field_name)
|
|
54
|
+
plot_field = sample_nearest(field, longitude_step=sample_step, latitude_step=sample_step)
|
|
55
|
+
setattr(plot_data, field_name, plot_field)
|
|
56
|
+
|
|
57
|
+
return plot_data
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def extract_area(field: xr.DataArray, area: AreaRange) -> xr.DataArray:
|
|
61
|
+
"""
|
|
62
|
+
extract field with area range.
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
field
|
|
67
|
+
area
|
|
68
|
+
|
|
69
|
+
Returns
|
|
70
|
+
-------
|
|
71
|
+
xr.DataArray
|
|
72
|
+
"""
|
|
73
|
+
longitude_range = slice(area.start_longitude, area.end_longitude)
|
|
74
|
+
latitude_range = slice(area.end_latitude, area.start_latitude)
|
|
75
|
+
extracted_array = field.sel(
|
|
76
|
+
longitude=longitude_range,
|
|
77
|
+
latitude=latitude_range,
|
|
78
|
+
)
|
|
79
|
+
return extracted_array
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def sample_nearest(field: xr.DataArray, longitude_step: float, latitude_step: Optional[float]=None) -> xr.DataArray:
|
|
83
|
+
"""
|
|
84
|
+
Sample field nearest to longitude and latitude step.
|
|
85
|
+
If field step is larger, return the original field without any change.
|
|
86
|
+
|
|
87
|
+
Parameters
|
|
88
|
+
----------
|
|
89
|
+
field
|
|
90
|
+
longitude_step
|
|
91
|
+
target longitude step, unit degree
|
|
92
|
+
latitude_step
|
|
93
|
+
target latitude step, unit degree
|
|
94
|
+
|
|
95
|
+
Returns
|
|
96
|
+
-------
|
|
97
|
+
xr.DataArray
|
|
98
|
+
|
|
99
|
+
"""
|
|
100
|
+
if latitude_step is None:
|
|
101
|
+
latitude_step = longitude_step
|
|
102
|
+
|
|
103
|
+
lat = field[field.dims[0]]
|
|
104
|
+
data_lat_step = abs(lat[0] - lat[1]).values
|
|
105
|
+
lon = field[field.dims[1]]
|
|
106
|
+
data_lon_step = abs(lon[0] - lon[1]).values
|
|
107
|
+
|
|
108
|
+
lat_ratio = int(np.round(latitude_step / data_lat_step))
|
|
109
|
+
lat_ratio = 1 if lat_ratio < 1 else lat_ratio
|
|
110
|
+
lon_ratio = int(np.round(longitude_step / data_lon_step))
|
|
111
|
+
lon_ratio = 1 if lon_ratio < 1 else lon_ratio
|
|
112
|
+
|
|
113
|
+
if lat_ratio == 1 and lon_ratio == 1:
|
|
114
|
+
return field
|
|
115
|
+
else:
|
|
116
|
+
sampled_field = field[::lat_ratio, ::lon_ratio]
|
|
117
|
+
return sampled_field
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Union, Optional, Callable
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
import xarray as xr
|
|
6
|
+
import pandas as pd
|
|
7
|
+
|
|
8
|
+
from reki.data_finder import find_local_file
|
|
9
|
+
from reki.format.grib.eccodes import load_field_from_file
|
|
10
|
+
|
|
11
|
+
from .field_info import FieldInfo
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class DataSource(ABC):
|
|
15
|
+
def __init__(self):
|
|
16
|
+
...
|
|
17
|
+
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def retrieve(
|
|
20
|
+
self, field_info: FieldInfo, start_time: pd.Timestamp, forecast_time: pd.Timedelta
|
|
21
|
+
) -> xr.DataArray or None:
|
|
22
|
+
"""
|
|
23
|
+
Retrieve field from data source.
|
|
24
|
+
|
|
25
|
+
Parameters
|
|
26
|
+
----------
|
|
27
|
+
field_info
|
|
28
|
+
start_time
|
|
29
|
+
forecast_time
|
|
30
|
+
|
|
31
|
+
Returns
|
|
32
|
+
-------
|
|
33
|
+
Optional[xr.DataArray]
|
|
34
|
+
"""
|
|
35
|
+
...
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def get_field_from_file(field_info: FieldInfo, file_path: Union[str, Path]) -> Optional[xr.DataArray]:
|
|
39
|
+
"""
|
|
40
|
+
Load field from local file according to field info.
|
|
41
|
+
|
|
42
|
+
Parameters
|
|
43
|
+
----------
|
|
44
|
+
field_info
|
|
45
|
+
Field info.
|
|
46
|
+
file_path
|
|
47
|
+
local file path.
|
|
48
|
+
|
|
49
|
+
Returns
|
|
50
|
+
-------
|
|
51
|
+
xr.DataArray
|
|
52
|
+
"""
|
|
53
|
+
additional_keys = field_info.additional_keys
|
|
54
|
+
if additional_keys is None:
|
|
55
|
+
additional_keys = dict()
|
|
56
|
+
field = load_field_from_file(
|
|
57
|
+
file_path,
|
|
58
|
+
parameter=field_info.parameter.get_parameter(),
|
|
59
|
+
level_type=field_info.level_type,
|
|
60
|
+
level=field_info.level,
|
|
61
|
+
**additional_keys,
|
|
62
|
+
)
|
|
63
|
+
return field
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
data_mapper = {
|
|
67
|
+
"CMA-MESO": "cma_meso_3km",
|
|
68
|
+
"CMA-GFS": "cma_gfs_gmf",
|
|
69
|
+
"CMA-TYM": "cma_tym",
|
|
70
|
+
"CMA-GEPS": "cma_geps",
|
|
71
|
+
"CMA-REPS": "cma_reps",
|
|
72
|
+
"CMA-MESO-1KM": "cma_meso_1km",
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def get_file_path(system_name, start_time, forecast_time, **kwargs) -> Union[str, Path]:
|
|
77
|
+
"""
|
|
78
|
+
Get file path using embedded system config files for CEMC systems.
|
|
79
|
+
|
|
80
|
+
Parameters
|
|
81
|
+
----------
|
|
82
|
+
system_name
|
|
83
|
+
start_time
|
|
84
|
+
forecast_time
|
|
85
|
+
kwargs
|
|
86
|
+
|
|
87
|
+
Returns
|
|
88
|
+
-------
|
|
89
|
+
Path or None
|
|
90
|
+
file path if found, None if not.
|
|
91
|
+
"""
|
|
92
|
+
data_type_system_name = data_mapper[system_name]
|
|
93
|
+
file_path = find_local_file(
|
|
94
|
+
f"{data_type_system_name}/grib2/orig",
|
|
95
|
+
start_time=start_time,
|
|
96
|
+
forecast_time=forecast_time,
|
|
97
|
+
**kwargs,
|
|
98
|
+
)
|
|
99
|
+
return file_path
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class LocalDataSource(DataSource):
|
|
103
|
+
"""
|
|
104
|
+
Data source for local files in CMA HPC system 1.
|
|
105
|
+
|
|
106
|
+
Notes
|
|
107
|
+
-----
|
|
108
|
+
use embedded config files in reki by default.
|
|
109
|
+
For other data source, please set ``file_path_func`` when object created.
|
|
110
|
+
"""
|
|
111
|
+
def __init__(self, system_name: str, file_path_func: Optional[Callable] = None):
|
|
112
|
+
super().__init__()
|
|
113
|
+
self.system_name = system_name
|
|
114
|
+
if file_path_func is None:
|
|
115
|
+
self.find_path_func = get_file_path
|
|
116
|
+
else:
|
|
117
|
+
self.find_path_func = file_path_func
|
|
118
|
+
|
|
119
|
+
def retrieve(
|
|
120
|
+
self, field_info: FieldInfo,
|
|
121
|
+
start_time: pd.Timestamp,
|
|
122
|
+
forecast_time: pd.Timedelta,
|
|
123
|
+
**kwargs,
|
|
124
|
+
) -> xr.DataArray or None:
|
|
125
|
+
file_path = self.find_path_func(
|
|
126
|
+
system_name=self.system_name,
|
|
127
|
+
start_time=start_time,
|
|
128
|
+
forecast_time=forecast_time,
|
|
129
|
+
**kwargs,
|
|
130
|
+
)
|
|
131
|
+
field = get_field_from_file(field_info=field_info, file_path=file_path)
|
|
132
|
+
return field
|
cedar_graph/logger.py
ADDED
cedar_graph/metadata.py
ADDED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Optional
|
|
3
|
+
from copy import deepcopy
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import pandas as pd
|
|
7
|
+
import xarray as xr
|
|
8
|
+
|
|
9
|
+
from cedarkit.maps.style import ContourStyle, ContourLabelStyle, BarbStyle
|
|
10
|
+
from cedarkit.maps.chart import Panel
|
|
11
|
+
from cedarkit.maps.domains import CnAreaMapTemplate, EastAsiaMapTemplate
|
|
12
|
+
from cedarkit.maps.colormap import get_ncl_colormap
|
|
13
|
+
from cedarkit.maps.util import AreaRange
|
|
14
|
+
|
|
15
|
+
from cedar_graph.metadata import BasePlotMetadata
|
|
16
|
+
from cedar_graph.data import DataLoader
|
|
17
|
+
from cedar_graph.data.field_info import u_info, v_info, bpli_info
|
|
18
|
+
from cedar_graph.data.operator import prepare_data
|
|
19
|
+
from cedar_graph.logger import get_logger
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
plot_logger = get_logger(__name__)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class PlotMetadata(BasePlotMetadata):
|
|
27
|
+
start_time: pd.Timestamp = None
|
|
28
|
+
forecast_time: pd.Timedelta = None
|
|
29
|
+
system_name: str = None
|
|
30
|
+
area_range: Optional[AreaRange] = None
|
|
31
|
+
area_name: str = None
|
|
32
|
+
wind_level: float = None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class PlotData:
|
|
37
|
+
field_bpli: xr.DataArray
|
|
38
|
+
field_u: xr.DataArray
|
|
39
|
+
field_v: xr.DataArray
|
|
40
|
+
wind_level: float
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def load_data(
|
|
44
|
+
data_loader: DataLoader,
|
|
45
|
+
start_time: pd.Timestamp,
|
|
46
|
+
forecast_time: pd.Timedelta,
|
|
47
|
+
wind_level: float,
|
|
48
|
+
**kwargs,
|
|
49
|
+
) -> PlotData:
|
|
50
|
+
plot_logger.debug("loading bpli...")
|
|
51
|
+
field_bpli = data_loader.load(
|
|
52
|
+
field_info=bpli_info,
|
|
53
|
+
start_time=start_time,
|
|
54
|
+
forecast_time=forecast_time,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
plot_logger.debug(f"loading u {wind_level}hPa...")
|
|
58
|
+
u_level_info = deepcopy(u_info)
|
|
59
|
+
u_level_info.level_type = "pl"
|
|
60
|
+
u_level_info.level = wind_level
|
|
61
|
+
field_u = data_loader.load(
|
|
62
|
+
field_info=u_level_info,
|
|
63
|
+
start_time=start_time,
|
|
64
|
+
forecast_time=forecast_time
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
plot_logger.debug(f"loading v {wind_level}hPa...")
|
|
68
|
+
v_level_info = deepcopy(v_info)
|
|
69
|
+
v_level_info.level_type = "pl"
|
|
70
|
+
v_level_info.level = wind_level
|
|
71
|
+
field_v = data_loader.load(
|
|
72
|
+
field_info=v_level_info,
|
|
73
|
+
start_time=start_time,
|
|
74
|
+
forecast_time=forecast_time
|
|
75
|
+
)
|
|
76
|
+
plot_logger.debug(f"loading done")
|
|
77
|
+
|
|
78
|
+
return PlotData(
|
|
79
|
+
field_bpli=field_bpli,
|
|
80
|
+
field_u=field_u,
|
|
81
|
+
field_v=field_v,
|
|
82
|
+
wind_level=wind_level,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def plot(plot_data: PlotData, plot_metadata: PlotMetadata) -> Panel:
|
|
87
|
+
system_name = plot_metadata.system_name
|
|
88
|
+
start_time = plot_metadata.start_time
|
|
89
|
+
forecast_time = plot_metadata.forecast_time
|
|
90
|
+
wind_level = plot_metadata.wind_level
|
|
91
|
+
area_range = plot_metadata.area_range
|
|
92
|
+
area_name = plot_metadata.area_name
|
|
93
|
+
|
|
94
|
+
bpli_levels = np.array([-48, -42, -36, -30, -24, -18, -12, -6, 0])
|
|
95
|
+
colormap_index = np.array([20, 19, 18, 16, 14, 12, 10, 8, 6, 4]) - 2
|
|
96
|
+
bpli_colormap = get_ncl_colormap("prcp_3", index=colormap_index)
|
|
97
|
+
|
|
98
|
+
bpli_style = ContourStyle(
|
|
99
|
+
colors=bpli_colormap,
|
|
100
|
+
levels=bpli_levels,
|
|
101
|
+
fill=True,
|
|
102
|
+
)
|
|
103
|
+
bpli_line_style = ContourStyle(
|
|
104
|
+
colors="black",
|
|
105
|
+
levels=bpli_levels,
|
|
106
|
+
linewidths=0.5,
|
|
107
|
+
fill=False,
|
|
108
|
+
label=True,
|
|
109
|
+
label_style=ContourLabelStyle(
|
|
110
|
+
fontsize=7,
|
|
111
|
+
background_color="white"
|
|
112
|
+
)
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
barb_style = BarbStyle(
|
|
116
|
+
barbcolor="black",
|
|
117
|
+
flagcolor="black",
|
|
118
|
+
linewidth=0.3,
|
|
119
|
+
# barb_increments=dict(half=2, full=4, flag=20)
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
# create domain
|
|
123
|
+
if plot_metadata.area_range is None:
|
|
124
|
+
domain = EastAsiaMapTemplate()
|
|
125
|
+
graph_name = f"BPLI(shadow) and {wind_level}hPa Wind(m/s)"
|
|
126
|
+
else:
|
|
127
|
+
domain = CnAreaMapTemplate(area=area_range)
|
|
128
|
+
graph_name = f"{area_name} BPLI(shadow) and {wind_level}hPa Wind(m/s)"
|
|
129
|
+
|
|
130
|
+
# prepare data
|
|
131
|
+
plot_logger.debug(f"preparing data...")
|
|
132
|
+
total_area = domain.total_area()
|
|
133
|
+
plot_data = prepare_data(plot_data=plot_data, plot_metadata=plot_metadata, total_area=total_area)
|
|
134
|
+
|
|
135
|
+
plot_field_bpli = plot_data.field_bpli
|
|
136
|
+
plot_field_u = plot_data.field_u
|
|
137
|
+
plot_field_v = plot_data.field_v
|
|
138
|
+
|
|
139
|
+
# create panel and plot
|
|
140
|
+
plot_logger.debug(f"plotting...")
|
|
141
|
+
panel = Panel(domain=domain)
|
|
142
|
+
panel.plot(plot_field_bpli, style=bpli_style)
|
|
143
|
+
panel.plot(plot_field_bpli, style=bpli_line_style)
|
|
144
|
+
panel.plot([[plot_field_u, plot_field_v]], style=barb_style, layer=[0])
|
|
145
|
+
|
|
146
|
+
domain.set_title(
|
|
147
|
+
panel=panel,
|
|
148
|
+
graph_name=graph_name,
|
|
149
|
+
system_name=system_name,
|
|
150
|
+
start_time=start_time,
|
|
151
|
+
forecast_time=forecast_time,
|
|
152
|
+
)
|
|
153
|
+
domain.add_colorbar(panel=panel, style=bpli_style)
|
|
154
|
+
plot_logger.debug(f"plotting...done")
|
|
155
|
+
|
|
156
|
+
return panel
|
|
File without changes
|