from pkg_resources import require
require("numpy")

from epics import pv
import numpy as np


class Camera(object):
    """ Camera object for display. This wraps an instance of an EPICS 'camera'
        providing image data over CA.

        API decision need to be taken:
        - should the owning object be notified on updates to the data PV
          or do we depend the the owner polling at some controlled rate?
    """

    def __init__(self, prefix):
        """ Create a camera instance attached to a camera IOC
            supplying $(prefix):arrayData, $(prefix):xsize and
            $(prefix):ysize PVs
        """
        self.data_pv = pv.PV(prefix + ":image1:ArrayData")
        self.xsize_pv = pv.PV(prefix + ":cam1:SizeX")
        self.ysize_pv = pv.PV(prefix + ":cam1:SizeY")

    def close(self):
        """ Close camerate instance, disconnecting monitors and
            callback handlers
        """
        self.data_pv.disconnect()
        self.xsize_pv.disconnect()
        self.ysize_pv.disconnect()

    @property
    def xsize(self):
        return self.xsize_pv.get()

    @property
    def ysize(self):
        return self.ysize_pv.get()

    def get_image_data(self):
        """ Get the current image data.

            Returns:
            numpy array reshaped as (y,x) dimensions
            specified by the xsize and ysize PVs
            
        """
        cdata = self.data_pv.get()
        x_size = self.xsize
        y_size = self.ysize

        data = np.array(cdata)
        reshaped = data.reshape(y_size, x_size).astype('int32')

        return np.transpose(reshaped)
@@ -1187,24 +1187,45 @@
 ge_data(self
+, last_timestamp=None
 ):%0A        %22
@@ -1342,16 +1342,20 @@
         
+    
 specifie
@@ -1395,16 +1395,33 @@
         
+timestamp of data
 %0A       
@@ -1456,24 +1456,162 @@
 ta_pv.get()%0A
+        image_data = None%0A        timestamp = self.data_pv.timestamp%0A        if last_timestamp is None or timestamp %3E last_timestamp:%0A    
         x_si
@@ -1626,16 +1626,20 @@
 f.xsize%0A
+    
         
@@ -1667,16 +1667,20 @@
         
+  
+  
 data = n
@@ -1694,16 +1694,20 @@
 (cdata)%0A
+    
         
@@ -1762,25 +1762,24 @@
 int32')%0A
-%0A
         
 return n
@@ -1770,22 +1770,32 @@
         
-return
+    image_data =
  np.tran
@@ -1809,8 +1809,47 @@
 eshaped)
+%0A%0A        return image_data, timestamp%0A

Extend getImageData to take 'last seen' timestamp