ceflib_mods.py
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /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'
)