user1643523
user1643523

Reputation: 273

Writing list to array and write it to file (Python)

I guess this is pretty easy, but I can not get it to work. I am having a function giving me a list (1x128). The function is inside a loop (1x32). I want to write all the lists (32x128) from the function into to a file. This is the code:

count = 0  
savez =  np.zeros((waveforms.size/len(waveforms),len(waveforms)))

for wvf in waveforms:  # waveforms is an array of 132x128, so wvf is a list of 1x128 
   # some code
       ...
       ...
       ...

    z, maxpeak = get_first_peak(wvf)  #Function giving me z. z is a list of 128 numbers. 

    for index in range(len(z)):
     savez[count,index] = z[index]

     # Some more Code
       ...
       ...
       ...
    count = count + 1   
# Writing z to file
savetxt("logwvf.dat", savez, fmt="%7.1F") 

Why is this not giving me a file with all the 32 lists of z?

EDIT (Does it help if I include the main code?):

if __name__ == '__main__':

print '***********************************************'
print '      CORRECT CS L2 OFF-RANGE WAVEFORMS        '
print '***********************************************'

try:
    ifile  = sys.argv[1]
    ifile2 = sys.argv[2]
    ifile3 = sys.argv[3]
    ofile  = sys.argv[4]

except:
    print "Usage:", sys.argv[0], "ifile"; sys.exit(1)   

# Open and read file from std, and assign first four (orbit, time, lat, lon) columns to four lists, and last 128 columns (waveforms) to an array.

data = np.genfromtxt(ifile, delimiter = ',', dtype = 'float',
             converters = {i:remove_bracket for i in range(132)}
                 )


# Initiating data, the variables are view not copies of data.
orbit = data[:,0]
time  = data[:,1]
lat   = data[:,2]
lon   = data[:,3]
waveforms   = data[:,4:132]

#---------------------------------------------------------------    
# Constants
threshold_coef = 0.50
#---------------------------------------------------------------                
#Getting height of satellite (H_SAT)
count = 0
H_SAT = []

with open(ifile3, 'r') as hsatfile:
    for line in hsatfile:
       col = line.split()
       H_SAT.append(col[4])

#---------------------------------------------------------------
h_corr = [0]*len(waveforms)
#z=np.zeros((len(waveforms),((waveforms.size)/len(waveforms))))
savez = np.zeros((len(waveforms),(waveforms.size/len(waveforms))))
count = 0
#logwvffile=open('logwvf.dat', 'a')
#---------------------------------------------------------------
# Looping over all waveforms:
for wvf in waveforms:  
    print 'Waveform #: ', count+1
    # Getting waveform, log waveform and maxpeak value
    z, maxpeak = get_first_peak(wvf)

    for index in range(len(z)):
        savez[count,index] = z[index]

    print savez

#    savetxt("wvf.dat", wvf, fmt="%7.1F")

    # Max. of first peak 
    peaklogwvf = np.amax(z)

    # Finding the first peak of org. waveform:
    for i in range(len(z)):
        if (z[i]==peaklogwvf):
            gate_firstpeak = i

    firstpeak = wvf[gate_firstpeak]
    print 'First peak at: ', gate_firstpeak,',', firstpeak


    # Retracking gate of first peak
    print 'Using', threshold_coef*100,'% threshold retracker ...'

    Pn = thermalnoise_firstpeak(wvf)
    retrack_gate_first = threshold_retracker(wvf,Pn,threshold_coef,firstpeak)

    #---------------------------------------------------------------
    # Finding the gate of max. peak:
    for i in range(len(z)):
        if (wvf[i]==maxpeak):
            gate_maxpeak = i

    print ''
    print 'Maximum peak at: ', gate_maxpeak,',', maxpeak
        # Retracking gate of max peak
    if (gate_maxpeak-gate_firstpeak > 3):
        Pnmax = thermalnoise_maxpeak(wvf,gate_firstpeak,gate_maxpeak)
    else:
        Pnmax = Pn
        print 'Thermal noise (Max. peak): ', Pnmax

    retrack_gate_max = threshold_retracker(wvf,Pnmax,threshold_coef,maxpeak)


    # Peak 2 peak bin seperation
    peak2peak = retrack_gate_max-retrack_gate_first  

    print ''
    print 'Difference between retracking gates', peak2peak
    print ''

    if (peak2peak > 1 ):
        print 'Range needs to be corrected!'
        h_peak2peak = off_range_calc(peak2peak,float(H_SAT[count]))
    else:
        print 'Range ok, NO correction is needed!'
        h_peak2peak = 0.0

    print '***********************************************'
    print ''

    h_corr[count] =    h_peak2peak 
    count = count + 1   
#---------------------------------------------------------------    
#---------------------------------------------------------------
# Loop is closed
# Height is corrected
print 'The height corrections: ', h_corr
correct_height(ifile2,ofile,lat,h_corr)    


np.savetxt("logwvf.dat", savez, fmt="%7.1F")

EDIT #2

Whao, this is embarrassing. I just found my own mistake. I did only include one digit when writing to file, when I needed at least 3.

Thank you guys for taking the time and helping me out.

Upvotes: 1

Views: 947

Answers (1)

Pierre GM
Pierre GM

Reputation: 20339

Instead of creating a (waveforms.size/len(waveforms),len(waveforms)) array of zeros, you may want to make savez a basic list [].

Then, when you loop on your waveforms, just append the new z:

savez.append(z)

Once you're done, just transform savez into an array (for example, with savez=np.array(savez)) and use this latter as input to your np.savetxt.

That way, you don't have to keep your whole savez in memory.

If you prefer to keep savez as an array all the time, at the very least, consider to get rid of

for index in range(len(z)):
 savez[count,index] = z[index]

which is seriously wasteful: you could just do a savez[count][:] = z

Upvotes: 1

Related Questions