Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

#!/usr/bin/env python 

#------------------------------------------------------------------- 

# Filename: libgse2.py 

#  Purpose: Python wrapper for gse_functions of Stefan Stange 

#   Author: Moritz Beyreuther 

#    Email: moritz.beyreuther@geophysik.uni-muenchen.de 

# 

# Copyright (C) 2008-2012 Moritz Beyreuther 

#--------------------------------------------------------------------- 

""" 

Lowlevel module internally used for handling GSE2 files 

 

Python wrappers for gse_functions - The GSE2 library of Stefan Stange. 

Currently CM6 compressed GSE2 files are supported, this should be 

sufficient for most cases. Gse_functions is written in C and 

interfaced via python-ctypes. 

 

See: http://www.orfeus-eu.org/Software/softwarelib.html#gse 

 

:copyright: 

    The ObsPy Development Team (devs@obspy.org) 

:license: 

    GNU Lesser General Public License, Version 3 

    (http://www.gnu.org/copyleft/lesser.html) 

""" 

 

from distutils import sysconfig 

from obspy import UTCDateTime 

from obspy.core.util import c_file_p 

import ctypes as C 

import doctest 

import numpy as np 

import os 

import platform 

import warnings 

 

 

# Import shared libgse2 

# create library names 

lib_names = [ 

     # platform specific library name 

    'libgse2-%s-%s-py%s' % (platform.system(), platform.architecture()[0], 

        ''.join([str(i) for i in platform.python_version_tuple()[:2]])), 

     # fallback for pre-packaged libraries 

    'libgse2'] 

# get default file extension for shared objects 

lib_extension, = sysconfig.get_config_vars('SO') 

# initialize library 

clibgse2 = None 

for lib_name in lib_names: 

    try: 

        clibgse2 = C.CDLL(os.path.join(os.path.dirname(__file__), os.pardir, 

                                       'lib', lib_name + lib_extension)) 

    except Exception, e: 

        pass 

    else: 

        break 

if not clibgse2: 

    msg = 'Could not load shared library for obspy.gse2.\n\n %s' % (e) 

    raise ImportError(msg) 

 

 

class ChksumError(StandardError): 

    """ 

    Exception type for mismatching checksums 

    """ 

    pass 

 

 

class GSEUtiError(StandardError): 

    """ 

    Exception type for other errors in GSE_UTI 

    """ 

    pass 

 

 

# gse2 header struct 

class HEADER(C.Structure): 

    """ 

    Ctypes based GSE2 header structure for internal usage. 

    """ 

    _fields_ = [ 

        ('d_year', C.c_int), 

        ('d_mon', C.c_int), 

        ('d_day', C.c_int), 

        ('t_hour', C.c_int), 

        ('t_min', C.c_int), 

        ('t_sec', C.c_float), 

        ('station', C.c_char * 6), 

        ('channel', C.c_char * 4), 

        ('auxid', C.c_char * 5), 

        ('datatype', C.c_char * 4), 

        ('n_samps', C.c_int), 

        ('samp_rate', C.c_float), 

        ('calib', C.c_float), 

        ('calper', C.c_float), 

        ('instype', C.c_char * 7), 

        ('hang', C.c_float), 

        ('vang', C.c_float), 

    ] 

 

 

# ctypes, PyFile_AsFile: convert python file pointer/ descriptor to C file 

# pointer descriptor 

C.pythonapi.PyFile_AsFile.argtypes = [C.py_object] 

C.pythonapi.PyFile_AsFile.restype = c_file_p 

 

# reading C memory into buffer which can be converted to numpy array 

C.pythonapi.PyBuffer_FromMemory.argtypes = [C.c_void_p, C.c_int] 

C.pythonapi.PyBuffer_FromMemory.restype = C.py_object 

 

## gse_functions read_header 

clibgse2.read_header.argtypes = [c_file_p, C.POINTER(HEADER)] 

clibgse2.read_header.restype = C.c_int 

 

## gse_functions decomp_6b 

clibgse2.decomp_6b.argtypes = [ 

    c_file_p, C.c_int, 

    np.ctypeslib.ndpointer(dtype='int32', ndim=1, flags='C_CONTIGUOUS')] 

clibgse2.decomp_6b.restype = C.c_int 

 

# gse_functions rem_2nd_diff 

clibgse2.rem_2nd_diff.argtypes = [ 

    np.ctypeslib.ndpointer(dtype='int32', ndim=1, flags='C_CONTIGUOUS'), 

    C.c_int] 

clibgse2.rem_2nd_diff.restype = C.c_int 

 

# gse_functions check_sum 

clibgse2.check_sum.argtypes = [ 

    np.ctypeslib.ndpointer(dtype='int32', ndim=1, flags='C_CONTIGUOUS'), 

    C.c_int, C.c_int32] 

clibgse2.check_sum.restype = C.c_int  # do not know why not C.c_int32 

 

# gse_functions buf_init 

clibgse2.buf_init.argtypes = [C.c_void_p] 

clibgse2.buf_init.restype = C.c_void_p 

 

# gse_functions diff_2nd 

clibgse2.diff_2nd.argtypes = [ 

    np.ctypeslib.ndpointer(dtype='int32', ndim=1, flags='C_CONTIGUOUS'), 

    C.c_int, C.c_int] 

clibgse2.diff_2nd.restype = C.c_void_p 

 

# gse_functions compress_6b 

clibgse2.compress_6b.argtypes = [ 

    np.ctypeslib.ndpointer(dtype='int32', ndim=1, flags='C_CONTIGUOUS'), 

    C.c_int] 

clibgse2.compress_6b.restype = C.c_int 

 

## gse_functions write_header 

clibgse2.write_header.argtypes = [c_file_p, C.POINTER(HEADER)] 

clibgse2.write_header.restype = C.c_void_p 

 

## gse_functions buf_dump 

clibgse2.buf_dump.argtypes = [c_file_p] 

clibgse2.buf_dump.restype = C.c_void_p 

 

# gse_functions buf_free 

clibgse2.buf_free.argtypes = [C.c_void_p] 

clibgse2.buf_free.restype = C.c_void_p 

 

# module wide variable, can be imported by: 

# >>> from obspy.gse2 import gse2head 

gse2head = [_i[0] for _i in HEADER._fields_] 

 

 

def isGse2(f): 

    """ 

    Checks whether a file is GSE2 or not. Returns True or False. 

 

    :type f : file pointer 

    :param f : file pointer to start of GSE2 file to be checked. 

    """ 

    pos = f.tell() 

    widi = f.read(4) 

    f.seek(pos) 

    if widi != 'WID2': 

        raise TypeError("File is not in GSE2 format") 

 

 

def writeHeader(f, head): 

    """ 

    Rewriting the write_header Function of gse_functions.c 

 

    Different operating systems are delivering different output for the 

    scientific format of floats (fprinf libc6). Here we ensure to deliver 

    in a for GSE2 valid format independent of the OS. For speed issues we 

    simple cut any number ending with E+0XX or E-0XX down to E+XX or E-XX. 

    This fails for numbers XX>99, but should not occur. 

 

    :type f: File pointer 

    :param f: File pointer to to GSE2 file to write 

    :type head: Ctypes struct 

    :param head: Ctypes structure to write 

    """ 

    calib = "%10.2e" % (head.calib) 

    header = "WID2 %4d/%02d/%02d %02d:%02d:%06.3f %-5s %-3s %-4s %-3s %8d " + \ 

             "%11.6f %s %7.3f %-6s %5.1f %4.1f\n" 

    f.write(header % ( 

            head.d_year, 

            head.d_mon, 

            head.d_day, 

            head.t_hour, 

            head.t_min, 

            head.t_sec, 

            head.station, 

            head.channel, 

            head.auxid, 

            head.datatype, 

            head.n_samps, 

            head.samp_rate, 

            calib, 

            head.calper, 

            head.instype, 

            head.hang, 

            head.vang)) 

 

 

def uncompress_CM6(f, n_samps): 

    """ 

    Uncompress n_samps of CM6 compressed data from file pointer fp. 

 

    :type f: File Pointer 

    :param f: File Pointer 

    :type n_samps: Int 

    :param n_samps: Number of samples 

    """ 

    # transform to a C file pointer 

    fp = C.pythonapi.PyFile_AsFile(f) 

    data = np.empty(n_samps, dtype='int32') 

    n = clibgse2.decomp_6b(fp, n_samps, data) 

    if n != n_samps: 

        raise GSEUtiError("Mismatching length in lib.decomp_6b") 

    clibgse2.rem_2nd_diff(data, n_samps) 

    return data 

 

 

def verifyChecksum(fh, data, version=2): 

    """ 

    Calculate checksum from data, as in gse_driver.c line 60 

 

    :type fh: File Pointer 

    :param fh: File Pointer 

    :type version: Int 

    :param version: GSE version, either 1 or 2, defaults to 2. 

    """ 

    chksum_data = clibgse2.check_sum(data, len(data), C.c_int32(0)) 

    # find checksum within file 

    buf = fh.readline() 

    chksum_file = 0 

    CHK_LINE = 'CHK%d' % version 

    while buf: 

        if buf.startswith(CHK_LINE): 

            chksum_file = int(buf.strip().split()[1]) 

            break 

        buf = fh.readline() 

    if chksum_data != chksum_file: 

        # 2012-02-12, should be deleted in a year from now 

        if abs(chksum_data) == abs(chksum_file): 

            msg = "Checksum differs only in absolute value. If this file " + \ 

                "was written with ObsPy GSE2, this is due to a bug in " + \ 

                "the obspy.gse2.write routine (resolved with [3431]), " + \ 

                "and thus this message can be safely ignored." 

            warnings.warn(msg, UserWarning) 

            return 

        msg = "Mismatching checksums, CHK %d != CHK %d" 

        raise ChksumError(msg % (chksum_data, chksum_file)) 

    return 

 

 

def read(f, verify_chksum=True): 

    """ 

    Read GSE2 file and return header and data. 

 

    Currently supports only CM6 compressed GSE2 files, this should be 

    sufficient for most cases. Data are in circular frequency counts, for 

    correction of calper multiply by 2PI and calper: data * 2 * pi * 

    header['calper']. 

 

    :type f: File Pointer 

    :param f: Open file pointer of GSE2 file to read, opened in binary mode, 

              e.g. f = open('myfile','rb') 

    :type test_chksum: Bool 

    :param verify_chksum: If True verify Checksum and raise Exception if it 

                          is not correct 

    :rtype: Dictionary, Numpy.ndarray int32 

    :return: Header entries and data as numpy.ndarray of type int32. 

    """ 

    fp = C.pythonapi.PyFile_AsFile(f) 

    head = HEADER() 

    errcode = clibgse2.read_header(fp, C.pointer(head)) 

    if errcode != 0: 

        raise GSEUtiError("Error in lib.read_header") 

    data = uncompress_CM6(f, head.n_samps) 

    # test checksum only if enabled 

    if verify_chksum: 

        verifyChecksum(f, data, version=2) 

    headdict = {} 

    for i in head._fields_: 

        headdict[i[0]] = getattr(head, i[0]) 

    # cleaning up 

    del fp, head 

    return headdict, data 

 

 

def write(headdict, data, f, inplace=False): 

    """ 

    Write GSE2 file, given the header and data. 

 

    Currently supports only CM6 compressed GSE2 files, this should be 

    sufficient for most cases. Data are in circular frequency counts, for 

    correction of calper multiply by 2PI and calper: 

    data * 2 * pi * header['calper']. 

 

    Warning: The data are actually compressed in place for performance 

    issues, if you still want to use the data afterwards use data.copy() 

 

    :note: headdict dictionary entries C{'datatype', 'n_samps', 

           'samp_rate'} are absolutely necessary 

    :type data: numpy.ndarray dtype int32 

    :param data: Contains the data. 

    :type f: File Pointer 

    :param f: Open file pointer of GSE2 file to write, opened in binary 

              mode, e.g. f = open('myfile','wb') 

    :type inplace: Bool 

    :param inplace: If True, do compression not on a copy of the data but 

                    on the data itself --- note this will change the data 

                    values and make them therefore unusable 

    :type headdict: Dictionary 

    :param headdict: Header containing the following entries:: 

        'd_year': int, 

        'd_mon': int, 

        'd_mon': int, 

        'd_day': int, 

        't_hour': int, 

        't_min': int, 

        't_sec': float, 

        'station': char*6, 

        'station': char*6, 

        'channel': char*4, 

        'auxid': char*5, 

        'datatype': char*4, 

        'n_samps': int, 

        'samp_rate': float, 

        'calib': float, 

        'calper': float, 

        'instype': char*7, 

        'hang': float, 

        'vang': float 

    """ 

    fp = C.pythonapi.PyFile_AsFile(f) 

    n = len(data) 

    clibgse2.buf_init(None) 

    # 

    chksum = clibgse2.check_sum(data, n, C.c_int32(0)) 

    # Maximum values above 2^26 will result in corrupted/wrong data! 

    # do this after chksum as chksum does the type checking for numpy array 

    # for you 

    if not inplace: 

        data = data.copy() 

    if data.max() > 2 ** 26: 

        raise OverflowError("Compression Error, data must be less equal 2^26") 

    clibgse2.diff_2nd(data, n, 0) 

    ierr = clibgse2.compress_6b(data, n) 

    assert ierr == 0, "Error status after compression is NOT 0 but %d" % ierr 

    # set some defaults if not available and convert header entries 

    headdict.setdefault('datatype', 'CM6') 

    headdict.setdefault('vang', -1) 

    headdict.setdefault('calper', 1.0) 

    headdict.setdefault('calib', 1.0) 

    head = HEADER() 

    for _i in headdict.keys(): 

        if _i in gse2head: 

            setattr(head, _i, headdict[_i]) 

    # This is the actual function where the header is written. It avoids 

    # the different format of 10.4e with fprintf on Windows and Linux. 

    # For further details, see the __doc__ of writeHeader 

    writeHeader(f, head) 

    clibgse2.buf_dump(fp) 

    f.write("CHK2 %8ld\n\n" % chksum) 

    clibgse2.buf_free(None) 

    del fp, head 

 

 

def readHead(f): 

    """ 

    Return (and read) only the header of gse2 file as dictionary. 

 

    Currently supports only CM6 compressed GSE2 files, this should be 

    sufficient for most cases. 

 

    :type file: File Pointer 

    :param file: Open file pointer of GSE2 file to read, opened in binary 

                 mode, e.g. f = open('myfile','rb') 

    :rtype: Dictionary 

    :return: Header entries. 

    """ 

    fp = C.pythonapi.PyFile_AsFile(f) 

    head = HEADER() 

    clibgse2.read_header(fp, C.pointer(head)) 

    headdict = {} 

    for i in head._fields_: 

        headdict[i[0]] = getattr(head, i[0]) 

    del fp, head 

    return headdict 

 

 

def getStartAndEndTime(f): 

    """ 

    Return start and endtime/date of GSE2 file 

 

    Currently supports only CM6 compressed GSE2 files, this should be 

    sufficient for most cases. 

 

    :type f: File Pointer 

    :param f: Open file pointer of GSE2 file to read, opened in binary 

              mode, e.g. f = open('myfile','rb') 

    :rtype: List 

    :return: C{[startdate,stopdate,startime,stoptime]} Start and Stop time as 

             Julian seconds and as date string. 

    """ 

    fp = C.pythonapi.PyFile_AsFile(f) 

    head = HEADER() 

    clibgse2.read_header(fp, C.pointer(head)) 

    seconds = int(head.t_sec) 

    microseconds = int(1e6 * (head.t_sec - seconds)) 

    startdate = UTCDateTime(head.d_year, head.d_mon, head.d_day, 

                            head.t_hour, head.t_min, seconds, microseconds) 

    stopdate = UTCDateTime(startdate.timestamp + 

                           head.n_samps / float(head.samp_rate)) 

    del fp, head 

    return [startdate, stopdate, startdate.timestamp, stopdate.timestamp] 

 

 

if __name__ == '__main__': 

    doctest.testmod(exclude_empty=True)