Adjust unit extraction from the JPK file format.
authorW. Trevor King <wking@drexel.edu>
Wed, 8 Sep 2010 17:57:56 +0000 (13:57 -0400)
committerW. Trevor King <wking@drexel.edu>
Wed, 8 Sep 2010 17:57:56 +0000 (13:57 -0400)
I've emailed Michael Haggerty to track down the version that made the
change from 'data.unit=' to 'data.unit.unit='.  For now, I'm assuming
it's just <= 0.2.

hooke/driver/jpk.py

index dcc10409a4dea2cb1cfd7484bcaf15cee65f820f..ba38f6a485d5b3c764318be8e21d45399c7c00c9 100644 (file)
@@ -73,17 +73,17 @@ class JPKDriver (Driver):
         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)']
@@ -94,7 +94,7 @@ class JPKDriver (Driver):
             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())
@@ -124,7 +124,7 @@ class JPKDriver (Driver):
             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']:
@@ -194,16 +194,20 @@ class JPKDriver (Driver):
         #    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
@@ -235,7 +239,7 @@ class JPKDriver (Driver):
             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,
@@ -250,7 +254,8 @@ class JPKDriver (Driver):
             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]
@@ -265,8 +270,8 @@ class JPKDriver (Driver):
                 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']
@@ -288,7 +293,7 @@ class JPKDriver (Driver):
             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
@@ -307,11 +312,17 @@ class JPKDriver (Driver):
             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: