00001
00009
00010
00011 #include <IFU_io.h>
00012 #include <data_io.h>
00013 #include <values.h>
00014
00015 #include "../cfitsio/fitsio.h"
00016 #include "../cfitsio/longnam.h"
00017
00018 extern void confirme_erase(char *name);
00019 extern int ASK;
00020 extern IO_Format InputIO, OutputIO;
00021
00022 #define CREAT_FILE_PERM 0666
00023 #define OPEN_FILE_PERM 0644
00024 #define DATA_UNDEF -MAXSHORT
00025 #define LENS_UNDEF -MAXINT
00026
00027 #define find_lens(frame,type_of_spec,no_lens,i) \
00028 for (*(i)=0; *(i)<frame->nbspec \
00029 && frame->type_of_spec[*(i)].specId != no_lens \
00030 && frame->type_of_spec[*(i)].specId != LENS_UNDEF; (*(i))++); \
00031 if ((frame->type_of_spec[*(i)].specId == LENS_UNDEF) \
00032 || (*(i) == frame->nbspec)) \
00033 *(i) = -1
00034
00035
00039
00045
00046
00047 int set_tiger_group(E3D_file *frame)
00048 {
00049 char instrument[lg_label+1];
00050
00051 instrument[0] = 0;
00052 frame->ngroups = 1;
00053 frame->groups = (GROUP *)malloc(1*sizeof(GROUP));
00054 frame->groups[0].groupId = 1;
00055 frame->groups[0].shape = SQUARE;
00056 frame->groups[0].size1 = 1;
00057 frame->groups[0].size2 = 0;
00058
00059 disable_user_warnings();
00060 RD_desc(frame,"INSTRUME",CHAR,lg_label,instrument);
00061 upper_strg(instrument);
00062 restore_user_warnings();
00063
00064 if (strcmp(instrument,"OASIS") == 0) {
00065 frame->groups[0].shape = HEXAGON;
00066 frame->groups[0].size1 = 1.19;
00067 frame->groups[0].size2 = 0;
00068 }
00069 if (strcmp(instrument,"SAURON") == 0) {
00070 frame->groups[0].shape = SQUARE;
00071 frame->groups[0].size1 = 1.308;
00072 frame->groups[0].size2 = 0;
00073 }
00074 frame->groups[0].angle = 0;
00075 frame->groups[0].poswav = 0;
00076 frame->groups[0].airmass = 0;
00077 frame->groups[0].parang = 0;
00078 frame->groups[0].pressure = 0;
00079 frame->groups[0].temperature = 0;
00080 frame->groups[0].rel_humidity = 0;
00081 return(0);
00082 }
00083
00084
00099
00100
00101 int
00102 create_tiger_frame(E3D_file *frame,char *name,int npix,double start,double step,short datatype,char *table_name,char *ident,char *unit)
00103 {
00104 int status;
00105
00106 status = create_E3D_file(frame,name,npix,start,step,datatype,ident,unit);
00107
00108 if (!status) {
00109 if (frame->data_format == EURO3D_FORMAT)
00110 sprintf(frame->table_name,"%s[%s]",frame->name,E3D_DATA);
00111
00112 if (frame->data_format == TIGER_FORMAT)
00113 strcpy(frame->table_name,table_name);
00114
00115 set_tiger_group(frame);
00116 }
00117 return(status);
00118 }
00119
00120
00126
00127
00128 int close_tiger_frame(E3D_file *frame)
00129 {
00130 set_tiger_group(frame);
00131 return(close_E3D_file(frame));
00132 }
00133
00134
00135
00144
00145 int
00146 delete_tiger_spec(E3D_file *frame, SPECTRUM *signal, SPECTRUM *noise, int specId)
00147 {
00148 int status, i=-1, j=-1;
00149 char errtext[132];
00150 E3Dspec_desc *current_spec;
00151
00152 status = ERR_NOIMA;
00153 if (frame->signal != NULL && signal != NULL) {
00154 find_lens(frame,signal,specId,&i);
00155 if (i >= 0) {
00156 current_spec = frame->signal+i;
00157 if (i < (frame->nbspec -1))
00158 memcpy((char *)current_spec,(char *)(frame->signal+i+1),
00159 (frame->nbspec-i-1)*sizeof(E3Dspec_desc));
00160 status = free_spec_mem(signal);
00161 }
00162 }
00163 if (frame->noise != NULL && noise != NULL) {
00164 find_lens(frame,noise,specId,&j);
00165 if (j >= 0) {
00166 current_spec = frame->noise+j;
00167 if (j < (frame->nbspec -1))
00168 memcpy((char *)current_spec,(char *)(frame->noise+j+1),
00169 (frame->nbspec-j-1)*sizeof(E3Dspec_desc));
00170 status = free_spec_mem(noise);
00171 }
00172 }
00173 if (i>=0 || j>=0)
00174 frame->nbspec--;
00175 if (status) {
00176 sprintf(errtext,"delete_tiger_spec: spec %s",signal->name);
00177 Handle_Error(errtext,status);
00178 }
00179 return(status);
00180 }
00181
00182
00188
00189
00190 int set_bigendian(short type) {
00191
00192 union {
00193 short type;
00194 char octets[2];
00195 } bytes;
00196
00197 bytes.type = type;
00198 if (bytes.octets[0] == 0) {
00199 bytes.type = 1;
00200 if (bytes.octets[0] == 0)
00201 return(0);
00202 else
00203 return(1);
00204 } else {
00205 bytes.type = 1;
00206 if (bytes.octets[0] == 0)
00207 return(1);
00208 else
00209 return(0);
00210 }
00211
00212 }
00213
00214
00221
00222 int
00223 get_assoc_table_name(char *name, char *tab_name)
00224 {
00225 char errtext[132], filename[lg_name+1];
00226 int status=0;
00227 int iomode;
00228 char buffer[lg_hist+1];
00229 int imno;
00230
00231 strcpy(filename,name);
00232 first_blk(filename);
00233 iomode = O_RDWR;
00234
00235 append_datacube_extension(filename,InputIO.datacubes);
00236
00237 switch (InputIO.datacubes) {
00238
00239 case TIGER_FORMAT :
00240 imno = open(filename,iomode,OPEN_FILE_PERM);
00241 if (imno <0) {
00242 status = ERR_OPEN;
00243 sprintf(errtext,"get_assoc_table_name: frame %s",filename);
00244 Handle_Error(errtext,status);
00245 return(status);
00246 }
00247 read(imno,buffer,lg_version);
00248
00249 if (strncmp(buffer,"v1.0",4) != 0) {
00250 close(imno);
00251 status = ERR_BAD_TYPE;
00252 sprintf(errtext,"get_assoc_table_name: frame %s",filename);
00253 Handle_Error(errtext,status);
00254 return(status);
00255 };
00256 read(imno,buffer,lg_ident);
00257 read(imno,buffer,lg_unit);
00258 read(imno,tab_name,lg_name);
00259 close(imno);
00260 break;
00261
00262 case EURO3D_FORMAT :
00263 status = 0;
00264 sprintf(tab_name,"%s[%s]",filename,E3D_DATA);
00265 break;
00266 }
00267 return(status);
00268 }
00269
00276
00277 int get_lenses_no_from_table(E3D_file *frame, int *specId)
00278 {
00279 int status = 0;
00280 int i, j, k;
00281 char errtext[132];
00282 char table_name[lg_name+1];
00283 int col_no, nol;
00284 TABLE tbl_lens;
00285 E3Dspec_desc *pt_spec = NULL;
00286
00287 if (frame->signal != NULL) {
00288 pt_spec = frame->signal;
00289 }
00290 else {
00291 if (frame->noise != NULL)
00292 pt_spec = frame->noise;
00293 }
00294 strcpy(table_name,frame->table_name);
00295 append_tbl_extension(table_name,InputIO.basic_io);
00296 status = open_table(&tbl_lens,table_name,"I");
00297 if (!status) {
00298 handle_select_flag(&tbl_lens,'W',NULL);
00299 col_no = get_col_ref(&tbl_lens,E3D_COL_ID);
00300 if (col_no < 0) {
00301 status = ERR_NOCOL;
00302 sprintf(errtext,"get_lenses_no_from_table");
00303 Handle_Error(errtext,status);
00304 return(status);
00305 }
00306 if (pt_spec != NULL) {
00307 for (i=0, j=0; i<tbl_lens.row;i++) {
00308 RD_tbl(&tbl_lens,i,col_no,&nol);
00309 for (k=0; k<frame->nbspec && pt_spec[k].specId != nol; k++);
00310 if (pt_spec[k].specId == nol) {
00311 specId[j] = nol;
00312 j++;
00313 }
00314 }
00315 }
00316 else {
00317
00318 for (i=0; i< tbl_lens.row;i++) {
00319 RD_tbl(&tbl_lens,i,col_no,&nol);
00320 specId[i] = nol;
00321 }
00322 j = i;
00323 }
00324 close_table(&tbl_lens);
00325 };
00326 if (status) {
00327 sprintf(errtext,"get_lenses_no_from_table");
00328 Handle_Error(errtext,status);
00329 return(status);
00330 }
00331 return(j);
00332 }
00333
00334
00346
00347 int get_lenses_coord(E3D_file *frame, char *lab_xcol, char *lab_ycol,
00348 int *specId, float *xlens, float *ylens, int *il)
00349 {
00350 int status = 0;
00351 int i, col_no, col_xl, col_yl, found=1, k;
00352 char errtext[132];
00353 char table_name[lg_name+1];
00354 E3Dspec_desc *pt_spec = NULL;
00355 TABLE tbl_lens;
00356
00357 if (frame->signal != NULL)
00358 pt_spec = frame->signal;
00359 else {
00360 if (frame->noise != NULL)
00361 pt_spec = frame->noise;
00362 }
00363 strcpy(table_name,frame->table_name);
00364 append_tbl_extension(table_name,InputIO.basic_io);
00365
00366 status = open_table(&tbl_lens,table_name,"I");
00367 if (!status) {
00368 col_no = get_col_ref(&tbl_lens,E3D_COL_ID);
00369 col_xl = get_col_ref(&tbl_lens,lab_xcol);
00370 col_yl = get_col_ref(&tbl_lens,lab_ycol);
00371 if ((col_no < 0) || (col_xl < 0) || (col_yl < 0)) {
00372 status = ERR_NOCOL;
00373 sprintf(errtext,"get_ID_and_coordinates");
00374 Handle_Error(errtext,status);
00375 return(status);
00376 }
00377 if (pt_spec == NULL) {
00378 RD_col(&tbl_lens,col_no,specId);
00379 RD_col(&tbl_lens,col_xl,xlens);
00380 RD_col(&tbl_lens,col_yl,ylens);
00381 for (i=0; i<tbl_lens.row;i++)
00382 il[i] = i;
00383 }
00384 else {
00385 for (i=0; i<frame->nbspec && found ;i++) {
00386 specId[i] = pt_spec[i].specId;
00387 k = search_in_col(&tbl_lens,col_no,specId+i);
00388 if (k < 0) {
00389 found = 0;
00390 status = ERR_NODATA;
00391 }
00392 else {
00393 RD_tbl(&tbl_lens,k,col_xl,xlens+i);
00394 RD_tbl(&tbl_lens,k,col_yl,ylens+i);
00395 il[i] = k;
00396 }
00397 }
00398 }
00399 close_table(&tbl_lens);
00400 };
00401 if (status) {
00402 sprintf(errtext,"get_lenses_coord");
00403 Handle_Error(errtext,status);
00404 return(status);
00405 }
00406 return(status);
00407 }
00408
00420
00421 int set_lenses_coord(E3D_file *frame,char *lab_xcol,char *lab_ycol,int nbl,int *specId, float *xlens, float *ylens)
00422 {
00423 int status = 0;
00424 int i, col_no, col_xl, col_yl;
00425 char errtext[132];
00426 TABLE tbl_lens;
00427 char table_name[lg_name+1];
00428
00429 set_ID_and_coordinates(frame,nbl,specId,xlens,ylens);
00430
00431 strcpy(table_name,frame->table_name);
00432 append_tbl_extension(table_name,InputIO.basic_io);
00433
00434 if (exist(table_name)) {
00435 status = open_table(&tbl_lens,table_name,"IO");
00436 } else {
00437 status = create_table(&tbl_lens,table_name,nbl,3,'W',NULL);
00438 }
00439 if (status < 0) {
00440 sprintf(errtext,"set_lenses_coord");
00441 Handle_Error(errtext,status);
00442 return(status);
00443 }
00444 col_no = create_col(&tbl_lens,LAB_COL_NO,LONG,'R',"I4","none");
00445 col_xl = create_col(&tbl_lens,lab_xcol,FLOAT,'R',"F9.6","pixels");
00446 col_yl = create_col(&tbl_lens,lab_ycol,FLOAT,'R',"F9.6","pixels");
00447 for (i=0;i<nbl;i++) {
00448 WR_tbl(&tbl_lens,i,col_no,specId+i);
00449 WR_tbl(&tbl_lens,i,col_xl,xlens+i);
00450 WR_tbl(&tbl_lens,i,col_yl,ylens+i);
00451 }
00452 close_table(&tbl_lens);
00453 return(status);
00454
00455 }
00456
00457
00470
00471 int get_lenses_coord_select(E3D_file *frame, char *lab_xcol, char *lab_ycol,
00472 int *specId, float *xlens, float *ylens,
00473 int *il, int *nbsel)
00474 {
00475 int status = 0;
00476 int i, col_no, col_xl, col_yl, k, *no, l;
00477 char errtext[132];
00478 char table_name[lg_name+1];
00479 E3Dspec_desc *pt_spec = NULL;
00480 TABLE tbl_lens;
00481
00482 if (frame->signal != NULL)
00483 pt_spec = frame->signal;
00484 else {
00485 if (frame->noise != NULL)
00486 pt_spec = frame->noise;
00487 }
00488 strcpy(table_name,frame->table_name);
00489 status = open_table(&tbl_lens,table_name,"I");
00490 handle_select_flag(&tbl_lens, 'W', errtext);
00491
00492 if (!status) {
00493 col_no = get_col_ref(&tbl_lens,E3D_COL_ID);
00494 col_xl = get_col_ref(&tbl_lens,lab_xcol);
00495 col_yl = get_col_ref(&tbl_lens,lab_ycol);
00496 if ((col_no < 0) || (col_xl < 0) || (col_yl < 0)) {
00497 status = ERR_NOCOL;
00498 sprintf(errtext,"get_lenses_coord");
00499 Handle_Error(errtext,status);
00500 return(status);
00501 }
00502 if (pt_spec == NULL) {
00503 RD_col(&tbl_lens,col_no,specId);
00504 RD_col(&tbl_lens,col_xl,xlens);
00505 RD_col(&tbl_lens,col_yl,ylens);
00506 for (i=0; i<tbl_lens.row;i++)
00507 il[i] = i;
00508 *nbsel = tbl_lens.row;
00509 }
00510 else {
00511 no = (int *)malloc(tbl_lens.row*sizeof(int));
00512 RD_col(&tbl_lens,col_no,no);
00513 *nbsel = 0;
00514 for (i=0; i<frame->nbspec; i++) {
00515 specId[*nbsel] = pt_spec[i].specId;
00516 for (k=-1, l=0; l<tbl_lens.row; l++) {
00517 if (specId[*nbsel] == no[l]) {
00518 k = l;
00519 break;
00520 }
00521 }
00522 if (k < 0) {
00523 continue;
00524 }
00525 else {
00526 RD_tbl(&tbl_lens,k,col_xl,&(xlens[*nbsel]));
00527 RD_tbl(&tbl_lens,k,col_yl,&(ylens[*nbsel]));
00528 il[*nbsel] = k;
00529 (*nbsel)++;
00530 }
00531 }
00532 free(no);
00533 }
00534 close_table(&tbl_lens);
00535 };
00536 if (status) {
00537 sprintf(errtext,"get_lenses_coord_select");
00538 Handle_Error(errtext,status);
00539 }
00540 return(status);
00541 }
00542
00543
00551
00552 int
00553 init_new_tiger_slice(E3D_file *frame, SLICE *slice, int npts)
00554 {
00555 char errtext[132];
00556 int status, i;
00557 E3Dspec_desc *pt_spec = NULL;
00558
00559 slice->index = -1;
00560 slice->npts = npts;
00561 slice->data_type = frame->data_type;
00562
00563 status = 0;
00564 slice->specId = (int *)calloc(slice->npts,sizeof(int));
00565 if (slice->specId == 0) status = ERR_ALLOC;
00566 if (frame->signal != NULL)
00567 pt_spec = frame->signal;
00568 else {
00569 if (frame->noise != NULL)
00570 pt_spec = frame->noise;
00571 }
00572 if (pt_spec == NULL) {
00573 get_lenses_no_from_table(frame,slice->specId);
00574 }
00575 else {
00576 for (i=0;i<slice->npts;i++)
00577 slice->specId[i] = pt_spec[i].specId;
00578 }
00579
00580 if (status == 0) {
00581 switch(slice->data_type) {
00582 case SHORT :
00583 slice->data.s_data = (short *)calloc(slice->npts,sizeof(short));
00584 if (slice->data.s_data == 0) status = ERR_ALLOC;
00585 break;
00586 case INT :
00587 case LONG :
00588 slice->data.l_data = (long *)calloc(slice->npts,sizeof(long));
00589 if (slice->data.l_data == 0) status = ERR_ALLOC;
00590 break;
00591 case FLOAT :
00592 slice->data.f_data = (float *)calloc(slice->npts,sizeof(float));
00593 if (slice->data.f_data == 0) status = ERR_ALLOC;
00594 break;
00595 case DOUBLE :
00596 slice->data.d_data = (double *)calloc(slice->npts,sizeof(double));
00597 if (slice->data.d_data == 0) status = ERR_ALLOC;
00598 break;
00599 }
00600 slice->quality = (unsigned long *)calloc(slice->npts,sizeof(unsigned long));
00601 }
00602 if (status) {
00603 sprintf(errtext,"init_new_tiger_slice");
00604 Handle_Error(errtext,status);
00605 }
00606 return(status);
00607 }
00608 #define extract_3D_slice_noalloc(type_of_slice) \
00609 type_of_slice->index = index; \
00610 current_spec = &(frame->type_of_slice[0]); \
00611 for (i=0;i <frame->nbspec; i++, current_spec++) { \
00612 type_of_slice->specId[i] = current_spec->specId; \
00613 if (index < current_spec->npix) { \
00614 lseek(frame->imno,(current_spec->data_offset \
00615 +index*sizeof_item(type_of_slice->data_type)),SEEK_SET); \
00616 if (frame->swapbytes) {\
00617 read_DOS(frame->imno,(char *)((char *)(type_of_slice->data.s_data)+\
00618 i*sizeof_item(type_of_slice->data_type)), \
00619 sizeof_item(type_of_slice->data_type)); \
00620 } else { \
00621 read(frame->imno,(char *)((char *)(type_of_slice->data.s_data)+\
00622 i*sizeof_item(type_of_slice->data_type)), \
00623 sizeof_item(type_of_slice->data_type)); \
00624 } \
00625 }\
00626 else \
00627 WR_slice(type_of_slice,i,DATA_UNDEF); \
00628 }
00629
00630 int
00631 get_3D_slice_noalloc(E3D_file *frame, SLICE *signal, SLICE *noise,
00632 int index)
00633 {
00634 char errtext[132];
00635 int i, status=0;
00636 E3Dspec_desc *current_spec;
00637
00638 if (! has_common_bounds(frame)) {
00639 sprintf(errtext,"get_3D_slice : Unconsistent data");
00640 Handle_Error(errtext,ERR_BAD_PARAM);
00641 return(ERR_BAD_PARAM);
00642 }
00643
00644 if (signal != NULL) {
00645 extract_3D_slice_noalloc(signal);
00646 }
00647 if (noise != NULL) {
00648 extract_3D_slice_noalloc(noise);
00649 }
00650
00651 return(status);
00652 }
00653
00654
00663
00664 int get_lens_coordinates(E3D_file *frame, int specId, float *xlens, float *ylens)
00665 {
00666 int status = 0;
00667 int i, col_no, col_xl, col_yl, no;
00668 char errtext[132];
00669 char table_name[lg_name+1];
00670 TABLE tbl_lens;
00671
00672 strcpy(table_name,frame->table_name);
00673 status = open_table(&tbl_lens,table_name,"I");
00674
00675 no = specId;
00676
00677 if (!status) {
00678 col_no = get_col_ref(&tbl_lens,LAB_COL_NO);
00679 col_xl = get_col_ref(&tbl_lens,LAB_COL_XLD);
00680 col_yl = get_col_ref(&tbl_lens,LAB_COL_YLD);
00681
00682 if ((col_no < 0) || (col_xl < 0) || (col_yl < 0)) {
00683 status = ERR_NOCOL;
00684 sprintf(errtext,"get_lens_coordinates");
00685 Handle_Error(errtext,status);
00686 return(status);
00687 }
00688 i = search_in_col(&tbl_lens,col_no,&no);
00689 RD_tbl(&tbl_lens,i,col_xl,xlens);
00690 RD_tbl(&tbl_lens,i,col_yl,ylens);
00691 close_table(&tbl_lens);
00692
00693 } else {
00694 sprintf(errtext,"get_lens_coordinates");
00695 Handle_Error(errtext,status);
00696 }
00697 return(status);
00698 }
00699
00708
00709 int set_lens_coordinates(E3D_file *frame, int specId, float *xlens, float *ylens)
00710 {
00711 int status = 0;
00712 int i, col_no, col_xl, col_yl, no;
00713 char errtext[132];
00714 char table_name[lg_name+1];
00715 TABLE tbl_lens;
00716
00717 no = specId;
00718
00719 strcpy(table_name,frame->table_name);
00720 append_tbl_extension(table_name,OutputIO.basic_io);
00721
00722 if (exist(table_name)) {
00723 status = open_table(&tbl_lens,table_name,"IO");
00724 if (status) {
00725 sprintf(errtext,"set_lens_coordinates");
00726 Handle_Error(errtext,status);
00727 return(status);
00728 }
00729 col_no = get_col_ref(&tbl_lens,LAB_COL_NO);
00730 col_xl = get_col_ref(&tbl_lens,LAB_COL_XLD);
00731 col_yl = get_col_ref(&tbl_lens,LAB_COL_YLD);
00732 if (col_no >= 0)
00733 i = search_in_col(&tbl_lens,col_no,&no);
00734 if (i < 0) i=tbl_lens.row;
00735 } else {
00736 status = create_table(&tbl_lens,table_name,-1,3,'W',NULL);
00737 if (status) {
00738 sprintf(errtext,"set_lens_coordinates");
00739 Handle_Error(errtext,status);
00740 return(status);
00741 }
00742 col_no = create_col(&tbl_lens,LAB_COL_NO,LONG,'R',"I4","none");
00743 col_xl = create_col(&tbl_lens,LAB_COL_XLD,FLOAT,'R',"F9.6","pixels");
00744 col_yl = create_col(&tbl_lens,LAB_COL_YLD,FLOAT,'R',"F9.6","pixels");
00745 i = 0;
00746 }
00747
00748 if ((col_no < 0) || (col_xl < 0) || (col_yl < 0)) {
00749 status = ERR_NOCOL;
00750 sprintf(errtext,"set_lens_coordinates");
00751 Handle_Error(errtext,status);
00752 return(status);
00753 }
00754 WR_tbl(&tbl_lens,i,col_no,&no);
00755 WR_tbl(&tbl_lens,i,col_xl,xlens);
00756 WR_tbl(&tbl_lens,i,col_yl,ylens);
00757 close_table(&tbl_lens);
00758
00759 return(status);
00760 }
00761
00768
00776
00777
00778 int
00779 alloc_3D_max(Maxima_Set *maxima, int nblines, int nbmax_by_line)
00780 {
00781 int i, j;
00782 int nby = nblines, max_pts = nbmax_by_line;
00783
00784 maxima->line = (max_lines *)malloc(nby*sizeof(max_lines));
00785 if (maxima->line == NULL) {
00786 Handle_Error("alloc_3D_max",ERR_ALLOC);
00787 return(ERR_ALLOC);
00788 }
00789 for (i=0; i<nby; i++) {
00790 maxima->line[i].ycoord = -MAXFLOAT;
00791 maxima->line[i].nb_max=0;
00792 maxima->line[i].maxima = (max_param *)malloc(max_pts*sizeof(max_param));
00793 if (maxima->line[i].maxima == NULL) {
00794 Handle_Error("alloc_3D_max",ERR_ALLOC);
00795 return(ERR_ALLOC);
00796 }
00797 for (j=0; j<max_pts; j++) {
00798 maxima->line[i].maxima[j].xcoord = -MAXFLOAT;
00799 maxima->line[i].maxima[j].intens = -MAXFLOAT;
00800 maxima->line[i].maxima[j].sigma[0] = -MAXFLOAT;
00801 maxima->line[i].maxima[j].sigma[1] = -MAXFLOAT;
00802 maxima->line[i].maxima[j].alpha = -MAXFLOAT;
00803 }
00804 }
00805 return(OK);
00806 }
00807
00808
00815
00816 int
00817 load_3D_max(Maxima_Set *maxima, char *name)
00818 {
00819 int fdmax, i, j, ipix;
00820 char errtext[132], filename[lg_name+1];
00821
00822 strcpy(filename,name);
00823 if (strchr(filename,'.') == NULL)
00824 strcat(filename,".max");
00825 fdmax = open(filename,O_RDONLY,OPEN_FILE_PERM);
00826 if (fdmax < 0) {
00827 sprintf(errtext,"load_3D_max: frame %s",filename);
00828 Handle_Error(errtext,ERR_OPEN);
00829 return(ERR_OPEN);
00830 }
00831 read(fdmax,maxima->config_name,21*sizeof(char));
00832 read(fdmax,&(maxima->enlarger.gamma),sizeof(double));
00833 read(fdmax,&(maxima->wedges.theta_w),sizeof(double));
00834 read(fdmax,&(maxima->grism.index_coef),sizeof(int));
00835 read(fdmax,&(maxima->grism.A),sizeof(double));
00836 read(fdmax,&(maxima->grism.g_per_mm),sizeof(int));
00837 read(fdmax,&(maxima->filter.inf_util),sizeof(double));
00838 read(fdmax,&(maxima->filter.sup_util),sizeof(double));
00839 read(fdmax,&(maxima->telescope.fratio),sizeof(float));
00840 if ((maxima->telescope.fratio < 5) || (maxima->telescope.fratio > 50)) {
00841
00842 lseek(fdmax,-sizeof(float),SEEK_CUR);
00843 read(fdmax,&ipix,sizeof(int));
00844 maxima->telescope.fratio = (float)ipix;
00845 }
00846 read(fdmax,&(maxima->ccd.nx),sizeof(int));
00847 read(fdmax,&(maxima->ccd.ny),sizeof(int));
00848 read(fdmax,&(maxima->ccd.pixsize),sizeof(float));
00849 if ((maxima->ccd.pixsize < 5) || (maxima->ccd.pixsize > 50)) {
00850
00851 lseek(fdmax,-sizeof(float),SEEK_CUR);
00852 read(fdmax,&ipix,sizeof(int));
00853 maxima->ccd.pixsize = (float)ipix;
00854 }
00855 read(fdmax,&(maxima->nb_ycoords),sizeof(int));
00856 maxima->line = (max_lines *)malloc(maxima->nb_ycoords*sizeof(max_lines));
00857 if (maxima->line == NULL) {
00858 close(fdmax);
00859 Handle_Error("load_3D_max",ERR_ALLOC);
00860 return(ERR_ALLOC);
00861 }
00862 for (i=0; i<maxima->nb_ycoords; i++) {
00863 read(fdmax,&(maxima->line[i].ycoord),sizeof(float));
00864 read(fdmax,&(maxima->line[i].nb_max),sizeof(int));
00865 if (maxima->line[i].nb_max <= 0) continue;
00866 maxima->line[i].maxima =
00867 (max_param *)malloc(maxima->line[i].nb_max*sizeof(max_param));
00868 if (maxima->line[i].maxima == NULL) {
00869 close(fdmax);
00870 Handle_Error("load_3D_max",ERR_ALLOC);
00871 return(ERR_ALLOC);
00872 }
00873 for (j=0; j<maxima->line[i].nb_max; j++) {
00874 read(fdmax,&(maxima->line[i].maxima[j].xcoord),sizeof(float));
00875 read(fdmax,&(maxima->line[i].maxima[j].intens),sizeof(float));
00876 read(fdmax,&(maxima->line[i].maxima[j].sigma[0]),sizeof(float));
00877 read(fdmax,&(maxima->line[i].maxima[j].sigma[1]),sizeof(float));
00878 read(fdmax,&(maxima->line[i].maxima[j].alpha),sizeof(float));
00879 }
00880 }
00881 close(fdmax);
00882 return(OK);
00883 }
00884
00885
00892
00893 int
00894 save_3D_max(Maxima_Set *maxima, char *name)
00895 {
00896 int fdmax, i, j;
00897 char errtext[132], filename[lg_name+1];
00898
00899 strcpy(filename,name);
00900 if (strchr(filename,'.') == NULL)
00901 strcat(filename,".max");
00902
00903 if (ASK && exist(filename))
00904 confirme_erase(filename);
00905
00906 if ((fdmax = creat(filename,CREAT_FILE_PERM))<0) {
00907 sprintf(errtext,"save_3D_max: frame %s",filename);
00908 Handle_Error(errtext,ERR_CREAT);
00909 return(ERR_CREAT);
00910 }
00911 write(fdmax,maxima->config_name,21*sizeof(char));
00912 write(fdmax,&(maxima->enlarger.gamma),sizeof(double));
00913 write(fdmax,&(maxima->wedges.theta_w),sizeof(double));
00914 write(fdmax,&(maxima->grism.index_coef),sizeof(int));
00915 write(fdmax,&(maxima->grism.A),sizeof(double));
00916 write(fdmax,&(maxima->grism.g_per_mm),sizeof(int));
00917 write(fdmax,&(maxima->filter.inf_util),sizeof(double));
00918 write(fdmax,&(maxima->filter.sup_util),sizeof(double));
00919 write(fdmax,&(maxima->telescope.fratio),sizeof(float));
00920 write(fdmax,&(maxima->ccd.nx),sizeof(int));
00921 write(fdmax,&(maxima->ccd.ny),sizeof(int));
00922 write(fdmax,&(maxima->ccd.pixsize),sizeof(float));
00923 write(fdmax,&(maxima->nb_ycoords),sizeof(int));
00924 for (i=0; i<maxima->nb_ycoords; i++) {
00925 write(fdmax,&(maxima->line[i].ycoord),sizeof(float));
00926 write(fdmax,&(maxima->line[i].nb_max),sizeof(int));
00927 for (j=0; j<maxima->line[i].nb_max; j++) {
00928 write(fdmax,&(maxima->line[i].maxima[j].xcoord),sizeof(float));
00929 write(fdmax,&(maxima->line[i].maxima[j].intens),sizeof(float));
00930 write(fdmax,&(maxima->line[i].maxima[j].sigma[0]),sizeof(float));
00931 write(fdmax,&(maxima->line[i].maxima[j].sigma[1]),sizeof(float));
00932 write(fdmax,&(maxima->line[i].maxima[j].alpha),sizeof(float));
00933 }
00934 }
00935 close(fdmax);
00936 if(TK)
00937 printf("@ N {%s}\n",filename);
00938
00939 return(OK);
00940 }
00941
00942
00949
00955
00956
00957 int interpolate_noise(SPECTRUM *noise) {
00958
00959 int i, i0, status;
00960 float x1, y1, x2, y2, a, b;
00961
00962 i = 0;
00963 x1 = 0;
00964 status = 0;
00965
00966 while (i < noise->npts) {
00967 for (; (RD_spec(noise,i) != -1) && (i < noise->npts);i++);
00968 if (i >= noise->npts)
00969 return(status);
00970 if (i == 0) {
00971 for (i++;RD_spec(noise,i) == -1;i++);
00972 y1 = RD_spec(noise,i);
00973 for (i0=0;i0<i;i0++)
00974 WR_spec(noise,i0,y1);
00975 }
00976 else {
00977 y1 = (float)RD_spec(noise,i-1);
00978 x1 = (float)i-1;
00979
00980 for (i++;(RD_spec(noise,i) == -1) && (i < noise->npts);i++);
00981 if (i < noise->npts) {
00982 y2 = (float)RD_spec(noise,i);
00983 x2 = (float)i;
00984 a = (y2 - y1)/(x2 -x1);
00985 b = y1 - a*x1;
00986 for (i0=(int)x1+1;i0<i;i0++) {
00987 WR_spec(noise,i0,a*i0+b);
00988 }
00989 }
00990 else {
00991
00992 for (i--;RD_spec(noise,i) == -1;i--);
00993 y2 = RD_spec(noise,i);
00994 for (i++;i<noise->npts;i++)
00995 WR_spec(noise,i,y2);
00996 }
00997 }
00998 }
00999 return(status);
01000 }