Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished Sofast HDF5 IO #31

Merged
merged 16 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import os
from os.path import join
from os.path import join, dirname

import matplotlib

from opencsp.app.sofast.lib.DisplayShape import DisplayShape as Display
from opencsp.app.sofast.lib.DefinitionEnsemble import DefinitionEnsemble
from opencsp.app.sofast.lib.DefinitionFacet import DefinitionFacet
from opencsp.app.sofast.lib.ImageCalibrationScaling import ImageCalibrationScaling
from opencsp.app.sofast.lib.MeasurementSofastFringe import (
MeasurementSofastFringe as Measurement,
)
from opencsp.app.sofast.lib.ProcessSofastFringe import ProcessSofastFringe as Sofast
from opencsp.app.sofast.lib.SpatialOrientation import SpatialOrientation
from opencsp.app.sofast.lib.visualize_setup import visualize_setup
from opencsp.common.lib.camera.Camera import Camera
from opencsp.common.lib.csp.FacetEnsemble import FacetEnsemble
from opencsp.app.sofast.lib.DisplayShape import DisplayShape as Display
from opencsp.app.sofast.lib.DefinitionEnsemble import DefinitionEnsemble
from opencsp.app.sofast.lib.DefinitionFacet import DefinitionFacet
from opencsp.app.sofast.lib.SpatialOrientation import SpatialOrientation
from opencsp.common.lib.deflectometry.Surface2DParabolic import Surface2DParabolic
from opencsp.common.lib.opencsp_path.opencsp_root_path import opencsp_code_dir
import opencsp.common.lib.render.figure_management as fm
import opencsp.common.lib.render_control.RenderControlAxis as rca
import opencsp.common.lib.render_control.RenderControlFigure as rcfg
import opencsp.common.lib.render_control.RenderControlMirror as rcm
import opencsp.common.lib.tool.log_tools as lt
import opencsp.common.lib.tool.file_tools as ft


def example_driver():
Expand All @@ -44,9 +46,11 @@ def example_driver():
file_ensemble = join(sample_data_dir, 'Ensemble_lab_6x4.json')

# Define save dir
dir_save = join(os.path.dirname(__file__), 'data/output/facet_ensemble')
if not os.path.exists(dir_save):
os.makedirs(dir_save)
dir_save = join(dirname(__file__), 'data/output/facet_ensemble')
ft.create_directories_if_necessary(dir_save)

# Set up logger
lt.logger(join(dir_save, 'log.txt'))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's typically better to do this step outside a function, such as in the if __name__ == "__main__": control, because lt.logger() should only be called once per process.

That being said, I think it's ok to leave it here, since the example_driver() function essentially is the main process.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll make sure to do that in the future.
I'll leave it here for the time being for the reason you mentioned. Also the save_dir needs to be set up before the log, so I'm fine with leaving it in this file.

# Load data
camera = Camera.load_from_hdf(file_camera)
Expand All @@ -58,12 +62,9 @@ def example_driver():
# Define facet data
facet_data = [DefinitionFacet.load_from_json(file_facet)] * ensemble_data.num_facets

# Define surface data (plano)
# surface_data = [dict(surface_type='plano', robust_least_squares=False, downsample=20)] * ensemble_data.num_facets
# Define surface data (parabolic)
# Define surface data
surface_data = [
dict(
surface_type='parabolic',
Surface2DParabolic(
initial_focal_lengths_xy=(100.0, 100.0),
robust_least_squares=False,
downsample=20,
Expand All @@ -88,12 +89,10 @@ def example_driver():

# Calculate focal length from parabolic fit
for idx in range(sofast.num_facets):
if surface_data[idx]['surface_type'] == 'parabolic':
surf_coefs = sofast.data_characterization_facet[idx].surf_coefs_facet
focal_lengths_xy = [1 / 4 / surf_coefs[2], 1 / 4 / surf_coefs[5]]
print('Parabolic fit focal lengths:')
print(f' X {focal_lengths_xy[0]:.3f} m')
print(f' Y {focal_lengths_xy[1]:.3f} m')
surf_coefs = sofast.data_characterization_facet[idx].surf_coefs_facet
focal_lengths_xy = [1 / 4 / surf_coefs[2], 1 / 4 / surf_coefs[5]]
lt.info(f'Facet {idx:d} xy focal lengths (meters): '
f'{focal_lengths_xy[0]:.3f}, {focal_lengths_xy[1]:.3f}')

# Get optic representation
ensemble: FacetEnsemble = sofast.get_optic()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import os
from os.path import join
from os.path import join, dirname

import matplotlib

from opencsp.app.sofast.lib.DisplayShape import DisplayShape as Display
from opencsp.app.sofast.lib.DefinitionFacet import DefinitionFacet
from opencsp.app.sofast.lib.ImageCalibrationScaling import ImageCalibrationScaling
from opencsp.app.sofast.lib.MeasurementSofastFringe import (
MeasurementSofastFringe as Measurement,
)
from opencsp.app.sofast.lib.ProcessSofastFringe import ProcessSofastFringe as Sofast
from opencsp.app.sofast.lib.SpatialOrientation import SpatialOrientation
from opencsp.app.sofast.lib.visualize_setup import visualize_setup
from opencsp.common.lib.camera.Camera import Camera
from opencsp.common.lib.csp.Facet import Facet
from opencsp.app.sofast.lib.DisplayShape import DisplayShape as Display
from opencsp.app.sofast.lib.DefinitionFacet import DefinitionFacet
from opencsp.app.sofast.lib.SpatialOrientation import SpatialOrientation
from opencsp.common.lib.deflectometry.Surface2DParabolic import Surface2DParabolic
from opencsp.common.lib.opencsp_path.opencsp_root_path import opencsp_code_dir
import opencsp.common.lib.render.figure_management as fm
import opencsp.common.lib.render_control.RenderControlAxis as rca
import opencsp.common.lib.render_control.RenderControlFigure as rcfg
import opencsp.common.lib.tool.log_tools as lt
import opencsp.common.lib.tool.file_tools as ft


def example_driver():
Expand All @@ -40,9 +42,11 @@ def example_driver():
file_facet = join(sample_data_dir, 'Facet_NSTTF.json')

# Define save dir
dir_save = join(os.path.dirname(__file__), 'data/output/single_facet')
if not os.path.exists(dir_save):
os.makedirs(dir_save)
dir_save = join(dirname(__file__), 'data/output/single_facet')
ft.create_directories_if_necessary(dir_save)

# Set up logger
lt.logger(join(dir_save, 'log.txt'))

# Load data
camera = Camera.load_from_hdf(file_camera)
Expand All @@ -52,9 +56,8 @@ def example_driver():
facet_data = DefinitionFacet.load_from_json(file_facet)

# Define surface definition (parabolic surface)
surface_data = dict(
surface_type='parabolic',
initial_focal_lengths_xy=(300.0, 300),
surface = Surface2DParabolic(
initial_focal_lengths_xy=(300., 300.),
robust_least_squares=True,
downsample=10,
)
Expand All @@ -66,15 +69,13 @@ def example_driver():
sofast = Sofast(measurement, camera, display)

# Process
sofast.process_optic_singlefacet(facet_data, surface_data)
sofast.process_optic_singlefacet(facet_data, surface)

# Calculate focal length from parabolic fit
if surface_data['surface_type'] == 'parabolic':
surf_coefs = sofast.data_characterization_facet[0].surf_coefs_facet
focal_lengths_xy = [1 / 4 / surf_coefs[2], 1 / 4 / surf_coefs[5]]
print('Parabolic fit focal lengths:')
print(f' X {focal_lengths_xy[0]:.3f} m')
print(f' Y {focal_lengths_xy[1]:.3f} m')
surf_coefs = sofast.data_characterization_facet[0].surf_coefs_facet
focal_lengths_xy = [1 / 4 / surf_coefs[2], 1 / 4 / surf_coefs[5]]
lt.info(f'Facet xy focal lengths (meters): '
f'{focal_lengths_xy[0]:.3f}, {focal_lengths_xy[1]:.3f}')

# Get optic representation
facet: Facet = sofast.get_optic()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from os.path import join
from os.path import join, dirname

import matplotlib

from opencsp.app.sofast.lib.visualize_setup import visualize_setup
from opencsp.app.sofast.lib.ImageCalibrationScaling import ImageCalibrationScaling
Expand All @@ -11,10 +12,13 @@
from opencsp.app.sofast.lib.SpatialOrientation import SpatialOrientation
from opencsp.common.lib.camera.Camera import Camera
from opencsp.common.lib.csp.Facet import Facet
from opencsp.common.lib.deflectometry.Surface2DParabolic import Surface2DParabolic
from opencsp.common.lib.opencsp_path.opencsp_root_path import opencsp_code_dir
import opencsp.common.lib.render.figure_management as fm
import opencsp.common.lib.render_control.RenderControlAxis as rca
import opencsp.common.lib.render_control.RenderControlFigure as rcfg
import opencsp.common.lib.tool.log_tools as lt
import opencsp.common.lib.tool.file_tools as ft


def example_driver():
Expand All @@ -36,9 +40,11 @@ def example_driver():
file_calibration = join(sample_data_dir, 'image_calibration.h5')

# Save directory
dir_save = join(os.path.dirname(__file__), 'data/output/undefined_facet')
if not os.path.exists(dir_save):
os.makedirs(dir_save)
dir_save = join(dirname(__file__), 'data/output/undefined_facet')
ft.create_directories_if_necessary(dir_save)

# Set up logger
lt.logger(join(dir_save, 'log.txt'))

# Load data
camera = Camera.load_from_hdf(file_camera)
Expand All @@ -47,9 +53,8 @@ def example_driver():
calibration = ImageCalibrationScaling.load_from_hdf(file_calibration)

# Define surface definition (parabolic surface)
surface_data = dict(
surface_type='parabolic',
initial_focal_lengths_xy=(300.0, 300),
surface = Surface2DParabolic(
initial_focal_lengths_xy=(300., 300.),
robust_least_squares=True,
downsample=10,
)
Expand All @@ -62,15 +67,13 @@ def example_driver():
sofast.params.mask_keep_largest_area = True

# Process
sofast.process_optic_undefined(surface_data)
sofast.process_optic_undefined(surface)

# Calculate focal length from parabolic fit
if surface_data['surface_type'] == 'parabolic':
surf_coefs = sofast.data_characterization_facet[0].surf_coefs_facet
focal_lengths_xy = [1 / 4 / surf_coefs[2], 1 / 4 / surf_coefs[5]]
print('Parabolic fit focal lengths:')
print(f' X {focal_lengths_xy[0]:.3f} m')
print(f' Y {focal_lengths_xy[1]:.3f} m')
surf_coefs = sofast.data_characterization_facet[0].surf_coefs_facet
focal_lengths_xy = [1 / 4 / surf_coefs[2], 1 / 4 / surf_coefs[5]]
lt.info(f'Facet xy focal lengths (meters): '
f'{focal_lengths_xy[0]:.3f}, {focal_lengths_xy[1]:.3f}')

# Get optic representation
facet: Facet = sofast.get_optic()
Expand Down
Binary file not shown.
Loading
Loading