Browse Source

Update combiner

compton 3 years ago
parent
commit
0db9bcc68f
2 changed files with 20 additions and 4 deletions
  1. 1 0
      .gitignore
  2. 19 4
      src/compton_combiner.py

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+__pycache__

+ 19 - 4
src/compton_combiner.py

@@ -4,6 +4,7 @@
 import argparse
 from configparser import ConfigParser
 from datetime import datetime, timedelta, timezone
+import os
 import sqlite3
 from typing import Union, Tuple, Optional
 
@@ -417,13 +418,25 @@ def final_table_to_clbrdb(df: pd.DataFrame, clbrdb: CalibrdbHandler, runs_df: pd
                            'mean_energy_stat_err', 'mean_energy_sys_err', 'mean_spread', 'mean_spread_stat_err']].values.tolist()
     clbrdb.insert(df_clbrdb, 'Misc', 'RunHeader', 'Compton_run_avg', 'Default', comment = season)
     clbrdb.commit()
+    
+def save_csv(df: pd.DataFrame, filepath: str):
+    """Saves csv file. Updates current file in filepath if exists"""
+    
+    if os.path.isfile(filepath):
+        df_current = pd.read_csv(filepath)
+        df_current = df_current.append(df, ignore_index=True)
+        df_current = df_current.drop_duplicates(subset=['energy_point', 'first_run'], keep='last')
+        df = df_current
+    
+    df.to_csv(filepath, index=False, float_format='%g')
+    return
 
-# python scripts/compton_combiner.py -s NNBAR2021 -c database.ini --csv --clbrdb
+# python scripts/compton_combiner.py -s NNBAR2021 -c database.ini --csv_dir . --clbrdb
 def main():
     parser = argparse.ArgumentParser(description = 'Mean compton energy measurements from clbrdb')
     parser.add_argument('-s', '--season', help = 'Name of the season')
     parser.add_argument('-c', '--config', help = 'Config file containing information for access to databases')
-    parser.add_argument('--csv', action = 'store_true', help = 'Save csv file with data or not')
+    parser.add_argument('--csv_dir', help = 'Save csv file with data in the folder or not if skip it')
     parser.add_argument('--clbrdb', action = 'store_true', help = 'Update Compton_run_avg clbrdb or not')
     parser.add_argument('--pics_folder', help = 'Path to the directory for saving the pictures')
     parser.add_argument('--only_last', action = 'store_true', help = 'Compute values of the last (in Compton_run_avg clbrdb) and new points only')
@@ -461,8 +474,10 @@ def main():
     
     cdf = process_combined(comb_df, runs_df, compton_df, args.pics_folder, rdb)
     
-    if args.csv:
-        cdf.to_csv(f'{args.season}.csv', index=False, float_format='%g')
+    if args.csv_dir is not None:
+        csv_path = os.path.join(args.csv_dir, f'{args.season}.csv')
+        save_csv(cdf, csv_path)
+        # cdf.to_csv(f'{args.season}.csv', index=False, float_format='%g')
     
     if args.clbrdb:
         final_table_to_clbrdb(cdf, clbrdb, runs_df, args.season)