Skip to content

Commit

Permalink
Added better support for temperature, gain and offset
Browse files Browse the repository at this point in the history
  • Loading branch information
gnthibault committed Aug 9, 2023
1 parent a48ede9 commit 2d52465
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 110 deletions.
15 changes: 12 additions & 3 deletions Camera/AbstractCamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ def take_observation(self, observation, headers=None, filename=None,
# (see `process_exposure`)
observation_event = Event()

(exp_time, gain, temperature, file_path, image_id, metadata,
(exp_time, gain, offset, temperature, file_path, image_id, metadata,
is_pointing) = self._setup_observation(observation,
headers,
filename,
*args,
**kwargs)
kwargs["gain"] = gain
kwargs["offset"] = offset
kwargs["temperature"] = temperature
exposure_event = self.take_exposure(
exposure_time=exp_time,
Expand Down Expand Up @@ -163,6 +164,7 @@ def _setup_observation(self, observation, headers, filename, **kwargs):
# get values
exp_time = kwargs.get('exp_time', observation.time_per_exposure)
gain = observation.configuration["gain"]
offset = observation.configuration["offset"]
temperature = observation.configuration["temperature"]
filter_name = observation.configuration.get("filter", "no-filter")

Expand All @@ -181,12 +183,12 @@ def _setup_observation(self, observation, headers, filename, **kwargs):
'temperature_degC': temperature
}
metadata.update(headers)
return (exp_time, gain, temperature, file_path, image_id, metadata,
is_pointing)
return (exp_time, gain, offset, temperature, file_path, image_id, metadata, is_pointing)

def take_calibration(self,
temperature,
gain,
offset,
exp_time,
headers=None,
calibration_seq_id=None,
Expand All @@ -201,6 +203,7 @@ def take_calibration(self,
file_path, metadata = self._setup_calibration(
temperature=temperature,
gain=gain,
offset=offset,
exp_time=exp_time,
headers=headers,
calibration_seq_id=calibration_seq_id,
Expand All @@ -213,6 +216,7 @@ def take_calibration(self,
filename=file_path,
temperature=temperature,
gain=gain,
offset=offset,
exposure_time=exp_time,
headers=headers,
calibration_seq_id=calibration_seq_id,
Expand All @@ -231,6 +235,7 @@ def take_calibration(self,
def get_calibration_directory(self,
temperature,
gain,
offset,
exp_time,
headers,
calibration_seq_id=None,
Expand All @@ -252,6 +257,7 @@ def get_calibration_directory(self,
image_dir,
f"temp_deg_{temperature}",
f"gain_{gain}",
f"offset_{offset}",
f"exp_time_sec_{exp_time.to(u.second).value}")
if calibration_seq_id is not None:
image_dir = os.path.join(
Expand All @@ -276,6 +282,7 @@ def get_calibration_directory(self,
def _setup_calibration(self,
temperature,
gain,
offset,
exp_time,
headers=None,
calibration_seq_id=None,
Expand All @@ -293,6 +300,7 @@ def _setup_calibration(self,
file_path = self.get_calibration_directory(
temperature=temperature,
gain=gain,
offset=offset,
exp_time=exp_time,
headers=headers,
calibration_seq_id=calibration_seq_id,
Expand All @@ -319,6 +327,7 @@ def _setup_calibration(self,
'start_time': start_time,
'exp_time': exp_time.to(u.second).value,
'gain': gain,
'offset': offset,
'temperature_degC': temperature.to(u.Celsius).value if temperature else "no_target",
'observation_ids': [o.id for o in observations]
}
Expand Down
3 changes: 3 additions & 0 deletions Camera/IndiAbstractCamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def shoot_asyncWithEvent(self, exp_time_sec, filename, exposure_event,
# set gain
gain = kwargs.get("gain", self.gain)
self.set_gain(gain)
# set offset
offset = kwargs.get("offset", self.offset)
self.set_offset(offset)
# set temperature
temperature = kwargs.get("temperature", None)
if temperature is not None:
Expand Down
27 changes: 27 additions & 0 deletions Camera/IndiAltairCameraNonCool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Basic stuff
import numpy as np

# Local stuff
from Camera.IndiAltairCamera import IndiAltairCamera

class IndiAltairCameraNonCool(IndiAltairCamera):
def __init__(self, serv_time, config=None,
connect_on_create=True):

# Parent initialization
super().__init__(
serv_time=serv_time,
config=config,
connect_on_create=connect_on_create)

def set_cooling_on(self):
pass

def set_cooling_off(self):
pass

def get_temperature(self):
return np.nan

def set_temperature(self, temperature):
pass
10 changes: 9 additions & 1 deletion Camera/IndiCamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def __init__(self, logger=None, config=None, connect_on_create=True):

# Default exposureTime, gain
self.exp_time_sec = 5
self.gain = 400
self.gain = 150
self.offset = 30

# Finished configuring
self.logger.debug('Configured Indi Camera successfully')
Expand Down Expand Up @@ -305,6 +306,13 @@ def get_gain(self):
gain = self.get_number('CCD_GAIN')
return gain["GAIN"]

def set_offset(self, value):
self.set_number('CCD_OFFSET', {'OFFSET': value}, sync=True, timeout=self.defaultTimeout)

def get_offset(self):
offset = self.get_number('CCD_OFFSET')
return offset["OFFSET"]

def get_frame_type(self):
return self.get_switch('CCD_FRAME_TYPE')

Expand Down
8 changes: 8 additions & 0 deletions Camera/IndiPlayerOneCamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ def set_gain(self, value):
def get_gain(self):
gain = self.get_number('CCD_CONTROLS')
return gain["Gain"]

def set_offset(self, value):
self.set_number('CCD_CONTROLS', {'Offset': value}, sync=True, timeout=self.defaultTimeout)

def get_offset(self):
offset = self.get_number('CCD_CONTROLS')
return offset["Offset"]

4 changes: 3 additions & 1 deletion ObservationPlanner/Scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,13 @@ def initialize_target_list(self):
count = config["count"]
temperature = config["temperature"]
gain = config["gain"]
offset = config["offset"]
exp_time_sec = config["exp_time_sec"]*u.second
configuration={
'filter': filter_name,
'temperature': temperature,
'gain': gain
'gain': gain,
'offset': offset
}
#TODO TN retrieve priority from the file ?
priority = 0 if (filter_name == 'Luminance') else 1
Expand Down
71 changes: 38 additions & 33 deletions apps/calibration_utilities/dark_library_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@

class DarkLibraryBuilder():

def __init__(self, camera, exp_time_list, gain_list, temp_list=[np.NaN],
def __init__(self, camera, exp_time_list, gain_list, offset_list, temp_list=[np.NaN],
outdir=None, nb_image=100):
#super(self).__init__()

#attributes
self.cam = camera
self.exp_time_list = exp_time_list
self.gain_list = gain_list
self.offset_list = offset_list
self.temp_list = temp_list
self.outdir = outdir or './dark_calibration' #TODO TN replace
self.nb_image = nb_image
Expand Down Expand Up @@ -72,42 +73,43 @@ def gen_report_gain_basedirname(self, temperature, gain):
temperature.to(u.Celsius).value,
gain
))
def gen_calib_basedirname(self, temperature, gain, exp_time):
def gen_calib_basedirname(self, temperature, gain, offset, exp_time):
return ("{}/calibration/{}/camera_{}/temperature_{}/gain_{}/exp_time_{}"
"".format(self.outdir,
'dark',
self.cam.name,
temperature.to(u.Celsius).value,
gain,
offset,
exp_time.to(u.second).value
))

def gen_calib_filename(self, temperature, gain, exp_time, index):
image_dir = self.gen_calib_basedirname(temperature, gain, exp_time)
def gen_calib_filename(self, temperature, gain, offset, exp_time, index):
image_dir = self.gen_calib_basedirname(temperature, gain, offset, exp_time)
os.makedirs(image_dir, exist_ok=True)
image_name = os.path.join(image_dir,str(index)+'.fits')
return image_name

def gen_calib_mastername(self, temperature, gain, exp_time):
image_dir = self.gen_calib_basedirname(temperature, gain, exp_time)
def gen_calib_mastername(self, temperature, gain, offset, exp_time):
image_dir = self.gen_calib_basedirname(temperature, gain, offset, exp_time)
os.makedirs(image_dir, exist_ok=True)
master_name = os.path.join(image_dir, 'master.tif')
return master_name

def gen_calib_masterstdname(self, temperature, gain, exp_time):
image_dir = self.gen_calib_basedirname(temperature, gain, exp_time)
def gen_calib_masterstdname(self, temperature, gain, offset, exp_time):
image_dir = self.gen_calib_basedirname(temperature, gain, offset, exp_time)
os.makedirs(image_dir, exist_ok=True)
master_std_name = os.path.join(image_dir, 'master_std.tif')
return master_std_name

def gen_calib_masterpsnrname(self, temperature, gain, exp_time):
image_dir = self.gen_calib_basedirname(temperature, gain, exp_time)
def gen_calib_masterpsnrname(self, temperature, gain, offset, exp_time):
image_dir = self.gen_calib_basedirname(temperature, gain, offset, exp_time)
os.makedirs(image_dir, exist_ok=True)
master_psnr_name = os.path.join(image_dir, 'master_psnr.tif')
return master_psnr_name

def gen_NLF_figname(self, temperature, gain, exp_time):
base_dir = self.gen_calib_basedirname(temperature, gain, exp_time)
def gen_NLF_figname(self, temperature, gain, offset, exp_time):
base_dir = self.gen_calib_basedirname(temperature, gain, offset, exp_time)
os.makedirs(base_dir, exist_ok=True)
conv_check = os.path.join(base_dir, 'NLF_conv_check.png')
regression = os.path.join(base_dir, 'NLF_regression.png')
Expand Down Expand Up @@ -483,26 +485,27 @@ def acquire_images(self):
self.cam.prepare_shoot()
for temperature in self.temp_list:
self.set_temperature(temperature)
for gain in self.gain_list:
self.cam.set_gain(gain)
for exp_time in self.exp_time_list:
for i in range(self.nb_image):
print('Temperature {}, gain {}, exp time {}, Acquiring'
' image {}'.format(temperature,gain,exp_time,i))
fname = self.gen_calib_filename(temperature, gain,
exp_time,i)
if not os.path.exists(fname):
print('before set exp time')
self.cam.setExpTimeSec(exp_time)
print('before shoot')
self.cam.shoot_async()
print('After shoot_async, going to sync')
self.cam.synchronize_with_image_reception()
print('After sync')
fits = self.cam.get_received_image()
print('Image received')
with open(fname, "wb") as f:
fits.writeto(f)
for offset in self.offset_list:
self.set_offset(offset)
for gain in self.gain_list:
self.cam.set_gain(gain)
for exp_time in self.exp_time_list:
for i in range(self.nb_image):
print('Temperature {}, gain {}, offset {}, exp time {}, Acquiring'
' image {}'.format(temperature, gain, offset, exp_time, i))
fname = self.gen_calib_filename(temperature, gain, offset, exp_time, i)
if not os.path.exists(fname):
print('before set exp time')
self.cam.setExpTimeSec(exp_time)
print('before shoot')
self.cam.shoot_async()
print('After shoot_async, going to sync')
self.cam.synchronize_with_image_reception()
print('After sync')
fits = self.cam.get_received_image()
print('Image received')
with open(fname, "wb") as f:
fits.writeto(f)
self.cleanup_device()

def compute_statistics(self):
Expand Down Expand Up @@ -626,10 +629,12 @@ def main(config_file='./jsonModel/IndiCCDSimulatorCamera.json',
# exp_time_list = np.linspace(1, 30, 8)*u.second
exp_time_list = np.array([1,5,10,30,60,120])*u.second
# gain_list = np.linspace(0,100,10, dtype=np.int32).tolist()
gain_list = [0,25,50,75,100]
gain_list = [120, 150]
offset_list = [20, 30]
temp_list = [np.NaN*u.Celsius]
b = DarkLibraryBuilder(cam, exp_time_list, temp_list=temp_list,
gain_list=gain_list,
offset_list=offset_list,
outdir='./dark_calibration',
nb_image=19)
b.show_plot = show_plot
Expand Down
10 changes: 7 additions & 3 deletions calibration/ImagingCalibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self,
self.flat_exp_sec = config["flat"]["sec"]*u.second
self.flat_nb = config["flat"]["nb"]
self.flat_gain = config["flat"]["gain"]
self.flat_offset = config["flat"]["offset"]
self.flat_temperature = config["flat"]["temperature"]
self.dark_nb = config["dark"]["dark_nb"]

Expand Down Expand Up @@ -65,6 +66,7 @@ def take_flat(self, observed_list):
event = self.camera.take_calibration(
temperature=self.flat_temperature,
gain=self.flat_gain,
offset=self.flat_offset,
exp_time=self.flat_exp_sec,
headers={"filter": filter_name},
calibration_name="flat",
Expand All @@ -83,21 +85,23 @@ def take_dark(self, observed_list):
for seq_time, observation in observed_list.items():
temp_deg = observation.configuration['temperature']
conf = (observation.time_per_exposure,
observation.configuration['gain'])
observation.configuration['gain'],
observation.configuration['offset'])
if temp_deg in dark_config_dict:
dark_config_dict[temp_deg].add(conf)
else:
dark_config_dict[temp_deg] = set((conf,))

self.controller.close_optical_path_for_dark()
for temp_deg, times_gains in dark_config_dict.items():
for temp_deg, times_gains_offsets in dark_config_dict.items():
if temp_deg:
self.camera.set_temperature(temp_deg)
for (exp_time, gain) in times_gains:
for (exp_time, gain, offset) in times_gains_offsets:
for i in range(self.dark_nb):
event = self.camera.take_calibration(
temperature=temp_deg,
gain=gain,
offset=offset,
exp_time=exp_time,
headers={},
calibration_name="dark",
Expand Down
Loading

0 comments on commit 2d52465

Please sign in to comment.