title: How to read NetCDF file and write to CSV using Python
body: <p>My aim is to access data from a netcdf file and write to a CSV file in the following format. </p>
  
  <pre><code>Latitude  Longitude Date1  Date2  Date3
  100       200       &lt;-- MIN_SFC values --&gt;
  </code></pre>
  
  <p>So far I have accessed the variables, written the header to the file and populated the lat/lons.</p>
  
  <p>How can I access the MIN_SFC values for specified lon,lat coordinates and dates and then write to a CSV file.   </p>
  
  <p>I'm a python newbie if there is a better way to go about this please let me know.</p>
  
  <p>NetCDF file info:</p>
  
  <pre><code>Dimensions:
    time = 7 
    latitude = 292
    longitude =341
  
  Variables:
    float MIN_SFC (time=7, latitude = 292, longitude = 341)
  </code></pre>
  
  <p>Here's what I've tried:
  <div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> from netCDF4 import Dataset, num2date
  
   filename = "C:/filename.nc"
  
   nc = Dataset(filename, 'r', Format='NETCDF4')
   print nc.variables
  
   print 'Variable List'
  
   for var in nc.variables:
      print var, var.units, var.shape
  
   # get coordinates variables
   lats = nc.variables['latitude'][:]
   lons = nc.variables['longitude'][:]
  
   sfc= nc.variables['Min_SFC'][:]
   times = nc.variables['time'][:]
  
   # convert date, how to store date only strip away time?
   print "Converting Dates"
   units = nc.variables['time'].units
   dates = num2date (times[:], units=units, calendar='365_day')
  
   #print [dates.strftime('%Y%m%d%H') for date in dates]
  
   header = ['Latitude', 'Longitude']
  
   # append dates to header string
  
   for d in dates:
      print d
      header.append(d)
  
   # write to file
   import csv
  
   with open('Output.csv', 'wb') as csvFile:
      outputwriter = csv.writer(csvFile, delimiter=',')
      outputwriter.writerow(header)
      for lat, lon in zip(lats, lons):
        outputwriter.writerow( [lat, lon] )
   
   # close the output file
   csvFile.close()
  
   # close netcdf
   nc.close()</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I've updated the code that writes the CSV file, there's an attribute error, because the lat/lon are doubles.</p>
  
  <p>AttributeError: 'numpy.float32' object has no attribute 'append'</p>
  
  <p>Any way to cast to a string in python? Do you think it'll work?</p>
  
  <p>I've noticed a number of values returned as "--" when I printed values to the console. I'm wondering if this represents the fillValue or missingValue defined as -32767.0. </p>
  
  <p>I'm also wondering whether the variables of the 3d dataset should be accessed by lats = nc.variables['latitude'][:][:] or lats = nc.variables['latitude'][:][:,:] ?  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code># the csv file is closed when you leave the block
  with open('output.csv', 'wb') as csvFile:
      outputwriter = csv.writer(csvFile, delimiter=',')
      for time_index, time in enumerate(times): # pull the dates out for the header
           t = num2date(time, units = units, calendar='365_day')
           header.append(t)
      outputwriter.writerow(header)  
      for lat_index, lat in enumerate(lats):
          content = lat
          print lat_index
          for lon_index, lon in enumerate(lons):
              content.append(lon)
              print lon_index    
              for time_index, time in enumerate(times): # for a date
                  # pull out the data 
                  data = sfc[time_index,lat_index,lon_index]
                  content.append(data)
                  outputwriter.writerow(content)</code></pre>
  </div>
  </div>
  </p>