Skip to content
Snippets Groups Projects

Sanitize cal_tools.tools.get_creation_date

Merged Cyril Danilevski requested to merge clean/get_dir_creation_date into master
Files
2
@@ -231,19 +231,18 @@ def get_run_info(proposal, run):
def get_dir_creation_date(directory: str, run: int,
use_dir: Optional[bool] = False,
verbosity: Optional[int] = 0) -> datetime.datetime:
"""
Return run start time from MyDC.
If not available from MyMDC, retrieve the data from the dataset's metadata
in [directory]/[run], or from the folder on disk if `use_dir` is set.
in [directory]/[run] or, if the dataset is older than 2020, from the
directory's creation time.
If the files are not available, this function will raise a ValueError.
If the data is not available from either source, this function will raise a
ValueError.
:param directory: path to directory which contains runs
:param run: run number
:param use_dir: get the creation time from the directory, needed for older
datasets.
:param verbosity: Level of verbosity (0 - silent)
:return: (datetime) modification time
"""
directory = Path(directory)
proposal = int(directory.parent.name[1:])
@@ -265,3+264,3 @@
ntries = 100
while ntries > 0:
try:
if use_dir:
return datetime.datetime.fromtimestamp(directory.stat().st_ctime) # noqa
dates = []
for f in directory.glob('*.h5'):
with h5py.File(f, 'r') as fin:
@@ -274,13 +271,10 @@ def get_dir_creation_date(directory: str, run: int,
cdate = datetime.datetime.strptime(cdate, "%Y%m%dT%H%M%SZ")
dates.append(cdate)
return min(dates)
except (IOError, ValueError):
ntries -= 1
except KeyError:
msg = ("It seems that you're working on old data. Try setting "
"`use_dir=True` to get file creation date")
raise ValueError(msg) from None
except KeyError: # The files are here, but it's an older dataset
return datetime.datetime.fromtimestamp(directory.stat().st_ctime)
msg = 'Could not get the creation time from the directory'
raise ValueError(msg, directory)
Loading