def ELS_maxflux_anode(elsdata, starttime, endtime): startslice, endslice = CAPS_slicenumber(elsdata, starttime), CAPS_slicenumber(elsdata, endtime) dataslice = ELS_backgroundremoval(elsdata, startslice, endslice) anodesums = np.sum(np.sum(dataslice, axis=2), axis=0) maxflux_anode = np.argmax(anodesums) #print(anodesums, maxflux_anode) return maxflux_anode
def CAPS_ramtimes(elsdata, starttime, endtime): ramtimes = [] startslice, endslice = CAPS_slicenumber(elsdata, starttime), CAPS_slicenumber(elsdata, endtime) for i in np.arange(startslice, endslice, 1): actuatorangle = elsdata['actuator'][i] ramangle = caps_ramdirection_azielv(elsdata['times_utc'][i])[0] if abs(actuatorangle - ramangle) < 1.5: ramtimes.append(caps_ramdirection_time(elsdata, elsdata['times_utc'][i])) for counter, i in enumerate(ramtimes): ramtimes[counter] = i.replace(microsecond=0) ramtimes = sorted(set(ramtimes)) return ramtimes
def heavy_ion_finder_ibs(ibsdata, startenergy, starttime, endtime): startslice, endslice = CAPS_slicenumber(ibsdata, starttime), CAPS_slicenumber( ibsdata, endtime) energybin = CAPS_energyslice("ibs", startenergy, startenergy)[0] tempslice = (ibsdata['ibsdata'][:, 1, startslice:endslice] / (ibscalib['ibsgeom'] * 1e-4)) maxindices = np.unravel_index(tempslice.argmax(), tempslice.shape) prominence = tempslice[maxindices] / 10 #print("prominence", prominence) # dataslice = ibsdata['ibsdata'][energybin, 1, startslice:endslice] singlepeakslice = (ibsdata['ibsdata'][energybin, 1, startslice:endslice] / (ibscalib['ibsgeom'] * 1e-4)) # peaks, properties = find_peaks(dataslice, height=5e2, prominence=prominence,distance=15) # print(peaks) # if peaks.size == 0: # raise ValueError("No Peaks") # while len(peaks) == 1: while max(singlepeakslice) > prominence: recentpeakslice = singlepeakslice recentenergybin = energybin recentpeaks = np.argmax(recentpeakslice) energybin += 1 singlepeakslice = ( ibsdata['ibsdata'][energybin, 1, startslice:endslice] / (ibscalib['ibsgeom'] * 1e-4)) # peaks, properties = find_peaks(singlepeakslice, height=5e2, prominence=prominence) zerocounter = 0 i = 0 while i == 0: i = int(ibsdata['ibsdata'][zerocounter, 1, startslice + recentpeaks]) zerocounter += 1 numberofsteps = (255 - (recentenergybin - zerocounter)) # print("Counter", zerocounter) # print("RecentEnergyBin", recentenergybin) # print("Number of steps", numberofsteps) times = ibsdata['times_utc'][startslice:endslice] times_adjusted = [ d + numberofsteps * datetime.timedelta(seconds=7.813e-3) for d in times ] timestamps_adjusted = [ datetime.datetime.timestamp(d) for d in times_adjusted ] weightedtime = datetime.datetime.fromtimestamp( np.average(timestamps_adjusted, weights=recentpeakslice)) # return times_adjusted[np.argmax(recentpeakslice)], ibscalib['ibsearray'][energybin] return weightedtime, ibscalib['ibsearray'][energybin]
def heavy_ion_finder(elsdata, startenergy, anode, starttime, endtime): startslice, endslice = CAPS_slicenumber(elsdata, starttime), CAPS_slicenumber(elsdata, endtime) tempslice = ELS_backgroundremoval(elsdata, startslice, endslice)[:, anode, :] maxindices = np.unravel_index(tempslice.argmax(), tempslice.shape) #print("Max DEF", tempslice[maxindices]) prominence = tempslice[maxindices] / 40 #print("prominence", prominence) energybin = CAPS_energyslice("els", startenergy, startenergy)[0] dataslice = ELS_backgroundremoval(elsdata, startslice, endslice)[energybin, anode, :] peaks, properties = find_peaks(dataslice, height=3e9, prominence=prominence, width=0.5, rel_height=0.5, distance=15) results = peak_widths(dataslice, peaks, rel_height=0.97) recentpeakslice = [] left = int(results[2][0]) right = int(results[3][0]) + 1 tempenergybin = energybin singlepeakslice = ELS_backgroundremoval(elsdata, startslice, endslice)[tempenergybin, anode, left:right] temppeaks, tempproperties = find_peaks(singlepeakslice, height=1e9, prominence=prominence, width=0.5, rel_height=0.5, distance=5) if temppeaks.size == 0: raise ValueError("No Peaks") while len(temppeaks) == 1: recentpeakslice = singlepeakslice recentenergybin = tempenergybin tempenergybin += 1 singlepeakslice = ELS_backgroundremoval(elsdata, startslice, endslice)[tempenergybin, anode, left:right] temppeaks, tempproperties = find_peaks(singlepeakslice, height=5e9, prominence=prominence, width=0.5, rel_height=0.5, distance=5) times = elsdata['times_utc'][startslice:endslice][left:right] times_adjusted = [d + (63 - recentenergybin) * datetime.timedelta(seconds=31.25e-3) for d in times] timestamps_adjusted = [datetime.datetime.timestamp(d) for d in times_adjusted] testtime = np.average(timestamps_adjusted, weights=recentpeakslice) # plt.plot(times_adjusted, recentpeakslice) # plt.vlines(times_adjusted[np.argmax(recentpeakslice)],min(recentpeakslice),max(recentpeakslice)) # plt.vlines(datetime.datetime.fromtimestamp(testtime), min(recentpeakslice), max(recentpeakslice),color='m') # print("currenttime", times_adjusted[np.argmax(recentpeakslice)]) # print("testtime", datetime.datetime.fromtimestamp(testtime)) # return times_adjusted[np.argmax(recentpeakslice)], elscalib['earray'][tempenergybin] return datetime.datetime.fromtimestamp(testtime), elscalib['earray'][tempenergybin]
ax.set_ylabel("CAPS ELS \nAnode {0} \n eV/q ".format(anode)) flyby = "t32" anodes = [4, 5] lowerenergy = 2 upperenergy = 500 elsdata = readsav("data/els/elsres_" + filedates_times[flyby][0] + ".dat") generate_mass_bins(elsdata, flyby, "els") starttime = flyby_datetimes[flyby][0] endtime = flyby_datetimes[flyby][1] alts = [] startslice, endslice = CAPS_slicenumber(elsdata, starttime), CAPS_slicenumber( elsdata, endtime) for i in np.arange(startslice, endslice, 1): alt, lat, lon = cassini_titan_altlatlon(elsdata['times_utc'][i]) alts.append(alt) fig, axes = plt.subplots(len(anodes) + 1, figsize=(18, 6), sharex='all') for anode, ax in zip(anodes, axes[:-1]): ELS_spectrogram(elsdata, anode, filedates_times[flyby][1], 600, ax=ax, fig=fig) axes[-1].plot(elsdata['times_utc'][startslice:endslice], alts) axes[-1].set_ylabel("Altitude [/km]")
def heavy_ion_finder_old(elsdata, startenergy, anode, starttime, endtime, prominence=1e10): startslice, endslice = CAPS_slicenumber(elsdata, starttime), CAPS_slicenumber( elsdata, endtime) energybin = CAPS_energyslice("els", startenergy, startenergy)[0] dataslice = ELS_backgroundremoval(elsdata, startslice, endslice)[energybin, anode, :] peaks, properties = find_peaks(dataslice, height=3e9, prominence=prominence, width=0.5, rel_height=0.4, distance=15) results = peak_widths(dataslice, peaks, rel_height=0.97) recentpeakslice = [] left = int(results[2][0]) right = int(results[3][0]) + 1 tempenergybin = energybin singlepeakslice = ELS_backgroundremoval(elsdata, startslice, endslice)[tempenergybin, anode, left:right] temppeaks, tempproperties = find_peaks(singlepeakslice, height=1e9, prominence=prominence, width=0.5, rel_height=0.5, distance=5) # if temppeaks == []: # raise ValueError("No Peaks") while len(temppeaks) == 1: recentpeaks = temppeaks recentproperties = tempproperties recentpeakslice = singlepeakslice recentenergybin = tempenergybin tempenergybin += 1 singlepeakslice = ELS_backgroundremoval(elsdata, startslice, endslice)[tempenergybin, anode, left:right] temppeaks, tempproperties = find_peaks(singlepeakslice, height=5e9, prominence=prominence, width=0.5, rel_height=0.5, distance=5) print( "Energy Bin time", elsdata['times_utc'][startslice + recentpeaks[0]] + (63 - recentenergybin) * datetime.timedelta(seconds=31.25e-3)) # print(elsdata['times_utc'][startslice + left], elsdata['times_utc'][startslice + right]) # print(anode, startslice, left, right) # print(recentpeakslice) times = elsdata['times_utc'][startslice:endslice][left:right] timestamps = [ datetime.datetime.timestamp(d) for d in elsdata['times_utc'][startslice:endslice][left:right] ] times_adjusted = [ d + (63 - recentenergybin) * datetime.timedelta(seconds=31.25e-3) for d in times ] timestamps_adjusted = [ datetime.datetime.timestamp(d + (63 - recentenergybin) * datetime.timedelta(seconds=31.25e-3)) for d in times ] peak_timestamps_lower = timestamps_adjusted[int( np.floor(recentproperties['left_ips'][0]))] peak_timestamps_upper = timestamps_adjusted[int( np.ceil(recentproperties['right_ips'][0]))] stddev = (peak_timestamps_upper - peak_timestamps_lower) / 2 time_init = models.Gaussian1D(amplitude=max(recentpeakslice), mean=timestamps_adjusted[recentpeaks[0]], stddev=stddev, bounds={ "amplitude": (max(recentpeakslice), max(recentpeakslice) * 1.2), "mean": (peak_timestamps_lower, peak_timestamps_upper), "stddev": (stddev / 1.5, stddev * 4) }) timefit = fitting.LevMarLSQFitter()(time_init, timestamps_adjusted, recentpeakslice) timestamp_linspace = np.linspace(timestamps_adjusted[0], timestamps_adjusted[-1], 100) actuatorangle = elsdata['actuator'][startslice:endslice][left:right] actuatorangle_adjusted = np.interp(timestamps_adjusted, timestamps, actuatorangle) print(timestamps, actuatorangle) print(timestamps_adjusted, actuatorangle_adjusted) act_init = models.Gaussian1D( amplitude=max(recentpeakslice), mean=actuatorangle_adjusted[recentpeaks[0]], stddev=1, bounds={ "amplitude": (max(recentpeakslice), max(recentpeakslice) * 1.2), "mean": (actuatorangle_adjusted[recentpeaks[0]] - 1, actuatorangle_adjusted[recentpeaks[0]] + 1), "stddev": (1, 3) }) actfit = fitting.LevMarLSQFitter()(act_init, actuatorangle, recentpeakslice) ramtime = caps_ramdirection_time( elsdata, datetime.datetime.fromtimestamp(timefit.mean.value)) ramangle = caps_ramdirection_azielv( caps_ramdirection_time( elsdata, datetime.datetime.fromtimestamp(timefit.mean.value)))[0] print("Ram Time", ramtime, ramangle) print( "Time Mean", datetime.datetime.fromtimestamp(timefit.mean.value), CAPS_actuation(elsdata, datetime.datetime.fromtimestamp(timefit.mean.value)), CAPS_actuation(elsdata, datetime.datetime.fromtimestamp(timefit.mean.value)) - ramangle) # fig, ax = plt.subplots() # ax.plot(elsdata['actuator'][startslice:endslice],timestamps) if elsdata['actuator'][startslice:endslice][0] > elsdata['actuator'][ startslice:endslice][-1]: testtimes = np.interp( actfit.mean.value, np.flip(elsdata['actuator'][startslice:endslice][left:right]), np.flip(timestamps)) else: testtimes = np.interp( actfit.mean.value, elsdata['actuator'][startslice:endslice][left:right], timestamps) # ax.vlines(actfit.mean.value,min(timestamps),max(timestamps),color='k') # ax.hlines(testtimes, min(elsdata['actuator'][startslice:endslice]), max(elsdata['actuator'][startslice:endslice]), color='k') print("Act Mean", datetime.datetime.fromtimestamp(testtimes), actfit.mean.value, actfit.mean.value - ramangle) print( "No Fit Mean", times_adjusted[np.argmax(recentpeakslice)], CAPS_actuation(elsdata, times_adjusted[np.argmax(recentpeakslice)]), CAPS_actuation(elsdata, times_adjusted[np.argmax(recentpeakslice)]) - ramangle) if actuatorangle_adjusted[0] > actuatorangle_adjusted[-1]: print("Negative Actuation") act_linspace = np.linspace(actuatorangle_adjusted[-1], actuatorangle_adjusted[0], 100) else: act_linspace = np.linspace(actuatorangle_adjusted[0], actuatorangle_adjusted[-1], 100) fig, axes = plt.subplots(2) axes[0].plot(times_adjusted, recentpeakslice, label="data") axes[0].plot( [datetime.datetime.fromtimestamp(d) for d in timestamp_linspace], timefit(timestamp_linspace), linestyle='--', label="Gaussian fit") axes[0].vlines(caps_ramdirection_time( elsdata, datetime.datetime.fromtimestamp(timefit.mean.value)), min(recentpeakslice), max(recentpeakslice), color='k') axes[1].plot(actuatorangle_adjusted, recentpeakslice, label="data") axes[1].plot(act_linspace, actfit(act_linspace), label="interp") axes[1].vlines(caps_ramdirection_azielv( caps_ramdirection_time( elsdata, datetime.datetime.fromtimestamp(timefit.mean.value)))[0], min(recentpeakslice), max(recentpeakslice), color='k') axes[0].set_xlabel("Time") axes[0].set_ylabel("DEF") axes[1].set_xlabel("Actuator Angle") axes[1].set_ylabel("DEF") axes[0].legend() axes[1].legend() return datetime.datetime.fromtimestamp( timefit.mean.value), elscalib['earray'][tempenergybin]
anode, starttime, endtime, peaks=True, prominence=1e10, ax=ax) IBS_intensityplot(ibsdata, 55, starttime, endtime, peaks=True, prominence=300, ax=ax2) fig.legend() startslice_els, endslice_els = CAPS_slicenumber(elsdata, starttime), CAPS_slicenumber( elsdata, endtime) dataslice_els = ELS_backgroundremoval(elsdata, startslice_els, endslice_els)[:, anode - 1, :] startslice_ibs, endslice_ibs = CAPS_slicenumber(ibsdata, starttime), CAPS_slicenumber( ibsdata, endtime) ax.plot(elsdata['times_utc'][startslice_els:endslice_els], np.sum(dataslice_els, axis=0), linestyle='--', label="ELS Flux Sum", color='C0') ax2.plot(ibsdata['times_utc'][startslice_ibs:endslice_ibs], np.sum(ibsdata['ibsdata'][:, 1, startslice_ibs:endslice_ibs], axis=0), linestyle='--',