How to write two CSV concurrently in Python?

I have two radios, sdr and sdr2, receiving data and I want to save that date (which are complex numbers) in a CSV file. I need to get the data from both radios at the same time,running a scan 5 times on each, so what I do in the main part of my code is:

#we save the sdr and sdr2 in the same array
radios = [ sdr, sdr2]
pool = ThreadPool(4)
#create an object of class Scan
s=Scan()
#pool.map(function, array)
pool.map(s.scan, radios)
pool.close() 
pool.join()

Then, the Scan function is:

class Scan: 
    def scan(self, object):   
      for i in range(0,1):
        #Read iq data
        samples = object.read_samples(256*1024)
        #print(samples)

       #get the maximum amplitude in frequency to save IQ samples if it's greater
       #than -1 dB
        sp = np.fft.fft(samples)
        ps_real=sp.real
        ps_imag=sp.imag
        sq=np.power(ps_real,2)+np.power(ps_imag,2)
        sqrt=np.sqrt(sq)
        psd=sqrt/1024
        value=[ps_real,ps_imag]
        max=np.max(psd)
        log=10*math.log10(max)
        print(value)
        current_time = time.strftime("%m.%d.%y-%H%M.csv", time.localtime())
        if log > -1:
            #save the IQ data in csv
            with open('%s' % current_time, 'w',newline='') as f:
                writer = csv.writer(f, delimiter=',')
                writer.writerows(zip(ps_real,ps_imag))

but what this does is getting the array (real,imag pairs) from the last iteration of one of the radios (I think it's only one) and save it in a unique CSV...I'd like to have 2 different CSVs, that's why I put the timestamp in the CSV name and I also need to record the data from any iteration. Any idea on how to fix this? Thanks!

Upvotes: 1

Views: 239

Answers (1)

Netwave
Netwave

Reputation: 42678

You are opening the outfile in the same day and same hour and minute so you are writing to the same file in both jobs, just make the function use a id and pass it as argument:

class Scan: 
    def scan(self, id, object):
        ...
        current_time = time.strftime("%m.%d.%y-%H%M", time.localtime())
        if log > -1:
            #save the IQ data in csv
            with open('{}_{}.csv' .format(current_time, id), 'w',newline='') as f:
                ...

And then use a wrapper to unpack the ids from an enumerate to the radios when mapping it in the thread pool:

#we save the sdr and sdr2 in the same array
radios = [ sdr, sdr2]
pool = ThreadPool(4)
#create an object of class Scan
s=Scan()

def scan(args_tuple):
    global s
    id, code = args_tuple
    return s.scan(id, code)

pool.map(scan, enumerate(radios))
pool.close() 
pool.join()

Upvotes: 1

Related Questions