22from __future__ import annotations
33
44import datetime as dt
5+ import logging
56from pathlib import Path
67from typing import Any
78from typing import Sequence
89
910import numpy as np
1011import pandas as pd
12+ import xarray as xr
1113from tqdm .auto import tqdm
1214
1315from specsanalyzer .config import complete_dictionary
1416from specsscan .metadata import MetadataRetriever
1517
18+ # Configure logging
19+ logger = logging .getLogger ("specsanalyzer.specsscan" )
20+
1621
1722def get_scan_path (path : Path | str , scan : int , basepath : Path | str ) -> Path :
1823 """Returns the path to the given scan.
@@ -123,7 +128,7 @@ def load_images(
123128 "load_scan method." ,
124129 ) from exc
125130
126- print (f"Averaging over { avg_dim } ..." )
131+ logger . info (f"Averaging over { avg_dim } ..." )
127132 for dim in tqdm (raw_2d_sliced ):
128133 avg_list = []
129134 for image in tqdm (dim , leave = False , disable = not tqdm_enable_nested ):
@@ -210,14 +215,14 @@ def parse_lut_to_df(scan_path: Path) -> pd.DataFrame:
210215 df_lut .reset_index (inplace = True )
211216
212217 new_cols = df_lut .columns .to_list ()[1 :]
213- new_cols [new_cols .index ("delaystage" )] = "Delay "
218+ new_cols [new_cols .index ("delaystage" )] = "DelayStage "
214219 new_cols .insert (3 , "delay (fs)" ) # Create label to drop the column later
215220
216221 df_lut = df_lut .set_axis (new_cols , axis = "columns" )
217222 df_lut .drop (columns = "delay (fs)" , inplace = True )
218223
219224 except FileNotFoundError :
220- print (
225+ logger . info (
221226 "LUT.txt not found. Storing metadata from info.txt" ,
222227 )
223228 return None
@@ -265,7 +270,7 @@ def get_coords(
265270 return (np .array ([]), "" )
266271
267272 if df_lut is not None :
268- print ("scanvector.txt not found. Obtaining coordinates from LUT" )
273+ logger . info ("scanvector.txt not found. Obtaining coordinates from LUT" )
269274
270275 df_new : pd .DataFrame = df_lut .loc [:, df_lut .columns [2 :]]
271276
@@ -276,13 +281,18 @@ def get_coords(
276281 raise FileNotFoundError ("scanvector.txt file not found!" ) from exc
277282
278283 if scan_type == "delay" :
279- t_0 = scan_info ["TimeZero" ]
280- coords -= t_0
281- coords *= 2 / 3e11 * 1e15
284+ t0 = scan_info ["TimeZero" ]
285+ coords = mm_to_fs (coords , t0 )
282286
283287 return coords , dim
284288
285289
290+ def mm_to_fs (delaystage : xr .DataArray | np .ndarray | float , t0 : float ) -> float :
291+ delay = delaystage - t0
292+ delay *= 2 / 2.99792458e11 * 1e15
293+ return delay
294+
295+
286296def compare_coords (axis_data : np .ndarray ) -> tuple [np .ndarray , int ]:
287297 """Identifies the most changing column in a given 2-D numpy array.
288298
@@ -338,6 +348,9 @@ def parse_info_to_dict(path: Path) -> dict:
338348 except FileNotFoundError as exc :
339349 raise FileNotFoundError ("info.txt file not found." ) from exc
340350
351+ if "DelayStage" in info_dict and "TimeZero" in info_dict :
352+ info_dict ["delay" ] = mm_to_fs (info_dict ["DelayStage" ], info_dict ["TimeZero" ])
353+
341354 return info_dict
342355
343356
@@ -377,7 +390,7 @@ def handle_meta(
377390 if metadata is None :
378391 metadata = {}
379392
380- print ("Gathering metadata from different locations" )
393+ logger . info ("Gathering metadata from different locations" )
381394 # get metadata from LUT dataframe
382395 lut_meta = {}
383396 energy_scan_mode = "snapshot"
@@ -395,10 +408,10 @@ def handle_meta(
395408
396409 metadata ["scan_info" ] = complete_dictionary (
397410 metadata .get ("scan_info" , {}),
398- complete_dictionary (lut_meta , scan_info ),
411+ complete_dictionary (scan_info , lut_meta ),
399412 ) # merging dictionaries
400413
401- print ("Collecting time stamps..." )
414+ logger . info ("Collecting time stamps..." )
402415 if "time" in metadata ["scan_info" ]:
403416 time_list = [metadata ["scan_info" ]["time" ][0 ], metadata ["scan_info" ]["time" ][- 1 ]]
404417 elif "StartTime" in metadata ["scan_info" ]:
@@ -445,8 +458,6 @@ def handle_meta(
445458 metadata ["scan_info" ]["slow_axes" ] = slow_axes
446459 metadata ["scan_info" ]["fast_axes" ] = fast_axes
447460
448- print ("Done!" )
449-
450461 return metadata
451462
452463
@@ -460,7 +471,7 @@ def find_scan(path: Path, scan: int) -> list[Path]:
460471 Returns:
461472 List[Path]: scan_path: Path object pointing to the scan folder
462473 """
463- print ("Scan path not provided, searching directories..." )
474+ logger . info ("Scan path not provided, searching directories..." )
464475 for file in path .iterdir ():
465476 if file .is_dir ():
466477 try :
@@ -474,7 +485,7 @@ def find_scan(path: Path, scan: int) -> list[Path]:
474485 file .glob (f"*/*/Raw Data/{ scan } " ),
475486 )
476487 if scan_path :
477- print ( "Scan found at path:" , scan_path [0 ])
488+ logger . info ( f "Scan found at path: { scan_path [0 ]} " )
478489 break
479490 else :
480491 scan_path = []
0 commit comments