@@ -64,22 +64,19 @@ def normalise_pulse(average):
6464
6565def get_serial_device_information ():
6666 try :
67- # Temporarily stop pulse data mode
68- process_03 ('-sto' ) # Stop pulse data
69- time .sleep (0.05 )
7067
71- process_03 ('-mode 0' ) # Reset to default mode
72- time .sleep (0.05 )
73-
74- # Send the `"-inf"` command
7568 with shproto .dispatcher .command_lock :
76- shproto . dispatcher . command = "-inf"
69+
7770 time .sleep (0.5 ) # Allow time for response
78-
79- # Retrieve the device information
80- with shproto .dispatcher .command_lock :
71+
72+ shproto .dispatcher .command = "-inf" # Send the `"-inf"` command
73+
74+ time .sleep (0.5 ) # Allow time for response
75+
8176 device_info = shproto .dispatcher .inf_str
82- time .sleep (0.05 )
77+
78+ time .sleep (0.5 )
79+
8380 shproto .dispatcher .inf_str = "" # Clear for subsequent commands
8481
8582 return device_info if device_info else "No response from device"
@@ -565,24 +562,49 @@ def stop_recording():
565562 logger .info ('functions recording stopped\n ' )
566563 return
567564
565+ import os
566+ import json
567+ import csv
568+
569+ def get_unique_filename (directory , filename ):
570+ """
571+ Generate a unique filename by appending (1), (2), etc., if the file already exists.
572+ """
573+ base , ext = os .path .splitext (filename )
574+ counter = 1
575+ new_filename = filename
576+
577+ while os .path .exists (os .path .join (directory , new_filename )):
578+ new_filename = f"{ base } ({ counter } ){ ext } "
579+ counter += 1
580+
581+ return new_filename
582+
568583def export_csv (filename , data_directory , calib_switch ):
569584 download_folder = os .path .expanduser ("~/Downloads" )
570- output_file = f'{ filename } .csv'
571-
572- with open (os .path .join (data_directory , f'{ filename } .json' )) as f :
573- data = json .load (f )
585+ output_file = get_unique_filename (download_folder , f'{ filename } .csv' )
574586
575- if data ["schemaVersion" ] == "NPESv2" :
576- data = data ["data" ][0 ]
587+ try :
588+ with open (os .path .join (data_directory , f'{ filename } .json' )) as f :
589+ data = json .load (f )
590+ except FileNotFoundError :
591+ print (f"Error: { filename } .json not found in { data_directory } " )
592+ return
577593
578- spectrum = data [ "resultData" ][ "energySpectrum" ][ "spectrum" ]
579- coefficients = data ["resultData " ]["energySpectrum" ][ "energyCalibration" ][ "coefficients" ]
594+ if data . get ( "schemaVersion" ) == "NPESv2" :
595+ data = data ["data " ][0 ]
580596
597+ try :
598+ spectrum = data ["resultData" ]["energySpectrum" ]["spectrum" ]
599+ coefficients = data ["resultData" ]["energySpectrum" ]["energyCalibration" ]["coefficients" ]
600+ except KeyError :
601+ print (f"Error: Missing expected keys in { filename } .json" )
602+ return
581603
582604 # Ensure the download folder exists
583- if not os .path . exists (download_folder ):
584- os . makedirs ( download_folder )
585-
605+ os .makedirs (download_folder , exist_ok = True )
606+
607+ # Write data to CSV
586608 with open (os .path .join (download_folder , output_file ), "w" , newline = "" ) as f :
587609 writer = csv .writer (f )
588610 writer .writerow (["energy" , "counts" ])
@@ -593,6 +615,8 @@ def export_csv(filename, data_directory, calib_switch):
593615 energy = i
594616 writer .writerow ([energy , value ])
595617
618+
619+
596620def update_coeff (filename ):
597621 with global_vars .write_lock :
598622 data_directory = global_vars .data_directory
@@ -759,22 +783,14 @@ def generate_device_settings_table():
759783 shproto .dispatcher .spec_stopflag = 0
760784 dispatcher = threading .Thread (target = shproto .dispatcher .start )
761785 dispatcher .start ()
762-
763- process_03 ('-sto' ) # Stop ongoing operations
764- time .sleep (0.10 )
765-
766- process_03 ('-mode 0' ) # Reset mode to default
767- time .sleep (0.10 )
768786
769- process_03 ('-cal' ) # Calibration command
770- time .sleep (0.10 )
771-
772- # Retrieve device information
787+ # # Retrieve device information
773788 dev_info = get_serial_device_information ()
774- time .sleep (0.10 )
789+ time .sleep (0.3 )
775790
776791 info_dict = parse_device_info (dev_info )
777- time .sleep (0.10 )
792+
793+ time .sleep (0.3 )
778794
779795 serial = shproto .dispatcher .serial_number
780796
@@ -1234,11 +1250,11 @@ def format_date(iso_datetime_str):
12341250def start_max_pulse_check ():
12351251 try :
12361252 process_03 ('-mode 2' ) # Switch to pulse mode
1237- time .sleep (0.05 )
1253+ time .sleep (0.2 )
12381254 process_03 ('-dbg 2000 8000' ) # Filter pulses between 2000 and 8000
1239- time .sleep (0.05 )
1255+ time .sleep (0.2 )
12401256 process_03 ('-sta' ) # Start recording
1241- time .sleep (0.05 )
1257+ time .sleep (0.2 )
12421258 except Exception as e :
12431259 logger .error (f"Error in process_03 command: { e } " )
12441260 return True # Signal that the interval should remain disabled
@@ -1251,9 +1267,9 @@ def start_max_pulse_check():
12511267def stop_max_pulse_check ():
12521268 try :
12531269 process_03 ('-sto' ) # Stop recording
1254- time .sleep (0.05 )
1270+ time .sleep (0.2 )
12551271 process_03 ('-mode 0' ) # Reset mode to default
1256- time .sleep (0.05 )
1272+ time .sleep (0.2 )
12571273 except Exception as e :
12581274 logger .error (f"Error in process_03 command: { e } " )
12591275
0 commit comments