ceflib_mods.py 4.6 KB
#! /bin/env python
# coding: utf8
import numpy as np
import datetime

class cefwrite(object) :
	
	def __init__(self, data = None, filename = 'test.cef', mode = 'w', checkVariableOrder = False):
		"""
		Write data into a cef file format
		data must be of the form data['field1'] = numpy array, and data['epoch'] = numpy time array
		provide filename (default is test.cef)
		mode : 'w' start from fresh empty file
		       'a' start from existing file and add only lines (variables must have same names)
		default is 'w'
		"""
		self.data = {key:np.copy(data[key]) for key in data}
		
		self.fichier = filename
		self.mode = mode
		
		self.variables = sorted(list(set(self.data.keys()) - set(['epoch'])))
		
		self.deleteAllNan()
		self.changeNanToFillVal()
		
		if checkVariableOrder : print self.variables
		
		if mode == 'w': 
			self.makeHeader()
			self.writeData()
		elif mode == 'a':
			self.writeData()
	
	def changeNanToFillVal(self):
		"""
		For CL : change NAN to -10E31
		seems to work fine :)
		"""
		for key in self.variables : self.data[key][np.isnan(self.data[key])] = -10**31
	
	def deleteAnyNan(self):
		"""
		Delete all lines containing at least one np.nan
		"""
		for var in self.variables :
			inds = np.where(np.isnan(self.data[var]))
			for key in self.data :
				self.data[key] = np.delete(self.data[key], inds)

	def deleteAllNan(self):
		"""
		Delete all lines where all data exept time are np.nan
		"""
		inds0 = set(np.where(np.isnan(self.data[self.variables[0]]))[0])
		for var in self.variables[1:] :
			inds1 = set(np.where(np.isnan(self.data[var]))[0]).intersection(inds0)
			inds0 = inds1
			
		for key in self.data :
			self.data[key] = np.delete(self.data[key], list(inds1))

	def makeHeader(self):
		self.initializeHeader()
		for var in self.variables : self.addvariableHeader(var)
		self.closeHeader()
	
	def writeData(self):
		out = open(self.fichier, 'a')
		for i,t in enumerate(self.data['epoch']):
			out.write(t.isoformat()+'Z')
			for var in self.variables :
				out.write(',  ')
				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(
		"!------------------------------------------------------------------------|\n"
		"!                                                                        |\n"
		"! Generated by the mms_tools_lib, developped by Yoann Vernisse, IRAP     |\n"
		"! ASCII Format                                                           |\n"
		"!------------------------------------------------------------------------|\n"
		"\n"
		'FILE_FORMAT_VERSION  = "CEF-2.0"\n'
		"\n"
		"\n"
		"!------------------------------------------------------------------------|\n"
		"!                            Global Metadata                             |\n"
		"!------------------------------------------------------------------------|\n"
		"\n"
		"START_META           = Generation_date\n"
		"  VALUE_TYPE         = ISO_TIME\n"
		"  ENTRY              = "+ datetime.datetime.now().isoformat() +"\n"
		"END_META             = Generation_date\n"
		"\n"
		"START_META           = Generated_by\n"
		'  ENTRY              = "mms_tools_lib"\n'
		"END_META             = Generated_by\n"
		"\n"
		"START_META           = Mission_Group\n"
		'  ENTRY              = "MMS"\n'
		"END_META             = Mission_Group\n"
		"\n"
		'END_OF_RECORD_MARKER = ";"\n'
		"\n"  
		"!------------------------------------------------------------------------|\n"
		"!                               Variables                                |\n"
		"!------------------------------------------------------------------------|\n"
		"\n"
		'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(
		"!------------------------------------------------------------------------|\n"
		"!                                  Data                                  |\n"
		"!------------------------------------------------------------------------|\n"
		'\n'
		'DATA_UNTIL = EOF\n'
		)