~ubuntu-branches/ubuntu/oneiric/python-scipy/oneiric-proposed

« back to all changes in this revision

Viewing changes to scipy/io/matlab/mio5_params.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-04-06 21:26:25 UTC
  • mfrom: (9.2.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110406212625-3izdplobqe6fzeql
Tags: 0.9.0+dfsg1-1
* New upstream release (Closes: #614407, #579041, #569008)
* Convert to dh_python2 (Closes: #617028)

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 
8
8
import numpy as np
9
9
 
 
10
from miobase import convert_dtypes
10
11
 
11
12
miINT8 = 1
12
13
miUINT8 = 2
51
52
# https://www-old.cae.wisc.edu/pipermail/octave-maintainers/2007-May/002824.html
52
53
mxOBJECT_CLASS_FROM_MATRIX_H = 18
53
54
 
 
55
mdtypes_template = {
 
56
    miINT8: 'i1',
 
57
    miUINT8: 'u1',
 
58
    miINT16: 'i2',
 
59
    miUINT16: 'u2',
 
60
    miINT32: 'i4',
 
61
    miUINT32: 'u4',
 
62
    miSINGLE: 'f4',
 
63
    miDOUBLE: 'f8',
 
64
    miINT64: 'i8',
 
65
    miUINT64: 'u8',
 
66
    miUTF8: 'u1',
 
67
    miUTF16: 'u2',
 
68
    miUTF32: 'u4',
 
69
    'file_header': [('description', 'S116'),
 
70
                    ('subsystem_offset', 'i8'),
 
71
                    ('version', 'u2'),
 
72
                    ('endian_test', 'S2')],
 
73
    'tag_full': [('mdtype', 'u4'), ('byte_count', 'u4')],
 
74
    'tag_smalldata':[('byte_count_mdtype', 'u4'), ('data', 'S4')],
 
75
    'array_flags': [('data_type', 'u4'),
 
76
                    ('byte_count', 'u4'),
 
77
                    ('flags_class','u4'),
 
78
                    ('nzmax', 'u4')],
 
79
    'U1': 'U1',
 
80
    }
 
81
 
 
82
mclass_dtypes_template = {
 
83
    mxINT8_CLASS: 'i1',
 
84
    mxUINT8_CLASS: 'u1',
 
85
    mxINT16_CLASS: 'i2',
 
86
    mxUINT16_CLASS: 'u2',
 
87
    mxINT32_CLASS: 'i4',
 
88
    mxUINT32_CLASS: 'u4',
 
89
    mxINT64_CLASS: 'i8',
 
90
    mxUINT64_CLASS: 'u8',
 
91
    mxSINGLE_CLASS: 'f4',
 
92
    mxDOUBLE_CLASS: 'f8',
 
93
    }
 
94
 
 
95
 
 
96
NP_TO_MTYPES = {
 
97
    'f8': miDOUBLE,
 
98
    'c32': miDOUBLE,
 
99
    'c24': miDOUBLE,
 
100
    'c16': miDOUBLE,
 
101
    'f4': miSINGLE,
 
102
    'c8': miSINGLE,
 
103
    'i8': miINT64,
 
104
    'i4': miINT32,
 
105
    'i2': miINT16,
 
106
    'i1': miINT8,
 
107
    'u8': miUINT64,
 
108
    'u4': miUINT32,
 
109
    'u2': miUINT16,
 
110
    'u1': miUINT8,
 
111
    'S1': miUINT8,
 
112
    'U1': miUTF16,
 
113
    }
 
114
 
 
115
 
 
116
NP_TO_MXTYPES = {
 
117
    'f8': mxDOUBLE_CLASS,
 
118
    'c32': mxDOUBLE_CLASS,
 
119
    'c24': mxDOUBLE_CLASS,
 
120
    'c16': mxDOUBLE_CLASS,
 
121
    'f4': mxSINGLE_CLASS,
 
122
    'c8': mxSINGLE_CLASS,
 
123
    'i8': mxINT64_CLASS,
 
124
    'i4': mxINT32_CLASS,
 
125
    'i2': mxINT16_CLASS,
 
126
    'i1': mxINT8_CLASS,
 
127
    'u8': mxUINT64_CLASS,
 
128
    'u4': mxUINT32_CLASS,
 
129
    'u2': mxUINT16_CLASS,
 
130
    'u1': mxUINT8_CLASS,
 
131
    'S1': mxUINT8_CLASS,
 
132
    }
 
133
 
 
134
''' Before release v7.1 (release 14) matlab (TM) used the system
 
135
default character encoding scheme padded out to 16-bits. Release 14
 
136
and later use Unicode. When saving character data, R14 checks if it
 
137
can be encoded in 7-bit ascii, and saves in that format if so.'''
 
138
 
 
139
codecs_template = {
 
140
    miUTF8: {'codec': 'utf_8', 'width': 1},
 
141
    miUTF16: {'codec': 'utf_16', 'width': 2},
 
142
    miUTF32: {'codec': 'utf_32','width': 4},
 
143
    }
 
144
 
 
145
 
 
146
def _convert_codecs(template, byte_order):
 
147
    ''' Convert codec template mapping to byte order
 
148
 
 
149
    Set codecs not on this system to None
 
150
 
 
151
    Parameters
 
152
    ----------
 
153
    template : mapping
 
154
       key, value are respectively codec name, and root name for codec
 
155
       (without byte order suffix)
 
156
    byte_order : {'<', '>'}
 
157
       code for little or big endian
 
158
 
 
159
    Returns
 
160
    -------
 
161
    codecs : dict
 
162
       key, value are name, codec (as in .encode(codec))
 
163
    '''
 
164
    codecs = {}
 
165
    postfix = byte_order == '<' and '_le' or '_be'
 
166
    for k, v in template.items():
 
167
        codec = v['codec']
 
168
        try:
 
169
            " ".encode(codec)
 
170
        except LookupError:
 
171
            codecs[k] = None
 
172
            continue
 
173
        if v['width'] > 1:
 
174
            codec += postfix
 
175
        codecs[k] = codec
 
176
    return codecs.copy()
 
177
 
 
178
 
 
179
MDTYPES = {}
 
180
for _bytecode in '<>':
 
181
    _def = {}
 
182
    _def['dtypes'] = convert_dtypes(mdtypes_template, _bytecode)
 
183
    _def['classes'] = convert_dtypes(mclass_dtypes_template, _bytecode)
 
184
    _def['codecs'] = _convert_codecs(codecs_template, _bytecode)
 
185
    MDTYPES[_bytecode] = _def
 
186
 
54
187
 
55
188
class mat_struct(object):
56
189
    ''' Placeholder for holding read data from structs