def read_result_file(station, dt, products, program, prefixdir): """ read text-file and return PPP_Result """ mjd = jdutil.datetime_to_mjd(dt) fname = "%s.%d.%s.%s.txt" % (station.receiver, mjd, products, program) r = PPP_Result() with open(prefixdir + "/results/" + station.name + "/" + fname) as f: for line in f: if line.startswith("#"): pass else: line = line.split() # 0 1 2 3 4 5 6 7 8 9 10 # Year Month Day Hour Min Sec Lat(deg) Lon(deg) Height(m) Clock(ns) ZTD(m) epoch = datetime.datetime(int(line[0]), int(line[1]), int(line[2]), int(line[3]), int(line[4]), int(line[5])) lat = float(line[6]) lon = float(line[7]) height = float(line[8]) clock = float(line[9]) ztd = float(line[10]) r.append(PPP_Point(epoch, lat, lon, height, clock, ztd)) return r
def write_result_file(ppp_result, preamble="", rapid=True, tag="ppp", prefixdir=""): """ write the ppp result out to a text file """ ftp_tools.check_dir(prefixdir + "/results") ftp_tools.check_dir(prefixdir + "/results/" + ppp_result.station.name) first_obs = ppp_result.observations[0] first_obs_mjd = int(jdutil.datetime_to_mjd(first_obs.epoch)) rapid_final = "final" if rapid: rapid_final = "rapid" result_file = ppp_result.station.receiver + "." + str( first_obs_mjd) + "." + rapid_final + "." + tag + ".txt" outfile = prefixdir + "/results/" + ppp_result.station.name + "/" + result_file with open(outfile, 'w') as f: datastring = "# " + result_file + " \n" f.write(datastring) for line in preamble.split('\n'): f.write("# %s\n" % line) f.write(PPP_Point.column_labels()) for point in ppp_result.observations: f.write(str(point) + "\n") print " wrote results to ", outfile
def diff_stations(prefixdir, station1, station2, dt, products, program): """ calculate clock difference between two stations Station1 PPP result is receive_clock_offset = IGST(rapid/final) - Station1 Station2 PPP result is receive_clock_offset = IGST(rapid/final) - Station2 Double difference Station1-Station2 gives: (IGST(rapid/final) - Station1) - (IGST(rapid/final) - Station2) = Station2 - Station1 """ year = dt.timetuple().tm_year doy = dt.timetuple().tm_yday mjd = jdutil.datetime_to_mjd(dt) # output filename diff_dir = prefixdir + "/results/diff/" ftp_tools.check_dir(diff_dir) fname = diff_dir + "%s.diff.%s.%d.%s.%s.txt" % ( station1.receiver, station2.receiver, mjd, products, program) if os.path.exists(fname): print(fname, " already exists - nothing to do.") return # result already exists, nothing to do r1 = read_result_file(station1, dt, products, program, prefixdir) r2 = read_result_file(station2, dt, products, program, prefixdir) print("diff ", station1.name, " - ", station2.name, end=' ') # print "diff station2 ", len(r2) # read_result_file( ) # 2,x2) = bipm_ftp.COD_read_day(prefixdir, station2, year, doy, rapid) # print len(t1), len(t2) (t_diff, clock_diff) = diff(r1, r2) print(len(t_diff), " points") # print len(td), len(d) # print d # print numpy.median(d) # (td2,d2) = mad.remove_timeseries_outliers(td,d) # write_diff_file(prefixdir, station1, station2, year, doy, td2, d2) # print len(td2) return (t_diff, clock_diff)
def write_result_file( ppp_result , preamble="" , rapid=True, tag="ppp", prefixdir=""): """ write the ppp result out to a text file """ ftp_tools.check_dir(prefixdir + "/results") ftp_tools.check_dir(prefixdir + "/results/" + ppp_result.station.name) first_obs = ppp_result.observations[0] first_obs_mjd = int(jdutil.datetime_to_mjd( first_obs.epoch ) ) rapid_final = "final" if rapid: rapid_final = "rapid" result_file = ppp_result.station.receiver +"." + str(first_obs_mjd) + "." + rapid_final + "." + tag + ".txt" outfile = prefixdir + "/results/" + ppp_result.station.name + "/" + result_file with open(outfile,'w') as f: datastring = "# " + result_file + " \n" f.write(datastring) for line in preamble.split('\n'): f.write( "# %s\n" % line) f.write(PPP_Point.column_labels()) for point in ppp_result.observations: f.write(str(point) + "\n") print " wrote results to ", outfile
def __init__(self, noteName, vlen, bandwidth, record): gr.sync_block.__init__( self, name="ra_event_sink", # inputs: time sequence of I,Q values, # peak, rms, Event MJD in_sig=[(np.complex64, int(vlen))], out_sig=None, ) vlen = int(vlen) self.vlen = vlen self.ecount = 1 self.record = int(record) self.obs = radioastronomy.Spectrum() self.setupdir = "./" noteParts = noteName.split('.') self.noteName = noteParts[0] + '.not' if len(noteParts) > 2: print '!!! Warning, unexpected Notes File name! ' print '!!! Using file: ', self.noteName else: if os.path.isfile(self.noteName): print 'Setup File : ', self.noteName else: if os.path.isfile("Watch.not"): try: import shutil shutil.copyfile("Watch.not", self.noteName) print "Created %s from file: Watch.not" % ( self.noteName) except: pformat = "! Create the Note file %s, and try again !" print pformat % (self.noteName) self.obs.read_spec_ast(self.noteName) # read the parameters # prepare to Event get messages # print 'Registered event on input port of sink' # self.set_tag_propagation_policy(gr.TPP_ALL_TO_ALL) # self.set_msg_handler(pmt.intern('in_port'), self.event_handler) self.obs.datadir = "../events/" # writing events not spectra self.obs.noteB = "Event Detection" if not os.path.exists(self.obs.datadir): os.makedirs(self.obs.datadir) nd = len(self.obs.datadir) if self.obs.datadir[nd - 1] != '/': self.obs.datadir = self.obs.datadir + "/" print 'DataDir : ', self.obs.datadir self.obs.nSpec = 0 # not working with spectra self.obs.nChan = 0 self.obs.nTime = 1 # working with time series self.obs.nSamples = vlen vlen2 = int(vlen / 2) self.obs.refSample = vlen2 + 1 # event is in middle of time sequence self.obs.ydataA = np.zeros(vlen, dtype=np.complex64) self.obs.xdata = np.zeros(vlen) now = datetime.datetime.utcnow() self.eventutc = now self.obs.utc = now self.eventmjd = jdutil.datetime_to_mjd(now) self.lastmjd = self.eventmjd self.emagnitude = 0. self.erms = 0. self.set_sample_rate(bandwidth)
def __init__(self, vlen, mode, nsigma, sample_rate, sample_delay): """ Initialize the event class, zero sample buffer """ gr.decim_block.__init__(self, name="ra_vevent", # input I/Q pairs in_sig=[np.complex64], # output vector and 3 scalar values out_sig=[(np.complex64, int(vlen))], decim=int(vlen)) self.vlen = int(vlen) if vlen < 10: print "ra_vevent: vector length too short:",self.vlen exit() # register the event parameters self.set_tag_propagation_policy(gr.TPP_ALL_TO_ALL) # self.message_port_register_out(pmt.intern('out_port')) print 'Registered: Event on output port' self.set_relative_rate(1./np.float(vlen)) self.vlen2 = int(vlen/2) self.next = 0 # where to place the next sample self.next2 = self.vlen2 + 1 # where to look for next event self.oneovern = 1./float(self.vlen) self.waitcount = self.vlen # samples until declaring an event self.mode = int(mode) self.nsigma = float(nsigma) self.nsigma2 = self.nsigma*self.nsigma self.first_event = False # track whether first event is found # vector of complex data currently stored self.values = np.zeros(self.vlen, dtype=np.complex64) self.value2s = np.zeros(self.vlen) # vector of magnitudes self.full = False # vector of last event found self.vevent = np.zeros(self.vlen, dtype=np.complex64) self.ecount = np.int_(0) # count of events detected so far self.sample_rate = float(sample_rate) if self.sample_rate < 100.: print 'Invalid Sample Rate: ', self.sample_rate, ' Hz' exit() self.delay = float(sample_delay) self.datetime_delay = datetime.timedelta(seconds=self.delay) self.rmssum2 = 0. self.rms2 = 0. # pre compute nsigma * nsigma * rms2 self.nsigmarms2 = self.nsigma2 * self.rms2 # compute time offset between current time and event self.dt = float(self.vlen2)/self.sample_rate self.dutc = datetime.timedelta(seconds=self.dt) # initialize event times self.eventutc = datetime.datetime.utcnow() - self.dutc self.eventmjd = np.float(jdutil.datetime_to_mjd(self.eventutc)) # !!!! # Hack alert! Had to send the MJD in to parts days, including 10ths # Plus hours, where days and 10ths of days were subtracted # !!!! self.emagnitude = 0. # event magnitude self.erms = 0. # event RMS print 'ra_event Vlen, Nsigma, dt: ', self.vlen, self.nsigma, self.dt if self.vlen < 16: print 'Not Enough samples (<16) to measure RMS: ', self.vlen exit() self.init_buffer()
def work(self, input_items, output_items): """ Work takes the input data and computes the average peak and RMS """ inn = input_items[0] # input complex samples mag2 = inn.real*inn.real + inn.imag*inn.imag # compute magnitudes^2 # get the number of input samples ns = len(inn) # number of samples in this port # print 'Vevent: ', ns, inn[0], mag2[0] outa = output_items[0] # all outputs in PORT 0; vector of samples nout = 0 # count number of output items # this code was optimized to remove some 'ifs' outside the main loop # if the buffer is already full. The cost is that no event can # be detected until the buffer is full. if self.full: # if buffer is already full process differently # run through all input samples for j in range(ns): # record the new values (maybe on top of previous self.values[self.next] = inn[j] self.value2s[self.next] = mag2[j] self.rmssum2 = self.rmssum2 + mag2[j] # now handle circular buffer and detect full buffer self.next = self.next + 1 # next2 is the place to look for the last event self.next2 = self.next2 + 1 if self.next2 >= self.vlen: self.next2 = 0 # determine when buffer is full. # Always output an event each time the buffer cycles # end cycling around the buffer if self.next >= self.vlen: self.next = 0 # only work with squares until event is found self.rms2 = self.rmssum2*self.oneovern # set threshold for the next block of samples self.nsigmarms2 = self.nsigma2 * self.rms2 self.rmssum2 = 0. # start new sum # if monitoring, output latest vector if self.mode <= EVENT_MONITOR: self.vevent = copy.deepcopy(self.values) self.emagnitude = np.sqrt(max( self.value2s)) self.erms = np.sqrt(self.rms2) self.eventutc = datetime.datetime.utcnow() - self.dutc self.eventutc = self.eventutc - self.datetime_delay self.eventmjd = np.float(jdutil.datetime_to_mjd(self.eventutc)) self.ecount = 0 self.lastmjd = self.eventmjd self.first_event = True # Tag the start of the burst (preamble) # if time to output and event outa[nout] = self.vevent # ouput vector of samples nout = nout + 1 # if event found if self.value2s[self.next2] > self.nsigmarms2: if self.full: # if still full # an event is found! self.emagnitude = np.sqrt(self.value2s[self.next2]) self.erms = np.sqrt(self.rms2) self.eventutc = datetime.datetime.utcnow() # offset now for middle of event self.eventutc = self.eventutc - self.dutc self.eventutc = self.eventutc - self.datetime_delay self.eventmjd = np.float(jdutil.datetime_to_mjd(self.eventutc)) # print "Event Detected: %15.9f (MJD) %9.4f %8.4f" % (self.eventmjd, self.emagnitude, self.erms) # deal with circular buffer in centering output event: self.select_event() # describe event to subscribers to the sink vector self.add_item_tag(0, (self.nitems_written(0)+1), pmt.to_pmt('MJD'), pmt.to_pmt(self.eventmjd), pmt.to_pmt('event')) self.add_item_tag(0, (self.nitems_written(0)+1), pmt.to_pmt('PEAK'), pmt.to_pmt(self.emagnitude), pmt.to_pmt('event')) self.add_item_tag(0, (self.nitems_written(0)+1), pmt.to_pmt('RMS'), pmt.to_pmt(self.erms), pmt.to_pmt('event')) # self.event_msg() # report magnitude, rms and date # self.ecount = self.ecount + 1 # keep event count self.first_event = True # print 'Event: ', self.ecount # print 'Utc event: ', self.eventutc # print 'Utc MJD : %12.6f' % (self.eventmjd) # print 'Utc days : %12.6f + %12.6f' % (fdays, hours) # print 'Magnitude: ', self.emagnitude, ' +/- ',self.erms self.init_buffer() # start again # end if an event found # end for all input samples # end if buffer already full else: # this is the start of the other major (un-usual) mode # buffer is not full, fill samples in buffer for j in range(ns): # record the new values (maybe on top of previous self.values[self.next] = inn[j] self.value2s[self.next] = mag2[j] self.rmssum2 = self.rmssum2 + mag2[j] # now handle circular buffer and detect full buffer self.next = self.next + 1 # determine when buffer is full. # Always output an event each time the buffer cycles # end cycling around the buffer if self.next >= self.vlen: self.full = True self.next = 0 # only work with squares until event is found self.rms2 = self.rmssum2*self.oneovern # set threshold for the next block of samples self.nsigmarms2 = self.nsigma2 * self.rms2 self.rmssum2 = 0. # start new sum outa[nout] = self.vevent # ouput vector of samples nout = nout + 1 # check whether we've waited enough for buffer to fill again if self.waitcount <= 0: self.full = True else: self.waitcount = self.waitcount - 1 # next2 is the place to look for the last event self.next2 = self.next2 + 1 if self.next2 >= self.vlen: self.next2 = 0 # end for all samples # end else if not full if nout > 0: output_items[0] = outa return nout