Skip to content

Commit 1b825a9

Browse files
committed
Fixes to allow successfull use of subarray_shape in s3nc_cfa_split.py
1 parent 70a9b6e commit 1b825a9

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

S3netCDF4/CFA/_CFAClasses.pyx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,13 @@ cdef class CFAGroup:
425425
)
426426
else:
427427
# check the size of the specified subarray
428-
if len(subarray_shape) != len(dim_names):
428+
if subarray_shape.size != len(dim_names):
429429
raise CFASubArrayError(
430430
"Number of dimensions in subarray_shape does not match"
431431
" those in shape."
432432
)
433433
# check each dimension is not longer than that in array
434-
for i in range(0, len(subarray_shape)):
434+
for i in range(0, subarray_shape.size):
435435
if subarray_shape[i] > self.cfa_dims[dim_names[i]].getLen():
436436
raise CFASubArrayError(
437437
"Dimension in desired sub_array is larger than"
@@ -902,10 +902,16 @@ cdef class CFAVariable:
902902
"""Create the file name path that is common between all subarray files"""
903903
# create the base filename
904904
file_path = self.getGroup().getDataset().getName()
905-
# trim the ".nc" off the end
906-
file_path = file_path.replace(".nc", "")
907-
# get just the filename, stripped of ".nc"
908-
file_name = os.path.basename(file_path).replace(".nc", "")
905+
# trim the ".nc" or ".nca" off the end
906+
if ".nca" in file_path: # note order is important as nca contains nc
907+
sf = ".nca"
908+
elif ".nc" in file_path:
909+
sf = ".nc"
910+
else:
911+
sf = ""
912+
file_path = file_path.replace(sf, "")
913+
# get just the filename, stripped of ".nc" or ".nca"
914+
file_name = os.path.basename(file_path).replace(sf, "")
909915
# construct the base path of the subarray filenames
910916
base_filename = file_path + "/" + file_name
911917
# add the group if it is not root

bin/s3nc_cfa_split.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,20 @@ def copy_vars(nc_object, s3_object, subarray_size, subarray_shape=[]):
3535
fill_value = None
3636
# create the variable - the createVariable method needs to distinguish
3737
# between whether the shape or size has been passed in
38+
# Also, if the subarray_shape has been passed in then only attempt to
39+
# do it for variables with the same number of dimensions as the subarray
40+
# shape.
41+
use_shape = False
3842
if subarray_shape!=[]:
43+
shape_list = [int(x) for x in subarray_shape.strip("[]").split(",")]
44+
shape_array = np.array(shape_list)
45+
# check if shape_array.size is the same as the number of dims in the
46+
# netCDF variable
47+
if(shape_array.size == nc_var.ndim):
48+
use_shape = True
49+
50+
if use_shape:
51+
# subarray shape at this moment is a string, [a,b,c,d]
3952
s3_var = s3_object.createVariable(
4053
# can only fill in endian from original dataset as
4154
# other initialisation variables are not stored in the
@@ -45,7 +58,7 @@ def copy_vars(nc_object, s3_object, subarray_size, subarray_shape=[]):
4558
endian=nc_var.endian(),
4659
fill_value=fill_value,
4760
dimensions=nc_var.dimensions,
48-
max_subarray_shape=subarray_shape)
61+
subarray_shape=shape_array)
4962
else:
5063
s3_var = s3_object.createVariable(
5164
# can only fill in endian from original dataset as
@@ -209,7 +222,7 @@ def split_into_CFA(output_path, input_path,
209222

210223
parser.add_argument(
211224
"--subarray_shape", action="store", default=[],
212-
metavar="<subarray shape>",
225+
metavar="<subarray_shape>",
213226
help=(
214227
"Shape for the subarray files (optional). Without this argument, "
215228
"the shape will be automatically determined."

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104

105105
setup(
106106
name='S3netCDF4',
107-
version='2.0.10',
107+
version='2.0.11',
108108
packages=['S3netCDF4'],
109109
install_requires=[
110110
'numpy>=1.19.0',

0 commit comments

Comments
 (0)