コード例 #1
0
def recalibrate_pulse(timeID, input_fname, output_fname, set_polarization_delay=True, upsample_factor=4, polarization_flips="polarization_flips.txt"):

    processed_data_folder = processed_data_dir(timeID)
    raw_fpaths = filePaths_by_stationName(timeID)
    polarization_flips = read_antenna_pol_flips( processed_data_folder + '/' + polarization_flips )
    
    input_file = h5py.File(processed_data_folder+'/'+input_fname, "r")
    
    try:
        output_file = h5py.File(processed_data_folder+'/'+output_fname, "r+")
    except:
        output_file = h5py.File(processed_data_folder+'/'+output_fname, "w")
        
    sample_time = 5.0e-9
    if upsample_factor>1:
        sample_time /= upsample_factor
    
    for sname, h5_statGroup in input_file.items():
        out_statGroup = output_file.require_group(sname)
        print()
        print()
        print(sname)
        
        datafile = MultiFile_Dal1(raw_fpaths[sname], polarization_flips=polarization_flips)
        
        if set_polarization_delay:
            datafile.find_and_set_polarization_delay()
            
        antenna_calibrations = { antname:calibration for antname,calibration in zip(datafile.get_antenna_names(), datafile.get_total_delays()) }
        
        for antname, in_antData in h5_statGroup.items():
            out_statGroup.copy(in_antData,  out_statGroup, name= antname)
            out_antData = out_statGroup[antname]
            
            old_even =  out_antData.attrs['PolE_timeOffset_CS']
            old_odd = out_antData.attrs['PolO_timeOffset_CS']
            
            new_even_delay = antenna_calibrations[antname]
            new_odd_delay = antenna_calibrations[ even_antName_to_odd(antname) ]
            
            out_antData.attrs['PolE_timeOffset'] = -new_even_delay ## NOTE: due to historical reasons, there is a sign flip here
            out_antData.attrs['PolO_timeOffset'] = -new_odd_delay ## NOTE: due to historical reasons, there is a sign flip here
            out_antData.attrs['PolE_timeOffset_CS'] = new_even_delay
            out_antData.attrs['PolO_timeOffset_CS'] = new_odd_delay
            
            print(antname, old_even-new_even_delay, old_odd-new_odd_delay)
            
            starting_index = out_antData.attrs['starting_index']
            
            if np.isfinite( out_antData.attrs['PolE_peakTime'] ):
                
                polE_HE = out_antData[1]
                
                if upsample_factor>1:
                    polE_HE = resample(polE_HE, len(polE_HE)*upsample_factor )
            
                PolE_peak_finder = parabolic_fit( polE_HE  )
                out_antData.attrs['PolE_peakTime'] =  starting_index*5.0E-9 - new_even_delay + PolE_peak_finder.peak_index*sample_time
            
            if np.isfinite( out_antData.attrs['PolO_peakTime'] ):
                
                polO_HE = out_antData[3]
                
                if upsample_factor>1:
                    polO_HE = resample(polO_HE, len(polO_HE)*upsample_factor )
            
                PolO_peak_finder = parabolic_fit( polO_HE  )
                out_antData.attrs['PolO_peakTime'] =  starting_index*5.0E-9 - new_odd_delay + PolO_peak_finder.peak_index*sample_time
コード例 #2
0
    def select_pulse(self, station, minT, maxT):
        TBB_data = self.TBB_data_dict[station]
        antenna_names = TBB_data.get_antenna_names()
        ant_delays = self.antenna_delays[station]

        out_fname = self.out_folder + "/potSource_" + str(
            self.current_event_index) + ".h5"
        try:
            out_file = h5py.File(out_fname, "r+")
        except:
            out_file = h5py.File(out_fname, "w")
        h5_statGroup = out_file.require_group(station)

        mid_T = (minT + maxT) * 0.5
        half_width_T = (maxT - minT) * 0.5
        half_block = int(self.block_size / 2)
        half_width_T_points = int(half_width_T / 5.0E-9)

        if self.RFI_filter_dict is not None:
            RFI_filter = self.RFI_filter_dict[station]
        else:
            RFI_filter = None

        even_pulse_time_list = []
        odd_pulse_time_list = []

        for even_ant_i in range(0, len(antenna_names), 2):
            even_delay = ant_delays[even_ant_i]
            odd_delay = ant_delays[even_ant_i + 1]

            ave_delay = (even_delay + odd_delay) * 0.5
            time_points = int((mid_T + ave_delay) / 5.0E-9)

            ## even bit
            self.temp_data_block[0, :] = TBB_data.get_data(
                time_points - half_block,
                self.block_size,
                antenna_index=even_ant_i)

            if self.remove_saturation_curry is not None:
                self.remove_saturation_curry(self.temp_data_block[0, :])

            if RFI_filter is not None:
                filtered_data = RFI_filter.filter(self.temp_data_block[0, :])
            else:
                filtered_data = hilbert(self.temp_data_block[0, :])

            even_real_all = np.real(filtered_data)
            np.abs(filtered_data, out=self.temp_data_block[0, :])

            ## odd bit
            self.temp_data_block[1, :] = TBB_data.get_data(
                time_points - half_block,
                self.block_size,
                antenna_index=even_ant_i + 1)

            if self.remove_saturation_curry is not None:
                self.remove_saturation_curry(self.temp_data_block[1, :])

            if RFI_filter is not None:
                filtered_data = RFI_filter.filter(self.temp_data_block[1, :])
            else:
                filtered_data = hilbert(self.temp_data_block[1, :])

            odd_real_all = np.real(filtered_data)
            np.abs(filtered_data, out=self.temp_data_block[1, :])

            even_peak_index = np.argmax(self.temp_data_block[
                0, half_block - half_width_T_points:half_block +
                half_width_T_points]) + (half_block - half_width_T_points)
            odd_peak_index = np.argmax(self.temp_data_block[
                1, half_block - half_width_T_points:half_block +
                half_width_T_points]) + (half_block - half_width_T_points)

            peak_index = odd_peak_index
            if self.temp_data_block[0, even_peak_index] > self.temp_data_block[
                    1, odd_peak_index]:
                peak_index = even_peak_index

            initial_index = peak_index - self.half_pulse_length
            even_HE = self.temp_data_block[0, initial_index:initial_index +
                                           self.pulse_length]
            even_real = even_real_all[initial_index:initial_index +
                                      self.pulse_length]
            odd_HE = self.temp_data_block[1, initial_index:initial_index +
                                          self.pulse_length]
            odd_real = odd_real_all[initial_index:initial_index +
                                    self.pulse_length]

            ##### now we save the data ####

            h5_Ant_dataset = h5_statGroup.require_dataset(
                antenna_names[even_ant_i], (4, self.pulse_length),
                dtype=np.double,
                exact=True)

            h5_Ant_dataset[0] = even_real
            h5_Ant_dataset[1] = even_HE
            h5_Ant_dataset[2] = odd_real
            h5_Ant_dataset[3] = odd_HE

            starting_index = (time_points - half_block) + initial_index
            h5_Ant_dataset.attrs['starting_index'] = starting_index
            h5_Ant_dataset.attrs[
                'PolE_timeOffset'] = -even_delay  ## NOTE: due to historical reasons, there is a sign flip here
            h5_Ant_dataset.attrs[
                'PolO_timeOffset'] = -odd_delay  ## NOTE: due to historical reasons, there is a sign flip here
            h5_Ant_dataset.attrs['PolE_timeOffset_CS'] = even_delay
            h5_Ant_dataset.attrs['PolO_timeOffset_CS'] = odd_delay
            #            ave_delay = (even_delay+odd_delay)*0.5

            sample_time = 5.0E-9
            PolE_HE = even_HE
            PolO_HE = odd_HE
            if self.upsample_factor > 1:
                PolE_HE = resample(even_HE,
                                   len(even_HE) * self.upsample_factor)
                PolO_HE = resample(odd_HE, len(odd_HE) * self.upsample_factor)

                sample_time /= self.upsample_factor

            if np.max(PolE_HE) > self.min_ant_amp:

                PolE_peak_finder = parabolic_fit(PolE_HE)
                h5_Ant_dataset.attrs[
                    'PolE_peakTime'] = starting_index * 5.0E-9 - even_delay + PolE_peak_finder.peak_index * sample_time
                even_pulse_time_list.append(
                    h5_Ant_dataset.attrs['PolE_peakTime'])
            else:
                h5_Ant_dataset.attrs['PolE_peakTime'] = np.nan

            if np.max(PolO_HE) > self.min_ant_amp:

                PolO_peak_finder = parabolic_fit(PolO_HE)
                h5_Ant_dataset.attrs[
                    'PolO_peakTime'] = starting_index * 5.0E-9 - odd_delay + PolO_peak_finder.peak_index * sample_time
                odd_pulse_time_list.append(
                    h5_Ant_dataset.attrs['PolO_peakTime'])
            else:
                h5_Ant_dataset.attrs['PolO_peakTime'] = np.nan

            print("  ", antenna_names[even_ant_i],
                  h5_Ant_dataset.attrs['PolE_peakTime'],
                  h5_Ant_dataset.attrs['PolO_peakTime'])

        if self.current_event_index not in self.editable_pulses:
            self.editable_pulses[self.current_event_index] = {}
        self.editable_pulses[self.current_event_index][station] = (
            np.array(even_pulse_time_list), np.array(odd_pulse_time_list))
コード例 #3
0
def save_EventByLoc(timeID, XYZT, station_timing_delays, pulse_index, output_folder, pulse_width=50, min_ant_amp=5, upsample_factor=0,
                    polarization_flips="polarization_flips.txt", bad_antennas="bad_antennas.txt", additional_antenna_delays = "ant_delays.txt"):
    
    processed_data_folder = processed_data_dir(timeID)
    
    polarization_flips = read_antenna_pol_flips( processed_data_folder + '/' + polarization_flips )
    bad_antennas = read_bad_antennas( processed_data_folder + '/' + bad_antennas )
    additional_antenna_delays = read_antenna_delays(  processed_data_folder + '/' + additional_antenna_delays )
    
    data_dir = processed_data_folder + "/" + output_folder
    if not isdir(data_dir):
        mkdir(data_dir)
        
    logging_folder = data_dir + '/logs_and_plots'
    if not isdir(logging_folder):
        mkdir(logging_folder)

    #Setup logger and open initial data set
    log = logger()
    log.set(logging_folder + "/log_out_"+str(pulse_index)+"_.txt") ## TODo: save all output to a specific output folder
    log.take_stderr()
    log.take_stdout()
    
    print("saving traces to file", pulse_index)
    print("location:", XYZT)
    print("upsample_factor:", upsample_factor)
    
    print()
    print()
    print("station timing offsets")
    print(station_timing_delays)
    
    print()
    print()
    print("pol flips")
    print(polarization_flips)
    
    print()
    print()
    print("bad antennas")
    print(bad_antennas)
    
    print()
    print()
    print("additional antenna delays")
    print(additional_antenna_delays)
    
    
    
    raw_fpaths = filePaths_by_stationName(timeID)
    raw_data_files = {sname:MultiFile_Dal1(fpaths, force_metadata_ant_pos=True, polarization_flips=polarization_flips, bad_antennas=bad_antennas, additional_ant_delays=additional_antenna_delays,
                                           station_delay=station_timing_delays[sname]) for sname,fpaths in raw_fpaths.items() if sname in station_timing_delays}
    
    data_filters = {sname:window_and_filter(timeID=timeID,sname=sname) for sname in station_timing_delays}
    
    trace_locator = getTrace_fromLoc( raw_data_files, data_filters )
    
    print()
    print()
    print("data opened")
    
    out_fname = data_dir + "/potSource_"+str(pulse_index)+".h5"
    out_file = h5py.File(out_fname, "w")
    for sname in station_timing_delays.keys():
        TBB_file = raw_data_files[ sname ]
        TBB_file.find_and_set_polarization_delay()
        antenna_names = TBB_file.get_antenna_names()
        
        
        h5_statGroup = out_file.create_group( sname )
        print()
        print(sname)
        
        for even_ant_i in range(0,len(antenna_names),2):
            even_ant_name = antenna_names[ even_ant_i ]
            odd_ant_name = antenna_names[ even_ant_i+1 ]
            
            starting_index, PolE_offset, throw, PolE_trace = trace_locator.get_trace_fromLoc(XYZT, even_ant_name, pulse_width, do_remove_RFI=True, do_remove_saturation=True)
            throw, PolO_offset, throw, PolO_trace = trace_locator.get_trace_fromIndex(starting_index, odd_ant_name, pulse_width, do_remove_RFI=True, do_remove_saturation=True)
            
            PolE_HE = np.abs(PolE_trace)
            PolO_HE = np.abs(PolO_trace)
            
            
            h5_Ant_dataset = h5_statGroup.create_dataset(even_ant_name, (4, pulse_width ), dtype=np.double)
            
            h5_Ant_dataset[0] = np.real(PolE_trace)
            h5_Ant_dataset[1] = PolE_HE
            h5_Ant_dataset[2] = np.real(PolO_trace)
            h5_Ant_dataset[3] = PolO_HE
            
            ### note that peak time should NOT account for station timing offsets!
            
            h5_Ant_dataset.attrs['starting_index'] = starting_index
            h5_Ant_dataset.attrs['PolE_timeOffset'] = -(PolE_offset - station_timing_delays[sname])
            h5_Ant_dataset.attrs['PolO_timeOffset'] = -(PolO_offset - station_timing_delays[sname])
            h5_Ant_dataset.attrs['PolE_timeOffset_CS'] = (PolE_offset - station_timing_delays[sname])
            h5_Ant_dataset.attrs['PolO_timeOffset_CS'] = (PolO_offset - station_timing_delays[sname])
            
            
            
            
            sample_time = 5.0E-9
            if upsample_factor > 1:
                PolE_HE = resample(PolE_HE, len(PolE_HE)*upsample_factor )
                PolO_HE = resample(PolO_HE, len(PolO_HE)*upsample_factor )
                
                sample_time /= upsample_factor
            
            
            if np.max(PolE_HE)> min_ant_amp:
                
                
                PolE_peak_finder = parabolic_fit( PolE_HE )
                h5_Ant_dataset.attrs['PolE_peakTime'] =  starting_index*5.0E-9 - (PolE_offset-station_timing_delays[sname]) + PolE_peak_finder.peak_index*sample_time
            else:
                h5_Ant_dataset.attrs['PolE_peakTime'] = np.nan
            
            if np.max(PolO_HE)> min_ant_amp:
                
                
                PolO_peak_finder = parabolic_fit( PolO_HE )
                h5_Ant_dataset.attrs['PolO_peakTime'] =  starting_index*5.0E-9 - (PolO_offset-station_timing_delays[sname]) + PolO_peak_finder.peak_index*sample_time
            else:
                h5_Ant_dataset.attrs['PolO_peakTime'] =  np.nan
            
            print("  ", even_ant_name, h5_Ant_dataset.attrs['PolE_peakTime'], h5_Ant_dataset.attrs['PolO_peakTime'])
                
            
    out_file.close()
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
    
コード例 #4
0
def planewave_fits(timeID, station, polarization, initial_block, number_of_blocks, pulses_per_block=10, pulse_length=50, min_amplitude=50, upsample_factor=0, min_num_antennas=4,
                    polarization_flips="polarization_flips.txt", bad_antennas="bad_antennas.txt", additional_antenna_delays = "ant_delays.txt", max_num_planewaves=np.inf,
                    positive_saturation = 2046, negative_saturation = -2047, saturation_post_removal_length = 50, saturation_half_hann_length = 50, verbose=True):
    
    left_pulse_length = int(pulse_length/2)
    right_pulse_length = pulse_length-left_pulse_length
    
    processed_data_folder = processed_data_dir(timeID)
    polarization_flips = read_antenna_pol_flips( processed_data_folder + '/' + polarization_flips )
    bad_antennas = read_bad_antennas( processed_data_folder + '/' + bad_antennas )
    additional_antenna_delays = read_antenna_delays(  processed_data_folder + '/' + additional_antenna_delays )
    
    raw_fpaths = filePaths_by_stationName(timeID)
    TBB_data = MultiFile_Dal1( raw_fpaths[station], polarization_flips=polarization_flips, bad_antennas=bad_antennas, additional_ant_delays=additional_antenna_delays )
    ant_names = TBB_data.get_antenna_names()
    antenna_locations = TBB_data.get_LOFAR_centered_positions()
    antenna_delays = TBB_data.get_timing_callibration_delays()
    num_antenna_pairs = int( len( ant_names )/2 ) 
    
    if num_antenna_pairs < min_num_antennas:
        return np.array([]), np.array([]), np.array([])

    RFI_filter = window_and_filter(timeID=timeID, sname=station)
    block_size = RFI_filter.blocksize
    
    out_RMS = np.empty( number_of_blocks*pulses_per_block, dtype=np.double )
    out_zenith = np.empty( number_of_blocks*pulses_per_block, dtype=np.double )
    out_azimuth = np.empty( number_of_blocks*pulses_per_block, dtype=np.double )
    N = 0
    data = np.empty( (num_antenna_pairs,block_size), dtype=np.double )
    for block in range(initial_block, initial_block+number_of_blocks):
        if N >= max_num_planewaves:
            break
        
        #### open and filter data
        for pair_i in range(num_antenna_pairs):
            ant_i = pair_i*2 + polarization
            
            data[pair_i,:] = TBB_data.get_data(block*block_size, block_size, antenna_index=ant_i)
            remove_saturation(data[pair_i,:], positive_saturation, negative_saturation, saturation_post_removal_length, saturation_half_hann_length)
            np.abs( RFI_filter.filter( data[pair_i,:] ), out=data[pair_i,:])
    
    
        #### loop over finding planewaves
        i = 0
        while i < pulses_per_block:
            if N >= max_num_planewaves:
                break
            
            pulse_location = 0
            pulse_amplitude = 0
            
            ## find highest peak
            for pair_i, HE in enumerate(data):
                loc = np.argmax( HE )
                amp = HE[ loc ]
                
                if amp > pulse_amplitude:
                    pulse_amplitude = amp
                    pulse_location = loc
                    
            ## check if is strong enough
            if pulse_amplitude < min_amplitude:
                break
            
            ## get 
            fitter = locatefier()
            for pair_i, HE in enumerate(data):
                ant_i = pair_i*2 + polarization
                
                signal = np.array( HE[ pulse_location-left_pulse_length : pulse_location+right_pulse_length] )
                if num_double_zeros(signal, threshold=0.1) == 0:
                    
                    sample_time = 5.0E-9
                    if upsample_factor > 1:
                        sample_time /= upsample_factor
                        signal = resample(signal, len(signal)*upsample_factor )
                        
                    peak_finder = parabolic_fit( signal )
                    peak_time = peak_finder.peak_index*sample_time - antenna_delays[ant_i]
                    fitter.add_antenna(peak_time, antenna_locations[ant_i])
                    
                HE[ pulse_location-left_pulse_length : pulse_location+right_pulse_length] = 0.0
            
            if fitter.num_measurments() < min_num_antennas:
                continue
            
            fitter.prep_for_fitting()
            
            X = brute(fitter.RMS, ranges=[[0,np.pi/2],[0,np.pi*2]], Ns=20)
            
            if X[0] >= np.pi/2:
                X[0] = (np.pi/2)*0.99
            if X[1] >= np.pi*2:
                X[1] = (np.pi*2)*0.99
                
            if X[0] < 0:
                X[0] = 0.00000001
            if X[1] < 0:
                X[1] = 0.00000001
            
            ret = least_squares( fitter.time_fits, X, bounds=[[0,0],[np.pi/2,2*np.pi]], ftol=3e-16, xtol=3e-16, gtol=3e-16, x_scale='jac' )
            
            out_RMS[N] = fitter.RMS(ret.x, 3)
            out_zenith[N] = ret.x[0] 
            out_azimuth[N] = ret.x[1] 
            N += 1
            
            i += 1
            
    return out_RMS[:N], out_zenith[:N], out_azimuth[:N]
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
コード例 #5
0
def save_Pulse(timeID, output_folder, event_index, pulse_time, station_delays, skip_stations=[], window_width=7E-6, pulse_width=50, block_size=2**16, 
               min_ant_amp=5.0, guess_flash_location=None, referance_station="CS002", polarization_flips="polarization_flips.txt", 
               bad_antennas="bad_antennas.txt", additional_antenna_delays = "ant_delays.txt"):
    """Desined to be used in conjuction with plot_multiple_data_all_stations.py. Given a pulse_time, and window_width, it saves a pulse on every antenna that is 
    centered on the largest peak in the window, with a width of pulse_width. station_delays doesn't need to be accurate, just should be the same as used in plot_multiple_data_all_stations.
    Same for referance_station, and guess_flash_location. Note that this will save the pulse under an index, and will overwrite any other pulse saved under that index in the output folder"""
    
    
    if referance_station in station_delays:
        ref_delay = station_delays[referance_station]
        station_delays = {sname:delay-ref_delay for sname,delay in station_delays.items()}
    
    #### setup directory variables ####
    processed_data_folder = processed_data_dir(timeID)
    
    data_dir = processed_data_folder + "/" + output_folder
    if not isdir(data_dir):
        mkdir(data_dir)
        
    logging_folder = data_dir + '/logs_and_plots'
    if not isdir(logging_folder):
        mkdir(logging_folder)

    #Setup logger and open initial data set
    log.set(logging_folder + "/log_event_"+str(event_index)+".txt") ## TODo: save all output to a specific output folder
    log.take_stderr()
    log.take_stdout()
    
    print("pulse time:", pulse_time)
    print("window_width:", window_width)
    print("pulse_width:", pulse_width)
    print("skip stations:", skip_stations)
    print("guess_flash_location:", guess_flash_location)
    print("input delays:")
    print( station_delays)
    print()
    
        ##station data
    print()
    print("opening station data")
    
    polarization_flips = read_antenna_pol_flips( processed_data_folder + '/' + polarization_flips )
    bad_antennas = read_bad_antennas( processed_data_folder + '/' + bad_antennas )
    additional_antenna_delays = read_antenna_delays(  processed_data_folder + '/' + additional_antenna_delays )
    
        
    raw_fpaths = filePaths_by_stationName(timeID)
    raw_data_files = {sname:MultiFile_Dal1(raw_fpaths[sname], force_metadata_ant_pos=True, polarization_flips=polarization_flips, bad_antennas=bad_antennas, additional_ant_delays=additional_antenna_delays) \
                      for sname in station_delays.keys()}
    
    data_filters = {sname:window_and_filter(timeID=timeID,sname=sname) for sname in station_delays.keys()}
    
    trace_locator = getTrace_fromLoc( raw_data_files, data_filters, {sname:0.0 for sname in station_delays.keys()} )
    
    
    if guess_flash_location is not None:
        guess_flash_location = np.array(guess_flash_location)
        
        ref_stat_file = raw_data_files[ referance_station ]
        ant_loc = ref_stat_file.get_LOFAR_centered_positions()[0]
        ref_delay = np.linalg.norm(ant_loc-guess_flash_location[:3])/v_air - ref_stat_file.get_nominal_sample_number()*5.0E-9
        
        for sname, data_file in raw_data_files.items():
            if sname not in station_delays:
                station_delays[sname] = 0.0
            
            data_file = raw_data_files[sname]
            ant_loc = data_file.get_LOFAR_centered_positions()[0]
            station_delays[sname] += (np.linalg.norm(ant_loc-guess_flash_location[:3])/v_air - ref_delay) 
            station_delays[sname] -= data_file.get_nominal_sample_number()*5.0E-9
    
    print()
    print("used apparent delays:")
    print(station_delays)
    print()
    
    
    out_fname = data_dir + "/potSource_"+str(event_index)+".h5"
    out_file = h5py.File(out_fname, "w")
    
    half_pulse_width = int(0.5*pulse_width)
    window_width_samples = int(window_width/5.0E-9)
    for sname in station_delays.keys():
        if (not np.isfinite(station_delays[sname])) or sname in skip_stations:
            continue
        
        h5_statGroup = out_file.create_group( sname )
        
        antenna_names = raw_data_files[sname].get_antenna_names()
        
        data_arrival_index = int((pulse_time + station_delays[sname] - window_width*0.5)/5.0E-9)
        
        print()
        print(sname)
        
        for even_ant_i in range(0, len(antenna_names), 2):
            
            #### get window and find peak ####
            throw, even_total_time_offset, throw, even_trace = trace_locator.get_trace_fromIndex( data_arrival_index, antenna_names[even_ant_i], window_width_samples)
            throw, odd_total_time_offset, throw, odd_trace = trace_locator.get_trace_fromIndex( data_arrival_index, antenna_names[even_ant_i+1], window_width_samples)

            even_HE = np.abs(even_trace)
            even_good = False
            if np.max( even_HE ) > min_ant_amp:
                even_good = True
                even_para_fit = parabolic_fit( even_HE )
            
            odd_HE = np.abs(odd_trace)
            odd_good = False
            if np.max( odd_HE ) > min_ant_amp:
                odd_good = True
                odd_para_fit = parabolic_fit( odd_HE )
                
            if (not even_good) and (not odd_good):
                continue
            elif even_good and (not odd_good):
                center_index = int(even_para_fit.peak_index)
            elif (not even_good) and odd_good:
                center_index = int(odd_para_fit.peak_index)
            else: ## both good
                even_amp = even_HE[ int(even_para_fit.peak_index) ]
                odd_amp = odd_HE[ int(odd_para_fit.peak_index) ]
                if even_amp > odd_amp:
                    center_index = int(even_para_fit.peak_index)
                else:
                    center_index = int(odd_para_fit.peak_index)

            
            #### now get data centered on peak. re-get data, as peak may be near edge ####
            throw, even_total_time_offset, throw, even_trace = trace_locator.get_trace_fromIndex( data_arrival_index+center_index-half_pulse_width, antenna_names[even_ant_i], half_pulse_width*2)
            throw, odd_total_time_offset , throw, odd_trace  = trace_locator.get_trace_fromIndex( data_arrival_index+center_index-half_pulse_width, antenna_names[even_ant_i+1], half_pulse_width*2)
                  
#            even_trace = even_trace[center_index-half_pulse_width:center_index+half_pulse_width]
#            odd_trace = odd_trace[center_index-half_pulse_width:center_index+half_pulse_width]
            even_HE = np.abs(even_trace)
            odd_HE = np.abs(odd_trace)
                
            h5_Ant_dataset = h5_statGroup.create_dataset(antenna_names[even_ant_i], (4, half_pulse_width*2), dtype=np.double)
            h5_Ant_dataset[0] = np.real( even_trace )
            h5_Ant_dataset[1] = even_HE
            h5_Ant_dataset[2] = np.real( odd_trace )
            h5_Ant_dataset[3] = odd_HE
                        
            WARNING: not sure this is correct. even_total_time_offset includes station delay
            starting_index = data_arrival_index+center_index-half_pulse_width
            h5_Ant_dataset.attrs['starting_index'] = starting_index
            h5_Ant_dataset.attrs['PolE_timeOffset'] = -even_total_time_offset ## NOTE: due to historical reasons, there is a sign flip here
            h5_Ant_dataset.attrs['PolO_timeOffset'] = -odd_total_time_offset ## NOTE: due to historical reasons, there is a sign flip here
               
            if np.max(even_HE) > min_ant_amp:
#                even_HE = resample(even_HE, len(even_HE)*8 )
#                even_para_fit = parabolic_fit( even_HE )
#                h5_Ant_dataset.attrs['PolE_peakTime'] = (even_para_fit.peak_index/8.0 + starting_index)*5.0E-9 - even_total_time_offset
                even_para_fit = parabolic_fit( even_HE )
                h5_Ant_dataset.attrs['PolE_peakTime'] = (even_para_fit.peak_index + starting_index)*5.0E-9 - even_total_time_offset
            else:
                h5_Ant_dataset.attrs['PolE_peakTime'] = np.nan
                 
            if np.max(odd_HE) > min_ant_amp:
#                odd_HE = resample(odd_HE, len(odd_HE)*8 )
#                odd_para_fit = parabolic_fit( odd_HE )
#                h5_Ant_dataset.attrs['PolO_peakTime'] = (odd_para_fit.peak_index/8.0 + starting_index)*5.0E-9 - odd_total_time_offset
                odd_para_fit = parabolic_fit( odd_HE )
                h5_Ant_dataset.attrs['PolO_peakTime'] = (odd_para_fit.peak_index + starting_index)*5.0E-9 - odd_total_time_offset
            else:
                h5_Ant_dataset.attrs['PolO_peakTime'] = np.nan
                
            
            print("   ", antenna_names[even_ant_i], (data_arrival_index+center_index)*5.0E-9 - station_delays[sname], h5_Ant_dataset.attrs['PolE_peakTime'], h5_Ant_dataset.attrs['PolO_peakTime'] )
            
    out_file.close()