diff --git a/catkit2/services/accufiz_interferometer/accufiz_interferometer.py b/catkit2/services/accufiz_interferometer/accufiz_interferometer.py index 8f058d65..5ea12d61 100644 --- a/catkit2/services/accufiz_interferometer/accufiz_interferometer.py +++ b/catkit2/services/accufiz_interferometer/accufiz_interferometer.py @@ -19,7 +19,7 @@ class AccufizInterferometer(Service): def __init__(self): super().__init__('accufiz_interferometer') - + # mask, server path, local path are required self.mask = self.config['mask'] self.server_path = self.config['server_path'] @@ -40,14 +40,15 @@ def __init__(self): # Set the 4D timeout. self.html_prefix = f"http://{self.ip}/WebService4D/WebService4D.asmx" set_timeout_string = f"{self.html_prefix}/SetTimeout?timeOut={self.timeout}" - + self.get(set_timeout_string) + # set mask self.set_mask() # Create data streams. self.detector_masks = self.make_data_stream('detector_masks', 'uint8', [self.image_height, self.image_width], self.NUM_FRAMES_IN_BUFFER) self.images = self.make_data_stream('images', 'float32', [self.image_height, self.image_width], self.NUM_FRAMES_IN_BUFFER) - + self.is_acquiring = self.make_data_stream('is_acquiring', 'int8', [1], 20) self.is_acquiring.submit_data(np.array([0], dtype='int8')) self.should_be_acquiring = threading.Event() @@ -82,7 +83,7 @@ def post(self, url, data=None, json=None, **kwargs): raise RuntimeError(f"{self.config_id} POST error: {resp.status_code}: {resp.text}") time.sleep(self.post_save_sleep) return resp - + def take_measurement(self, num_frames=2): # Send request to take data. @@ -106,7 +107,7 @@ def take_measurement(self, num_frames=2): if not glob(f"{local_file_path}.h5"): raise RuntimeError(f"{self.config_id}: Failed to save measurement data to '{local_file_path}'.") - + local_file_path = local_file_path if local_file_path.endswith(".h5") else f"{local_file_path}.h5" self.log.info(f"{self.config_id}: Succeeded to save measurement data to '{local_file_path}'") @@ -140,7 +141,7 @@ def convert_h5_to_fits(filepath, rotate, fliplr, img, mask, wavelength=632.8, cr center = ndimage.measurements.center_of_mass(mask) image = np.clip(img, -10, +10)[np.int64(center[0]) - radiusmask:np.int64(center[0]) + radiusmask - 1, - np.int64(center[1]) - radiusmask: np.int64(center[1]) + radiusmask - 1] + np.int64(center[1]) - radiusmask: np.int64(center[1]) + radiusmask - 1] # Apply the rotation and flips. image = rotate_and_flip_image(image, rotate, fliplr) @@ -179,12 +180,15 @@ def acquisition_loop(self): finally: self.is_acquiring.submit_data(np.array([0], dtype='int8')) + def start_acquisition(self): self.should_be_acquiring.set() + def end_acquisition(self): self.should_be_acquiring.clear() + if __name__ == '__main__': service = AccufizInterferometer() service.run() diff --git a/catkit2/services/accufiz_interferometer_sim/accufiz_interferometer_sim.py b/catkit2/services/accufiz_interferometer_sim/accufiz_interferometer_sim.py index 270d2eda..410d2513 100644 --- a/catkit2/services/accufiz_interferometer_sim/accufiz_interferometer_sim.py +++ b/catkit2/services/accufiz_interferometer_sim/accufiz_interferometer_sim.py @@ -43,7 +43,7 @@ class AccufizInterferometerSim(Service): def __init__(self): super().__init__('accufiz_interferometer_sim') - + # mask, server path, local path are required self.mask = self.config['mask'] self.server_path = self.config['server_path'] @@ -65,14 +65,15 @@ def __init__(self): # Set the 4D timeout. self.html_prefix = f"http://{self.ip}/WebService4D/WebService4D.asmx" set_timeout_string = f"{self.html_prefix}/SetTimeout?timeOut={self.timeout}" - + self.get(set_timeout_string) + # set mask self.set_mask() # Create data streams. self.detector_masks = self.make_data_stream('detector_masks', 'uint8', [self.image_height, self.image_width], self.NUM_FRAMES_IN_BUFFER) self.images = self.make_data_stream('images', 'float32', [self.image_height, self.image_width], self.NUM_FRAMES_IN_BUFFER) - + self.is_acquiring = self.make_data_stream('is_acquiring', 'int8', [1], 20) self.is_acquiring.submit_data(np.array([0], dtype='int8')) self.should_be_acquiring = threading.Event() @@ -105,7 +106,6 @@ def post(self, url, data=None, json=None, **kwargs): time.sleep(self.post_save_sleep) return sim_response() - def take_measurement(self, num_frames=1): # Send request to take data. resp = self.post(f"{self.html_prefix}/AverageMeasure", data={"count": int(num_frames)}) @@ -124,7 +124,7 @@ def take_measurement(self, num_frames=1): tmph5f = h5py.File(fname, 'w') tmph5f['measurement0/Detectormask'] = generate_circle_array(radius=1, h=self.image_height, w=self.image_width) tmph5f['measurement0/genraw/data'] = np.random.rand(self.image_height, self.image_width) - tmph5f.close() + tmph5f.close() else: local_file_path = self.sim_data.replace('.h5', '') @@ -140,7 +140,7 @@ def take_measurement(self, num_frames=1): if not glob(f"{local_file_path}.h5"): raise RuntimeError(f"{self.config_id}: Failed to save measurement data to '{local_file_path}'.") - + local_file_path = local_file_path if local_file_path.endswith(".h5") else f"{local_file_path}.h5" self.log.info(f"{self.config_id}: Succeeded to save measurement data to '{local_file_path}'") @@ -156,13 +156,12 @@ def take_measurement(self, num_frames=1): if os.path.exists(local_file_path): os.remove(local_file_path) - + if os.path.exists(fitsfile): os.remove(fitsfile) self.log.info('cleaning up temporary simulated files') return image - @staticmethod def convert_h5_to_fits(filepath, rotate, fliplr, img, mask, wavelength=632.8, create_fits=False): @@ -179,7 +178,7 @@ def convert_h5_to_fits(filepath, rotate, fliplr, img, mask, wavelength=632.8, cr center = ndimage.measurements.center_of_mass(mask) image = np.clip(img, -10, +10)[np.int64(center[0]) - radiusmask:np.int64(center[0]) + radiusmask - 1, - np.int64(center[1]) - radiusmask: np.int64(center[1]) + radiusmask - 1] + np.int64(center[1]) - radiusmask: np.int64(center[1]) + radiusmask - 1] # Apply the rotation and flips. image = rotate_and_flip_image(image, rotate, fliplr) @@ -218,12 +217,15 @@ def acquisition_loop(self): finally: self.is_acquiring.submit_data(np.array([0], dtype='int8')) + def start_acquisition(self): self.should_be_acquiring.set() + def end_acquisition(self): self.should_be_acquiring.clear() + if __name__ == '__main__': service = AccufizInterferometerSim() service.run()