title: convert csv to netcdf
body: <p>I am trying to convert a .csv file to a netCDF4 via Python but I am having trouble figuring out how I can store information from a .csv table format into a netCDF.  My main concern is how do we declare the variables from the columns into a workable netCDF4 format?  Everything I have found is normally extracting information from a netCDF4 to a .csv or ASCII.  I have provided the sample data, sample code, and my errors for declaring the appropriate arrays.  Any help would be much appreciated.</p>
  
  <p>The sample table is below: </p>
  
  <pre><code>Station Name    Country  Code   Lat Lon mn.yr   temp1   temp2   temp3   hpa 
  Somewhere   US  12340   35.52   23.358  1.19    -8.3    -13.1   -5  69.5
  Somewhere   US  12340           2.1971  -10.7   -13.9   -7.9    27.9
  Somewhere   US  12340           3.1971  -8.4    -13 -4.3    90.8
  </code></pre>
  
  <p>My sample code is:</p>
  
  <p><strong>#!/usr/bin/env python</strong></p>
  
  <pre><code>import scipy
  import numpy
  import netCDF4
  import csv
  
  from numpy import arange, dtype 
  </code></pre>
  
  <p><strong>#Declare empty arrays</strong></p>
  
  <pre><code>v1 = []
  v2 = []
  v3 = []
  v4 = []
  </code></pre>
  
  <p><strong># Open csv file and declare variable for arrays for each heading</strong></p>
  
  <pre><code>f = open('station_data.csv', 'r').readlines()
  
  for line in f[1:]:
      fields = line.split(',')
      v1.append(fields[0]) #station
      v2.append(fields[1])#country
      v3.append(int(fields[2]))#code
      v4.append(float(fields[3]))#lat
      v5.append(float(fields[3]))#lon
  #more variables included but this is just an abridged list
  print v1
  print v2
  print v3
  print v4
  </code></pre>
  
  <p><strong>#convert to netcdf4 framework that works as a netcdf</strong> </p>
  
  <pre><code>ncout = netCDF4.Dataset('station_data.nc','w') 
  </code></pre>
  
  <p><strong># latitudes and longitudes. Include NaN for missing numbers</strong> </p>
  
  <pre><code>lats_out = -25.0 + 5.0*arange(v4,dtype='float32')
  lons_out = -125.0 + 5.0*arange(v5,dtype='float32')
  </code></pre>
  
  <p><strong># output data.</strong></p>
  
  <pre><code>press_out = 900. + arange(v4*v5,dtype='float32') # 1d array
  press_out.shape = (v4,v5) # reshape to 2d array
  temp_out = 9. + 0.25*arange(v4*v5,dtype='float32') # 1d array
  temp_out.shape = (v4,v5) # reshape to 2d array
  </code></pre>
  
  <p><strong># create the lat and lon dimensions.</strong></p>
  
  <pre><code>ncout.createDimension('latitude',v4)
  ncout.createDimension('longitude',v5)
  </code></pre>
  
  <p><strong># Define the coordinate variables. They will hold the coordinate information</strong></p>
  
  <pre><code>lats = ncout.createVariable('latitude',dtype('float32').char,('latitude',))
  lons = ncout.createVariable('longitude',dtype('float32').char,('longitude',))
  </code></pre>
  
  <p><strong># Assign units attributes to coordinate var data. This attaches a text attribute to each of the coordinate variables, containing the units.</strong></p>
  
  <pre><code>lats.units = 'degrees_north'
  lons.units = 'degrees_east'
  </code></pre>
  
  <p><strong># write data to coordinate vars.</strong></p>
  
  <pre><code>lats[:] = lats_out
  lons[:] = lons_out
  </code></pre>
  
  <p><strong># create the pressure and temperature variables</strong> </p>
  
  <pre><code>press = ncout.createVariable('pressure',dtype('float32').char,('latitude','longitude'))
  temp = ncout.createVariable('temperature',dtype('float32').char,'latitude','longitude'))
  </code></pre>
  
  <p><strong># set the units attribute.</strong></p>
  
  <pre><code>press.units =  'hPa'
  temp.units = 'celsius'
  </code></pre>
  
  <p><strong># write data to variables.</strong></p>
  
  <pre><code>press[:] = press_out
  temp[:] = temp_out
  
  ncout.close()
  f.close()
  </code></pre>
  
  <p>error:  </p>
  
  <pre><code>Traceback (most recent call last):
    File "station_data.py", line 33, in &lt;module&gt;
      v4.append(float(fields[3]))#lat
  ValueError: could not convert string to float: 
  </code></pre>