Blame view

ceflib_mods.py 4.6 KB
fec5faec   Vernisse Yoann   Adding all alread...
1
2
#! /bin/env python
# coding: utf8
0b317600   Vernisse Yoann   added several mod...
3
4
import numpy as np
import datetime
fec5faec   Vernisse Yoann   Adding all alread...
5
6
7

class cefwrite(object) :
	
0b317600   Vernisse Yoann   added several mod...
8
	def __init__(self, data = None, filename = 'test.cef', mode = 'w', checkVariableOrder = False):
fec5faec   Vernisse Yoann   Adding all alread...
9
		"""
0b317600   Vernisse Yoann   added several mod...
10
11
12
13
14
15
		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'
fec5faec   Vernisse Yoann   Adding all alread...
16
		"""
0b317600   Vernisse Yoann   added several mod...
17
18
		self.data = {key:np.copy(data[key]) for key in data}
		
fec5faec   Vernisse Yoann   Adding all alread...
19
		self.fichier = filename
0b317600   Vernisse Yoann   added several mod...
20
21
22
23
24
25
		self.mode = mode
		
		self.variables = sorted(list(set(self.data.keys()) - set(['epoch'])))
		
		self.deleteAllNan()
		self.changeNanToFillVal()
fec5faec   Vernisse Yoann   Adding all alread...
26
		
0b317600   Vernisse Yoann   added several mod...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
		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):
fec5faec   Vernisse Yoann   Adding all alread...
64
		self.initializeHeader()
0b317600   Vernisse Yoann   added several mod...
65
		for var in self.variables : self.addvariableHeader(var)
fec5faec   Vernisse Yoann   Adding all alread...
66
		self.closeHeader()
0b317600   Vernisse Yoann   added several mod...
67
68
	
	def writeData(self):
fec5faec   Vernisse Yoann   Adding all alread...
69
70
		out = open(self.fichier, 'a')
		for i,t in enumerate(self.data['epoch']):
0b317600   Vernisse Yoann   added several mod...
71
72
73
			out.write(t.isoformat()+'Z')
			for var in self.variables :
				out.write(',  ')
fec5faec   Vernisse Yoann   Adding all alread...
74
				out.write('{:f}'.format(self.data[var][i]))
0b317600   Vernisse Yoann   added several mod...
75
			out.write(';\n')
fec5faec   Vernisse Yoann   Adding all alread...
76
77
78
		out.close()
		
	def initializeHeader(self):
fec5faec   Vernisse Yoann   Adding all alread...
79
80
81
		#set time variable as first variable
		out = open(self.fichier, 'w')
		out.write(
0b317600   Vernisse Yoann   added several mod...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
		"!------------------------------------------------------------------------|\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"
fec5faec   Vernisse Yoann   Adding all alread...
114
		'START_VARIABLE       = epoch \n'
0b317600   Vernisse Yoann   added several mod...
115
116
117
118
119
120
121
		'  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'
fec5faec   Vernisse Yoann   Adding all alread...
122
123
124
125
126
127
		)
		
	def addvariableHeader(self, variable):
	  
		out = open(self.fichier, 'a')
		out.write(
0b317600   Vernisse Yoann   added several mod...
128
129
130
131
132
133
134
		'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'
fec5faec   Vernisse Yoann   Adding all alread...
135
		'END_VARIABLE         = '+variable+ '\n'
0b317600   Vernisse Yoann   added several mod...
136
		'\n'
fec5faec   Vernisse Yoann   Adding all alread...
137
138
139
140
141
142
		)
	
	def closeHeader(self):
	  
		out = open(self.fichier, 'a')
		out.write(
0b317600   Vernisse Yoann   added several mod...
143
144
145
146
147
148
		"!------------------------------------------------------------------------|\n"
		"!                                  Data                                  |\n"
		"!------------------------------------------------------------------------|\n"
		'\n'
		'DATA_UNTIL = EOF\n'
		)
fec5faec   Vernisse Yoann   Adding all alread...
149