1

これはかなり簡単だと思いますが、うまくいきません。リスト(1x128)を提供する機能があります。関数はループ (1x32) 内にあります。関数からすべてのリスト (32x128) をファイルに書き込みたい。これはコードです:

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") 

z の 32 個のリストすべてを含むファイルが得られないのはなぜですか?

編集(メインコードを含めると役立ちますか?):

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")

編集#2

うわー、これは恥ずかしいです。私は自分の間違いを見つけただけです。ファイルに書き込むときは、少なくとも 3 桁が必要だったのに、1 桁しか含めませんでした。

時間を割いて助けてくれてありがとう。

4

1 に答える 1

1

ゼロの配列を作成する代わりに、基本的なリスト(waveforms.size/len(waveforms),len(waveforms))を作成したい場合があります。savez[]

次に、をループするときにwaveforms、新しいを追加するだけzです:

savez.append(z)

完了したらsavez、配列に変換し (たとえば、 をsavez=np.array(savez)使用)、この後者を への入力として使用しますnp.savetxt

savezそうすれば、全体をメモリに保持する必要はありません。

常に配列として保持したい場合はsavez、少なくとも、削除することを検討してください

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

これは非常に無駄です。savez[count][:] = z

于 2012-09-19T13:28:43.727 に答える