Main Page   Modules   Data Structures   File List   Data Fields   Globals  

C_libs/io/3D_io/3D_iolib.c

Go to the documentation of this file.
00001 /* === Doxygen File Comment ============================== */
00012 /* ======================================================= */
00013 
00014 #include <IFU_io.h>
00015 #include <data_io.h>
00016 #include <values.h>
00017 
00018 #include "../cfitsio/fitsio.h"
00019 #include "../cfitsio/longnam.h"
00020 
00021 extern void confirme_erase(char *name);
00022 extern int ASK;
00023 extern IO_Format InputIO, OutputIO;
00024 
00025 #define CREAT_FILE_PERM 0666
00026 #define OPEN_FILE_PERM 0644
00027 #define DATA_UNDEF -MAXSHORT
00028 #define LENS_UNDEF -MAXINT
00029 
00030 #define find_lens(frame,type_of_spec,specId,i) \
00031         if (frame->type_of_spec == NULL) *(i) = -1; \
00032         else { \
00033         for (*(i)=0; *(i)<frame->nbspec \
00034                 && frame->type_of_spec[*(i)].specId != specId \
00035                 && frame->type_of_spec[*(i)].specId != LENS_UNDEF; (*(i))++); \
00036         if ((frame->type_of_spec[*(i)].specId == LENS_UNDEF) \
00037                 || (*(i) == frame->nbspec)) \
00038                 *(i) = -1; \
00039         }
00040 
00041 /* --- Doxygen Function Comment -------------------------- */
00045                 /* @{ ***** Starting group5 ***** */
00071 /* ------------------------------------------------------- */
00072 
00073 #define HEADER_LENGTH   (lg_version+lg_ident+lg_unit+lg_name)*sizeof(char) \
00074                                 +2*sizeof(int)+2*sizeof(short)+sizeof(double)
00075 
00076 /* --- Doxygen Function Comment -------------------------- */
00083                 /* @{ ***** Starting group6 ***** */
00096 /* ------------------------------------------------------- */
00097 
00098 int 
00099 create_E3D_file(E3D_file *frame,char *name,int npix,double start,double step,short datatype,char *ident,char *unit)
00100 {
00101         char errtext[132], filename[lg_name+1], tablename[lg_name+1];
00102         int status=0;
00103         TABLE table;
00104         fitsfile *fptr;
00105 
00106         strcpy(frame->version,"v1.0");
00107 
00108         strcpy(filename,name);
00109         first_blk(filename);
00110         append_datacube_extension(filename,OutputIO.datacubes);
00111         strcpy(frame->name,filename);
00112 
00113         frame->imno = -1;
00114         frame->ident[0] = '\0';
00115         frame->history[0] = '\0';
00116         strcpy(frame->cunit,"None given");
00117         frame->step = step;
00118         frame->iomode = (int)O_MODE;
00119         frame->data_type = datatype;
00120         frame->file_type = -1;
00121         frame->data_format = OutputIO.datacubes;
00122         frame->nbspec = 0;
00123         frame->swapbytes = 0;
00124         if (npix < 0) 
00125                 frame->common_bounds = 0;
00126         else {
00127                 frame->common_bounds = 1;
00128                 frame->common_parameters[0] = start;
00129                 frame->common_parameters[1] = start + (double)(npix-1)*step;
00130                 frame->common_parameters[2] = (double)npix;
00131         }
00132         frame->extra_hd_off = 0;
00133         frame->external_info = NULL;
00134         frame->signal = NULL;
00135         frame->noise = NULL;
00136         frame->ngroups = 0;
00137         frame->groups = NULL;
00138         
00139         if (ident != NULL) strcpy(frame->ident,ident);
00140         if (unit != NULL) {
00141                 memcpy(frame->cunit,unit,lg_unit);
00142                 frame->cunit[lg_unit] = '\0';
00143         }
00144         
00145         if(ASK)
00146                 confirme_erase(filename);
00147 
00148         switch (OutputIO.datacubes) {
00149 
00150         case TIGER_FORMAT :
00151                 frame->data_offset = HEADER_LENGTH;
00152                 frame->crop = 1;
00153 
00154                 if ((frame->imno = creat(filename,CREAT_FILE_PERM))<0)  
00155                         status = ERR_CREAT;
00156                 else {          
00157                                 /* writing header */
00158                         write(frame->imno,frame->version,lg_version);
00159                         write(frame->imno,frame->ident,lg_ident);
00160                         write(frame->imno,frame->cunit,lg_unit);
00161                         write(frame->imno,frame->table_name,lg_name);
00162                         write(frame->imno,&(frame->data_type),sizeof(short));
00163                         write(frame->imno,&(frame->step),sizeof(double));
00164                         write(frame->imno,&(frame->nbspec),sizeof(int));
00165                         write(frame->imno,&(frame->common_bounds),sizeof(short));
00166                         write(frame->imno,&(frame->extra_hd_off),sizeof(int));
00167                         /* close and reopen to get read access */
00168                         close(frame->imno);
00169                         frame->imno = open(filename,O_RDWR,OPEN_FILE_PERM);
00170                 }
00171                 remove_file_extension(filename);
00172                 append_tbl_extension(filename,OutputIO.basic_io);
00173                 strcpy(frame->table_name,filename);
00174                 break;
00175 
00176         case EURO3D_FORMAT :
00177                 frame->data_offset = 0;
00178                 frame->crop = 0;
00179                 sprintf(frame->table_name,"%s[%s]",filename,E3D_DATA);
00180                 status =0;
00181 
00182                 if (exist(filename))
00183                     delete_E3D_file(frame);
00184 
00185                 if (fits_create_file(&fptr,filename,&status)) {
00186                     status = ERR_WRIT; break;
00187                 }
00188                 frame->external_info = (void *)fptr;
00189                 frame->imno = 0;
00190 
00191                 fits_write_imghdr(fptr, 8, 0, NULL, &status);
00192                 fits_write_key_str(fptr, "EURO3D", "T", "All mandatory EuroE3D extensions present", &status);
00193                 fits_write_key_str(fptr, E3D_ADC, "F", "data corrected from atm dispersion" , &status);
00194                 fits_write_key_str(fptr, E3D_VERS, "1.0", "version number of the EuroE3D format" , &status);
00195                 sprintf(tablename,"%s[%s]",filename,E3D_DATA);
00196                 status = create_table(&table,tablename,-1,9,'Q',NULL);
00197 
00198                 create_col(&table,E3D_COL_ID,LONG,'R',"I8","none");
00199                 create_col(&table,E3D_COL_FLAG,LONG,'R',"I1","none");
00200                 create_col(&table,E3D_COL_NSPAX,LONG,'R',"I8","none");
00201                 create_col(&table,E3D_COL_NPIX,LONG,'R',"I8","none");
00202                 create_col(&table,E3D_COL_IDX,LONG,'R',"I8","none");
00203                 create_col(&table,E3D_COL_XPOS,FLOAT,'R',"F8","none""E");
00204                 create_col(&table,E3D_COL_YPOS,FLOAT,'R',"F8","none""E");
00205                 create_col(&table,E3D_COL_GRP,LONG,'R',"I4","none""J");
00206                 create_col(&table,E3D_COL_SPAXID,CHAR,'R',"1PA","none");
00207 
00208                 close_table(&table);
00209                 if (status)
00210                         status = ERR_WRIT;
00211                 break;
00212 
00213         default :
00214                 status = ERR_FORMAT;
00215                 break;
00216         }
00217         
00218         if (status) {
00219                 sprintf(errtext,"create_E3D_file: frame %s",filename);
00220                 Handle_Error(errtext,status);
00221         } else {
00222                 WR_desc(frame,"COMMENT",CHAR,8,"       ");
00223         }
00224         return(status);
00225 } 
00226 
00227 /* --- Doxygen Function Comment -------------------------- */
00235 /* ------------------------------------------------------- */
00236 int 
00237 open_E3D_file(E3D_file *frame, char *name, char *mode)          
00238 {
00239         char errtext[132], filename[lg_name+1];
00240         char descr_name[lg_label+1];
00241         char tbl_ext[lg_name+1];
00242         short codesize, data_type;
00243         int status=0, i, j, colno, colnpix, colidx;
00244         int colgid, colsh, cols1, cols2, colang, colpang, colpwav, colairm, colpres, coltemp, colhum;
00245         int iomode, nbdesc, nb_values, user_desc, idx;
00246         E3Dspec_desc *current_spec;
00247         TABLE tblpos, tblgrp;
00248         long refpix;
00249         int int_datatype;
00250         Descriptor *descr;
00251         fitsfile *fptr;
00252         double min, max, start, step;
00253 
00254         strcpy(filename,name);
00255         first_blk(filename); 
00256         strcpy(frame->name,filename);
00257         memset(frame->ident,' ',lg_ident);
00258         frame->ident[lg_ident] = '\0';
00259         memset(frame->cunit,' ',lg_unit);
00260         frame->cunit[lg_unit] = '\0';
00261         memset(frame->history,' ',lg_hist);
00262         frame->history[lg_hist] = '\0';
00263         frame->file_type = -1;
00264         frame->imno = -1;
00265         frame->nbspec = 0;
00266         frame->common_bounds = 0;
00267         frame->crop = 0;
00268         frame->external_info = NULL;
00269         frame->signal = NULL;
00270         frame->noise = NULL;
00271         frame->swapbytes = 0;
00272         frame->ngroups = 0;
00273         frame->groups = NULL;
00274 
00275         switch(mode[0]) {
00276                 case 'I' : 
00277                         if (mode[1] == 'O') {
00278                                 frame->iomode = (int)IO_MODE;
00279                                 iomode = O_RDWR;
00280                         }
00281                         else {
00282                                 frame->iomode = (int)I_MODE;
00283                                 iomode = O_RDONLY;
00284                         }
00285                         break;
00286                 case 'O' : frame->iomode = (int)O_MODE;
00287                                 iomode = O_WRONLY;
00288                         break;
00289                 default  : frame->iomode = (int)I_MODE;
00290                                 iomode = O_RDONLY;
00291                         break;
00292         }
00293         
00294         append_datacube_extension(frame->name,InputIO.datacubes);
00295 
00296         strcpy(filename,frame->name);
00297 
00298         switch(InputIO.datacubes) {
00299 
00300         case TIGER_FORMAT :
00301                 frame->data_format = TIGER_FORMAT;
00302                 frame->crop = 1;
00303                 frame->imno = open(filename,iomode,OPEN_FILE_PERM);
00304                 if (frame->imno <0) {
00305                         status = ERR_OPEN; 
00306                         sprintf(errtext,"open_E3D_file: frame %s",frame->name);
00307                         Handle_Error(errtext,status);
00308                         return(status);
00309                 }
00310                 read(frame->imno,frame->version,lg_version);
00311         
00312                 if (strncmp(frame->version,"v1.0",4) != 0) {
00313                         close(frame->imno);
00314                         status = ERR_BAD_TYPE; 
00315                         sprintf(errtext,"open_E3D_file: frame %s",frame->name);
00316                         Handle_Error(errtext,status);
00317                         return(status);
00318                 };
00319                 read(frame->imno,frame->ident,lg_ident);
00320                 read(frame->imno,frame->cunit,lg_unit);
00321                 strcpy(frame->table_name,get_path(filename));
00322                 read(frame->imno,filename,lg_name);
00323                 remove_path(filename);
00324                 strcat(frame->table_name,"/");
00325                 strcat(frame->table_name,filename);
00326                 read(frame->imno,&(frame->data_type),sizeof(short));
00327                 frame->swapbytes = set_bigendian(frame->data_type);
00328                 if (frame->swapbytes) {
00329                         lseek(frame->imno,-sizeof(short),SEEK_CUR);
00330                         read_DOS(frame->imno,&(frame->data_type),sizeof(short));
00331                 }
00332                 status = sizeof_item(frame->data_type);
00333                 if (status < 0) {
00334                         close(frame->imno);
00335                         status = ERR_BAD_TYPE; 
00336                         sprintf(errtext,"open_E3D_file: frame %s",frame->name);
00337                         Handle_Error(errtext,status);
00338                         return(status);
00339                 }
00340                 else
00341                         status = 0;
00342                 if (frame->swapbytes) {
00343                         read_DOS(frame->imno,&(frame->step),sizeof(double));
00344                         read_DOS(frame->imno,&(frame->nbspec),sizeof(int));
00345                 } else {
00346                         read(frame->imno,&(frame->step),sizeof(double));
00347                         read(frame->imno,&(frame->nbspec),sizeof(int));
00348                 }
00349                 if (frame->nbspec < 0) {
00350                         close(frame->imno);
00351                         status = ERR_BAD_TYPE; 
00352                         sprintf(errtext,"open_E3D_file: frame %s",frame->name);
00353                         Handle_Error(errtext,status);
00354                         return(status);
00355                 }
00356                 if (frame->swapbytes) {
00357                         read_DOS(frame->imno,&(frame->common_bounds),sizeof(short));
00358                         read_DOS(frame->imno,&(frame->extra_hd_off),sizeof(int));
00359                 } else {
00360                         read(frame->imno,&(frame->common_bounds),sizeof(short));
00361                         read(frame->imno,&(frame->extra_hd_off),sizeof(int));
00362                 }
00363                 frame->data_offset = HEADER_LENGTH;
00364         
00365                 lseek(frame->imno,frame->extra_hd_off,SEEK_SET);
00366         
00367                 if (frame->nbspec != 0)
00368                         frame->signal = (E3Dspec_desc *)malloc(frame->nbspec*sizeof(E3Dspec_desc));
00369                 current_spec = frame->signal;
00370                 if (frame->swapbytes) {
00371                     for (i=0; i < frame->nbspec ;i++, current_spec++) {
00372                         read_DOS(frame->imno,&(current_spec->specId),sizeof(int));
00373                         if (current_spec->specId == LENS_UNDEF) break;
00374                         read_DOS(frame->imno,&(current_spec->start),sizeof(double));
00375                         read_DOS(frame->imno,&(current_spec->end),sizeof(double));
00376                         read_DOS(frame->imno,&(current_spec->npix),sizeof(int));
00377                         read_DOS(frame->imno,&min,sizeof(double));
00378                         read_DOS(frame->imno,&max,sizeof(double));
00379                         read_DOS(frame->imno,&(current_spec->data_offset),sizeof(int));
00380                         frame->data_offset += current_spec->npix*sizeof_item(frame->data_type); 
00381                     }
00382                 } else {
00383                     for (i=0; i < frame->nbspec ;i++, current_spec++) {
00384                         read(frame->imno,&(current_spec->specId),sizeof(int));
00385                         if (current_spec->specId == LENS_UNDEF) break;
00386                         read(frame->imno,&(current_spec->start),sizeof(double));
00387                         read(frame->imno,&(current_spec->end),sizeof(double));
00388                         read(frame->imno,&(current_spec->npix),sizeof(int));
00389                         read(frame->imno,&min,sizeof(double));
00390                         read(frame->imno,&max,sizeof(double));
00391                         read(frame->imno,&(current_spec->data_offset),sizeof(int));
00392                         frame->data_offset += current_spec->npix*sizeof_item(frame->data_type); 
00393                     }
00394                 }
00395                 if (i == 0 && frame->signal != NULL) {
00396                         free((char *)frame->signal);
00397                         frame->noise = NULL;
00398                 }
00399                 if (frame->nbspec != 0 && frame->noise == NULL)
00400                         frame->noise = (E3Dspec_desc *)malloc(frame->nbspec*sizeof(E3Dspec_desc));
00401                 current_spec = frame->noise;
00402                 if (frame->swapbytes) {
00403                     for (i=0; i < frame->nbspec ;i++, current_spec++) {
00404                         read_DOS(frame->imno,&(current_spec->specId),sizeof(int));
00405                         if (current_spec->specId == LENS_UNDEF) break;
00406                         read_DOS(frame->imno,&(current_spec->start),sizeof(double));
00407                         read_DOS(frame->imno,&(current_spec->end),sizeof(double));
00408                         read_DOS(frame->imno,&(current_spec->npix),sizeof(int));
00409                         read_DOS(frame->imno,&min,sizeof(double));
00410                         read_DOS(frame->imno,&max,sizeof(double));
00411                         read_DOS(frame->imno,&(current_spec->data_offset),sizeof(int));
00412                         frame->data_offset += current_spec->npix*sizeof_item(frame->data_type); 
00413                     }
00414                 } else {
00415                     for (i=0; i < frame->nbspec ;i++, current_spec++) {
00416                         read(frame->imno,&(current_spec->specId),sizeof(int));
00417                         if (current_spec->specId == LENS_UNDEF) break;
00418                         read(frame->imno,&(current_spec->start),sizeof(double));
00419                         read(frame->imno,&(current_spec->end),sizeof(double));
00420                         read(frame->imno,&(current_spec->npix),sizeof(int));
00421                         read(frame->imno,&min,sizeof(double));
00422                         read(frame->imno,&max,sizeof(double));
00423                         read(frame->imno,&(current_spec->data_offset),sizeof(int));
00424                         frame->data_offset += current_spec->npix*sizeof_item(frame->data_type); 
00425                     }
00426                 }
00427                 if (i == 0 && frame->noise != NULL) {
00428                         free((char *)frame->noise);
00429                         frame->noise = NULL;
00430                 }
00431                 if (frame->swapbytes)
00432                         user_desc = read_DOS(frame->imno,&nbdesc,sizeof(int));
00433                 else
00434                         user_desc = read(frame->imno,&nbdesc,sizeof(int));
00435                 if (user_desc > 0) {
00436                                         /* read user defined descriptors */
00437         
00438               /* if nbdesc greater 1000 it's probably seems that the file is corrupted 
00439                         so we return an error */
00440         
00441               if (nbdesc>1000)
00442                 {
00443                   status = ERR_READ;
00444                   sprintf(errtext,"open_E3D_file: frame %s",frame->name);
00445                   Handle_Error(errtext,status);
00446                   return(status);
00447                 }
00448               
00449               if (frame->swapbytes) {
00450                     for (i=0; i<nbdesc; i++) {
00451                         memset(descr_name,0,lg_label+1);
00452                         read(frame->imno,descr_name,lg_label*sizeof(char));
00453                         read_DOS(frame->imno,&data_type,sizeof(short));
00454                         read_DOS(frame->imno,&nb_values,sizeof(int));
00455                         alloc_new_desc((Anyfile *)(frame),data_type,nb_values);
00456                         descr = ((Descr_Items *)(frame->external_info))->descr_list;
00457                         descr += i;
00458                         strcpy(descr->descr_name,descr_name);
00459                         codesize = (short)sizeof_item(descr->data_type);
00460                         if (data_type != CHAR) 
00461                                 for (j=0;j<nb_values;j++)
00462                                         read_DOS(frame->imno,&(descr->descr_value.c_data[j*codesize]),codesize);
00463                         else
00464                                 read(frame->imno,&(descr->descr_value.c_data[0]),descr->nb_values*codesize);
00465                         
00466                         ((Descr_Items *)(frame->external_info))->nb_descr++;
00467                     }
00468                 } else {
00469                     for (i=0; i<nbdesc; i++) {
00470                         memset(descr_name,0,lg_label+1);
00471                         read(frame->imno,descr_name,lg_label*sizeof(char));
00472                         read(frame->imno,&data_type,sizeof(short));
00473                         read(frame->imno,&nb_values,sizeof(int));
00474                         alloc_new_desc((Anyfile *)(frame),data_type,nb_values);
00475                         descr = ((Descr_Items *)(frame->external_info))->descr_list;
00476                         descr += i;
00477                         strcpy(descr->descr_name,descr_name);
00478                         codesize = (short)sizeof_item(descr->data_type);
00479                         read(frame->imno,&(descr->descr_value.c_data[0]),descr->nb_values*codesize);
00480                         ((Descr_Items *)(frame->external_info))->nb_descr++;
00481                     }
00482                 }
00483         }
00484         lseek(frame->imno,frame->data_offset,SEEK_SET);
00485         break;
00486 
00487         case EURO3D_FORMAT :
00488                 frame->data_format = EURO3D_FORMAT;
00489                 sprintf(frame->table_name,"%s[%s]",filename,E3D_TIGER_EXT);
00490                 status = 0;
00491                 iomode = get_iomode_code(InputIO.basic_io,frame->iomode);
00492                 if (fits_open_file(&fptr,filename,iomode,&status)) {
00493                         status = ERR_OPEN; break;
00494                 }
00495                 frame->external_info = (void *)fptr;
00496                 frame->imno = 0;
00497 
00498                 fits_read_key_str(fptr, E3D_VERS,frame->version, NULL, &status);
00499         
00500                 if (strncmp(frame->version,"1.0",3) != 0) {
00501                         close(frame->imno);
00502                         status = ERR_BAD_TYPE; 
00503                         break;
00504                 };
00505                 sprintf(frame->table_name,"%s[%s]",filename,E3D_DATA);
00506                 fits_read_key_str(fptr, "OBJECT",frame->ident, NULL, &status);
00507 
00508                 sprintf(tbl_ext,"%s[%s]",filename,E3D_DATA);
00509                 status = open_table(&tblpos,tbl_ext,mode);
00510                 if (status < 0) {
00511                         close_table(&tblpos);
00512                         status = ERR_TBL_EXT; 
00513                         break;
00514                 }
00515 
00516                 RD_desc(&tblpos,E3D_KW_UNITS,CHAR,lg_label,frame->cunit);
00517                 RD_desc(&tblpos,E3D_KW_START,DOUBLE,1,&start);
00518                 RD_desc(&tblpos,E3D_KW_STEP,DOUBLE,1,&step);
00519                 RD_desc(&tblpos,E3D_KW_REFPIX,LONG,1,&refpix);
00520                 if (refpix != 1) {
00521                         start += (refpix -1)*step;
00522                 }
00523                 colnpix = get_col_ref(&tblpos,E3D_COL_NPIX);
00524                 colidx = get_col_ref(&tblpos,E3D_COL_IDX);
00525 
00526                 colno = get_col_ref(&tblpos,E3D_COL_INT);
00527                 if (colno < 0) {
00528                         close_table(&tblpos);
00529                         status = ERR_BAD_COL; 
00530                         break;
00531                 }
00532                 get_col_info(&tblpos, colno, &int_datatype, NULL, NULL);
00533                 frame->data_type = (short)int_datatype;
00534 
00535                 colno = get_col_ref(&tblpos,E3D_COL_ID);
00536                 if (colno < 0) {
00537                         close_table(&tblpos);
00538                         status = ERR_BAD_COL; 
00539                         break;
00540                 }
00541                 frame->step = step;
00542                 frame->nbspec = tblpos.row;
00543 
00544                 if (frame->nbspec != 0)
00545                         frame->signal = (E3Dspec_desc *)malloc(frame->nbspec*sizeof(E3Dspec_desc));
00546 
00547                 current_spec = frame->signal;
00548 
00549                 for (j=0; j < frame->nbspec ;j++, current_spec++) {
00550                         RD_tbl(&tblpos,j,colno,&(current_spec->specId));
00551                         RD_tbl(&tblpos,j,colnpix,&(current_spec->npix));
00552                         RD_tbl(&tblpos,j,colidx,&idx);
00553                         current_spec->start = start + idx*step;;
00554                         current_spec->end = start+(idx+current_spec->npix)*step;
00555                         min = MAXFLOAT;
00556                         max = -MAXFLOAT;
00557                         current_spec->data_offset = 0x0;
00558                 }
00559 
00560                 if (j == 0 && frame->signal != NULL) {
00561                         free((char *)frame->signal);
00562                         frame->signal = 0x0;
00563                 }
00564                 if (frame->nbspec != 0)
00565                         frame->noise = (E3Dspec_desc *)malloc(frame->nbspec*sizeof(E3Dspec_desc));
00566                 current_spec = frame->noise;
00567 
00568                 j = 0;
00569                 if (status == 0) {
00570                     for (j=0; j < frame->nbspec ;j++, current_spec++) {
00571                         RD_tbl(&tblpos,j,colno,&(current_spec->specId));
00572                         RD_tbl(&tblpos,j,colnpix,&(current_spec->npix));
00573                         RD_tbl(&tblpos,j,colidx,&idx);
00574                         current_spec->start = start + idx*step;;
00575                         current_spec->end = start+(idx+current_spec->npix)*step;
00576                         min = MAXFLOAT;
00577                         max = -MAXFLOAT;
00578                         current_spec->data_offset = 0x0;
00579                     }
00580                 }
00581                 else
00582                    status = 0;
00583                 if (j == 0 && frame->noise != NULL) {
00584                         free((char *)frame->noise);
00585                         frame->noise = 0x0;
00586                 }
00587                 close_table(&tblpos);
00588 
00589                 sprintf(tbl_ext,"%s[%s]",filename,E3D_GRP);
00590                 status = open_table(&tblgrp,tbl_ext,mode);
00591                 if (status < 0) {
00592                         close_table(&tblgrp);
00593                         status = ERR_TBL_EXT; 
00594                         break;
00595                 }
00596                 frame->ngroups = tblgrp.row;
00597                 if (frame->ngroups > 0) 
00598                         frame->groups = malloc(frame->ngroups*sizeof(GROUP));
00599                 colgid = get_col_ref(&tblgrp,E3D_COL_GRP);
00600                 if (colgid < 0) {
00601                         close_table(&tblgrp);
00602                         status = ERR_BAD_COL; 
00603                         break;
00604                 }
00605                 colsh = get_col_ref(&tblgrp,E3D_COL_SHAPE);
00606                 if (colsh < 0) {
00607                         close_table(&tblgrp);
00608                         status = ERR_BAD_COL; 
00609                         break;
00610                 }
00611                 cols1 = get_col_ref(&tblgrp,E3D_COL_SIZE1);
00612                 if (cols1 < 0) {
00613                         close_table(&tblgrp);
00614                         status = ERR_BAD_COL; 
00615                         break;
00616                 }
00617                 colang = get_col_ref(&tblgrp,E3D_COL_ANGLE);
00618                 if (colang < 0) {
00619                         close_table(&tblgrp);
00620                         status = ERR_BAD_COL; 
00621                         break;
00622                 }
00623                 cols2 = get_col_ref(&tblgrp,E3D_COL_SIZE2);
00624                 if (cols2 < 0) {
00625                         close_table(&tblgrp);
00626                         status = ERR_BAD_COL; 
00627                         break;
00628                 }
00629                 colpwav = get_col_ref(&tblgrp,E3D_COL_PWAVE);
00630                 if (colpwav < 0) {
00631                         close_table(&tblgrp);
00632                         status = ERR_BAD_COL; 
00633                         break;
00634                 }
00635                 colairm = get_col_ref(&tblgrp,E3D_COL_AIRM);
00636                 if (colairm < 0) {
00637                         close_table(&tblgrp);
00638                         status = ERR_BAD_COL; 
00639                         break;
00640                 }
00641                 colpang = get_col_ref(&tblgrp,E3D_COL_PANG);
00642                 if (colpang < 0) {
00643                         close_table(&tblgrp);
00644                         status = ERR_BAD_COL; 
00645                         break;
00646                 }
00647                 colpres = get_col_ref(&tblgrp,E3D_COL_PRES);
00648                 if (colpres < 0) {
00649                         close_table(&tblgrp);
00650                         status = ERR_BAD_COL; 
00651                         break;
00652                 }
00653                 coltemp = get_col_ref(&tblgrp,E3D_COL_TEMP);
00654                 if (coltemp < 0) {
00655                         close_table(&tblgrp);
00656                         status = ERR_BAD_COL; 
00657                         break;
00658                 }
00659                 colhum = get_col_ref(&tblgrp,E3D_COL_HUM);
00660                 if (colhum < 0) {
00661                         close_table(&tblgrp);
00662                         status = ERR_BAD_COL; 
00663                         break;
00664                 }
00665                 for (j=0; j< frame->ngroups; j++) {
00666                         RD_tbl(&tblgrp,j,colgid,&(frame->groups[j].groupId));
00667                         RD_tbl(&tblgrp,j,colsh,&(frame->groups[j].shape));
00668                         RD_tbl(&tblgrp,j,cols1,&(frame->groups[j].size1));
00669                         RD_tbl(&tblgrp,j,colang,&(frame->groups[j].angle));
00670                         RD_tbl(&tblgrp,j,cols2,&(frame->groups[j].size2));
00671                         RD_tbl(&tblgrp,j,colpwav,&(frame->groups[j].poswav));
00672                         RD_tbl(&tblgrp,j,colairm,&(frame->groups[j].airmass));
00673                         RD_tbl(&tblgrp,j,colpang,&(frame->groups[j].parang));
00674                         RD_tbl(&tblgrp,j,colpres,&(frame->groups[j].pressure));
00675                         RD_tbl(&tblgrp,j,coltemp,&(frame->groups[j].temperature));
00676                         RD_tbl(&tblgrp,j,colhum,&(frame->groups[j].rel_humidity));
00677                 }
00678                 close_table(&tblgrp);
00679                 fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
00680 
00681                 /* checks for common bounds for all spectra */
00682                 set_common_bounds(frame);
00683                 break;
00684 
00685         default :
00686                 status = ERR_FORMAT;
00687                 break;
00688     }
00689     if (frame->common_bounds) {
00690         memset(frame->common_parameters,0,3*sizeof(double));
00691         if (frame->signal != NULL)
00692                 current_spec = frame->signal;
00693         frame->common_parameters[0] = current_spec->start;
00694         frame->common_parameters[1] = current_spec->end;
00695         frame->common_parameters[2] = (int)current_spec->npix;
00696     }
00697     
00698     if (!status) {
00699         disable_user_warnings();
00700         RD_desc(frame,"HISTORY",CHAR,lg_hist,frame->history);
00701         restore_user_warnings();
00702     } else {
00703         sprintf(errtext,"open_E3D_file: frame %s",frame->name);
00704         Handle_Error(errtext,status);
00705     }
00706     return(status);
00707 }
00708 /* --- Doxygen Function Comment -------------------------- */
00714 /* ------------------------------------------------------- */
00715 int 
00716 close_E3D_file(E3D_file *frame)                 /* close active frame */
00717 {
00718         E3Dspec_desc *current_spec;
00719         Descr_Items *dsc_items;
00720         Descriptor *descr;
00721         char   errtext[132];
00722         char   tbl_ext[lg_name+1];
00723         double min=0, max=0;
00724         int    colgid, colsh, cols1, cols2, colang, colpang, colpwav, colairm, colpres, coltemp, colhum;
00725         short  codesize;
00726         int    stat, i, j, none=LENS_UNDEF;
00727         fitsfile *fptr;
00728         TABLE  tblgrp;
00729 
00730         if (frame->iomode != (int)I_MODE) {
00731 
00732                 switch (frame->data_format) {
00733 
00734                 case TIGER_FORMAT :
00735 
00736                     /* checks for common bounds for all spectra */
00737                     set_common_bounds(frame);
00738 
00739                     frame->extra_hd_off = frame->data_offset;
00740 
00741                     lseek(frame->imno,HEADER_LENGTH -2*sizeof(short) -2*sizeof(int)
00742                         -sizeof(double) - lg_name,
00743                         SEEK_SET);
00744 
00745                     remove_path(frame->table_name);
00746                     write(frame->imno,frame->table_name,lg_name);
00747 
00748                     if (frame->swapbytes) {
00749                         write_DOS(frame->imno,&(frame->data_type),sizeof(short));
00750                         write_DOS(frame->imno,&(frame->step),sizeof(double));
00751                         write_DOS(frame->imno,&(frame->nbspec),sizeof(int));
00752                         write_DOS(frame->imno,&(frame->common_bounds),sizeof(short));
00753                         write_DOS(frame->imno,&(frame->extra_hd_off),sizeof(int));
00754                     } else {
00755                         write(frame->imno,&(frame->data_type),sizeof(short));
00756                         write(frame->imno,&(frame->step),sizeof(double));
00757                         write(frame->imno,&(frame->nbspec),sizeof(int));
00758                         write(frame->imno,&(frame->common_bounds),sizeof(short));
00759                         write(frame->imno,&(frame->extra_hd_off),sizeof(int));
00760                     }
00761                     lseek(frame->imno,frame->extra_hd_off,SEEK_SET);
00762                     if (frame->swapbytes) {
00763                         if (frame->signal != NULL) {
00764                             current_spec = frame->signal;
00765                         for (i=0; i < frame->nbspec ;i++, current_spec++) {
00766                                 write_DOS(frame->imno,&(current_spec->specId),sizeof(int));
00767                                 write_DOS(frame->imno,&(current_spec->start),sizeof(double));
00768                                 write_DOS(frame->imno,&(current_spec->end),sizeof(double));
00769                                 write_DOS(frame->imno,&(current_spec->npix),sizeof(int));
00770                                 write_DOS(frame->imno,&min,sizeof(double));
00771                                 write_DOS(frame->imno,&max,sizeof(double));
00772                                 write_DOS(frame->imno,&(current_spec->data_offset),sizeof(int));
00773                         }
00774                     }
00775                     else
00776                         write_DOS(frame->imno,&none,sizeof(int));
00777                     } else {
00778                         if (frame->signal != NULL) {
00779                             current_spec = frame->signal;
00780                         for (i=0; i < frame->nbspec ;i++, current_spec++) {
00781                                 write(frame->imno,&(current_spec->specId),sizeof(int));
00782                                 write(frame->imno,&(current_spec->start),sizeof(double));
00783                                 write(frame->imno,&(current_spec->end),sizeof(double));
00784                                 write(frame->imno,&(current_spec->npix),sizeof(int));
00785                                 write(frame->imno,&min,sizeof(double));
00786                                 write(frame->imno,&max,sizeof(double));
00787                                 write(frame->imno,&(current_spec->data_offset),sizeof(int));
00788                         }
00789                     }
00790                     else
00791                         write(frame->imno,&none,sizeof(int));
00792                     }
00793 
00794                     if (frame->swapbytes) {
00795                         if (frame->noise != NULL) {
00796                             current_spec = frame->noise;
00797                         for (i=0; i < frame->nbspec ;i++, current_spec++) {
00798                                 write_DOS(frame->imno,&(current_spec->specId),sizeof(int));
00799                                 write_DOS(frame->imno,&(current_spec->start),sizeof(double));
00800                                 write_DOS(frame->imno,&(current_spec->end),sizeof(double));
00801                                 write_DOS(frame->imno,&(current_spec->npix),sizeof(int));
00802                                 write_DOS(frame->imno,&min,sizeof(double));
00803                                 write_DOS(frame->imno,&max,sizeof(double));
00804                                 write_DOS(frame->imno,&(current_spec->data_offset),sizeof(int));
00805                         }
00806                        }
00807                        else
00808                             write_DOS(frame->imno,&none,sizeof(int));
00809                     } else {
00810                         if (frame->noise != NULL) {
00811                             current_spec = frame->noise;
00812                         for (i=0; i < frame->nbspec ;i++, current_spec++) {
00813                                 write(frame->imno,&(current_spec->specId),sizeof(int));
00814                                 write(frame->imno,&(current_spec->start),sizeof(double));
00815                                 write(frame->imno,&(current_spec->end),sizeof(double));
00816                                 write(frame->imno,&(current_spec->npix),sizeof(int));
00817                                 write(frame->imno,&min,sizeof(double));
00818                                 write(frame->imno,&max,sizeof(double));
00819                                 write(frame->imno,&(current_spec->data_offset),sizeof(int));
00820                         }
00821                        }
00822                        else
00823                             write(frame->imno,&none,sizeof(int));
00824                     }
00825 
00826                     WR_history(frame, (Anyfile *)0);
00827 
00828                     /* save user descriptors */
00829 
00830                     if (frame->external_info != NULL) {
00831                         dsc_items = (Descr_Items *)frame->external_info;
00832                         if (frame->swapbytes) {
00833                             write_DOS(frame->imno,&(dsc_items->nb_descr),sizeof(int));
00834                             for (i=0; i< dsc_items->nb_descr; i++) {
00835                                 descr = dsc_items->descr_list+i;
00836                                 codesize = (short)sizeof_item(descr->data_type);
00837                                 write(frame->imno,descr->descr_name,lg_label*sizeof(char));
00838                                 write_DOS(frame->imno,&(descr->data_type),sizeof(short));
00839                                 write_DOS(frame->imno,&(descr->nb_values),sizeof(int));
00840                                 if (descr->data_type != CHAR) 
00841                                     for (j=0;j<descr->nb_values;j++)
00842                                         write_DOS(frame->imno,&(descr->descr_value.c_data[j*codesize]),codesize);
00843                                 else
00844                                     write(frame->imno,descr->descr_value.c_data,
00845                                         codesize*descr->nb_values);
00846                             }   
00847                         } else {
00848                             write(frame->imno,&(dsc_items->nb_descr),sizeof(int));
00849                             for (i=0; i< dsc_items->nb_descr; i++) {
00850                                 descr = dsc_items->descr_list+i;
00851                                 codesize = (short)sizeof_item(descr->data_type);
00852                                 write(frame->imno,descr->descr_name,lg_label*sizeof(char));
00853                                 write(frame->imno,&(descr->data_type),sizeof(short));
00854                                 write(frame->imno,&(descr->nb_values),sizeof(int));
00855                                 write(frame->imno,descr->descr_value.c_data,
00856                                         codesize*descr->nb_values);
00857                             }   
00858                         }
00859                     }
00860                     free_all_desc((Anyfile *)frame);
00861                     break;
00862 
00863                     case EURO3D_FORMAT :
00864 
00865                     WR_history(frame, (Anyfile *)0);
00866 
00867                                         /* saving groups */
00868 
00869                     sprintf(tbl_ext,"%s[%s]",frame->name,E3D_GRP);
00870                     append_tbl_extension(tbl_ext,OutputIO.basic_io);
00871                     if (exist(tbl_ext)) 
00872                         stat = open_table(&tblgrp,tbl_ext,"IO");
00873                     else
00874                         stat = create_table(&tblgrp,tbl_ext,frame->ngroups,10,'Q',NULL);
00875                     if (stat < 0) {
00876                         stat = ERR_TBL_EXT; 
00877                         break;
00878                     }
00879                     colgid = create_col(&tblgrp,E3D_COL_GRP,LONG,'R',"I4","none");
00880                     if (colgid < 0) {
00881                         close_table(&tblgrp);
00882                         stat = ERR_BAD_COL; 
00883                         break;
00884                     }
00885                     colsh = create_col(&tblgrp,E3D_COL_SHAPE,CHAR,'R',"A1","none");
00886                     if (colsh < 0) {
00887                         close_table(&tblgrp);
00888                         stat = ERR_BAD_COL; 
00889                         break;
00890                     }
00891                     cols1 = create_col(&tblgrp,E3D_COL_SIZE1,FLOAT,'R',"F9.4","none");
00892                     if (cols1 < 0) {
00893                         close_table(&tblgrp);
00894                         stat = ERR_BAD_COL; 
00895                         break;
00896                     }
00897                     colang = create_col(&tblgrp,E3D_COL_ANGLE,FLOAT,'R',"F9.4","none");
00898                     if (colang < 0) {
00899                         close_table(&tblgrp);
00900                         stat = ERR_BAD_COL; 
00901                         break;
00902                     }
00903                     cols2 = create_col(&tblgrp,E3D_COL_SIZE2,FLOAT,'R',"F9.4","none");
00904                     if (cols2 < 0) {
00905                         close_table(&tblgrp);
00906                         stat = ERR_BAD_COL; 
00907                         break;
00908                     }
00909                     colpwav = create_col(&tblgrp,E3D_COL_PWAVE,FLOAT,'R',"F9.4","none");
00910                     if (colpwav < 0) {
00911                         close_table(&tblgrp);
00912                         stat = ERR_BAD_COL; 
00913                         break;
00914                     }
00915                     colairm = create_col(&tblgrp,E3D_COL_AIRM,FLOAT,'R',"F9.4","none");
00916                     if (colairm < 0) {
00917                         close_table(&tblgrp);
00918                         stat = ERR_BAD_COL; 
00919                         break;
00920                     }
00921                     colpang = create_col(&tblgrp,E3D_COL_PANG,FLOAT,'R',"F9.4","none");
00922                     if (colpang < 0) {
00923                         close_table(&tblgrp);
00924                         stat = ERR_BAD_COL; 
00925                         break;
00926                     }
00927                     colpres = create_col(&tblgrp,E3D_COL_PRES,FLOAT,'R',"F9.4","none");
00928                     if (colpres < 0) {
00929                         close_table(&tblgrp);
00930                         stat = ERR_BAD_COL; 
00931                         break;
00932                     }
00933                     coltemp = create_col(&tblgrp,E3D_COL_TEMP,FLOAT,'R',"F9.4","none");
00934                     if (coltemp < 0) {
00935                         close_table(&tblgrp);
00936                         stat = ERR_BAD_COL; 
00937                         break;
00938                     }
00939                     colhum = create_col(&tblgrp,E3D_COL_HUM,FLOAT,'R',"F9.4","none");
00940                     if (colhum < 0) {
00941                         close_table(&tblgrp);
00942                         stat = ERR_BAD_COL; 
00943                         break;
00944                     }
00945                     for (j=0; j< frame->ngroups; j++) {
00946                         WR_tbl(&tblgrp,j,colgid,&(frame->groups[j].groupId));
00947                         WR_tbl(&tblgrp,j,colsh,&(frame->groups[j].shape));
00948                         WR_tbl(&tblgrp,j,cols1,&(frame->groups[j].size1));
00949                         WR_tbl(&tblgrp,j,colang,&(frame->groups[j].angle));
00950                         WR_tbl(&tblgrp,j,cols2,&(frame->groups[j].size2));
00951                         WR_tbl(&tblgrp,j,colpwav,&(frame->groups[j].poswav));
00952                         WR_tbl(&tblgrp,j,colairm,&(frame->groups[j].airmass));
00953                         WR_tbl(&tblgrp,j,colpang,&(frame->groups[j].parang));
00954                         WR_tbl(&tblgrp,j,colpres,&(frame->groups[j].pressure));
00955                         WR_tbl(&tblgrp,j,coltemp,&(frame->groups[j].temperature));
00956                         WR_tbl(&tblgrp,j,colhum,&(frame->groups[j].rel_humidity));
00957                     }
00958                     stat = close_table(&tblgrp);
00959                     break;
00960                 }
00961         }
00962         if (frame->signal != NULL) free(frame->signal);
00963         if (frame->noise != NULL) free(frame->noise);
00964 
00965         if(TK && (frame->iomode == O_MODE || frame->iomode == IO_MODE))
00966         {
00967                 printf("@ N {%s}\n",frame->name);
00968         }
00969         switch(frame->data_format) {
00970 
00971                 case TIGER_FORMAT :
00972                 stat = close(frame->imno);
00973                 break;
00974 
00975                 case EURO3D_FORMAT :
00976                 fptr = (fitsfile *)frame->external_info;
00977                 stat = 0;
00978                 fits_close_file(fptr,&stat);
00979                 break;
00980 
00981                 default :
00982                 stat = ERR_FORMAT;
00983                 break;
00984         }
00985         if (stat) {
00986                 sprintf(errtext,"close_E3D_file: frame %s",frame->name);
00987                 Handle_Error(errtext,stat);
00988         } 
00989         else 
00990                 frame->imno = -1;
00991 
00992         return(stat);
00993 }
00994 
00995 /* --- Doxygen Function Comment -------------------------- */
01001 /* ------------------------------------------------------- */
01002 
01003 int delete_E3D_file(E3D_file *frame)            
01004 {
01005         int status;
01006         char errtext[132];
01007         char filename[lg_name+1];
01008         
01009         append_datacube_extension(frame->name,InputIO.datacubes);
01010 
01011         strcpy(filename,frame->name);
01012 
01013         if (frame->imno >= 0)
01014                 close_E3D_file(frame);
01015 
01016         status = remove(filename);
01017         if (status)     {
01018                 sprintf(errtext,"delete_E3D_file: frame %s",frame->name);
01019                 Handle_Error(errtext,status);
01020         }
01021         return(status);
01022 }
01023 
01024 /* --- Doxygen Function Comment -------------------------- */
01031                 /* @{ ***** Starting group7 ***** */
01040 /* ------------------------------------------------------- */
01041 int 
01042 init_new_E3D_spec(E3D_file *frame, SPECTRUM *spectrum, int npix, double start)
01043 {
01044         char errtext[132];
01045         int status = 0;
01046         
01047         sprintf(spectrum->name,"%s->s%.4d",frame->name,-1);
01048         spectrum->imno = -1;
01049         spectrum->file_type = -1;
01050         spectrum->data_format = TIGER_FORMAT;
01051         spectrum->iomode = frame->iomode;
01052         strcpy(spectrum->ident,frame->ident);
01053         strcpy(spectrum->cunit,frame->cunit);
01054         strcpy(spectrum->history,frame->history);
01055         spectrum->step = frame->step;
01056         if (npix < 0) {
01057                 if (frame->common_bounds) {
01058                         get_common_param(frame,&(spectrum->npts), &(spectrum->start),&(spectrum->end));
01059                 }
01060                 else
01061                         status = ERR_BAD_PARAM;
01062         }
01063         else {
01064                 spectrum->npts = npix;
01065                 spectrum->start = start;
01066         }
01067         if (!status) {
01068                 spectrum->end = ((spectrum->npts-1)*spectrum->step + spectrum->start);
01069                 spectrum->wstart = spectrum->start;      
01070                 spectrum->wend = spectrum->end;
01071                 spectrum->iwstart = 0;      
01072                 spectrum->iwend = spectrum->npts - 1;
01073                 spectrum->data_type = frame->data_type;
01074                 spectrum->external_info = NULL;
01075 
01076                 status = alloc_spec_mem(spectrum, frame->data_type);
01077         }
01078         if (status) {
01079                 sprintf(errtext,"init_new_E3D_spec: spec %s (%d pixels)",spectrum->name,npix);
01080                 Handle_Error(errtext,status);
01081         }
01082         return(status);
01083 }
01084 
01085 int extract_E3D_spec(E3D_file *frame, SPECTRUM *spectrum, E3Dspec_desc **type_of_spec, int specId) {
01086 
01087         char errtext[132];
01088         E3Dspec_desc *current_spec;
01089         int status, i, colnum;
01090         fitsfile *fptr;
01091 
01092         sprintf(spectrum->name,"%s->s%.4d",frame->name,specId); 
01093         spectrum->imno = -1; 
01094         spectrum->file_type = -1; 
01095         spectrum->data_format = frame->data_format;
01096         spectrum->iomode = frame->iomode; 
01097         strcpy(spectrum->ident,frame->ident);
01098         strcpy(spectrum->cunit,frame->cunit);
01099         strcpy(spectrum->history,frame->history);
01100         spectrum->step = frame->step; 
01101         spectrum->data_type = frame->data_type;
01102         spectrum->external_info = NULL; 
01103 
01104         if (*type_of_spec == frame->signal)  {
01105                 find_lens(frame,signal,specId,&i); 
01106         }       
01107         else {
01108                 find_lens(frame,noise,specId,&i); 
01109         }
01110 
01111         if (i < 0 ) 
01112                 status = ERR_NOIMA;
01113         else { 
01114                 current_spec = *(type_of_spec); 
01115                 current_spec += i;
01116                 spectrum->start = current_spec->start; 
01117                 spectrum->end = current_spec->end; 
01118                 spectrum->npts = current_spec->npix;
01119                 spectrum->wstart = spectrum->start; 
01120                 spectrum->wend = spectrum->end; 
01121                 spectrum->iwstart = 0;
01122                 spectrum->iwend = spectrum->npts - 1; 
01123                 status = alloc_spec_mem(spectrum, frame->data_type); 
01124 
01125                 switch (spectrum->data_format) {
01126                 case TIGER_FORMAT :
01127                         lseek(frame->imno,current_spec->data_offset,SEEK_SET);
01128                         if (frame->swapbytes) {
01129                         for (i=0;i<spectrum->npts;i++)
01130                                 read_DOS(frame->imno,(char *)(spectrum->data.s_data)+
01131                                 i*sizeof_item(spectrum->data_type), 
01132                                 sizeof_item(spectrum->data_type)); 
01133                         } 
01134                         else {
01135                                 read(frame->imno,(char *)(spectrum->data.s_data), 
01136                                 spectrum->npts*sizeof_item(spectrum->data_type)); 
01137                         } 
01138                         break;
01139 
01140                 case EURO3D_FORMAT :
01141                         status = 0;
01142                         fptr = (fitsfile *)frame->external_info;
01143 
01144                         fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01145         
01146                         if (status) { 
01147                                 status = 0;
01148                                 fits_movabs_hdu(fptr, 1, NULL, &status);
01149                                 fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01150                         }
01151         
01152                         /* read signal or noise */
01153                         if (*type_of_spec == frame->signal) 
01154                                 fits_get_colnum(fptr,0,E3D_COL_INT,&colnum,&status);
01155                         else 
01156                                 fits_get_colnum(fptr,0,E3D_COL_RMS,&colnum,&status);
01157                         if (status) {
01158                                 status = ERR_BAD_COL; break;
01159                         }
01160                         switch(spectrum->data_type) {
01161                         case SHORT :
01162                                 fits_read_col_sht(fptr, colnum, i+1, 1, spectrum->npts, 0, spectrum->data.s_data, NULL, &status);
01163                                 break;
01164                         case INT :
01165                         case LONG :
01166                                 fits_read_col_lng(fptr, colnum, i+1, 1, spectrum->npts, 0, spectrum->data.l_data, NULL, &status);
01167                                 break;
01168                         case FLOAT :
01169                                 fits_read_col_flt(fptr, colnum, i+1, 1, spectrum->npts, 0, spectrum->data.f_data, NULL, &status);
01170                                 break;
01171                         case DOUBLE :
01172                                 fits_read_col_dbl(fptr, colnum, i+1, 1, spectrum->npts, 0, spectrum->data.d_data, NULL, &status);
01173                                 break;
01174                         }
01175                         if (status) {
01176                                 status = ERR_READ; break;
01177                         }
01178         
01179                         /* read data quality */
01180                         fits_get_colnum(fptr,0,E3D_COL_DQ,&colnum,&status);
01181                         if (status) {
01182                                 status = ERR_BAD_COL; break;
01183                         }
01184                         fits_read_col_lng(fptr, colnum, i+1, 1, spectrum->npts, 0, spectrum->quality, NULL, &status);
01185                         if (status) {
01186                                 status = ERR_WRIT; break;
01187                         }
01188         
01189                         break;
01190 
01191                         default :
01192                         status = ERR_FORMAT;
01193                         break;
01194                 }
01195         } 
01196         if (status) {
01197                 sprintf(errtext,"get_E3D_spec: spec %s",spectrum->name);
01198                 Handle_Error(errtext,status); 
01199         } 
01200         return(status);
01201 }
01202 
01203 /* --- Doxygen Function Comment -------------------------- */
01212 /* ------------------------------------------------------- */
01213 
01214 int 
01215 get_E3D_spec(E3D_file *frame, SPECTRUM *signal, SPECTRUM *noise, int specId)
01216 {
01217 
01218         int status;
01219 
01220         if ((signal != NULL) && (frame->signal != NULL)) {
01221                 status = extract_E3D_spec(frame,signal,&(frame->signal),specId);
01222                 if (status) return(status);
01223         } 
01224         if ((noise != NULL) && (frame->noise != NULL)) {
01225                 status = extract_E3D_spec(frame,noise,&(frame->noise),specId);
01226         } 
01227 
01228         return(status);
01229 }
01230 
01231 int save_E3D_spec(E3D_file *frame, SPECTRUM *spectrum, E3Dspec_desc **type_of_spec, int specId) {
01232 
01233         char errtext[132];
01234         int status=0, i, len, idx;
01235         E3Dspec_desc *current_spec;
01236         fitsfile *fptr;
01237         int colnum, tformat;
01238         char format[132], colname[17];
01239         char *pt_data;
01240 
01241         sprintf(spectrum->name,"%s->s%.4d",frame->name,specId); 
01242         set_subspec(spectrum,spectrum->start,spectrum->end);
01243         spec_minmax(spectrum); 
01244  
01245         if (*type_of_spec == NULL) {    /* create first spectrum */ 
01246                 *type_of_spec = (E3Dspec_desc *)malloc(sizeof(E3Dspec_desc));
01247                 (*type_of_spec)[0].specId = LENS_UNDEF; 
01248                 i = 0; 
01249         } 
01250         else {  /* find which spectrum to rewrite */
01251  
01252                 for (i=0; i<frame->nbspec
01253                         && (*type_of_spec)[i].specId != specId 
01254                         && (*type_of_spec)[i].specId != LENS_UNDEF; i++); 
01255         } 
01256         if ((i == frame->nbspec) || ((*type_of_spec)[i].specId == LENS_UNDEF)) { 
01257                 *type_of_spec = (E3Dspec_desc *)realloc(
01258                                 (char *)*type_of_spec, 
01259                                 (i+2)*sizeof(E3Dspec_desc)); 
01260                 current_spec = (*type_of_spec+i); 
01261                 current_spec->specId = specId; 
01262                 current_spec->data_offset = frame->data_offset;
01263                 if (i == frame->nbspec) /* create new spectrum */ {
01264                         frame->nbspec++; 
01265                 }
01266                 else 
01267                         (current_spec+1)->specId = LENS_UNDEF;
01268         } 
01269         else
01270                 current_spec = *type_of_spec+i; 
01271 
01272         current_spec->start = spectrum->start; 
01273         current_spec->end = spectrum->end; 
01274         current_spec->npix = spectrum->npts; 
01275          
01276         switch (frame->data_format) {
01277 
01278         case TIGER_FORMAT :
01279                 lseek(frame->imno,current_spec->data_offset,SEEK_SET); 
01280                 len = spectrum->npts*sizeof_item(spectrum->data_type); 
01281                 if (frame->swapbytes) { 
01282                 for (i=0;i<spectrum->npts;i++)
01283                         write_DOS(frame->imno,(char *)(spectrum->data.s_data)+
01284                                 i*sizeof_item(spectrum->data_type), 
01285                                 sizeof_item(spectrum->data_type)); 
01286                 } 
01287                 else 
01288                         write(frame->imno,(char *)(spectrum->data.s_data),len); 
01289                 break;
01290 
01291         case EURO3D_FORMAT :
01292                 status = 0;
01293                 fptr = (fitsfile *)frame->external_info;
01294 
01295                 fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01296 
01297                 if (status) { 
01298                         status = 0;
01299                         fits_movabs_hdu(fptr, 1, NULL, &status);
01300                         fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01301                 }
01302 
01303                 /* write npix */
01304                 fits_get_colnum(fptr,0,E3D_COL_NPIX,&colnum,&status);
01305                 if (status) {
01306                         status = 0;
01307                         sprintf(format,"J");
01308                         fits_get_num_cols(fptr, &colnum, &status);
01309                         colnum++;
01310                         fits_insert_col(fptr,colnum,E3D_COL_NPIX,format,&status);
01311                 }
01312                 fits_write_col(fptr, TLONG, colnum, i+1, 1, 1, &(spectrum->npts), &status);
01313 
01314                 /* write starting pixel */
01315                 fits_get_colnum(fptr,0,E3D_COL_IDX,&colnum,&status);
01316                 if (status) {
01317                         status = 0;
01318                         sprintf(format,"J");
01319                         fits_get_num_cols(fptr, &colnum, &status);
01320                         colnum++;
01321                         fits_insert_col(fptr,colnum,E3D_COL_IDX,format,&status);
01322                 }
01323                 idx = (int)((spectrum->start - (*type_of_spec)[0].start) / spectrum->step);
01324                 fits_write_col(fptr, TLONG, colnum, i+1, 1, 1, &idx, &status);
01325 
01326                 /* write signal or noise */
01327                 if (*type_of_spec == frame->signal) 
01328                         strcpy(colname,E3D_COL_INT);
01329                 else 
01330                         strcpy(colname,E3D_COL_RMS);
01331                 switch(spectrum->data_type) {
01332                         case SHORT :
01333                                 if (frame->common_bounds)
01334                                         sprintf(format,"%dI",(int)frame->common_parameters[2]);
01335                                 else
01336                                         sprintf(format,"1PI");
01337                                 tformat = TSHORT;
01338                                 pt_data = (char *)spectrum->data.s_data;
01339                                 break;
01340                         case INT :
01341                         case LONG :
01342                                 if (frame->common_bounds)
01343                                         sprintf(format,"%dJ",(int)frame->common_parameters[2]);
01344                                 else
01345                                         sprintf(format,"1PJ");
01346                                 tformat = TLONG;
01347                                 pt_data = (char *)spectrum->data.l_data;
01348                                 break;
01349                         case FLOAT :
01350                                 if (frame->common_bounds)
01351                                         sprintf(format,"%dE",(int)frame->common_parameters[2]);
01352                                 else
01353                                         sprintf(format,"1PE");
01354                                 tformat = TFLOAT;
01355                                 pt_data = (char *)spectrum->data.f_data;
01356                                 break;
01357                         case DOUBLE :
01358                                 if (frame->common_bounds)
01359                                         sprintf(format,"%dD",(int)frame->common_parameters[2]);
01360                                 else
01361                                         sprintf(format,"1PD");
01362                                 tformat = TDOUBLE;
01363                                 pt_data = (char *)spectrum->data.d_data;
01364                                 break;
01365                 }
01366                 fits_get_colnum(fptr,0,colname,&colnum,&status);
01367                 if (status) {
01368                         status = 0;
01369                         fits_get_num_cols(fptr, &colnum, &status);
01370                         colnum++;
01371                         fits_insert_col(fptr,colnum,colname,format,&status);
01372                 }
01373                 fits_write_col(fptr, tformat, colnum, i+1, 1, spectrum->npts, pt_data, &status);
01374                 if (status) {
01375                         status = ERR_WRIT; break;
01376                 }
01377 
01378                 /* write data quality */
01379                 fits_get_colnum(fptr,0,E3D_COL_DQ,&colnum,&status);
01380                 if (status) {
01381                         status = 0;
01382                         if (frame->common_bounds)
01383                                 sprintf(format,"%dJ",(int)frame->common_parameters[2]);
01384                         else
01385                                 sprintf(format,"1PJ");
01386                         fits_get_num_cols(fptr, &colnum, &status);
01387                         colnum++;
01388                         fits_insert_col(fptr,colnum,E3D_COL_DQ,format,&status);
01389                 }
01390                 fits_write_col(fptr, TLONG, colnum, i+1, 1, spectrum->npts, spectrum->quality, &status);
01391                 if (status) {
01392                         status = ERR_WRIT; break;
01393                 }
01394 
01395                 if (i == 0) { /* first spectrum, create associated keywords */
01396                         fits_write_key_str(fptr,E3D_KW_UNITS,frame->cunit,"wavelength unit", &status);
01397                         fits_write_key_dbl(fptr,E3D_KW_START,spectrum->start,10,"reference wavelength", &status);
01398                         fits_write_key_dbl(fptr,E3D_KW_STEP,spectrum->step,10,"wavelength increment per pixel", &status);
01399                         fits_write_key_lng(fptr,E3D_KW_REFPIX,(long)1,"reference pixel", &status);
01400                 }
01401                 break;
01402 
01403                 default :
01404                 status = ERR_FORMAT;
01405                 break;
01406         }
01407         free_spec_mem(spectrum); 
01408  
01409         if (status) { 
01410                 sprintf(errtext,"put_E3D_spec: spec %s",spectrum->name); 
01411                 Handle_Error(errtext,status);
01412         } 
01413         else
01414                 if (frame->data_offset == current_spec->data_offset) {
01415                    frame->data_offset += len; 
01416                 }
01417         return(status);
01418 }
01419 
01420 /* --- Doxygen Function Comment -------------------------- */
01431 /* ------------------------------------------------------- */
01432 
01433 int 
01434 put_E3D_spec(E3D_file *frame, SPECTRUM *signal, SPECTRUM *noise, int specId)
01435 {
01436         int status;
01437         char errtext[132];
01438 
01439         if (frame->iomode == (int)I_MODE) {
01440                 sprintf(errtext,"put_E3D_spec: file %s is write protected",
01441                         frame->name);
01442                 Handle_Error(errtext,ERR_ACCESS); 
01443                 return(ERR_ACCESS); 
01444         } 
01445 
01446         if (signal != NULL) {
01447                 status = save_E3D_spec(frame,signal,&(frame->signal),specId);
01448                 if (status) return(status);
01449         } 
01450         if (noise != NULL) {
01451                 status = save_E3D_spec(frame,noise,&(frame->noise),specId);
01452         } 
01453 
01454         return(status);
01455 }
01456 
01457 /* --- Doxygen Function Comment -------------------------- */
01464 /* ------------------------------------------------------- */
01465 int 
01466 delete_E3D_spec(E3D_file *frame, int specId)
01467 {
01468         int status, i = -1, j = -1;
01469         char errtext[132];
01470         E3Dspec_desc *current_spec;
01471         fitsfile *fptr;
01472         
01473 
01474         status = ERR_NOIMA;
01475         if (frame->signal != NULL) {
01476             find_lens(frame,signal,specId,&i);
01477             if (i >= 0) {
01478                 current_spec = frame->signal+i;
01479                 if (i < (frame->nbspec -1))
01480                         memcpy((char *)current_spec,(char *)(frame->signal+i+1),
01481                                 (frame->nbspec-i-1)*sizeof(E3Dspec_desc));
01482                 }
01483         }
01484         if (frame->noise != NULL) {
01485             find_lens(frame,noise,specId,&j);
01486             if (j >= 0) {
01487                 current_spec = frame->noise+j;
01488                 if (j < (frame->nbspec -1))
01489                 memcpy((char *)current_spec,(char *)(frame->noise+j+1),
01490                         (frame->nbspec-j-1)*sizeof(E3Dspec_desc));
01491             }
01492         }
01493         if (i>= 0 || j>= 0)
01494                 frame->nbspec--;
01495 
01496         switch (frame->data_format) {
01497 
01498                 case EURO3D_FORMAT :
01499                 status = 0;
01500                 fptr = (fitsfile *)frame->external_info;
01501 
01502                 fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01503 
01504                 if (status) { 
01505                         status = 0;
01506                         fits_movabs_hdu(fptr, 1, NULL, &status);
01507                         fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01508                 }
01509 
01510                 fits_delete_rows(fptr,(long)(i+1),1L,&status);
01511                 status = get_tiger_errcode(frame->data_format,status);
01512                 break;
01513         }
01514         if (status)     {
01515                 sprintf(errtext,"delete_E3D_spec: spec %d in %s",specId,frame->name);
01516                 Handle_Error(errtext,status);
01517         }
01518         return(status);
01519 }
01520 
01521 /* --- Doxygen Function Comment -------------------------- */
01529 /* ------------------------------------------------------- */
01530 
01531 int get_E3D_spaxels(E3D_file *frame, int specId, SPAXEL **spaxels)
01532 {
01533         char errtext[132];
01534         int status, i, colnum, nspaxels;
01535         long *groups;
01536         char *spaxid, nullval[2]= "";
01537         float *xpos, *ypos, x, y;
01538         fitsfile *fptr;
01539         SPAXEL *cur_spax;
01540 
01541         if (frame->signal != NULL) {
01542                 find_lens(frame,signal,specId,&i); 
01543         }
01544         else {
01545                 find_lens(frame,noise,specId,&i); 
01546         }
01547 
01548         if (i < 0 ) 
01549                 status = ERR_NOIMA;
01550         else { 
01551                 switch (frame->data_format) {
01552                 case TIGER_FORMAT :
01553                         status = 0;
01554                         nspaxels = 1;
01555                         cur_spax = (SPAXEL *)calloc(1,sizeof(SPAXEL));
01556                         *spaxels = cur_spax;
01557                         cur_spax[0].specId = specId;
01558                         cur_spax[0].spaxelId = 0;
01559                         cur_spax[0].group = 1;
01560                         get_lens_coordinates(frame, specId, &x, &y);
01561                         cur_spax[0].xpos = x;
01562                         cur_spax[0].ypos = y;
01563                         break;
01564 
01565                 case EURO3D_FORMAT :
01566                         status = 0;
01567                         fptr = (fitsfile *)frame->external_info;
01568 
01569                         fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01570         
01571                         if (status) { 
01572                                 status = 0;
01573                                 fits_movabs_hdu(fptr, 1, NULL, &status);
01574                                 fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01575                         }
01576                         fits_get_colnum(fptr,0,E3D_COL_NSPAX,&colnum,&status);
01577                         fits_read_col_lng(fptr, colnum, i+1, 1, 1, 0, (long *)&nspaxels, NULL, &status);
01578                         xpos = (float *)calloc(nspaxels,sizeof(float));
01579                         fits_get_colnum(fptr,0,E3D_COL_XPOS,&colnum,&status);
01580                         fits_read_col_flt(fptr, colnum, i+1, 1, (long)nspaxels, 0, xpos, NULL, &status);
01581                         ypos = (float *)calloc(nspaxels,sizeof(float));
01582                         fits_get_colnum(fptr,0,E3D_COL_YPOS,&colnum,&status);
01583                         fits_read_col_flt(fptr, colnum, i+1, 1, (long)nspaxels, 0, ypos, NULL, &status);
01584                         groups = (long *)calloc(nspaxels,sizeof(long));
01585                         fits_get_colnum(fptr,0,E3D_COL_GRP,&colnum,&status);
01586                         fits_read_col_lng(fptr, colnum, i+1, 1, (long)nspaxels, 0, (long *)groups, NULL, &status);
01587                         spaxid = calloc(nspaxels+1,sizeof(char));
01588                         fits_get_colnum(fptr,0,E3D_COL_SPAXID,&colnum,&status);
01589                         fits_read_col_str(fptr, colnum, i+1, 1, (long)nspaxels, nullval, &spaxid, NULL, &status);
01590                         if (status) {
01591                                 status = ERR_READ; break;
01592                         }
01593                         cur_spax = (SPAXEL *)calloc(nspaxels,sizeof(SPAXEL));
01594                         *spaxels = cur_spax;
01595                         for (i=0; i<nspaxels; i++) {
01596                                 cur_spax[i].specId = specId;
01597                                 cur_spax[i].spaxelId = spaxid[i];
01598                                 cur_spax[i].group = groups[i];
01599                                 cur_spax[i].xpos = xpos[i];
01600                                 cur_spax[i].ypos = ypos[i];
01601                         }
01602                         free(xpos);free(ypos);free(groups);free(spaxid);
01603                         break;
01604 
01605                 default :
01606                         status = ERR_FORMAT;
01607                         break;
01608                 }
01609         } 
01610         if (status) {
01611                 sprintf(errtext,"get_E3D_spaxels: spec %d",specId);
01612                 Handle_Error(errtext,status); 
01613         } 
01614         else
01615                 status = nspaxels;
01616         return(status);
01617 }
01618 
01619 /* --- Doxygen Function Comment -------------------------- */
01628 /* ------------------------------------------------------- */
01629 
01630 int put_E3D_spaxels(E3D_file *frame, int specId, int nspax, SPAXEL *spaxels)
01631 {
01632         char errtext[132];
01633         int status, i, j, colnum;
01634         long nspaxels = nspax;
01635         long id = specId;
01636         long *groups;
01637         char *spaxid;
01638         float *xpos, *ypos;
01639         fitsfile *fptr;
01640         SPAXEL *cur_spax;
01641 
01642         if (frame->signal != NULL) {
01643                 find_lens(frame,signal,specId,&i); 
01644         }
01645         else {
01646                 find_lens(frame,noise,specId,&i); 
01647         }
01648 
01649         if (i < 0 ) 
01650                 status = ERR_NOIMA;
01651         else { 
01652                 switch (frame->data_format) {
01653                 case TIGER_FORMAT :
01654                         status = 0;
01655                         xpos = &(spaxels[0].xpos);
01656                         ypos = &(spaxels[0].ypos);
01657                         set_lens_coordinates(frame, specId, xpos, ypos);
01658                         break;
01659 
01660                 case EURO3D_FORMAT :
01661                         status = 0;
01662                         cur_spax = spaxels;
01663                         xpos = (float *)calloc(nspaxels,sizeof(float));
01664                         ypos = (float *)calloc(nspaxels,sizeof(float));
01665                         groups = (long *)calloc(nspaxels,sizeof(long));
01666                         spaxid = (char *)calloc(nspaxels+1,sizeof(char));
01667                         for (j=0; j<nspaxels; j++) {
01668                                 groups[j] = cur_spax[j].group;
01669                                 xpos[j] = cur_spax[j].xpos;
01670                                 ypos[j] = cur_spax[j].ypos;
01671                                 spaxid[j] = cur_spax[j].spaxelId;
01672                         }
01673                         spaxid[nspaxels] = '\0';
01674                         fptr = (fitsfile *)frame->external_info;
01675 
01676                         fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01677         
01678                         if (status) { 
01679                                 status = 0;
01680                                 fits_movabs_hdu(fptr, 1, NULL, &status);
01681                                 fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01682                         }
01683                         fits_get_colnum(fptr,0,E3D_COL_ID,&colnum,&status);
01684                         fits_write_col(fptr, TLONG, colnum, i+1, 1, 1, &id, &status);
01685                         fits_get_colnum(fptr,0,E3D_COL_NSPAX,&colnum,&status);
01686                         fits_write_col(fptr, TLONG, colnum, i+1, 1, 1, &nspaxels, &status);
01687                         fits_get_colnum(fptr,0,E3D_COL_XPOS,&colnum,&status);
01688                         fits_write_col(fptr, TFLOAT, colnum, i+1, 1, nspaxels, xpos, &status);
01689                         fits_get_colnum(fptr,0,E3D_COL_YPOS,&colnum,&status);
01690                         fits_write_col(fptr, TFLOAT, colnum, i+1, 1, nspaxels, ypos, &status);
01691                         fits_get_colnum(fptr,0,E3D_COL_GRP,&colnum,&status);
01692                         fits_write_col(fptr, TLONG, colnum, i+1, 1, nspaxels, groups, &status);
01693                         fits_get_colnum(fptr,0,E3D_COL_SPAXID,&colnum,&status);
01694                         fits_write_col(fptr, TSTRING, colnum, i+1, 1, nspaxels, &spaxid, &status);
01695                         free(xpos);free(ypos);free(groups); free(spaxid);
01696 
01697                         if (status) {
01698                                 status = ERR_WRIT; break;
01699                         }
01700                         break;
01701 
01702                 default :
01703                         status = ERR_FORMAT;
01704                         break;
01705                 }
01706         } 
01707         if (status) {
01708                 sprintf(errtext,"put_E3D_spaxels: spec %d",specId);
01709                 Handle_Error(errtext,status); 
01710         } 
01711         return(status);
01712 }
01713 
01714 /* --- Doxygen Function Comment -------------------------- */
01721 /* ------------------------------------------------------- */
01722 
01723 int exist_group_ID(E3D_file *frame, int groupID)
01724 {
01725         int j, found = 0;
01726 
01727         /* check if group is already defined */
01728 
01729         for (j=0; j<frame->ngroups && !found; j++) {
01730                 if (frame->groups[j].groupId == groupID) {
01731                                 /* group already exist */
01732                                 found = 1;
01733                 }
01734         }
01735         if (found > 0)
01736                 return(j);
01737         else
01738                 return(-1);
01739 }
01740 
01741 /* --- Doxygen Function Comment -------------------------- */
01750 /* ------------------------------------------------------- */
01751 
01752 int get_E3D_groups(E3D_file *frame, int specId, GROUP **groups)
01753 {
01754         char errtext[132];
01755         int status, i, j, colnum, ngroups;
01756         long *groups_id;
01757         fitsfile *fptr;
01758 
01759         if (frame->signal != NULL) {
01760                 find_lens(frame,signal,specId,&i); 
01761         }
01762         else {
01763                 find_lens(frame,noise,specId,&i); 
01764         }
01765 
01766         if (i < 0 ) 
01767                 status = ERR_NOIMA;
01768         else { 
01769                 switch (frame->data_format) {
01770                 case TIGER_FORMAT :
01771                         ngroups = 1;
01772                         break;
01773 
01774                 case EURO3D_FORMAT :
01775                         status = 0;
01776                         /* ngroups = (long)frame->common_parameters[2]; */
01777                         ngroups = frame->ngroups;
01778                         fptr = (fitsfile *)frame->external_info;
01779 
01780                         fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01781         
01782                         if (status) { 
01783                                 status = 0;
01784                                 fits_movabs_hdu(fptr, 1, NULL, &status);
01785                                 fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
01786                         }
01787                         fits_get_colnum(fptr,0,E3D_COL_GRP,&colnum,&status);
01788                         groups_id = (long *)calloc(ngroups,sizeof(long));
01789                         fits_read_col_lng(fptr, colnum, i+1, 1, (long)ngroups, 
01790                                 0, groups_id, NULL, &status);
01791                         if (status) {
01792                                 status = ERR_READ; break;
01793                         }
01794                         *groups = (GROUP *)calloc(ngroups,sizeof(GROUP));
01795                         for (i=0; i<ngroups; i++) {
01796                              for(j=0; j<frame->ngroups; j++)
01797                                 if (frame->groups[j].groupId == groups_id[i]) {
01798                                         memcpy((*groups)+i,frame->groups+j,sizeof(GROUP));                      
01799                                 }
01800                         }
01801                         free(groups_id);
01802                         break;
01803 
01804                 default :
01805                         status = ERR_FORMAT;
01806                         break;
01807                 }
01808         } 
01809         if (status) {
01810                 sprintf(errtext,"get_E3D_groups: spec %d",specId);
01811                 Handle_Error(errtext,status); 
01812         } 
01813         else
01814                 return ngroups;
01815         return(status);
01816 }
01817 
01818 /* --- Doxygen Function Comment -------------------------- */
01826 /* ------------------------------------------------------- */
01827 
01828 int put_E3D_groups(E3D_file *frame, int ngroups, GROUP *groups)
01829 {
01830         char errtext[132];
01831         int status = 0;
01832         int i, j, found = 0;
01833 
01834                 /* add new group(s) in frame structure */
01835 
01836         for (i=0; i<ngroups && !found; i++) {
01837 
01838                 found = 0;
01839 
01840                 /* check if group not already defined */
01841 
01842                 for (j=0; j<frame->ngroups && !found; j++) {
01843                         if (frame->groups[j].groupId == groups[i].groupId) {
01844                                                                 /* groups already exist */
01845                                 found = 1;
01846                         }
01847                 }
01848                 if (!found) {
01849                         frame->groups =(GROUP *)realloc(frame->groups,(frame->ngroups+1)*sizeof(GROUP));
01850                         memcpy((char *)(frame->groups+frame->ngroups),(char *)(groups+i),sizeof(GROUP));
01851                         frame->ngroups++;
01852                 }
01853                 else {
01854                         memcpy((char *)(frame->groups+j-1),(char *)(groups+i),sizeof(GROUP));
01855                 }
01856         }
01857 
01858         if (status) {
01859                 sprintf(errtext,"put_E3D_group: file %s",frame->name);
01860                 Handle_Error(errtext,status); 
01861         } 
01862         return(status);
01863 }
01864 
01865 /* --- Doxygen Function Comment -------------------------- */
01872 /* ------------------------------------------------------- */
01873 
01874 int delete_E3D_group(E3D_file *frame, int groupID)
01875 {
01876         char errtext[132];
01877         int status = 0;
01878         int j, found = 0;
01879 
01880         /* check if group is defined */
01881 
01882         for (j=0; j<frame->ngroups && !found; j++) {
01883                 if (frame->groups[j].groupId == groupID) {
01884                                                         /* group exist */
01885                         found = 1;
01886                 }
01887         }
01888         if (found) {    /* delete group */
01889                 for (; j<frame->ngroups -1; j++) {
01890                         memcpy(frame->groups+j,frame->groups+j+1,sizeof(GROUP));
01891                 }
01892                 frame->ngroups--;
01893         }
01894         else
01895                 status = ERR_BAD_PARAM;
01896 
01897         if (status) {
01898                 sprintf(errtext,"delete_E3D_group: file %s, group %d",frame->name, groupID);
01899                 Handle_Error(errtext,status); 
01900         } 
01901         return(status);
01902 }
01903 
01904 /* --- Doxygen Function Comment -------------------------- */
01917 /* ------------------------------------------------------- */
01918 
01919 int 
01920 get_E3D_row(E3D_file *frame, int specId, SPECTRUM *signal, SPECTRUM *noise, int *nspax , SPAXEL **spaxels, int *ngroups, GROUP **groups)
01921 {
01922         int status;
01923 
01924         status = get_E3D_spec(frame,signal,noise,specId);
01925         if (!status) {
01926                 *nspax = get_E3D_spaxels(frame,specId,spaxels);
01927                 *ngroups = get_E3D_groups(frame,specId,groups);
01928         }
01929 
01930         return(status);
01931 }
01932 
01933 /* --- Doxygen Function Comment -------------------------- */
01946 /* ------------------------------------------------------- */
01947 
01948 int 
01949 put_E3D_row(E3D_file *frame, int specId, SPECTRUM *signal, SPECTRUM *noise, int nspax, SPAXEL *spaxels, int ngroups, GROUP *groups)
01950 {
01951         int status;
01952 
01953         status = put_E3D_spec(frame,signal,noise,specId);
01954         if (!status) {
01955                 status = put_E3D_spaxels(frame,specId,nspax,spaxels);
01956                 status = put_E3D_groups(frame,ngroups,groups);
01957         }
01958         return(status);
01959 }
01960 
01961 /* --- Doxygen Function Comment -------------------------- */
01969 /* ------------------------------------------------------- */
01970 
01971 int 
01972 get_E3D_frame(E3D_file *E3Dframe, IMAGE2D *signal, IMAGE2D *noise)
01973 {
01974         int status;
01975         int npix, i, j;
01976         double start, step, end;
01977         SPECTRUM spec;
01978 
01979         switch (E3Dframe->data_format) {
01980 
01981                 case TIGER_FORMAT :
01982                         status = ERR_NOIMPL;
01983                         break;
01984 
01985                 case EURO3D_FORMAT :
01986 
01987                         if (signal != NULL) {
01988                                 memset(signal->ident,' ',lg_ident);
01989                                 signal->ident[lg_ident] = '\0';
01990                                 memset(signal->cunit,' ',lg_unit);
01991                                 signal->cunit[lg_unit] = '\0';
01992                                 memset(signal->history,' ',lg_hist);
01993                                 signal->history[lg_hist] = '\0';
01994                                 signal->external_info = 0;
01995                                 signal->file_type = IMA_TYPE;
01996                                 signal->data_format = InputIO.basic_io;
01997                                 signal->data_type = E3Dframe->data_type;
01998                                 sprintf(signal->name,"%s[%s]",E3Dframe->name,E3D_DATA);
01999                                 get_common_param(E3Dframe, &npix, &start, &end);
02000                                 signal->startx = start;
02001                                 signal->starty = 1;
02002                                 signal->stepx = step;
02003                                 signal->stepy = 1;
02004                                 signal->nx = npix;
02005                                 signal->ny = E3Dframe->nbspec;
02006                                 signal->endx = end;
02007                                 signal->endy = E3Dframe->nbspec;
02008                                 if (alloc_frame_mem(signal, signal->data_type) < 0) {
02009                                         status = ERR_ALLOC;
02010                                         break;
02011                                 }
02012                                 for (j=0; j<signal->ny; j++) {
02013                                         get_E3D_spec(E3Dframe,&spec,NULL,E3Dframe->signal[j].specId);
02014                                         for (i=0; i<signal->nx; i++) {
02015                                                 WR_frame(signal,i,j,RD_spec(&spec,i));
02016                                                 WR_qframe(signal,i,j,RD_qspec(&spec,i));
02017                                         }
02018                                         free_spec_mem(&spec);
02019                                 }
02020                                 image_minmax(signal);
02021                         }
02022                         if (noise != NULL) {
02023                                 memset(noise->ident,' ',lg_ident);
02024                                 noise->ident[lg_ident] = '\0';
02025                                 memset(noise->cunit,' ',lg_unit);
02026                                 noise->cunit[lg_unit] = '\0';
02027                                 memset(noise->history,' ',lg_hist);
02028                                 noise->history[lg_hist] = '\0';
02029                                 noise->external_info = 0;
02030                                 noise->file_type = IMA_TYPE;
02031                                 noise->data_format = InputIO.basic_io;
02032                                 noise->data_type = E3Dframe->data_type;
02033                                 sprintf(noise->name,"%s[%s]",E3Dframe->name,E3D_DATA);
02034                                 get_common_param(E3Dframe, &npix, &start, &end);
02035                                 noise->startx = start;
02036                                 noise->starty = 1;
02037                                 noise->stepx = step;
02038                                 noise->stepy = 1;
02039                                 noise->nx = npix;
02040                                 noise->ny = E3Dframe->nbspec;
02041                                 noise->endx = end;
02042                                 noise->endy = E3Dframe->nbspec;
02043                                 if (alloc_frame_mem(noise, E3Dframe->data_type) < 0) {
02044                                         status = ERR_ALLOC;
02045                                         break;
02046                                 }
02047                                 for (j=0; j<noise->ny; j++) {
02048                                         get_E3D_spec(E3Dframe,NULL,&spec,E3Dframe->noise[j].specId);
02049                                         for (i=0; i<noise->nx; i++) {
02050                                                 WR_frame(noise,i,j,RD_spec(&spec,i));
02051                                                 WR_qframe(noise,i,j,RD_qspec(&spec,i));
02052                                         }
02053                                         free_spec_mem(&spec);
02054                                 }
02055                                 image_minmax(noise);
02056                         }
02057                         break;
02058         }
02059 
02060         return(status);
02061 }
02062 
02063 /* --- Doxygen Function Comment -------------------------- */
02070                 /* @{ ***** Starting group8 ***** */
02076 /* ------------------------------------------------------- */
02077 int 
02078 has_common_bounds(E3D_file *frame)              
02079 {
02080         if (frame->common_bounds)
02081                 return(1);
02082         else
02083                 return(0);
02084 }
02085 
02086 /* --- Doxygen Function Comment -------------------------- */
02095 /* ------------------------------------------------------- */
02096 int 
02097 get_common_param(E3D_file *frame, int *npix, double *start, double *end)                
02098 {
02099         int status;
02100 
02101         status = ERR_NODATA;
02102 
02103         if (frame->common_bounds) {
02104                 *npix = (int)(frame->common_parameters[2]);
02105                 *start = frame->common_parameters[0];
02106                 *end = frame->common_parameters[1];
02107                 status = 0;
02108         }
02109         return(status);
02110 }
02111 
02112 /* --- Doxygen Function Comment -------------------------- */
02118 /* ------------------------------------------------------- */
02119 int 
02120 set_common_bounds(E3D_file *frame)              
02121 {
02122         E3Dspec_desc *current_spec;
02123         double start;
02124         int    i, npix;
02125 
02126        /* checks for common bounds for all spectra */
02127 
02128        if (frame->nbspec > 0)
02129               frame->common_bounds = 1;       /* assume yes, and let's see */
02130        else
02131               frame->common_bounds = 0;
02132 
02133        if (frame->signal != NULL && frame->nbspec > 0) {
02134                current_spec = frame->signal;
02135                start = current_spec->start;
02136                npix = current_spec->npix;
02137                current_spec++;
02138                for (i=1; i < frame->nbspec
02139                        && (current_spec->start == start
02140                        && current_spec->npix == npix);
02141                        i++, current_spec++);
02142                if (i < frame->nbspec)
02143                        frame->common_bounds = 0;
02144        }
02145        if (frame->noise != NULL && frame->nbspec > 0) {
02146                current_spec = frame->noise;
02147                start = current_spec->start;
02148                npix = current_spec->npix;
02149                current_spec++;
02150                for (i=1; i < frame->nbspec
02151                        && (current_spec->start == start
02152                        && current_spec->npix == npix);
02153                        i++, current_spec++);
02154                if (i < frame->nbspec)
02155                        frame->common_bounds = 0;
02156        }
02157 
02158         return(0);
02159 }
02160 
02161 /* --- Doxygen Function Comment -------------------------- */
02167 /* ------------------------------------------------------- */
02168 int 
02169 exist_statistical_error(E3D_file *frame)                
02170 {
02171         if (frame->noise != NULL)
02172                 return(TRUE);
02173         else
02174                 return(FALSE);
02175 }
02176 
02177 /* --- Doxygen Function Comment -------------------------- */
02184                 /* @{ ***** Starting group9 ***** */
02191 /* ------------------------------------------------------- */
02192 int 
02193 exist_spec_ID(E3D_file *frame, int specId)              
02194 {
02195         int i;
02196 
02197         if (frame->signal != NULL) {
02198                 find_lens(frame,signal,specId,&i);
02199         }
02200         else {
02201                 if (frame->noise != NULL) {
02202                         find_lens(frame,noise,specId,&i);
02203                 }
02204         }
02205         if (i >= 0) 
02206                 return(i); /* returns true, number exists */
02207         else
02208                 return(-1);
02209 }
02210 
02211 /* --- Doxygen Function Comment -------------------------- */
02221 /* ------------------------------------------------------- */
02222 int set_ID_and_coordinates(E3D_file *frame, int nbl,int *specId, float *xlens, float *ylens)    
02223 {
02224         int status = 0;
02225         int one = 1;
02226         int i, col_no, col_xl, col_yl, col_sp, col_gr;
02227         char errtext[132];
02228         TABLE tbl_lens;
02229         char table_name[lg_name+1];
02230 
02231         sprintf(table_name,"%s[%s]",frame->name,E3D_DATA);
02232         append_tbl_extension(table_name,InputIO.basic_io);
02233 
02234         if (frame->data_format != EURO3D_FORMAT)
02235                 return(ERR_BAD_PARAM);
02236 
02237         status = open_table(&tbl_lens,table_name,"IO");
02238         if (status < 0) {
02239                 sprintf(errtext,"set_ID_and_coordinates");
02240                 Handle_Error(errtext,status);
02241                 return(status);
02242         }
02243         col_no = get_col_ref(&tbl_lens,E3D_COL_ID);
02244         col_sp = get_col_ref(&tbl_lens,E3D_COL_NSPAX);
02245         col_xl = get_col_ref(&tbl_lens,E3D_COL_XPOS);
02246         col_yl = get_col_ref(&tbl_lens,E3D_COL_YPOS);
02247         col_gr = get_col_ref(&tbl_lens,E3D_COL_GRP);
02248         for (i=0;i<nbl;i++) {
02249                 WR_tbl(&tbl_lens,i,col_no,specId+i);
02250                 WR_tbl(&tbl_lens,i,col_sp,&one);
02251                 WR_tbl(&tbl_lens,i,col_xl,xlens+i);
02252                 WR_tbl(&tbl_lens,i,col_yl,ylens+i);
02253                 WR_tbl(&tbl_lens,i,col_gr,&one);
02254         }
02255         close_table(&tbl_lens);
02256         return(status);
02257 }
02258 /* --- Doxygen Function Comment -------------------------- */
02267 /* ------------------------------------------------------- */
02268 int get_ID_and_coordinates(E3D_file *frame, int *specId, float *xlens, float *ylens)    
02269 {
02270         int status = 0;
02271         int i, col_no, col_xl, col_yl;
02272         char errtext[132];
02273         char table_name[lg_name+1];
02274         TABLE tbl_lens;
02275 
02276         sprintf(table_name,"%s[%s]",frame->name,E3D_DATA);
02277         append_tbl_extension(table_name,InputIO.basic_io);
02278 
02279         if (frame->data_format != EURO3D_FORMAT)
02280                 return(ERR_BAD_PARAM);
02281 
02282         status = open_table(&tbl_lens,table_name,"I");
02283         if (status) {
02284                 sprintf(errtext,"get_ID_and_coordinates");
02285                 Handle_Error(errtext,status);
02286                 return(status);
02287         }
02288         col_no = get_col_ref(&tbl_lens,E3D_COL_ID);
02289         col_xl = get_col_ref(&tbl_lens,E3D_COL_XPOS);
02290         col_yl = get_col_ref(&tbl_lens,E3D_COL_YPOS);
02291         if ((col_no < 0) || (col_xl < 0) || (col_yl < 0)) {
02292                         status = ERR_NOCOL;
02293                         sprintf(errtext,"get_ID_and_coordinates");
02294                         Handle_Error(errtext,status);
02295                         return(status);
02296         }
02297         for (i=0;i<tbl_lens.row;i++) {
02298                 RD_tbl(&tbl_lens,i,col_no,specId+i);
02299                 RD_tbl(&tbl_lens,i,col_xl,xlens+i);
02300                 RD_tbl(&tbl_lens,i,col_yl,ylens+i);
02301         }
02302         close_table(&tbl_lens);
02303         if (status) {
02304                 sprintf(errtext,"get_ID_and_coordinates");
02305                 Handle_Error(errtext,status);
02306                 return(status);
02307         }
02308         return(status);
02309 }
02310 /* --- Doxygen Function Comment -------------------------- */
02317 /* ------------------------------------------------------- */
02318 int get_spectra_ID(E3D_file *frame, int *specId)        
02319 {
02320         int status = 0;
02321         int i;
02322         char errtext[132];
02323         E3Dspec_desc *pt_spec_desc;
02324 
02325         pt_spec_desc = NULL;
02326 
02327         if (frame->signal != NULL) {
02328                 pt_spec_desc = frame->signal;
02329                 if (frame->noise != NULL) 
02330                         pt_spec_desc = frame->noise;
02331         }
02332 
02333         if (pt_spec_desc != NULL) {
02334 
02335                 for (i=0; i<frame->nbspec;i++)
02336                         specId[i] = pt_spec_desc[i].specId;
02337         }
02338         else 
02339                 status = ERR_BAD_PARAM;
02340                 
02341         if (status) {
02342                 sprintf(errtext,"get_spectra_ID");
02343                 Handle_Error(errtext,status);
02344                 return(status);
02345         }
02346         return(status);
02347 }
02348 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
02349 !    
02350 !.blk                   Routines for E3D SLICES i/o 
02351 !                                      
02352         routines to read/write values into spectrum files are
02353                      given in ../incl/funcdef.h
02354 !
02355 !.func                        RD_slice()
02356 !
02357 !.purp                  reads a slice value
02358 !.desc
02359 ! (type) value = RD_slice(slice,index)
02360 !
02361 ! SLICE *slice;         slice structure
02362 ! int index;            index of value to read
02363 !.ed
02364 !    
02365 !.func                        WR_slice()
02366 !
02367 !.purp                  writes a slice value
02368 !.desc
02369 ! WR_slice(slice,index,value)
02370 !
02371 ! SLICE *slice;         slice structure
02372 ! int index;            index of value to write
02373 ! (type) value;         value to write
02374 !.ed
02375 !
02376 !.func                        RD_qslice()
02377 !
02378 !.purp                  reads slice quality flag
02379 !.desc
02380 ! (type) value = RD_qslice(slice,index)
02381 !
02382 ! SLICE *slice;         slice structure
02383 ! int index;            index of value to read
02384 !.ed
02385 !    
02386 !.func                        WR_qslice()
02387 !
02388 !.purp                  writes slice quality flag
02389 !.desc
02390 ! WR_qslice(slice,index,value)
02391 !
02392 ! SLICE *slice;         slice structure
02393 ! int index;            index of value to write
02394 ! (type) value;         value to write
02395 !.ed
02396 -------------------------------------------------------------------- */
02397 
02398 /* --- Doxygen Function Comment -------------------------- */
02406 /* ------------------------------------------------------- */
02407 int alloc_slice_mem(SLICE *slice, short type, int npts) 
02408 {
02409         int status = 0;
02410 
02411         slice->npts = npts;
02412         slice->data_type = type;
02413 
02414         slice->specId = (int *)calloc(slice->npts,sizeof(int));
02415         if (slice->specId == 0) return(ERR_ALLOC);
02416 
02417         switch(slice->data_type) {
02418         case SHORT :
02419             slice->data.s_data = (short *)calloc(slice->npts,sizeof(short));
02420             if (slice->data.s_data == 0) status = ERR_ALLOC;
02421             break;
02422         case INT :
02423         case LONG :
02424             slice->data.l_data = (long *)calloc(slice->npts,sizeof(long));
02425             if (slice->data.l_data == 0) status = ERR_ALLOC;
02426             break;
02427         case FLOAT :
02428             slice->data.f_data = (float *)calloc(slice->npts,sizeof(float));
02429             if (slice->data.f_data == 0) status = ERR_ALLOC;
02430             break;
02431         case DOUBLE :
02432             slice->data.d_data = (double *)calloc(slice->npts,sizeof(double));
02433             if (slice->data.d_data == 0) status = ERR_ALLOC;
02434             break;
02435         }
02436         if (status == 0) {
02437                 slice->quality = (unsigned long *)calloc(slice->npts,sizeof(unsigned long));
02438                 if (slice->quality == 0) status = ERR_ALLOC;
02439         }
02440         return(status);
02441 }
02442 
02443 /* --- Doxygen Function Comment -------------------------- */
02449 /* ------------------------------------------------------- */
02450 int free_slice_mem(SLICE *slice) 
02451 {
02452         int status = 0;
02453 
02454         free(slice->specId);
02455 
02456         switch(slice->data_type) {
02457         case SHORT :
02458         case INT :
02459         case LONG :
02460         case FLOAT :
02461         case DOUBLE :
02462             free((char *)slice->data.s_data);
02463             break;
02464         }
02465         slice->data.d_data = 0x0;
02466         free(slice->quality);
02467         return(status);
02468 }
02469 
02470 /* --- Doxygen Function Comment -------------------------- */
02477 /* ------------------------------------------------------- */
02478 int 
02479 init_new_E3D_slice(E3D_file *frame, SLICE *slice)
02480 {
02481         char errtext[132], table_name[lg_name+1];
02482         int status, i;
02483         E3Dspec_desc *pt_spec = NULL;
02484         TABLE tbl_lens;
02485         
02486         slice->index = -1;
02487         slice->npts = frame->nbspec;
02488 
02489         if (frame->signal != NULL)
02490                 pt_spec = frame->signal;
02491         else {
02492             if (frame->noise != NULL)
02493                 pt_spec = frame->noise;
02494         }
02495 
02496         if (pt_spec == NULL) {
02497 
02498                 strcpy(table_name,frame->table_name);
02499                 append_tbl_extension(table_name,InputIO.basic_io);
02500                 if (open_table(&tbl_lens,table_name,"I") < 0) 
02501                         return(ERR_OPEN);
02502                 slice->npts = tbl_lens.row;
02503                 close_table(&tbl_lens);
02504 
02505                 status = alloc_slice_mem(slice, frame->data_type, slice->npts);
02506                 get_lenses_no_from_table(frame,slice->specId);
02507         }
02508         else {
02509                 status = alloc_slice_mem(slice, frame->data_type, slice->npts);
02510                 for (i=0;i<slice->npts;i++)
02511                         slice->specId[i] = pt_spec[i].specId;
02512         }
02513 
02514         if (status) {
02515                 sprintf(errtext,"init_new_E3D_slice");
02516                 Handle_Error(errtext,status);
02517         }
02518         return(status);
02519 }
02520 
02521 int extract_E3D_slice(E3D_file *frame, SLICE *slice, E3Dspec_desc **type_of_slice, int index) {
02522 
02523         char errtext[132];
02524         E3Dspec_desc *current_spec;
02525         int status, i, colnum;
02526         long inc[2];
02527         fitsfile *fptr;
02528         long npix[2], lpixel[2], fpixel[2];
02529 
02530         current_spec = *type_of_slice;
02531         status = init_new_E3D_slice(frame,slice); 
02532         slice->index = index; 
02533 
02534         switch (frame->data_format) {
02535         case TIGER_FORMAT :
02536             for (i=0;i <frame->nbspec; i++, current_spec++) { 
02537                 slice->specId[i] = current_spec->specId; 
02538                 if (index < current_spec->npix) { 
02539                         lseek(frame->imno,(current_spec->data_offset 
02540                                 +index*sizeof_item(slice->data_type)),SEEK_SET); 
02541                 if (frame->swapbytes) { 
02542                         read_DOS(frame->imno,(char *)((char *)(slice->data.s_data)+
02543                                 i*sizeof_item(slice->data_type)), 
02544                                 sizeof_item(slice->data_type)); 
02545                         } else { 
02546                         read(frame->imno,(char *)((char *)(slice->data.s_data)+
02547                                 i*sizeof_item(slice->data_type)), 
02548                                 sizeof_item(slice->data_type)); 
02549                         } 
02550                 }
02551                 else 
02552                         WR_slice(slice,i,DATA_UNDEF); 
02553             } 
02554             break;
02555 
02556         case EURO3D_FORMAT :
02557             status = 0;
02558             fptr = (fitsfile *)frame->external_info;
02559 
02560             fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
02561         
02562             if (status) { 
02563                 status = 0;
02564                 fits_movabs_hdu(fptr, 1, NULL, &status);
02565                 fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
02566             }
02567         
02568                 /* read signal or noise */
02569             if (*type_of_slice == frame->signal) 
02570                 fits_get_colnum(fptr,0,E3D_COL_INT,&colnum,&status);
02571             else 
02572                 fits_get_colnum(fptr,0,E3D_COL_RMS,&colnum,&status);
02573             if (status) {
02574                 status = ERR_BAD_COL; break;
02575             }
02576             inc[0] = 1;
02577             inc[1] = 1;
02578             npix[0] = (long)(frame->nbspec);
02579             npix[1] = (long)(frame->common_parameters[2]); 
02580             fpixel[0] = index+1; lpixel[0] = index+1;
02581             switch(slice->data_type) {
02582                 case SHORT :
02583                 for (i=0;i <frame->nbspec; i++, current_spec++) { 
02584                         fpixel[1] = i+1; lpixel[1] = i+1;
02585                         slice->specId[i] = current_spec->specId; 
02586                         if (index < current_spec->npix) { 
02587                                 fits_read_subset_sht(fptr,colnum,1,npix,fpixel,lpixel,inc,0,slice->data.s_data+i,NULL,&status);
02588                         }
02589                         else 
02590                                 slice->data.s_data[i] = -MAXSHORT;
02591                 }
02592                 break;
02593                 case INT :
02594                 case LONG :
02595                 for (i=0;i <frame->nbspec; i++, current_spec++) { 
02596                         fpixel[1] = i+1; lpixel[1] = i+1;
02597                         slice->specId[i] = current_spec->specId; 
02598                         if (index < current_spec->npix) { 
02599                                 fits_read_subset_lng(fptr,colnum,1,npix,fpixel,lpixel,inc,0,slice->data.l_data+i,NULL,&status);
02600                         }
02601                         else 
02602                                 slice->data.l_data[i] = -MAXLONG;
02603                 }
02604                 break;
02605                 case FLOAT :
02606                 for (i=0;i <frame->nbspec; i++, current_spec++) { 
02607                         fpixel[1] = i+1; lpixel[1] = i+1;
02608                         slice->specId[i] = current_spec->specId; 
02609                         if (index < current_spec->npix) { 
02610                                 fits_read_subset_flt(fptr,colnum,1,npix,fpixel,lpixel,inc,0,slice->data.f_data+i,NULL,&status);
02611                         }
02612                         else 
02613                                 slice->data.f_data[i] = -MAXFLOAT;
02614                 }
02615                 break;
02616                 case DOUBLE :
02617                 for (i=0;i <frame->nbspec; i++, current_spec++) { 
02618                         fpixel[1] = i+1; lpixel[1] = i+1;
02619                         slice->specId[i] = current_spec->specId; 
02620                         if (index < current_spec->npix) { 
02621                                 fits_read_subset_dbl(fptr,colnum,1,npix,fpixel,lpixel,inc,0,slice->data.d_data+i,NULL,&status);
02622                         }
02623                         else 
02624                                 slice->data.d_data[i] = -MAXDOUBLE;
02625                 }
02626                 break;
02627             }           
02628             break;
02629 
02630             default :
02631             status = ERR_FORMAT;
02632             break;
02633         }
02634         if (status) {
02635                 sprintf(errtext,"get_E3D_slice: slice %d",slice->index); 
02636                 Handle_Error(errtext,status); 
02637         } 
02638         return(status);
02639 }
02640 
02641 /* --- Doxygen Function Comment -------------------------- */
02650 /* ------------------------------------------------------- */
02651 
02652 int 
02653 get_E3D_slice(E3D_file *frame, SLICE *signal, SLICE *noise, int index)
02654 {
02655         int status=0;
02656         char errtext[132];
02657 
02658         if (! has_common_bounds(frame)) {
02659                 sprintf(errtext,"get_E3D_slice : Unconsistent data. No common bounds");
02660                 Handle_Error(errtext,ERR_IMA_BOUND); 
02661                 return(ERR_IMA_BOUND); 
02662         }
02663 
02664         if ((signal != NULL) && (frame->signal != NULL)) {
02665                 status = extract_E3D_slice(frame,signal,&(frame->signal),index);
02666                 if (status) return(status);
02667         } 
02668         if ((noise != NULL) && (frame->noise != NULL)) {
02669                 status = extract_E3D_slice(frame,noise,&(frame->noise),index);
02670         } 
02671 
02672         return(status);
02673 }
02674 
02675 #define extract_E3D_slice_noalloc(type_of_slice) \
02676         type_of_slice->index = index; \
02677         current_spec = &(frame->type_of_slice[0]); \
02678         for (i=0;i <frame->nbspec; i++, current_spec++) { \
02679                 type_of_slice->specId[i] = current_spec->specId; \
02680                 if (index < current_spec->npix) { \
02681                         lseek(frame->imno,(current_spec->data_offset \
02682                                 +index*sizeof_item(type_of_slice->data_type)),SEEK_SET); \
02683                         if (frame->swapbytes) {\
02684                                 read_DOS(frame->imno,(char *)((char *)(type_of_slice->data.s_data)+\
02685                                         i*sizeof_item(type_of_slice->data_type)), \
02686                                         sizeof_item(type_of_slice->data_type)); \
02687                         } else { \
02688                                 read(frame->imno,(char *)((char *)(type_of_slice->data.s_data)+\
02689                                         i*sizeof_item(type_of_slice->data_type)), \
02690                                         sizeof_item(type_of_slice->data_type)); \
02691                         } \
02692                 }\
02693                 else \
02694                         WR_slice(type_of_slice,i,DATA_UNDEF); \
02695         }  
02696 
02697 int save_E3D_slice(E3D_file *frame, SLICE *slice, E3Dspec_desc **type_of_slice, int index) {
02698 
02699         char errtext[132];
02700         int status=0, len, specId;
02701         E3Dspec_desc *current_spec;
02702         fitsfile *fptr;
02703         int coli, coldq, tformat, i, j;
02704         char format[132], colname[17];
02705         double dval;
02706         SPECTRUM spec;
02707 
02708         slice->index = index;
02709         if (frame->nbspec == 0 || *type_of_slice == NULL) {     /* create first slice */ 
02710                 len = (int)(frame->common_parameters[2]); 
02711                 for (i=0; i<slice->npts;i++) { 
02712                         specId = slice->specId[i];
02713                         init_new_E3D_spec(frame,&spec, len,
02714                                 frame->common_parameters[0]);
02715                         for (j=0; j<len; j++) 
02716                                 WR_spec(&spec,j,DATA_UNDEF); 
02717                         if (*type_of_slice == frame->signal) 
02718                                 put_E3D_spec(frame,&spec,NULL,specId); 
02719                         else 
02720                                 put_E3D_spec(frame,NULL,&spec,specId);
02721                 } 
02722         }; 
02723 
02724         switch (frame->data_format) {
02725         case TIGER_FORMAT :
02726             for (i=0; i<frame->nbspec;i++) {
02727                 specId = slice->specId[i]; 
02728                 if (*type_of_slice == frame->signal) {
02729                         find_lens(frame,signal,specId,&j); 
02730                 }
02731                 else {
02732                         find_lens(frame,noise,specId,&j); 
02733                 }
02734                 if (j<0) { 
02735                         status = ERR_NODATA; 
02736                         break; 
02737                 } 
02738                 current_spec = *type_of_slice+j;
02739                 dval = (double)RD_slice(slice,i);
02740                 lseek(frame->imno,(current_spec->data_offset
02741                         +index*sizeof_item(slice->data_type)),SEEK_SET); 
02742                 if (frame->swapbytes) { 
02743                         write_DOS(frame->imno,(char *)((char *)(slice->data.s_data)+
02744                                 i*sizeof_item(slice->data_type)), 
02745                                 sizeof_item(slice->data_type)); 
02746                 } else { 
02747                         write(frame->imno,(char *)((char *)(slice->data.s_data)+
02748                                 i*sizeof_item(slice->data_type)), 
02749                                 sizeof_item(slice->data_type)); 
02750                 } 
02751              } 
02752              break;
02753 
02754         case EURO3D_FORMAT :
02755                 status = 0;
02756                 fptr = (fitsfile *)frame->external_info;
02757 
02758                 fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
02759 
02760                 if (status) { 
02761                         status = 0;
02762                         fits_movabs_hdu(fptr, 1, NULL, &status);
02763                         fits_movnam_hdu(fptr,BINARY_TBL,E3D_DATA,0,&status);
02764                 }
02765 
02766                 if (*type_of_slice == frame->signal) 
02767                         strcpy(colname,E3D_COL_INT);
02768                 else 
02769                         strcpy(colname,E3D_COL_RMS);
02770 
02771                 switch(slice->data_type) {
02772                         case SHORT :
02773                                 sprintf(format,"%dI",len);
02774                                 tformat = TSHORT;
02775                                 break;
02776                         case INT :
02777                         case LONG :
02778                                 sprintf(format,"%dJ",len);
02779                                 tformat = TLONG;
02780                                 break;
02781                         case FLOAT :
02782                                 sprintf(format,"%dE",len);
02783                                 tformat = TFLOAT;
02784                                 break;
02785                         case DOUBLE :
02786                                 sprintf(format,"%dD",len);
02787                                 tformat = TDOUBLE;
02788                                 break;
02789                 }
02790                 fits_get_colnum(fptr,0,colname,&coli,&status);
02791                 if (status) {
02792                         status = 0;
02793                         fits_get_num_cols(fptr, &coli, &status);
02794                         coli++;
02795                         fits_insert_col(fptr,coli,colname,format,&status);
02796                         if (status) {
02797                                 status = ERR_WRIT;
02798                                 break;
02799                         }
02800                 }
02801                 fits_get_colnum(fptr,0,E3D_COL_DQ,&coldq,&status);
02802                 if (status) {
02803                         status = 0;
02804                         sprintf(format,"%dJ",len);
02805                         fits_get_num_cols(fptr, &coldq, &status);
02806                         coldq++;
02807                         fits_insert_col(fptr,coldq,E3D_COL_DQ,format,&status);
02808                         if (status) {
02809                                 status = ERR_WRIT;
02810                                 break;
02811                         }
02812                 }
02813                 for (i=0; i<frame->nbspec;i++) {
02814                         specId = slice->specId[i]; 
02815                         if (*type_of_slice == frame->signal) {
02816                                 find_lens(frame,signal,specId,&j); 
02817                         }
02818                         else {
02819                                 find_lens(frame,noise,specId,&j); 
02820                         }
02821                         if (j<0 || j>= frame->nbspec) { 
02822                                 status = ERR_NODATA; 
02823                                 break; 
02824                         } 
02825                         current_spec = *type_of_slice+j;
02826 
02827                         /* write signal or noise */
02828                 
02829                         switch(slice->data_type) {
02830                         case SHORT :
02831                                 fits_write_col_sht(fptr,coli,i+1,index+1,1,slice->data.s_data+i,&status);
02832                                 break;
02833                         case INT :
02834                         case LONG :
02835                                 fits_write_col_lng(fptr,coli,i+1,index+1,1,slice->data.l_data+i,&status);
02836                                 break;
02837                         case FLOAT :
02838                                 fits_write_col_flt(fptr,coli,i+1,index+1,1,slice->data.f_data+i,&status);
02839                                 break;
02840                         case DOUBLE :
02841                                 fits_write_col_dbl(fptr,coli,i+1,index+1,1,slice->data.d_data+i,&status);
02842                                 break;
02843                         }
02844                         if (status) {
02845                                 status = ERR_WRIT;
02846                                 break;
02847                         }
02848                         fits_write_col_lng(fptr,coldq,i+1,index+1,1,slice->quality+i,&status);
02849                 }
02850                 break;
02851 
02852                 default :
02853                 status = ERR_FORMAT;
02854                 break;
02855         }
02856         free_slice_mem(slice);
02857  
02858         if (status) {
02859                 sprintf(errtext,"put_E3D_slice: slice %d",slice->index);
02860                 Handle_Error(errtext,status); 
02861         }
02862         return(status);
02863 }
02864 
02865 /* --- Doxygen Function Comment -------------------------- */
02874 /* ------------------------------------------------------- */
02875 
02876 int 
02877 put_E3D_slice(E3D_file *frame, SLICE *signal, SLICE *noise, int index)
02878 {
02879         char errtext[132];
02880         int status=0;
02881 
02882         if (frame->iomode == (int)I_MODE) {
02883                 sprintf(errtext,"put_E3D_slice: file %s is write protected",
02884                         frame->name);
02885                 Handle_Error(errtext,ERR_ACCESS); 
02886                 return(ERR_ACCESS); 
02887         } 
02888         if (! has_common_bounds(frame)) {
02889                 sprintf(errtext,"put_E3D_slice : Unconsistent data");
02890                 Handle_Error(errtext,ERR_BAD_PARAM); 
02891                 return(ERR_BAD_PARAM); 
02892         }
02893 
02894         if (signal != NULL) {
02895                 status = save_E3D_slice(frame,signal,&(frame->signal),index);
02896                 if (status) return(status);
02897         } 
02898         if (noise != NULL) {
02899                 status = save_E3D_slice(frame,noise,&(frame->noise),index);
02900         }
02901 
02902         return(status);
02903 }

Generated on Tue Jul 1 17:35:44 2003 for IFU_C_iolibs by doxygen1.3