atmospheric_lidar/licel.py

changeset 144
c61651ad1137
parent 137
0e7aa8e32d4f
child 145
a99ae250e086
--- 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