In python using os.makedirs('./folder1/folder2/folder3/',exist_ok=True) will create from the parentdir './' all the folders up to folder3
I wonder if the same logic can be applied to netcdf files when creating groups. I made this wrapper functions that given a root_grp inside a Netcdf4 file will create a child group inside.
def create_group(root_group: netCDF4.Group, group_name: str) -> None:
"""
create_group
creates a group inside a netcdf file
Parameters
----------
root_group : netCDF4.Group
root group, the new group will be a child of this one
group_name : str
name of the child group
"""
_ = root_group.createGroup(group_name)
I can the create groups this way.
rootgrp = netCDF4.Dataset(path+"test.nc", "w", format="NETCDF4")
_ = root_group.createGroup(group_name)
create_group(rootgrp,'first')
create_group(rootgrp['first'],'second')
I was thinking of a way to do it recursively. is it possible?
I tried in this way
rootgrp = netCDF4.Dataset(path+"test.nc", "w", format="NETCDF4")
path = 'first/second/third'
groups = [grp for grp in path.split('/') if grp != '']
print(groups)
count =0
while count < len(groups):
current= groups[count]
rootgrp.createGroup(current)
print(current)
rootgrp=rootgrp[current]
count+=1
But it creates one group /first/second/third instead of 3 nested ones.