Pages

19 June 2013

458. Briefly: Converting GRAMS ASP ascii data to two-column ascii data

We have a couple of CARY 630 FT-IR /ATR instruments.

I hate them. Apart from being the Mac equivalent of spectrometers (if you try to do anything remotely creative you'll have a bad day. Point and click works well, most of the time), they aren't able to output data in any reasonable format.

At least not the way I'd define 'reasonable' i.e. simple x-y ascii data file and/or JCAMP-DX and/or even .csv. The default output is a binary .a2r file.

The only ascii-type format is a proprietary GRAMS ASP ascii file, for which I haven't been able to get the formal specs. Using google it seems as if the German arm of agilent did publish it, but when clicking on the links I'm told the file no longer exists, and google cache isn't playing ball.

Anyway. Luckily the format seems pretty simple.

Here are the first ten lines of an .asp file;
1798 4000.41016197344 650.579285428114 1 128 4 98.4862110783457 98.4183476284596 98.4587565715995 98.5660576694946
* The first line is the number of acquired data points
* The second line is the highest reciprocal wavelength in cm-1.
* The third line is the lowest reciprocal wavelength in cm_1.
* I don't know what the fourth and fifth lines signify. It could be dynamic resolution in the Y axis.
* The sixth line is the native resolution, i.e. 4 cm-1/data point. However, the data seems to be zero-filled, i.e. it seems the resolution is really ca 1.86 cm-1/pt.
Knowing the above, we can write a simple python script, which we'll call asp2asc, which will allow us to generate files suitable for gnuplot.
Example usage:
./asp2asc -i data.asp -o data.dat


asp2asc:
#!/usr/bin/python
#converts GRAMS ascii (asp) output from an CARY 630 FT-ATR-IR to a two-column ascii dat file
import sys

def getvars(arguments):
 exit=0
 ver=0.1
 try: 
  if "-o" in arguments:
   theoutput=arguments[arguments.index('-o')+1]
   print 'Output: %s.'%theoutput
  elif "--output" in arguments:
   theoutput=arguments[arguments.index('--output')+1]
   print 'Output: %s.'%theoutput
  else:
   print ''
   print 'Error -- no output file defined.'
   print ''
   arguments="--help"
 except:
  arguments="--help"

 try: 
  if "-i" in arguments:
   theinput=arguments[arguments.index('-i')+1]
   print 'Input: %s.'%theinput
  elif "--input" in arguments:
   theinput=arguments[arguments.index('--input')+1]
   print 'Input: %s.'%theinput
  else:
   print ''
   print 'Error -- no input file defined.'
   print ''
   arguments="--help"
 except:
  arguments="--help"

 try:
  if ("-h" in arguments) or ("--help" in arguments):
   print " "
   print "\t\tThis is asp2asc, a tool for generating converting"
   print "\t\tGRAMS ASP ascii files to two-column ascii files"
   print "\t\tThis is version",ver
   print "\tUsage:"
   print "\t-h\t--help   \tYou're looking at it."
   print "\t-i\t--input \tInput file, e.g. data.asp"
   print "\t-o\t--output \tOutput file, e.g. data.dat"
   print ""
   exit=1
 except:
  a=1   #do nothing
 
 if exit==1:
  sys.exit(0)
 print ''

 switches={'i':theinput,'o':theoutput}
 return switches

def getparams(datafile):
 params=[]
 n=1
 for line in datafile:
  try:
   params+=[int(line.rstrip('\n'))] 
  except:
   params+=[float(line.rstrip('\n'))] 
  if n==6:
   break
  n+=1 
 return params
 
def getydata(datafile):
 ydata=[]
 for line in datafile:
  ydata+=[float(line.rstrip('\n'))]
  
 return ydata
 
 
def makexdata(xpts,xmax,increment):
 n=0
 xdata=[]
 while n < xpts:
  xdata+=[xmax-n*increment]
  n+=1
 return xdata

def writexydata(outfile,xdata,ydata):
 for n in range(0,len(xdata)):
  outfile.write(str(xdata[n])+'\t'+str(ydata[n])+'\n')
 return 0

if __name__ == "__main__":
 arguments=sys.argv[1:len(sys.argv)]

 switches=getvars(arguments)
 infile=open(switches['i'],'r')
 
 params=getparams(infile) 
 ydata=getydata(infile) # needs getparams to have parked file reading at the 7th line 

 infile.close()

 xdata=makexdata(params[0],params[1],(params[1]-params[2])/(params[0]-1))

 if len(xdata)==len(ydata):
  outfile=open(switches['o'],'w')
  success=writexydata(outfile,xdata,ydata)
  outfile.close()  
 else:
  print 'Something bad happened:'
  print 'Number of X data points not equal to number of Y data points'
  print 'x pts: %i, y pts: %i'%(len(xdata),len(ydata))

Of course you could do this easily in a spreadsheet too, but I honestly find myself avoiding spreadsheet programmes like the plague ever since I learned how to use sed, gawk, and python.
Also, WHY do they make it so unnecessarily difficult to export your own data?

No comments:

Post a Comment