Exemplo n.º 1
0
def find_calsource(self,datadir):
    '''
    try to find, and then read the calsource file corresponding to the dataset
    '''
    # look for files within the last hour, and then take the closest one to the start time
    # the files are in FITS format as of Wed 10 Apr 2019 10:21:35 CEST
    self.printmsg('trying to find calsource data corresponding to %s' % self.dataset_name,verbosity=2)

    # calsource directory is normally two up
    calsource_dir = '%s/calsource' % os.path.dirname(os.path.dirname(datadir))
    filetype = 'calsource'
    datadir = calsource_dir
    search_start = self.obsdate - dt.timedelta(minutes=30)
    pattern = []
    pattern.append('%s/calsource_%s*.fits' % (calsource_dir,search_start.strftime('%Y%m%dT%H')))
    pattern.append('%s/calsource_%s*.fits' % (calsource_dir,self.obsdate.strftime('%Y%m%dT%H')))
    files = []
    for p in pattern:
        files += glob(p)
    if len(files)==0:
        self.printmsg('No %s data found in directory: %s' % (filetype,datadir),verbosity=1)
        return

    # find the file which starts before and nearest to obsdate
    filename = None
    file_delta = 1e6
    for f in files:
        basename = os.path.basename(f)
        file_date = dt.datetime.strptime(basename,'calsource_%Y%m%dT%H%M%S.fits')
        delta = tot_seconds(self.obsdate - file_date)
        if np.abs(delta)<file_delta:
            file_delta = np.abs(delta)
            filename = f

    if file_delta>30:
        self.printmsg('Did not find a corresponding calsource file.')
        return
    
    self.printmsg('found calsource file which started %.1f seconds before the data acquisition' % file_delta)
    self.printmsg('reading calsource file: %s' % filename)
    hdulist=pyfits.open(filename)
    nhdu=len(hdulist)
    if nhdu!=2:
        self.printmsg("This doesn't look like a calsource file!")
        hdulist.close()
        return
    hdu = hdulist[1]
    if 'EXTNAME' not in hdu.header.keys()\
       and hdu.header['EXTNAME']!='CALSOURCE':
        self.printmsg("This is not a calsource FITS file!")
        hdulist.close()
        return
    
    self.read_calsource_fits(hdu)
    hdulist.close()
    return
Exemplo n.º 2
0
def calsource2fits(filename):

    starttime = dt.datetime.utcnow()
    sys.stdout.write('%s ... ' % filename)
    sys.stdout.flush()
    t, v = read_calsource_dat(filename)
    if t is None: return
    endtime = dt.datetime.utcnow()
    delta = tot_seconds(endtime - starttime)
    sys.stdout.write(' read file in %.1f seconds' % delta)
    sys.stdout.flush()

    starttime = dt.datetime.utcnow()
    fitsname = write_calsource_fits(t, v)
    if fitsname is None: return
    endtime = dt.datetime.utcnow()
    delta = tot_seconds(endtime - starttime)
    sys.stdout.write(' saved file in %.1f seconds\n' % delta)
    sys.stdout.flush()

    return
Exemplo n.º 3
0
    def onoff(self, states=None):
        '''
        switch on or off devices
        we have to wait for the Energenie powerbar to reset
        '''
        reset_delta = self.energenie_timeout  # minimum time to wait
        now = dt.datetime.utcnow()
        delta = tot_seconds(now - self.energenie_lastcommand_date)

        if delta < reset_delta:
            extra_wait = reset_delta - delta
            time.sleep(extra_wait)

        ack = ''
        if states is not None:
            info = energenie_set_socket_states(states)
            if info['ok']:
                ack = 'OK-'
            else:
                ack = 'FAILED_SET_STATES-'
            if 'error_message' in info.keys():
                self.log('energenie error: %s' % info['error_message'])
            self.log('energenie message: %s' % info['message'])

        # check for the on/off status
        time.sleep(reset_delta)  # wait a bit before sending another command
        states_read = energenie_get_socket_states()
        if states_read is not None:
            ack += 'OK'
            self.log('retrieved energenie states: %s' % states_read,
                     verbosity=2)
        else:
            ack += 'FAILED_GET_STATES'
            self.log('FAILED to get energenie states', verbosity=2)

        if ack.find('FAILED_GET_STATES') < 0:
            for socket_no in states_read.keys():
                socket_idx = socket_no - 1
                state = states_read[socket_no]
                dev = self.device_list[socket_idx]
                self.device_on[dev] = state

        self.energenie_lastcommand_date = dt.datetime.utcnow()
        return ack
Exemplo n.º 4
0
    def acquire(self, duration=None):
        '''
        acquire data with timestamps from the Arduino Uno

        duration is given in seconds


        Fri 12 Apr 2019 14:17:47 CEST:  change of behaviour.
        we don't return the data, we return the filename with the data
        "save" is no longer an option
        '''
        if not self.connected: self.init()
        if not self.connected: return None, None
        self.clear_interrupt_flag()

        if duration is None:
            dt_duration = dt.timedelta(minutes=5)
        else:
            dt_duration = dt.timedelta(seconds=duration)

        self.log('##### arduino_acquire #####')
        if self.connection == 'serial':
            self.s.flush()
        else:
            client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # UDP
            client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
            #client.settimeout(2.0)
            client.bind(('', self.broadcast_port))
            self.log('listening to Arduino on socket port %i' %
                     self.broadcast_port)

        #y=[]
        #t=[]
        start_time = dt.datetime.utcnow()
        end_time = start_time + dt_duration
        now = dt.datetime.utcnow()

        # open a file for on-the-fly acquisition to disk
        outfile = start_time.strftime('calsource_%Y%m%dT%H%M%S.dat')
        h = open(outfile, 'w')

        ## acquisition for serial connection.  Don't put the "if" inside the loop!
        if self.connection == 'serial':
            while now < end_time and not os.path.isfile(
                    self.interrupt_flag_file):
                x = self.s.readline()
                val = x.strip()
                now = dt.datetime.utcnow()
                h.write('%s %s\n' % (now.strftime('%s.%f'), val))
                #y.append(val)
                #t.append(now)
        else:
            counter = 0
            while now < end_time and not os.path.isfile(
                    self.interrupt_flag_file):
                x, addr = client.recvfrom(1024)

                # Mon 29 Apr 2019 16:31:25 CEST
                # now we are using the ADC on the Raspberry Pi and not the Arduino
                # the name "arduino" remains as a nickname
                now = dt.datetime.utcnow()
                #val = x.strip()
                #h.write('%s %s\n' % (now.strftime('%s.%f'),val))

                # Sat 22 Jun 2019 23:19:53 CEST
                # now the Raspberry Pi PiGPS is sending packed data instead of string
                #dat = x.strip().split()
                #tstamp = dat[0]
                #val = dat[1]
                fmts = '<Bdq'
                data_tuple = struct.unpack(fmts, x)
                stx = data_tuple[0]
                tstamp = data_tuple[1]
                val = data_tuple[2]

                h.write('%.6f %i\n' % (tstamp, val))

                #y.append(val)
                #t.append(now)
                counter += 1

        end_time = now
        h.close()
        self.log('output file written: %s' % outfile)
        self.log('started data acquisition at %s' %
                 start_time.strftime('%Y-%m-%d %H:%M:%S.%f UTC'))
        self.log('  ended data acquisition at %s' %
                 end_time.strftime('%Y-%m-%d %H:%M:%S.%f UTC'))
        delta = end_time - start_time
        self.log('total acquisition time: %.3f seconds' % tot_seconds(delta))

        self.clear_interrupt_flag()
        return outfile
        '''