import numpy as np
import argparse
import os
def restore_arrays(data_dir, array_names, output_file, overwrite=False):
"""
Restores data arrays from a directory.
Args:
data_dir (str): Path to the directory containing the array data.
array_names (list): List of array names to restore.
output_file (str): Path to the output file for saving the restored arrays.
overwrite (bool): Whether to overwrite existing arrays in the output file.
"""
try:
with np.nditer(data_dir) as f: # Iterate through the data directory
for name in array_names:
filepath = os.path.join(data_dir, name)
if not os.path.exists(filepath):
print(f"Warning: File not found: {filepath}")
continue
try:
array = np.load(filepath) # Load the array
if overwrite and os.path.exists(output_file):
np.save(output_file, array) # Overwrite if specified
else:
np.save(output_file, array)
print(f"Restored and saved: {name}")
except Exception as e:
print(f"Error restoring {name}: {e}")
except Exception as e:
print(f"Error processing data directory: {e}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Restore data arrays from a directory.')
parser.add_argument('data_dir', type=str, help='Path to the directory containing the array data.')
parser.add_argument('array_names', nargs='+', type=str, help='List of array names to restore.')
parser.add_argument('output_file', type=str, help='Path to the output file for saving the restored arrays.')
parser.add_argument('--overwrite', action='store_true', help='Overwrite existing arrays in the output file.')
args = parser.parse_args()
restore_arrays(args.data_dir, args.array_names, args.output_file, args.overwrite)
Add your comment