Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e29161d

Browse files
authored
Merge pull request #118 from jgieseler/main
Refactor make_spec_gif and plot_spec_slices to accept filenames
2 parents 9705e35 + 4f3cc3f commit e29161d

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

spectra/__init__.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,11 @@ def plot_flux(self, spec_start, spec_end, subtract_background=True, background_s
298298
plt.show()
299299
return fig, axs
300300

301-
def make_spec_gif(self, base_filename):
302-
# Get all PNG files (assuming they're named plot_0.png, plot_1.png, etc.)
303-
png_files = sorted(glob.glob(f'{base_filename}*.png'))
301+
def make_spec_gif(self, filenames):
304302
# Define the output GIF filename
305-
self.gif_filename = f'{base_filename}_animation.gif'
303+
self.gif_filename = f'{os.path.commonprefix(filenames)}_animation.gif'
306304
# write to animated gif; duration (in ms) defines how fast the animation is.
307-
frames = [Image.open(f) for f in png_files]
305+
frames = [Image.open(f + '.png') for f in filenames]
308306
frames[0].save(
309307
self.gif_filename,
310308
save_all=True,
@@ -313,17 +311,16 @@ def make_spec_gif(self, base_filename):
313311
loop=0
314312
)
315313

316-
def plot_spec_slices(self, base_filename, spec_start, duration):
314+
def plot_spec_slices(self, filenames, spec_start, duration):
317315
# makes a plot of each spectrum slice based on the already saved csv files
318316
# taking all csv files, we determine a global y-range used in all plots
319317

320-
csv_files = sorted(glob.glob(f'{base_filename}*.csv'))
321318
global_min = None
322319
global_max = None
323320
data_frames = []
324321

325-
for file in csv_files:
326-
df = pd.read_csv(file)
322+
for file in filenames:
323+
df = pd.read_csv(file + '.csv')
327324
intensity = df['Intensity'].astype(float)
328325
if self.spacecraft == 'Wind':
329326
# i_err = np.zeros(len(intensity))
@@ -357,7 +354,7 @@ def plot_spec_slices(self, base_filename, spec_start, duration):
357354
# print(global_min, global_max)
358355
print(f'Global y-range: {global_min:.2f} to {global_max:.2f}')
359356
# Plot each file with shared y-limits
360-
for idx, (df, file) in enumerate(zip(data_frames, csv_files)):
357+
for idx, (df, file) in enumerate(zip(data_frames, filenames)):
361358
t1 = spec_start + idx * duration
362359
t2 = spec_start + (idx+1) * duration
363360

@@ -387,8 +384,7 @@ def plot_spec_slices(self, base_filename, spec_start, duration):
387384
ax.text(0.95, 0.95, f'{t1}-{t2}', ha='right', transform=ax.transAxes)
388385
ax.legend(loc=3)
389386

390-
filename = f'{base_filename}_{idx}.png'
391-
plt.savefig(filename)
387+
plt.savefig(file + '.png')
392388
plt.close('all')
393389

394390
def get_spec_slices(self, spec_start, spec_end, duration, subtract_background=True, background_start=None, background_end=None):
@@ -403,12 +399,14 @@ def get_spec_slices(self, spec_start, spec_end, duration, subtract_background=Tr
403399
print(base_filename)
404400

405401
num_steps = int((spec_end-spec_start) / duration)
402+
filenames = []
406403
for i in np.arange(0, num_steps, 1):
407404
t1 = spec_start + i * duration
408405
t2 = spec_start + (i+1) * duration
409406
self.get_spec(t1, t2, spec_type='integral', subtract_background=subtract_background,
410407
background_start=background_start, background_end=background_end)
411408
filename = f'{base_filename}{i}'
409+
filenames.append(filename)
412410

413411
# self.E_unc = self.DE/2.
414412
# self.I_unc = self.final_unc
@@ -417,9 +415,9 @@ def get_spec_slices(self, spec_start, spec_end, duration, subtract_background=Tr
417415
self.spec_df.to_csv(filename+'.csv', index=False)
418416

419417
# make plots for each spec slice using common y-range:
420-
self.plot_spec_slices(base_filename, spec_start, duration)
418+
self.plot_spec_slices(filenames, spec_start, duration)
421419

422-
self.make_spec_gif(base_filename)
420+
self.make_spec_gif(filenames)
423421

424422
def get_spec(self,
425423
spec_start: dt.datetime | pd.Timestamp | str,

0 commit comments

Comments
 (0)