Main Page   Modules   Data Structures   File List   Data Fields   Globals  

C_libs/io/std_io/iolib.c

Go to the documentation of this file.
00001 /* === Doxygen File Comment ============================== */
00011 /* ======================================================= */
00012 
00013 #include <sys/types.h>
00014 #include <sys/times.h>
00015 
00016 #include <IFU_io.h>
00017 #include <signal.h>
00018 #include <string.h>
00019 #include <time.h>
00020 #include <errno.h>
00021 
00022 /* include specific codes for I/O */
00023 
00024 #include <data_io.h>
00025 
00026 #ifdef MIDAS
00027 #include "../midas_io/midas_defs.h"
00028 #include "../midas_io/tbldef.h"
00029 #include "../midas_io/proto_st.h"
00030 #include "../midas_io/proto_tb.h"
00031 #endif
00032 
00033 #ifdef FITS
00034 #include "../cfitsio/fitsio.h"
00035 #include "../cfitsio/longnam.h"
00036 #endif
00037 
00038 #ifdef IRAF
00039 #include "../iraf_io/incl/iraf_def.h"
00040 #endif
00041 
00042 #define STOP_PROG 10000
00043 
00044 extern IO_Format InputIO, OutputIO;
00045 
00046 /* --- Doxygen Function Comment -------------------------- */
00050                 /* @{ ***** Starting group24 ***** */
00062 /* ------------------------------------------------------- */
00063 
00064 /* --- Doxygen Function Comment -------------------------- */
00071                 /* @{ ***** Starting group25 ***** */
00077 /* ------------------------------------------------------- */
00078 
00079 void sig_segm(int nb)
00080 {
00081   char text[132];
00082   if(TK)
00083     {
00084       printf("@ K\n");
00085       fflush(stdout);
00086     }
00087   sprintf(text,"Memory fault detected, signal %d",nb);
00088   print_error(text);
00089   exit(-1);
00090 }
00091 
00092 void sig_usr1(int nb)
00093 {
00094   ASK_BACK = 1;
00095 }
00096 
00097 void sig_usr2(int nb)
00098 {
00099   ASK_BACK = 0;
00100 }
00101 
00102 void do_nothing(int nb)
00103 {
00104 
00105 }
00106 
00107 void confirme_erase(char *name)
00108 {
00109   if(exist(name))
00110     {
00111       if(TK)
00112         {
00113           signal(SIGUSR1,sig_usr1);
00114           signal(SIGUSR2,sig_usr2);
00115 
00116           printf("@ Q \"%s\"\n",name);
00117           fflush(stdout);
00118           pause();
00119           signal(SIGUSR1,do_nothing);
00120           signal(SIGUSR2,do_nothing);
00121           if(!ASK_BACK)
00122             {
00123               print_error("Aborting current program");
00124               exit_session(-1);
00125             }
00126           else
00127             {
00128               printf("Overwriting file %s\n",name);
00129             }
00130         }
00131       else
00132         {
00133           char back;
00134 
00135           printf("ERROR: %s already exists...\n",name);
00136           printf("Do you really want to overwrite it (Y/N)? ");
00137           back=getchar();
00138 
00139           if((back=='N')||(back=='n'))
00140              {
00141                print_error("Aborting current program");
00142                exit_session(-1);
00143              }
00144           else
00145             {
00146               printf("Overwriting file %s\n",name);
00147             }
00148         }
00149     }
00150 }
00151 
00152 
00153 char Calling_Prog[lg_name+1] = "program";
00154 char Cmd_History[lg_hist+1] = "";
00155 extern int List_Length;
00156 extern char **pt_ArgLabels;
00157 char soft_version[10] = "0.0";  /* non initialized value */
00158 int parse_arg(char **, int, char ***, char ***);
00159 
00160 /* --- Doxygen Function Comment -------------------------- */
00167                 /* @{ ***** Starting group26 ***** */
00176 /* ------------------------------------------------------- */
00177 
00178 void 
00179 init_session(char **argv, int argc, char ***arg_labels, char ***arg_values)
00180 {
00181         char errtext[256], *pt_filename, debug_file[81], strtime[25], **pt_arg;
00182         int status, i;
00183         time_t elapsed;
00184 
00185         signal(SIGSEGV,sig_segm); /* for detecting segmentation fault) */
00186 
00187         if ((pt_filename = strrchr(argv[0],'/')) != NULL)
00188                 strcpy(Calling_Prog,pt_filename+1);
00189         else
00190                 strcpy(Calling_Prog,argv[0]);
00191 
00192         sprintf(debug_file,"%s.dbg",Calling_Prog);
00193 
00194         set_user_dataformat();
00195 
00196         status = parse_arg(argv,argc,arg_labels,arg_values);
00197         if (status < 0) {
00198         Handle_Error("init_session", status);
00199                 exit_session(status);
00200         }
00201         time(&elapsed);
00202         strcpy(strtime,(char *)asctime(localtime(&elapsed)));
00203         strtime[24]='\0';
00204         sprintf(Cmd_History,"%s: %s",strtime,Calling_Prog);
00205         pt_arg = *arg_values;
00206         for (i=0; i< List_Length; i++) {        
00207                         /* save command for history desc. */
00208                 strcat(Cmd_History," ");
00209                 strcat(Cmd_History,pt_ArgLabels[i]);
00210                 strcat(Cmd_History," ");
00211                 strcat(Cmd_History,pt_arg[i]);
00212         }
00213 
00214         if (DEBUG) {
00215                 freopen(debug_file,"w",stderr);
00216                 if (stderr == NULL)  
00217                         exit_session(-1);
00218                 else {
00219                         fprintf(stderr, "Date    : %s\n", strtime);
00220                         fprintf(stderr, "Path    : %s\n", getenv("PWD"));
00221                         fprintf(stderr, "Command : %s\n", Calling_Prog);
00222                         for (i=0; i<List_Length; i++)
00223                                 fprintf(stderr,"%s %s\n",pt_ArgLabels[i],pt_arg[i]);
00224                         fprintf(stderr,"\n");
00225                 }
00226         }
00227         sprintf(errtext,
00228                 "Program: %s  Version %s-%s", Calling_Prog,soft_version,LIBRARY_VERSION);
00229 
00230         if (VERBOSE) print_msg(errtext);
00231 
00232 #ifdef MIDAS
00233         if ((InputIO.basic_io == MIDAS_FORMAT) || (OutputIO.basic_io == MIDAS_FORMAT))
00234                 SCSPRO("-");
00235 #endif
00236 
00237 #ifdef IRAF
00238         if ((InputIO.basic_io == IRAF_FORMAT) || (OutputIO.basic_io == IRAF_FORMAT)
00239         || (InputIO.basic_io == STSDAS_FORMAT) || (OutputIO.basic_io == STSDAS_FORMAT))
00240                 irafmn();
00241 #endif
00242 
00243         set_control_level(0);                   /* default value */
00244 
00245         if(TK) printf("@ I %d\n",getpid());
00246 }
00247 
00248 /* --- Doxygen Function Comment -------------------------- */
00254 /* ------------------------------------------------------- */
00255 void
00256 exit_session(int status)
00257 {
00258         char errtext[256];
00259 
00260         switch (status) {
00261 
00262         case 0 :                                                                        /* everything is OK */
00263                 sprintf(errtext,"End of %s",Calling_Prog);
00264                 if(TK) printf("@ E\n");
00265                 break;
00266         case STOP_PROG :
00267                 sprintf(errtext,"%s stopped by user",Calling_Prog);
00268                 if(TK) printf("@ S\n");
00269                 break;
00270         default :
00271                 if (errno == ENOSPC)
00272                         sprintf(errtext,"%s aborted - disk is full",Calling_Prog);
00273                 else
00274                         sprintf(errtext,"%s aborted - status code : %d",Calling_Prog,status);
00275                 if(TK) printf("@ A\n");
00276                 break;
00277         }
00278         if (VERBOSE) print_msg(errtext);
00279         if (DEBUG) {
00280                 fclose(stderr);
00281         }
00282 
00283 #ifdef MIDAS
00284         if ((InputIO.basic_io == MIDAS_FORMAT) || (OutputIO.basic_io == MIDAS_FORMAT)) {
00285                 SCSEPI();
00286         }
00287 #endif
00288         exit(status);
00289 }
00290 
00291 /* --- Doxygen Function Comment -------------------------- */
00296 /* ------------------------------------------------------- */
00297 
00298 void 
00299 stop_by_user()
00300 {
00301         exit_session(STOP_PROG);
00302 }
00303 
00304 /* --- Doxygen Function Comment -------------------------- */
00311                 /* @{ ***** Starting group27 ***** */
00319 /* ------------------------------------------------------- */
00320 
00321 int get_descr_type(void *anyfile, char *descr, short *type) 
00322 {
00323         Anyfile *pt_file;
00324         int stat=0, i;
00325         int nbelem, bytelem;
00326         Descr_Items *dsc_items;
00327     char errtext[132];
00328         char data_type[2];
00329 
00330 #ifdef FITS
00331         fitsfile *fptr;
00332         char buffer[4097];
00333         char dtype;
00334         char *pt_buf;
00335         int n;
00336 #endif
00337 
00338         pt_file = (Anyfile *)anyfile;
00339     switch (pt_file->data_format) {
00340 #ifdef MIDAS
00341     case MIDAS_FORMAT :
00342                 stat = SCDFND(pt_file->imno,descr,data_type,&nbelem,&bytelem);
00343                 if (stat) break;
00344                 switch (data_type[0]) {
00345                         case 'C' : *type = CHAR;
00346                                 break;
00347                         case 'I' : *type = INT;
00348                                 break;
00349                         case 'R' : *type = FLOAT;
00350                                 break;
00351                         case 'D' : *type = DOUBLE;
00352                                 break;
00353                         default : stat = -1;
00354                                 break;
00355                 }
00356                 break;
00357 #endif
00358 #ifdef IRAF
00359     case IRAF_FORMAT :
00360     case STSDAS_FORMAT :
00361                 len_descr = strlen(descr);
00362 
00363                 uhdgtp(&(pt_file->imno),descr,&datatype,&stat,len_descr);
00364 
00365                 *type = decode_datatype(IRAF_FORMAT,(short)datatype);
00366                 break;
00367 #endif
00368 #ifdef FITS
00369     case FITS_A_FORMAT :
00370     case FITS_B_FORMAT :
00371                 stat = 0;
00372                 fptr = (fitsfile *)pt_file->external_info;
00373                 if  ((strcmp(descr,"COMMENT") == 0)
00374                 ||  (strcmp(descr,"HISTORY") == 0)) {
00375                         fits_read_key_longstr(fptr, descr, &pt_buf, NULL, &stat);
00376                         if (stat) break;
00377                         n = MIN(strlen(pt_buf),4096);
00378                         strncpy(buffer,pt_buf,n);
00379                         buffer[n] = 0;
00380                         dtype = 'C';
00381                         free(pt_buf);
00382                 }
00383                 else {
00384                         fits_read_keyword(fptr, descr, buffer, NULL, &stat);
00385                         nbelem = 1;
00386                         fits_get_keytype(buffer,&dtype,&stat);
00387                 }
00388                 if (stat == 0) {
00389                         switch(dtype)
00390                         {
00391                         case 'L' : /* logical */
00392                         case 'C' : /* string */
00393                                 *type = CHAR;
00394                                 nbelem = strlen(buffer);
00395                                 if (buffer[0] == '\'') nbelem -= 1;
00396                                 if (buffer[strlen(buffer)-1] == '\'') nbelem -= 1;
00397                                 if (buffer[0] == '=') nbelem -= 2;
00398                                 break;
00399                         case 'I' : /* integer */
00400                                 *type = INT;
00401                                 break;
00402                         case 'F' : /* float */
00403                                 *type = FLOAT;
00404                                 break;
00405                         }
00406                 }
00407                 break;
00408 
00409 #endif
00410     case TIGER_FORMAT :
00411                 stat = 0;
00412                 if (pt_file->external_info == 0) {
00413                         stat = -1;
00414                         break;
00415                 }
00416                 dsc_items = (Descr_Items *)(pt_file->external_info);
00417                 for (i=0; i<dsc_items->nb_descr
00418                         && strcmp(dsc_items->descr_list[i].descr_name,descr) != 0 ;i++);
00419                 if (i == dsc_items->nb_descr) {
00420                                 stat = ERR_NODESC;
00421                         break;
00422                 }
00423                 *type = dsc_items->descr_list[i].data_type;
00424                 nbelem = dsc_items->descr_list[i].nb_values;
00425         break;
00426         }
00427     if (stat){
00428         sprintf(errtext, "get_descr_type: file= %s desc=%s", pt_file->name,descr);
00429                 stat = get_tiger_errcode(pt_file->data_format,stat);
00430         Handle_Error(errtext, stat);
00431         }
00432 
00433         if(stat<0)
00434                 return(stat);
00435         else
00436                 return(nbelem);
00437 }
00438 
00439 int 
00440 Read_one_desc(void *anyfile, char *descr, short type, int nb_elt, void *val)
00441 {
00442     int stat, nb_val=nb_elt, nbread, i;
00443     int l;
00444     int nulls;
00445     float *pt_float;
00446     double *pt_double;
00447     char errtext[132];
00448     Anyfile *pt_file;
00449     Descr_Items *dsc_items;
00450     int unit;
00451 #ifdef FITS
00452     fitsfile *fptr;
00453     char buffer[4097];
00454         char *pt_buf;
00455     int int_datatype;
00456     int n;
00457 #endif
00458 
00459 
00460         pt_file = (Anyfile *)anyfile;
00461     switch (pt_file->data_format) {
00462 #ifdef MIDAS
00463     case MIDAS_FORMAT :
00464         switch (type) {
00465                 case CHAR :
00466             stat = SCDRDC(pt_file->imno,descr,1L,1L,nb_val,&nbread,
00467                                 (char *)val,&unit, &nulls);
00468                         if (! stat) *((char *)val+nbread) = '\0';
00469             break;
00470         case SHORT :
00471         case INT :
00472         case LONG :
00473             stat = SCDRDI(pt_file->imno,descr,1L,nb_val,&nbread,
00474                                         (int *)val,&unit, &nulls);
00475             break;
00476         case FLOAT :
00477             stat = SCDRDR(pt_file->imno,descr,1L,nb_val,&nbread,
00478                                         (float *)val,&unit, &nulls);
00479             break;
00480         case DOUBLE :
00481             stat = SCDRDD(pt_file->imno,descr,1L,nb_val,&nbread,
00482                                         (double *)val,&unit, &nulls);
00483             break;
00484         }
00485         break;
00486 #endif
00487 #ifdef IRAF
00488     case IRAF_FORMAT :
00489     case STSDAS_FORMAT :
00490                 len_descr = strlen(descr);
00491                                                                 /* check for table descriptors */
00492                 if (pt_file->file_type != TBL_TYPE) {
00493 
00494                 switch (type) {         /* frame descriptor */
00495                         case CHAR :
00496                         uhdgvt(&(pt_file->imno),descr,&one,&one,&nbread,val,
00497                                         &stat,len_descr,nb_val);
00498                                 break;
00499                 case SHORT :
00500                         uhdgvs(&(pt_file->imno),descr,&one,&nb_val,&nbread,val,
00501                                         &stat,len_descr);
00502                                 break;
00503                 case INT :
00504                         uhdgvi(&(pt_file->imno),descr,&one,&nb_val,&nbread,val,
00505                                         &stat,len_descr);
00506                                 break;
00507                 case LONG :
00508                         uhdgvl(&(pt_file->imno),descr,&one,&nb_val,&nbread,val,
00509                                         &stat,len_descr);
00510                                 break;
00511                 case FLOAT :
00512                         uhdgvr(&(pt_file->imno),descr,&one,&nb_val,&nbread,val,
00513                                         &stat,len_descr);
00514                         break;
00515                 case DOUBLE :
00516                         uhdgvd(&(pt_file->imno),descr,&one,&nb_val,&nbread,val,
00517                                         &stat,len_descr);
00518                         break;
00519                         }
00520                 }
00521                 else {
00522                 switch (type) {         /* table descriptor */
00523                         case CHAR :
00524                         uthgtt(&(pt_file->imno),descr,val,&stat,len_descr,nb_val);
00525                                 break;
00526                 case SHORT :
00527                 case LONG :
00528                 case INT :
00529                         uthgti(&(pt_file->imno),descr,val,&stat,len_descr);
00530                                 break;
00531                 case FLOAT :
00532                         uthgtr(&(pt_file->imno),descr,val,&stat,len_descr);
00533                         break;
00534                 case DOUBLE :
00535                         uthgtd(&(pt_file->imno),descr,val,&stat,len_descr);
00536                         break;
00537                         }
00538                 }
00539                 break;
00540 #endif
00541 #ifdef FITS
00542     case FITS_A_FORMAT :
00543     case FITS_B_FORMAT :
00544     case EURO3D_FORMAT :
00545                 fptr = (fitsfile *)pt_file->external_info;
00546                 stat = 0;
00547                 nbread = nb_val;
00548                 if (nbread == 0)
00549                         break;
00550                 if (type == CHAR) {
00551                         *((char *)val) = 0;
00552                         int_datatype = get_datatype_code(pt_file->data_format,type);
00553                         if  ((strcmp(descr,"COMMENT") == 0) 
00554                         ||  (strcmp(descr,"HISTORY") == 0)) {
00555                                 fits_read_key_longstr(fptr, descr, &pt_buf, NULL, &stat);
00556                                 if (stat) break;
00557                                 n = MIN(strlen(pt_buf),nb_val);
00558                                 strncpy((char *)val,pt_buf,n);
00559                                 *((char *)val + n) = 0;
00560                             free(pt_buf);
00561                         }
00562                         else {
00563                         fits_read_key(fptr, int_datatype, descr, buffer, NULL, &stat);
00564                                 if (stat) break;
00565                                 if (strlen(buffer) < nb_val)
00566                                         strcpy((char *)val,buffer);
00567                                 else {
00568                                         strncpy((char *)val,buffer,nb_val);
00569                                         *((char *)val + nb_val) = 0;
00570                                 }
00571                         }
00572                         break;
00573                 }
00574                 if (nb_val == 1) {
00575                         int_datatype = get_datatype_code(pt_file->data_format,type);
00576                         fits_read_key(fptr, int_datatype, descr, val, NULL, &stat);
00577                         break;
00578                 }
00579                 switch (type) {
00580                 case CHAR :
00581                     fits_read_keys_str(fptr,descr,1,nb_val,val,&nbread,&stat);
00582                     break;
00583                 case SHORT :
00584                 case LONG :
00585                 case INT :
00586                     fits_read_keys_lng(fptr,descr,1,nb_val,val,&nbread,&stat);
00587                     break;
00588                 case FLOAT :
00589                     fits_read_keys_flt(fptr,descr,1,nb_val,val,&nbread,&stat);
00590                     break;
00591                 case DOUBLE :
00592                     fits_read_keys_dbl(fptr,descr,1,nb_val,val,&nbread,&stat);
00593                     break;
00594                 }
00595                 break;
00596 #endif
00597     case TIGER_FORMAT :
00598                 stat = 0;
00599                 if (pt_file->external_info == 0) {
00600                         stat = -1;
00601                         break;
00602                 }
00603                 dsc_items = (Descr_Items *)(pt_file->external_info);
00604                 for (i=0; i<dsc_items->nb_descr
00605                         && strcmp(dsc_items->descr_list[i].descr_name,descr) != 0 ;i++);
00606                 if (i == dsc_items->nb_descr) {
00607                                 stat = ERR_NODESC;
00608                         break;
00609                 }
00610                 if ((dsc_items->descr_list[i].data_type == LONG) 
00611                         && (type == INT)) 
00612                         dsc_items->descr_list[i].data_type = type;
00613                 if ((dsc_items->descr_list[i].data_type == INT) 
00614                         && (type == LONG)) 
00615                         dsc_items->descr_list[i].data_type = type;
00616 
00617                 nb_val = MIN(nb_val,dsc_items->descr_list[i].nb_values);
00618                 nbread = nb_val;
00619 
00620                 if (dsc_items->descr_list[i].data_type != type) {
00621                         if ((dsc_items->descr_list[i].data_type == DOUBLE) 
00622                                 && (type == FLOAT)) {
00623                                 pt_float = (float*)val;
00624                                 for (l=0;l<nb_val;l++)
00625                                     pt_float[l] = 
00626                                         (float)dsc_items->descr_list[i].descr_value.d_data[l];
00627                         }
00628                         else {
00629                                 if ((dsc_items->descr_list[i].data_type == FLOAT) 
00630                                 && (type == DOUBLE)) {
00631                                         pt_double = (double*)val;
00632                                         for (l=0;l<nb_val;l++)
00633                                             pt_double[l] = 
00634                                                 (double)dsc_items->descr_list[i].descr_value.f_data[l];
00635                                 }
00636                                 else 
00637                                         stat = ERR_BAD_DESC;
00638                         }
00639                 } 
00640                 else {
00641                         memcpy(val,dsc_items->descr_list[i].descr_value.c_data,
00642                                         nb_val*sizeof_item(type));
00643                         if (type == CHAR)
00644                           *((char *)val+nb_val) = 0;
00645                 }
00646         break;
00647     }
00648     if (stat){
00649         sprintf(errtext, "RD_desc : file= %s desc=%s", pt_file->name,descr);
00650                 stat = get_tiger_errcode(pt_file->data_format,stat);
00651         Handle_Error(errtext, stat);
00652         }
00653         if (stat < 0)
00654                 return(stat);
00655         else
00656                 return(nbread);
00657 }
00658 
00659 /* --- Doxygen Function Comment -------------------------- */
00669 /* ------------------------------------------------------- */
00670 
00671 int 
00672 RD_desc(void *anyfile, char *descr, short type, int nb_elt, void *val) 
00673 {
00674     short ltype = type;
00675     int nb = nb_elt;
00676     int status;
00677     char *item, *list_of_desc;
00678     char errtext[132];
00679     Anyfile *pt_file;
00680 
00681     list_of_desc = (char *)malloc((strlen(descr)+1)*sizeof(char));
00682     strcpy(list_of_desc,descr);
00683 
00684     item = strtok(list_of_desc,"|");
00685         
00686     disable_user_warnings();
00687 
00688     status = Read_one_desc(anyfile, item, ltype, nb, val);
00689 
00690     while ((item != NULL) && (status < 0)) {
00691         item = strtok(NULL,"|");
00692         if (item != NULL) {
00693                 status = Read_one_desc(anyfile, item, ltype, nb, val);
00694         }
00695     }
00696 
00697     free(list_of_desc);
00698 
00699     restore_user_warnings();
00700 
00701     if (status < 0 ){
00702         pt_file = (Anyfile *)anyfile;
00703         sprintf(errtext, "RD_desc : file= %s desc=%s", pt_file->name,descr);
00704         Handle_Error(errtext, ERR_BAD_DESC);
00705     }
00706     return(status);
00707 }
00708 
00709 /* --- Doxygen Function Comment -------------------------- */
00719 /* ------------------------------------------------------- */
00720 
00721 int 
00722 WR_desc(void *anyfile, char *descr, short type ,int nb_elt, void *val)
00723 {
00724     int stat=0, nb_val=nb_elt, i;
00725     char errtext[132];
00726     int unit;
00727     char *descup;
00728     char **pt_val;
00729     Anyfile *pt_file;
00730     Descr_Items *dsc_items;
00731 
00732 #ifdef IRAF
00733     int one=1, int_type;
00734     char iraf_unit[lg_unit+1];
00735     int len_descr, len_unit, len_val;
00736 #endif
00737 
00738 #ifdef FITS
00739     fitsfile *fptr;
00740     char **comm;
00741     char keyname[FLEN_KEYWORD+1];
00742     char card[FLEN_CARD+1];
00743     char buffer[4097];
00744 #endif
00745 
00746     pt_file = (Anyfile *)anyfile;
00747 
00748     if (strlen(descr) > 8) {
00749         stat = ERR_BAD_DESC;
00750         sprintf(errtext, "WR_desc : file= %s desc=%s", pt_file->name,descr);
00751         Handle_Error(errtext, stat);
00752         return(stat);
00753     }
00754 
00755     switch (pt_file->data_format) {
00756 #ifdef MIDAS
00757     case MIDAS_FORMAT :
00758         switch (type) {
00759         case CHAR :
00760             stat = SCDWRC(pt_file->imno,descr,1,(char *)val,1,nb_val,&unit);
00761             break;
00762         case SHORT :
00763         case INT :
00764         case LONG :
00765             stat = SCDWRI(pt_file->imno,descr,(int *)val,1L,nb_val,&unit);
00766             break;
00767         case FLOAT :
00768             stat = SCDWRR(pt_file->imno,descr,(float *)val,1L,nb_val,&unit);
00769             break;
00770         case DOUBLE :
00771             stat = SCDWRD(pt_file->imno,descr,(double *)val,1L,nb_val,&unit);
00772             break;
00773         }
00774         break;
00775 #endif
00776 #ifdef IRAF
00777     case IRAF_FORMAT :
00778     case STSDAS_FORMAT :
00779                 len_descr = strlen(descr);
00780         int_type = 0;
00781                                                                 /* check for tables descriptors */
00782 
00783                 if (pt_file->file_type != TBL_TYPE) {
00784                         strcpy(iraf_unit,"None given");
00785                         len_unit = strlen(iraf_unit);
00786 
00787                 switch (type) {         /* frames */
00788                 case CHAR :
00789                                 len_val = strlen(val);
00790                         uhdpvt(&(pt_file->imno),descr,&one,&one,val,&stat,
00791                                         len_descr,len_val);
00792                         if (stat != 0) {
00793                         uhdavt(&(pt_file->imno),descr,&one,&one,val,
00794                                                 iraf_unit,&int_type,&stat,len_descr,len_val,len_unit);
00795                                 }
00796                         break;
00797                 case SHORT :
00798                         uhdpvs(&(pt_file->imno),descr,&one,&nb_val,val,&stat,len_descr);
00799                         if (stat != 0) {
00800                                 int_type = get_datatype_code(pt_file->data_format,type);
00801                                 uhdavs(&(pt_file->imno),descr,&one,&nb_val,val,
00802                                                 iraf_unit,&int_type,&stat,len_descr,len_unit);
00803                                 }
00804                         break;
00805                 case INT :
00806                         uhdpvi(&(pt_file->imno),descr,&one,&nb_val,val,&stat,len_descr);
00807                         if (stat != 0) {
00808                                 int_type = get_datatype_code(pt_file->data_format,type);
00809                                 uhdavi(&(pt_file->imno),descr,&one,&nb_val,val,
00810                                                 iraf_unit,&int_type,&stat,len_descr,len_unit);
00811                                 }
00812                         break;
00813                 case LONG :
00814                         uhdpvl(&(pt_file->imno),descr,&one,&nb_val,val,&stat,len_descr);
00815                         if (stat != 0) {
00816                                 int_type = get_datatype_code(pt_file->data_format,type);
00817                                 uhdavl(&(pt_file->imno),descr,&one,&nb_val,val,
00818                                                 iraf_unit,&int_type,&stat,len_descr,len_unit);
00819                                 }
00820                         break;
00821                 case FLOAT :
00822                         uhdpvr(&(pt_file->imno),descr,&one,&nb_val,val,&stat,len_descr);
00823                         if (stat != 0) {
00824                         int_type = get_datatype_code(pt_file->data_format,type);
00825                         uhdavr(&(pt_file->imno),descr,&one,&nb_val,val,
00826                                                 iraf_unit,&int_type,&stat,len_descr,len_unit);
00827                                 }
00828                         break;
00829                 case DOUBLE :
00830                         uhdpvd(&(pt_file->imno),descr,&one,&nb_val,val,&stat,len_descr);
00831                         if (stat != 0) {
00832                         int_type = get_datatype_code(pt_file->data_format,type);
00833                         uhdavd(&(pt_file->imno),descr,&one,&nb_val,val,
00834                                                 iraf_unit,&int_type,&stat,len_descr,len_unit);
00835                                 }
00836                         break;
00837                         }
00838                 }
00839                 else {
00840                 switch (type) {         /* tables */
00841                 case CHAR :
00842                                 len_val = strlen(val);
00843                         uthptt(&(pt_file->imno),descr,val,&stat,len_descr,
00844                                         len_val);
00845                         if (stat != 0)
00846                                 uthadt(&(pt_file->imno),descr,val,&stat,len_descr,
00847                                                 len_val);
00848                         break;
00849                 case SHORT :
00850                 case INT :
00851                 case LONG :
00852                         uthpti(&(pt_file->imno),descr,val,&stat,len_descr);
00853                         if (stat != 0) 
00854                                 uthadi(&(pt_file->imno),descr,val,&stat,len_descr);
00855                         break;
00856                 case FLOAT :
00857                         uthptr(&(pt_file->imno),descr,val,&stat,len_descr);
00858                         if (stat != 0) 
00859                                 uthadr(&(pt_file->imno),descr,val,&stat,len_descr);
00860                         break;
00861                 case DOUBLE :
00862                         uthptd(&(pt_file->imno),descr,val,&stat,len_descr);
00863                         if (stat != 0) 
00864                                 uthadd(&(pt_file->imno),descr,val,&stat,len_descr);
00865                         break;
00866                         }
00867                 }
00868                 break;
00869 #endif
00870 #ifdef FITS
00871     case FITS_A_FORMAT :
00872     case FITS_B_FORMAT :
00873     case EURO3D_FORMAT :
00874 
00875                 fptr = (fitsfile *)pt_file->external_info;
00876                 stat = 0; /* check if key already exists */
00877                 strncpy(keyname,descr,8);
00878                 keyname[8] = 0;
00879                 if ((nb_val == 1) || (type == CHAR)) {
00880                         comm = (char **)malloc(sizeof(char *)); 
00881                         comm[0] = (char *)malloc((FLEN_COMMENT+1)*sizeof(char));
00882                         strcpy(comm[0], "none");
00883                         switch (type) {
00884                         case CHAR :
00885                                 memset(buffer,0,4097);
00886                                 strncpy(buffer,(char *)val,nb_val);
00887                         fits_update_key_str(fptr, keyname, buffer, comm[0], &stat);
00888                         break;
00889                         case SHORT :
00890                         case INT :
00891                         case LONG :
00892                         fits_update_key_lng(fptr, keyname, *((int *)val), comm[0], &stat);
00893                         break;
00894                         case FLOAT :
00895                         fits_update_key_flt(fptr, keyname, *((float *)val), 10, comm[0], &stat);
00896                         break;
00897                         case DOUBLE :
00898                         fits_update_key_dbl(fptr, keyname, *((double *)val), 10, comm[0], &stat);
00899                         break;
00900                         }
00901                         free((char *)comm[0]); free((char *)comm);
00902                         break;
00903                 }
00904                 fits_read_record(fptr,1,card,&stat); /* reset to start of header */
00905                 strcat(keyname,"#");
00906                 while (stat == 0)
00907                         fits_delete_key(fptr,keyname,&stat);
00908                 stat = 0;
00909                 comm = (char **)malloc(nb_val*sizeof(char *));  
00910                 for (i=0; i< nb_val; i++) {
00911                         comm[i] = (char *)malloc((FLEN_COMMENT+1)*sizeof(char));
00912                         if (comm[i] != NULL)
00913                                 strcpy(comm[i], "none");
00914                 }
00915                 switch (type) {
00916                 case CHAR :
00917                     *pt_val = (char *)malloc(strlen(val)*sizeof(char));
00918                     fits_write_keys_str(fptr, descr, 1, nb_val, pt_val, comm, &stat);
00919                     break;
00920                 case SHORT :
00921                 case INT :
00922                 case LONG :
00923                     fits_write_keys_lng(fptr, descr, 1, nb_val, (long *)val, comm, &stat);
00924                     break;
00925                 case FLOAT :
00926                     fits_write_keys_flt(fptr, descr, 1, nb_val, (float *)val, 10, comm, &stat);
00927                     break;
00928                 case DOUBLE :
00929                     fits_write_keys_dbl(fptr, descr, 1, nb_val, (double *)val, 10, comm, &stat);
00930                     break;
00931                 }
00932                 for (i=0; i< nb_val; i++) 
00933                         if (comm[i] != NULL)
00934                                 free((char *)comm[i]);
00935                 free((char *)comm);             
00936                 break;
00937 #endif
00938     case TIGER_FORMAT :
00939                 stat = 0;
00940 
00941                 descup = malloc((strlen(descr)+1)*sizeof(char));
00942         if (descup == NULL) {
00943                         stat = ERR_BAD_DESC;
00944                         return(stat);
00945                 }
00946                 strcpy(descup,descr);
00947                 upper_strg(descup);
00948 
00949                 if (pt_file->external_info == 0) { /* add new descriptor */
00950                         alloc_new_desc(pt_file,type,nb_val);
00951                         dsc_items = (Descr_Items *)(pt_file->external_info);
00952                         strcpy(dsc_items->descr_list[0].descr_name,descup);
00953                         dsc_items->nb_descr++;
00954                         i = 0;
00955                 }
00956                 else {
00957                         dsc_items = (Descr_Items *)(pt_file->external_info);
00958                         for (i=0; i< dsc_items->nb_descr 
00959                                 && strcmp(dsc_items->descr_list[i].descr_name,descup) != 0; i++);
00960                         if (i == dsc_items->nb_descr) { /* add new descriptor */
00961                                 alloc_new_desc(pt_file,type,nb_val);
00962                                 strcpy(dsc_items->descr_list[i].descr_name,descup);
00963                                 dsc_items->nb_descr++;
00964                         }
00965                 }
00966                 if (dsc_items->descr_list[i].data_type != type)
00967                         stat = -1;
00968                 else {
00969                         if (dsc_items->descr_list[i].nb_values < nb_val) {
00970                                 dsc_items->descr_list[i].nb_values = nb_val;
00971                                 dsc_items->descr_list[i].descr_value.c_data = 
00972                                 realloc(dsc_items->descr_list[i].descr_value.c_data,
00973                                         (nb_val+1)*sizeof_item(type));
00974                         }
00975                         if (type == CHAR) {
00976                                 if (strlen((char *)val) > nb_val) {
00977                                         strncpy(dsc_items->descr_list[i].descr_value.c_data,(char *)val,
00978                                 nb_val);
00979                                         dsc_items->descr_list[i].descr_value.c_data[nb_val] = 0;
00980                                 }
00981                                 else
00982                                         strcpy(dsc_items->descr_list[i].descr_value.c_data,(char *)val);
00983                         }
00984                         else {
00985                                 memcpy(dsc_items->descr_list[i].descr_value.c_data,val,
00986                                 nb_val*sizeof_item(type));
00987                         }
00988                 }
00989         break;
00990     }
00991     if (stat){
00992         sprintf(errtext, "WR_desc : file= %s desc=%s", pt_file->name,descr);
00993         stat = get_tiger_errcode(pt_file->data_format,stat);
00994         Handle_Error(errtext, stat);
00995     }
00996     return(stat);
00997 
00998 }
00999 
01000 /* --- Doxygen Function Comment -------------------------- */
01007 /* ------------------------------------------------------- */
01008 
01009 int 
01010 WR_history(void *anyfile_out, void *anyfile_in) 
01011 { 
01012         char  *pt_hist, *pt_tmp;
01013         char  history[lg_hist+1];
01014         int   status, i, len;
01015         char filename[lg_name+1];
01016         Anyfile *pt_in, *pt_out;
01017 
01018         pt_in = (Anyfile *)anyfile_in;
01019         pt_out = (Anyfile *)anyfile_out;
01020         history[0] = '\0';
01021 
01022         strcpy(filename,pt_out->name);
01023         if (exist_extension(filename) > 0) {
01024                 return(0);      /* don't write history in each FITS extension */
01025         }
01026         /* temporairement pour eviter un core dumped (a corriger) */
01027         strcpy(history,Cmd_History);
01028         status = WR_desc(anyfile_out,"HISTORY",CHAR,lg_hist+1,history); 
01029         return(status);
01030 
01031         if (pt_in != (Anyfile *)0)
01032                 strcpy(history,pt_in->history);
01033         else 
01034                 strcpy(history,pt_out->history);
01035 
01036         pt_hist = history;                      /* if history is too long, truncate it */
01037         while ((strlen(pt_hist)+strlen(Cmd_History) >lg_hist) 
01038                 && (*pt_hist != '\0'))
01039                 pt_hist = strpbrk(pt_hist,"\n")+1;
01040         len = strlen(pt_hist);
01041         if (pt_hist != history)
01042                 for (i=0, pt_tmp=pt_hist; i <= len; pt_tmp++,i++) 
01043                         history[i] = *pt_tmp;
01044 
01045         strcat(history,Cmd_History);
01046         strcpy(pt_out->history,history);
01047         status = WR_desc(anyfile_out,"HISTORY",CHAR,lg_hist+1,history); 
01048         return(status);
01049 }
01050 /* --- Doxygen Function Comment -------------------------- */
01057 /* ------------------------------------------------------- */
01058 
01059 int 
01060 delete_desc(void *anyfile, char *descr)
01061 {
01062     int stat=0, i, j;
01063     char errtext[132];
01064     char *descup;
01065     Anyfile *pt_file;
01066     Descr_Items *dsc_items;
01067 
01068 #ifdef IRAF
01069     char iraf_unit[lg_unit+1];
01070 #endif
01071 
01072 #ifdef FITS
01073     fitsfile *fptr;
01074         char card[FLEN_CARD+1];
01075     char keyname[FLEN_KEYWORD+1];
01076 #endif
01077 
01078         pt_file = (Anyfile *)anyfile;
01079 
01080     switch (pt_file->data_format) {
01081 #ifdef MIDAS
01082     case MIDAS_FORMAT :
01083         stat = SCDDEL(pt_file->imno,descr);
01084         break;
01085 #endif
01086 #ifdef IRAF
01087     case IRAF_FORMAT :
01088     case STSDAS_FORMAT :
01089                 len_descr = strlen(descr);
01090         int_type = 0;
01091                                                                 /* check for tables descriptors */
01092 
01093                 if (pt_file->file_type != TBL_TYPE) {
01094                         strcpy(iraf_unit,"None given");
01095                         len_unit = strlen(iraf_unit);
01096 
01097                 switch (type) {         /* frames */
01098                         }
01099                 }
01100                 else {
01101                 switch (type) {         /* tables */
01102                         }
01103                 }
01104                 break;
01105 #endif
01106 #ifdef FITS
01107     case FITS_A_FORMAT :
01108     case FITS_B_FORMAT :
01109 
01110                 fptr = (fitsfile *)pt_file->external_info;
01111                 stat = 0; /* check if key already exists */
01112                 strncpy(keyname,descr,MIN(strlen(descr),8));
01113                 keyname[MIN(strlen(descr),8)] = 0;
01114                 fits_read_record(fptr,1,card,&stat); /* reset to start of header */
01115                 fits_delete_key(fptr,keyname,&stat);
01116                 if (stat) {
01117                         stat = 0;
01118                         if (strlen(keyname) < 8) strcat(keyname,"#");
01119                         fits_read_record(fptr,1,card,&stat); /* reset to start of header */
01120                         while (stat == 0)
01121                                 fits_delete_key(fptr,keyname,&stat);
01122                         stat = 0;
01123                 }
01124                 break;
01125 #endif
01126     case TIGER_FORMAT :
01127                 stat = 0;
01128 
01129                 descup = malloc((strlen(descr)+1)*sizeof(char));
01130         if (descup == NULL) {
01131                         stat = ERR_BAD_DESC;
01132                         return(stat);
01133                 }
01134                 strcpy(descup,descr);
01135                 upper_strg(descup);
01136 
01137                 if (pt_file->external_info == 0) break;
01138                 dsc_items = (Descr_Items *)(pt_file->external_info);
01139                 for (i=0; i< dsc_items->nb_descr 
01140                         && strcmp(dsc_items->descr_list[i].descr_name,descup) != 0; i++);
01141                 if (i >= dsc_items->nb_descr) break;
01142                 for (j=dsc_items->nb_descr-1;j>i;j--)
01143                         memcpy(dsc_items->descr_list+j-1,dsc_items->descr_list+j,
01144                         sizeof(Descr_Items *));
01145                 dsc_items->nb_descr--;
01146         break;
01147     }
01148     if (stat){
01149         sprintf(errtext, "delete_desc : file= %s desc=%s", pt_file->name,descr);
01150                 stat = get_tiger_errcode(pt_file->data_format,stat);
01151         Handle_Error(errtext, stat);
01152     }
01153     return(stat);
01154 
01155 }
01156 /* --- Doxygen Function Comment -------------------------- */
01163 /* ------------------------------------------------------- */
01164 
01165 int get_all_desc(void *anyfile_in, char ***descr_list)
01166 {
01167         Anyfile *pt_file_in;
01168         Descr_Items *dsc_items;
01169         int status, nb_desc, fno = 1;
01170         int info;
01171         char **pt_list;
01172         char desc_name[lg_label+1];
01173 
01174 #ifdef IRAF
01175         char type[2], pattern[2];
01176         int key_list;
01177         char bool;
01178 #endif
01179 
01180 #ifdef FITS
01181     fitsfile *fptr_in;
01182     char *pt_char;
01183     int nkeys, keypos, j, i;
01184     char card[FLEN_CARD+1];  /* standard string lengths defined in fitsioc.h */
01185 #endif
01186 
01187         pt_file_in = (Anyfile *)anyfile_in;
01188 
01189         disable_user_warnings();
01190 
01191     switch (pt_file_in->data_format) {
01192 #ifdef MIDAS
01193     case MIDAS_FORMAT :
01194             nb_desc = 1; /* first non standard descriptor */
01195                 while (SCDINF(pt_file_in->imno,nb_desc,fno,desc_name,lg_label,&info) == 0) {
01196                         first_blk(desc_name);
01197                         if (strlen(desc_name) == 0) break;
01198                         nb_desc++;
01199                 }
01200                 alloc2d(descr_list,nb_desc,16,CHAR);
01201                 pt_list = *descr_list;
01202             nb_desc = 1; /* first non standard descriptor */
01203                 while (SCDINF(pt_file_in->imno,nb_desc,fno,desc_name,lg_label,&info) == 0) {
01204                         first_blk(desc_name);
01205                         if (strlen(desc_name) == 0) break;
01206                         if (strlen(desc_name) > 16) desc_name[16] = 0;
01207                         strcpy(pt_list[nb_desc-1],desc_name);
01208                         nb_desc++;
01209                 }
01210                 nb_desc--;
01211         break;
01212 #endif
01213 #ifdef IRAF
01214         case IRAF_FORMAT :
01215         case STSDAS_FORMAT :
01216             nb_desc = 1; /* first non standard descriptor */
01217                 strcpy(pattern,"*");
01218                 bool = 0;
01219 /*
01220                 udhokl(&(pt_file_in->imno),pattern,&bool,&key_list,&status,strlen(pattern));
01221                 while (! status) {
01222                         uhdgnk(&key_list,desc_name,&status);
01223                     if (status) break;
01224                         nb_desc++;
01225                 }
01226                 uhdckl(&key_list,&status);
01227 */
01228         break;
01229 #endif
01230 #ifdef FITS
01231         case FITS_A_FORMAT :
01232     case FITS_B_FORMAT :
01233         
01234         fptr_in = (fitsfile *)pt_file_in->external_info;
01235 
01236         status = 0;
01237         /* get no. of keywords */
01238         if (fits_get_hdrpos(fptr_in, &nkeys, &keypos, &status)) {
01239                 status = ERR_READ;
01240                 break;
01241         }
01242         alloc2d(descr_list,nkeys,FLEN_KEYWORD+1,CHAR);
01243         pt_list = *descr_list;
01244         for (i = 1, j = 0; i <= nkeys; i++)  {
01245                 status = 0;
01246                 if (fits_read_record(fptr_in, i, card, &status)) 
01247                         status = ERR_READ;
01248                 else {
01249                         strncpy(pt_list[j],card,FLEN_KEYWORD);
01250                         pt_char = strchr(pt_list[j],'=');
01251                         if (pt_char == NULL)
01252                                 continue;
01253                         else
01254                                 *pt_char = 0;
01255                         first_blk(pt_list[j]);
01256                         if (strlen(pt_list[j]) > 0)
01257                                 j++;
01258                 }       
01259         }
01260         nb_desc = j;
01261         break;
01262 #endif
01263     case TIGER_FORMAT :
01264                 status = 0;
01265                 if (pt_file_in->external_info == 0) {
01266                         break;
01267                 }
01268                 dsc_items = (Descr_Items *)(pt_file_in->external_info);
01269                 alloc2d(descr_list,dsc_items->nb_descr,16,CHAR);
01270                 pt_list = *descr_list;
01271                 for (nb_desc=0; nb_desc<dsc_items->nb_descr;nb_desc++) {
01272                         strncpy(pt_list[nb_desc],
01273                                 dsc_items->descr_list[nb_desc].descr_name,16);
01274                         if (strlen(pt_list[nb_desc]) > 16) pt_list[nb_desc][16] = 0;
01275                 }
01276         break;
01277         }
01278         restore_user_warnings();
01279 
01280         return(nb_desc);
01281 }
01282 /* --- Doxygen Function Comment -------------------------- */
01289 /* ------------------------------------------------------- */
01290 
01291 int CP_non_std_desc(void *anyfile_in, void *anyfile_out)
01292 {
01293         Anyfile *pt_file_in, *pt_file_out;
01294         Descr_Items *dsc_items;
01295         int status, nb_desc, fno = 1, nb_desc_tot;
01296         int nb_elt, nbytes;
01297         short data_type;
01298         int info;
01299         char desc_name[lg_label+1], type[2];
01300         void *value;
01301 
01302 #ifdef IRAF
01303         int key_list;
01304         char pattern[2];
01305         char bool;
01306 #endif
01307 #ifdef FITS
01308     fitsfile *fptr_in;
01309     fitsfile *fptr_out;
01310     int nkeys, keypos, j;
01311     int toFits;
01312     char card[FLEN_CARD+1];  /* standard string lengths defined in fitsioc.h */
01313 #endif
01314 
01315         pt_file_in = (Anyfile *)anyfile_in;
01316         pt_file_out = (Anyfile *)anyfile_out;
01317 
01318         disable_user_warnings();
01319 
01320     switch (pt_file_in->data_format) {
01321 #ifdef MIDAS
01322     case MIDAS_FORMAT :
01323 
01324          /* Access to the total number of descriptors */
01325          SCDINF(pt_file_in->imno,nb_desc,99,desc_name,lg_label,&nb_desc_tot);
01326 
01327            /* Since there are 8 std descriptors, we start at the 9th */
01328          for (nb_desc=9; nb_desc <= nb_desc_tot;nb_desc++) {
01329 
01330                     SCDINF(pt_file_in->imno,nb_desc,fno,desc_name,lg_label,&info);      
01331                     first_blk(desc_name);
01332                     status = SCDFND(pt_file_in->imno,desc_name,type,&nb_elt,&nbytes);
01333                     if (status) break;
01334                     switch (type[0]) {
01335                         case 'C' : data_type = CHAR;
01336                                 break;
01337                         case 'I' : data_type = INT;
01338                                 break;
01339                         case 'R' : data_type = FLOAT;
01340                                 break;
01341                         case 'D' : data_type = DOUBLE;
01342                                 break;
01343                         default : status = ERR_BAD_TYPE;
01344                                 break;
01345                         }
01346                     if (status) break;
01347                         if (data_type != CHAR) 
01348                                 value = (void *)malloc(nb_elt*nbytes);
01349                         else
01350                                 value = (void *)malloc((nb_elt+1)*nbytes);
01351                         RD_desc(pt_file_in,desc_name,data_type,nb_elt,value);
01352                         if (fits_non_std_desc(desc_name))
01353                                 WR_desc(pt_file_out,desc_name,data_type,nb_elt,value);
01354                         free(value);
01355                 }
01356         break;
01357 #endif
01358 #ifdef IRAF
01359         case IRAF_FORMAT :
01360         case STSDAS_FORMAT :
01361             nb_desc = 1; /* first non standard descriptor */
01362                 strcpy(pattern,"*");
01363                 bool = 0;
01364 /*
01365                 udhokl(&(pt_file_in->imno),pattern,&bool,&key_list,&status,strlen(pattern));
01366                 while (! status) {
01367                         uhdgnk(&key_list,desc_name,&status);
01368                     if (status) break;
01369                     *type = decode_datatype(IRAF_FORMAT,(short)datatype);
01370                         uhdgtp(&pt_file_in->imno),desc_name,&int_type,&status);
01371                     if (status) break;
01372                     switch (int_type) {
01373                         case 'C' : data_type = CHAR;
01374                                 break;
01375                         case 'I' : data_type = INT;
01376                                 break;
01377                         case 'R' : data_type = FLOAT;
01378                                 break;
01379                         case 'D' : data_type = DOUBLE;
01380                                 break;
01381                         default : status = ERR_BAD_TYPE;
01382                                 break;
01383                         } 
01384                         printf("[%d] desc_name=%s type=%c nb_elt=%d nbytes=%d\n",
01385                                 nb_desc,desc_name,type[0],nb_elt,nbytes);
01386 
01387                         if (data_type != CHAR) 
01388                                 value = (void *)malloc(nb_elt*nbytes);
01389                         else
01390                                 value = (void *)malloc((nb_elt+1)*nbytes);
01391                         RD_desc(pt_file_in,desc_name,data_type,nb_elt,value);
01392                         if (fits_non_std_desc(desc_name))
01393                                 WR_desc(pt_file_out,desc_name,data_type,nb_elt,value);
01394                         free(value);
01395                         nb_desc++;
01396                 }
01397                 uhdckl(&key_list,&status);
01398 */
01399         break;
01400 #endif
01401 #ifdef FITS
01402         case FITS_A_FORMAT :
01403     case FITS_B_FORMAT :
01404 
01405         fptr_in = (fitsfile *)pt_file_in->external_info;
01406 
01407     if ((pt_file_out->data_format == FITS_A_FORMAT) ||
01408                 (pt_file_out->data_format == FITS_B_FORMAT)) {
01409                 toFits = 1;
01410                 fptr_out = (fitsfile *)pt_file_out->external_info;
01411         }
01412         else
01413                 toFits = 0;
01414 
01415 
01416         status = 0;
01417         if (TK) 
01418                 print_progress("Copying user descriptors",0,-1);
01419                 
01420         /* get no. of keywords */
01421         if (fits_get_hdrpos(fptr_in, &nkeys, &keypos, &status)) {
01422                 status = ERR_READ;
01423                 break;
01424         }
01425         for (j = 1; j <= nkeys; j++)  {
01426                 status = 0;
01427                 if (fits_read_record(fptr_in, j, card, &status)) 
01428                         status = ERR_READ;
01429                 else {
01430                         if (fits_non_std_desc(card)) {
01431                                 if (toFits) {
01432                                         fits_write_record(fptr_out, card, &status); 
01433                                         continue;
01434                                 }
01435                                 
01436                                 if (card[8] != '=') continue;
01437                                 strncpy(desc_name,card,8);
01438                                 desc_name[8] = 0;
01439                                 first_blk(desc_name);
01440                                 nb_elt = get_descr_type(pt_file_in,desc_name, &data_type);
01441                                 nbytes = sizeof_item(data_type);
01442                             if (data_type != CHAR)
01443                       value = (void *)malloc(nb_elt*nbytes);
01444                 else
01445                       value = (void *)malloc((nb_elt+1)*nbytes);
01446                                 if (TK) 
01447                                         print_progress("Copying user descriptors",
01448                                                         (int)(j*100/nkeys),1);
01449                                 if (RD_desc(pt_file_in,desc_name,data_type,nb_elt,value) > 0)
01450                                         WR_desc(pt_file_out,desc_name,data_type,nb_elt,value);
01451                                 free((char *)value);
01452                         }
01453                 }
01454         }
01455         if (TK) 
01456                 print_progress("Copying user descriptors",100,1);
01457         break;
01458 #endif
01459     case TIGER_FORMAT :
01460                 status = 0;
01461                 if (pt_file_in->external_info == 0) {
01462                         break;
01463                 }
01464                 dsc_items = (Descr_Items *)(pt_file_in->external_info);
01465                 for (nb_desc=0; nb_desc<dsc_items->nb_descr;nb_desc++) {
01466                         strcpy(desc_name,dsc_items->descr_list[nb_desc].descr_name);
01467                         data_type = dsc_items->descr_list[nb_desc].data_type;
01468                         nb_elt = dsc_items->descr_list[nb_desc].nb_values;
01469                         nbytes = sizeof_item(data_type);
01470                         if (data_type != CHAR) 
01471                                 value = (void *)malloc(nb_elt*nbytes);
01472                         else
01473                                 value = (void *)malloc((nb_elt+1)*nbytes);
01474                         RD_desc(pt_file_in,desc_name,data_type,nb_elt,value);
01475                         WR_desc(pt_file_out,desc_name,data_type,nb_elt,value);
01476                         free(value);
01477                 }
01478         break;
01479         }
01480         restore_user_warnings();
01481 
01482         return(0);
01483 }
01484 
01485 /* --- Doxygen Function Comment -------------------------- */
01492                 /* @{ ***** Starting group28 ***** */
01505 /* ------------------------------------------------------- */
01506 
01507 int 
01508 create_spec(SPECTRUM *spectrum, char *name, int npix, double start, double step, short datatype, char *ident, char *unit)
01509 {
01510         char errtext[132], filename[lg_name+1];
01511         int iomode, int_datatype;
01512         int one_dim=1;
01513         int status,SCIPUT();
01514 #ifdef IRAF
01515         int len;
01516 #endif
01517 #ifdef FITS
01518     fitsfile *fptr;
01519     int bitpix;
01520     int one = 1;
01521 #endif
01522 
01523         strcpy(filename,name);
01524         first_blk(filename);
01525         strcpy(spectrum->name, filename);
01526         append_ima_extension(spectrum->name,OutputIO.basic_io);
01527         strcpy(filename,spectrum->name);
01528 
01529         if(ASK)
01530           confirme_erase(filename);
01531 
01532         spectrum->ident[0] = '\0';
01533         spectrum->history[0] = '\0';
01534         strcpy(spectrum->cunit,"None given");
01535         spectrum->start = start;
01536         spectrum->step = step;
01537         spectrum->npts = npix;
01538         spectrum->end = start + (spectrum->npts-1)*step;
01539         spectrum->iomode = (int)O_MODE;
01540         spectrum->data_type = datatype;
01541         spectrum->file_type = IMA_TYPE;
01542         spectrum->data_format = OutputIO.basic_io;
01543         spectrum->external_info = 0;
01544         
01545         if (ident != NULL) strcpy(spectrum->ident,ident);
01546         if (unit != NULL) {
01547                 memcpy(spectrum->cunit,unit,lg_unit);
01548                 spectrum->cunit[lg_unit] = '\0';
01549         }
01550         iomode = get_iomode_code(OutputIO.basic_io,spectrum->iomode);
01551         int_datatype = get_datatype_code(OutputIO.basic_io,datatype);
01552 
01553         switch(OutputIO.basic_io) {
01554 
01555 #ifdef MIDAS
01556         case MIDAS_FORMAT :     
01557                 status = SCIPUT(filename, int_datatype, 
01558                   iomode, F_IMA_TYPE, one_dim, &(spectrum->npts), 
01559           &(spectrum->start), 
01560           &(spectrum->step),spectrum->ident, spectrum->cunit, 
01561           &(spectrum->data), &(spectrum->imno));
01562                 break;
01563 #endif
01564 #ifdef IRAF
01565         case IRAF_FORMAT :
01566         case STSDAS_FORMAT :
01567                 len = strlen(filename);
01568         uimdel(filename,&status,len);
01569                 uimcre(filename,&int_datatype,&one_dim,&(spectrum->npts),
01570                   &(spectrum->imno),&status,len);
01571                 if (status) break;
01572                 status = WR_desc(spectrum,"IDENT",CHAR,strlen(spectrum->ident)
01573                                         ,spectrum->ident);
01574                 if (status) break;
01575                 status = alloc_spec_mem(spectrum, datatype);
01576                 break;
01577 #endif
01578 #ifdef FITS
01579         case FITS_A_FORMAT :
01580     case FITS_B_FORMAT :
01581                 status =0;
01582                 if (exist(filename))
01583                         delete_spec(spectrum);
01584                 if (fits_create_file(&fptr,filename,&status)) {
01585                         status = ERR_WRIT; break;
01586                 }
01587                 spectrum->external_info = (void *)fptr;
01588                 bitpix = fits_bitpix(datatype);
01589                 if (fits_create_img(fptr,bitpix,one_dim,(long *)&(spectrum->npts),&status)) {
01590                         status = ERR_WRIT; break;
01591                 }
01592                 fits_write_key(fptr, TINT, "CRPIX1", &one, "reference pixel", &status);
01593                 fits_write_key(fptr, TDOUBLE, "CRVAL1", &(spectrum->start), 
01594                         "coordinate at reference pixel", &status);
01595                 fits_write_key(fptr, TDOUBLE, "CDELT1", &(spectrum->step), 
01596                         "coordinate increment per pixel", &status);
01597                 status = alloc_spec_mem(spectrum, datatype);
01598                 WR_desc(spectrum,"IDENT",CHAR,lg_ident,spectrum->ident);
01599                 break;
01600 #endif
01601         }
01602 
01603         if (status) {
01604                 sprintf(errtext,"create_spec: spec %s",filename);
01605                 status = get_tiger_errcode(spectrum->data_format,status);
01606                 Handle_Error(errtext,status);
01607         }
01608         else {
01609                 spectrum->wstart = spectrum->start;      
01610                 spectrum->wend = spectrum->end;
01611                 spectrum->iwstart = 0;      
01612                 spectrum->iwend = spectrum->npts - 1;
01613                 spectrum->min = 0;
01614                 spectrum->max = 0;
01615                 spectrum->wmin = 0;
01616                 spectrum->wmax = 0;
01617 
01618                 WR_desc(spectrum,"COMMENT",CHAR,8,"        ");
01619         }
01620         return(status);
01621 }
01622 
01623 /* --- Doxygen Function Comment -------------------------- */
01631 /* ------------------------------------------------------- */
01632 int 
01633 header_spec(SPECTRUM *spectrum, char *name,char *mode)
01634 {
01635         char errtext[132], filename[lg_name+1];
01636         int status, nbaxes, info[5], iomode, one=1, int_datatype;
01637         float cuts[4];
01638 #ifdef IRAF
01639         int one_dim=1, len;
01640 #endif
01641 #ifdef FITS
01642     fitsfile *fptr;
01643 #endif
01644         
01645         memset(spectrum->ident,' ',lg_ident);
01646         spectrum->ident[lg_ident] = '\0';
01647         memset(spectrum->cunit,' ',lg_unit);
01648         spectrum->cunit[lg_unit] = '\0';
01649         memset(spectrum->history,' ',lg_hist);
01650         spectrum->history[lg_hist] = '\0';
01651         spectrum->file_type = IMA_TYPE;
01652         spectrum->data_format = InputIO.basic_io;
01653         spectrum->external_info = 0;
01654         spectrum->data.d_data = NULL;
01655         spectrum->min = 0;
01656         spectrum->max = 0;
01657 
01658         strcpy(filename,name);
01659         first_blk(filename);
01660         strcpy(spectrum->name,filename);
01661         append_ima_extension(spectrum->name,InputIO.basic_io);
01662 
01663         strcpy(filename,spectrum->name);
01664 
01665         if (! exist(filename)) {
01666                 status = ERR_OPEN;
01667                 sprintf(errtext,"header_spec: spec %s",filename);
01668                 Handle_Error(errtext,status);
01669                 return(status);
01670         }
01671 
01672         switch(mode[0]) {
01673                 case 'I' : 
01674                         if (mode[1] == 'O')
01675                                 spectrum->iomode = (int)IO_MODE;
01676                         else
01677                                 spectrum->iomode = (int)I_MODE;
01678                         break;
01679                 case 'O' : spectrum->iomode = (int)O_MODE;
01680                         break;
01681                 default  : spectrum->iomode = (int)I_MODE;
01682                         break;
01683         }
01684         
01685         iomode = get_iomode_code(InputIO.basic_io,spectrum->iomode);
01686 
01687         switch (InputIO.basic_io) {
01688 
01689 #ifdef MIDAS
01690         case MIDAS_FORMAT :
01691                 status =  SCFINF(filename,2,info);  
01692                                                                 /* returns info on file storage */
01693                 if (status == 0) {
01694                         status = SCFOPN(filename, info[1], 0, F_IMA_TYPE,
01695                     &(spectrum->imno));
01696                         nbaxes = RD_desc(spectrum,"NPIX",INT,2,&(spectrum->npts));
01697                         if(nbaxes != 1)  {
01698                                 /* We open an image as spectrum, too bad ! */
01699                                 status = ERR_OPEN; 
01700                                 break;
01701                         }
01702                         RD_desc(spectrum,"START",DOUBLE,1,&(spectrum->start));
01703                         RD_desc(spectrum,"STEP",DOUBLE,1,&(spectrum->step));
01704                         RD_desc(spectrum,"IDENT",CHAR,lg_ident,spectrum->ident);
01705                         RD_desc(spectrum,"CUNIT",CHAR,lg_unit,spectrum->cunit);
01706 
01707                                 
01708                 }
01709                 break;
01710 #endif
01711 
01712 #ifdef IRAF
01713         case IRAF_FORMAT :
01714         case STSDAS_FORMAT :
01715                 len = strlen(filename);
01716                 uimopn(filename,&iomode,&(spectrum->imno),&status,len);
01717                 if (status != 0)
01718                         break;
01719                 uimgid(&(spectrum->imno),&int_datatype,&one_dim,&(spectrum->npts),
01720                         &status);
01721                 spectrum->data_type= decode_datatype(InputIO.basic_io,(short)(int_datatype));
01722         if (status != 0)
01723                         break;
01724                 disable_user_warnings();
01725                 RD_desc(spectrum,"IDENT",CHAR,lg_ident,spectrum->ident);
01726                 restore_user_warnings();
01727                 break;
01728 #endif
01729 #ifdef FITS
01730         case FITS_A_FORMAT :
01731         case FITS_B_FORMAT :
01732                 status =0;
01733                 if (fits_open_file(&fptr,filename,iomode,&status)) {
01734                         status = ERR_ACCESS; break;
01735                 }
01736                 spectrum->external_info = (void *)fptr;
01737 
01738                 if (fits_read_key(fptr, TINT,"NAXIS", &nbaxes,NULL, &status)) {
01739                         status = ERR_READ; break;
01740                 }
01741                 if (nbaxes != 1) {
01742                         status = ERR_IMA_HEAD; break;
01743                 }
01744                 int_datatype = (fptr->Fptr->tableptr)->tdatatype;
01745                 spectrum->data_type = decode_datatype(InputIO.basic_io,(short)int_datatype);
01746                 if (fits_read_key(fptr, TINT, "NAXIS1", 
01747                                 &(spectrum->npts), NULL, &status)) {
01748                         status = ERR_READ; break;
01749                 }
01750                 fits_read_key(fptr, TINT, "CRPIX1", &one, NULL, &status);
01751                 if (status) { status = 0; one = 1; }
01752                 fits_read_key(fptr, TDOUBLE, "CRVAL1", &(spectrum->start), NULL, &status);
01753                 if (status) { status = 0; spectrum->start = (double)1; }
01754                 fits_read_key(fptr, TDOUBLE, "CDELT1", &(spectrum->step), NULL, &status);
01755                 if (status) { status = 0; spectrum->step = (double)1; }
01756             spectrum->start -= (one-1)*spectrum->step;
01757 
01758         break;
01759 #endif
01760         }
01761 
01762         if (status) {
01763                 sprintf(errtext,"header_spec: spec %s",filename);
01764                 status = get_tiger_errcode(spectrum->data_format,status);
01765                 Handle_Error(errtext,status);
01766         }
01767         else {           
01768                 disable_user_warnings();
01769                 status = RD_desc(spectrum,"LHCUTS",FLOAT,4,cuts);
01770                 RD_desc(spectrum,"HISTORY",CHAR,lg_hist,spectrum->history);
01771                 restore_user_warnings();
01772 
01773                 spectrum->end = ((spectrum->npts-1)*spectrum->step + spectrum->start);      
01774                 spectrum->wstart = spectrum->start;      
01775                 spectrum->wend = spectrum->end;
01776                 spectrum->iwstart = 0;      
01777                 spectrum->iwend = spectrum->npts - 1;
01778                 if (status > 0) {
01779                         spectrum->min = cuts[2];
01780                         spectrum->max = cuts[3];
01781                 }
01782                 status = 0;
01783                 spectrum->wmin = spectrum->min;
01784                 spectrum->wmax = spectrum->max;
01785         }
01786         return(status);
01787 }
01788 /* --- Doxygen Function Comment -------------------------- */
01796 /* ------------------------------------------------------- */
01797 int 
01798 open_spec(SPECTRUM *spectrum, char *name,char *mode)
01799 {
01800         char errtext[132], filename[lg_name+1];
01801         int status, nbaxes, info[5], iomode, one=1, int_datatype;
01802         float cuts[4];
01803 #ifdef IRAF
01804         int len, one_dim=1;
01805 #endif
01806 #ifdef FITS
01807         fitsfile *fptr;
01808         int bitpix;
01809         int nbread;
01810         int group = 0;
01811         int npix;
01812 #endif
01813         
01814         memset(spectrum->ident,' ',lg_ident);
01815         spectrum->ident[lg_ident] = '\0';
01816         memset(spectrum->cunit,' ',lg_unit);
01817         spectrum->cunit[lg_unit] = '\0';
01818         memset(spectrum->history,' ',lg_hist);
01819         spectrum->history[lg_hist] = '\0';
01820         spectrum->file_type = IMA_TYPE;
01821         spectrum->data_format = InputIO.basic_io;
01822         spectrum->external_info = 0;
01823 
01824         strcpy(filename,name);
01825         first_blk(filename); 
01826         strcpy(spectrum->name, filename);
01827         append_ima_extension(spectrum->name,InputIO.basic_io);
01828 
01829         strcpy(filename,spectrum->name);
01830 
01831         if (! exist(filename)) {
01832                 status = ERR_OPEN;
01833                 sprintf(errtext,"open_spec: spec %s",filename);
01834                 Handle_Error(errtext,status);
01835                 return(status);
01836         }
01837 
01838         switch(mode[0]) {
01839                 case 'I' : 
01840                         if (mode[1] == 'O')
01841                                 spectrum->iomode = (int)IO_MODE;
01842                         else
01843                                 spectrum->iomode = (int)I_MODE;
01844                         break;
01845                 case 'O' : spectrum->iomode = (int)O_MODE;
01846                         break;
01847                 default  : spectrum->iomode = (int)I_MODE;
01848                         break;
01849         }
01850         
01851         iomode = get_iomode_code(InputIO.basic_io,spectrum->iomode);
01852 
01853         switch (InputIO.basic_io) {
01854 
01855 #ifdef MIDAS
01856         case MIDAS_FORMAT :
01857                 status =  SCFINF(filename,2,info);  
01858                                                                 /* returns info on file storage */
01859                 if (status == 0) {
01860                         status = SCIGET(filename, info[1], iomode, F_IMA_TYPE, 
01861                                 1, &nbaxes, &(spectrum->npts), &(spectrum->start), 
01862                                 &(spectrum->step), spectrum->ident, spectrum->cunit, 
01863                                 (char **)(&(spectrum->data)), &(spectrum->imno));
01864                                 spectrum->data_type = info[1];
01865                                 spectrum->data_type = decode_datatype(InputIO.basic_io,
01866                                                 spectrum->data_type);
01867 
01868                                 if(nbaxes!=1) /* We open an image like a spectrum , and that's not good */
01869                                   status = ERR_OPEN; 
01870                                 
01871                 }
01872                 break;
01873 #endif
01874 
01875 #ifdef IRAF
01876         case IRAF_FORMAT :
01877         case STSDAS_FORMAT :
01878                 len = strlen(filename);
01879                 uimopn(filename,&iomode,&(spectrum->imno),&status,len);
01880                 if (status != 0)
01881                         break;
01882                 uimgid(&(spectrum->imno),&int_datatype,&one_dim,&(spectrum->npts),
01883                         &status);
01884                 spectrum->data_type= decode_datatype(InputIO.basic_io,(short)(int_datatype));
01885                 if (status != 0)
01886                         break;
01887                 alloc_spec_mem(spectrum, datatype);
01888                 switch(spectrum->data_type) {
01889                 case SHORT :
01890                         uigs1s(&(spectrum->imno),&one,&(spectrum->npts),
01891                         spectrum->data.s_data,&status);
01892                         break;
01893                 case INT :
01894                 case LONG :
01895                         uigs1l(&(spectrum->imno),&one,&(spectrum->npts),
01896                         spectrum->data.l_data,&status);
01897                         break;
01898                 case FLOAT :
01899                         uigs1r(&(spectrum->imno),&one,&(spectrum->npts),
01900                         spectrum->data.f_data,&status);
01901                         break;
01902                 case DOUBLE :
01903                         uigs1d(&(spectrum->imno),&one,&(spectrum->npts),
01904                         spectrum->data.d_data,&status);
01905                         break;
01906                 }
01907                 disable_user_warnings();
01908                 RD_desc(spectrum,"IDENT",CHAR,lg_ident,spectrum->ident);
01909                 restore_user_warnings();
01910                 break;
01911 #endif
01912 #ifdef FITS
01913         case FITS_A_FORMAT :
01914         case FITS_B_FORMAT :
01915                 status =0;
01916                 if (fits_open_file(&fptr,filename,iomode,&status)) {
01917                         status = ERR_ACCESS; break;
01918                 }
01919                 spectrum->external_info = (void *)fptr;
01920 
01921                 if (fits_read_key(fptr, TINT,"NAXIS", &nbaxes,NULL, &status)) {
01922                         status = ERR_READ; break;
01923                 }
01924                 if (nbaxes != 1) {
01925                         status = ERR_IMA_HEAD; break;
01926                 }
01927                 if (fits_read_key(fptr, TINT,"BITPIX", &bitpix,NULL, &status)) {
01928                         status = ERR_READ; break;
01929                 }
01930                 int_datatype = (fptr->Fptr->tableptr)->tdatatype;
01931                 spectrum->data_type = decode_datatype(InputIO.basic_io,(short)int_datatype);
01932                 if (spectrum->data_type == SHORT) {
01933                         if (fptr->Fptr->tableptr[1].tscale == 1 && fptr->Fptr->tableptr[1].tzero == 32768)
01934                         /* unsigned short !!! */
01935                                 spectrum->data_type = LONG;
01936                 }
01937                 if (fits_read_key(fptr, TINT, "NAXIS1", 
01938                                 &(spectrum->npts), NULL, &status)) {
01939                         status = ERR_READ; break;
01940                 }
01941                 if (status == 0) {
01942                         fits_read_key(fptr, TINT, "CRPIX1", &one, NULL, &status);
01943                         if (status) { status = 0; one = 1; }
01944                         fits_read_key(fptr, TDOUBLE, "CRVAL1", &(spectrum->start), NULL, &status);
01945                         if (status) { status = 0; spectrum->start = (double)1; }
01946                         fits_read_key(fptr, TDOUBLE, "CDELT1", &(spectrum->step), NULL, &status);
01947                         if (status) { status = 0; spectrum->step = (double)1; }
01948                         spectrum->start -= (one-1)*spectrum->step;
01949                 }
01950                 else
01951                         break;
01952 
01953                 if (alloc_spec_mem(spectrum, spectrum->data_type) < 0) {
01954                         fits_close_file(fptr,&status);
01955                         status = ERR_ALLOC;
01956                         break;
01957                 }
01958 
01959                 npix = spectrum->npts;
01960                 switch (spectrum->data_type) {
01961                 case SHORT :
01962                         if (fits_read_img_sht(fptr,group,1L,npix,(short)0,
01963                                 spectrum->data.s_data,&nbread,&status)) {
01964                                 status = ERR_READ; break;
01965                         }
01966                         break;
01967                 case LONG :
01968                 case INT :
01969                         if (fits_read_img_lng(fptr,group,1L,npix,(int)0,
01970                                 spectrum->data.l_data,&nbread,&status)) {
01971                                 status = ERR_READ; break;
01972                         }
01973                         break;
01974                 case FLOAT :
01975                         if (fits_read_img_flt(fptr,group,1L,npix,(float)0,
01976                                 spectrum->data.f_data,&nbread,&status)) {
01977                                 status = ERR_READ; break;
01978                         }
01979                         break;
01980                 case DOUBLE :
01981                         if (fits_read_img_dbl(fptr,group,1L,npix,(double)0,
01982                                 spectrum->data.d_data,&nbread,&status)) {
01983                                 status = ERR_READ; break;
01984                         }
01985                         break;
01986                 }
01987 
01988         break;
01989 #endif
01990         }
01991 
01992         if (status) {
01993                 sprintf(errtext,"open_spec: spec %s",filename);
01994                 status = get_tiger_errcode(spectrum->data_format,status);
01995                 Handle_Error(errtext,status);
01996         }
01997         else {           
01998                 disable_user_warnings();
01999                 status = RD_desc(spectrum,"LHCUTS",FLOAT,4,cuts);
02000                 RD_desc(spectrum,"HISTORY",CHAR,lg_hist,spectrum->history);
02001                 restore_user_warnings();
02002 
02003                 spectrum->end = ((spectrum->npts-1)*spectrum->step + spectrum->start);      
02004                 spectrum->wstart = spectrum->start;      
02005                 spectrum->wend = spectrum->end;
02006                 spectrum->iwstart = 0;      
02007                 spectrum->iwend = spectrum->npts - 1;
02008                 if (status <= 0) {
02009                         spec_minmax(spectrum);
02010                 }
02011                 else {
02012                         spectrum->min = cuts[2];
02013                         spectrum->max = cuts[3];
02014                 }
02015                 status = 0;
02016                 spectrum->wmin = spectrum->min;
02017                 spectrum->wmax = spectrum->max;
02018         }
02019         return(status);
02020 }
02021 /* --- Doxygen Function Comment -------------------------- */
02027 /* ------------------------------------------------------- */
02028 int 
02029 close_spec(SPECTRUM *spectrum)                  /* close active spectrum */
02030 {
02031         char    errtext[132], filename[lg_name+1];
02032         int     stat, int_datatype;
02033         float   cuts[4];
02034 #ifdef IRAF
02035         int one=1;
02036 #endif
02037 #ifdef FITS
02038     fitsfile *fptr;
02039 #endif
02040 
02041         strcpy(filename,spectrum->name);
02042 
02043         if (spectrum->iomode == (int)I_MODE) {
02044                 switch (spectrum->data_format) {
02045 #ifdef MIDAS
02046                 case MIDAS_FORMAT :
02047                         stat = SCFCLO(spectrum->imno);
02048                         break;
02049 #endif
02050 #ifdef IRAF
02051                 case IRAF_FORMAT :
02052                 case STSDAS_FORMAT :
02053                         uimclo(&(spectrum->imno),&stat);
02054                         break;
02055 #endif
02056 #ifdef FITS
02057                 case FITS_A_FORMAT :
02058         case FITS_B_FORMAT :
02059 
02060                     stat =0;
02061                     fptr = (fitsfile *)spectrum->external_info;
02062                     fits_close_file(fptr,&stat);
02063                     free_spec_mem(spectrum);
02064                     break;
02065 #endif
02066                 }
02067                 if (stat) {
02068                 sprintf(errtext,"close_spec: spec %s",filename);
02069                 stat = get_tiger_errcode(spectrum->data_format,stat);
02070                 Handle_Error(errtext,stat);
02071                 }
02072                 return(stat);
02073         }
02074 
02075         if (spectrum->data.d_data != NULL) {
02076                 set_subspec(spectrum,spectrum->start,spectrum->end);
02077                 spec_minmax(spectrum);
02078                 cuts[0]=(float)spectrum->wmin; cuts[2]=(float)spectrum->wmin;
02079                 cuts[1]=(float)spectrum->wmax; cuts[3]=(float)spectrum->wmax;
02080                 stat = WR_desc(spectrum,"LHCUTS",FLOAT,4,cuts);
02081         }
02082 
02083         WR_history(spectrum, (Anyfile *)0);
02084 
02085         switch (spectrum->data_format) {
02086 #ifdef MIDAS
02087         case MIDAS_FORMAT :
02088                 stat = SCFCLO(spectrum->imno);
02089                 break;
02090 #endif
02091 #ifdef IRAF
02092         case IRAF_FORMAT :
02093         case STSDAS_FORMAT :
02094         switch(spectrum->data_type) {
02095         case SHORT :
02096                         uips1s(&(spectrum->imno),&one,&(spectrum->npts),
02097                                 spectrum->data.s_data,&stat);
02098                         int_datatype = sizeof(short);
02099             break;
02100         case INT :
02101         case LONG :
02102             uips1l(&(spectrum->imno),&one,&(spectrum->npts),
02103                spectrum->data.l_data,&stat);
02104                         int_datatype = sizeof(long);
02105             break;
02106         case FLOAT :
02107                         uips1r(&(spectrum->imno),&one,&(spectrum->npts),
02108                 spectrum->data.f_data,&stat);
02109                         int_datatype = sizeof(float);
02110             break;
02111         case DOUBLE :
02112                         uips1d(&(spectrum->imno),&one,&(spectrum->npts),
02113                 spectrum->data.d_data,&stat);
02114                         int_datatype = sizeof(double);
02115             break;
02116         }
02117         if (stat == 0) {
02118                         WR_desc(spectrum,"IRAF-MIN",FLOAT,1,cuts);
02119                         WR_desc(spectrum,"IRAF-MAX",FLOAT,1,&(cuts[1]));
02120                         int_datatype *= 8;
02121                         WR_desc(spectrum,"IRAF-BPX",INT,1,&int_datatype);
02122                         uimclo(&(spectrum->imno),&stat);
02123                 }
02124                 free_spec_mem(spectrum);
02125         break;
02126 #endif
02127 #ifdef FITS
02128         case FITS_A_FORMAT :
02129     case FITS_B_FORMAT :
02130         stat = 0;
02131         fptr = (fitsfile *)spectrum->external_info;
02132         if (spectrum->iomode != (int)I_MODE) {
02133                 if (spectrum->data.d_data != NULL) {
02134                         int_datatype = get_datatype_code(OutputIO.basic_io,spectrum->data_type);
02135                         if (fits_write_img(fptr,int_datatype,1L,
02136                                 spectrum->npts,spectrum->data.s_data,&stat)) {
02137                                 stat = ERR_WRIT; 
02138                         }
02139                 }
02140         }
02141         if (! stat) 
02142             fits_close_file(fptr,&stat);
02143         free_spec_mem(spectrum);
02144         break;
02145 #endif
02146         }
02147         if (stat) {
02148                 sprintf(errtext,"close_spec: spec %s",filename);
02149                 stat = get_tiger_errcode(spectrum->data_format,stat);
02150                 Handle_Error(errtext,stat);
02151         } else {
02152                 if(TK && (spectrum->iomode == O_MODE || spectrum->iomode == IO_MODE))
02153                 {
02154                         printf("@ N {%s}\n",filename);
02155                 }
02156         }
02157 
02158         return(stat);
02159 }
02160 
02161 /* --- Doxygen Function Comment -------------------------- */
02167 /* ------------------------------------------------------- */
02168 int 
02169 delete_spec(SPECTRUM *spectrum)                 /* delete active spectrum */
02170 {
02171         int status, iomode;
02172         char errtext[132],filename[lg_name+1];
02173 #ifdef IRAF
02174         int len;
02175 #endif
02176 #ifdef FITS
02177     fitsfile *fptr;
02178 #endif
02179         
02180         strcpy(filename,spectrum->name);
02181 
02182         switch (spectrum->data_format) {
02183 #ifdef MIDAS
02184         case MIDAS_FORMAT :
02185                 status = SCFDEL(filename);
02186                 break;
02187 #endif
02188 #ifdef IRAF
02189         case IRAF_FORMAT :
02190         case STSDAS_FORMAT :
02191                 len = strlen(filename);
02192         uimdel(filename,&status,len);
02193                 break;
02194 #endif
02195 #ifdef FITS
02196         case FITS_A_FORMAT :
02197         case FITS_B_FORMAT :
02198         status = 0;
02199         if (spectrum->external_info == NULL) {
02200                 iomode = get_iomode_code(InputIO.basic_io,I_MODE);
02201                 fits_open_file(&fptr,filename,iomode,&status);
02202                 if (status)
02203                         status = unlink(filename);
02204                 fits_delete_file(fptr, &status);
02205         }
02206         else {
02207                 fptr = (fitsfile *)spectrum->external_info;
02208                 if (status)
02209                         status = unlink(filename);
02210                 else {
02211                         fits_delete_file(fptr, &status);
02212                         free_spec_mem(spectrum);
02213                 }
02214         }
02215         break;
02216 #endif
02217         }
02218         if (status)     {
02219                 sprintf(errtext,"delete_spec: spec %s",spectrum->name);
02220                 status = get_tiger_errcode(spectrum->data_format,status);
02221                 Handle_Error(errtext,status);
02222         }
02223         return(status);
02224 }
02225 
02226 /* --- Doxygen Function Comment -------------------------- */
02236 /* ------------------------------------------------------- */
02237 
02238 int 
02239 create_spec_mem(SPECTRUM *spectrum, int npix, double start, double step, short datatype)
02240 {
02241         char errtext[132];
02242         int iomode, int_datatype;
02243         int status;
02244 
02245         spectrum->name[0] = '\0';
02246         spectrum->ident[0] = '\0';
02247         spectrum->history[0] = '\0';
02248         strcpy(spectrum->cunit,"None given");
02249         spectrum->start = start;
02250         spectrum->step = step;
02251         spectrum->npts = npix;
02252         spectrum->end = start + (spectrum->npts-1)*step;
02253         spectrum->iomode = (int)O_MODE;
02254         spectrum->data_type = datatype;
02255         spectrum->file_type = IMA_TYPE;
02256         spectrum->data_format = OutputIO.basic_io;
02257         spectrum->external_info = 0;
02258         
02259         iomode = get_iomode_code(OutputIO.basic_io,spectrum->iomode);
02260         int_datatype = get_datatype_code(OutputIO.basic_io,datatype);
02261 
02262         status = alloc_spec_mem(spectrum, datatype);
02263 
02264         if (status) {
02265                 sprintf(errtext,"create_spec_mem: ");
02266                 status = get_tiger_errcode(spectrum->data_format,status);
02267                 Handle_Error(errtext,status);
02268         }
02269         else {
02270                 spectrum->wstart = spectrum->start;      
02271                 spectrum->wend = spectrum->end;
02272                 spectrum->iwstart = 0;      
02273                 spectrum->iwend = spectrum->npts - 1;
02274                 spectrum->min = 0;
02275                 spectrum->max = 0;
02276                 spectrum->wmin = 0;
02277                 spectrum->wmax = 0;
02278         }
02279         return(status);
02280 }
02281 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
02282                 routines to read/write values into spectrum files are
02283                      given in ../incl/funcdef.h
02284 !     
02285 !.func                        RD_spec()
02286 !
02287 !.purp                  reads a spectrum value
02288 !.desc
02289 ! (type) value = RD_spec(spectrum,pixel)
02290 !
02291 ! SPECTRUM *spectrum;   spectrum structure
02292 ! int pixel;            pixel value to read
02293 !.ed
02294 !     
02295 !.func                        WR_spec()
02296 !
02297 !.purp                  writes a spectrum value
02298 !.desc
02299 ! WR_spec(spectrum,pixel,value)
02300 !
02301 ! SPECTRUM *spectrum;   spectrum structure
02302 ! int pixel;            pixel value to write
02303 ! (type) value;         value to write
02304 !.ed
02305 !     
02306 !.func                        RD_qspec()
02307 !
02308 !.purp                  reads a spectrum quality flag
02309 !.desc
02310 ! (type) value = RD_qspec(spectrum,pixel)
02311 !
02312 ! SPECTRUM *spectrum;   spectrum structure
02313 ! int pixel;            pixel value to read
02314 !.ed
02315 !     
02316 !.func                        WR_qspec()
02317 !
02318 !.purp                  writes a spectrum quality flag
02319 !.desc
02320 ! WR_qspec(spectrum,pixel,value)
02321 !
02322 ! SPECTRUM *spectrum;   spectrum structure
02323 ! int pixel;            pixel value to write
02324 ! (type) value;         value to write
02325 !.ed
02326 -----------------------------------------------------------------------*/
02327 /* --- Doxygen Function Comment -------------------------- */
02334                 /* @{ ***** Starting group29 ***** */
02347 /* ------------------------------------------------------- */
02348 
02349 int 
02350 create_frame(IMAGE2D *frame, char *name, int *npix, double *start, double *step, short datatype, char *ident, char *unit)
02351 {
02352         char errtext[132], filename[lg_name+1];
02353         int two_dim=2, iomode, int_datatype;
02354         int status, SCIPUT();
02355 #ifdef IRAF
02356         int len;
02357 #endif
02358 #ifdef FITS
02359     fitsfile *fptr;
02360     int bitpix;
02361     int one = 1;
02362 #endif
02363 
02364         strcpy(filename,name);
02365         first_blk(filename);
02366         strcpy(frame->name, filename);
02367         append_ima_extension(frame->name,OutputIO.basic_io);
02368         
02369         strcpy(filename, frame->name);
02370 
02371         if(ASK)
02372           confirme_erase(filename);
02373 
02374         frame->ident[0] = '\0';
02375         frame->history[0] = '\0';
02376         strcpy(frame->cunit,"None given");
02377         frame->startx = start[0];       frame->starty = start[1];
02378         frame->stepx = step[0];         frame->stepy = step[1];
02379         frame->endx = start[0] + (npix[0]-1)*step[0];
02380         frame->endy = start[1] + (npix[1]-1)*step[1];
02381         frame->nx = npix[0];            frame->ny = npix[1];
02382         frame->iomode = (int)O_MODE;
02383         frame->data_type = datatype;
02384         frame->file_type = IMA_TYPE;
02385         frame->data_format = OutputIO.basic_io;
02386         frame->external_info = 0;
02387         
02388         if (ident != NULL) strcpy(frame->ident,ident);
02389         if (unit != NULL) {
02390                 memcpy(frame->cunit,unit,lg_unit);
02391                 frame->cunit[lg_unit] = '\0';
02392         }
02393         
02394         iomode = get_iomode_code(OutputIO.basic_io,frame->iomode);
02395         int_datatype = get_datatype_code(OutputIO.basic_io,datatype);
02396 
02397     switch(OutputIO.basic_io) {
02398 
02399 #ifdef MIDAS
02400     case MIDAS_FORMAT :
02401           status = SCIPUT(filename, int_datatype, iomode, 
02402                   F_IMA_TYPE, two_dim, &(frame->nx), &(frame->startx), 
02403               &(frame->stepx),frame->ident, frame->cunit, &(frame->data), 
02404                   &(frame->imno));
02405         break;
02406 #endif
02407 #ifdef IRAF
02408     case IRAF_FORMAT :
02409     case STSDAS_FORMAT :
02410                 len = strlen(filename);
02411                 uimdel(filename,&status,len);
02412                 uimcre(filename,&int_datatype,&two_dim,&(frame->nx),
02413           &(frame->imno),&status,len);
02414                 if (status) break;
02415                 status = WR_desc(frame,"IDENT",CHAR,strlen(frame->ident),
02416                                 frame->ident);
02417                 if (status) break;
02418                 status = alloc_frame_mem(frame, datatype);
02419         break;
02420 #endif
02421 #ifdef FITS
02422     case FITS_A_FORMAT :
02423     case FITS_B_FORMAT :
02424                 status =0;
02425                 if (exist(filename))
02426                     delete_frame(frame);
02427                 if (fits_create_file(&fptr,filename,&status)) {
02428                     status = ERR_WRIT; break;
02429                 }
02430                 frame->external_info = (void *)fptr;
02431                 bitpix = fits_bitpix(datatype);
02432                 if (fits_create_img(fptr,bitpix,two_dim,(long *)&(frame->nx),
02433                         &status)) {
02434                      status = ERR_WRIT; break;
02435                 }
02436                 fits_write_key(fptr, TINT, "CRPIX1", &one, "reference pixel", &status);
02437                 fits_write_key(fptr, TDOUBLE, "CRVAL1", &(frame->startx), "coordinate at reference pixel", &status);
02438                 fits_write_key(fptr, TDOUBLE, "CDELT1", &(frame->stepx), "coordinate increment per pixel", &status);
02439                 fits_write_key(fptr, TINT, "CRPIX2", &one, "reference pixel", &status);
02440                 fits_write_key(fptr, TDOUBLE, "CRVAL2", &(frame->starty), "coordinate at reference pixel", &status);
02441                 fits_write_key(fptr, TDOUBLE, "CDELT2", &(frame->stepy), "coordinate increment per pixel", &status);
02442                 status = alloc_frame_mem(frame, datatype);
02443                 WR_desc(frame,"IDENT",CHAR,lg_ident,frame->ident);
02444         break;
02445 #endif
02446     }
02447 
02448         if (status) {
02449           sprintf(errtext,"create_frame: frame %s",filename);
02450           status = get_tiger_errcode(frame->data_format,status);
02451           Handle_Error(errtext,status);
02452         }
02453         else {
02454           frame->min = 0;
02455           frame->max = 0;
02456           WR_desc(frame,"COMMENT",CHAR,8,"       ");
02457         }
02458         return(status);
02459 } 
02460 /* --- Doxygen Function Comment -------------------------- */
02468 /* ------------------------------------------------------- */
02469 int 
02470 header_frame(IMAGE2D *frame, char *name, char *mode)            
02471 {
02472         char errtext[132], filename[lg_name+1];
02473         int status, nbaxes, iomode, int_datatype;
02474         int one=1;
02475         float cuts[4];
02476         int info[5];
02477 #ifdef IRAF
02478         int two_dim=2;
02479         int len;
02480 #endif
02481 #ifdef FITS
02482     fitsfile *fptr;
02483     int bitpix;
02484 #endif
02485 
02486         memset(frame->ident,' ',lg_ident);
02487         frame->ident[lg_ident] = '\0';
02488         memset(frame->cunit,' ',lg_unit);
02489         frame->cunit[lg_unit] = '\0';
02490         memset(frame->history,' ',lg_hist);
02491         frame->history[lg_hist] = '\0';
02492         frame->external_info = 0;
02493         frame->file_type = IMA_TYPE;
02494         frame->data_format = InputIO.basic_io;
02495         frame->data.d_data = NULL;
02496         frame->min = 0;
02497         frame->max = 0;
02498 
02499         strcpy(filename,name);
02500         first_blk(filename); 
02501         strcpy(frame->name,filename);
02502         append_ima_extension(frame->name,InputIO.basic_io);
02503 
02504         strcpy(filename,frame->name);
02505 
02506         if (!exist(filename)) {
02507                 status = ERR_OPEN;
02508                 sprintf(errtext,"header_frame: frame %s",filename);
02509                 Handle_Error(errtext,status);
02510                 return(status);
02511         }
02512 
02513         switch(mode[0]) {
02514                 case 'I' : 
02515                         if (mode[1] == 'O')
02516                                 frame->iomode = (int)IO_MODE;
02517                         else
02518                                 frame->iomode = (int)I_MODE;
02519                         break;
02520                 case 'O' : frame->iomode = (int)O_MODE;
02521                         break;
02522                 default  : frame->iomode = (int)I_MODE;
02523                         break;
02524         }
02525         
02526     iomode = get_iomode_code(InputIO.basic_io,frame->iomode);
02527 
02528     switch (InputIO.basic_io) {
02529 
02530 #ifdef MIDAS
02531         case MIDAS_FORMAT :
02532                 status = SCFINF(filename,2,info);  
02533                 if (status == 0) {
02534                         status = SCFOPN(filename, info[1], 0, F_IMA_TYPE,
02535                     &(frame->imno));
02536                         nbaxes = RD_desc(frame,"NPIX",INT,2,&(frame->nx));
02537                     if(nbaxes != 2) {
02538                                 /* We opened a spectrum as an image, bad work */
02539                         status = ERR_OPEN; 
02540                                 break;
02541                         }
02542 
02543                         RD_desc(frame,"START",DOUBLE,2,&(frame->startx));
02544                         RD_desc(frame,"STEP",DOUBLE,2,&(frame->stepx));
02545                         RD_desc(frame,"IDENT",CHAR,lg_ident,frame->ident);
02546                         RD_desc(frame,"CUNIT",CHAR,lg_unit,frame->cunit);
02547                     frame->data_type = info[1];
02548                     frame->data_type = decode_datatype(InputIO.basic_io,frame->data_type);
02549 
02550                 }
02551                 break;
02552 #endif
02553 #ifdef IRAF
02554     case IRAF_FORMAT :
02555     case STSDAS_FORMAT :
02556                 len = strlen(filename);
02557         uimopn(filename,&iomode,&(frame->imno),&status,len);
02558         if (status != 0) 
02559                         break;
02560         uimgid(&(frame->imno),&int_datatype,&two_dim,&(frame->nx),&status);
02561                 frame->data_type = decode_datatype(InputIO.basic_io,(short)(int_datatype));
02562                 disable_user_warnings();
02563                 RD_desc(frame,"IDENT",CHAR,lg_ident,frame->ident);
02564                 restore_user_warnings();
02565                 break;
02566 #endif
02567 #ifdef FITS
02568         case FITS_A_FORMAT :
02569     case FITS_B_FORMAT :
02570         status =0;
02571         if (fits_open_file(&fptr,filename,iomode,&status)) {
02572                 status = ERR_ACCESS; break;
02573         }
02574         frame->external_info = (void *)fptr;
02575         if (fits_read_key(fptr, TINT,"NAXIS", &nbaxes,NULL, &status)) {
02576                 status = ERR_READ; break;
02577         }
02578         if (nbaxes != 2) {
02579                 status = ERR_IMA_HEAD; break;
02580         }
02581         if (fits_read_key(fptr, TINT,"BITPIX", &bitpix,NULL, &status)) {
02582                 status = ERR_READ; break;
02583         }
02584         int_datatype = (fptr->Fptr->tableptr)->tdatatype;
02585         frame->data_type = decode_datatype(InputIO.basic_io,(short)int_datatype);
02586         if (fits_read_key(fptr, TINT, "NAXIS1",
02587                 &(frame->nx), NULL, &status)) {
02588                 status = ERR_READ; break;
02589         }
02590         if (fits_read_key(fptr, TINT, "NAXIS2",
02591                 &(frame->ny), NULL, &status)) {
02592                 status = ERR_READ; break;
02593         }
02594         fits_read_key(fptr, TINT, "CRPIX1", &one, NULL, &status);
02595         if (status) { status = 0; one = 1; }
02596         fits_read_key(fptr, TDOUBLE, "CRVAL1", &(frame->startx), NULL, &status);
02597         if (status) { status = 0; frame->startx = (double)1; }
02598         fits_read_key(fptr, TDOUBLE, "CDELT1", &(frame->stepx), NULL, &status);
02599         if (status) { status = 0; frame->stepx = (double)1; }
02600         frame->startx -= (one-1)*frame->stepx;
02601         fits_read_key(fptr, TINT, "CRPIX2", &one, NULL, &status);
02602         if (status) { status = 0; one = 1; }
02603         fits_read_key(fptr, TDOUBLE, "CRVAL2", &(frame->starty), NULL, &status);
02604         if (status) { status = 0; frame->starty = (double)1; }
02605         fits_read_key(fptr, TDOUBLE, "CDELT2", &(frame->stepy), NULL, &status);
02606         if (status) { status = 0; frame->stepy = (double)1; }
02607         frame->starty -= (one-1)*frame->stepy;
02608                                                                                         break;
02609 #endif
02610         }
02611 
02612         if (status) {
02613                 sprintf(errtext,"header_frame: frame %s",filename);
02614                 status = get_tiger_errcode(frame->data_format,status);
02615                 Handle_Error(errtext,status);
02616         }
02617         else {
02618                 disable_user_warnings();
02619                 status = RD_desc(frame,"LHCUTS",FLOAT,4,cuts);
02620                 RD_desc(frame,"HISTORY",CHAR,lg_hist,frame->history);
02621                 restore_user_warnings();
02622 
02623                 frame->endx = frame->startx + (frame->nx -1)*frame->stepx;
02624                 frame->endy = frame->starty + (frame->ny -1)*frame->stepy;
02625                 if (status > 0) {
02626                         frame->min = cuts[2];
02627                         frame->max = cuts[3];
02628                 }
02629                 status = 0;
02630         }
02631         return(status);
02632 }
02633 /* --- Doxygen Function Comment -------------------------- */
02641 /* ------------------------------------------------------- */
02642 int 
02643 open_frame(IMAGE2D *frame, char *name, char *mode)              
02644 {
02645         char errtext[132], filename[lg_name+1];
02646         int status, nbaxes, iomode, int_datatype;
02647         int one=1;
02648         float cuts[4];
02649         int info[5];
02650 #ifdef IRAF
02651         int two_dim=2;
02652         int len;
02653 #endif
02654 #ifdef FITS
02655     fitsfile *fptr;
02656     int nbread;
02657         int npix;
02658         int group = 0;
02659 #endif
02660 
02661         memset(frame->ident,' ',lg_ident);
02662         frame->ident[lg_ident] = '\0';
02663         memset(frame->cunit,' ',lg_unit);
02664         frame->cunit[lg_unit] = '\0';
02665         memset(frame->history,' ',lg_hist);
02666         frame->history[lg_hist] = '\0';
02667         frame->external_info = 0;
02668         frame->file_type = IMA_TYPE;
02669         frame->data_format = InputIO.basic_io;
02670 
02671         strcpy(filename,name);
02672         first_blk(filename); 
02673         strcpy(frame->name,filename);
02674         append_ima_extension(frame->name,InputIO.basic_io);
02675 
02676         strcpy(filename,frame->name);
02677 
02678         if (!exist(filename)) { /* check if fil exists */
02679                 status = ERR_OPEN;
02680                 sprintf(errtext,"open_frame: frame %s",filename);
02681                 Handle_Error(errtext,status);
02682                 return(status);
02683         }
02684 
02685         switch(mode[0]) {
02686                 case 'I' : 
02687                         if (mode[1] == 'O')
02688                                 frame->iomode = (int)IO_MODE;
02689                         else
02690                                 frame->iomode = (int)I_MODE;
02691                         break;
02692                 case 'O' : frame->iomode = (int)O_MODE;
02693                         break;
02694                 default  : frame->iomode = (int)I_MODE;
02695                         break;
02696         }
02697         
02698     iomode = get_iomode_code(InputIO.basic_io,frame->iomode);
02699 
02700     switch (InputIO.basic_io) {
02701 
02702 #ifdef MIDAS
02703         case MIDAS_FORMAT :
02704                 status = SCFINF(filename,2,info);  
02705                 if (status == 0) {
02706                         status = SCIGET(filename, info[1], iomode, F_IMA_TYPE, 2, 
02707                     &nbaxes, &(frame->nx), &(frame->startx), &(frame->stepx),
02708                     frame->ident, frame->cunit, (char **)(&(frame->data)), 
02709                     &(frame->imno));
02710                     frame->data_type = info[1];
02711                     frame->data_type = decode_datatype(InputIO.basic_io,frame->data_type);
02712 
02713                     if(nbaxes!=2) /* We open a spectrum like an image , and that's not good */
02714                       status = ERR_OPEN; 
02715 
02716                 }
02717                 break;
02718 #endif
02719 #ifdef IRAF
02720     case IRAF_FORMAT :
02721     case STSDAS_FORMAT :
02722                 len = strlen(filename);
02723         uimopn(filename,&iomode,&(frame->imno),&status,len);
02724         if (status != 0) 
02725                         break;
02726         uimgid(&(frame->imno),&int_datatype,&two_dim,&(frame->nx),&status);
02727                 frame->data_type = decode_datatype(InputIO.basic_io,(short)(int_datatype));
02728         if (status != 0)
02729                         break;
02730                 alloc_frame_mem(frame, datatype);
02731                 switch(frame->data_type) {
02732                 case SHORT :
02733                         uigs2s(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
02734                                 frame->data.s_data,&status);
02735                         break;
02736                 case INT :
02737                 case LONG :
02738                         uigs2l(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
02739                                 frame->data.l_data,&status);
02740                         break;
02741                 case FLOAT :
02742                         uigs2r(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
02743                                 frame->data.f_data,&status);
02744                         break;
02745                 case DOUBLE :
02746                         uigs2d(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
02747                                 frame->data.d_data,&status);
02748                         break;
02749         }
02750                 disable_user_warnings();
02751                 RD_desc(frame,"IDENT",CHAR,lg_ident,frame->ident);
02752                 restore_user_warnings();
02753                 break;
02754 #endif
02755 #ifdef FITS
02756         case FITS_A_FORMAT :
02757     case FITS_B_FORMAT :
02758         status =0;
02759         if (fits_open_file(&fptr,filename,iomode,&status)) {
02760                 status = ERR_ACCESS; break;
02761         }
02762         frame->external_info = (void *)fptr;
02763         if (fits_read_key(fptr, TINT,"NAXIS", &nbaxes,NULL, &status)) {
02764                 status = ERR_READ; break;
02765         }
02766         if (nbaxes != 2) {
02767                 status = ERR_IMA_HEAD; break;
02768         }
02769         if (fits_read_key(fptr, TINT, "NAXIS1",
02770                 &(frame->nx), NULL, &status)) {
02771                 status = ERR_READ; break;
02772         }
02773         if (fits_read_key(fptr, TINT, "NAXIS2",
02774                 &(frame->ny), NULL, &status)) {
02775                 status = ERR_READ; break;
02776         }
02777         if (status == 0) {
02778                 fits_read_key(fptr, TINT, "CRPIX1", &one, NULL, &status);
02779                 if (status) { status = 0; one = 1; }
02780                 fits_read_key(fptr, TDOUBLE, "CRVAL1", &(frame->startx), NULL, &status);
02781                 if (status) { status = 0; frame->startx = (double)1; }
02782                 fits_read_key(fptr, TDOUBLE, "CDELT1", &(frame->stepx), NULL, &status);
02783                 if (status) { status = 0; frame->stepx = (double)1; }
02784                 frame->startx -= (one-1)*frame->stepx;
02785                 fits_read_key(fptr, TINT, "CRPIX2", &one, NULL, &status);
02786                 if (status) { status = 0; one = 1; }
02787                 fits_read_key(fptr, TDOUBLE, "CRVAL2", &(frame->starty), NULL, &status);
02788                 if (status) { status = 0; frame->starty = (double)1; }
02789                 fits_read_key(fptr, TDOUBLE, "CDELT2", &(frame->stepy), NULL, &status);
02790                 if (status) { status = 0; frame->stepy = (double)1; }
02791             frame->starty -= (one-1)*frame->stepy;
02792         }
02793         else
02794                 break;
02795 
02796         int_datatype = (fptr->Fptr->tableptr)->tdatatype;
02797         frame->data_type = decode_datatype(InputIO.basic_io,(short)int_datatype);
02798         if (frame->data_type == SHORT) {
02799                 if (fptr->Fptr->tableptr[1].tscale == 1 && fptr->Fptr->tableptr[1].tzero == 32768)
02800                         /* unsigned short !!! */
02801                         frame->data_type = LONG;
02802         }
02803 
02804         if (alloc_frame_mem(frame, frame->data_type) < 0) {
02805                 fits_close_file(fptr,&status);
02806                 status = ERR_ALLOC;
02807                 break;
02808         }
02809 
02810         npix = frame->nx*frame->ny;
02811         switch (frame->data_type) {
02812         case SHORT :
02813                 if (fits_read_img_sht(fptr,group,1L,npix,(short)0,
02814                         frame->data.s_data,&nbread,&status)) {
02815                         status = ERR_READ; break;
02816                 }
02817                 break;
02818         case LONG :
02819         case INT :
02820                 if (fits_read_img_lng(fptr,group,1L,npix,(int)0,
02821                         frame->data.l_data,&nbread,&status)) {
02822                         status = ERR_READ; break;
02823                 }
02824                 break;
02825         case FLOAT :
02826                 if (fits_read_img_flt(fptr,group,1L,npix,(float)0,
02827                         frame->data.f_data,&nbread,&status)) {
02828                         status = ERR_READ; break;
02829                 }
02830                 break;
02831         case DOUBLE :
02832                 if (fits_read_img_dbl(fptr,group,1L,npix,(double)0,
02833                         frame->data.d_data,&nbread,&status)) {
02834                         status = ERR_READ; break;
02835                 }
02836                 break;
02837         }
02838                 break;
02839 #endif
02840         }
02841 
02842         if (status) {
02843                 sprintf(errtext,"open_frame: frame %s",filename);
02844                 status = get_tiger_errcode(frame->data_format,status);
02845                 Handle_Error(errtext,status);
02846         }
02847         else {
02848                 disable_user_warnings();
02849                 status = RD_desc(frame,"LHCUTS",FLOAT,4,cuts);
02850                 RD_desc(frame,"HISTORY",CHAR,lg_hist,frame->history);
02851                 restore_user_warnings();
02852 
02853                 frame->endx = frame->startx + (frame->nx -1)*frame->stepx;
02854                 frame->endy = frame->starty + (frame->ny -1)*frame->stepy;
02855                 if (status <= 0) {
02856                         image_minmax(frame);
02857                 }
02858                 else {
02859                         frame->min = cuts[2];
02860                         frame->max = cuts[3];
02861                 }
02862                 status = 0;
02863         }
02864         return(status);
02865 }
02866 /* --- Doxygen Function Comment -------------------------- */
02872 /* ------------------------------------------------------- */
02873 int 
02874 close_frame(IMAGE2D *frame)                     /* close active frame */
02875 {
02876         char   errtext[132], filename[lg_name+1];
02877         int    stat, int_datatype;
02878         float  cuts[4];
02879 #ifdef IRAF
02880         int one=1;
02881 #endif
02882 #ifdef FITS
02883     fitsfile *fptr;
02884         int npix;
02885 #endif
02886 
02887         strcpy(filename,frame->name);
02888 
02889         if (frame->iomode == (int)I_MODE) {
02890                 switch (frame->data_format) {
02891 #ifdef MIDAS
02892                 case MIDAS_FORMAT :
02893                         stat = SCFCLO(frame->imno);
02894                         break;
02895 #endif
02896 #ifdef IRAF
02897                 case IRAF_FORMAT :
02898                 case STSDAS_FORMAT :
02899                 uimclo(&(frame->imno),&stat);
02900                 break;
02901 #endif
02902 #ifdef FITS
02903                 case FITS_A_FORMAT :
02904                 case FITS_B_FORMAT :
02905                         stat =0;
02906                         fptr = (fitsfile *)frame->external_info;
02907                         fits_close_file(fptr,&stat);
02908                         free_frame_mem(frame);
02909                         break;
02910 #endif
02911                 }
02912                 if (stat) {
02913                         sprintf(errtext,"close_frame: frame %s",filename);
02914                         stat = get_tiger_errcode(frame->data_format,stat);
02915                         Handle_Error(errtext,stat);
02916                 }
02917                 return(stat);
02918         }
02919 
02920         if (frame->data.d_data != NULL) {
02921                 image_minmax(frame);
02922 
02923                 cuts[0]=frame->min; cuts[2]=frame->min;
02924                 cuts[1]=frame->max; cuts[3]=frame->max;
02925                 stat = WR_desc(frame,"LHCUTS",FLOAT,4,cuts);
02926         }
02927 
02928         WR_history(frame, (Anyfile *)0);
02929 
02930         switch (frame->data_format) {
02931 #ifdef MIDAS
02932         case MIDAS_FORMAT :
02933                 stat = SCFCLO(frame->imno);
02934                 break;
02935 #endif
02936 #ifdef IRAF
02937         case IRAF_FORMAT :
02938         case STSDAS_FORMAT :
02939                 switch(frame->data_type) {
02940                 case SHORT :
02941                 uips2s(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
02942                                 frame->data.s_data,&stat);
02943                         break;
02944                 case INT :
02945                 case LONG :
02946                 uips2l(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
02947                                 frame->data.l_data,&stat);
02948                         break;
02949                 case FLOAT :
02950                 uips2r(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
02951                                 frame->data.f_data,&stat);
02952                         break;
02953                 case DOUBLE :
02954                 uips2d(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
02955                                 frame->data.d_data,&stat);
02956                         break;
02957                 }
02958                 if (stat == 0)  
02959                         uimclo(&(frame->imno),&stat);
02960                 free_frame_mem(frame);
02961         break;
02962 #endif
02963 #ifdef FITS
02964         case FITS_A_FORMAT :
02965     case FITS_B_FORMAT :
02966         stat = 0;
02967         fptr = (fitsfile *)frame->external_info;
02968         if (frame->iomode != (int)I_MODE) {
02969                 if (frame->data.d_data != NULL) {
02970                         int_datatype = get_datatype_code(OutputIO.basic_io,frame->data_type);
02971                         npix = frame->nx*frame->ny;
02972                         if (fits_write_img(fptr,int_datatype,1L,npix,
02973                                 frame->data.s_data,&stat)) {
02974                                 stat = ERR_WRIT;
02975                         }
02976                 }
02977         }
02978         if (! stat)
02979             fits_close_file(fptr,&stat);
02980         free_frame_mem(frame);
02981         break;
02982 #endif
02983         }
02984         if (stat) {
02985                 sprintf(errtext,"close_frame: frame %s",filename);
02986                 stat = get_tiger_errcode(frame->data_format,stat);
02987                 Handle_Error(errtext,stat);
02988         } else {
02989                 if(TK && (frame->iomode == O_MODE || frame->iomode == IO_MODE))
02990                 {
02991                         printf("@ N {%s}\n",filename);
02992                 }
02993         }
02994 
02995         return(stat);
02996 }
02997 /* --- Doxygen Function Comment -------------------------- */
03003 /* ------------------------------------------------------- */
03004 
03005 int 
03006 delete_frame(IMAGE2D *frame)            
03007 {
03008         int status;
03009         char errtext[132],filename[lg_name+1];
03010 #ifdef IRAF
03011         int len;
03012 #endif
03013 #ifdef FITS
03014     fitsfile *fptr;
03015     int iomode;
03016 #endif
03017         
03018         strcpy(filename,frame->name);
03019 
03020         switch (frame->data_format) {
03021 #ifdef MIDAS
03022         case MIDAS_FORMAT :
03023                 status = SCFDEL(filename);
03024                 break;
03025 #endif
03026 #ifdef IRAF
03027         case IRAF_FORMAT :
03028         case STSDAS_FORMAT :
03029                 len = strlen(filename);
03030                 uimdel(filename,&status);
03031                 break;
03032 #endif
03033 #ifdef FITS
03034         case FITS_A_FORMAT :
03035         case FITS_B_FORMAT :
03036                 status = 0;
03037                 if (frame->external_info == NULL) {
03038                         iomode = get_iomode_code(InputIO.basic_io,I_MODE);
03039                         fits_open_file(&fptr,filename,iomode,&status);
03040                         if (status)
03041                                 status = unlink(filename);
03042                         fits_delete_file(fptr, &status);
03043                 }
03044                 else {
03045                         fptr = (fitsfile *)frame->external_info;
03046                         if (status)
03047                                 status = unlink(filename);
03048                         else {
03049                                 fits_delete_file(fptr, &status);
03050                                 free_frame_mem(frame);
03051                         }
03052                 }
03053                 break;
03054 #endif
03055         }
03056         if (status)     {
03057                 sprintf(errtext,"delete_frame: frame %s",filename);
03058                 status = get_tiger_errcode(frame->data_format,status);
03059                 Handle_Error(errtext,status);
03060         }
03061         return(status);
03062 }
03063 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
03064                 routines to read/write values into image files are
03065                      given in ../incl/funcdef.h
03066 !     
03067 !.func                        RD_frame()
03068 !
03069 !.purp                  reads a frame value
03070 !.desc
03071 ! (type) value = RD_frame(image,x_pixel,y_pixel)
03072 !
03073 ! IMAGE2D *image;       image structure
03074 ! int x_pixel;          pixel to read along x axis
03075 ! int y_pixel;          pixel to read along y axis
03076 !.ed
03077 !     
03078 !.func                        WR_frame()
03079 !
03080 !.purp                  writes a frame value
03081 !.desc
03082 ! WR_frame(image,pixel_x,pixel_y,value)
03083 !
03084 ! IMAGE2D *image;       image structure
03085 ! int x_pixel;          pixel to write along x axis
03086 ! int y_pixel;          pixel to write along y axis
03087 ! (type) value;         value to write
03088 !.ed
03089 !     
03090 !.func                        RD_qframe()
03091 !
03092 !.purp                  reads a frame quality flag
03093 !.desc
03094 ! (type) value = RD_qframe(image,x_pixel,y_pixel)
03095 !
03096 ! IMAGE2D *image;       image structure
03097 ! int x_pixel;          pixel to read along x axis
03098 ! int y_pixel;          pixel to read along y axis
03099 !.ed
03100 !     
03101 !.func                        WR_qframe()
03102 !
03103 !.purp                  writes a frame quality flag
03104 !.desc
03105 ! WR_qframe(image,pixel_x,pixel_y,value)
03106 !
03107 ! IMAGE2D *image;       image structure
03108 ! int x_pixel;          pixel to write along x axis
03109 !.ed
03110 -----------------------------------------------------------------------*/
03111 /* --- Doxygen Function Comment -------------------------- */
03118                 /* @{ ***** Starting group30 ***** */
03129 /* ------------------------------------------------------- */
03130 int 
03131 create_table(TABLE *table, char *name, int nbrow, int nbcol, char mode, char *ident)
03132 {
03133         int TCTINI();
03134         char ok[10];
03135         char errtext[132], filename[lg_name+1]; 
03136         int iomode, end;
03137         short already_exist=0;
03138         int  status, nb_row=-1, nb_col=-1;
03139 #ifdef IRAF
03140         int param;
03141 #endif
03142 
03143 #ifdef FITS
03144         fitsfile *fptr;
03145         int hdutype;
03146         int one = 1;
03147         char **cttype = 0x0;
03148         char **ctform = 0x0;
03149         char **ctunit = 0x0;
03150         char *extname;
03151         char file[lg_name+1];
03152 #endif
03153 
03154         strcpy(filename,name);
03155         first_blk(filename);
03156         strcpy(table->name,filename);
03157         append_tbl_extension(table->name,OutputIO.basic_io);
03158         memset(table->ident,' ',lg_ident);
03159         table->ident[lg_ident] = '\0';
03160         table->file_type = TBL_TYPE;
03161         table->iomode = (int)O_MODE;
03162         table->data_format = InputIO.basic_io;
03163         table->external_info = 0;
03164         table->allrow = 0;
03165         table->row = 0;
03166         table->col = 0;
03167         table->sel_row = NULL;
03168                 
03169         if (nbrow > 0) nb_row = nbrow;
03170         if (nbcol > 0) nb_col = nbcol;
03171 
03172         strcpy(filename,table->name);
03173 
03174         if (exist(filename)) 
03175                 already_exist = 1;
03176 
03177         if (already_exist) {
03178                 switch(mode) {
03179                                         /* Warning      */
03180                 case 'W' :      
03181                         if (VERBOSE) {
03182                                 sprintf(errtext,"Table %s overwritten",name);
03183                                 print_warning(errtext);
03184                         }
03185                         break;
03186                                         /* Inquire before overwritting */
03187                 case 'I' :
03188                   if(ASK) {
03189                      if(!TK)
03190                      {
03191                         sprintf(errtext,"Do you want to overwrite table %s (y/n)? ",name);
03192                         print_msg(errtext);
03193                         scanf("%s",ok); upper_strg(ok);
03194                         if (ok[0] != 'Y') stop_by_user();
03195                         }
03196                         else
03197                                 confirme_erase(filename);
03198                    }
03199                   break;
03200                                         
03201                 case 'N' : 
03202                         sprintf(errtext,"create_table: table %s already exists",name);
03203                         Handle_Error(errtext,ERR_REN_TBL);
03204                         return(ERR_REN_TBL);
03205                         break;
03206 
03207                 case 'Q' : 
03208                         break;
03209                                         /* Unrecognize option */
03210                                         
03211                 default  : 
03212                     sprintf(errtext,"create_table : unknown option");
03213                         Handle_Error(errtext,ERR_BAD_PARAM);
03214                         return(ERR_BAD_PARAM);
03215                         break;          
03216                 }
03217                 delete_table(table);
03218         }
03219         
03220         table->select_flag = FALSE;
03221         table->file_type = TBL_TYPE;
03222         table->data_format = OutputIO.basic_io;
03223 
03224         iomode = get_iomode_code(OutputIO.basic_io,O_MODE);
03225         switch (table->data_format) {
03226 #ifdef MIDAS
03227         case MIDAS_FORMAT :
03228                 if (nb_row <= 0) nb_row = tble_nb_row;
03229                 if (nb_col <= 0) nb_col= -1;                    /* MIDAS default */
03230                 status= TCTINI(filename,F_TRANS,iomode,nb_col,nb_row,&(table->imno));
03231                 break;
03232 #endif
03233 #ifdef IRAF
03234         case IRAF_FORMAT :
03235         case STSDAS_FORMAT :
03236                 len = strlen(name);
03237                 uttinn(name,&(table->imno),&status, len);
03238                 if (status != 0) break;
03239                 param = TBMXCL; /* default maximum nber of columns */
03240                 if (nb_col <= 0) nb_col = 10;
03241                 utppti(&(table->imno),&param,&nb_col,&status);
03242                 if (status != 0) break;
03243                 uttcre(&(table->imno),&status);
03244                 break;
03245 #endif
03246 #ifdef FITS
03247         case FITS_A_FORMAT :
03248         case FITS_B_FORMAT :
03249                 if (table->data_format == FITS_A_FORMAT)
03250                         hdutype = ASCII_TBL;
03251                 else
03252                         hdutype = BINARY_TBL;
03253                 status = 0;
03254                 strcpy(file,filename);
03255                 extname = strchr(file,'[');
03256                 if (extname == NULL) {
03257                         fits_create_file(&fptr,file,&status);
03258                         fits_create_img(fptr,8,0,(long *)&one, &status);
03259                 }
03260                 else {
03261                         extname[0] = '\0';
03262                         extname++;
03263                         extname[strlen(extname)-1] = '\0';
03264 
03265                         if (!exist(file))
03266                                 fits_create_file(&fptr,file,&status);
03267                         else 
03268                                 fits_open_file(&fptr,file,iomode,&status);
03269                         if (status) {
03270                                 status = ERR_OPEN;
03271                                 break;
03272                         }
03273 
03274                         fits_movabs_hdu(fptr, 1, NULL, &status);
03275                         fits_movnam_hdu(fptr,hdutype,"",0,&status);
03276                         if (status) {
03277                                 status = 0;
03278                         }
03279                         else { /* delete current extension */
03280                                 fits_delete_hdu(fptr, &hdutype, &status);
03281                         }
03282                         fits_get_num_hdus(fptr,&end,&status);   /* reach end of file */
03283                         fits_movabs_hdu(fptr,end,NULL,&status);
03284 
03285                 }
03286                 table->external_info = (void *)fptr;
03287 
03288                 nb_row = MAX(0,nb_row);
03289                 if (table->data_format == FITS_A_FORMAT) {
03290 
03291                         if (fits_insert_atbl(fptr, 132, nb_row, 0, cttype,
03292                                 (long *)&one,ctform, ctunit, extname, &status)) {
03293                                 status = ERR_WRIT; break;
03294                         }
03295                 }
03296                 else {
03297                         if (fits_insert_btbl(fptr, nb_row, 0, cttype,
03298                                 ctform, ctunit, extname, 1, &status)) {
03299                                 status = ERR_WRIT; break;
03300                         }
03301                 }
03302 
03303                 if (status) {
03304                      status = ERR_WRIT; break;
03305                 }
03306                 else {
03307                         table->allrow = nb_row;
03308                 }
03309                 break;
03310 #endif
03311         }
03312         if (status) {
03313                 sprintf(errtext,"create_table: table %s",filename);
03314                 status = get_tiger_errcode(table->data_format,status);
03315                 Handle_Error(errtext,status);
03316                 return(status); 
03317         }
03318 
03319         if (ident != NULL) {
03320                 strcpy(table->ident,ident);
03321                 WR_desc(table,"COMMENT",CHAR,8,"        ");
03322                 status = WR_desc(table,"IDENT",CHAR,strlen(ident),table->ident); 
03323                 if (status) {
03324                         sprintf(errtext,"create_table: table %s",filename);
03325                         status = get_tiger_errcode(table->data_format,status);
03326                         Handle_Error(errtext,status);
03327                 }
03328         } 
03329         return(status); 
03330 }
03331 /* --- Doxygen Function Comment -------------------------- */
03339 /* ------------------------------------------------------- */
03340 int 
03341 open_table(TABLE *table, char *name, char *mode)
03342 {
03343         int allcol, allrow, nsort, status, iomode;
03344         char errtext[132], filename[lg_name+1];
03345         short already_exist=1;
03346 #ifdef IRAF
03347         int lens, tbnrow, tbncol;
03348 #endif
03349 #ifdef FITS
03350         fitsfile *fptr;
03351         int hdutype;
03352 #endif
03353 
03354         table->select_flag = FALSE;
03355     memset(table->ident,' ',lg_ident);
03356     table->ident[lg_ident] = '\0';
03357     memset(table->history,' ',lg_hist);
03358     table->history[lg_hist] = '\0';
03359         table->file_type = TBL_TYPE;
03360     table->data_format = InputIO.basic_io;
03361         table->external_info = 0;
03362         table->sel_row = NULL;
03363 
03364         strcpy(filename,name);
03365         first_blk(filename);
03366         strcpy(table->name,filename);
03367         append_tbl_extension(table->name,InputIO.basic_io);
03368 
03369         strcpy(filename,table->name);
03370 
03371         if (!exist(filename)) {
03372                 status = ERR_OPEN;
03373                 sprintf(errtext,"open_table: table %s",filename);
03374                 Handle_Error(errtext,status);
03375                 return(status);
03376         }
03377 
03378         switch(mode[0]) {
03379                 case 'I' : 
03380                         if (mode[1] == 'O')
03381                                 table->iomode = (int)IO_MODE;
03382                         else
03383                                 table->iomode = (int)I_MODE;
03384                         break;
03385                 case 'O' : table->iomode = (int)O_MODE;
03386                         break;
03387                 default  : table->iomode = (int)I_MODE;
03388                         break;
03389         }
03390 
03391         if (!exist(filename)) 
03392                 already_exist = 0;
03393 
03394         if (already_exist == 0) {
03395                 status = ERR_NOTBL;
03396                 sprintf(errtext,"open_table: %s doesn't exist",filename);
03397                 status = get_tiger_errcode(table->data_format,status);
03398                 Handle_Error(errtext,status);
03399                 return(status);
03400         }
03401 
03402         iomode = get_iomode_code(InputIO.basic_io,table->iomode);
03403 
03404         switch (InputIO.basic_io) {
03405 #ifdef MIDAS
03406         case MIDAS_FORMAT :
03407                 status = TCTOPN(filename,iomode, &(table->imno));
03408                 if (status == 0) {
03409                         status = TCIGET(table->imno,&(table->col), &(table->row),
03410                                 &nsort, &allcol, &allrow);
03411                 }
03412                 break;
03413 #endif
03414 #ifdef IRAF
03415         case IRAF_FORMAT :
03416         case STSDAS_FORMAT :
03417                 tbnrow = TBNROW;
03418                 tbncol = TBNCOL;
03419                 len = strlen(filename);
03420                 uttopn(filename,&iomode,&(table->imno),&status,len);
03421                 if (status == 0) {
03422                         utpgti(&(table->imno),&tbnrow, &(table->row),&status);
03423                         utpgti(&(table->imno),&tbncol, &(table->col),&status);
03424                 }
03425                 break;
03426 #endif
03427 #ifdef FITS
03428         case FITS_A_FORMAT :
03429     case FITS_B_FORMAT :
03430                 status = 0;
03431                 if (fits_open_file(&fptr, filename, iomode, &status)) {
03432                         status = ERR_OPEN;
03433                         break;
03434                 }
03435                 table->external_info = (void *)fptr;
03436                 if (fits_get_hdu_type(fptr, &hdutype, &status)) {
03437                         fits_close_file(fptr,&status);
03438                         status = ERR_BAD_HEAD;
03439                         break;
03440                 }
03441                 switch (hdutype) {
03442                         case ASCII_TBL :
03443                         case BINARY_TBL :
03444                                 break;
03445                         default:
03446                                 if (fits_movrel_hdu(fptr, 1, &hdutype, &status)) {
03447                                         fits_close_file(fptr,&status);
03448                                         status = ERR_BAD_HEAD;
03449                                 }
03450                         break;
03451                 }
03452 
03453                 if (hdutype == ASCII_TBL)
03454                         table->data_format = FITS_A_FORMAT;
03455                 else 
03456                         table->data_format = FITS_B_FORMAT;
03457                 table->col = fptr->Fptr->tfield;
03458                 if (fits_read_key(fptr, TINT, "NAXIS2", 
03459                                 &(table->row), NULL, &status)) {
03460                         fits_close_file(fptr,&status);
03461                         status = ERR_READ; break;
03462                 }
03463                 break;
03464 #endif
03465         }
03466         if (status) {
03467                 sprintf(errtext,"open_table: table %s",filename);
03468                 status = get_tiger_errcode(table->data_format,status);
03469                 Handle_Error(errtext,status);
03470         }
03471         else {
03472                 table->allrow = table->row;
03473                 disable_user_warnings();
03474                 RD_desc(table,"IDENT",CHAR,lg_ident,table->ident);
03475                 /* YC: remove HISTORY, as it is not used anymore */
03476                 /* RD_desc(table,"HISTORY",CHAR,lg_hist,table->history); */
03477                 restore_user_warnings();
03478         }
03479         return(status);             
03480 }
03481 /* --- Doxygen Function Comment -------------------------- */
03487 /* ------------------------------------------------------- */
03488 
03489 int 
03490 close_table(TABLE *table)
03491 {
03492         int     status, l, nbl, sel=0,iomode;
03493         int     *selline;
03494         char select_mode[133], filename[lg_name+1];
03495 
03496 #ifdef FITS
03497         fitsfile *fptr;
03498 #endif
03499 
03500         strcpy(filename,table->name);
03501 
03502         if (table->iomode != I_MODE) 
03503                 WR_history(table, (Anyfile *)0);
03504 
03505         switch(table->data_format) {
03506 #ifdef MIDAS
03507         case MIDAS_FORMAT :
03508                 if (table->select_flag) {
03509 
03510                 /* if selected, need to close and reopen the table with
03511                 previous selecction since Midas unselect the whole table 
03512                 when a column is created */
03513 
03514                         sel = 1;
03515                         selline = (int *)malloc(table->row*sizeof(int));
03516                         nbl = table->row;
03517                         status = RD_desc(table,"TSELTABL",CHAR,132,select_mode);
03518                         if (status > 0)
03519                                 for (l=0; l<nbl; l++) 
03520                                         TCSGET(table->imno,l+1,&(selline[l]));
03521                 }
03522                 status = TCTCLO(table->imno);
03523                 if (sel) {
03524                         iomode = get_iomode_code(table->data_format,table->iomode);
03525                         TCTOPN(filename, iomode, &(table->imno));
03526                         for (l=0; l<nbl; l++) 
03527                                 TCSPUT(table->imno,l+1,&(selline[l]));
03528                         status = WR_desc(table,"TSELTABL",CHAR,132,select_mode);
03529                         free(selline);
03530                         status = TCTCLO(table->imno);
03531                 }
03532                 break;
03533 #endif
03534 #ifdef IRAF
03535         case IRAF_FORMAT :
03536         case STSDAS_FORMAT :
03537                 uttclo(&(table->imno),&status);
03538                 break;
03539 #endif
03540 #ifdef FITS
03541         case FITS_A_FORMAT :
03542     case FITS_B_FORMAT :
03543         status = 0;
03544         fptr = (fitsfile *)table->external_info;
03545         fits_close_file(fptr,&status);
03546         break;
03547 #endif
03548         }
03549 
03550         if (status) {
03551                 status = get_tiger_errcode(table->data_format,status);
03552                 Handle_Error("Close_table",status);
03553                 return(status);
03554         } else {
03555                 if(TK && (table->iomode == O_MODE || table->iomode == IO_MODE))
03556                 {               
03557                         printf("@ N {%s}\n",filename);
03558                 }
03559         }
03560         if (table->select_flag) {
03561                 if (table->sel_row != NULL) 
03562                         free((char *)table->sel_row);
03563         }
03564 
03565         return(status);             
03566 }
03567 
03568 /* --- Doxygen Function Comment -------------------------- */
03577 /* ------------------------------------------------------- */
03578 
03579 int handle_select_flag(TABLE *table, char mode, char *selection)
03580 {
03581         int status, select, nselect, i, no_col;
03582         int *selected;
03583         char select_mode[133], errtext[132];
03584 
03585         select_mode[0] = 0;
03586 
03587         disable_user_warnings();
03588         status = RD_desc(table,"TSELTABL",CHAR,132,select_mode);
03589         restore_user_warnings();
03590 
03591         if ((status > 0) && (select_mode[0] != '-')) {
03592                 first_blk(select_mode);
03593                 if (mode != 'Q') {
03594                         sprintf(errtext,"Selection flag enable %s",
03595                        select_mode);
03596                         print_warning(errtext);
03597                 }
03598                 table->select_flag = 1;         
03599                 table->sel_row = (int *)calloc(table->row,sizeof(int));
03600         }
03601         status = 0;
03602                         /* cross reference for read/write operations */
03603 
03604         if (table->select_flag) {
03605                 switch (table->data_format) {
03606 #ifdef MIDAS
03607                 case MIDAS_FORMAT :
03608                         for (nselect=0,i=0; i< table->row; i++) {
03609                                 status = TCSGET(table->imno,i+1,&select);
03610                                 if (select == TRUE) {
03611                                         table->sel_row[nselect] = i;
03612                                         nselect++;
03613                                 }
03614                         }
03615                         table->row = nselect;
03616                         break;
03617 #endif
03618 #ifdef IRAF
03619                 case IRAF_FORMAT :
03620                 case STSDAS_FORMAT :
03621 #endif
03622 #ifdef FITS
03623         case FITS_A_FORMAT :
03624         case FITS_B_FORMAT :
03625 #endif
03626 #if defined(IRAF) || defined(FITS)
03627                         no_col = get_col_ref(table,"TSELTABL");
03628                         if (no_col < 0) {
03629                                 table->select_flag = 0;         
03630                                 free(table->sel_row);
03631                                 break;
03632                         }
03633                         selected = (int *)calloc(table->allrow,sizeof(int));
03634                         table->select_flag = 0;         
03635                         RD_col(table,no_col,selected);
03636                         table->select_flag = 1;         
03637                         for (nselect=0,i=0; i<table->row; i++) {
03638                                 if (selected[i] == TRUE) {
03639                                         table->sel_row[nselect] = i;
03640                                         nselect++;
03641                                 }
03642                         }
03643                         table->row = nselect;
03644                         free((char *)selected);
03645                         break;
03646 #endif
03647                 default :
03648                         return(UNKNOWN);
03649                         break;
03650                 }
03651         }
03652         if (selection != NULL)
03653                 strcpy(selection,select_mode);
03654         return(status);
03655 }
03656 
03657 /* --- Doxygen Function Comment -------------------------- */
03665 /* ------------------------------------------------------- */
03666 
03667 int 
03668 write_selection(TABLE *table,int *sel, char *selection)
03669 {
03670   int status = 0;
03671   int nselect,no_col,i;
03672 
03673   /* on ecrit le label de la selection */
03674 
03675   /* disable_user_warnings(); */
03676   status = WR_desc(table,"TSELTABL",CHAR,strlen(selection),selection);
03677   /* restore_user_warnings(); */
03678 
03679   if(table->sel_row!=NULL)
03680     free(table->sel_row);
03681 
03682   table->sel_row = (int *)calloc(table->allrow,sizeof(int));
03683 
03684   switch (table->data_format) {
03685 #ifdef MIDAS
03686   case MIDAS_FORMAT :
03687     for (nselect=0,i=0; i<table->allrow; i++) 
03688       {
03689         status = TCSPUT(table->imno,i+1,&(sel[i]));
03690         if (sel[i] == TRUE) 
03691           {
03692             table->sel_row[nselect] = i;
03693             nselect++;
03694           }
03695       }
03696     
03697     break;
03698 #endif
03699 #ifdef IRAF
03700   case IRAF_FORMAT :
03701   case STSDAS_FORMAT :
03702 #endif
03703 #ifdef FITS
03704   case FITS_A_FORMAT :
03705   case FITS_B_FORMAT :
03706 #endif
03707 #if defined(IRAF) || defined(FITS)
03708     disable_user_warnings();
03709     no_col = get_col_ref(table,"TSELTABL");
03710     restore_user_warnings();
03711     if (no_col < 0) {
03712         no_col = create_col(table,"TSELTABL",INT,'N',"I1",NULL);
03713       if (no_col < 0) {
03714          free(table->sel_row);
03715          table->sel_row = NULL;
03716          break;
03717       }
03718     }
03719 
03720     for (nselect=0,i=0; i<table->allrow; i++) {
03721       WR_tbl(table,i,no_col,sel+i);
03722       if (sel[i] == TRUE) {
03723                  table->sel_row[nselect] = i;
03724                  nselect++;
03725           }
03726     }
03727     break;
03728 #endif
03729   default :
03730     return(UNKNOWN);
03731     break;
03732   }
03733   table->row = nselect;
03734   table->select_flag = 1; 
03735   return(status);
03736 }
03737 
03738 /* --- Doxygen Function Comment -------------------------- */
03745 /* ------------------------------------------------------- */
03746 int 
03747 get_col_ref (TABLE *table, char *label)                 
03748 {
03749         int  status, num, TCLSER();
03750         char errtext[132], *column_label;
03751 
03752 #ifdef IRAF
03753         int one=1;
03754 #endif
03755 
03756 #ifdef FITS
03757         fitsfile *fptr;
03758         char key[FLEN_KEYWORD+1];
03759 #endif
03760 
03761         column_label = malloc((strlen(label)+1)*sizeof(char));
03762     if (column_label == NULL) {
03763                 status = ERR_NOCOL;
03764                 return(status);
03765         }
03766         strcpy(column_label,label);
03767 
03768         if (sscanf(column_label,"#%d",&num) == 1) {
03769                 switch (table->data_format) {
03770 #ifdef MIDAS
03771                 case MIDAS_FORMAT :
03772                         break;
03773 #endif
03774 #ifdef IRAF
03775                 case IRAF_FORMAT :
03776                 case STSDAS_FORMAT :
03777                         num = tbcnum(&(table->imno),&num);
03778                         break;
03779 #endif
03780 #ifdef FITS
03781                 case FITS_A_FORMAT :
03782                 case FITS_B_FORMAT :
03783                         status = 0;
03784                         upper_strg(column_label);
03785                         fptr = (fitsfile *)table->external_info;
03786                         fits_get_colnum(fptr,0,column_label,&num,&status);
03787                         if (num > 0) {
03788                                 status = get_col_name(table, num, key);
03789                                 if (strcmp(column_label,key) != 0) status = -1;
03790                         }
03791                         if (status) {
03792                                 num = -1; status = 0;
03793                         };
03794                         break;
03795 #endif
03796                 }
03797         }
03798         else {
03799                 switch (table->data_format) {
03800 #ifdef MIDAS
03801                 case MIDAS_FORMAT :
03802                         status = TCCSER(table->imno, label, &num);
03803                         break;
03804 #endif
03805 #ifdef IRAF
03806                 case IRAF_FORMAT :
03807                 case STSDAS_FORMAT :
03808                         utcfnd(&(table->imno),label,&one,&num,&status,strlen(label));
03809                         break;
03810 #endif
03811 #ifdef FITS
03812                 case FITS_A_FORMAT :
03813                 case FITS_B_FORMAT :
03814                         status = 0;
03815                         upper_strg(column_label);
03816                         fptr = (fitsfile *)table->external_info;
03817                         fits_get_colnum(fptr,0,column_label,&num,&status);
03818                         if (num > 0) {
03819                                 status = get_col_name(table, num, key);
03820                                 if (strcmp(column_label,key) != 0) status = -1;
03821                         }
03822                         /* compatibility with old stuff */
03823                         if (status) {
03824                                 if (strcmp(column_label,E3D_COL_ID) == 0) {
03825                                         status = 0;
03826                                         fits_get_colnum(fptr,0,LAB_COL_NO,&num,&status);
03827                                 }
03828                                 if (strcmp(column_label,LAB_COL_NO) == 0) {
03829                                         status = 0;
03830                                         fits_get_colnum(fptr,0,E3D_COL_ID,&num,&status);
03831                                 }
03832                                 if (strcmp(column_label,E3D_COL_XPOS) == 0) {
03833                                         status = 0;
03834                                         fits_get_colnum(fptr,0,"A",&num,&status);
03835                                         if (status) {
03836                                                 status = 0;
03837                                                 fits_get_colnum(fptr,0,LAB_COL_XLD,&num,&status);
03838                                         }
03839                                 }
03840                                 if (strcmp(column_label,LAB_COL_XLD) == 0) {
03841                                         status = 0;
03842                                         fits_get_colnum(fptr,0,"A",&num,&status);
03843                                 }
03844                                 if (strcmp(column_label,E3D_COL_YPOS) == 0) {
03845                                         status = 0;
03846                                         fits_get_colnum(fptr,0,"D",&num,&status);
03847                                         if (status) {
03848                                                 status = 0;
03849                                                 fits_get_colnum(fptr,0,LAB_COL_XLD,&num,&status);
03850                                         }
03851                                 }
03852                                 if (strcmp(column_label,LAB_COL_YLD) == 0) {
03853                                         status = 0;
03854                                         fits_get_colnum(fptr,0,"D",&num,&status);
03855                                 }
03856                         }
03857                         if (status) {
03858                                 num = -1; status = 0;
03859                         };
03860                         break;
03861 #endif
03862                 }
03863         }
03864         free(column_label);
03865         if (num < 0) {
03866                 status = ERR_NOCOL;
03867         }
03868 
03869         if (status) {
03870                 sprintf(errtext,"get_col_ref: table %s column %s", table->name, label);
03871                 status = get_tiger_errcode(table->data_format,status);
03872                 Handle_Error(errtext,status);
03873                 return(status);
03874         }
03875         return(num);
03876 }
03877 
03878 /* --- Doxygen Function Comment -------------------------- */
03886 /* ------------------------------------------------------- */
03887 int get_col_name (TABLE *table, int no_col, char *label)
03888 {
03889         int  status;
03890 #ifdef IRAF
03891         int status;
03892         char unit[lg_unit+1], format[10];
03893         int nocol;
03894 #endif
03895 #ifdef FITS
03896         fitsfile *fptr;
03897         char key[FLEN_KEYWORD+1];
03898 #endif
03899 
03900 
03901         switch (table->data_format) {
03902 #ifdef MIDAS
03903         case MIDAS_FORMAT :
03904                 status = TCLGET(table->imno, no_col, label);
03905                 break;
03906 #endif
03907 #ifdef IRAF
03908         case IRAF_FORMAT :
03909         case STSDAS_FORMAT :
03910                 nocol = no_col;
03911                 utcinf(&(table->imno),&nocol,label,unit,format,&dtype,&status,
03912                         132,strlen(unit),5);
03913                 break;
03914 #endif
03915 #ifdef FITS
03916         case FITS_A_FORMAT :
03917     case FITS_B_FORMAT :
03918                 status = 0;
03919                 fptr = (fitsfile *)table->external_info;
03920                 sprintf(key,"TTYPE%d",no_col);
03921                 fits_read_key_str(fptr,key,label,NULL,&status);
03922                 break;
03923 #endif
03924         }       
03925 
03926     if (status) {
03927                 status = get_tiger_errcode(table->data_format,status);
03928         Handle_Error("get_col_name",status);
03929     }
03930         return(status);
03931 }
03932 /* --- Doxygen Function Comment -------------------------- */
03942 /* ------------------------------------------------------- */
03943 
03944 int 
03945 get_col_info (TABLE *table, int no_col, int *dtype, char *format, char *unit)
03946 {
03947     int  status, TCFGET(), len;
03948     char c;
03949 
03950 #ifdef IRAF
03951     int nocol;
03952     char colname[132];
03953 #endif
03954 
03955 #ifdef FITS
03956         fitsfile *fptr;
03957         char key[FLEN_KEYWORD+1];
03958         int repeat, width;
03959 #endif
03960 
03961         switch (table->data_format) {
03962 #ifdef MIDAS
03963         case MIDAS_FORMAT :
03964                 status = TCFGET(table->imno, no_col, format, &len, dtype);
03965                 if (status == 0) 
03966                         status = TCUGET(table->imno, no_col, unit);
03967                 break;
03968 #endif
03969 #ifdef IRAF
03970         case IRAF_FORMAT :
03971         case STSDAS_FORMAT :
03972                 nocol = no_col;
03973                 utcinf(&(table->imno),&nocol,colname,unit,format,dtype,&status,
03974                         132,strlen(unit),5);
03975                 break;
03976 #endif
03977 #ifdef FITS
03978         case FITS_A_FORMAT :
03979     case FITS_B_FORMAT :
03980                 status = 0;
03981                 fptr = (fitsfile *)table->external_info;
03982                 fits_get_coltype(fptr,no_col,dtype, (long *)&repeat, (long *)&width,&status);
03983                         /* variable length column */
03984                 if (*dtype < 0) 
03985                         *dtype *= -1;
03986                 if (format != NULL) {
03987                         sprintf(key,"TFORM%d",no_col);
03988                         fits_read_key_str(fptr,key,format,NULL,&status);
03989                         if ( table->data_format == FITS_B_FORMAT) {
03990                         if (strlen(format) > 1)
03991                                 sscanf(format,"%d%c",&width,&c);
03992                         else
03993                                 c = format[0];
03994                         switch(c) {
03995                                 case 'A' :
03996                                         sprintf(format,"A%d",width);
03997                                         break;
03998                                 case 'I' :
03999                                 case 'J' :
04000                                         strcpy(format,"I4");
04001                                         break;
04002                                 case 'E' :
04003                                 case 'F' :
04004                                         strcpy(format,"F7.4");
04005                                         break;
04006                                 case 'D' :
04007                                         strcpy(format,"F9.6");
04008                                         break;
04009                                 }
04010                         }
04011                 }
04012                 if (unit != NULL) {
04013                         sprintf(key,"TUNIT%d",no_col);
04014                         fits_read_key_str(fptr,key,unit,NULL,&status);
04015                         if (status) {
04016                                 status = 0; strcpy(unit,"none");
04017                         }
04018                 }
04019                 break;
04020 #endif
04021         }       
04022 
04023     if (status) {
04024                 status = get_tiger_errcode(table->data_format,status);
04025         Handle_Error("get_col_info",status);
04026     }
04027         *dtype = decode_datatype(table->data_format,(short)(*dtype));
04028     return(status);
04029 }
04030 /* --- Doxygen Function Comment -------------------------- */
04041 /* ------------------------------------------------------- */
04042 
04043 int 
04044 create_col(TABLE *table, char *colname, short type, char mode, char *format, char *user_unit)   
04045 {
04046         char *column_label;
04047         char errtext[132], unit[132], col_format[5];
04048         int  i, status, len, ttype, num;
04049 
04050 #ifdef IRAF
04051         int one=1;
04052 #endif
04053 
04054 #ifdef FITS
04055         fitsfile *fptr;
04056         int width;
04057         char tform[FLEN_KEYWORD+1];
04058         char desc[FLEN_KEYWORD+1];
04059 #endif
04060 
04061         column_label = malloc((strlen(colname)+1)*sizeof(char));
04062         if (column_label == NULL) {
04063                 status = ERR_NOCOL;
04064                 return(status);
04065         }
04066         strcpy(column_label,colname);
04067         first_blk(column_label);
04068 
04069         if (strlen(column_label) > 16) {
04070                 status = ERR_BAD_COL;
04071                 sprintf(errtext, "create_col : file= %s desc=%s", table->name,colname);
04072                 Handle_Error(errtext, status);
04073                 return(status);
04074         }
04075         if (user_unit == NULL) 
04076                 strcpy(unit,"None given");
04077         else
04078                 strcpy(unit,user_unit);
04079 
04080         len = 1;
04081 
04082         switch(type) {
04083 
04084                 case CHAR : 
04085                         ttype = get_datatype_code(table->data_format,CHAR);
04086                         strcpy(col_format,format);
04087                         status = sscanf(col_format,"A%d",&len);
04088                         if (status == 1)  {
04089                                 status = 0;
04090                                 if ((table->data_format == IRAF_FORMAT) 
04091                                 || (table->data_format == STSDAS_FORMAT))
04092                                         ttype = -len;
04093                         }
04094                         break;
04095                 case SHORT :
04096                 case INT :
04097                 case LONG :
04098                 case FLOAT :
04099                 case DOUBLE :
04100                         status = 0;
04101                         ttype = get_datatype_code(table->data_format,type);
04102                         break;
04103                 default : 
04104                         status = ERR_BAD_TYPE;
04105                         break;
04106         }
04107         if (status != 0) {
04108                 sprintf(errtext,"create_col: table %s col %s\n", table->name, column_label);
04109                 status = get_tiger_errcode(table->data_format,status);
04110                 Handle_Error(errtext,status);
04111                 return(status);
04112         } 
04113         /* does column exist ? */
04114         disable_user_warnings();
04115         num = get_col_ref(table,column_label);
04116         restore_user_warnings();
04117         if (num > 0) { /* column exists */
04118 
04119                 switch(mode) {
04120                                                 /* Overwrite column */
04121                 case 'O' :
04122                         if (format != NULL) { /* new format */
04123                                 switch (table->data_format) {
04124 #ifdef MIDAS
04125                                 case MIDAS_FORMAT :     
04126                                         status = TCFPUT(table->imno,num,format);
04127                                         break;
04128 #endif
04129 #ifdef IRAF
04130                                 case IRAF_FORMAT :      
04131                                 case STSDAS_FORMAT :    
04132                                         utcfmt(&(table->imno),&num,format,&status,strlen(format));
04133                                         break;
04134 #endif
04135 #ifdef FITS
04136                                 case FITS_A_FORMAT :    
04137                                 case FITS_B_FORMAT :
04138                         break;
04139 #endif
04140                                 }
04141                                 if (status) {   
04142                                         sprintf(errtext,"create_col: table %s col %s\n",
04143                            table->name, column_label);
04144                                         status = get_tiger_errcode(table->data_format,status);
04145                                 Handle_Error(errtext,status);
04146                                         return(status);
04147                                 }
04148                         }                       /* new unit */
04149                         if (user_unit != NULL) {
04150                                 switch (table->data_format) {
04151 #ifdef MIDAS
04152                                 case MIDAS_FORMAT :     
04153                                         status = TCUPUT(table->imno,num,unit);
04154                                         break;
04155 #endif
04156 #ifdef IRAF
04157                                 case IRAF_FORMAT :      
04158                                 case STSDAS_FORMAT :    
04159                                         utcnit(&(table->imno),&num,unit,&status,strlen(unit));
04160                                         break;
04161 #endif
04162 #ifdef FITS
04163                                 case FITS_A_FORMAT :    
04164                         case FITS_B_FORMAT :
04165                                         break;
04166 #endif
04167                                 }
04168                                 if (status) {   
04169                                         sprintf(errtext,"create_col : table %s col %s \n",
04170                           table->name, column_label);
04171                                         status = get_tiger_errcode(table->data_format,status);
04172                                 Handle_Error(errtext,status);
04173                                         return(status);
04174                                 }
04175                         }
04176                         free(column_label);
04177                         return(num);
04178                         break;
04179 
04180                                                 /* Rewrite */
04181                 case 'R' :              
04182                         for (i=0; i<table->row; i++)
04183                                 WR_null(table,i,num);
04184                         free(column_label);
04185                         return(num);
04186                         break;
04187 
04188                                                 /* Column must be new */
04189                 case 'N' : 
04190                         sprintf(errtext,"create_col: table %s col %s already exist\n",
04191                         table->name, column_label);
04192                         status = get_tiger_errcode(table->data_format,status);
04193                         Handle_Error(errtext,ERR_REN_TBL);
04194                         return(ERR_REN_TBL);
04195                         break;
04196 
04197                                         /* Unrecognize option */
04198                 default  : 
04199                         sprintf(errtext,"create_col: table %s col %s Unknown option\n",
04200                   table->name, column_label);
04201             Handle_Error(errtext,ERR_BAD_PARAM);
04202                         return(ERR_BAD_PARAM);
04203                         break;          
04204                 }
04205         } 
04206         switch (table->data_format) { /* create new column */
04207 #ifdef MIDAS
04208         case MIDAS_FORMAT :     
04209                 if (type != CHAR) 
04210                         len = 1;
04211                 status = TCCINI(table->imno,ttype,len,format,unit,column_label,&num);
04212                 break;
04213 #endif
04214 #ifdef IRAF
04215         case IRAF_FORMAT :      
04216         case STSDAS_FORMAT :    
04217                 utcdef(&(table->imno),column_label,unit,format,&ttype,&one,&num,&status,
04218                         strlen(column_label),strlen(unit),strlen(format));
04219                 break;
04220 #endif
04221 #ifdef FITS
04222 
04223         case FITS_A_FORMAT :    
04224                 status = 0;
04225                 upper_strg(column_label);
04226                 fptr = (fitsfile *)table->external_info;
04227                 num = table->col +1;
04228                 fits_insert_col(fptr,num,column_label,format,&status);
04229                 sprintf(desc,"TUNIT%d",num);
04230             WR_desc(table,desc,CHAR,lg_unit,unit);
04231                 sprintf(desc,"TDISP%d",num);
04232             WR_desc(table,desc,CHAR,FLEN_KEYWORD,format);
04233                 break;
04234 
04235     case FITS_B_FORMAT :
04236                 status = 0;
04237                 upper_strg(column_label); 
04238                 fptr = (fitsfile *)table->external_info;
04239                 num = table->col +1;
04240                 switch(type) {
04241                 case CHAR :
04242                         if (sscanf(format,"A%d",&width) > 0)
04243                                 sprintf(tform,"%dA",width);
04244                         else
04245                                 strcpy(tform,format);
04246                         break;
04247                 case SHORT :
04248                         strcpy(tform,"I");
04249                         break;
04250                 case LONG :
04251                 case INT :
04252                         strcpy(tform,"J");
04253                         break;
04254                 case FLOAT :
04255                         strcpy(tform,"E");
04256                         break;
04257                 case DOUBLE :
04258                         strcpy(tform,"D");
04259                         break;
04260                 }
04261                 fits_insert_col(fptr,num,column_label,tform,&status);
04262                 sprintf(desc,"TUNIT%d",num);
04263             WR_desc(table,desc,CHAR,lg_unit,unit);
04264                 sprintf(desc,"TDISP%d",num);
04265             WR_desc(table,desc,CHAR,FLEN_KEYWORD,format);
04266                 break;
04267 #endif
04268         }
04269         if (num < 0) status = -1;
04270 
04271         free(column_label);
04272         if (status) {
04273                 status = get_tiger_errcode(table->data_format,status);
04274                 Handle_Error("create_col",status);
04275                 return(status); 
04276         }
04277         else
04278                 table->col++;
04279         return(num);
04280 }
04281 
04282 /* --- Doxygen Function Comment -------------------------- */
04289 /* ------------------------------------------------------- */
04290 
04291 int 
04292 delete_col (TABLE *table,int nocol)                     
04293 {
04294         int  status=0 , ncol;
04295 #ifdef IRAF
04296         int i;
04297 #endif
04298 #ifdef FITS
04299         fitsfile *fptr;
04300 #endif
04301 
04302 
04303         switch (table->data_format) {
04304 #ifdef MIDAS
04305         case MIDAS_FORMAT :
04306                 status = TCCDEL(table->imno, nocol, &ncol);
04307                 break;
04308 #endif
04309 #ifdef IRAF
04310         case IRAF_FORMAT :
04311         case STSDAS_FORMAT :
04312                 for (i=0; i<table->allrow && (!status); i++)
04313                         status = WR_null(table,i,nocol);
04314                 break;
04315 #endif
04316 #ifdef FITS
04317         case FITS_A_FORMAT :
04318     case FITS_B_FORMAT :
04319                 status = 0;
04320                 fptr = (fitsfile *)table->external_info;
04321                 fits_delete_col(fptr,nocol,&status);
04322                 break;
04323 #endif
04324         }
04325         if (status) {
04326                 status = get_tiger_errcode(table->data_format,status);
04327                 Handle_Error("Delete_col",status);
04328         }
04329         else
04330                 table->col--;
04331         return(status);
04332 }
04333 /* --- Doxygen Function Comment -------------------------- */
04340 /* ------------------------------------------------------- */
04341 
04342 int 
04343 delete_row (TABLE *table,int nrow)                      
04344 {
04345         int  status, true_row=nrow+1;
04346 #ifdef FITS
04347         fitsfile *fptr;
04348 #endif
04349 
04350 
04351         if (table->select_flag) 
04352                 true_row = table->sel_row[nrow]+1;
04353 
04354         switch (table->data_format) {
04355 #ifdef MIDAS
04356         case MIDAS_FORMAT :
04357                 status = TCRDEL(table->imno, true_row);
04358                 break;
04359 #endif
04360 #ifdef IRAF
04361         case IRAF_FORMAT :
04362         case STSDAS_FORMAT :
04363                 status = ERR_NOIMPL;
04364                 break;
04365 #endif
04366 #ifdef FITS
04367         case FITS_A_FORMAT :
04368     case FITS_B_FORMAT :
04369                 status = 0;
04370                 fptr = (fitsfile *)table->external_info;
04371                 fits_delete_rows(fptr,true_row,1L,&status);
04372                 break;
04373 #endif
04374         }
04375         if (status) {
04376                 status = get_tiger_errcode(table->data_format,status);
04377                 Handle_Error("Delete_row",status);
04378         }
04379         return(status);
04380 }
04381 
04382 /* --- Doxygen Function Comment -------------------------- */
04391 /* ------------------------------------------------------- */
04392 
04393 int 
04394 RD_tbl(TABLE *table, int row, int col, void *value)
04395 {
04396         int  null=0, status, true_row=row+1, len, dtype, lval;
04397         char format[20], unit[lg_unit+1], err_text[132];
04398         char *pt_value = (char *)value;
04399         int type;
04400 
04401 #ifdef IRAF
04402         int one=1;
04403 #endif
04404 #ifdef FITS
04405         fitsfile *fptr;
04406         int int_datatype;
04407         char **str_val;
04408 #endif
04409 
04410         if (table->select_flag) 
04411                 true_row = table->sel_row[row]+1;
04412 
04413         status = get_col_info(table, col, &dtype, format, unit);        
04414     if (status) {
04415         sprintf(err_text,"RD_tbl table %s col #%d row #%d",
04416                                         table->name, col,true_row);
04417                 status = get_tiger_errcode(table->data_format,status);
04418         Handle_Error(err_text,status);
04419         return(status);
04420     }
04421 
04422     switch (table->data_format) {
04423 #ifdef MIDAS
04424     case MIDAS_FORMAT :
04425 
04426                 switch (dtype) {
04427         
04428                 case CHAR :
04429                         status = TCFGET(table->imno,col,format,&len,&type);
04430                         if (! status) {
04431                                 status = TCERDC(table->imno,true_row,col,pt_value,&null);
04432                                 pt_value[len] = '\0';
04433                         }
04434                         break;
04435                 case SHORT :    
04436                         status = TCERDI(table->imno,true_row,col,&lval,&null);
04437                         *((short *)pt_value) = (short)lval;
04438                         break;
04439                 case INT :      
04440                 case LONG :     
04441                         status = TCERDI(table->imno,true_row,col,(int *)pt_value,&null);
04442                         break;
04443                 case FLOAT :
04444                         status = TCERDR(table->imno,true_row,col,(float *)pt_value,&null);
04445                         break;
04446                 case DOUBLE :
04447                         status = TCERDD(table->imno,true_row,col,(double *)pt_value,&null);
04448                         break;
04449                 }
04450                 break;
04451 #endif
04452 #ifdef IRAF
04453     case IRAF_FORMAT :
04454     case STSDAS_FORMAT :
04455 
04456                 switch (dtype) {
04457         
04458         case CHAR :
04459                         sscanf(format,"A%d",&len);
04460                         utrgtt(&(table->imno),&col,&one,&true_row,pt_value,&null,
04461                                 &status,len);
04462                         pt_value[len] = '\0';
04463                         pt_value[last_char(pt_value)] = '\0';
04464             break;
04465         case SHORT :
04466             utrgti(&(table->imno),&col,&one,&true_row,&lval,&null,&status);
04467             *(short *)pt_value = (short)lval;
04468                         break;
04469                 case INT :      
04470         case LONG :
04471             utrgti(&(table->imno),&col,&one,&true_row,&lval,&null,&status);
04472             *(long *)pt_value = lval;
04473                         break;
04474         case FLOAT :
04475             utrgtr(&(table->imno),&col,&one,&true_row,pt_value,&null,&status);
04476                         break;
04477         case DOUBLE :
04478             utrgtd(&(table->imno),&col,&one,&true_row,pt_value,&null,&status);
04479                         break;
04480                 }
04481                 break;
04482 #endif
04483 #ifdef FITS
04484     case FITS_A_FORMAT :
04485     case FITS_B_FORMAT :
04486         status = 0;
04487         fptr = (fitsfile *)table->external_info;
04488         int_datatype = get_datatype_code(FITS_A_FORMAT,dtype);
04489         if (dtype == CHAR) {
04490                 sscanf(format,"A%d",&len);
04491                 str_val = (char **)malloc(sizeof(char *));
04492                 str_val[0] = pt_value;
04493                 fits_read_col(fptr,int_datatype,col,true_row,1L,1L,     
04494                         NULL, str_val, &null, &status);
04495                 free((char *)str_val);
04496                 pt_value[len] = '\0';
04497         }
04498         else {
04499                 fits_read_col(fptr,int_datatype,col,true_row,1L,1L,     
04500                         NULL, pt_value, &null, &status);
04501         }
04502         break;
04503 #endif
04504         }
04505         if (status) {
04506                 sprintf(err_text,"RD_tbl table %s col #%d row #%d", 
04507                                                                         table->name, col,true_row);
04508                 status = get_tiger_errcode(table->data_format,status);
04509                 Handle_Error(err_text,status);
04510         }
04511         if (null != 0) 
04512                         status = ERR_NODATA;
04513         return(status);
04514 }
04515 /* --- Doxygen Function Comment -------------------------- */
04523 /* ------------------------------------------------------- */
04524 
04525 int 
04526 RD_col(TABLE *table, int col, void *value)
04527 {
04528         int  i, null, status, true_row, first_row, last_row, dtype, nocol=col,len,
04529                 nbchars;
04530         char format[20], unit[lg_unit+1], err_text[132];
04531         char *pt_value, *pt_tmp;
04532 
04533 #ifdef IRAF
04534         int nbelt;
04535         unsigned char *nullflag;
04536 #endif
04537 #ifdef FITS
04538         fitsfile *fptr;
04539         int int_datatype;
04540         char **str_val;
04541 #endif
04542 
04543         status = get_col_info(table, col, &dtype, format, unit);        
04544         if (dtype == CHAR) {
04545                 if (('0' <= *format) && (*format <= '9'))
04546                         sscanf(format,"%dA1",&len);
04547                 else
04548                         sscanf(format,"A%d",&len);
04549         }
04550         else
04551                 len= 1;
04552     if (status) {
04553         sprintf(err_text,"RD_col table %s col #%d", table->name, col);
04554                 status = get_tiger_errcode(table->data_format,status);
04555         Handle_Error(err_text,status);
04556         return(status);
04557     }
04558 
04559         if (table->select_flag) {
04560                 first_row = table->sel_row[0]+1;
04561                 last_row = table->sel_row[table->row-1]+1;
04562         }
04563         else {
04564                 first_row = 1;
04565                 last_row = table->allrow;
04566         }
04567         switch (table->data_format) {
04568 #ifdef MIDAS
04569     case MIDAS_FORMAT :
04570                 status = TCCMAP(table->imno,nocol,&pt_value);
04571                 break;
04572 #endif
04573 #ifdef IRAF
04574     case IRAF_FORMAT :
04575     case STSDAS_FORMAT :
04576                 nullflag = (unsigned char *)malloc(table->allrow*sizeof(unsigned char));
04577                 switch (dtype) {
04578                         case CHAR :
04579                         nbelt = (last_row-first_row+1);
04580                         pt_value = (char *)malloc(nbelt*len*sizeof(char));
04581                         utcgtt(&(table->imno),&nocol,&first_row,&last_row,pt_value,nullflag,
04582                                 &status,len);
04583                         if (status != 0) break;
04584                         for (pt_tmp=pt_value, i=0; i<nbelt; i++,pt_tmp+=len) {
04585                                 pt_tmp[len-1] = 0;
04586                                 pt_tmp[last_char(pt_tmp)] = 0;
04587                         }
04588                         break;
04589                         case SHORT :
04590                         pt_value = (char *)malloc((last_row-first_row+1)*sizeof(short));
04591                         utcgts(&(table->imno),&nocol,&first_row,&last_row,pt_value,nullflag,
04592                                 &status);
04593                         break;
04594                         case INT :
04595                         case LONG :
04596                         pt_value = (char *)malloc((last_row-first_row+1)*sizeof(int));
04597                         utcgti(&(table->imno),&nocol,&first_row,&last_row,pt_value,nullflag,
04598                                 &status);
04599                         break;
04600                         case FLOAT :
04601                         pt_value = (char *)malloc((last_row-first_row+1)*sizeof(float));
04602                         utcgtr(&(table->imno),&nocol,&first_row,&last_row,pt_value,nullflag,
04603                                 &status);
04604                         break;
04605                         case DOUBLE :
04606                         pt_value = (char *)malloc((last_row-first_row+1)*sizeof(double));
04607                         utcgtd(&(table->imno),&nocol,&first_row,&last_row,pt_value,nullflag,
04608                                 &status);
04609                         break;
04610                 }
04611                 free((char *)nullflag);
04612                 break;
04613 #endif
04614 #ifdef FITS
04615     case FITS_A_FORMAT :
04616     case FITS_B_FORMAT :
04617                 status = 0;
04618                 first_row = 1;
04619                 last_row = table->allrow;
04620                 fptr = (fitsfile *)table->external_info;
04621                 int_datatype =
04622                         get_datatype_code(table->data_format,(short)dtype);
04623                 if (dtype == CHAR) {
04624                         pt_value = (char *)malloc(table->allrow*len);
04625                         str_val = (char **)malloc(table->allrow*sizeof(char *));
04626                         for (i=0; i<table->allrow;i++)
04627                                 str_val[i] = pt_value + i*len;
04628                         fits_read_col(fptr,int_datatype,col,1L,1L,table->row,   
04629                                 NULL, str_val, &null, &status);
04630                         free((char *)str_val);
04631                 }
04632                 else {
04633                         pt_value = (char *)malloc(table->allrow*sizeof_item((short)dtype));
04634                         fits_read_col(fptr,int_datatype,col,1L,1L,table->allrow,        
04635                                 NULL, (void *)pt_value, &null, &status);
04636                 }
04637                 break;
04638 #endif
04639         }
04640         if (status != 0) {
04641         sprintf(err_text,"RD_col table %s col #%d", table->name, col);
04642                 status = get_tiger_errcode(table->data_format,status);
04643         Handle_Error(err_text,status);
04644                 return (status);
04645         }
04646 
04647         if (!table->select_flag) {
04648                 nbchars = (last_row - first_row +1)*len;
04649                 nbchars *= sizeof_item((short)dtype);
04650                 memcpy((char *)(value),pt_value,nbchars);
04651         }
04652         else {
04653         
04654                 switch (dtype) {        /* handle table selection if any */
04655                 case CHAR :
04656                         for (pt_tmp = (char *)value, i=0; i<table->row; i++) {
04657                                 true_row = table->sel_row[i];
04658                                 memcpy(pt_tmp,pt_value+(true_row)*len,len);
04659                                 pt_tmp += len;
04660                         }
04661                         break;
04662                 case SHORT :
04663                         for (i=0; i<table->row; i++) {
04664                                 true_row = table->sel_row[i];
04665                                 *((short *)(value)+i) = *((short *)(pt_value)+true_row);
04666                         }
04667                         break;
04668                 case INT :
04669                 case LONG :
04670                         for (i=0; i<table->row; i++) {
04671                                 true_row = table->sel_row[i];
04672                                 *((int *)(value)+i) = *((int *)(pt_value)+true_row);
04673                         }
04674                         break;
04675                 case FLOAT :
04676                         for (i=0; i<table->row; i++) {
04677                                 true_row = table->sel_row[i];
04678                                 *((float *)(value)+i) = *((float *)(pt_value)+true_row);
04679                         }
04680                         break;
04681                 case DOUBLE :
04682                         for (i=0; i<table->row; i++) {
04683                                 true_row = table->sel_row[i];
04684                                 *((double *)(value)+i) = *((double *)(pt_value)+true_row);
04685                         }
04686                         break;
04687                 }
04688         }
04689         if (table->data_format != MIDAS_FORMAT)
04690                 free((char *)pt_value);
04691         return(status);
04692 }
04693 
04694 /* --- Doxygen Function Comment -------------------------- */
04703 /* ------------------------------------------------------- */
04704 
04705 int 
04706 WR_tbl(TABLE *table, int row, int col, void *value)
04707 {
04708         int status, true_row=row+1, dtype, lval;
04709         char format[20], unit[lg_unit+1], err_text[132], *pt_value;
04710         
04711 #ifdef IRAF
04712         int one=1;
04713 #endif
04714 #ifdef FITS
04715         fitsfile *fptr;
04716         int int_datatype;
04717         char **str_val;
04718 #endif
04719 
04720         if (table->select_flag) 
04721                 true_row = table->sel_row[row]+1;
04722 
04723         status = get_col_info(table,col,&dtype,format,unit);
04724         if (status) {
04725                 sprintf(err_text,"WR_tbl table %s col #%d row #%d", 
04726                                                                                 table->name, col,true_row);
04727                 status = get_tiger_errcode(table->data_format,status);
04728                 Handle_Error(err_text,status);
04729                 return(status);
04730         }
04731 
04732         switch (table->data_format) {
04733 #ifdef MIDAS
04734         case MIDAS_FORMAT :
04735 
04736                 switch(dtype) {
04737 
04738                 case CHAR :
04739                         pt_value = (char *)value;
04740                         status = TCEWRC(table->imno,true_row,col,pt_value);
04741                         break;
04742                 case SHORT :
04743                         lval = *(short *)value;
04744                         status = TCEWRI(table->imno,true_row,col,&lval);
04745                         break;
04746         case INT :
04747                 case LONG :
04748                         lval = *(long *)value;
04749                         status = TCEWRI(table->imno,true_row,col,&lval);
04750                         break;
04751                 case FLOAT :
04752                         status = TCEWRR(table->imno,true_row,col,value);
04753                         break;
04754                 case DOUBLE :
04755                         status = TCEWRD(table->imno,true_row,col,value);
04756                         break;
04757                 }
04758                 break;
04759 #endif
04760 
04761 #ifdef IRAF
04762         case IRAF_FORMAT :
04763         case STSDAS_FORMAT :
04764 
04765                 if (dtype < 0) dtype = CHAR;
04766 
04767                 switch(dtype) {
04768 
04769                 case CHAR :
04770                         utrptt(&(table->imno),&col,&one,&true_row,value,&status,
04771                                 strlen(value));
04772                         break;
04773                 case SHORT :
04774                         lval = *(short *)value;
04775                         utrpti(&(table->imno),&col,&one,&true_row,&lval,&status);
04776                         break;
04777         case INT :
04778                 case LONG :
04779                         lval = *(long *)value;
04780                         utrpti(&(table->imno),&col,&one,&true_row,&lval,&status);
04781                         break;
04782                 case FLOAT :
04783                         utrptr(&(table->imno),&col,&one,&true_row,value,&status);
04784                         break;
04785                 case DOUBLE :
04786                         utrptd(&(table->imno),&col,&one,&true_row,value,&status);
04787                         break;
04788                 }
04789                 break;
04790 #endif
04791 
04792 #ifdef FITS
04793         case FITS_A_FORMAT :
04794     case FITS_B_FORMAT :
04795                 status = 0;
04796                 fptr = (fitsfile *) table->external_info;
04797                 int_datatype = get_datatype_code(table->data_format,dtype);
04798                 if (true_row > table->allrow) {/*need to insert a new line*/
04799                         fits_insert_rows(fptr,true_row-1,1,&status);    
04800                 }
04801                 switch(dtype) {
04802                 case CHAR :
04803                         sscanf(format,"A%d",&lval);
04804                         str_val = (char **)malloc(sizeof(char *));
04805                         if (lval == 1) {
04806                                 pt_value = (char *)malloc(2*sizeof(char));
04807                                 pt_value[0] = *(char *)value;
04808                                 pt_value[1] = '\0';
04809                         }
04810                         else {
04811                                 pt_value = (char *)value;
04812                         }
04813                         str_val[0] = pt_value;
04814                         fits_write_col(fptr,int_datatype,col,true_row,1L,1L,str_val,&status);
04815                         free((char *)str_val);
04816                         if (lval == 1)
04817                                 free(pt_value);
04818                         break;
04819                 default :
04820                         fits_write_col(fptr,int_datatype,col,true_row,1L,1L,value,&status);
04821                 }
04822                 break;
04823 #endif
04824         }
04825         if (status) {
04826                 sprintf(err_text,"WR_tbl table %s col #%d row #%d", 
04827                                                                                 table->name, col,true_row);
04828                 status = get_tiger_errcode(table->data_format,status);
04829                 Handle_Error(err_text,status);
04830         }
04831         if (table->allrow < true_row) {
04832                 table->allrow++;
04833                 table->row++;
04834         }
04835         return(status);
04836 }
04837 
04838 #define NUL_IVAL  2147483647            /* undefined integer */
04839 #define NUL_RVAL  1.701411e38           /* undefined real */
04840 #define NUL_DVAL  1.70141183460469229e38    /* undefined double */
04841 #define NUL_CVAL  '\0'              /* undefined char  */
04842 
04843 /* --- Doxygen Function Comment -------------------------- */
04851 /* ------------------------------------------------------- */
04852 
04853 int 
04854 WR_null(TABLE *table,int row,int col)
04855 {
04856         int status, true_row=row+1;
04857 
04858 #ifdef IRAF
04859         int one=1;
04860         int no_col=col;
04861 #endif
04862         
04863 #ifdef FITS
04864         char format[20], unit[lg_unit+1];
04865         int dtype;
04866         char c_null = NUL_CVAL;
04867         int i_null = NUL_IVAL;
04868         float f_null = NUL_RVAL;
04869         double d_null = NUL_DVAL;
04870 #endif
04871 
04872         if (table->select_flag) 
04873                 true_row = table->sel_row[row-1]+1;
04874 
04875         switch (table->data_format) {
04876 #ifdef MIDAS
04877         case MIDAS_FORMAT :
04878                 status = TCEDEL(table->imno,true_row,col);
04879                 break;
04880 #endif
04881 #ifdef IRAF
04882         case IRAF_FORMAT :
04883         case STSDAS_FORMAT :
04884                 utrudf(&(table->imno),&no_col,&one,&true_row,&status);
04885                 break;
04886 #endif
04887 #ifdef FITS
04888         case FITS_A_FORMAT :
04889     case FITS_B_FORMAT :
04890                 status = get_col_info(table,col,&dtype,format,unit);
04891                 switch(dtype) {
04892                 case CHAR :
04893                         status = WR_tbl(table, row, col, &c_null);
04894                         break;
04895                 case SHORT :
04896                 case INT :
04897                 case LONG :
04898                         status = WR_tbl(table, row, col, &i_null);
04899                         break;
04900                 case FLOAT :
04901                         status = WR_tbl(table, row, col, &f_null);
04902                         break;
04903                 case DOUBLE :
04904                         status = WR_tbl(table, row, col, &d_null);
04905                         break;
04906                 }
04907                 break;
04908 #endif
04909         }
04910         if (status) {
04911                 status = get_tiger_errcode(table->data_format,status);
04912                 Handle_Error("WR_null",status);
04913         }
04914         return(status);             
04915 }
04916 
04917 /* --- Doxygen Function Comment -------------------------- */
04923 /* ------------------------------------------------------- */
04924 int 
04925 delete_table(TABLE *table)                      /* delete active spectrum */
04926 {
04927         int status;
04928         char errtext[132], filename[lg_name+1];
04929         
04930 #ifdef IRAF
04931         int len;
04932 #endif
04933 #ifdef FITS
04934         fitsfile *fptr;
04935         int iomode;
04936 #endif
04937 
04938         strcpy(filename,table->name);
04939 
04940         switch (table->data_format) {
04941 #ifdef MIDAS
04942         case MIDAS_FORMAT :
04943                 status = SCFDEL(filename);
04944                 break;
04945 #endif
04946 #ifdef IRAF
04947         case IRAF_FORMAT :
04948         case STSDAS_FORMAT :
04949                 len = strlen(filename);
04950                 uttdel(filename,&status,len);
04951                 break;
04952 #endif
04953 #ifdef FITS
04954         case FITS_A_FORMAT :
04955         case FITS_B_FORMAT :
04956         status = 0;
04957         if (table->external_info == NULL) {
04958                 iomode = get_iomode_code(InputIO.basic_io,I_MODE);
04959                 fits_open_file(&fptr,filename,iomode,&status);
04960                 if (status)
04961                         status = unlink(filename);
04962                 fits_delete_file(fptr, &status);
04963         }
04964         else {
04965                 fptr = (fitsfile *)table->external_info;
04966                 if (status)
04967                         status = unlink(filename);
04968                 else {
04969                         fits_delete_file(fptr, &status);
04970                         if (table->select_flag) {
04971                                 if (table->sel_row != NULL) 
04972                                         free((char *)table->sel_row);
04973                         }
04974                 }
04975         }
04976         break;
04977 #endif
04978         }
04979                 
04980         if (status)     {
04981                 sprintf(errtext,"delete_table: table %s",filename);
04982                 status = get_tiger_errcode(table->data_format,status);
04983                 Handle_Error(errtext,status);
04984         }
04985         return(status);
04986 }
04987 /* --- Doxygen Function Comment -------------------------- */
04994                 /* @{ ***** Starting group31 ***** */
05007 /* ------------------------------------------------------- */
05008 
05009 int
05010 create_cube(IMAGE3D *cube, char *name, int *npix, double *start, double *step, short datatype, char *ident, char *unit)
05011 {
05012         char errtext[132], filename[lg_name+1];
05013         int three_dim=3, iomode, int_datatype;
05014         int status, SCIPUT();
05015 #ifdef IRAF
05016         int len;
05017 #endif
05018 #ifdef FITS
05019    fitsfile *fptr;
05020    int bitpix;
05021    int one = 1;
05022 #endif
05023 
05024         strcpy(filename,name);
05025         first_blk(filename);
05026         strcpy(cube->name,filename);
05027         append_ima_extension(cube->name,OutputIO.basic_io);
05028         
05029         strcpy(filename,cube->name);
05030 
05031         if(ASK)
05032           confirme_erase(filename);
05033 
05034         cube->ident[0] = '\0';
05035         cube->history[0] = '\0';
05036         strcpy(cube->cunit,"None given");
05037         cube->startx = start[0]; cube->starty = start[1]; cube->startz = start[2];      
05038         cube->stepx = step[0]; cube->stepy = step[1]; cube->stepz = step[2];
05039         cube->endx = start[0] + (npix[0]-1)*step[0];
05040         cube->endy = start[1] + (npix[1]-1)*step[1];
05041         cube->endz = start[2] + (npix[2]-1)*step[2];
05042         cube->nx = npix[0];     cube->ny = npix[1];     cube->nz = npix[2];
05043         cube->iomode = (int)O_MODE;
05044         cube->data_type = datatype;
05045         cube->file_type = IMA_TYPE;
05046         cube->data_format = OutputIO.basic_io;
05047         cube->external_info = 0;
05048         
05049         if (ident != NULL) strcpy(cube->ident,ident);
05050         if (unit != NULL) {
05051                 memcpy(cube->cunit,unit,lg_unit);
05052                 cube->cunit[lg_unit] = '\0';
05053         }
05054         
05055         iomode = get_iomode_code(OutputIO.basic_io,cube->iomode);
05056         int_datatype = get_datatype_code(OutputIO.basic_io,datatype);
05057 
05058     switch(OutputIO.basic_io) {
05059 
05060 #ifdef MIDAS
05061     case MIDAS_FORMAT :
05062           status = SCIPUT(filename, int_datatype, iomode,
05063                   F_IMA_TYPE, three_dim, &(cube->nx), &(cube->startx),
05064               &(cube->stepx),cube->ident, cube->cunit, &(cube->data),
05065                   &(cube->imno));
05066         break;
05067 #endif
05068 #ifdef IRAF
05069 /*    case IRAF_FORMAT :
05070     case STSDAS_FORMAT :
05071                 len = strlen(filename);
05072                 uimdel(filename,&status,len);
05073                 uimcre(filename,&int_datatype,&two_dim,&(frame->nx),
05074           &(frame->imno),&status,len);
05075                 if (status) break;
05076                 status = WR_desc(frame,"IDENT",CHAR,strlen(frame->ident),
05077                                 frame->ident);
05078                 if (status) break;
05079                 status = alloc_frame_mem(frame, datatype);
05080         break; */
05081 #endif
05082 #ifdef FITS
05083     case FITS_A_FORMAT :
05084     case FITS_B_FORMAT :
05085                 status =0;
05086                 if (exist(filename))
05087                     delete_cube(cube);
05088                 if (fits_create_file(&fptr,filename,&status)) {
05089                     status = ERR_WRIT; break;
05090                 }
05091                 cube->external_info = (void *)fptr;
05092                 bitpix = fits_bitpix(datatype);
05093                 if (fits_create_img(fptr,bitpix,three_dim,(long *)&(cube->nx),&status)) {
05094                      status = ERR_WRIT; break;
05095                 }
05096                 fits_write_key(fptr, TINT, "CRPIX1", &one, "reference pixel", &status);
05097                 fits_write_key(fptr, TDOUBLE, "CRVAL1", &(cube->startx), "coordinate at reference pixel", &status);
05098                 fits_write_key(fptr, TDOUBLE, "CDELT1", &(cube->stepx), "coordinate increment per pixel", &status);
05099                 fits_write_key(fptr, TINT, "CRPIX2", &one, "reference pixel", &status);
05100                 fits_write_key(fptr, TDOUBLE, "CRVAL2", &(cube->starty), "coordinate at reference pixel", &status);
05101                 fits_write_key(fptr, TDOUBLE, "CDELT2", &(cube->stepy), "coordinate increment per pixel", &status);
05102                 fits_write_key(fptr, TINT, "CRPIX3", &one, "reference pixel", &status);
05103                 fits_write_key(fptr, TDOUBLE, "CRVAL3", &(cube->startz), "coordinate at reference pixel", &status);
05104                 fits_write_key(fptr, TDOUBLE, "CDELT3", &(cube->stepz), "coordinate increment per pixel", &status);
05105                 status = alloc_cube_mem(cube, datatype);
05106                 WR_desc(cube,"IDENT",CHAR,lg_ident,cube->ident);
05107         break;
05108 #endif
05109     }
05110 
05111         if (status) {
05112           sprintf(errtext,"create_cube: cube %s",filename);
05113           status = get_tiger_errcode(cube->data_format,status);
05114           Handle_Error(errtext,status);
05115         }
05116         else {
05117           cube->min = 0;
05118           cube->max = 0;
05119           WR_desc(cube,"COMMENT",CHAR,8,"       ");
05120         }
05121         return(status);
05122 }
05123 /* --- Doxygen Function Comment -------------------------- */
05131 /* ------------------------------------------------------- */
05132 int
05133 header_cube(IMAGE3D *cube, char *name, char *mode)              
05134 {
05135         char errtext[132], filename[lg_name+1];
05136         int status, nbaxes, iomode, int_datatype;
05137         int one=1;
05138         float cuts[4];
05139         int info[5];
05140 #ifdef IRAF
05141         int len;
05142 #endif
05143 #ifdef FITS
05144     fitsfile *fptr;
05145     int bitpix;
05146 #endif
05147 
05148         memset(cube->ident,' ',lg_ident);
05149         cube->ident[lg_ident] = '\0';
05150         memset(cube->cunit,' ',lg_unit);
05151         cube->cunit[lg_unit] = '\0';
05152         memset(cube->history,' ',lg_hist);
05153         cube->history[lg_hist] = '\0';
05154         cube->external_info = 0;
05155         cube->file_type = IMA_TYPE;
05156         cube->data_format = InputIO.basic_io;
05157         cube->data.d_data = NULL;
05158         cube->min = 0;
05159         cube->max = 0;
05160 
05161         strcpy(filename,name);
05162         first_blk(filename);
05163         strcpy(cube->name,filename);
05164         append_ima_extension(cube->name,InputIO.basic_io);
05165 
05166         strcpy(filename,cube->name);
05167 
05168         if (!exist(filename)) {
05169                 status = ERR_OPEN;
05170                 sprintf(errtext,"header_cube: cube %s",filename);
05171                 Handle_Error(errtext,status);
05172                 return(status);
05173         }
05174 
05175         switch(mode[0]) {
05176                 case 'I' :
05177                         if (mode[1] == 'O')
05178                                 cube->iomode = (int)IO_MODE;
05179                         else
05180                                 cube->iomode = (int)I_MODE;
05181                         break;
05182                 case 'O' : cube->iomode = (int)O_MODE;
05183                         break;
05184                 default  : cube->iomode = (int)I_MODE;
05185                         break;
05186         }
05187         
05188     iomode = get_iomode_code(InputIO.basic_io,cube->iomode);
05189 
05190     switch (InputIO.basic_io) {
05191 
05192 #ifdef MIDAS
05193         case MIDAS_FORMAT :
05194                 status = SCFINF(filename,2,info);
05195                 if (status == 0) {
05196                         status = SCFOPN(filename, info[1], 0, F_IMA_TYPE,
05197                     &(cube->imno));
05198                         nbaxes = RD_desc(cube,"NPIX",INT,2,&(cube->nx));
05199                     if(nbaxes != 3) {
05200 /*               We opened a spectrum or image as a cube, bad work */
05201                         status = ERR_OPEN;
05202                                 break;
05203                         }
05204 
05205                         RD_desc(cube,"START",DOUBLE,2,&(cube->startx));
05206                         RD_desc(cube,"STEP",DOUBLE,2,&(cube->stepx));
05207                         RD_desc(cube,"IDENT",CHAR,lg_ident,cube->ident);
05208                         RD_desc(cube,"CUNIT",CHAR,lg_unit,cube->cunit);
05209                     cube->data_type = info[1];
05210                     cube->data_type = decode_datatype(InputIO.basic_io,cube->data_type);
05211 
05212                 }
05213                 break;
05214 #endif
05215 #ifdef IRAF
05216 /*    case IRAF_FORMAT :
05217     case STSDAS_FORMAT :
05218                 len = strlen(filename);
05219         uimopn(filename,&iomode,&(frame->imno),&status,len);
05220         if (status != 0)
05221                         break;
05222         uimgid(&(frame->imno),&int_datatype,&two_dim,&(frame->nx),&status);
05223                 frame->data_type = decode_datatype(InputIO.basic_io,(short)(int_datatype));
05224                 disable_user_warnings();
05225                 RD_desc(frame,"IDENT",CHAR,lg_ident,frame->ident);
05226                 restore_user_warnings();
05227                 break;*/
05228 #endif
05229 #ifdef FITS
05230     case FITS_A_FORMAT :
05231     case FITS_B_FORMAT :
05232         status =0;
05233         if (fits_open_file(&fptr,filename,iomode,&status)) {
05234                 status = ERR_ACCESS; break;
05235         }
05236         cube->external_info = (void *)fptr;
05237         if (fits_read_key(fptr, TINT,"NAXIS", &nbaxes,NULL, &status)) {
05238                 status = ERR_READ; break;
05239         }
05240         if (nbaxes != 3) {
05241                 status = ERR_IMA_HEAD; break;
05242         }
05243         if (fits_read_key(fptr, TINT,"BITPIX", &bitpix,NULL, &status)) {
05244                 status = ERR_READ; break;
05245         }
05246         int_datatype = (fptr->Fptr->tableptr)->tdatatype;
05247         cube->data_type = decode_datatype(InputIO.basic_io,(short)int_datatype);
05248         if (fits_read_key(fptr, TINT, "NAXIS1",
05249                 &(cube->nx), NULL, &status)) {
05250                 status = ERR_READ; break;
05251         }
05252         if (fits_read_key(fptr, TINT, "NAXIS2",
05253                 &(cube->ny), NULL, &status)) {
05254                 status = ERR_READ; break;
05255         }
05256         if (fits_read_key(fptr, TINT, "NAXIS3",
05257                 &(cube->nz), NULL, &status)) {
05258                 status = ERR_READ; break;
05259         }
05260         fits_read_key(fptr, TINT, "CRPIX1", &one, NULL, &status);
05261         if (status) { status = 0; one = 1; }
05262         fits_read_key(fptr, TDOUBLE, "CRVAL1", &(cube->startx), NULL, &status);
05263         if (status) { status = 0; cube->startx = (double)1; }
05264         fits_read_key(fptr, TDOUBLE, "CDELT1", &(cube->stepx), NULL, &status);
05265         if (status) { status = 0; cube->stepx = (double)1; }
05266         cube->startx -= (one-1)*cube->stepx;
05267         fits_read_key(fptr, TINT, "CRPIX2", &one, NULL, &status);
05268         if (status) { status = 0; one = 1; }
05269         fits_read_key(fptr, TDOUBLE, "CRVAL2", &(cube->starty), NULL, &status);
05270         if (status) { status = 0; cube->starty = (double)1; }
05271         fits_read_key(fptr, TDOUBLE, "CDELT2", &(cube->stepy), NULL, &status);
05272         if (status) { status = 0; cube->stepy = (double)1; }
05273         cube->starty -= (one-1)*cube->stepy;
05274         fits_read_key(fptr, TINT, "CRPIX3", &one, NULL, &status);
05275         if (status) { status = 0; one = 1; }
05276         fits_read_key(fptr, TDOUBLE, "CRVAL3", &(cube->startz), NULL, &status);
05277         if (status) { status = 0; cube->startz = (double)1; }
05278         fits_read_key(fptr, TDOUBLE, "CDELT3", &(cube->stepz), NULL, &status);
05279         if (status) { status = 0; cube->stepz = (double)1; }
05280         break;
05281 #endif
05282         }
05283 
05284         if (status) {
05285                 sprintf(errtext,"header_cube: cube %s",filename);
05286                 status = get_tiger_errcode(cube->data_format,status);
05287                 Handle_Error(errtext,status);
05288         }
05289         else {
05290                 disable_user_warnings();
05291                 status = RD_desc(cube,"LHCUTS",FLOAT,4,cuts);
05292                 RD_desc(cube,"HISTORY",CHAR,lg_hist,cube->history);
05293                 restore_user_warnings();
05294 
05295                 cube->endx = cube->startx + (cube->nx -1)*cube->stepx;
05296                 cube->endy = cube->starty + (cube->ny -1)*cube->stepy;
05297                 cube->endz = cube->startz + (cube->nz -1)*cube->stepz;
05298                 if (status > 0) {
05299                         cube->min = cuts[2];
05300                         cube->max = cuts[3];
05301                 }
05302                 status = 0;
05303         }
05304         return(status);
05305 }
05306 /* --- Doxygen Function Comment -------------------------- */
05314 /* ------------------------------------------------------- */
05315 int
05316 open_cube(IMAGE3D *cube, char *name, char *mode)                
05317 {
05318         char errtext[132], filename[lg_name+1];
05319         int status, nbaxes, iomode, int_datatype;
05320         int one=1;
05321         float cuts[4];
05322         int info[5];
05323 #ifdef IRAF
05324         int len;
05325 #endif
05326 #ifdef FITS
05327     fitsfile *fptr;
05328     int nbread;
05329         int npix;
05330         int group = 0;
05331 #endif
05332 
05333         memset(cube->ident,' ',lg_ident);
05334         cube->ident[lg_ident] = '\0';
05335         memset(cube->cunit,' ',lg_unit);
05336         cube->cunit[lg_unit] = '\0';
05337         memset(cube->history,' ',lg_hist);
05338         cube->history[lg_hist] = '\0';
05339         cube->external_info = 0;
05340         cube->file_type = IMA_TYPE;
05341         cube->data_format = InputIO.basic_io;
05342 
05343         strcpy(filename,name);
05344         first_blk(filename);
05345         strcpy(cube->name,filename);
05346         append_ima_extension(cube->name,InputIO.basic_io);
05347 
05348         strcpy(filename,cube->name);
05349 
05350         if (!exist(filename)) {
05351                 status = ERR_OPEN;
05352                 sprintf(errtext,"open_cube: cube %s",filename);
05353                 Handle_Error(errtext,status);
05354                 return(status);
05355         }
05356 
05357         switch(mode[0]) {
05358                 case 'I' :
05359                         if (mode[1] == 'O')
05360                                 cube->iomode = (int)IO_MODE;
05361                         else
05362                                 cube->iomode = (int)I_MODE;
05363                         break;
05364                 case 'O' : cube->iomode = (int)O_MODE;
05365                         break;
05366                 default  : cube->iomode = (int)I_MODE;
05367                         break;
05368         }
05369         
05370     iomode = get_iomode_code(InputIO.basic_io,cube->iomode);
05371 
05372     switch (InputIO.basic_io) {
05373 
05374 #ifdef MIDAS
05375         case MIDAS_FORMAT :
05376                 status = SCFINF(filename,2,info);
05377                 if (status == 0) {
05378                         status = SCIGET(filename, info[1], iomode, F_IMA_TYPE, 3,
05379                     &nbaxes, &(cube->nx), &(cube->startx), &(cube->stepx),
05380                     cube->ident, cube->cunit, (char **)(&(cube->data)),
05381                     &(cube->imno));
05382                     cube->data_type = info[1];
05383                     cube->data_type = decode_datatype(InputIO.basic_io,cube->data_type);
05384 
05385                     if(nbaxes!=3) /* We open a spectrum or an image like a cube , and that's not good */
05386                       status = ERR_OPEN;
05387 
05388                 }
05389                 break;
05390 #endif
05391 #ifdef IRAF
05392 /*    case IRAF_FORMAT :
05393     case STSDAS_FORMAT :
05394                 len = strlen(filename);
05395         uimopn(filename,&iomode,&(frame->imno),&status,len);
05396         if (status != 0)
05397                         break;
05398         uimgid(&(frame->imno),&int_datatype,&two_dim,&(frame->nx),&status);
05399                 frame->data_type = decode_datatype(InputIO.basic_io,(short)(int_datatype));
05400         if (status != 0)
05401                         break;
05402                 alloc_frame_mem(frame, datatype);
05403                 switch(frame->data_type) {
05404                 case SHORT :
05405                         uigs2s(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
05406                                 frame->data.s_data,&status);
05407                         break;
05408                 case INT :
05409                 case LONG :
05410                         uigs2l(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
05411                                 frame->data.l_data,&status);
05412                         break;
05413                 case FLOAT :
05414                         uigs2r(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
05415                                 frame->data.f_data,&status);
05416                         break;
05417                 case DOUBLE :
05418                         uigs2d(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
05419                                 frame->data.d_data,&status);
05420                         break;
05421         }
05422                 disable_user_warnings();
05423                 RD_desc(frame,"IDENT",CHAR,lg_ident,frame->ident);
05424                 restore_user_warnings();
05425                 break; */
05426 #endif
05427 #ifdef FITS
05428     case FITS_A_FORMAT :
05429     case FITS_B_FORMAT :
05430         status =0;
05431         if (fits_open_file(&fptr,filename,iomode,&status)) {
05432                 status = ERR_ACCESS; break;
05433         }
05434         cube->external_info = (void *)fptr;
05435         if (fits_read_key(fptr, TINT,"NAXIS", &nbaxes,NULL, &status)) {
05436                 status = ERR_READ; break;
05437         }
05438         if (nbaxes != 3) {
05439                 status = ERR_IMA_HEAD; break;
05440         }
05441         if (fits_read_key(fptr, TINT, "NAXIS1",
05442                 &(cube->nx), NULL, &status)) {
05443                 status = ERR_READ; break;
05444         }
05445         if (fits_read_key(fptr, TINT, "NAXIS2",
05446                 &(cube->ny), NULL, &status)) {
05447                 status = ERR_READ; break;
05448         }
05449         if (fits_read_key(fptr, TINT, "NAXIS3",
05450                 &(cube->nz), NULL, &status)) {
05451                 status = ERR_READ; break;
05452         }
05453         if (status == 0) {
05454                 fits_read_key(fptr, TINT, "CRPIX1", &one, NULL, &status);
05455                 if (status) { status = 0; one = 1; }
05456                 fits_read_key(fptr, TDOUBLE, "CRVAL1", &(cube->startx), NULL, &status);
05457                 if (status) { status = 0; cube->startx = (double)1; }
05458                 fits_read_key(fptr, TDOUBLE, "CDELT1", &(cube->stepx), NULL, &status);
05459                 if (status) { status = 0; cube->stepx = (double)1; }
05460                 cube->startx -= (one-1)*cube->stepx;
05461                 fits_read_key(fptr, TINT, "CRPIX2", &one, NULL, &status);
05462                 if (status) { status = 0; one = 1; }
05463                 fits_read_key(fptr, TDOUBLE, "CRVAL2", &(cube->starty), NULL, &status);
05464                 if (status) { status = 0; cube->starty = (double)1; }
05465                 fits_read_key(fptr, TDOUBLE, "CDELT2", &(cube->stepy), NULL, &status);
05466                 if (status) { status = 0; cube->stepy = (double)1; }
05467                 cube->starty -= (one-1)*cube->stepy;
05468                 fits_read_key(fptr, TINT, "CRPIX3", &one, NULL, &status);
05469                 if (status) { status = 0; one = 1; }
05470                 fits_read_key(fptr, TDOUBLE, "CRVAL3", &(cube->startz), NULL, &status);
05471                 if (status) { status = 0; cube->startz = (double)1; }
05472                 fits_read_key(fptr, TDOUBLE, "CDELT3", &(cube->stepz), NULL, &status);
05473                 if (status) { status = 0; cube->stepz = (double)1; }
05474                 cube->startz -= (one-1)*cube->stepz;
05475         }
05476         else
05477                 break;
05478 
05479         int_datatype = (fptr->Fptr->tableptr)->tdatatype;
05480         cube->data_type = decode_datatype(InputIO.basic_io,(short)int_datatype);
05481         if (cube->data_type == SHORT) {
05482                 if (fptr->Fptr->tableptr[1].tscale == 1 && fptr->Fptr->tableptr[1].tzero == 32768)
05483 //                       unsigned short !!!
05484                         cube->data_type = LONG;
05485         }
05486 
05487         if (alloc_cube_mem(cube, cube->data_type) < 0) {
05488                 fits_close_file(fptr,&status);
05489                 status = ERR_ALLOC;
05490                 break;
05491         }
05492 
05493         npix = cube->nx*cube->ny*cube->nz;
05494         switch (cube->data_type) {
05495         case SHORT :
05496                 if (fits_read_img_sht(fptr,group,1L,npix,(short)0,
05497                         cube->data.s_data,&nbread,&status)) {
05498                         status = ERR_READ; break;
05499                 }
05500                 break;
05501         case LONG :
05502         case INT :
05503                 if (fits_read_img_lng(fptr,group,1L,npix,(int)0,
05504                         cube->data.l_data,&nbread,&status)) {
05505                         status = ERR_READ; break;
05506                 }
05507                 break;
05508         case FLOAT :
05509                 if (fits_read_img_flt(fptr,group,1L,npix,(float)0,
05510                         cube->data.f_data,&nbread,&status)) {
05511                         status = ERR_READ; break;
05512                 }
05513                 break;
05514         case DOUBLE :
05515                 if (fits_read_img_dbl(fptr,group,1L,npix,(double)0,
05516                         cube->data.d_data,&nbread,&status)) {
05517                         status = ERR_READ; break;
05518                 }
05519                 break;
05520         }
05521                 break;
05522 #endif
05523         }
05524 
05525         if (status) {
05526                 sprintf(errtext,"open_cube: cube %s",filename);
05527                 status = get_tiger_errcode(cube->data_format,status);
05528                 Handle_Error(errtext,status);
05529         }
05530         else {
05531                 disable_user_warnings();
05532                 status = RD_desc(cube,"LHCUTS",FLOAT,4,cuts);
05533                 RD_desc(cube,"HISTORY",CHAR,lg_hist,cube->history);
05534                 restore_user_warnings();
05535 
05536                 cube->endx = cube->startx + (cube->nx -1)*cube->stepx;
05537                 cube->endy = cube->starty + (cube->ny -1)*cube->stepy;
05538                 cube->endz = cube->startz + (cube->nz -1)*cube->stepz;
05539                 if (status <= 0) {
05540                         cube_minmax(cube);
05541                 }
05542                 else {
05543                         cube->min = cuts[2];
05544                         cube->max = cuts[3];
05545                 }
05546                 status = 0;
05547         }
05548         return(status);
05549 }
05550 /* --- Doxygen Function Comment -------------------------- */
05556 /* ------------------------------------------------------- */
05557 int
05558 close_cube(IMAGE3D *cube)                       /* close active cube */
05559 {
05560         char   errtext[132], filename[lg_name+1];
05561         int    stat, int_datatype;
05562         float  cuts[4];
05563 #ifdef IRAF
05564         int one=1;
05565 #endif
05566 #ifdef FITS
05567         fitsfile *fptr;
05568         int npix;
05569 #endif
05570 
05571         strcpy(filename,cube->name);
05572 
05573         if (cube->iomode == (int)I_MODE) {
05574                 switch (cube->data_format) {
05575 #ifdef MIDAS
05576                 case MIDAS_FORMAT :
05577                         stat = SCFCLO(cube->imno);
05578                         break;
05579 #endif
05580 #ifdef IRAF
05581 /*              case IRAF_FORMAT :
05582                 case STSDAS_FORMAT :
05583                 uimclo(&(frame->imno),&stat);
05584                 break;  */
05585 #endif
05586 #ifdef FITS
05587                 case FITS_A_FORMAT :
05588                 case FITS_B_FORMAT :
05589                         stat =0;
05590                         fptr = (fitsfile *)cube->external_info;
05591                         fits_close_file(fptr,&stat);
05592                         free_cube_mem(cube);
05593                         break;
05594 #endif
05595                 }
05596                 if (stat) {
05597                         sprintf(errtext,"close_cube: cube %s",filename);
05598                         stat = get_tiger_errcode(cube->data_format,stat);
05599                         Handle_Error(errtext,stat);
05600                 }
05601                 return(stat);
05602         }
05603 
05604         if (cube->data.d_data != NULL) {
05605                 cube_minmax(cube);
05606 
05607                 cuts[0]=cube->min; cuts[2]=cube->min;
05608                 cuts[1]=cube->max; cuts[3]=cube->max;
05609                 stat = WR_desc(cube,"LHCUTS",FLOAT,4,cuts);
05610         }
05611 
05612         WR_history(cube, (Anyfile *)0);
05613 
05614         switch (cube->data_format) {
05615 #ifdef MIDAS
05616         case MIDAS_FORMAT :
05617                 stat = SCFCLO(cube->imno);
05618                 break;
05619 #endif
05620 #ifdef IRAF
05621 /*      case IRAF_FORMAT :
05622         case STSDAS_FORMAT :
05623                 switch(frame->data_type) {
05624                 case SHORT :
05625                 uips2s(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
05626                                 frame->data.s_data,&stat);
05627                         break;
05628                 case INT :
05629                 case LONG :
05630                 uips2l(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
05631                                 frame->data.l_data,&stat);
05632                         break;
05633                 case FLOAT :
05634                 uips2r(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
05635                                 frame->data.f_data,&stat);
05636                         break;
05637                 case DOUBLE :
05638                 uips2d(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
05639                                 frame->data.d_data,&stat);
05640                         break;
05641                 }
05642                 if (stat == 0)
05643                         uimclo(&(frame->imno),&stat);
05644                 free_frame_mem(frame);
05645         break;*/
05646 #endif
05647 #ifdef FITS
05648     case FITS_A_FORMAT :
05649     case FITS_B_FORMAT :
05650         stat = 0;
05651         fptr = (fitsfile *)cube->external_info;
05652         if (cube->iomode == (int)O_MODE) {
05653                 int_datatype = get_datatype_code(OutputIO.basic_io,cube->data_type);
05654                 npix = cube->nx*cube->ny*cube->nz;
05655                 if (fits_write_img(fptr,int_datatype,1L,npix,
05656                         cube->data.s_data,&stat)) {
05657                         stat = ERR_WRIT;
05658                 }
05659         }
05660         if (! stat)
05661             fits_close_file(fptr,&stat);
05662         free_cube_mem(cube);
05663         break;
05664 #endif
05665         }
05666         if (stat) {
05667                 sprintf(errtext,"close_cube: cube %s",filename);
05668                 stat = get_tiger_errcode(cube->data_format,stat);
05669                 Handle_Error(errtext,stat);
05670         } else {
05671                 if(TK && (cube->iomode == O_MODE || cube->iomode == IO_MODE))
05672                 {
05673                         printf("@ N {%s}\n",filename);
05674                 }
05675         }
05676 
05677         return(stat);
05678 }
05679 /* --- Doxygen Function Comment -------------------------- */
05685 /* ------------------------------------------------------- */
05686 
05687 int
05688 delete_cube(IMAGE3D *cube)              
05689 {
05690         int status, iomode;
05691         char errtext[132],filename[lg_name+1];
05692 #ifdef IRAF
05693         int len;
05694 #endif
05695 #ifdef FITS
05696         fitsfile *fptr;
05697 #endif
05698         
05699         strcpy(filename,cube->name);
05700 
05701         switch (cube->data_format) {
05702 #ifdef MIDAS
05703         case MIDAS_FORMAT :
05704                 status = SCFDEL(filename);
05705                 break;
05706 #endif
05707 #ifdef IRAF
05708         case IRAF_FORMAT :
05709         case STSDAS_FORMAT :
05710                 len = strlen(filename);
05711                 uimdel(filename,&status);
05712                 break; 
05713 #endif
05714 #ifdef FITS
05715     case FITS_A_FORMAT :
05716     case FITS_B_FORMAT :
05717         status = 0;
05718         if (cube->external_info == NULL) {
05719                 iomode = get_iomode_code(InputIO.basic_io,I_MODE);
05720                 fits_open_file(&fptr,filename,iomode,&status);
05721                 if (status)
05722                         status = unlink(filename);
05723                 fits_delete_file(fptr, &status);
05724         }
05725         else {
05726                 fptr = (fitsfile *)cube->external_info;
05727                 if (status)
05728                         status = unlink(filename);
05729                 else {
05730                         fits_delete_file(fptr, &status);
05731                         free_cube_mem(cube);
05732                 }
05733         }
05734 #endif
05735         }
05736         if (status)     {
05737                 sprintf(errtext,"delete_cube: cube %s",filename);
05738                 status = get_tiger_errcode(cube->data_format,status);
05739                 Handle_Error(errtext,status);
05740         }
05741         return(status);
05742 }
05743 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
05744                 routines to read/write values into image files are
05745                      given in ../incl/funcdef.h
05746 !     
05747 !.func                        RD_cube()
05748 !
05749 !.purp                  reads a cube value
05750 !.desc
05751 ! (type) value = RD_cube(cube,x_pixel,y_pixel,z_value)
05752 !
05753 ! IMAGE3D *cube;        cube structure
05754 ! int x_pixel;          pixel to read along x axis
05755 ! int y_pixel;          pixel to read along y axis
05756 ! int z_pixel;          pixel to read along z axis
05757 !.ed
05758 !     
05759 !.func                        WR_cube()
05760 !
05761 !.purp                  writes a cube value
05762 !.desc
05763 ! WR_cube(cube,pixel_x,pixel_y,pixel_z,value)
05764 !
05765 ! IMAGE2D *cube;        cube structure
05766 ! int x_pixel;          pixel to write along x axis
05767 ! int y_pixel;          pixel to write along y axis
05768 ! int z_pixel;          pixel to write along z axis
05769 ! (type) value;         value to write
05770 !.ed
05771 !
05772 !.func                        RD_qcube()
05773 !
05774 !.purp                  reads a cube quality flag
05775 !.desc
05776 ! (type) value = RD_qcube(image,x_pixel,y_pixel)
05777 !
05778 ! IMAGE3D *cube;        cube structure
05779 ! int x_pixel;          pixel to read along x axis
05780 ! int y_pixel;          pixel to read along y axis
05781 ! int z_pixel;          pixel to write along z axis
05782 !.ed
05783 !     
05784 !.func                        WR_qcube()
05785 !
05786 !.purp                  writes a cube quality flag
05787 !.desc
05788 ! WR_qcube(image,pixel_x,pixel_y,value)
05789 !
05790 ! IMAGE3D *cube;        cube structure
05791 ! int x_pixel;          pixel to write along x axis
05792 ! int y_pixel;          pixel to read along y axis
05793 ! int z_pixel;          pixel to write along z axis
05794 ! (type) value;         value to write
05795 !.ed
05796 -------------------------------------------------------------------- */

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