ceflib_mods.py 1.61 KB
#! /bin/env python
# coding: utf8

class cefwrite(object) :
	
	def __init__(self, data = None, filename = 'test.cef'):
		"""
		Data of the form data['field1'] = numpy array, and data['epoch'] = numpy time array
		"""
		self.data = data
		self.fichier = filename
		
		self.initializeHeader()
		variables = sorted(list(set(self.data.keys()) - set(['epoch'])))
		for var in variables : self.addvariableHeader(var)
		self.closeHeader()
		
		out = open(self.fichier, 'a')
		for i,t in enumerate(self.data['epoch']):
			
			out.write(t.isoformat())
			#print >> out, t.isoformat()
			for var in variables :
				out.write('  ')
				#print >> out, str(self.data[key][i])
				out.write('{:f}'.format(self.data[var][i]))
			out.write('\n')
		
		out.close()
		
	def initializeHeader(self):
		
		#set time variable as first variable
		out = open(self.fichier, 'w')
		out.write(
		'START_VARIABLE       = epoch \n'
		'  PARAMETER_TYPE     = "Support_Data" \n'
		'  VALUE_TYPE         = ISO_TIME \n'
		'  UNITS              = "s" \n'
		'  DELTA_PLUS         = 0 \n'
		'  DELTA_MINUS        = 0 \n'
		'END_VARIABLE         = epoch \n'
		' \n'
		)
		
	def addvariableHeader(self, variable):
	  
		out = open(self.fichier, 'a')
		out.write(
		'START_VARIABLE     = '+variable +'\n'
		'  PARAMETER_TYPE     = "Data" \n'
		'  VALUE_TYPE         = FLOAT \n'
		'  FILLVAL            =  -1.000E+31 \n'
		'  DEPEND_0           = epoch \n'
		'  UNITS              = "" \n'
		'  FIELDNAM           = "" \n'
		'END_VARIABLE         = '+variable+ '\n'
		' \n'
		)
	
	def closeHeader(self):
	  
		out = open(self.fichier, 'a')
		out.write(
		  'DATA_UNTIL = EOF \n'
		  )