Beispiel #1
0
 def parse_line_v1(line):
     ant_name, pol = line.split()[0:2]
     # bad_antenna_data.append((ant_name,int(pol)))
     if pol:
         bad_antenna_data.append(util.even_antName_to_odd(ant_name))
     else:
         bad_antenna_data.append(ant_name)
Beispiel #2
0
    def parse_line_v2(line):
        ant_name, delay = line.split()[0:2]
        pol = 0
        if not util.antName_is_even(ant_name):
            ant_name = util.even_antName_to_odd(ant_name)
            pol = 1

        if ant_name not in additional_ant_delays:
            additional_ant_delays[ant_name] = [0.0, 0.0]

        additional_ant_delays[ant_name][pol] = float(delay)
def plot_all_stations(event_ID, 
               timeID, output_folder, pulse_input_folders, guess_timings, sources_to_fit, guess_source_locations,
               source_polarizations, source_stations_to_exclude, source_antennas_to_exclude, bad_ants,
               antennas_to_recalibrate={}, min_ant_amplitude=10, ref_station="CS002"):
    
    processed_data_folder = processed_data_dir( timeID )
    file_manager = input_manager( processed_data_folder, pulse_input_folders )
    throw, input_Fname = file_manager.known_source( event_ID )
    
    data_file = h5py.File(input_Fname, "r")
    
    fpaths = filePaths_by_stationName( timeID )
    
    source_XYZT = guess_source_locations[event_ID]
    source_polarization = source_polarizations[event_ID]
    antennas_to_exclude = bad_ants + source_antennas_to_exclude[event_ID]
    
    ref_station_delay = 0.0
    if ref_station in guess_timings:
        ref_station_delay = guess_timings[ref_station]
    
    current_offset = 0
    for sname in data_file.keys():
        if (sname not in guess_timings and sname !=ref_station) or (sname in source_stations_to_exclude[event_ID]):
            continue
            
        stat_group = data_file[ sname ]
        station_file = MultiFile_Dal1(fpaths[sname], force_metadata_ant_pos=True)
            
        stat_delay = 0.0
        if sname != ref_station:
            stat_delay = guess_timings[sname] - ref_station_delay
        
        even_HEs = []
        odd_HEs = []
        
        even_offet = []
        odd_offset = []
        
        even_peakT_offset = []
        odd_peakT_offset = []
        
        peak_amp = 0.0
        ## first we open the data
        for ant_name, ant_loc in zip(station_file.get_antenna_names(),station_file.get_LOFAR_centered_positions()):
            if ant_name not in stat_group:
                continue
            
            even_ant_name = ant_name
            odd_ant_name = even_antName_to_odd( ant_name )
            
            ant_dataset = stat_group[ant_name]
#            even_trace = np.array( ant_dataset[0] )
            even_HE = np.array( ant_dataset[1] )
#            odd_trace = np.array( ant_dataset[2] )
            odd_HE = np.array( ant_dataset[3] )
            
            even_amp = np.max(even_HE)
            odd_amp = np.max(odd_HE)
            
            
            travel_delay = np.sqrt( (ant_loc[0]-source_XYZT[0])**2 + (ant_loc[1]-source_XYZT[1])**2 + (ant_loc[2]-source_XYZT[2])**2 )/v_air
            total_correction = travel_delay + stat_delay
            
            even_time =  - total_correction - source_XYZT[3]
            odd_time =  - total_correction
            
            if even_ant_name in antennas_to_recalibrate:
                    
                even_time -= antennas_to_recalibrate[even_ant_name]
                
            if odd_ant_name in antennas_to_recalibrate:
                odd_time -= antennas_to_recalibrate[odd_ant_name]  
                
            if source_polarization==2 and len(source_XYZT)==5:
                odd_time -= source_XYZT[4]
            else:
                odd_time -= source_XYZT[3]
                
            even_peak_time = ant_dataset.attrs["PolE_peakTime"] + even_time
            odd_peak_time = ant_dataset.attrs["PolO_peakTime"] + odd_time
                
            even_time += ant_dataset.attrs['starting_index']*5.0E-9 - ant_dataset.attrs['PolE_timeOffset_CS']
            odd_time += ant_dataset.attrs['starting_index']*5.0E-9 - ant_dataset.attrs['PolO_timeOffset_CS']
            
            even_is_good = (even_amp>min_ant_amplitude) and not (even_ant_name in antennas_to_exclude) 
            odd_is_good  = (odd_amp>min_ant_amplitude) and not (odd_ant_name in antennas_to_exclude)
                
            if (not even_is_good) and (not odd_is_good):
                continue
            
            M = max(even_amp, odd_amp)
            if M>peak_amp:
                peak_amp = M
                
            if even_is_good and source_polarization!=1:
                even_HEs.append( even_HE )
            else:
                even_HEs.append( None )
                
            if odd_is_good and source_polarization!=0:
                odd_HEs.append( odd_HE )
            else:
                odd_HEs.append( None )
        
            even_offet.append( even_time )
            odd_offset.append( odd_time )

            even_peakT_offset.append( even_peak_time )
            odd_peakT_offset.append( odd_peak_time )
            
        earliest_T = np.inf
        for pair_i in range(len(even_HEs)):
            
            if even_HEs[pair_i] is not None:
                even_T = np.arange( len(even_HEs[pair_i]) )*5.0E-9 + even_offet[pair_i]
                
                if even_T[0] < earliest_T:
                    earliest_T = even_T[0]
                
                p = plt.plot(even_T, even_HEs[pair_i]/peak_amp + current_offset)
                color = p[0].get_color()
                
                plt.plot( (even_peakT_offset[pair_i],even_peakT_offset[pair_i]), (current_offset,current_offset+1), color=color )
                
            if odd_HEs[pair_i] is not None:
                odd_T = np.arange( len(odd_HEs[pair_i]) )*5.0E-9 + odd_offset[pair_i]
                
                if odd_T[0] < earliest_T:
                    earliest_T = odd_T[0]
                
                p = plt.plot(odd_T, odd_HEs[pair_i]/peak_amp + current_offset)
                color = p[0].get_color()
                
                plt.plot( (odd_peakT_offset[pair_i],odd_peakT_offset[pair_i]), (current_offset,current_offset+1), color=color )

        plt.annotate(sname, xy=(earliest_T,current_offset+0.5))
        current_offset += 1
        
    plt.axvline(x=0)
    plt.show()
def plot_station(event_ID, sname_to_plot, plot_bad_antennas,
               timeID, output_folder, pulse_input_folders, guess_timings, sources_to_fit, guess_source_locations,
               source_polarizations, source_stations_to_exclude, source_antennas_to_exclude, bad_ants,
               antennas_to_recalibrate={}, min_ant_amplitude=10, ref_station="CS002"):
    
    processed_data_folder = processed_data_dir( timeID )
    file_manager = input_manager( processed_data_folder, pulse_input_folders )
    throw, input_Fname = file_manager.known_source( event_ID )
    
    
    data_file = h5py.File(input_Fname, "r")
    stat_group = data_file[ sname_to_plot ]
    
    fpaths = filePaths_by_stationName( timeID )[ sname_to_plot ]
    station_file = MultiFile_Dal1(fpaths, force_metadata_ant_pos=True)
    
    source_XYZT = guess_source_locations[event_ID]
    source_polarization = source_polarizations[event_ID]
    antennas_to_exclude = bad_ants + source_antennas_to_exclude[event_ID]
    
    ref_station_delay = 0.0
    if ref_station in guess_timings:
        ref_station_delay = guess_timings[ref_station]
        
    stat_delay = 0.0
    if sname_to_plot != ref_station:
        stat_delay = guess_timings[sname_to_plot] - ref_station_delay
    
    even_HEs = []
    odd_HEs = []
    even_sig = []
    odd_sig = []
    even_good = []
    odd_good = []
    even_offet = []
    odd_offset = []
    even_peakT_offset = []
    odd_peakT_offset = []
    even_ant_names = []
    odd_ant_names = []
    
    peak_amp = 0.0
    ## first we open the data
    even_SSqE = 0.0
    odd_SSqE = 0.0
    even_N = 0
    odd_N = 0
    for ant_name, ant_loc in zip(station_file.get_antenna_names(),station_file.get_LOFAR_centered_positions()):
        if ant_name not in stat_group:
            continue
        
        even_ant_name = ant_name
        odd_ant_name = even_antName_to_odd( ant_name )
        
        ant_dataset = stat_group[ant_name]
        even_trace = np.array( ant_dataset[0] )
        even_HE = np.array( ant_dataset[1] )
        odd_trace = np.array( ant_dataset[2] )
        odd_HE = np.array( ant_dataset[3] )
        
        even_amp = np.max(even_HE)
        odd_amp = np.max(odd_HE)
        
        M = max(even_amp, odd_amp)
        if M>peak_amp:
            peak_amp = M
        
        travel_delay = np.sqrt( (ant_loc[0]-source_XYZT[0])**2 + (ant_loc[1]-source_XYZT[1])**2 + (ant_loc[2]-source_XYZT[2])**2 )/v_air
        travel_delay_odd = travel_delay
        
        if source_polarization==3  and len(source_XYZT)==8:
            travel_delay_odd = np.sqrt( (ant_loc[0]-source_XYZT[4])**2 + (ant_loc[1]-source_XYZT[5])**2 + (ant_loc[2]-source_XYZT[6])**2 )/v_air
        
        even_time =  - travel_delay - stat_delay -source_XYZT[3]
        odd_time =  - travel_delay_odd - stat_delay
        
        if even_ant_name in antennas_to_recalibrate:
            even_time -= antennas_to_recalibrate[even_ant_name]
            
        if odd_ant_name in antennas_to_recalibrate:
            odd_time -= antennas_to_recalibrate[odd_ant_name]  
            
        if source_polarization==0 or source_polarization==1 or len(source_XYZT)==4:
            odd_time -= source_XYZT[3]
        elif (source_polarization==2 or source_polarization==3) and len(source_XYZT)==5:
            odd_time -= source_XYZT[4]
        elif (source_polarization==2 or source_polarization==3)  and len(source_XYZT)==8:
            odd_time -= source_XYZT[7]
            
        even_peak_time = ant_dataset.attrs["PolE_peakTime"] + even_time
        odd_peak_time = ant_dataset.attrs["PolO_peakTime"] + odd_time
            
        even_time += ant_dataset.attrs['starting_index']*5.0E-9 - ant_dataset.attrs['PolE_timeOffset_CS']
        odd_time += ant_dataset.attrs['starting_index']*5.0E-9 - ant_dataset.attrs['PolO_timeOffset_CS']
        
        
        even_is_good = even_amp>min_ant_amplitude
        if not even_is_good:
            print( even_ant_name, ": amp too low" )
        else:
            even_is_good = not (even_ant_name in antennas_to_exclude)
            if not even_is_good:
                print( even_ant_name, ": excluded" )
            else:
                print( even_ant_name, ": good" )
            
        odd_is_good  = odd_amp>min_ant_amplitude
        if not odd_is_good:
            print( odd_ant_name, ": amp too low" )
        else:
            odd_is_good = not (odd_ant_name in antennas_to_exclude)
            if not odd_is_good:
                print( odd_ant_name, ": excluded" )
            else:
                print( odd_ant_name, ": good" )
                
            
        even_HEs.append( even_HE )
        odd_HEs.append( odd_HE )
        even_sig.append( even_trace )
        odd_sig.append( odd_trace )
        even_good.append( even_is_good )
        odd_good.append( odd_is_good )
        even_offet.append( even_time )
        odd_offset.append( odd_time )
        even_ant_names.append( even_ant_name )
        odd_ant_names.append( odd_ant_name )
        even_peakT_offset.append( even_peak_time )
        odd_peakT_offset.append( odd_peak_time )
        
        if even_is_good:
            even_SSqE += even_peak_time*even_peak_time
            even_N += 1
            
        if odd_is_good:
            odd_SSqE += odd_peak_time*odd_peak_time
            odd_N += 1
    if even_N > 0:
        print('even RMS:', np.sqrt(even_SSqE/even_N),end=' ')
    if odd_N > 0:
        print('odd RMS:', np.sqrt(odd_SSqE/odd_N), end='')
    print()
        
    current_offset = 0
    for pair_i in range(len(even_HEs)):
        
        even_T = np.arange( len(even_HEs[pair_i]) )*5.0E-9 + even_offet[pair_i]
        odd_T = np.arange( len(odd_HEs[pair_i]) )*5.0E-9 + odd_offset[pair_i]
        
        if even_good[pair_i] or plot_bad_antennas:
            plt.plot(even_T, even_HEs[pair_i]/peak_amp + current_offset, 'g')
            plt.plot(even_T, even_sig[pair_i]/peak_amp + current_offset, 'g')
            
        if even_good[pair_i]:
            plt.plot( (even_peakT_offset[pair_i],even_peakT_offset[pair_i]), (current_offset,current_offset+1), 'g' )
            
        if odd_good[pair_i] or plot_bad_antennas:
            plt.plot(odd_T, odd_HEs[pair_i]/peak_amp + current_offset, 'm')
            plt.plot(odd_T, odd_sig[pair_i]/peak_amp + current_offset, 'm')
            
        if odd_good[pair_i]:
            plt.plot( (odd_peakT_offset[pair_i],odd_peakT_offset[pair_i]), (current_offset,current_offset+1), 'm' )
        
        even_str = even_ant_names[pair_i] + " "
        odd_str = odd_ant_names[pair_i] + " "
        
        if even_good[pair_i]:
            even_str += ': good'
        else:
            even_str += ': bad'
        
        if odd_good[pair_i]:
            odd_str += ': good'
        else:
            odd_str += ': bad'
            
        plt.annotate(odd_str,  xy=(odd_T[0], current_offset+0.3), c='m')
        plt.annotate(even_str, xy=(even_T[0], current_offset+0.6), c='g')
        
        current_offset += 2
        
    plt.axvline(x=0)
    plt.show()
            
        
        
        
    
Beispiel #5
0
    def __init__(self,
                 filename_list,
                 force_metadata_ant_pos=False,
                 total_cal=None,
                 polarization_flips=None,
                 bad_antennas=[],
                 additional_ant_delays=None,
                 station_delay=0.0,
                 only_complete_pairs=True,
                 pol_flips_are_bad=False):
        """filename_list:  list of filenames for this station for this event.
            force_metadata_ant_pos -if True, then load antenna positions from a metadata file and not the raw data file. Default False
            polarization_flips     -list of even antennas where it is known that even and odd antenna names are flipped in file. This is assumed to apply both to data and timing calibration
            bad_antennas           -list of antennas that should not be used. Each item in the list is a tuple, first item of tuple is name of even antenna, second item is a 0 or 1 indicating if even or odd antenna is bad.
                                        assumed to be BEFORE antenna flips are accounted for
            additional_ant_delays  -a dictionary. Each key is name of even antenna, each value is a tuple with additional even and odd antenna delays. This should rarely be needed.
                                        assumed to be found BEFORE antenna flips are accounted for
            station_delay          -a single number that represents the clock offset of this station, as a delay
            NOTE: polarization_flips, bad_antennas, additional_ant_delays, and station_delay can now be strings that are file names. If this is the case, they will be read automatically
            only_complete_pairs    -if True, discards antenna if the other in pair is not present or is bad. If False, keeps all good antennas with a 'none' value if other antenna in pair is missing
            pol_flips_are_bad      -if True, antennas that are in pol-flips are included in 'bad_antennas' 
        NOTE: This always defaults to using antenna timing calibration from metadata."""

        self.files = [
            TBBData_Dal1(fname, force_metadata_ant_pos)
            for fname in filename_list
        ]

        #### get some data that should be constant #### TODO: change code to make use of getters
        self.antennaSet = self.files[0].antennaSet
        self.StationID = self.files[0].StationID
        self.StationName = self.files[0].StationName
        self.SampleFrequency = self.files[0].SampleFrequency
        self.FilterSelection = self.files[0].FilterSelection
        self.Time = self.files[0].Time

        if total_cal is not None:
            self.using_total_cal = True

            if isinstance(total_cal, str):
                bad_antennas, polarization_flips, station_delays, ant_delays = read_cal_file(
                    total_cal, pol_flips_are_bad)
            elif isinstance(total_cal, list):
                bad_antennas, polarization_flips, station_delays, ant_delays = total_cal
            else:
                ## bad_antennas from input
                #polarization_flips from input
                station_delays = {
                    self.StationName: station_delay
                }  ## hack to be compatible with below
                ant_delays = additional_ant_delays
                additional_ant_delays = None  ## since this should really be a different thing

            if self.StationName in station_delays:
                station_delay = station_delays[self.StationName]
            else:
                station_delay = 0.0
            self.ant_delays = ant_delays

        else:
            self.using_total_cal = False

            if isinstance(polarization_flips, str):
                polarization_flips = read_antenna_pol_flips(polarization_flips)
            if isinstance(bad_antennas, str):
                bad_antennas = read_bad_antennas(bad_antennas)
            if isinstance(additional_ant_delays, str):
                additional_ant_delays = read_antenna_delays(
                    additional_ant_delays)

            if isinstance(station_delay, str):
                station_delay = read_station_delays(station_delay)[
                    self.StationName]

            if polarization_flips is not None and pol_flips_are_bad:
                for even_ant in polarization_flips:
                    bad_antennas.append(even_ant)
                    bad_antennas.append(util.even_antName_to_odd(even_ant))
                    # bad_antennas.append( (even_ant,0) )
                    # bad_antennas.append( (even_ant,1) )
                polarization_flips = []

        self.bad_antennas = bad_antennas
        self.odd_pol_additional_timing_delay = 0.0  # anouther timing delay to add to all odd-polarized antennas. Should remain zero if using_total_cal
        self.station_delay = station_delay

        #### check consistancy of data ####
        for TBB_file in self.files:
            if TBB_file.antennaSet != self.antennaSet:
                raise IOError(
                    "antenna set not the same between files for station: " +
                    self.StationName)
            if TBB_file.StationID != self.StationID:
                raise IOError(
                    "station ID not the same between files for station: " +
                    self.StationName)
            if TBB_file.StationName != self.StationName:
                raise IOError(
                    "station name not the same between files for station: " +
                    self.StationName)
            if TBB_file.FilterSelection != self.FilterSelection:
                raise IOError(
                    "filter selection not the same between files for station: "
                    + self.StationName)
            if TBB_file.Time != self.Time:
                raise IOError(
                    "antenna set not the same between files for station: " +
                    self.StationName)

        ## check LBA outer antenna set
        if self.antennaSet != "LBA_OUTER":
            print("WARNING: antenna set on station", self.StationName,
                  "is not LBA_OUTER")

        #### find best files to get antennas from ####
        ## require each antenna shows up once, and even pol is followed by odd pol

        self.dipoleNames = []
        self.antenna_to_file = [
        ]  ##each item is a tuple. First item is file object, second is antenna index in file

        unused_antenna_names = []
        unused_antenna_to_file = []
        # bad_PolE_antennas = [ant for ant,pol in bad_antennas if pol==0]
        # bad_PolO_antennas = [ant for ant,pol in bad_antennas if pol==1] ## note that this is still the name of the even antenna, although it is the ODD antenna that is bad!!!
        for TBB_file in self.files:
            file_ant_names = TBB_file.get_antenna_names()

            for ant_i, ant_name in enumerate(file_ant_names):

                if (ant_name in self.dipoleNames):
                    continue
                # ant_ID = int(ant_name[-3:])

                if util.antName_is_even(
                        ant_name):  #ant_ID%2 == 0: ##check if antenna is even

                    if ant_name in bad_antennas:  #bad_PolE_antennas:
                        continue

                    odd_ant_name = util.even_antName_to_odd(
                        ant_name)  #ant_name[:-3] + str(ant_ID+1).zfill(3)
                    if odd_ant_name in unused_antenna_names:  ## we have the odd antenna
                        self.dipoleNames.append(ant_name)
                        self.dipoleNames.append(odd_ant_name)

                        self.antenna_to_file.append((TBB_file, ant_i))
                        odd_unused_index = unused_antenna_names.index(
                            odd_ant_name)
                        self.antenna_to_file.append(
                            unused_antenna_to_file[odd_unused_index])

                        unused_antenna_names.pop(odd_unused_index)
                        unused_antenna_to_file.pop(odd_unused_index)
                    else:  ## we haven't found the odd antenna, so store info for now
                        unused_antenna_names.append(ant_name)
                        unused_antenna_to_file.append((TBB_file, ant_i))

                else:  ## antenna is odd
                    even_ant_name = util.odd_antName_to_even(
                        ant_name)  #ant_name[:-3] + str(ant_ID-1).zfill(3)
                    if ant_name in bad_antennas:  #bad_PolO_antennas: ## note that have to check if EVEN antenna is in bad antenna names...
                        continue

                    if even_ant_name in unused_antenna_names:  ## we have the even antenna
                        self.dipoleNames.append(even_ant_name)
                        self.dipoleNames.append(ant_name)

                        even_unused_index = unused_antenna_names.index(
                            even_ant_name)
                        self.antenna_to_file.append(
                            unused_antenna_to_file[even_unused_index])

                        unused_antenna_names.pop(even_unused_index)
                        unused_antenna_to_file.pop(even_unused_index)

                        self.antenna_to_file.append((TBB_file, ant_i))

                    else:  ## we haven't found the odd antenna, so store info for now
                        unused_antenna_names.append(ant_name)
                        unused_antenna_to_file.append((TBB_file, ant_i))

        if not only_complete_pairs:
            for ant_name, to_file in zip(unused_antenna_names,
                                         unused_antenna_to_file):
                ant_ID = int(ant_name[-3:])
                if ant_ID % 2 == 0:  ##check if antenna is even

                    self.dipoleNames.append(ant_name)
                    self.antenna_to_file.append(to_file)

                    self.dipoleNames.append(
                        ant_name[:-3] +
                        str(ant_ID + 1).zfill(3))  ## add the odd antenna
                    self.antenna_to_file.append(
                        None)  ## doesn't exist in a file

                else:

                    self.dipoleNames.append(
                        ant_name[:-3] +
                        str(ant_ID - 1).zfill(3))  ## add the even antenna
                    self.antenna_to_file.append(
                        None)  ## doesn't exist in a file

                    self.dipoleNames.append(ant_name)
                    self.antenna_to_file.append(to_file)

        if len(self.dipoleNames) == 0:
            print('station', self.StationName, 'has no antennas')
            return

        self.index_adjusts = np.arange(len(
            self.antenna_to_file))  ##used to compensate for polarization flips
        ### when given an antnna index to open data, use this index instead to open the correct data location

        #### get sample numbers and offsets and lengths and other related stuff ####
        self.SampleNumbers = []
        self.DataLengths = []
        for TBB_file, file_ant_i in self.antenna_to_file:
            self.SampleNumbers.append(TBB_file.SampleNumbers[file_ant_i])
            self.DataLengths.append(TBB_file.DataLengths[file_ant_i])

        self.SampleNumbers = np.array(self.SampleNumbers, dtype=int)
        self.DataLengths = np.array(self.DataLengths, dtype=int)

        self.nominal_sample_number = np.max(self.SampleNumbers)
        self.sample_offsets = self.nominal_sample_number - self.SampleNumbers
        self.nominal_DataLengths = self.DataLengths - self.sample_offsets

        self.even_ant_pol_flips = None
        if polarization_flips is not None:
            self.set_polarization_flips(polarization_flips)
        self.additional_ant_delays = additional_ant_delays
Beispiel #6
0
    def prep_for_fitting(self, polarization, info):
        self.polarization = polarization  ## 0 is even, 1 is odd, 2 is both

        self.pulse_times = np.empty(len(info.sorted_antenna_names),
                                    dtype=np.double)
        self.pulse_times[:] = np.nan
        self.waveforms = [None] * len(self.pulse_times)
        self.waveform_startTimes = [None] * len(self.pulse_times)

        #### first add times from referance_station
        for sname, ant_range in info.station_to_antenna_index_dict.items():
            if (sname in self.stations_to_exclude) or (sname
                                                       not in self.data_file):
                continue

            station_group = self.data_file[sname]

            for ant_i in range(ant_range[0], ant_range[1]):
                ant_name = info.sorted_antenna_names[ant_i]

                if (not antName_is_even(ant_name)) or (ant_name
                                                       not in station_group):
                    #                if (ant_name in self.antennas_to_exclude) or (ant_name in bad_antennas) :
                    continue

                even_ant_name = ant_name
                odd_ant_name = even_antName_to_odd(ant_name)

                ant_data = station_group[ant_name]
                start_index = ant_data.attrs['starting_index']

                ## even
                even_is_bad = (even_ant_name in self.antennas_to_exclude) or (
                    even_ant_name in info.bad_ants)
                if (not even_is_bad) and (polarization == 0
                                          or polarization == 2):
                    peak_time = ant_data.attrs['PolE_peakTime']
                    waveform = ant_data[0, :]
                    HE_waveform = ant_data[1, :]
                    amp = np.max(HE_waveform)

                    if (not np.isfinite(peak_time)) or (
                            amp < info.min_ant_amplitude):
                        peak_time = np.nan

                    self.pulse_times[ant_i] = peak_time
                    self.waveforms[ant_i] = waveform
                    self.waveform_startTimes[
                        ant_i] = start_index * 5.0E-9 - ant_data.attrs[
                            'PolE_timeOffset_CS']
                    self._num_data += 1

                ## odd
                odd_is_bad = (odd_ant_name in self.antennas_to_exclude) or (
                    odd_ant_name in info.bad_ants)
                if (not odd_is_bad) and (polarization == 1
                                         or polarization == 2):
                    peak_time = ant_data.attrs['PolO_peakTime']
                    waveform = ant_data[2, :]
                    HE_waveform = ant_data[3, :]
                    amp = np.max(HE_waveform)

                    if (not np.isfinite(peak_time)) or (
                            amp < info.min_ant_amplitude):
                        peak_time = np.nan

                    self.pulse_times[ant_i + 1] = peak_time
                    self.waveforms[ant_i + 1] = waveform
                    self.waveform_startTimes[
                        ant_i + 1] = start_index * 5.0E-9 - ant_data.attrs[
                            'PolO_timeOffset_CS']
                    self._num_data += 1
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