From 4ba322216a2e7c7dec1dba21f24d46ef0ba6ec0c Mon Sep 17 00:00:00 2001 From: "Eugeniy E. Mikhailov" Date: Sat, 4 Dec 2021 13:31:40 -0500 Subject: working draft of file utilites --- qolab/file_utils/__init__.py | 51 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) (limited to 'qolab/file_utils/__init__.py') diff --git a/qolab/file_utils/__init__.py b/qolab/file_utils/__init__.py index 044f3c7..67e64c8 100644 --- a/qolab/file_utils/__init__.py +++ b/qolab/file_utils/__init__.py @@ -1,6 +1,7 @@ import platform import re import os +from datetime import date def filename2os_fname( fname ): # filename2os_fname translate Win or Linux fname to OS dependent style @@ -16,15 +17,55 @@ def filename2os_fname( fname ): return (fname) -def get_runnum(savepath): - # For the provided datapath: +def get_runnum(data_dir): + # For the provided data_dir: # reads, increments data counter and saves it back. # If nesessary creates counter file and full path to it. # example # get_runnum('Z:\Ramsi_EIT\data\') # get_runnum('/mnt/qol_grp_data/data') - pass + data_dir = filename2os_fname( data_dir ); + if not os.path.exists(data_dir): + os.mkdir(data_dir) + if not os.path.isdir(data_dir): + print(f"ERROR: cannot create directory for data: {data_dir}") + print(f"Will use current dir for storage") + data_dir = "." -def get_next_data_file(prefix, savepath): - run_number = get_runnum( savepath ) + runnumpath = os.path.join(data_dir, 'autofile') + # convert to OS dependent way + runnumpath = filename2os_fname( runnumpath ); + + if not os.path.exists(runnumpath): + os.mkdir(runnumpath) + runnum_file = os.path.join(runnumpath, 'runnum.dat'); + runnum_file = filename2os_fname( runnum_file ); + print(runnum_file) + + run_number = 0 + if os.path.isfile(runnum_file): + with open(runnum_file, 'r') as f: + content = f.readlines() + run_number = int(content[0]) + f.close() + + # Increment it and fold if needed + run_number = run_number + 1; + # Important: we are using five digit counters to synchronize + # with qol_get_next_data_file.m + if run_number > 99999: + run_number = 0 + + with open(runnum_file, 'w') as f: + f.write(f'{run_number}') + f.close() + return(run_number) + +def get_next_data_file(prefix, savepath, run_number=None, date_format='%Y%m%d', extention='dat'): + if run_number is None: + run_number = get_runnum( savepath ) + today = date.today() + datestr = today.strftime(date_format) + fname = os.path.join(savepath, f'{prefix}_{datestr}_f{run_number:05d}.{extention}') + return(fname) -- cgit v1.2.3