Loading src/plotiq.py 0 → 100644 +82 −0 Original line number Diff line number Diff line # -*- coding: utf-8 -*- """ Created on Thu Nov 2 15:46:56 2023 @author: ccd """ import sys, os import matplotlib.pyplot as plt import numpy as np def is_float(value): try: float(value) return True except ValueError: return False def load_and_plot_ascii(input_file, output_file): file_basename = os.path.splitext(os.path.basename(input_file))[0] with open(input_file, 'r') as file: lines = file.readlines() data_start = 0 for i, line in enumerate(lines): values = line.split() if len(values) >= 2 and all(is_float(val) for val in values[:2]): data_start = i break x = [] y = [] y_err = [] for line in lines[data_start:]: values = line.split() if len(values) >= 2 and all(is_float(val) for val in values[:2]): x.append(float(values[0])) y.append(float(values[1])) if len(values) >= 3 and is_float(values[2]): y_err.append(float(values[2])) else: y_err.append(0) fig, ax = plt.subplots() ax.scatter(x, y) if y_err: ax.errorbar(x, y, yerr=y_err, fmt='none', ecolor='r') ax.set_xlabel('Q') ax.set_ylabel('Intensity') ax.set_xscale('log') ax.set_yscale('log') ax.set_title(file_basename) plt.savefig(output_file) plt.show() if __name__ == '__main__': default_fn = 'output.png' if len(sys.argv) == 2: input_file = sys.argv[1] output_file = default_fn # default name load_and_plot_ascii(input_file, output_file) elif len(sys.argv)== 3: if sys.argv[1] == '-i': input_file = sys.argv[2] output_file = default_fn load_and_plot_ascii(input_file, output_file) else : print("Usage: python this_code.py [-i] input_ascii_file [-o output_plot]") sys.exit(1) elif len(sys.argv) == 5: if sys.argv[1] == '-i' and sys.argv[3] == '-o': input_file = sys.argv[2] output_file = sys.argv[4] load_and_plot_ascii(input_file, output_file) else: print("Usage: python this_code.py [-i] input_ascii_file [-o output_plot]") sys.exit(1) else: print("Usage: python this_code.py [-i] input_ascii_file [-o output_plot]") sys.exit(1) No newline at end of file src/test.dat 0 → 100644 +62 −0 Original line number Diff line number Diff line # I(Q) #Q (1/A) I (1/cm) dI (1/cm) dQ (1/A) 1.199964E-02 5.682900E-01 4.238577E-02 2.490751E-03 1.275902E-02 5.456678E-01 1.899944E-02 2.558608E-03 1.356646E-02 5.774685E-01 1.305905E-02 2.598957E-03 1.442499E-02 5.395965E-01 9.600574E-03 2.683330E-03 1.533785E-02 5.843780E-01 8.233809E-03 2.720112E-03 1.630849E-02 5.862100E-01 7.018327E-03 2.803551E-03 1.734054E-02 5.878889E-01 5.971451E-03 2.839215E-03 1.843791E-02 5.970609E-01 5.424993E-03 2.895889E-03 1.960473E-02 5.939188E-01 4.686415E-03 2.954623E-03 2.084539E-02 5.979111E-01 4.240381E-03 2.991850E-03 2.216455E-02 6.015106E-01 3.766292E-03 3.029806E-03 2.356720E-02 5.949994E-01 3.455076E-03 3.086722E-03 2.505862E-02 5.970182E-01 3.149820E-03 3.126108E-03 2.664442E-02 6.003383E-01 2.914285E-03 3.149157E-03 2.833057E-02 5.979606E-01 2.672286E-03 3.185241E-03 3.012343E-02 5.959317E-01 2.507677E-03 3.196237E-03 3.202974E-02 5.980290E-01 2.347064E-03 3.212470E-03 3.405670E-02 5.955801E-01 2.201801E-03 3.218791E-03 3.621192E-02 5.895442E-01 2.054838E-03 3.225747E-03 3.850354E-02 5.877516E-01 1.931582E-03 3.255893E-03 4.094018E-02 5.862807E-01 1.815816E-03 3.260426E-03 4.353102E-02 5.793891E-01 1.718056E-03 3.281937E-03 4.628581E-02 5.720001E-01 1.586039E-03 3.277233E-03 4.921494E-02 5.603955E-01 1.495566E-03 3.299693E-03 5.232943E-02 5.508956E-01 1.401422E-03 3.302103E-03 5.564102E-02 5.383710E-01 1.303149E-03 3.314842E-03 5.916218E-02 5.230848E-01 1.207636E-03 3.330909E-03 6.290617E-02 5.042124E-01 1.122830E-03 3.348464E-03 6.688709E-02 4.862755E-01 1.035920E-03 3.364112E-03 7.111994E-02 4.641050E-01 9.578751E-04 3.387786E-03 7.562066E-02 4.379560E-01 8.777189E-04 3.406256E-03 8.040620E-02 4.123322E-01 8.032452E-04 3.433074E-03 8.549459E-02 3.831396E-01 7.322191E-04 3.458467E-03 9.090498E-02 3.540508E-01 6.684104E-04 3.494917E-03 9.665777E-02 3.252627E-01 6.032279E-04 3.535270E-03 1.027746E-01 2.930124E-01 5.457026E-04 3.577408E-03 1.092785E-01 2.632801E-01 4.913873E-04 3.621680E-03 1.161941E-01 2.330943E-01 4.396440E-04 3.673904E-03 1.235472E-01 2.054670E-01 3.937492E-04 3.730166E-03 1.313657E-01 1.805773E-01 3.530403E-04 3.796249E-03 1.396790E-01 1.588443E-01 3.180208E-04 3.879574E-03 1.485184E-01 1.403651E-01 2.907479E-04 3.983674E-03 1.579172E-01 1.260612E-01 2.699526E-04 4.123704E-03 1.679107E-01 1.150382E-01 2.555421E-04 4.292719E-03 1.785367E-01 1.073359E-01 2.473238E-04 4.504896E-03 1.898351E-01 1.024142E-01 2.434906E-04 4.765710E-03 2.018486E-01 1.002718E-01 2.459127E-04 5.078415E-03 2.146222E-01 9.976511E-02 2.507530E-04 5.454049E-03 2.282043E-01 9.885901E-02 2.579234E-04 5.869131E-03 2.426458E-01 9.779594E-02 2.667214E-04 6.317731E-03 2.580013E-01 9.616329E-02 2.783043E-04 6.790399E-03 2.743286E-01 9.461655E-02 2.945714E-04 7.294713E-03 2.916890E-01 9.271437E-02 3.197080E-04 7.821574E-03 3.101481E-01 9.050872E-02 3.581882E-04 8.363574E-03 3.297754E-01 8.983666E-02 4.192013E-04 8.934901E-03 3.506447E-01 8.844824E-02 5.131968E-04 9.537144E-03 3.728347E-01 8.600537E-02 6.745900E-04 1.017496E-02 3.964290E-01 8.431894E-02 9.745432E-04 1.084961E-02 4.215164E-01 8.551484E-02 1.767057E-03 1.151633E-02 4.481915E-01 7.863065E-02 5.478638E-03 1.203376E-02 Loading
src/plotiq.py 0 → 100644 +82 −0 Original line number Diff line number Diff line # -*- coding: utf-8 -*- """ Created on Thu Nov 2 15:46:56 2023 @author: ccd """ import sys, os import matplotlib.pyplot as plt import numpy as np def is_float(value): try: float(value) return True except ValueError: return False def load_and_plot_ascii(input_file, output_file): file_basename = os.path.splitext(os.path.basename(input_file))[0] with open(input_file, 'r') as file: lines = file.readlines() data_start = 0 for i, line in enumerate(lines): values = line.split() if len(values) >= 2 and all(is_float(val) for val in values[:2]): data_start = i break x = [] y = [] y_err = [] for line in lines[data_start:]: values = line.split() if len(values) >= 2 and all(is_float(val) for val in values[:2]): x.append(float(values[0])) y.append(float(values[1])) if len(values) >= 3 and is_float(values[2]): y_err.append(float(values[2])) else: y_err.append(0) fig, ax = plt.subplots() ax.scatter(x, y) if y_err: ax.errorbar(x, y, yerr=y_err, fmt='none', ecolor='r') ax.set_xlabel('Q') ax.set_ylabel('Intensity') ax.set_xscale('log') ax.set_yscale('log') ax.set_title(file_basename) plt.savefig(output_file) plt.show() if __name__ == '__main__': default_fn = 'output.png' if len(sys.argv) == 2: input_file = sys.argv[1] output_file = default_fn # default name load_and_plot_ascii(input_file, output_file) elif len(sys.argv)== 3: if sys.argv[1] == '-i': input_file = sys.argv[2] output_file = default_fn load_and_plot_ascii(input_file, output_file) else : print("Usage: python this_code.py [-i] input_ascii_file [-o output_plot]") sys.exit(1) elif len(sys.argv) == 5: if sys.argv[1] == '-i' and sys.argv[3] == '-o': input_file = sys.argv[2] output_file = sys.argv[4] load_and_plot_ascii(input_file, output_file) else: print("Usage: python this_code.py [-i] input_ascii_file [-o output_plot]") sys.exit(1) else: print("Usage: python this_code.py [-i] input_ascii_file [-o output_plot]") sys.exit(1) No newline at end of file
src/test.dat 0 → 100644 +62 −0 Original line number Diff line number Diff line # I(Q) #Q (1/A) I (1/cm) dI (1/cm) dQ (1/A) 1.199964E-02 5.682900E-01 4.238577E-02 2.490751E-03 1.275902E-02 5.456678E-01 1.899944E-02 2.558608E-03 1.356646E-02 5.774685E-01 1.305905E-02 2.598957E-03 1.442499E-02 5.395965E-01 9.600574E-03 2.683330E-03 1.533785E-02 5.843780E-01 8.233809E-03 2.720112E-03 1.630849E-02 5.862100E-01 7.018327E-03 2.803551E-03 1.734054E-02 5.878889E-01 5.971451E-03 2.839215E-03 1.843791E-02 5.970609E-01 5.424993E-03 2.895889E-03 1.960473E-02 5.939188E-01 4.686415E-03 2.954623E-03 2.084539E-02 5.979111E-01 4.240381E-03 2.991850E-03 2.216455E-02 6.015106E-01 3.766292E-03 3.029806E-03 2.356720E-02 5.949994E-01 3.455076E-03 3.086722E-03 2.505862E-02 5.970182E-01 3.149820E-03 3.126108E-03 2.664442E-02 6.003383E-01 2.914285E-03 3.149157E-03 2.833057E-02 5.979606E-01 2.672286E-03 3.185241E-03 3.012343E-02 5.959317E-01 2.507677E-03 3.196237E-03 3.202974E-02 5.980290E-01 2.347064E-03 3.212470E-03 3.405670E-02 5.955801E-01 2.201801E-03 3.218791E-03 3.621192E-02 5.895442E-01 2.054838E-03 3.225747E-03 3.850354E-02 5.877516E-01 1.931582E-03 3.255893E-03 4.094018E-02 5.862807E-01 1.815816E-03 3.260426E-03 4.353102E-02 5.793891E-01 1.718056E-03 3.281937E-03 4.628581E-02 5.720001E-01 1.586039E-03 3.277233E-03 4.921494E-02 5.603955E-01 1.495566E-03 3.299693E-03 5.232943E-02 5.508956E-01 1.401422E-03 3.302103E-03 5.564102E-02 5.383710E-01 1.303149E-03 3.314842E-03 5.916218E-02 5.230848E-01 1.207636E-03 3.330909E-03 6.290617E-02 5.042124E-01 1.122830E-03 3.348464E-03 6.688709E-02 4.862755E-01 1.035920E-03 3.364112E-03 7.111994E-02 4.641050E-01 9.578751E-04 3.387786E-03 7.562066E-02 4.379560E-01 8.777189E-04 3.406256E-03 8.040620E-02 4.123322E-01 8.032452E-04 3.433074E-03 8.549459E-02 3.831396E-01 7.322191E-04 3.458467E-03 9.090498E-02 3.540508E-01 6.684104E-04 3.494917E-03 9.665777E-02 3.252627E-01 6.032279E-04 3.535270E-03 1.027746E-01 2.930124E-01 5.457026E-04 3.577408E-03 1.092785E-01 2.632801E-01 4.913873E-04 3.621680E-03 1.161941E-01 2.330943E-01 4.396440E-04 3.673904E-03 1.235472E-01 2.054670E-01 3.937492E-04 3.730166E-03 1.313657E-01 1.805773E-01 3.530403E-04 3.796249E-03 1.396790E-01 1.588443E-01 3.180208E-04 3.879574E-03 1.485184E-01 1.403651E-01 2.907479E-04 3.983674E-03 1.579172E-01 1.260612E-01 2.699526E-04 4.123704E-03 1.679107E-01 1.150382E-01 2.555421E-04 4.292719E-03 1.785367E-01 1.073359E-01 2.473238E-04 4.504896E-03 1.898351E-01 1.024142E-01 2.434906E-04 4.765710E-03 2.018486E-01 1.002718E-01 2.459127E-04 5.078415E-03 2.146222E-01 9.976511E-02 2.507530E-04 5.454049E-03 2.282043E-01 9.885901E-02 2.579234E-04 5.869131E-03 2.426458E-01 9.779594E-02 2.667214E-04 6.317731E-03 2.580013E-01 9.616329E-02 2.783043E-04 6.790399E-03 2.743286E-01 9.461655E-02 2.945714E-04 7.294713E-03 2.916890E-01 9.271437E-02 3.197080E-04 7.821574E-03 3.101481E-01 9.050872E-02 3.581882E-04 8.363574E-03 3.297754E-01 8.983666E-02 4.192013E-04 8.934901E-03 3.506447E-01 8.844824E-02 5.131968E-04 9.537144E-03 3.728347E-01 8.600537E-02 6.745900E-04 1.017496E-02 3.964290E-01 8.431894E-02 9.745432E-04 1.084961E-02 4.215164E-01 8.551484E-02 1.767057E-03 1.151633E-02 4.481915E-01 7.863065E-02 5.478638E-03 1.203376E-02