1

I have five files that I want to loop through so I can get all of the density dens values at the end. The code is the same except where I have i. The issue I'm having is that when I do print(dens) outside of the loop I am only getting the values from the last file (the fifth one). How do I fix the code to have all of the values from all of the files? I'm thinking I need to change dens.append at the end of the code but I'm not sure how. Just for reference this code is to interpolation between latitudes/longitudes to get density values.

for i in range(0,5):
    dens = [] # creating an empty list in order to make dens array from fifth day model output at the end of the for loop 
    for x, y in zip(satlong, satlat): # creating a loop to go through both satellite lat and long arrays 
    
        decindx_long = x/2 
    
        # model longitude parameter values at four corners of grid 
        v1_long = int(decindx_long)
        v3_long = int(decindx_long)
        v2_long = v1_long + 1
        v4_long = v1_long + 1
    
        # fraction for the longitude
        f = decindx_long - v1_long
    
        decindx_lat = (y - (-90))/2
    
        # model latitude parameter values at four corners of grid
        v1_lat = int(decindx_lat)
        v2_lat = int(decindx_lat)
        v3_lat = v1_lat + 1
        v4_lat = v1_lat + 1
    
        # fraction for the latitude 
        g = decindx_lat - v1_lat
    
        # parameter value between longitude grid values at latitude 1
        vp12 = density[i,v1_lat,v1_long] + f*((density[i, v2_lat, v2_long]) - (density[i, v1_lat, v1_long]))
    
        # parameter value between longitude grid values at latitude 2
        vp34 = density[i,v4_lat,v4_long] + f*((density[i,v4_lat,v4_long]) - (density[i,v3_lat,v3_long]))
    
        # value of parameter: the result of interpolation
        vp = vp12 + g*(vp34 - vp12)
        # creating density array for ephemeris data that was just interpolated
        dens.append(vp)
1
  • You reset dens inside of each iteration of your i loop. Maybe you want a nested list or a dict (created before the first loop) to hold all of the results? Commented Sep 10, 2020 at 16:01

1 Answer 1

1

Create dens = [] above the for loop. You create it every time you take a new file to process, therefore only the last one is taken into consideration.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.