1

I am using a for loop to open 12 files in a directory, each representing a month in the year, with shapes as shown:

(31, 180, 140)
(28, 180, 140)
(31, 180, 140)
(30, 180, 140)
(31, 180, 140)
(30, 180, 140)
(31, 180, 140)
(31, 180, 140)
(30, 180, 140)
(31, 180, 140)
(30, 180, 140)
(31, 180, 140)

I was trying to use append to combine these files into one list, as shown:

directory = r"C:\Users\matth\Downloads\TRMM_3B42RT"
for root, dirs, filenames in os.walk(directory):
    precip_subsetland2010 = []
    for f in filenames:
        if f.startswith("3B42RT_Daily.2010"):
            log = open(os.path.join(root, f), 'r')
            datapath2 = (("C:\\Users\\matth\\Downloads\\TRMM_3B42RT\\") + f)
            f = Dataset(datapath2)

            latbounds = [ -45 , -10 ]
            lonbounds = [ 105, 150 ] 
            lats = f.variables['lat'][:] 
            lons = f.variables['lon'][:]

            # latitude lower and upper index
            latli = np.argmin( np.abs( lats - latbounds[0] ) )
            latui = np.argmin( np.abs( lats - latbounds[1] ) ) 

            # longitude lower and upper index
            lonli = np.argmin( np.abs( lons - lonbounds[0] ) )
            lonui = np.argmin( np.abs( lons - lonbounds[1] ) )

            precip_subset = f.variables['precipitation'][ : , lonli:lonui , latli:latui ]
            precip_subsetland2010.append(precip_subset)
            precipsubsetland2010 = np.asarray(precip_subsetland2010)
            print(precipsubsetland2010.shape)

However, I get an output as shown:

OUTPUT:

(1, 31, 180, 140)
(2,)
(3,)
(4,)
(5,)
(6,)
(7,)
(8,)
(9,)
(10,)
(11,)
(12,)

I would ultimately like to add the data to a list, then convert into an array with a shape of (365, 180, 140). How can I achieve this? Is this the correct application of append?

3
  • Do you guys need any more info to answer this? Commented Aug 6, 2017 at 22:23
  • Does precip_subset have the shapes you mentioned? Print its shape out. Commented Aug 6, 2017 at 22:26
  • @cᴏʟᴅsᴘᴇᴇᴅ: The list of shapes I printed at the beginning are the shapes of precip_subset of the 12 files. Commented Aug 6, 2017 at 22:34

1 Answer 1

2

Inside the loop, just append precip_subset to your list:

precip_subsetland2010.append(precip_subset)

Outside the loop, call np.vstack, to vertically stack your data.

output = np.vstack(precip_subsetland2010)

Printing output.shape should give you something like (X, 180, 140) (X being the sum of all rows of the constituent arrays).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.