Ejemplo n.º 1
0
    def __init__(self, path=None, on_start=None):

        if path: self.events = EventIterator(path)
        self._current_vertex = None
        self._all_vertices = collections.deque([])
        self._on_start = on_start

        # Register the extra event handlers.
        self.accept("raw-e", self.show_next_event)
        self.accept("raw-q", self.show_previous_event)
        self.accept("raw-t", self.toggle_all_decays)
Ejemplo n.º 2
0
def process(path, latitude, longitude, rate, opt='sel'):
    """Process a set of event files"""
    tstart = time.time()
    generated, data = 0, []
    nf = 0
    for name in os.listdir(path):
        nf += 1
        if not name.endswith("json"):
            continue
        filename = os.path.join(path, name)
        t0 = time.time()
        #print "o Processing", filename
        for event in EventIterator(filename):
            generated += add_triggers(event, latitude, longitude, rate, opt)
        #print "  --> Done in {:.1f} s".format(time.time() - t0)
        if float(nf) / 100 == np.floor(nf / 100):
            print 'Nb of json files processed:', nf
#if nf==1000:

#  break

    if generated > 0:
        d = antennas_density(latitude, longitude)
        rate /= (generated * d)
    latitude = 0.5 * (latitude[1:] + latitude[:-1])
    longitude = 0.5 * (longitude[1:] + longitude[:-1])

    while path.endswith("/"):
        path = path[:-1]
    path += ".triggers.p"
    #ratet = np.transpose(rate)
    with open(path, "wb+") as f:
        pickle.dump((generated, latitude, longitude, rate), f, -1)
    print "o Triggers dumped to", path
    print "  --> All done in {:.1f} s".format(time.time() - tstart)
def inputfromjson(path,json_file):
#===========================================================================================================
    # shower you are interested in
  #showerID = str(path.split('/')[-1])    
  #if not showerID:
  #      showerID = str(path.split('/')[-2])
  for event in EventIterator(json_file):
    #if showerID !=  event["tag"]:
    #    print "Wrong tag!... Was expecting",showerID,", got",event["tag"]
    #	return

    ### DECAY
    decay_pos=event["tau_at_decay"][2]
    injection_height=decay_pos[2]
    decay_pos=decay_pos+np.array([0.,0.,EARTH_RADIUS]) # corrected for earth radius
    #print "decay position after correction: ", decay_pos
    decay_altitude=event["tau_at_decay"][3]
    #print "decay decay_altitude: ", decay_altitude

    ### ANGLES
    v=event["tau_at_decay"][3]# shower direction, assuming decay products strongly forward beamed
    #zenith_sim = np.degrees(np.arccos(np.dot(v, decay_pos) / np.linalg.norm(decay_pos))) # zenith in GRAND conv.
    zenith_sim = np.degrees(np.arccos(v[2])) # zenith in GRAND conv.
    #print "theta:", zenith_sim
    
    #orthogonal projection of v onto flat plane to get the azimuth
    x=np.array([1.,0.,0.]) #NS
    y=np.array([0.,1.,0.]) #EW
    proj_v= np.dot(v,x)*x + np.dot(v,y)*y
    azimuth_sim = np.degrees(np.arccos(np.dot(proj_v, x))) # azimuth in GRAND conv., rt NORTH
    if proj_v[1]<0.: # y component of projection negativ, means azimuth >180deg
        azimuth_sim = 360.-azimuth_sim
    #print "azimuth: ", azimuth_sim

    ### ENERGY
    ep_array=np.zeros(len(event["decay"])-1)
    for i in range(1, len(event["decay"])): #len(event["decay"])-1 # gives you the number of decay products in event
        if float(event["decay"][i][0]) in particle_list: # just for valid particles
            pp=event["decay"][i][1] # momentum vector, second decay product: event["decay"][2][1]
            ep_array[i-1]=np.sqrt(pp[0]**2+pp[1]**2+pp[2]**2)# in GeV
    #    print "particle ", str(i), "PID:",event["decay"][i][0]," energy in EeV: ", ep_array[i-1]*1e-9 #np.sqrt(pp[0]**2+pp[1]**2+pp[2]**2)* 1.e-9
    energy= np.sum(ep_array)* 1.e-9 # GeV in EeV
    #print "energy in EeV: ", energy

    ### PID primary
    #part_dic={'221.0':'eta','211.0': 'pi+', '-211.0': 'pi-','111.0': 'pi0', '22.0':'gamma', '13.0':'muon', '11.0': 'electron', '15.0':'tau', '16.0':'nu(t)', '321.0': 'K+', '-321.0': 'K-','130.0':'K0L', '310.0':'K0S','-323.0':'K*+'}
    particle=int(np.argmax(ep_array) +1) # not forget about the inital neutrino and array start with 0
    PID= float(event["decay"][int(np.argmax(ep_array) +1)][0])
    el_list=[22.0, 11.0, -11.0, 111.0] #'22.0':'gamma', '11.0': 'electron', '-11':positron,  '111.0': 'pi0'
    if PID in el_list:
        primary="electron"
    else: # pion-like
        primary="pion"

    return zenith_sim,azimuth_sim,energy,injection_height,primary
Ejemplo n.º 4
0
def load(path):
    """Load and parse a set of decay files
    """
    theta, phi, altitude = [], [], []
    position, energy, weight = [], [], []
    generated = 0
    for name in os.listdir(path):
        if not name.startswith("events"):
            continue
        filename = os.path.join(path, name)
        for event in EventIterator(filename):
            e, r, u = event["tau_at_decay"]
            t, p = topo.local_to_angular(r, u)
            _, _, a = topo.local_to_lla(r)
            K = [
                numpy.linalg.norm(v[3]) > 6371E+03
                for v in event["primaries"][0]
            ]
            J = [not k for k in K]
            K, J = map(lambda X: numpy.nonzero(X)[0], (K, J))
            wp = numpy.array([v[0] for v in event["primaries"][0]])
            norm = event["statistics"][0] * PHI0 * year / event["primaries"][1]
            wE = sum(wp[J]) * norm
            wA = sum(wp[K]) * norm
            generated += event["statistics"][1]
            if wE + wA == 0:
                continue
            theta.append(t)
            phi.append(p)
            altitude.append(a * 1E-03)
            position.append(r)
            energy.append(e)
            weight.append((wE, wA))
    position = numpy.array(position) * 1E-03
    weight = numpy.array(weight)

    def get_stats(w, n):
        mu = sum(w) / n
        sigma = sum(w**2) / n
        sigma = numpy.sqrt((sigma - mu**2) / n)
        return mu, sigma

    mE, sE = get_stats(weight[:, 0], generated)
    mA, sA = get_stats(weight[:, 1], generated)
    print "Earth skimming = {:.3f} +-{:.3f} a^-1".format(mE, sE)
    print "Atmospheric    = {:.3f} +-{:.3f} a^-1".format(mA, sA)
    print "Events ratio   = {:.2f}".format(
        sum(weight[:, 0] > 0.) / float(len(weight)))

    if mE <= 0.:
        # Bring the atmospheric events to the front
        weight[:, [0, 1]] = weight[:, [1, 0]]

    return theta, phi, altitude, position, energy, weight, generated
Ejemplo n.º 5
0
def load(path):
    """Load samples
    """
    weight, weight2, generated = 0., 0., 0
    for event in EventIterator(path):
        w, n = event["statistics"]
        weight += w
        weight2 += w**2
        generated += n
    weight /= generated
    weight2 /= generated
    sigma = ((weight2 - weight**2) / generated)**0.5
    return weight, sigma
Ejemplo n.º 6
0
def process(path):
    """Summarise a set of event files"""
    global origin, topo, flat 
    origin, topo = None, None
    flat = False # Use topography of flat ground (with Earth curvature)
    print "*** Warning: flat=",flat
    for name in os.listdir(path):
        if not name.endswith("json"):
	    continue
	filename = os.path.join(path, name)
        print "o Processing", filename
	t0 = time.time()
        for event in EventIterator(filename):
            fresnel(event)
        print "  --> Done in {:.1f} s".format(time.time() - t0)
Ejemplo n.º 7
0
def process(path, summarise, opt='sel'):
    """Summarise a set of event files"""
    tstart = time.time()
    global origin, topo
    origin, topo = None, None
    generated, data = 0, []
    nf = 0
    novolt = 0
    for name in os.listdir(path):
        if not name.endswith("json"):
            continue
        filename = os.path.join(path, name)
        t0 = time.time()
        #print "o Processing", filename
        for event in EventIterator(filename):
            n, d = summarise(event, opt)
            generated += n
            if d is not None:
                data.append(d)
        else:
            novolt += 1
            #print 'novolt=',novolt
        if float(nf) / 100 == np.floor(nf / 100):
            print 'Nb of json files processed:', nf

        nf += 1
        #if nf==1000:
    #    break

    if len(data) == 0:
        raise RuntimeError("No event found")

    while path.endswith("/"):
        path = path[:-1]
    if opt == 'sel':
        path += "_sel.p"
    else:
        path += ".p"

    with open(path, "wb+") as f:
        pickle.dump((generated, data), f, -1)

    print "o Events dumped to", path
    print "  --> All done in {:.1f} s".format(time.time() - tstart)
Ejemplo n.º 8
0
def process(path, summarise, opt='sel'):
    """Summarise a set of event files"""
    tstart = time.time()
    nf = 0
    generated, data = 0, []
    for name in os.listdir(path):
        if not name.endswith("json"):
            continue
        nf += 1
        filename = os.path.join(path, name)
        t0 = time.time()
        #print "o Processing", filename
        for event in EventIterator(filename):
            n, d = summarise_primaries(event, opt)
            generated += n
            if d is not None:
                data += d
#print "  --> Done in {:.1f} s".format(time.time() - t0)
        if float(nf) / 100 == np.floor(nf / 100):
            print 'Nb of json files processed:', nf

#if nf==19900:
#  print "#### Warning! Stopping at",nf
#  break

    if len(data) == 0:
        raise RuntimeError("No event found")

    while path.endswith("/"):
        path = path[:-1]
    if opt == 'sel':
        path += ".primaries_sel.p"
    else:
        path += ".primaries.p"

    with open(path, "wb+") as f:
        pickle.dump((generated, data), f, -1)
    print "o Events dumped to", path
    print "  --> All done in {:.1f} s".format(time.time() - tstart)
Ejemplo n.º 9
0
def compute(opt_input,
            path,
            effective,
            zenith_sim,
            azimuth_sim,
            energy,
            injection_height,
            primary,
            json_file=None):
    #===========================================================================================================

    if opt_input == 'json':
        # shower you are interested in
        showerID = str(path.split('/')[-1])
        if not showerID:
            showerID = str(path.split('/')[-2])
        # Find that shower in the json file
        event = [
            evt for evt in EventIterator(json_file) if evt["tag"] == showerID
        ][0]
        ### json file containing additional data for analysis
        filename = str(showerID) + ".voltage.json"
        path2 = join(path_json, filename)
        print path2
        log_event = EventLogger(path=path2)

    voltage = []
    time_peaks = []
    print "Zenith, azimuth=", zenith_sim, azimuth_sim

    ##########################################################################################
    ###Handing over one antenna or a whole array
    if opt_input == 'txt':
        if len(sys.argv) >= 7:  # just one specif antenna handed over
            start = int(sys.argv[6])  # antenna ID
            end = start + 1
        #    print "single antenna with ID: ", str(start)," handed over"
        if len(sys.argv) < 7:  # grep all antennas from the antenna file
            positions = np.genfromtxt(path + '/antpos.dat')
            start = 0
            end = len(positions)
        #    print "Array with ", end, " antennas handed over"
    elif opt_input == 'json':
        if len(sys.argv) >= 7:  # just one specif antenna handed over
            start = int(sys.argv[6])  # antenna ID
            end = start + 1
        #    print "single antenna with ID: ", str(start)," handed over"
        if len(sys.argv) < 7:  # grep all antennas from the antenna file
            positions = np.array(event["antennas"], dtype=float)
            decay_pos = event["tau_at_decay"][1]
            positions = positions - [decay_pos[0], decay_pos[1], 0.]
            #positions=np.genfromtxt(path+'/antpos.dat')
            start = 0
            end = len(positions)
            #print "Array with ", end, " antennas handed over"
    elif opt_input == 'manual':
        if len(sys.argv) >= 11:  # just one specif antenna handed over
            start = int(sys.argv[10])  # antenna ID
            end = start + 1
        #    print "single antenna with ID: ", str(start)," handed over"
        if len(sys.argv) < 11:  # grep all antennas from the antenna file
            positions = np.genfromtxt(path + '/antpos.dat')
            start = 0
            end = len(positions)
        #    print "Array with ", end, " antennas handed over"

    if effective == 1:  # effective zenith caclculation needs Xmax position as input
        #    print "effective zenith calculated - Xmax position approximated ..."
        # Then compute Xmax
        caz = np.cos(np.deg2rad(azimuth_sim))
        saz = np.sin(np.deg2rad(azimuth_sim))
        czen = np.cos(np.deg2rad(zenith_sim))
        szen = np.sin(np.deg2rad(zenith_sim))

        Xmax_primary = modules._getXmax(
            primary, energy, np.deg2rad(zenith_sim)
        )  # approximation based on values from plots for gamma (=e) and protons (=pi) # g/cm2
        Xmax_height, Xmax_distance = modules._dist_decay_Xmax(
            np.deg2rad(zenith_sim), injection_height,
            Xmax_primary)  # d_prime: distance from decay point to Xmax
        Xmax = Xmax_distance * np.array([caz * szen, saz * szen, czen
                                         ]) + np.array(
                                             [0, 0, injection_height])
    #   print 'Xmax=',Xmax_primary,' Xmax height=',Xmax_height,' Xmax distance =',Xmax_distance,'Xmax position= ',Xmax
    #   print 'Now computing Xmax position from injection height=',injection_height,'m and (zen,azim) values.'

    ###### loop  over l --- LOOP OVER ANTENNA ARRAY
    for l in range(start, end):
        efieldtxt = path + '/a' + str(l) + '.trace'
        #    print 'Wave direction: zenith = ', zenith_sim, ' deg, azimuth = ', azimuth_sim, 'deg. (GRAND conventions), mountain slope: ', alpha_sim, 'deg.'
        #    print 'Efield file: ', efieldtxt

        # Model the input signal.
        try:
            time1_sim, Ex_sim, Ey_sim, Ez_sim = np.loadtxt(efieldtxt,
                                                           delimiter=' ',
                                                           usecols=(0, 1, 2,
                                                                    3),
                                                           unpack=True)
        except IOError:
            continue  #jlzhang if the file and the path is not exist, if will go to next antenna, so I think it should be break;

        # NOTE: adapt to your time from whatever to s
        time1_sim = time1_sim * 1e-9  # time has to be handed in s

        #print 'Now computing antenna response...'
        if effective == 1:

            # Compute effective zenith
            # First get antenna position

            #print 'Reading antenna position from parameter input.'
            if (opt_input == 'json' or opt_input == 'txt') and (len(sys.argv)
                                                                == 12):
                x_sim = float(sys.argv[7])
                y_sim = float(sys.argv[8])
                z_sim = float(sys.argv[9])

                # include a mountain slope - correction of zenith angle
                alpha_sim = float(sys.argv[10])
                beta_sim = float(sys.argv[11])

            elif (opt_input == 'manual') and (len(sys.argv) == 16):
                x_sim = float(sys.argv[11])
                y_sim = float(sys.argv[12])
                z_sim = float(sys.argv[13])

                # include a mountain slope - correction of zenith angle
                alpha_sim = float(sys.argv[14])
                beta_sim = float(sys.argv[15])
            else:
                try:
                    if opt_input == 'json':
                        x_sim, y_sim, z_sim = positions[
                            l]  #,alpha_sim,beta_sim
                        alpha_sim = 0.
                        beta_sim = 0.
                    else:
                        #print 'Trying to read antenna position from antpos.dat file...'
                        numberline = int(l) + 1
                        line = linecache.getline(path + '/antpos.dat',
                                                 numberline)
                        #[x_sim, y_sim, z_sim] = map(float, line.split())
                        [x_sim, y_sim, z_sim, alpha_sim,
                         beta_sim] = map(float, line.split())
                    #print 'Read antenna position from antpos.dat file... Antenna',l,' at position [', x_sim, y_sim, z_sim,'].'
                except:
                    print 'No antenna position file found, please put antpos.dat in', path, 'or enter check antenna informations in json file or enter antenna positions as arguments.'
                    sys.exit()

        Xant = [x_sim, y_sim, z_sim]
        # Hack OMH 24/01
        alpha_sim = 10
        beta_sim = 0
        Xant = [400000, 0, 0]
        ush = Xmax - Xant
        ush = ush / np.linalg.norm(
            ush)  # Unitary vector pointing to Xmax from antenna pos
        voltage_NS, timeNS = get_voltage(time1=time1_sim,
                                         Ex=Ex_sim,
                                         Ey=Ey_sim,
                                         Ez=Ez_sim,
                                         ush=ush,
                                         alpha=alpha_sim,
                                         beta=beta_sim,
                                         typ="X")
        voltage_EW, timeEW = get_voltage(time1=time1_sim,
                                         Ex=Ex_sim,
                                         Ey=Ey_sim,
                                         Ez=Ez_sim,
                                         ush=ush,
                                         alpha=alpha_sim,
                                         beta=beta_sim,
                                         typ="Y")
        voltage_vert, timevert = get_voltage(time1=time1_sim,
                                             Ex=Ex_sim,
                                             Ey=Ey_sim,
                                             Ez=Ez_sim,
                                             ush=ush,
                                             alpha=alpha_sim,
                                             beta=beta_sim,
                                             typ="Z")

        #pl.savetxt(path+'out_'+str(l)+'.txt', (timeEW, voltage_EW, voltage_NS), newline='\r\n')#, voltage_NS)) # is not working correctly
        if np.size(timeEW) > 0:  # Dat was computed
            f = file(pathout + '/out_' + str(l) + '.txt', "w")
            #print "OUTFILE : ", pathout+'/out_'+str(l)+'.txt'
            for i in np.arange(len(timeEW)):
                print >> f, "%1.5e	%1.2e	%1.2e	%1.2e" % (
                    timeNS[i], voltage_NS[i], voltage_EW[i], voltage_vert[i]
                )  # same number of digits as input
            f.close()

        ###plots
        DISPLAY = 1
        if DISPLAY == 1:
            import pylab as pl
            import matplotlib.pyplot as plt
            plt.figure(1, facecolor='w', edgecolor='k')
            plt.subplot(211)
            plt.plot(time1_sim * 1e9, Ey_sim, label="Ey = EW")
            plt.plot(time1_sim * 1e9, Ex_sim, label="Ex = NS")
            plt.plot(time1_sim * 1e9, Ez_sim, label="Ez = UP")
            plt.xlabel('Time (nsec)')
            plt.ylabel('Electric field (muV/m)')
            plt.legend(loc='best')
            plt.subplot(212)
            plt.plot(timeEW * 1e9, voltage_EW, label="EW")
            plt.plot(timeEW * 1e9, voltage_NS, label="NS")
            plt.plot(timeEW * 1e9, voltage_vert, label="Vertical")
            plt.xlabel('Time (nsec)')
            plt.ylabel('Voltage (muV)')
            plt.legend(loc='best')

            plt.show()

##################################################################
##################################################################
        if opt_input == 'json':
            #### additional output needed for later study, added in the json file
            # p2p voltage:  antenna ID, p2p EW, NS, UP, EW+NS
            voltage_com = np.copy(voltage_EW)
            for i in range(0, len(voltage_EW)):
                voltage_com[i] += voltage_NS[i]
            v_list = (
                str(l), max(voltage_EW) - min(voltage_EW), max(voltage_NS) -
                min(voltage_NS), max(voltage_vert) - min(voltage_vert),
                max(voltage_com) - min(voltage_com)
            )  #jlzhang zen>90, so there is no voltage, the max() will be empty
            voltage.append(v_list)

            # time of peaks and value: t_EW_max, v_EW_max, t_EW_min, v_EW_min,.... EW, NS, vert, EW+NS
            import operator
            EW_ind_max, value = max(enumerate(voltage_EW),
                                    key=operator.itemgetter(1))
            EW_ind_min, value = min(enumerate(voltage_EW),
                                    key=operator.itemgetter(1))

            NS_ind_max, value = max(enumerate(voltage_NS),
                                    key=operator.itemgetter(1))
            NS_ind_min, value = min(enumerate(voltage_NS),
                                    key=operator.itemgetter(1))

            vert_ind_max, value = max(enumerate(voltage_vert),
                                      key=operator.itemgetter(1))
            vert_ind_min, value = min(enumerate(voltage_vert),
                                      key=operator.itemgetter(1))

            com_ind_max, value = max(enumerate(voltage_com),
                                     key=operator.itemgetter(1))
            com_ind_min, value = min(enumerate(voltage_com),
                                     key=operator.itemgetter(1))

            time_peaks.append(
                (round(timeEW[EW_ind_max], 11), voltage_EW[EW_ind_max],
                 round(timeEW[EW_ind_min], 11), voltage_EW[EW_ind_min],
                 round(timeNS[NS_ind_max], 11), voltage_NS[NS_ind_max],
                 round(timeNS[NS_ind_min], 11), voltage_NS[NS_ind_min],
                 round(timevert[vert_ind_max], 11), voltage_vert[vert_ind_max],
                 round(timevert[vert_ind_min], 11), voltage_vert[vert_ind_min],
                 round(timeEW[com_ind_max], 11), voltage_com[com_ind_max],
                 round(timeEW[com_ind_min], 11), voltage_com[com_ind_min]))

############### end of loop over antennas
    if opt_input == 'json':
        if len(voltage) == 0:
            print "- effective zenith not fulfilled - NO VOLTAGE COMPUTED"
            log_event(**event)
        else:
            # add the additional informations to the shower event
            event['voltage'] = voltage  # muV
            event['time_peaks'] = time_peaks  # s, muV
            log_event(**event)
Ejemplo n.º 10
0
def compute(opt_input,path, effective,zenith_sim, azimuth_sim, energy, injection_height, primary,json_file=None):
#===========================================================================================================

    if opt_input=='json':
        # shower you are interested in
        showerID = str(path.split('/')[-1])
        if not showerID:
            showerID = str(path.split('/')[-2])
        # Find that shower in the json file
        event = [evt for evt in EventIterator(json_file) if evt["tag"]==showerID][0]
        log_event = EventLogger(path=json_file)

    voltage=[]
    time_peaks=[]

    ##########################################################################################
    ###Handing over one antenna or a whole array
    if opt_input=='txt':
        if len(sys.argv)>=6: # just one specif antenna handed over
            start=int(sys.argv[5]) # antenna ID
            end=start+1
        #    print "single antenna with ID: ", str(start)," handed over"
        if  len(sys.argv)<6: # grep all antennas from the antenna file
            positions=np.genfromtxt(path+'/antpos.dat')
            start=0
            end=len(positions)
        #    print "Array with ", end, " antennas handed over"
    elif opt_input=='json':
        if len(sys.argv)>=6: # just one specif antenna handed over
            start=int(sys.argv[5]) # antenna ID
            end=start+1
        #    print "single antenna with ID: ", str(start)," handed over"
        if  len(sys.argv)<6: # grep all antennas from the antenna file
            positions=np.array(event["antennas"],dtype=float)
            decay_pos=event["tau_at_decay"][1]
            positions = positions - [decay_pos[0],decay_pos[1],0.]
            #positions=np.genfromtxt(path+'/antpos.dat')
            start=0
            end=len(positions)
            #print "Array with ", end, " antennas handed over"
    elif opt_input=='manual':
        if len(sys.argv)>=10: # just one specif antenna handed over
            start=int(sys.argv[9]) # antenna ID
            end=start+1
        #    print "single antenna with ID: ", str(start)," handed over"
        if  len(sys.argv)<10: # grep all antennas from the antenna file
            positions=np.genfromtxt(path+'/antpos.dat')
            start=0
            end=len(positions)
        #    print "Array with ", end, " antennas handed over"

    if effective==1: # effective zenith caclculation needs Xmax position as input
    #    print "effective zenith calculated - Xmax position approximated ..."
        # Then compute Xmax
        caz = np.cos(np.deg2rad(azimuth_sim))
        saz = np.sin(np.deg2rad(azimuth_sim))
        czen = np.cos(np.deg2rad(zenith_sim))
        szen = np.sin(np.deg2rad(zenith_sim))

        Xmax_primary = modules._getXmax(primary, energy, np.deg2rad(zenith_sim)) # approximation based on values from plots for gamma (=e) and protons (=pi) # g/cm2
        Xmax_height, Xmax_distance = modules._dist_decay_Xmax(np.deg2rad(zenith_sim), injection_height, Xmax_primary) # d_prime: distance from decay point to Xmax
        Xmax = Xmax_distance*np.array([caz*szen, saz*szen, czen])+np.array([0,0,injection_height])
    #   print 'Xmax=',Xmax_primary,' Xmax height=',Xmax_height,' Xmax distance =',Xmax_distance,'Xmax position= ',Xmax
    #   print 'Now computing Xmax position from injection height=',injection_height,'m and (zen,azim) values.'

    ###### loop  over l --- LOOP OVER ANTENNA ARRAY
    for l in range(start,end):
        efieldtxt=path+'/a'+str(l)+'.trace'

    #    print 'Wave direction: zenith = ', zenith_sim, ' deg, azimuth = ', azimuth_sim, 'deg. (GRAND conventions), mountain slope: ', alpha_sim, 'deg.'
    #    print 'Efield file: ', efieldtxt

        # Model the input signal.
        try:
            time1_sim, Ex_sim, Ey_sim,Ez_sim = np.loadtxt(efieldtxt,delimiter=' ',usecols=(0,1,2,3),unpack=True)
        except IOError:
            continue

        # NOTE: adapt to your time from whatever to s
        time1_sim= time1_sim*1e-9 # time has to be handed in s

        #print 'Now computing antenna response...'
        if effective==1:

            # Compute effective zenith
            # First get antenna position

            #print 'Reading antenna position from parameter input.'
            if (opt_input=='json' or opt_input=='txt') and (len(sys.argv)==11) :
                    x_sim = float(sys.argv[6])
                    y_sim = float(sys.argv[7])
                    z_sim = float(sys.argv[8])

                    # include a mountain slope - correction of zenith angle
                    alpha_sim=float(sys.argv[9])
                    beta_sim=float(sys.argv[10])

            elif (opt_input=='manual') and (len(sys.argv)==15) :
                    x_sim = float(sys.argv[10])
                    y_sim = float(sys.argv[11])
                    z_sim = float(sys.argv[12])

                    # include a mountain slope - correction of zenith angle
                    alpha_sim=float(sys.argv[13])
                    beta_sim=float(sys.argv[14])
            else :
                try :
                    if opt_input=='json':
                        x_sim,y_sim,z_sim = positions[l] #,alpha_sim,beta_sim
                        alpha_sim = 0.
                        beta_sim = 0.
                    else:
                        #print 'Trying to read antenna position from antpos.dat file...'
                        numberline = int(l) + 1
                        line = linecache.getline(path+'/antpos.dat', numberline)
                        #[x_sim, y_sim, z_sim] = map(float, line.split())
                        [x_sim, y_sim, z_sim, alpha_sim, beta_sim] = map(float, line.split())
                    #print 'Read antenna position from antpos.dat file... Antenna',l,' at position [', x_sim, y_sim, z_sim,'].'
                except :
                    print 'No antenna position file found, please put antpos.dat in', path, 'or enter check antenna informations in json file or enter antenna positions as arguments.'
                    sys.exit()

            # Finally compute effective zenith
            #alpha_sim = 0.
 	    # Hack OMH 24/01
	    alpha_sim=10
	    beta_sim=0
 	    x_sim = 400000
 	    y_sim = 0
 	    z_sim = 0

	    print "input zen, azim, alpha,beta : ", zenith_sim, azimuth_sim, alpha_sim,beta_sim
            #zenith_eff = effective_zenith(zenith_sim, azimuth_sim, alpha_sim, x_sim, y_sim, z_sim, Xmax[0], Xmax[1], Xmax[2])
            #print 'zenith_eff = ',zenith_eff
            zen_inant,azim_inant = effective_angles(alpha_sim, beta_sim, x_sim, y_sim, z_sim, Xmax[0], Xmax[1], Xmax[2])
            print "effective zenith and azimuth in GRAND convention: ", zen_inant,azim_inant,' deg'

        else: # in case effective zenith not wanted, one still has to account for mountain slope
            # zenith: correct for mountain slope
            #zenith_eff= 180.-(zenith_sim+alpha_sim) # in antenna convention
            #zenith_eff= 180.- zenith_eff # back to GRAND conventions
            print('Not supported')

        #if zenith_eff < 90 :
        if zen_inant < 90 : #>90 if in NEC convention
            pass
            print ' --- Wave coming from ground (GRAND zenith smaller than 90deg), antenna response not computed for that angle. Abort --- '
            #exit()
        else:
            # Compute the output voltage for EW component
        #    print '*** Computing EW voltage...'
            #voltage_EW, timeEW  = get_voltage( time1=time1_sim,Ex=Ex_sim, Ey=Ey_sim, Ez=Ez_sim, zenith=zenith_eff, azimuth=azimuth_sim, EW=1)
            voltage_EW, timeEW  = get_voltage( time1=time1_sim,Ex=Ex_sim, Ey=Ey_sim, Ez=Ez_sim, zenith=zen_inant, azimuth=azim_inant, EW=1)

            # Compute the output voltage for NS component -- TODO add here the correct antenna file at some point
        #    print '*** Computing SN voltage...'
            #voltage_NS, timeNS = get_voltage( time1=time1_sim,Ex=Ex_sim, Ey=Ey_sim, Ez=Ez_sim, zenith=zenith_eff, azimuth=azimuth_sim, EW=0)
            voltage_NS, timeNS = get_voltage( time1=time1_sim,Ex=Ex_sim, Ey=Ey_sim, Ez=Ez_sim, zenith=zen_inant, azimuth=azim_inant, EW=0)

            # Compute the output voltage for vertical component -- TODO add here the correct antenna file at some point, at the moment equivalent to EW arm
        #    print '*** Computing Vertical voltage...'
            #voltage_vert, timevert = get_voltage( time1=time1_sim,Ex=Ex_sim, Ey=Ey_sim, Ez=Ez_sim, zenith=zenith_eff, azimuth=azimuth_sim, EW=2)
            voltage_vert, timevert = get_voltage( time1=time1_sim,Ex=Ex_sim, Ey=Ey_sim, Ez=Ez_sim, zenith=zen_inant, azimuth=azim_inant, EW=2)

            #pl.savetxt(path+'out_'+str(l)+'.txt', (timeEW, voltage_EW, voltage_NS), newline='\r\n')#, voltage_NS)) # is not working correctly
            f = file(path+'/out_'+str(l)+'.txt',"w")
            print "OUTFILE : ", path+'/out_'+str(l)+'.txt'
            for i in np.arange(len(timeEW)):
                print >>f,"%1.5e	%1.2e	%1.2e	%1.2e" % (timeEW[i], voltage_EW[i], voltage_NS[i], voltage_vert[i] ) # same number of digits as input
            f.close()

            ###plots
            DISPLAY=1
            if DISPLAY==1:
                import pylab as pl
                import matplotlib.pyplot as plt
                plt.figure(1,  facecolor='w', edgecolor='k')
                plt.subplot(211)
                plt.plot(time1_sim*1e9,Ey_sim, label="Ey = EW")
                plt.plot(time1_sim*1e9,Ex_sim, label="Ex = NS")
                plt.plot(time1_sim*1e9,Ez_sim, label="Ez = UP")
                plt.xlabel('Time (nsec)')
                plt.ylabel('Electric field (muV/m)')
                plt.legend(loc='best')
                plt.subplot(212)
                plt.plot(timeEW*1e9,voltage_EW, label="EW")
                plt.plot(timeNS*1e9,voltage_NS, label="NS")
                plt.plot(timeNS*1e9,voltage_vert, label="Vertical")
                plt.xlabel('Time (nsec)')
                plt.ylabel('Voltage (muV)')
                plt.legend(loc='best')

                plt.show()

##################################################################
##################################################################
            if opt_input=='json':
                #### additional output needed for later study, added in the json file
                # p2p voltage:  antenna ID, p2p EW, NS, UP, EW+NS
                voltage_com=np.copy(voltage_EW)
                for i in range (0, len(voltage_EW)):
                                    voltage_com[i]+=voltage_NS[i]
                v_list =( str(l),  max(voltage_EW) - min(voltage_EW), max(voltage_NS) - min(voltage_NS), max(voltage_vert) - min(voltage_vert), max(voltage_com) - min(voltage_com)   )
                voltage.append( v_list )

                # time of peaks and value: t_EW_max, v_EW_max, t_EW_min, v_EW_min,.... EW, NS, vert, EW+NS
                import operator
                EW_ind_max, value = max(enumerate(voltage_EW), key=operator.itemgetter(1))
                EW_ind_min, value = min(enumerate(voltage_EW), key=operator.itemgetter(1))

                NS_ind_max, value = max(enumerate(voltage_NS), key=operator.itemgetter(1))
                NS_ind_min, value = min(enumerate(voltage_NS), key=operator.itemgetter(1))

                vert_ind_max, value = max(enumerate(voltage_vert), key=operator.itemgetter(1))
                vert_ind_min, value = min(enumerate(voltage_vert), key=operator.itemgetter(1))

                com_ind_max, value = max(enumerate(voltage_com), key=operator.itemgetter(1))
                com_ind_min, value = min(enumerate(voltage_com), key=operator.itemgetter(1))

                time_peaks.append( (round(timeEW[EW_ind_max],11),  voltage_EW[EW_ind_max], round(timeEW[EW_ind_min],11), voltage_EW[EW_ind_min],
                                    round(timeNS[NS_ind_max],11), voltage_NS[NS_ind_max], round(timeNS[NS_ind_min],11), voltage_NS[NS_ind_min],
                                    round(timevert[vert_ind_max],11), voltage_vert[vert_ind_max], round(timevert[vert_ind_min],11), voltage_vert[vert_ind_min],
                                    round(timeEW[com_ind_max],11), voltage_com[com_ind_max], round(timeEW[com_ind_min],11), voltage_com[com_ind_min] )  )

############### end of loop over antennas
    if opt_input=='json':
        if len(voltage)==0:
            print "- effective zenith not fulfilled - NO VOLTAGE COMPUTED"
            log_event(**event)
        else:
            # add the additional informations to the shower event
            event['voltage'] = voltage # muV
            event['time_peaks'] = time_peaks # s, muV
            log_event(**event)
Ejemplo n.º 11
0
#  Author: Valentin NIESS ([email protected])
#
#  A basic event display for RETRO, based on matplotlib.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>

import sys

sys.path.append("lib/python")
sys.path.append("../../lib/python")

from retro.event import EventIterator
from display import Display

display = Display()

if __name__ == "__main__":
    for event in EventIterator(sys.argv[1]):
        display(event)
Ejemplo n.º 12
0
if __name__ == "__main__":
    i = 0
    global origin, topo
    origin, topo = None, None
    #xall = np.zeros(shape=(0,0))
    #yall = np.zeros(shape=(0,0))
    path = sys.argv[1]
    f = open('compIniGRAND.txt', 'ab')
    #np.savetxt(f,"#E (GeV), u(GRANDconv), x_decay(GRANDconv)",delimiter=" ",fmt="%s")
    for name in os.listdir(path):
        if not name.endswith("json"):
            continue
        filename = os.path.join(path, name)
        print "o Processing", filename
        for event in EventIterator(filename):
            i += 1
            #xa,ya = ants(event)
            #xall = np.append(xall,xa)
            #yall = np.append(yall,ya)
            #display(event)
            display(42.1, 86.3)
            #fresnel(event)
            #dumpIni(event)

            #if i >1000:
            #  break

    f.close()
    #pl.figure()
    #pl.subplot(211)
Ejemplo n.º 13
0
if __name__ == "__main__":
    wkdir = sys.argv[1]  # path where the simulation file is
    if len(sys.argv) != 2:
        print """\
     This script plot the 2D map of the Efield & voltages amplitude pattern
     Usage:  python plotShowerCaras.py [folder containing the .json file]
     """
        sys.exit(1)

    # First load json infos
    json_files = glob.glob(wkdir + '/*' + '.voltage.json')
    for i in range(len(json_files)):  #
        json_file = json_files[i]
        print "o Processing ", json_file
        for evt in EventIterator(json_file):  # Should only be one
            # Tau decay infos
            tau = evt["tau_at_decay"]
            decay_pos = tau[2]
            decay_pos = np.array(decay_pos)
            tau_dir = tau[5]
            zen = tau_dir[0]
            # Compute the shower energy
            shower_energy = 0.
            for (pid_, momentum) in evt["decay"]:
                aid = abs(pid_)
                if aid in (12, 13, 14, 16):
                    continue
                shower_energy += sum(m**2 for m in momentum)**0.5
            shower_energy = shower_energy / 1e9  # now in EeV
            #if shower_energy<1:
Ejemplo n.º 14
0
    '-323.0': 'K*+'
}

showerID = 0
DISPLAY = 1

work_dir = "./"

json_file = str(sys.argv[1])
#print "json file : ", json_file

j = 0
### MAYBE: this has to be done in a script which is one level higher and calling the example.py
from retro.event import EventIterator
for event in EventIterator(
        json_file
):  #"events-flat.json"): #json files contains a list of events which shall run on one node"
    showerID = showerID + 1
    Dd = 10000. + random.uniform(
        0, 1
    ) * 40000.  #m   #float(sys.argv[3]) #distance from decay point to beginning of radio array [m] at Grd-level
    print "DISTANCE ", Dd

    #### to choose one specific event from a json file or test running on cluster
    j = j + 1
    if j < 10:
        #if event["antennas"][0][2]>0:
        print "\n"
        print "Event ", str(event["tag"]), " started"

        ###DECAY
Ejemplo n.º 15
0
class EventManager(object):
    """Manage GRAND events."""
    def __init__(self, path=None, on_start=None):

        if path: self.events = EventIterator(path)
        self._current_vertex = None
        self._all_vertices = collections.deque([])
        self._on_start = on_start

        # Register the extra event handlers.
        self.accept("raw-e", self.show_next_event)
        self.accept("raw-q", self.show_previous_event)
        self.accept("raw-t", self.toggle_all_decays)

    def do_start(self):
        if self._on_start:
            for process in self._on_start:
                try:
                    method, kwargs = process
                except ValueError:
                    method, kwargs = process, {}
                getattr(self, method)(**kwargs)

    def toggle_all_decays(self):
        """Toggle the display of all the tau decay vertices."""
        t0 = time.time()
        print "o Toggling all tau decays ..."
        if self._all_vertices:
            while True:
                try:
                    node = self._all_vertices.pop()
                except IndexError:
                    break
                node.removeNode()
        else:
            for event in self.events:
                self._all_vertices.append(self._render_decay(event))
            self.events.rewind()
        print "  -> Done in {:.1f}s".format(time.time() - t0)

    def show_next_event(self):
        """Render the next event and relocate to it."""
        try:
            event = self.events.next()
        except StopIteration:
            event = self.events._current
        self._update_event(event)

    def show_previous_event(self):
        """Render the previous event and relocate to it."""
        event = self.events.previous()
        self._update_event(event)

    def _render_decay(self, event):
        """Render a decay."""
        energy, position, direction, _ = event["tau_at_decay"]
        size = numpy.log10(energy)
        vertex = puppy.build.Box(size,
                                 face_color=(1, 1, 0, 1),
                                 line_color=(0, 0, 0, 1)).render()
        vertex.setPos(*position)
        ub = -numpy.array(direction)
        distance = _topography.distance(position, ub)
        if distance is None:
            distance = 1E+04
        edges = (position, position + distance * ub)
        track = puppy.build.Track(edges, line_color=(1, 1, 0, 1)).render()
        vertex.reparentTo(track)
        return track

    def _update_event(self, event):
        """Update the current event and relocate to it."""
        self._relocate_camera(event)
        if not self._all_vertices:
            if self._current_vertex: self._current_vertex.removeNode()
            self._current_vertex = self._render_decay(event)

    def _relocate_camera(self, event):
        """Relocate the camera to the given event."""
        position = event["tau_at_decay"][1] + numpy.array((100., 100., 100.))
        self.camera.setPos(*position)
        self.camera.lookAt(*event["tau_at_decay"][1])
Ejemplo n.º 16
0
def process(jsonpath,attpath=None,tarpath=None):
  doAtt = True
  untardir = "./tmp"
  try:
    os.stat(untardir)
  except:
    os.mkdir(untardir)   
  
  for name in os.listdir(jsonpath):
    if not name.endswith("json"):
    	continue
    jsonf = name
    target = jsonf[:-13]
  
    print "o Processing", target
    t0 = time.time()
    if attpath is None:
      attpath = jsonpath+"/"
    if tarpath is None:
      tarpath = jsonpath+"/"
    jsonf = jsonpath+"/"+jsonf 
    attf = attpath+target+".att"
    tarf =  tarpath+target+".tgz"

    if os.path.isfile(tarf):
      # Unpack tar file with traces
      print "Extracting",tarf
      tar = tarfile.open(tarf, "r:gz")
      tar.extractall(path=untardir)
      tar.close()
      print "Done."
    else:
      print "No file",tarf,"! Abort."
      continue
 
    if doAtt:
      # Load attenuation
      print "Loading attenuation table",attf
      if os.path.isfile(attf):
        attt = np.loadtxt(attf)
        antid = attt[:,0]
        att = attt[:,1:]
        print "Done."
      else:
        print "No file",attf,"! Abort."
        #continue
    else:
        print "No attenuation considered in this treatment because doAtt=",doAtt
	
    # Now loop on events in json file (Should only be one)
    for evt in EventIterator(jsonf):
      ants = np.array(evt["antennas"])
      nAnts = len(ants)
      
      print "Now computing attenuated + filtered voltages"
      voltage=[]
      time_peaks=[]
      # Now loop on all antennas
      for i in range(nAnts):
  	file_freespace = untardir+"/"+target+"/out_{0}.txt".format(i)
	if os.path.isfile(file_freespace):  # Voltage file exists
	  if doAtt:
	    res = attenuate(file_freespace,att[i,:])
	  else:
	    res = filt(file_freespace)
	  
	  res[np.isnan(res)] = 0;
	  v_list = (i,round(res[1]-res[3],3),round(res[5]-res[7],3),round(res[9]-res[11],3),round(res[13]-res[15],3))
	  voltage.append( v_list )
  	  time_peaks.append(list(res))
 
      # Now write output to file
      print "Now logging results to",jsonf
      log_event = EventLogger(path=jsonf)
      # Add the additional informations to the shower event
      evt['voltage'] = voltage # muV
      evt['time_peaks'] = time_peaks # s, muV
      log_event(**evt)

    print "  --> Done in {:.1f} s".format(time.time() - t0)
    shutil.rmtree(untardir)
Ejemplo n.º 17
0
    def signal_handler(sig, frame):
        sys.exit(0)

    signal.signal(signal.SIGINT, signal_handler)

    # Set the output stream
    if args.output is not None:
        with open(args.output, "w+") as stream:
            pass
    else:
        stream = sys.stdout

    # Loop over events
    done = 0
    for filename in args.files:
        for i, event in enumerate(EventIterator(filename)):
            if i < args.skip:
                continue

            # Unpack the tau event
            tau = event["tau_at_decay"]
            energy = tau[1]
            latitude, longitude, altitude = tau[4]
            azimuth, elevation = -tau[5][1], 90. - tau[5][0]
            if azimuth < 0: azimuth += 360

            # Sample the primaries
            primaries = sample_primaries(args.events, energy, latitude,
                longitude, altitude, azimuth, elevation)

            # Dump the result
Ejemplo n.º 18
0
    d = numpy.array(direction, dtype=numpy.float64)
    d /= numpy.linalg.norm(d)

    eye = numpy.eye(3, dtype=numpy.float64)
    ddt = numpy.outer(d, d)
    skew = numpy.array([[0, d[2], -d[1]], [-d[2], 0, d[0]], [d[1], -d[0], 0]],
                       dtype=numpy.float64)

    mtx = ddt + numpy.cos(angle) * (eye - ddt) + numpy.sin(angle) * skew
    return mtx


# Load the event
tag = "E.1e17_X.93815_Y.74409_Z.-559_T.91_P.21_D.3750214666968983.more-antennas"
topo = Topography(43, 87, "flat/10")
event = EventIterator("share/events/{:}.json".format(tag)).next()
antennas = numpy.array(event["antennas"])
energy, position, direction, altitude = map(numpy.array, event["tau_at_decay"])

gamma = numpy.deg2rad(3.)
zcmin = 14E+03
zcmax = 165E+03 * energy / 1E+09 + 55E+03
rmin = position + zcmin * direction

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(antennas[:, 0], antennas[:, 1], antennas[:, 2], "ko")
ax.plot((position[0], ), (position[1], ), (position[2], ), "ko")

r0 = position
r1 = position + zcmax * direction
Ejemplo n.º 19
0
# Settings
DATA_DIR = "share/events/earth-skimming"

outdir = os.path.join(DATA_DIR, "pruned")
if not os.path.exists(outdir):
    os.makedirs(outdir)

topography = Topography(latitude=43, longitude=87, path="flat/10")

for filename in os.listdir(DATA_DIR):
    if not filename.startswith("events"):
        continue
    t0 = time.time()
    print "# Processing", filename
    subindex = 1
    path = os.path.join(DATA_DIR, filename)
    for i, event in enumerate(EventIterator(path)):
        if (i % 100) == 0:
            v = filename.split(".")
            v.insert(-1, str(subindex))
            newname = ".".join(v)
            subindex += 1
            path = os.path.join(DATA_DIR, "pruned", newname)
            log_event = EventLogger(path=path)
        tau = event["tau_at_decay"]
        tau.append(topography.local_to_lla(tau[1])[2])
        event.pop("previous")
        log_event(**event)
    print "  --> Done in {:.1f} s".format(time.time() - t0)
Ejemplo n.º 20
0
###########################
if len(sys.argv)<3:
    print """\
This script plot the 2D map of the Efield & voltages amplitude pattern

Usage:  python plotAmpPattern.py [folder containing the .traces file]  [file extension]
"""
    sys.exit(1)
###########################

wkdir = sys.argv[1] # path where the simulation file is

# First load json infos
try:
  json_file =  glob.glob(wkdir+'*'+'.voltage.json')[0]
  for evt in EventIterator(json_file):
    # Ants infos
    ants = np.array(evt["antennas"])
except:
  try: 
    print wkdir
    ants = np.loadtxt(wkdir+"/antpos.dat")    
  except:
    print "caca"
    
    
xants = ants[:,0]
yants = ants[:,1]
zants = ants[:,2]
alpha = ants[:,3]
beta = ants[:,4]
Ejemplo n.º 21
0
out_dir = os.path.realpath(
    os.path.join(sys.argv[1], "..", out_prefix + "-split"))
if not os.path.exists(out_dir):
    os.makedirs(out_dir)

# Loop over files
log_event = None
out_index, n_events = 0, 0
for filename in os.listdir(sys.argv[1]):
    if not filename.startswith("events"):
        continue
    path = os.path.join(sys.argv[1], filename)
    print "o Processing file", path
    t0 = time.time()
    # Loop over events in a file
    for event in EventIterator(path):
        event.pop("previous")
        if log_event is None:
            # Create a new output file
            if events_per_file <= 1:
                path = os.path.join(out_dir, "{:}.json".format(event["tag"]))
            else:
                out_index += 1
                path = os.path.join(out_dir,
                                    "events.{:}.json".format(out_index))
            log_event = EventLogger(path=path)
        log_event(**event)
        n_events += 1
        if n_events >= events_per_file:
            # Stop writting to the current output file
            log_event = None
Ejemplo n.º 22
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This is a test procedure. It shows the distribution of the elevation angle
# of selected events.

import numpy
import matplotlib.pyplot as plt

from retro.event import EventIterator
from grand_tour import Topography

topo = Topography(42.928056, 86.741667, "../../share/topography", 25)
elevation = []
for event in EventIterator("../../events-ulastai.json"):
    energy, position, direction = event["tau_at_decay"]
    theta, phi = topo.local_to_angular(position, direction)
    elevation.append(90. - theta)

p, x = numpy.histogram(elevation, 40, density=True)
x = 0.5 * (x[1:] + x[:-1])

plt.style.use("deps/mplstyle-l3/style/l3.mplstyle")
plt.figure()
plt.plot(x, p, "ko-")
plt.show()