4
4
5
5
import json
6
6
import logging
7
+ import warnings
7
8
from typing import Any , Hashable
8
9
9
10
import xarray as xr
@@ -91,19 +92,29 @@ def write_levels(
91
92
If `False`, the default, a level dataset `{target_path}/0.zarr`
92
93
will be written instead.
93
94
zappend_config:
94
- Configuration passed to the `zappend()` call for each
95
- slice in the append dimension.
95
+ Configuration passed to zappend as `zappend(slice, **zappend_config)`
96
+ for each slice in the append dimension. The zappend `config`
97
+ parameter is not supported.
96
98
"""
97
- from xcube .core .tilingscheme import get_num_levels
98
99
from xcube .core .gridmapping import GridMapping
99
100
from xcube .core .subsampling import get_dataset_agg_methods
100
101
from xcube .core .subsampling import subsample_dataset
102
+ from xcube .core .tilingscheme import get_num_levels
101
103
from xcube .util .fspath import get_fs_path_class
102
104
105
+ dry_run = zappend_config .pop ("dry_run" , False )
106
+
107
+ if dry_run and use_saved_levels :
108
+ warnings .warn (f"'use_saved_levels' argument is not applicable if dry_run=True" )
109
+ use_saved_levels = False
110
+ config = zappend_config .pop ("config" , None )
111
+ if config is not None :
112
+ raise TypeError ("write_levels() got an unexpected keyword argument 'config'" )
113
+
103
114
target_dir = zappend_config .pop ("target_dir" , None )
104
- if not target_dir and not target_path :
105
- raise ValueError ("either 'target_dir' or ' target_path' can be given, not both " )
106
- if target_dir and target_path and target_dir != target_path :
115
+ if not target_path and not target_dir :
116
+ raise ValueError ("missing ' target_path' argument " )
117
+ if target_dir and target_path :
107
118
raise ValueError ("either 'target_dir' or 'target_path' can be given, not both" )
108
119
target_path = target_path or target_dir
109
120
target_storage_options = zappend_config .pop (
@@ -161,17 +172,24 @@ def write_levels(
161
172
variables = zappend_config .pop ("variables" , None ),
162
173
)
163
174
164
- target_fs .mkdirs (target_root , exist_ok = True )
165
- with target_fs .open (f"{ target_root } /.zlevels" , "wt" ) as fp :
166
- levels_data : dict [str , Any ] = dict (
167
- version = "1.0" ,
168
- num_levels = num_levels ,
169
- agg_methods = agg_methods ,
170
- use_saved_levels = use_saved_levels ,
171
- )
172
- json .dump (levels_data , fp , indent = 2 )
173
-
174
- if link_level_zero :
175
+ if target_fs .exists (target_root ):
176
+ if target_fs .exists (target_root ):
177
+ logger .warning (f"Permanently deleting { target_dir } " )
178
+ if not dry_run :
179
+ target_fs .rm (target_root , recursive = True )
180
+
181
+ if not dry_run :
182
+ target_fs .mkdirs (target_root , exist_ok = True )
183
+ with target_fs .open (f"{ target_root } /.zlevels" , "wt" ) as fp :
184
+ levels_data : dict [str , Any ] = dict (
185
+ version = "1.0" ,
186
+ num_levels = num_levels ,
187
+ agg_methods = agg_methods ,
188
+ use_saved_levels = use_saved_levels ,
189
+ )
190
+ json .dump (levels_data , fp , indent = 2 )
191
+
192
+ if link_level_zero and not dry_run :
175
193
path_class = get_fs_path_class (target_fs )
176
194
rel_source_path = (
177
195
"../"
@@ -218,6 +236,7 @@ def write_levels(
218
236
target_dir = level_slice_path ,
219
237
target_storage_options = target_storage_options ,
220
238
append_dim = append_dim ,
239
+ dry_run = dry_run ,
221
240
force_new = force_new if slice_index == 0 else False ,
222
241
variables = variables ,
223
242
** zappend_config ,
@@ -227,13 +246,13 @@ def write_levels(
227
246
100 * ((slice_index * num_levels ) + level_index + 1 ) / steps_total
228
247
)
229
248
logger .info (
230
- f"Slice { level_slice_path } written ,"
249
+ f"Level slice written to { level_slice_path } ,"
231
250
f" { slice_index + 1 } /{ num_slices } slices,"
232
251
f" { level_index + 1 } /{ num_levels } levels,"
233
252
f" { percent_total :.2f} % total"
234
253
)
235
-
236
- logger .info (f"Done appending { num_slices } slices to { target_path } " )
254
+ logger . info ( f"Done appending { num_levels } level slices to { target_path } " )
255
+ logger .info (f"Done appending { num_slices } slices to { target_path } " )
237
256
238
257
239
258
def get_variables_config (
0 commit comments