licel.py

changeset 27
74f7617f5356
parent 22
a2355e871b23
child 28
73337ce10473
--- a/licel.py	Wed Jun 18 17:56:46 2014 +0300
+++ b/licel.py	Sun Nov 23 23:24:32 2014 +0200
@@ -1,6 +1,6 @@
 import numpy as np
 import datetime
-from generic import BaseLidarMeasurement, Lidar_channel
+from generic import BaseLidarMeasurement, LidarChannel
 import musa_netcdf_parameters
 import musa_2009_netcdf_parameters
 
@@ -11,6 +11,7 @@
 
 
 class LicelFile:
+    
     def __init__(self, filename):
         self.start_time = None
         self.stop_time = None
@@ -61,7 +62,7 @@
             
             if (a[0] != 13) | (b[0] != 10):
                 print "Warning: No end of line found after record. File could be corrupt"
-            channel = LicelFileChannel(current_channel_info, raw_data)
+            channel = LicelFileChannel(current_channel_info, raw_data, self.duration())
             
             channel_name = channel.channel_name
             if channel_name in channels.keys():
@@ -73,14 +74,20 @@
 
         self.raw_info = raw_info
         self.channels = channels
-       
-
+    
+    def duration(self):
+        """ Return the duration of the file. """
+        dt = self.stop_time - self.start_time
+        return dt.seconds
+        
+        
 class LicelFileChannel:
     
-    def __init__(self, raw_info = None, raw_data = None):
+    def __init__(self, raw_info = None, raw_data = None, duration = None):
         self.raw_info = raw_info
         self.raw_data = raw_data
-    
+        self.duration = duration
+        
     @property
     def wavelength(self):
         if self.raw_info is not None:
@@ -110,7 +117,7 @@
         data = self.raw_data
 
         number_of_shots = float(self.raw_info['NShots'])
-        norm = data/number_of_shots
+        norm = data / number_of_shots
         dz = float(self.raw_info['BinW'])
  
         if self.raw_info['AnalogPhoton']=='0':
@@ -128,7 +135,7 @@
             # channel_data = norm*SR
             # CHANGE:
             # For the SCC the data are needed in photons
-            channel_data = norm*number_of_shots
+            channel_data = norm *number_of_shots
             #print res,c,cdata,norm
 
         #Calculate Z
@@ -143,9 +150,9 @@
     '''
     
     '''
-    
     extra_netcdf_parameters = musa_netcdf_parameters
-    raw_info = {} # Keep the raw info from the files
+    raw_info = {}   # Keep the raw info from the files
+    durations = {}  # Keep the duration of the files
     
     def import_file(self, filename):
         if filename in self.files:
@@ -153,10 +160,11 @@
         else:
             current_file = LicelFile(filename)
             self.raw_info[current_file.filename] = current_file.raw_info
+            self.durations[current_file.filename] = current_file.duration()
             
             for channel_name, channel in current_file.channels.items():
                 if channel_name not in self.channels:
-                    self.channels[channel_name] = Licel_channel(channel)
+                    self.channels[channel_name] = LicelChannel(channel)
                 self.channels[channel_name].data[current_file.start_time] = channel.data   
             self.files.append(current_file.filename)
         
@@ -172,16 +180,10 @@
         ''' Return the duration for a given time scale. If only a single
         file is imported, then this cannot be guessed from the time difference
         and the raw_info of the file are checked.
-         
         '''
         
         if len(raw_start_in_seconds) == 1: # If only one file imported
-            raw_info = self.raw_info.itervalues().next() # Get the first (and only) raw_info
-            start_str = "%s %s" % (raw_info['StartDate'], raw_info['StartTime'])
-            end_str = "%s %s" % (raw_info['EndDate'], raw_info['EndTime'])
-            start_time = datetime.datetime.strptime(start_str, "%d/%m/%Y %H:%M:%S")
-            end_time = datetime.datetime.strptime(end_str, "%d/%m/%Y %H:%M:%S")
-            duration = end_time - start_time
+            duration = self.durations.itervalues().next() # Get the first (and only) raw_info
             duration_sec = duration.seconds
         else:
             duration_sec = np.diff(raw_start_in_seconds)[0]
@@ -189,19 +191,19 @@
         return duration_sec
 
        
-class Licel_channel(Lidar_channel):
+class LicelChannel(LidarChannel):
     
     def __init__(self, channel_file):
-        c = 299792458.0 #Speed of light
+        c = 299792458.0 # Speed of light
         self.wavelength = channel_file.wavelength
         self.name = channel_file.channel_name
-        self.binwidth = channel_file.dz * 2 / c # in microseconds
+        self.binwidth = channel_file.dz * 2 / c  # in seconds 
         self.data = {}
         self.resolution = channel_file.dz
-        self.z = np.arange(channel_file.number_of_bins) *self.resolution + self.resolution/2.0 #Change: add half bin in the z 
+        self.z = np.arange(channel_file.number_of_bins) * self.resolution + self.resolution/2.0 #Change: add half bin in the z 
         self.points = channel_file.number_of_bins
         self.rc = []
-        self.duration  = 60
+        self.duration  = channel_file.duration
     
     def append(self, other):
         if self.info != other.info:

mercurial