-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmakedata.py
113 lines (97 loc) · 3.44 KB
/
makedata.py
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
import struct
import numpy as np
import time
import bluefile
def pack_list(data_list,file_format):
if file_format[1] in ("F","f"):
return struct.pack('f' * len(datalist),*datalist)
elif file_format[1] in ("I","i"):
return struct.pack('h' * len(datalist),*datalist)
elif file_format[1] in ("D","d"):
return struct.pack('d' * len(datalist),*datalist)
elif file_format[1] in ("L","l"):
return struct.pack('i' * len(datalist),*datalist)
elif file_format[1] in ("B","b"):
return struct.pack('b' * len(datalist),*datalist)
def make_2d_data(x,y,file_format):
""" makes fake 2D data where the data set returned is x by y in size and the value of each value is x-y """
complex_file = False
if file_format[0] in ("C","c"):
complex_file = True
data = []
for yy in range(y):
data.append([])
if (yy % 25) == 0:
for xx in range(x):
data[yy].append(abs(yy))
if complex_file: #If data is complex, create another entry of the same value.
data[yy].append((yy))
else:
for xx in range(x):
data[yy].append((xx-yy)%127)
if complex_file: #If data is complex, create another entry of the same value.
data[yy].append((xx-yy))
return data
def make_2d_data_np(x,y,file_format):
""" makes fake 2D data where the data set returned is x by y in size and the value of each value is x-y """
complex_file = False
if file_format[0] in ("C","c"):
complex_file = True
data = []
for yy in range(y):
data.append(np.empty(x))
if (yy < (x/6)) :
print "Making 0 line"
for xx in range(x):
if not(complex_file):
data[yy][xx]=(0)
else:
data[yy][xx]=(0+0j)
elif (yy >= (x*5/6)) :
print "Making 10 line"
for xx in range(x):
if not(complex_file):
data[yy][xx]=(10)
else:
data[yy][xx]=(10+10j)
else:
for xx in range(x):
if not(complex_file):
data[yy][xx]=(xx/6)
else:
data[yy][xx]=(xx/6)*(1+1j)
return data
def make_midas_header(xfile,yfile,file_format):
hdr = bluefile.header(type=2000, format=file_format,subsize=xfile)
#hdr, data = bluefile.read('mydata_SL_500_1000.tmp')
hdr['xstart']=0
hdr['ystart']=0
hdr['xdelta']=1
hdr['ydelta']=1
#hdr['subsize']=xfile
#hdr['size']=yfile
# hdr['Version']="BLUE"
# hdr['head_rep']="EEEI"
# hdr['data_rep']="EEEI"
return hdr
if __name__ == "__main__":
xfile = 480
yfile = 480
file_format = "SP"
filename = "mydata_%s_%s_%s" %(file_format,xfile,yfile)
blue = True
if blue:
bluefile.set_type2000_format(np.ndarray)
filename = filename+".tmp"
hdr = make_midas_header(xfile,yfile,file_format)
data = make_2d_data_np(xfile,yfile,file_format)
print("len data", len(data), len(data[0]),type(data),type(data[0]))
bluefile.write(filename, hdr, data)
else:
data = make_2d_data(xfile,yfile,file_format)
f= open(filename,"a+")
for datalist in data:
# time.sleep(0.1)
binarydata = pack_list(datalist,file_format)
f.write(binarydata)
f.close()