35 #include "gimessages.h"
37 #include "gifiberutils.h"
39 #include "gislitgeometry.h"
65 _giraffe_slitgeometry_insert(
GiSlitGeometry *
self, cxint pos, cxint nrow,
73 if (self->subslits == NULL) {
77 if ((pos < 0) || (pos > self->nsubslits)) {
81 if (self->subslits[pos] != NULL) {
82 cpl_matrix_delete(self->subslits[pos]);
85 self->subslits[pos] = cpl_matrix_new(nrow, ncol);
113 self->subslits = NULL;
194 const cxchar *fctid =
"giraffe_slitgeometry_create";
197 const cxchar *c_xf =
"XF";
198 const cxchar *c_yf =
"YF";
199 const cxchar *c_fps =
"FPS";
200 const cxchar *c_ssn =
"SSN";
201 const cxchar *c_rindex = NULL;
205 cxint max_nsubslits = 0;
209 cxint column_index = 0;
211 cpl_matrix *nsubslits = NULL;
213 cpl_table *_slitgeometry = NULL;
218 if (slitgeometry == NULL) {
229 nfibers = cpl_table_get_nrow(_slitgeometry);
231 self->xf = cpl_matrix_new(nfibers, 1);
232 self->yf = cpl_matrix_new(nfibers, 1);
233 self->fps = cpl_matrix_new(nfibers, 1);
234 self->rindex = cpl_matrix_new(nfibers, 1);
236 nsubslits = cpl_matrix_new(nfibers, 1);
247 for (i = 0; i < nfibers; i++) {
249 cxint _nsubslits = cpl_table_get_int(_slitgeometry, c_ssn, i, NULL);
250 cxint _fps = cpl_table_get_int(_slitgeometry, c_fps, i, NULL) - 1;
251 cxint _index = cpl_table_get_int(_slitgeometry, c_rindex,
254 cxdouble _xf = cpl_table_get(_slitgeometry, c_xf, i, NULL);;
255 cxdouble _yf = cpl_table_get(_slitgeometry, c_yf, i, NULL);
258 if (_nsubslits > max_nsubslits) {
259 max_nsubslits = _nsubslits;
262 status = cpl_matrix_set(self->xf, i, 0, _xf);
263 status = cpl_matrix_set(self->yf, i, 0, _yf);
264 status = cpl_matrix_set(self->fps, i, 0, (cxdouble)_fps);
265 status = cpl_matrix_set(self->rindex, i, 0, (cxdouble)_index);
267 status = cpl_matrix_set(nsubslits, i, 0, (cxdouble)_nsubslits);
282 for (i = 1; i <= max_nsubslits; i++) {
286 cpl_matrix *ref_matrix = NULL;
289 for (j = 0; j < nfibers; j++) {
290 curr_ssn = (cxint) cpl_matrix_get(nsubslits, j, 0);
297 _giraffe_slitgeometry_insert(
self, i - 1, count, 1);
302 for (j = 0; j < nfibers; j++) {
304 curr_ssn = (cxint) cpl_matrix_get(nsubslits, j, 0);
308 status = cpl_matrix_set(ref_matrix, column_index, 0,
315 cpl_msg_debug(fctid,
"Using multiple slits for Slit Geometry");
322 cpl_matrix *ref_matrix = NULL;
325 _giraffe_slitgeometry_insert(
self, 0, nfibers, 1);
329 for (j = 0; j < nfibers; j++) {
331 status = cpl_matrix_set(ref_matrix, j, 0, (cxdouble)j);
335 cpl_msg_debug(fctid,
"Using single slit for Slit Geometry");
363 if (self->subslits != NULL) {
367 for (i = 0; i <
self->nsubslits; i++) {
368 cpl_matrix_delete(self->subslits[i]);
371 cx_free(self->subslits);
401 if (self->subslits != NULL) {
402 return self->nsubslits;
430 if (size == self->nsubslits) {
434 if (self->subslits != NULL) {
438 for (i = 0; i <
self->nsubslits; i++) {
439 cpl_matrix_delete(self->subslits[i]);
444 cx_free(self->subslits);
446 self->nsubslits = size;
447 self->subslits = cx_calloc(self->nsubslits,
sizeof(cpl_matrix *));
478 if (self->subslits == NULL) {
482 if ((pos < 0) || (pos > self->nsubslits)) {
486 if (self->subslits[pos] != NULL) {
487 cpl_matrix_delete(self->subslits[pos]);
491 self->subslits[pos] = cpl_matrix_duplicate(nm);
494 self->subslits[pos] = NULL;
523 if (self->subslits == NULL) {
527 if ((pos < 0) || (pos > self->nsubslits)) {
531 return self->subslits[pos];
550 const cxchar *fctid =
"giraffe_slitgeometry_print";
561 if (self->subslits == NULL) {
562 gi_message(fctid,
"Invalid slit geometry, no slit matrices "
568 for (i = 0; i <
self->nsubslits; i++) {
572 cpl_matrix *ref = NULL;
575 nrow = cpl_matrix_get_nrow(ref);
607 cxint pos,
const cxchar *tag)
610 const cxchar *fctid =
"giraffe_slitgeometry_load";
613 const cxchar *fps_name =
"FPS";
614 const cxchar *ridx = NULL;
619 cpl_propertylist *properties = NULL;
621 cpl_table *_fibers = NULL;
622 cpl_table *_slit_geometry = NULL;
624 GiTable *slit_geometry = NULL;
626 GiInstrumentMode mode;
630 if (fibers == NULL) {
631 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
637 if (_fibers == NULL) {
638 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
647 properties = cpl_propertylist_load(filename, 0);
649 if (properties == NULL) {
650 cpl_msg_error(fctid,
"Cannot load properies of data set 0 "
651 "from `%s'!", filename);
652 cpl_propertylist_delete(properties);
659 if (mode == GIMODE_NONE) {
660 cpl_msg_error(fctid,
"Invalid instrument mode!");
662 cpl_propertylist_delete(properties);
668 cpl_propertylist_delete(properties);
677 giraffe_error_push();
680 if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
681 cpl_msg_error(fctid,
"Data set %d in `%s' is not a slit "
682 "geometry table!", pos, filename);
687 cpl_msg_error(fctid,
"Cannot load data set %d (slit geometry) "
688 "from `%s!", pos, filename);
698 if (!cpl_table_has_column(_slit_geometry, fps_name)) {
699 if (cpl_table_has_column(_slit_geometry,
"NSPEC")) {
700 cpl_msg_warning(fctid,
"Slit geometry loaded from `%s' uses "
701 "deprecated OGL column names.", filename);
712 cpl_table_duplicate_column(_slit_geometry, fps_name,
713 _slit_geometry,
"NSPEC");
715 cpl_table_name_column(_slit_geometry,
"NSPEC",
"INDEX");
717 if (mode == GIMODE_ARGUS) {
719 cxint nrow = cpl_table_get_nrow(_slit_geometry);
721 for (i = 0; i < nrow; i++) {
723 cxint idx = cpl_table_get_int(_slit_geometry,
"INDEX",
726 cpl_table_set_int(_slit_geometry, fps_name, i, idx);
734 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
746 nfibers = cpl_table_get_nrow(_fibers);
748 cpl_table_unselect_all(_slit_geometry);
750 for (i = 0; i < cpl_table_get_nrow(_slit_geometry); i++) {
752 cxint fps = cpl_table_get_int(_slit_geometry, fps_name, i, NULL);
755 for (j = 0; j < nfibers; j++) {
757 cxint _fps = cpl_table_get_int(_fibers,
"FPS", j, NULL);
760 cpl_table_select_row(_slit_geometry, i);
768 _slit_geometry = cpl_table_extract_selected(_slit_geometry);
772 cpl_table_new_column(_slit_geometry,
"RINDEX", CPL_TYPE_INT);
774 for (i = 0; i < cpl_table_get_nrow(_slit_geometry); i++) {
776 cxint fps = cpl_table_get_int(_slit_geometry, fps_name, i, NULL);
779 for (j = 0; j < nfibers; j++) {
781 cxint _fps = cpl_table_get_int(_fibers,
"FPS", j, NULL);
785 cxint _ridx = cpl_table_get_int(_fibers, ridx, j , NULL);
787 cpl_table_set_int(_slit_geometry,
"RINDEX", i, _ridx);
802 if (strcmp(fps_name,
"FPS") != 0) {
803 cpl_table_name_column(_slit_geometry, fps_name,
"FPS");
811 for (i = 0; i < cpl_table_get_nrow(_slit_geometry); i++) {
812 cpl_table_set_int(_slit_geometry,
"INDEX", i, i + 1);
817 cpl_table_delete(_slit_geometry);
818 _slit_geometry = NULL;
820 return slit_geometry;
826 giraffe_slitgeometry_save(
const GiTable *slitgeometry)
829 cpl_frame *frame = NULL;
846 CPL_FRAME_LEVEL_FINAL,