When preparing files for the ncDataReader2 I often have to convert text files that contain values separated by TABs or commas. In most simple cases this can be done with some lines of python code. An example is here:
import sys, pupynere ll = [l.strip().split(',') for l in open(sys.argv) if not l.startswith('#')] vv = zip(*[map(float, l) for l in ll]) nc = pupynere.netcdf_file(sys.argv+'.nc', 'w') nc.createDimension('dim', None) for i in range(len(vv)): nc.createVariable('var_%02d' % i, 'd', ('dim',))[:] = vv[i]
This short program will read the column data separated by commas, skip comment lines starting with # and write a netCDF file, all in 7 lines of code! Pupynere is one of the netCDF-modules available for python. It is written in pure python and has an API which is compatible with ‘Scientific.IO.NetCDF’. This simple approach works for small, well-formed files without headers. For all other cases I’m working on more sophisticated tool which uses the CSV module of python.
Can you please upload a sample tab or comma separated file that we can use in this code?
A simple example looks like this:
# this is a comment