Skip to content

Commit ca852f0

Browse files
committed
Generic command to write file zarr or netcdf
1 parent 33a2e46 commit ca852f0

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

src/py_eddy_tracker/observations/observation.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,18 +1113,25 @@ def create_variable_zarr(
11131113
except ValueError:
11141114
logging.warning("Data is empty")
11151115

1116-
def write_netcdf(self, path="./", filename="%(path)s/%(sign_type)s.nc"):
1116+
def write_file(self, path="./", filename="%(path)s/%(sign_type)s.nc", zarr_flag=False):
11171117
"""Write a netcdf with eddy obs
11181118
"""
1119-
eddy_size = len(self.observations)
11201119
filename = filename % dict(
11211120
path=path,
11221121
sign_type=self.sign_legend,
11231122
prod_time=datetime.now().strftime("%Y%m%d"),
11241123
)
1124+
if zarr_flag:
1125+
filename = filename.replace('.nc', '.zarr')
1126+
if filename.endswith('.zarr'):
1127+
zarr_flag = True
11251128
logging.info("Store in %s", filename)
1126-
with Dataset(filename, "w", format="NETCDF4") as handler:
1127-
self.to_netcdf(handler)
1129+
if zarr_flag:
1130+
handler = zarr.open(filename, 'w')
1131+
self.to_zarr(handler)
1132+
else:
1133+
with Dataset(filename, "w", format="NETCDF4") as handler:
1134+
self.to_netcdf(handler)
11281135

11291136
@property
11301137
def global_attr(self):

src/scripts/EddyFinalTracking

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ if __name__ == '__main__':
5656

5757
if CONFIG.eddies_untracked_model is None:
5858
CONFIG.eddies_untracked_model = '%(path)s/%(sign_type)s_%(prod_time)s_untracked.nc'
59-
CORRESPONDANCES.get_unused_data(raw_data=True).write_netcdf(path=SAVE_DIR, filename=CONFIG.eddies_untracked_model)
59+
CORRESPONDANCES.get_unused_data(raw_data=True).write_file(path=SAVE_DIR, filename=CONFIG.eddies_untracked_model)
6060

6161
SHORT_CORRESPONDANCES = CORRESPONDANCES._copy()
6262
SHORT_CORRESPONDANCES.shorter_than(size_max=NB_OBS_MIN)
@@ -84,5 +84,5 @@ if __name__ == '__main__':
8484
CONFIG.eddies_long_model = '%(path)s/%(sign_type)s_%(prod_time)s.nc'
8585
if CONFIG.eddies_short_model is None:
8686
CONFIG.eddies_short_model = '%(path)s/%(sign_type)s_%(prod_time)s_track_too_short.nc'
87-
FINAL_EDDIES.write_netcdf(filename=CONFIG.eddies_long_model, path=SAVE_DIR)
88-
SHORT_TRACK.write_netcdf(filename=CONFIG.eddies_short_model, path=SAVE_DIR)
87+
FINAL_EDDIES.write_file(filename=CONFIG.eddies_long_model, path=SAVE_DIR)
88+
SHORT_TRACK.write_file(filename=CONFIG.eddies_short_model, path=SAVE_DIR)

src/scripts/EddySubSetter

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ if __name__ == '__main__':
7474
if len(dataset) == 0:
7575
logging.warning("No data are selected, out file couldn't be create")
7676
else:
77-
dataset.write_netcdf(filename=args.filename_out)
77+
dataset.write_file(filename=args.filename_out)

src/scripts/EddyTracking

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ def usage():
9898
action='store_true',
9999
help='Stop tracking after correspondance computation,'
100100
' merging can be done with EddyFinalTracking')
101+
parser.add_argument('--zarr',
102+
action='store_true',
103+
help='Output will be wrote in zarr')
101104
parser.add_argument('--blank_period',
102105
type=int,
103106
default=0,
@@ -109,11 +112,11 @@ def usage():
109112
config = yaml_load(stream)
110113
if args.correspondance_in is not None and not exists(args.correspondance_in):
111114
args.correspondance_in = None
112-
return config, args.save_correspondance_and_stop, args.correspondance_in, args.correspondance_out, args.blank_period
115+
return config, args.save_correspondance_and_stop, args.correspondance_in, args.correspondance_out, args.blank_period, args.zarr
113116

114117

115118
if __name__ == '__main__':
116-
CONFIG, SAVE_STOP, CORRESPONDANCES_IN, CORRESPONDANCES_OUT, BLANK_PERIOD = usage()
119+
CONFIG, SAVE_STOP, CORRESPONDANCES_IN, CORRESPONDANCES_OUT, BLANK_PERIOD, ZARR = usage()
117120

118121
# Create output directory
119122
SAVE_DIR = CONFIG['PATHS'].get('SAVE_DIR', None)
@@ -184,7 +187,7 @@ if __name__ == '__main__':
184187
logging.info('The longest tracks have %d observations', CORRESPONDANCES.nb_obs_by_tracks.max())
185188
logging.info('The mean length is %d observations before filtering', CORRESPONDANCES.nb_obs_by_tracks.mean())
186189

187-
CORRESPONDANCES.get_unused_data().write_netcdf(path=SAVE_DIR, filename='%(path)s/%(sign_type)s_untracked.nc')
190+
CORRESPONDANCES.get_unused_data().write_file(path=SAVE_DIR, filename='%(path)s/%(sign_type)s_untracked.nc', zarr_flag=ZARR)
188191

189192
SHORT_CORRESPONDANCES = CORRESPONDANCES._copy()
190193
SHORT_CORRESPONDANCES.shorter_than(size_max=NB_OBS_MIN)
@@ -210,5 +213,5 @@ if __name__ == '__main__':
210213
logging.info('Longer track saved have %d obs', CORRESPONDANCES.nb_obs_by_tracks.max())
211214
logging.info('The mean length is %d observations after filtering', CORRESPONDANCES.nb_obs_by_tracks.mean())
212215

213-
FINAL_EDDIES.write_netcdf(path=SAVE_DIR)
214-
SHORT_TRACK.write_netcdf(filename='%(path)s/%(sign_type)s_track_too_short.nc', path=SAVE_DIR)
216+
FINAL_EDDIES.write_file(path=SAVE_DIR, zarr_flag=ZARR)
217+
SHORT_TRACK.write_file(filename='%(path)s/%(sign_type)s_track_too_short.nc', path=SAVE_DIR, zarr_flag=ZARR)

0 commit comments

Comments
 (0)