Added option to read just header from licel file.

Fri, 23 Mar 2018 16:09:49 +0200

author
Iannis B <ioannis@inoe.ro>
date
Fri, 23 Mar 2018 16:09:49 +0200
changeset 144
c61651ad1137
parent 143
44c3d7550e63
child 145
a99ae250e086

Added option to read just header from licel file.

atmospheric_lidar/licel.py file | annotate | diff | comparison | revisions
--- a/atmospheric_lidar/licel.py	Thu Mar 22 13:53:40 2018 +0200
+++ b/atmospheric_lidar/licel.py	Fri Mar 23 16:09:49 2018 +0200
@@ -152,7 +152,7 @@
 
     channel_data_class = LicelChannelData
 
-    def __init__(self, file_path, use_id_as_name=False, licel_timezone="UTC"):
+    def __init__(self, file_path, use_id_as_name=False, licel_timezone="UTC", import_now=True):
         """
         This is run when creating a new object.
 
@@ -166,35 +166,27 @@
         licel_timezone : str
            The timezone of dates found in the Licel files. Should match the available
            timezones in the TZ database.
+        import_file : bool
+           If True, the header and data are read immediately. If not, the user has to call the
+           corresponding methods directly. This is used to speed up reading files when only
+           header information are required.
         """
         self.filename = file_path
         self.use_id_as_name = use_id_as_name
         self.start_time = None
         self.stop_time = None
         self.licel_timezone = licel_timezone
-        self._import_file(file_path)
-        self._calculate_physical()
-
-    def _calculate_physical(self):
-        """ Calculate physical quantities from raw data for all channels in the file. """
-        for channel in self.channels.itervalues():
-            channel.calculate_physical()
 
-        for photodiode in self.photodiodes.itervalues():
-            photodiode.calculate_physical()
+        if import_now:
+            self.import_file()
 
-    def _import_file(self, file_path):
+    def import_file(self):
         """ Read the header info and data of the Licel file.
-
-        Parameters
-        ----------
-        file_path : str
-           The path to the Licel file.
         """
         channels = {}
         photodiodes = {}
 
-        with open(file_path, 'rb') as f:
+        with open(self.filename, 'rb') as f:
 
             self.read_header(f)
 
@@ -228,6 +220,8 @@
         self.channels = channels
         self.photodiodes = photodiodes
 
+        self._calculate_physical()
+
     def read_header(self, f):
         """ Read the header of an open Licel file.
 
@@ -311,6 +305,14 @@
         raw_dict = self.match_lines(third_line, self.licel_file_header_format[2])
         return raw_dict
 
+    def _calculate_physical(self):
+        """ Calculate physical quantities from raw data for all channels in the file. """
+        for channel in self.channels.itervalues():
+            channel.calculate_physical()
+
+        for photodiode in self.photodiodes.itervalues():
+            photodiode.calculate_physical()
+
     def duration(self):
         """ Return the duration of the file.
 
@@ -322,6 +324,11 @@
         dt = self.stop_time - self.start_time
         return dt.seconds
 
+    def import_header_only(self):
+        """ Import only the header lines, withouth reading the acutal data."""
+        with open(self.filename, 'rb') as f:
+            self.read_header(f)
+
     @staticmethod
     def match_lines(f1, f2):
         list1 = f1.split()
@@ -504,7 +511,11 @@
             channel = self.channels[channel_name]
             laser_shots.append(channel.laser_shots)
 
-        laser_shots = np.array(laser_shots).T
+        try:
+            laser_shots = np.vstack(laser_shots).T
+        except Exception as e:
+            logger.error('Could not read laser shots as an array. Maybe files contain different number of channels?')
+            raise e
 
         params = [{
             "name": "DAQ_Range",

mercurial