12 #include "vircam_channel.h"
13 #include "vircam_utils.h"
18 #define NCHANTAB_COLS 15
19 static const char *chantab_columns[] = {
"channum",
35 static const cpl_type chantab_types[] = {CPL_TYPE_INT,
50 #define NTESTPOSITIVE 7
51 static const char *testpositive[] = {
"channum",
60 static const char *test1[] = {
"dcd1_1",
67 static int vircam_chan_init(
int channum,
int ixmin,
int ixmax,
int iymin,
68 int iymax,
int dcrpix1,
int dcrpix2,
int dcd1_1,
69 int dcd1_2,
int dcd2_1,
int dcd2_2, parquet *p);
70 static void vircam_chan_close(parquet *p);
71 static int vircam_chan_testpos(cpl_table *intab,
const char *column);
72 static int vircam_chan_test1(cpl_table *intab,
const char *column);
73 static int vircam_chan_fill_lin(cpl_table *intab,
int row, parquet *p);
115 int i,missing[NCHANTAB_COLS],wrongtype[NCHANTAB_COLS],ierr,norder;
118 const char *fctid =
"vircam_chantab_verify";
119 char errmsg[1024],colname[SZCOL],msg[1024];
126 cpl_msg_error(fctid,
"Null input channel table");
134 for (i = 0; i < NCHANTAB_COLS; i++) {
137 if (cpl_table_has_column(intab,chantab_columns[i]) != 1) {
140 }
else if (cpl_table_get_column_type(intab,chantab_columns[i]) !=
147 (void)strcat(errmsg,
"These columns are missing or have incorrect types");
148 for (i = 0; i < NCHANTAB_COLS; i++) {
149 if (missing[i] == 1) {
150 (void)strcat(errmsg,
"\n");
151 (void)strcat(errmsg,chantab_columns[i]);
152 (void)strcat(errmsg,
" Missing");
153 }
else if (wrongtype[i] == 1) {
154 (void)strcat(errmsg,
"\n");
155 (void)strcat(errmsg,chantab_columns[i]);
156 (void)strcat(errmsg,
" Wrong type");
159 (void)strcat(errmsg,
"\n");
166 col = cpl_table_get_column_name(intab);
167 while (col != NULL) {
168 if (! strncmp(col,
"coeff_",6)) {
169 if (cpl_table_get_column_type(intab,col) != CPL_TYPE_DOUBLE) {
170 (void)sprintf(msg,
"Column %s must be double\n",col);
171 (void)strcat(errmsg,msg);
174 col = cpl_table_get_column_name(NULL);
181 for (i = 0; i < NTESTPOSITIVE; i++) {
183 if (vircam_chan_testpos(intab,testpositive[i]) == VIR_OK)
189 (void)strcat(errmsg,
"These columns should be positive integers only:");
190 for (i = 0; i < NTESTPOSITIVE; i++) {
191 if (missing[i] == 1) {
192 (void)strcat(errmsg,
"\n");
193 (void)strcat(errmsg,testpositive[i]);
196 (void)strcat(errmsg,
"\n");
202 for (i = 0; i < NTEST1; i++) {
204 if (vircam_chan_test1(intab,test1[i]) == VIR_OK)
210 (void)strcat(errmsg,
"These columns should be integers and (-1,0,1) only:");
211 for (i = 0; i < NTEST1; i++) {
212 if (missing[i] == 1) {
213 (void)strcat(errmsg,
"\n");
214 (void)strcat(errmsg,test1[i]);
217 (void)strcat(errmsg,
"\n");
224 norder = cpl_table_get_int(intab,
"norder",0,&null);
225 nrows = (int)cpl_table_get_nrow(intab);
226 c1 = cpl_table_get_data_double(intab,
"coeff_1");
227 for (i = 0; i < nrows; i++) {
229 (void)strcat(errmsg,
"\ncoeff_1 must be all 1s!\n");
233 for (i = 2; i <= norder; i++) {
235 (void)snprintf(colname,SZCOL,
"coeff_%d",i);
236 if (cpl_table_has_column(intab,colname) != 1) {
243 "\nThese coefficient columns are missing from the channel table:");
244 for (i = 2; i < norder; i++) {
245 if (missing[i] == 1) {
246 (void)snprintf(colname,SZCOL,
"coeff_%d",i);
247 (void)strcat(errmsg,
"\n");
248 (void)strcat(errmsg,colname);
251 (void)strcat(errmsg,
"\n");
256 if (errmsg[0] !=
'\0') {
257 cpl_msg_error(fctid,
"Errors in channel table\n%s",errmsg);
290 int i,oldnorder,j,nr,null;
295 tab = cpl_table_duplicate(
template);
300 oldnorder = cpl_table_get_int(tab,
"norder",0,&null);
301 if (oldnorder > nord) {
302 for (i = nord+1; i <= oldnorder; i++) {
303 (void)snprintf(colname,SZCOL,
"coeff_%d",i);
304 cpl_table_erase_column(tab,colname);
306 }
else if (oldnorder < nord) {
307 for (i = oldnorder+1; i <= nord; i++) {
308 (void)snprintf(colname,SZCOL,
"coeff_%d",i);
309 if (cpl_table_has_column(tab,colname))
311 cpl_table_new_column(tab,colname,CPL_TYPE_DOUBLE);
317 nr = (int)cpl_table_get_nrow(tab);
318 for (i = 0; i < nr; i++) {
319 cpl_table_set_int(tab,
"norder",(cpl_size)i,nord);
320 cpl_table_set_double(tab,
"coeff_1",(cpl_size)i,1.0);
321 for (j = 2; j <= nord; j++) {
322 (void)snprintf(colname,SZCOL,
"coeff_%d",j);
323 cpl_table_set_double(tab,colname,(cpl_size)i,0.0);
363 int i,channum,ixmin,ixmax,iymin,iymax,null;
364 int dcrpix1,dcrpix2,dcd1_1,dcd1_2,dcd2_1,dcd2_2,retval;
374 *np = (int)cpl_table_get_nrow(tab);
375 *p = cpl_malloc(*np*
sizeof(parquet));
379 for (i = 0; i < *np; i++) {
381 channum = cpl_table_get_int(tab,
"channum",(cpl_size)i,&null);
382 ixmin = cpl_table_get_int(tab,
"ixmin",(cpl_size)i,&null);
383 ixmax = cpl_table_get_int(tab,
"ixmax",(cpl_size)i,&null);
384 iymin = cpl_table_get_int(tab,
"iymin",(cpl_size)i,&null);
385 iymax = cpl_table_get_int(tab,
"iymax",(cpl_size)i,&null);
386 dcrpix1 = cpl_table_get_int(tab,
"dcrpix1",(cpl_size)i,&null);
387 dcrpix2 = cpl_table_get_int(tab,
"dcrpix2",(cpl_size)i,&null);
388 dcd1_1 = cpl_table_get_int(tab,
"dcd1_1",(cpl_size)i,&null);
389 dcd1_2 = cpl_table_get_int(tab,
"dcd1_2",(cpl_size)i,&null);
390 dcd2_1 = cpl_table_get_int(tab,
"dcd2_1",(cpl_size)i,&null);
391 dcd2_2 = cpl_table_get_int(tab,
"dcd2_2",(cpl_size)i,&null);
395 retval = vircam_chan_init(channum,ixmin,ixmax,iymin,iymax,dcrpix1,
396 dcrpix2,dcd1_1,dcd1_2,dcd2_1,dcd2_2,pp);
397 if (retval != VIR_OK) {
404 retval = vircam_chan_fill_lin(tab,i,pp);
405 if (retval != VIR_OK) {
442 for (i = 0; i < np; i++)
443 vircam_chan_close(*p+i);
480 ly = (int)(l/(p->delta_i));
481 lx = l - ly*(p->delta_i);
485 kx = (lx - p->Lx)*(p->dcd1_1) + (ly - p->Ly)*(p->dcd1_2);
486 ky = (lx - p->Lx)*(p->dcd2_1) + (ly - p->Ly)*(p->dcd2_2);
490 k = ky*(p->delta_x) + kx;
525 ky = (int)(k/(p->delta_x));
526 kx = k - ky*(p->delta_x);
530 lx = (kx*(p->dcd2_2) - ky*(p->dcd1_2))/(p->det_cd) + p->Lx;
531 ly = (ky*(p->dcd1_1) - kx*(p->dcd2_1))/(p->det_cd) + p->Ly;
535 l = ly*(p->delta_i) + lx;
571 ky = (int)(k/(p->delta_x));
572 kx = k - ky*(p->delta_x);
576 lx = (kx*(p->dcd2_2) - ky*(p->dcd1_2))/(p->det_cd) + p->Lx + p->ixmin - 1;
577 ly = (ky*(p->dcd1_1) - kx*(p->dcd2_1))/(p->det_cd) + p->Ly + p->iymin - 1;
581 l = ly*naxis[0] + lx;
639 static int vircam_chan_init(
int channum,
int ixmin,
int ixmax,
int iymin,
640 int iymax,
int dcrpix1,
int dcrpix2,
int dcd1_1,
641 int dcd1_2,
int dcd2_1,
int dcd2_2, parquet *p) {
645 p->channum = channum;
654 (void)snprintf(p->imsec,SECSIZ,
"[%d:%d,%d:%d]",ixmin,ixmax,iymin,iymax);
655 p->delta_i = ixmax - ixmin + 1;
656 p->delta_j = iymax - iymin + 1;
657 if (dcrpix1 < 0 || dcrpix1 > p->delta_i ||
658 dcrpix2 < 0 || dcrpix2 > p->delta_j) {
659 cpl_msg_error(
"vircam_chan_init",
660 "nonsense values of dcrpix");
669 p->det_cd = dcd1_1*dcd2_2 - dcd1_2*dcd2_1;
670 p->delta_x = abs(dcd1_1*(p->delta_i) + dcd1_2*(p->delta_j));
671 p->delta_y = abs(dcd2_1*(p->delta_i) + dcd2_2*(p->delta_j));
696 static void vircam_chan_close(parquet *p) {
730 static int vircam_chan_testpos(cpl_table *intab,
const char *column) {
735 if (cpl_table_get_column_type(intab,column) != CPL_TYPE_INT)
741 nrows = (int)cpl_table_get_nrow(intab);
742 idata = cpl_table_get_data_int(intab,column);
743 for (i = 0; i < nrows; i++)
778 static int vircam_chan_test1(cpl_table *intab,
const char *column) {
783 if (cpl_table_get_column_type(intab,column) != CPL_TYPE_INT)
789 nrows = (int)cpl_table_get_nrow(intab);
790 idata = cpl_table_get_data_int(intab,column);
791 for (i = 0; i < nrows; i++)
792 if (idata[i] != 0 && idata[i] != -1 && idata[i] != 1)
824 static int vircam_chan_fill_lin(cpl_table *intab,
int row, parquet *p) {
830 p->norder = cpl_table_get_int(intab,
"norder",row,&null);
834 p->bb = cpl_malloc(p->norder*
sizeof(
double));
838 for (i = 1; i <= p->norder; i++) {
839 (void)snprintf(colname,SZCOL,
"coeff_%d",i);
840 p->bb[i-1] = cpl_table_get_double(intab,colname,(cpl_size)row,&null);