From be044a9768918ae4fe0e1f89453cf559a474462f Mon Sep 17 00:00:00 2001 From: Alfredo Oliviero Date: Mon, 4 Nov 2024 12:11:20 +0100 Subject: [PATCH] datadir --- d4science_copernicus_cds/datadir_cds.py | 35 +++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/d4science_copernicus_cds/datadir_cds.py b/d4science_copernicus_cds/datadir_cds.py index 9f40780..e8028a9 100644 --- a/d4science_copernicus_cds/datadir_cds.py +++ b/d4science_copernicus_cds/datadir_cds.py @@ -1,14 +1,33 @@ +import os import datetime as dt import calendar -import os +import re -DEFAULT_BASEPTH='dataDir' +DEFAULT_BASEPTH = '~/cds_dataDir' -def cds_datadir(basepath=DEFAULT_BASEPTH): - #strike_time = dt.datetime.strptime('01/01/79 00:00:00.000', '%m/%d/%y %H:%M:%S.%f') - #DAY = (dt.datetime.now() - strike_time).total_seconds() # seconds from the first day of the observation - tstamp = calendar.timegm(dt.datetime.now().timetuple()) - datadir = os.path.join( basepath,str(tstamp)) # - os.makedirs(datadir) +def cds_datadir(label=None, basepath=DEFAULT_BASEPTH, verbose=True): + # Expand user directory + basepath = os.path.expanduser(basepath) + + # Get current timestamp in readable format + timestamp = dt.datetime.now().strftime('%Y_%m_%d_%H_%M_%S') + + # Sanitize and truncate label if provided + if label: + sanitized_label = re.sub(r'[^a-zA-Z0-9_-]', '', label)[:20] + else: + sanitized_label = '' + + # Create directory name with optional label + folder_name = f"out_{timestamp}_{sanitized_label}" if sanitized_label else f"out_{timestamp}" + datadir = os.path.join(basepath, folder_name) + + # Create the directory + os.makedirs(datadir, exist_ok=True) + if verbose: + print ("datadir: %s", datadir ) return datadir +# Example usage +directory = cds_datadir(label='example_label') +print(directory)