with Closing(zipfile.ZipFile(path, 'r')) as f:
f.path = path
zip_info = self._zip_info(f)
+ version = zip_info['file-format-version']
segments = []
for i in range(len([p for p in f.namelist()
if p.endswith('segment-header.properties')])):
- segments.append(self._zip_segment(f, path, info, zip_info, i))
- if zip_info['file-format-version'] not in ['0.%d' % i
- for i in range(12)]:
+ segments.append(self._zip_segment(
+ f, path, info, zip_info, i, version))
+ if version not in ['0.%d' % i for i in range(12)]:
raise NotImplementedError(
- 'JPK file version %s not supported (yet).'
- % zip_info['file-format-version'])
- curve_info = self._zip_translate_params(zip_info,
- segments[0].info['raw info'])
+ 'JPK file version %s not supported (yet).' % version)
+ curve_info = self._zip_translate_params(
+ zip_info, segments[0].info['raw info'], version)
for segment in segments: # HACK, should use curve-level spring constant
segment.info['spring constant (N/m)'] = \
curve_info['spring constant (N/m)']
info = self._parse_params(f.readlines())
return info
- def _zip_segment(self, zipfile, path, info, zip_info, index):
+ def _zip_segment(self, zipfile, path, info, zip_info, index, version):
prop_file = zipfile.open(os.path.join(
'segments', str(index), 'segment-header.properties'))
prop = self._parse_params(prop_file.readlines())
info=self._zip_translate_segment_params(prop))
for i,chan in enumerate(channels):
d[:,i] = chan
- return self._zip_scale_segment(d, path, info)
+ return self._zip_scale_segment(d, path, info, version)
def _zip_channel(self, zipfile, segment_index, channel_name, chan_info):
if chan_info['data']['type'] in ['constant-data', 'raster-data']:
# float (4 bytes, IEEE format).
return dtype.newbyteorder(byte_order)
- def _zip_translate_params(self, params, chan_info):
+ def _zip_translate_params(self, params, chan_info, version):
info = {
'raw info':params,
#'time':self._time_from_TODO(raw_info[]),
}
- force_unit = chan_info['channel']['vDeflection']['conversion-set']['conversion']['force']['scaling']['unit']['unit']
+ force_unit = self._zip_segment_conversion_unit(
+ chan_info['channel']['vDeflection']['conversion-set']['conversion']['force'],
+ version)
assert force_unit == 'N', force_unit
force_base = chan_info['channel']['vDeflection']['conversion-set']['conversion']['force']['base-calibration-slot']
assert force_base == 'distance', force_base
- dist_unit = chan_info['channel']['vDeflection']['conversion-set']['conversion']['distance']['scaling']['unit']['unit']
+ dist_unit = self._zip_segment_conversion_unit(
+ chan_info['channel']['vDeflection']['conversion-set']['conversion']['distance'],
+ version)
assert dist_unit == 'm', dist_unit
distance_base = chan_info['channel']['vDeflection']['conversion-set']['conversion']['distance']['base-calibration-slot']
assert distance_base == 'volts', distance_base
name = 'pause'
return name
- def _zip_scale_segment(self, segment, path, info):
+ def _zip_scale_segment(self, segment, path, info, version):
data = curve.Data(
shape=segment.shape,
dtype=segment.dtype,
if channel == 'vDeflection':
conversion = 'distance'
segment = self._zip_scale_channel(
- segment, channel, conversion=conversion, path=path, info=info)
+ segment, channel, conversion=conversion,
+ path=path, info=info, version=version)
name,unit = split_data_label(segment.info['columns'][i])
if name == 'vDeflection':
assert unit == 'm', segment.info['columns'][i]
segment.info['columns'][i] = join_data_label('z piezo', 'm')
return segment
- def _zip_scale_channel(self, segment, channel_name, conversion=None,
- path=None, info={}):
+ def _zip_scale_channel(self, segment, channel_name,
+ conversion=None, path=None, info={}, version=None):
channel = segment.info['raw info']['channels']['list'].index(
channel_name)
conversion_set = segment.info['raw info']['channel'][channel_name]['conversion-set']
segment = self._zip_scale_channel(
segment, channel_name,
conversion_info['base-calibration-slot'],
- path=path, info=info)
+ path=path, info=info, version=version)
if conversion_info['type'] == 'file':
# Michael Haggerty at JPK points out that the conversion
# information stored in the external file is reproduced in
conversion_info['scaling']['style']
multiplier = float(conversion_info['scaling']['multiplier'])
offset = float(conversion_info['scaling']['offset'])
- unit = conversion_info['scaling']['unit']['unit']
+ unit = self._zip_segment_conversion_unit(conversion_info, version)
segment[:,channel] = segment[:,channel] * multiplier + offset
segment.info['columns'][channel] = join_data_label(channel_name, unit)
return segment
+ def _zip_segment_conversion_unit(self, conversion_info, version):
+ if version in ['0.%d' % i for i in range(3)]:
+ return conversion_info['scaling']['unit']
+ else:
+ return conversion_info['scaling']['unit']['unit']
+
def _parse_params(self, lines):
info = {}
for line in lines: