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?
precip_subsethave the shapes you mentioned? Print its shape out.precip_subsetof the 12 files.