chap06/helix.py

サンプルコードのダウンロード

 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3
 4fn = "helix2.dat"
 5
 6try:
 7    # use numpy/scipy if available
 8    import numpy as np
 9    import scipy as sp
10    from scipy import io
11
12    # open file in fortran unformatted binary mode
13    fp = io.FortranFile(fn, "r")
14
15    # read x
16    x  = fp.read_record(np.float64)
17
18    # read y
19    y  = fp.read_record(np.float64)
20
21    # read z
22    z  = fp.read_record(np.float64)
23
24except:
25    # instead pure python implementation
26    import array
27
28    # prepare array objects
29    x  = array.array('d')
30    y  = array.array('d')
31    z  = array.array('d')
32
33    # open file in binary read mode
34    fp = open(fn, "rb")
35
36    # x
37    h  = fp.read(4)
38    x.fromfile(fp, 32)
39    f  = fp.read(4)
40
41    # y
42    h  = fp.read(4)
43    y.fromfile(fp, 32)
44    f  = fp.read(4)
45
46    # z
47    h  = fp.read(4)
48    z.fromfile(fp, 32)
49    f  = fp.read(4)
50
51# array size
52N = len(x)
53
54# print
55for i in range(N):
56    print("%5.2f %5.2f %5.2f" % (x[i], y[i], z[i]))