def process_gains(self, proc_dir, send_dir):

        # find the most recent gains file
        files = [
            file for file in os.listdir(proc_dir)
            if file.lower().endswith(".gains")
        ]
        self.trace("files=" + str(files))

        if len(files) > 0:

            gains_time_file = proc_dir + "/gains.time"
            cmd = ""

            # combine all the gains files together
            if os.path.exists(gains_time_file):
                cmd = "uwb_adaptive_filter_tappend " + gains_time_file
                for file in files:
                    cmd = cmd + " " + proc_dir + "/" + file
                cmd = cmd + " " + gains_time_file
            else:
                if len(files) == 1:
                    cmd = "cp " + proc_dir + "/" + files[0] + " " + \
                        gains_time_file
                else:
                    cmd = "uwb_adaptive_filter_tappend"
                    for file in files:
                        cmd = cmd + " " + proc_dir + "/" + file
                    cmd = cmd + " " + gains_time_file

            self.info(cmd)
            rval, lines = self.system(cmd, 2)
            if not rval == 0:
                self.warn("failed to tappend gains files")
                return

            # read the data from file into a numpy array
            file_size = os.path.getsize(gains_time_file)
            header_size = 4096
            data_size = file_size - header_size

            gains_file = open(gains_time_file, "rb")
            header_str = gains_file.read(header_size)
            header = Config.readDictFromString(header_str)

            npol = int(header["NPOL"])
            ndim = int(header["NDIM"])
            nchan = int(header["NCHAN"])
            nant = int(header["NANT"])
            nbit = int(header["NBIT"])
            freq = float(header["FREQ"])
            bw = float(header["BW"])
            tsamp = float(header["TSAMP"])

            self.info("npol=" + str(npol) + " ndim=" + str(ndim) + " nchan=" +
                      str(nchan) + " nant=" + str(nant) + " nbit=" +
                      str(nbit) + " freq=" + str(freq) + " bw=" + str(bw))

            # check that the nbit is 32
            bytes_per_sample = (npol * ndim * nchan * nant * nbit) / 8
            ndat = data_size / bytes_per_sample
            nval = ndat * nant * nchan * npol * ndim

            self.info("ndat=" + str(ndat) + " bytes_per_sample=" +
                      str(bytes_per_sample) + " nval=" + str(nval))

            raw = np.fromfile(gains_file, dtype=np.float32, count=nval)
            gains_file.close()

            self.info("np.shape(raw)=" + str(np.shape(raw)))
            self.info("npol=" + str(npol) + " nchan=" + str(nchan))

            # reshape the raw data into a numpy array with specified dimensions
            data = raw.reshape((ndat, nant, npol, nchan, ndim))

            # acquire the results lock
            self.results["lock"].acquire()

            # generate an empty numpy array with ndat values
            xvals = np.zeros(ndat)
            gains_time = np.zeros((npol, ndat))
            gains_freq = np.zeros((npol, nchan))

            for idat in range(ndat):
                xvals[idat] = float(idat) * tsamp / 1e6
                for ipol in range(npol):
                    for isig in range(nant):
                        for ichan in range(nchan):
                            power = 0
                            for idim in range(ndim):
                                g = data[idat][isig][ipol][ichan][idim]
                                power += g * g
                            if power > gains_time[ipol][idat]:
                                gains_time[ipol][idat] = power
                            if idat == ndat - 1:
                                gains_freq[ipol][ichan] = power

            if npol == 1:
                labels = ["AA+BB"]
                colours = ["red"]
            if npol == 2:
                labels = ["AA", "BB"]
                colours = ["red", "green"]
            else:
                labels = []
                colours = []

            self.gains_time_plot.plot(240, 180, True, xvals, gains_time,
                                      labels, colours)
            self.results["gainstime_lo"] = self.gains_time_plot.getRawImage()

            self.gains_time_plot.plot(1024, 768, False, xvals, gains_time,
                                      labels, colours)
            self.results["gainstime_hi"] = self.gains_time_plot.getRawImage()

            self.gains_freq_plot.plot_npol(240, 180, True, nchan, freq, bw,
                                           gains_freq, labels)
            self.results["gainsfreq_lo"] = self.gains_freq_plot.getRawImage()

            self.gains_freq_plot.plot_npol(1024, 768, False, nchan, freq, bw,
                                           gains_freq, labels)
            self.results["gainsfreq_hi"] = self.gains_freq_plot.getRawImage()

            self.gains_valid = True

            self.results["lock"].release()

            for file in files:
                os.rename(proc_dir + "/" + file, send_dir + "/" + file)
        return len(files)
    def process_gains(self, proc_dir, send_dir):

        # find the most recent gains file
        files = [file for file in os.listdir(proc_dir)
                 if file.lower().endswith(".gains")]
        self.trace("files=" + str(files))

        if len(files) > 0:

            gains_time_file = proc_dir + "/gains.time"
            cmd = ""

            # combine all the gains files together
            if os.path.exists(gains_time_file):
                cmd = "uwb_adaptive_filter_tappend " + gains_time_file
                for file in files:
                    cmd = cmd + " " + proc_dir + "/" + file
                cmd = cmd + " " + gains_time_file
            else:
                if len(files) == 1:
                    cmd = "cp " + proc_dir + "/" + files[0] + " " + \
                        gains_time_file
                else:
                    cmd = "uwb_adaptive_filter_tappend"
                    for file in files:
                        cmd = cmd + " " + proc_dir + "/" + file
                    cmd = cmd + " " + gains_time_file

            self.info(cmd)
            rval, lines = self.system(cmd, 2)
            if not rval == 0:
                self.warn("failed to tappend gains files")
                return

            # read the data from file into a numpy array
            file_size = os.path.getsize(gains_time_file)
            header_size = 4096
            data_size = file_size - header_size

            gains_file = open(gains_time_file, "rb")
            header_str = gains_file.read(header_size)
            header = Config.readDictFromString(header_str)

            npol = int(header["NPOL"])
            ndim = int(header["NDIM"])
            nchan = int(header["NCHAN"])
            nant = int(header["NANT"])
            nbit = int(header["NBIT"])
            freq = float(header["FREQ"])
            bw = float(header["BW"])
            tsamp = float(header["TSAMP"])

            self.info("npol=" + str(npol) + " ndim=" + str(ndim) + " nchan=" +
                      str(nchan) + " nant=" + str(nant) + " nbit=" +
                      str(nbit) + " freq=" + str(freq) + " bw=" + str(bw))

            # check that the nbit is 32
            bytes_per_sample = (npol * ndim * nchan * nant * nbit) / 8
            ndat = data_size / bytes_per_sample
            nval = ndat*nant*nchan*npol*ndim

            self.info("ndat=" + str(ndat) + " bytes_per_sample=" +
                      str(bytes_per_sample) + " nval=" + str(nval))

            raw = np.fromfile(gains_file, dtype=np.float32, count=nval)
            gains_file.close()

            self.info("np.shape(raw)=" + str(np.shape(raw)))
            self.info("npol=" + str(npol) + " nchan=" + str(nchan))

            # reshape the raw data into a numpy array with specified dimensions
            data = raw.reshape((ndat, nant, npol, nchan, ndim))

            # acquire the results lock
            self.results["lock"].acquire()

            # generate an empty numpy array with ndat values
            xvals = np.zeros(ndat)
            gains_time = np.zeros((npol, ndat))
            gains_freq = np.zeros((npol, nchan))

            for idat in range(ndat):
                xvals[idat] = float(idat) * tsamp / 1e6
                for ipol in range(npol):
                    for isig in range(nant):
                        for ichan in range(nchan):
                            power = 0
                            for idim in range(ndim):
                                g = data[idat][isig][ipol][ichan][idim]
                                power += g * g
                            if power > gains_time[ipol][idat]:
                                gains_time[ipol][idat] = power
                            if idat == ndat-1:
                                gains_freq[ipol][ichan] = power

            if npol == 1:
                labels = ["AA+BB"]
                colours = ["red"]
            if npol == 2:
                labels = ["AA", "BB"]
                colours = ["red", "green"]
            else:
                labels = []
                colours = []

            self.gains_time_plot.plot(240, 180, True, xvals, gains_time,
                                      labels, colours)
            self.results["gainstime_lo"] = self.gains_time_plot.getRawImage()

            self.gains_time_plot.plot(1024, 768, False, xvals, gains_time,
                                      labels, colours)
            self.results["gainstime_hi"] = self.gains_time_plot.getRawImage()

            self.gains_freq_plot.plot_npol(240, 180, True, nchan, freq, bw,
                                           gains_freq, labels)
            self.results["gainsfreq_lo"] = self.gains_freq_plot.getRawImage()

            self.gains_freq_plot.plot_npol(1024, 768, False, nchan, freq, bw,
                                           gains_freq, labels)
            self.results["gainsfreq_hi"] = self.gains_freq_plot.getRawImage()

            self.gains_valid = True

            self.results["lock"].release()

            for file in files:
                os.rename(proc_dir + "/" + file, send_dir + "/" + file)
        return len(files)
    def process_cleaned(self, proc_dir, send_dir):

        files = [
            file for file in os.listdir(proc_dir)
            if file.lower().endswith(".cleaned")
        ]
        self.trace("files=" + str(files))

        if len(files) > 0:

            # process only the most recent file
            last_file = proc_dir + "/" + files[-1]
            self.info("file=" + last_file)

            # read the data from file into a numpy array
            file_size = os.path.getsize(last_file)
            header_size = 4096
            data_size = file_size - header_size

            # read the data from file into a numpy array
            fptr = open(last_file, "rb")
            header_str = fptr.read(header_size)
            header = Config.readDictFromString(header_str)

            npol = int(header["NPOL"])
            ndim = int(header["NDIM"])
            nchan = int(header["NCHAN"])
            nant = int(header["NANT"])
            nbit = int(header["NBIT"])
            freq = float(header["FREQ"])
            bw = float(header["BW"])
            self.info("npol=" + str(npol) + " ndim=" + str(ndim) + " nchan=" +
                      str(nchan) + " nant=" + str(nant) + " nbit=" +
                      str(nbit) + " freq=" + str(freq) + " bw=" + str(bw))

            bytes_per_sample = (npol * ndim * nchan * nant * nbit) / 8
            ndat = data_size / bytes_per_sample

            # TODO check that nbit==32 ndat==1 nant=1
            nval = ndat * nant * nchan * npol * ndim
            self.info("bytes_per_sample=" + str(bytes_per_sample) + " nval=" +
                      str(nval))

            raw = np.fromfile(fptr, dtype=np.float32, count=nval)
            fptr.close()

            # reshape the raw data into a numpy array with specified dimensions
            self.info("np.shape(raw)=" + str(np.shape(raw)))
            self.info("npol=" + str(npol) + " nchan=" + str(nchan))
            data = raw.reshape((npol, nchan))

            if npol == 1:
                labels = ["AA"]
            elif npol == 2:
                labels = ["AA", "BB"]
            else:
                labels = []

            # acquire the results lock
            self.results["lock"].acquire()

            self.info("len(data)=" + str(len(data)))

            # generate plots multi polarisation plots
            self.cleaned_plot.plot_npol(240, 180, True, nchan, freq, bw, data,
                                        labels)
            self.results["cleaned_lo"] = self.cleaned_plot.getRawImage()

            self.cleaned_plot.plot_npol(1024, 768, False, nchan, freq, bw,
                                        data, labels)
            self.results["cleaned_hi"] = self.cleaned_plot.getRawImage()
            self.cleaned_valid = True
            self.results["lock"].release()

            for file in files:
                os.rename(proc_dir + "/" + file, send_dir + "/" + file)
        return len(files)
    def process_cleaned(self, proc_dir, send_dir):

        files = [file for file in os.listdir(proc_dir)
                 if file.lower().endswith(".cleaned")]
        self.trace("files=" + str(files))

        if len(files) > 0:

            # process only the most recent file
            last_file = proc_dir + "/" + files[-1]
            self.info("file=" + last_file)

            # read the data from file into a numpy array
            file_size = os.path.getsize(last_file)
            header_size = 4096
            data_size = file_size - header_size

            # read the data from file into a numpy array
            fptr = open(last_file, "rb")
            header_str = fptr.read(header_size)
            header = Config.readDictFromString(header_str)

            npol = int(header["NPOL"])
            ndim = int(header["NDIM"])
            nchan = int(header["NCHAN"])
            nant = int(header["NANT"])
            nbit = int(header["NBIT"])
            freq = float(header["FREQ"])
            bw = float(header["BW"])
            self.info("npol=" + str(npol) + " ndim=" + str(ndim) + " nchan=" +
                      str(nchan) + " nant=" + str(nant) + " nbit=" +
                      str(nbit) + " freq=" + str(freq) + " bw=" + str(bw))

            bytes_per_sample = (npol * ndim * nchan * nant * nbit) / 8
            ndat = data_size / bytes_per_sample

            # TODO check that nbit==32 ndat==1 nant=1
            nval = ndat*nant*nchan*npol*ndim
            self.info("bytes_per_sample=" + str(bytes_per_sample) + " nval=" +
                      str(nval))

            raw = np.fromfile(fptr, dtype=np.float32, count=nval)
            fptr.close()

            # reshape the raw data into a numpy array with specified dimensions
            self.info("np.shape(raw)=" + str(np.shape(raw)))
            self.info("npol=" + str(npol) + " nchan=" + str(nchan))
            data = raw.reshape((npol, nchan))

            if npol == 1:
                labels = ["AA"]
            elif npol == 2:
                labels = ["AA", "BB"]
            else:
                labels = []

            # acquire the results lock
            self.results["lock"].acquire()

            self.info("len(data)=" + str(len(data)))

            # generate plots multi polarisation plots
            self.cleaned_plot.plot_npol(240, 180, True, nchan, freq, bw,
                                        data, labels)
            self.results["cleaned_lo"] = self.cleaned_plot.getRawImage()

            self.cleaned_plot.plot_npol(1024, 768, False, nchan, freq, bw,
                                        data, labels)
            self.results["cleaned_hi"] = self.cleaned_plot.getRawImage()
            self.cleaned_valid = True
            self.results["lock"].release()

            for file in files:
                os.rename(proc_dir + "/" + file, send_dir + "/" + file)
        return len(files)