title: Combine multiple NetCDF files into timeseries multidimensional array python
body: <p>I am using data from multiple netcdf files (in a folder on my computer). Each file holds data for the entire USA, for a time period of 5 years. Locations are referenced based on the index of an x and y coordinate. I am trying to create a time series for multiple locations(grid cells), compiling the 5 year periods into a 20 year period (this would be combining 4 files).  Right now I am able to extract the data from all files for one location and compile this into an array using numpy append. However, I would like to extract the data for multiple locations, placing this into a matrix where the rows are the locations and the columns contain the time series precipitation data. I think I have to create a list or dictionary, but I am not really sure how to allocate the data to the list/dictionary within a loop. </p>
  
  <p>I am new to python and netCDF, so forgive me if this is an easy solution. I have been using this code as a guide, but haven't figured out how to format it for what I'd like to do: <a href="https://stackoverflow.com/questions/18939210/python-reading-multiple-netcdf-rainfall-files-of-variable-size?newreg=63b025fb70454ce99c76846c5091694f">Python Reading Multiple NetCDF Rainfall files of variable size</a></p>
  
  <p>Here is my code: </p>
  
  <pre><code>import glob
  from netCDF4 import Dataset
  import numpy as np
  
  # Define x &amp; y index for grid cell of interest 
      # Pittsburgh is 37,89
  yindex = 37  #first number
  xindex = 89  #second number
  
  # Path
  path = '/Users/LMC/Research Data/NARCCAP/'  
  folder = 'MM5I_ccsm/'
  
  ## load data file names    
  all_files = glob.glob(path + folder+'*.nc')
  all_files.sort()
  
  ## initialize np arrays of timeperiods and locations
  yindexlist = [yindex,'38','39'] # y indices for all grid cells of interest
  xindexlist = [xindex,xindex,xindex] # x indices for all grid cells of interest
  ngridcell = len(yindexlist)
  ntimestep = 58400  # This is for 4 files of 14600 timesteps
  
  ## Initialize np array
  timeseries_per_gridcell = np.empty(0)
  
  ## START LOOP FOR FILE IMPORT
  for timestep, datafile in enumerate(all_files):    
      fh = Dataset(datafile,mode='r')  
      days = fh.variables['time'][:]
      lons = fh.variables['lon'][:]
      lats = fh.variables['lat'][:]
      precip = fh.variables['pr'][:]
  
      for i in range(1):
          timeseries_per_gridcell = np.append(timeseries_per_gridcell,precip[:,yindexlist[i],xindexlist[i]]*10800)
  
      fh.close()
  
  print timeseries_per_gridcell     
  </code></pre>
  
  <p>I put 3 files on dropbox so you could access them, but I am only allowed to post 2 links. Here are they are: </p>
  
  <p><a href="https://www.dropbox.com/s/rso0hce8bq7yi2h/pr_MM5I_ccsm_2041010103.nc?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/rso0hce8bq7yi2h/pr_MM5I_ccsm_2041010103.nc?dl=0</a>
  <a href="https://www.dropbox.com/s/j56undjvv7iph0f/pr_MM5I_ccsm_2046010103.nc?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/j56undjvv7iph0f/pr_MM5I_ccsm_2046010103.nc?dl=0</a></p>