run("Devices/Elements") def _create_tables(paths, stream_value, data_type, shape): root = paths["root"] create_dataset(paths["image"], data_type, dimensions = [images, shape[0], shape[1]]) create_dataset(paths["pid"], 'l', dimensions = [images]) create_dataset(paths["timestamp_str"], 's', dimensions = [images]) for id in stream_value.identifiers: val = stream_value.getValue(id) if id == "image": pass elif id == "processing_parameters": val = json.loads(val) for key in val.keys(): set_attribute(paths["image"], key, "" if val[key] is None else val[key] ) elif isinstance(val, PyArray): create_dataset(root + id, 'd', dimensions = [images, len(val)]) elif isinstance(val, PyLong): create_dataset(root + id, 'l', dimensions = [images]) elif isinstance(val, PyFloat): create_dataset(root + id, 'd', dimensions = [images]) else: print "Unmanaged stream type: ", val, type(val) pass def _append_frame(paths, stream_value, index, data_type, shape): print "Saving frame :", index #append_dataset(paths["image"], data, index, type = data_type) root = paths["root"] append_dataset(paths["image"], stream_value.getValue("image"),[index,0,0], type = data_type, shape=[1, shape[0], shape[1]]) append_dataset(paths["pid"], stream_value.getPulseId(), index) append_dataset(paths["timestamp_str"], datetime.datetime.fromtimestamp(stream_value.timestampNanos/1e9).strftime('%Y-%m-%d %H:%M:%S'), index) for id in stream_value.identifiers: try: val = stream_value.getValue(id) if id == "image": pass elif isinstance(val, PyArray): append_dataset(root + id, val, index) elif isinstance(val, PyLong): append_dataset(root + id, int(val), index) elif isinstance(val, PyFloat): append_dataset(root + id, float(val), index) else: pass except: print id, val traceback.print_exc() print "Saved frame: ", index def _write_metadata(paths, camera_name, shared = False, images = 1, interval = -1): root = paths["root"] set_attribute(root, "Name", camera_name) set_attribute(root, "Shared", shared) set_attribute(root, "Images", images) set_attribute(root, "Interval", interval) cam_type = get_camera_type(camera_name) set_attribute(root, "Type", cam_type) if cam_type=="ELECTRONS": set_attribute(root, "Screen", caget(camera_name + (":POSITION" if (camera_name.startswith("DSRM")) else ":GET_SCREEN1_POS"), 's')) set_attribute(root, "Filter", caget(camera_name + ":GET_FILTER", 's')) def save_camera_data(server, camera_name, shared = False, root = "/", images = 1, interval = -1, parallel = True, pause = False): stream_value = server.stream.take() if stream_value is None: server.waitNext(10000) stream_value = server.stream.take() if not root.endswith('/'): root = root + "/" paths = {"root": root, "image":root+"image", "pid":root+"/pulse_id", "timestamp_str":root+"/timestamp_str"} shape = [stream_value.getValue("height"),stream_value.getValue("width")] data_type = stream_value.getValue("image").typecode tasks = [] if pause: server.paused = True try: for i in range(images): if i==0: _create_tables(paths, stream_value, data_type, shape) _write_metadata(paths, camera_name, shared, images, interval) start = time.time() stream_value = server.stream.take() if parallel: tasks.extend( fork((_append_frame,(paths, stream_value, i, data_type, shape)),) ) else: _append_frame(paths, stream_value, i, data_type, shape) if i< (images-1): if interval<=0: server.stream.waitCacheChange(10000) else: sleep_time = float(interval)/1000.0 - (time.time()-start) time.sleep(max(sleep_time,0)) finally: if pause: server.paused = False pass print "Waiting finish persisting..." join(tasks) print "Done"