Main Page   Modules   Data Structures   File List   Data Fields   Globals  

C_libs/io/std_io/io_misc.c

Go to the documentation of this file.
00001 /* === Doxygen File Comment ============================== */
00010 /* ======================================================= */
00011 #include <stdlib.h>
00012 #include <IFU_io.h>
00013 
00014 #include <data_io.h>
00015 #ifdef MIDAS
00016 #include "../midas_io/midas_defs.h"
00017 #endif
00018 #ifdef IRAF
00019 #include "../iraf_io/incl/iraf_def.h"
00020 #endif
00021 #ifdef FITS
00022 #include "../cfitsio/fitsio.h"
00023 #include "../cfitsio/longnam.h"
00024 #endif
00025 
00026 IO_Format InputIO, OutputIO;
00027 
00028 /* --- Doxygen Function Comment -------------------------- */
00032                 /* @{ ***** Starting group15 ***** */
00039 /* ------------------------------------------------------- */
00040 
00041 int 
00042 decode_format(char *format, IO_Format *IOstruct)
00043 {
00044         short Format;
00045 
00046         IOstruct->basic_io = FITS_B_FORMAT; /* default format*/
00047         IOstruct->datacubes = 0;
00048 
00049         lower_strg(format);
00050 
00051         if (strlen(format) == 0) return(OK);
00052 
00053         if (strcmp(format,"midas") == 0)  Format = MIDAS_FORMAT; 
00054         if (strcmp(format,"iraf") == 0)   Format = IRAF_FORMAT; 
00055         if (strcmp(format,"stsdas") == 0) Format = STSDAS_FORMAT;
00056         if (strcmp(format,"fits") == 0)   Format = FITS_B_FORMAT;
00057         if (strcmp(format,"fits,a") == 0) Format = FITS_A_FORMAT;
00058         if (strcmp(format,"fits,b") == 0) Format = FITS_B_FORMAT;
00059         if (strcmp(format,"euro3d") == 0) Format = EURO3D_FORMAT;
00060         if (strcmp(format,"tiger+fits") == 0) Format = TIGER_FITS_FORMAT;
00061         if (strcmp(format,"tiger+midas") == 0) Format = TIGER_MIDAS_FORMAT;
00062 
00063 #ifdef MIDAS
00064         if (Format == MIDAS_FORMAT) {
00065                 IOstruct->basic_io = Format;
00066                 return(OK);
00067         }
00068         if (Format == TIGER_MIDAS_FORMAT) {
00069                 IOstruct->basic_io = MIDAS_FORMAT;
00070                 IOstruct->datacubes = TIGER_FORMAT;
00071                 return(OK);
00072         }
00073 #endif
00074 #ifdef IRAF
00075         if ((Format == IRAF_FORMAT) || (Format == STSDAS_FORMAT)) {
00076                 IOstruct->basic_io = Format;
00077                 return(OK);
00078         }
00079 #endif
00080 #ifdef FITS
00081         if ((Format == FITS_A_FORMAT) || (Format == FITS_B_FORMAT)) {
00082                 IOstruct->basic_io = Format;
00083                 return(OK);
00084         }
00085         if (Format == TIGER_FITS_FORMAT) {
00086                 IOstruct->basic_io = FITS_B_FORMAT;
00087                 IOstruct->datacubes = TIGER_FORMAT;
00088                 return(OK);
00089         }
00090         if (Format == EURO3D_FORMAT) {
00091                 IOstruct->basic_io = FITS_B_FORMAT;
00092                 IOstruct->datacubes = EURO3D_FORMAT;
00093                 return(OK);
00094         }
00095 #endif
00096         return(ERR_BAD_PARAM);
00097 }
00098 
00099 /* --- Doxygen Function Comment -------------------------- */
00105 /* ------------------------------------------------------- */
00106 
00107 int 
00108 set_inputformat(char *format)
00109 {
00110         char errtext[132];
00111         IO_Format IOstruct;
00112 
00113         if (decode_format(format,&IOstruct)) {
00114 
00115                 sprintf(errtext,"set_inputformat, %s has not been included into I/O library\n",format);
00116                 Handle_Error(errtext,ERR_BAD_TYPE);
00117                 return(ERR_BAD_TYPE);
00118         }
00119         InputIO = IOstruct;
00120         return(OK);
00121 }
00122 
00123 /* --- Doxygen Function Comment -------------------------- */
00129 /* ------------------------------------------------------- */
00130 
00131 int 
00132 set_outputformat(char *format)
00133 {
00134         char errtext[132];
00135         IO_Format IOstruct;
00136 
00137         if (decode_format(format,&IOstruct)) {
00138 
00139                 sprintf(errtext,"set_outputformat, %s has not been included into I/O library\n",format);
00140                 Handle_Error(errtext,ERR_BAD_TYPE);
00141                 return(ERR_BAD_TYPE);
00142         }
00143         OutputIO = IOstruct;
00144         return(OK);
00145 }
00146 
00147 /* --- Doxygen Function Comment -------------------------- */
00152 /* ------------------------------------------------------- */
00153 
00154 int 
00155 set_user_dataformat() 
00156 {
00157         char format_strg[132];
00158         char *pt_env;
00159         int status;
00160 
00161         pt_env = getenv("IFU_DEFAULT_FMT");
00162         if ((pt_env == NULL) || (*pt_env == 0))
00163                 return(FITS_B_FORMAT);
00164 
00165         if (strlen(pt_env) > 80)
00166                 pt_env[80] = 0;
00167         strcpy(format_strg,pt_env);
00168 
00169         first_blk(format_strg);
00170         status = set_inputformat(format_strg);
00171         status = set_outputformat(format_strg);
00172         return(status);
00173 }
00174 
00175 /* --- Doxygen Function Comment -------------------------- */
00182                 /* @{ ***** Starting group16 ***** */
00189 /* ------------------------------------------------------- */
00190 
00191 void append_ima_extension(char *name, short format)
00192 {
00193         char *pt_filename;
00194 
00195         pt_filename = strrchr(name,'/');
00196         if (pt_filename == NULL)
00197                 pt_filename = name;
00198 
00199         if (strchr(pt_filename,'.') == NULL) 
00200         switch (format) { 
00201             case MIDAS_FORMAT  :
00202                         strcat(name,".bdf"); 
00203                         break; 
00204             case STSDAS_FORMAT :
00205             case IRAF_FORMAT   :
00206                         strcat(name,".imh"); 
00207                         break; 
00208             case FITS_A_FORMAT :
00209             case FITS_B_FORMAT :
00210                         strcat(name,".fits");
00211                         break; 
00212         };
00213 }
00214 
00215 /* --- Doxygen Function Comment -------------------------- */
00222 /* ------------------------------------------------------- */
00223 
00224 void append_tbl_extension(char *name, short format)
00225 {
00226         char *pt_filename;
00227 
00228         pt_filename = strrchr(name,'/');
00229         if (pt_filename == NULL)
00230                 pt_filename = name;
00231 
00232         if (strchr(pt_filename,'.') == NULL)
00233         switch (format) {
00234             case MIDAS_FORMAT  : 
00235                         strcat(name,".tbl");
00236                         break; 
00237             case IRAF_FORMAT   :
00238             case STSDAS_FORMAT :
00239                         strcat(name,".tab"); 
00240                         break; 
00241             case FITS_A_FORMAT :
00242             case FITS_B_FORMAT :
00243                         strcat(name,".fits");
00244                         break; 
00245         };
00246 
00247 }
00248 
00249 /* --- Doxygen Function Comment -------------------------- */
00256 /* ------------------------------------------------------- */
00257 
00258 void append_datacube_extension(char *name, short format)
00259 {
00260         char *pt_filename;
00261 
00262         pt_filename = strrchr(name,'/');
00263         if (pt_filename == NULL)
00264                 pt_filename = name;
00265 
00266         if (strchr(pt_filename,'.') == NULL)
00267         switch (format) {
00268             case EURO3D_FORMAT :
00269                         strcat(name,".fits");
00270                         break; 
00271             case TIGER_FORMAT :
00272                         strcat(name,".tig");
00273                         break; 
00274         };
00275 
00276 }
00277 
00278 /* --- Doxygen Function Comment -------------------------- */
00284 /* ------------------------------------------------------- */
00285 
00286 void remove_file_extension(char *name)
00287 {
00288     char *pt_filename, *pt_extension;
00289 
00290     pt_filename = strrchr(name,'/');
00291     if (pt_filename == NULL)
00292         pt_filename = name;
00293 
00294     pt_extension = strchr(pt_filename,'.');
00295 
00296     if (pt_extension == NULL)
00297         return;
00298     *pt_extension = 0;
00299 }
00300 
00301 /* --- Doxygen Function Comment -------------------------- */
00307 /* ------------------------------------------------------- */
00308 
00309 char *file_format(char *filename) {
00310 
00311     FILE *fd;
00312     char *format;
00313     char *pt_filename, *pt_ext, fname[1024];
00314     char buffer[2880];
00315 
00316     format = (char *)malloc(3*sizeof(char));
00317     format[0] = -1;
00318     format[1] = -1;
00319     format[2] = 0;
00320 
00321     pt_filename = strrchr(filename,'/');
00322     if (pt_filename == NULL)
00323         pt_filename = filename;
00324     strcpy(fname,filename);
00325     pt_ext = strchr(pt_filename,'.');
00326     if (pt_ext == NULL) {
00327       append_ima_extension(fname,InputIO.basic_io);
00328     }
00329     else {
00330       if (strcmp(pt_ext,".max") == 0) {
00331         format[0] = TIGER_FORMAT;
00332         format[1] = T_TIGMAX;
00333         return(format);
00334       }
00335     }
00336 
00337     fd = fopen(fname,"r");
00338     if (fd == NULL) {
00339       return(format);
00340     }
00341     if (fread(buffer,1,11,fd) < 11) {
00342         fclose(fd);
00343         return(format);
00344     }
00345     if (strncmp(buffer,"SIMPLE  =",9) == 0) {
00346         format[0] = FITS_B_FORMAT;
00347         format[1] = IMA_TYPE;
00348         if (fread(buffer,1,2869,fd) > 0) {
00349                 if (fgets(buffer,132,fd) > 0) {
00350                      if (strncmp(buffer,"XTENSION=",9) == 0) {
00351                              pt_ext = buffer+9;
00352                              while (*pt_ext != 0) {
00353                              if (strncmp(pt_ext,"TABLE",5) == 0) {
00354                                      format[1] = TBL_TYPE;
00355                                      *pt_ext = 0;
00356                              }
00357                              else
00358                                      pt_ext++;
00359                              }
00360                     }
00361                 }
00362         }
00363     }
00364     else {
00365         if (strncmp(buffer+4,"TABLE ",6) == 0) {
00366                         format[0] = MIDAS_FORMAT;
00367                         format[1] = TBL_TYPE;
00368         }     
00369         else {
00370                 if (strncmp(buffer+4,"IMAGE ",6) == 0) {
00371                         format[0] = MIDAS_FORMAT;
00372                         format[1] = IMA_TYPE;
00373                 }
00374                 else {
00375                         if (strncmp(buffer,"\0i\0m\0h\0d\0r\0",11) == 0)
00376                                 format[0] = IRAF_FORMAT;
00377                         else {
00378                                 if (strncmp(buffer,"\0i\0m\0p\0i\0x\0",11) == 0)
00379                                         format[0] = IRAF_FORMAT;
00380                                 else {
00381                                         if (strncmp(buffer,"v1.0",4) == 0) {
00382                                                 format[0] = TIGER_FORMAT;
00383                                                 format[1] = T_TIGER;
00384                                         }
00385                                 }
00386                         }
00387                 }
00388         }
00389     }
00390     fclose(fd);
00391     return(format);
00392 }
00393 
00394 /* --- Doxygen Function Comment -------------------------- */
00400 /* ------------------------------------------------------- */
00401 
00402 int file_type(char *filename)
00403 {
00404     SPECTRUM spec;
00405     IMAGE2D Image2d;
00406     int type_of_file;
00407     int unknown = 'U';
00408     char *format, *file_format();
00409 
00410     disable_user_warnings();
00411 
00412     format = file_format(filename);
00413     
00414     switch (format[0]) {
00415 
00416         case TIGER_FORMAT :
00417                 type_of_file = format[1];       
00418                 break;
00419 
00420         default :
00421                 switch (format[1]) {
00422                         case IMA_TYPE :
00423 
00424                         if(header_frame(&Image2d,filename,"I")<0) {
00425                                 close_frame(&Image2d);
00426                                 if(header_spec(&spec,filename,"I")<0) {
00427                                         close_spec(&spec);
00428                                         type_of_file = unknown;
00429                                 }
00430                                 else {
00431                                         close_spec(&spec);
00432                                         type_of_file = T_IMA1D;
00433                                 }
00434                         }
00435                         else {
00436                                 close_frame(&Image2d);
00437                                 type_of_file = T_IMA2D;
00438                         }
00439                         break;
00440 
00441                         case TBL_TYPE :
00442 
00443                         type_of_file = T_TABLE;
00444                         break;
00445 
00446                         default :
00447                         type_of_file = unknown;
00448                         break;
00449                 }
00450     }
00451     restore_user_warnings();
00452     return(type_of_file); 
00453 }
00454 
00455 /* --- Doxygen Function Comment -------------------------- */
00461 /* ------------------------------------------------------- */
00462 
00463 int exist_extension(char *file) {
00464 
00465         char *pt_sep;
00466         int status = 0;
00467         fitsfile *fptr;
00468 
00469         pt_sep = strchr(file,'[');
00470         if (pt_sep == NULL)    /* no extension */
00471                 return(-1);
00472         fits_open_file(&fptr,file,0,&status);
00473         if (status)
00474                 return 0;
00475         else {
00476                 fits_close_file(fptr,&status);
00477                 return 1;
00478         }
00479         
00480         return(0);
00481 }
00482 
00483 /* --- Doxygen Function Comment -------------------------- */
00490 /* ------------------------------------------------------- */
00491 
00492 int 
00493 get_iomode_code(short data_format, int iomode)
00494 {
00495         switch (data_format) {
00496 #ifdef MIDAS
00497         case MIDAS_FORMAT :
00498                 switch (iomode) {
00499                 case I_MODE :
00500                         return(F_I_MODE);
00501                         break;
00502                 case O_MODE :
00503                         return(F_O_MODE);
00504                         break;
00505                 case IO_MODE :
00506                         return (F_IO_MODE);
00507                         break;
00508                 }
00509                 break;
00510 #endif
00511 #ifdef IRAF
00512         case IRAF_FORMAT:
00513         case STSDAS_FORMAT :
00514                 switch (iomode) {
00515                 case I_MODE :
00516                         return(RDONLY);
00517                         break;
00518                 case O_MODE :
00519                         return(RDWRIT);
00520                         break;
00521                 case IO_MODE :
00522                         return (RDWRIT);
00523                         break;
00524                 }
00525                 break;
00526 #endif
00527 #ifdef FITS
00528         case FITS_A_FORMAT :
00529         case FITS_B_FORMAT :
00530                 switch (iomode) {
00531                 case I_MODE :
00532                         return(READONLY);
00533                         break;
00534                 case O_MODE :
00535                 case IO_MODE :
00536                         return(READWRITE);
00537                         break;
00538                 }
00539                 break;
00540 #endif
00541         }
00542         return(UNKNOWN);
00543 }
00544 
00545 /* --- Doxygen Function Comment -------------------------- */
00552 /* ------------------------------------------------------- */
00553 int 
00554 get_datatype_code(short data_format, short data_type)
00555 {
00556         switch (data_format) {
00557 #ifdef MIDAS
00558         case MIDAS_FORMAT :
00559                 switch (data_type) {
00560                 case CHAR :
00561                         return(D_C_FORMAT);
00562                         break;
00563                 case SHORT :
00564                         return(D_I2_FORMAT);
00565                         break;
00566                 case INT :
00567                 case LONG :
00568                         return(D_I4_FORMAT);
00569                         break;
00570                 case FLOAT :
00571                         return(D_R4_FORMAT);
00572                         break;
00573                 case DOUBLE :
00574                         return(D_R8_FORMAT);
00575                         break;
00576                 }
00577                 break;
00578 #endif
00579 #ifdef IRAF
00580         case IRAF_FORMAT:
00581         case STSDAS_FORMAT :
00582                 switch (data_type) {
00583                 case CHAR :
00584                         return(-1);
00585                         break;
00586                 case SHORT :
00587                         return(TYSHOR);
00588                         break;
00589                 case INT :
00590                         return(TYINT);
00591                         break;
00592                 case LONG :
00593                         return(TYINT);
00594                         break;
00595                 case FLOAT :
00596                         return(TYREAL);
00597                         break;
00598                 case DOUBLE :
00599                         return(TYDOUB);
00600                         break;
00601                 }
00602                 break;
00603 #endif
00604 #ifdef FITS
00605         case FITS_A_FORMAT :
00606         case FITS_B_FORMAT :
00607         case EURO3D_FORMAT :
00608                 switch (data_type) {
00609                 case CHAR :
00610                         return(TSTRING);
00611                         break;
00612                 case SHORT :
00613                         return(TSHORT);
00614                         break;
00615                 case INT :
00616                 case LONG :
00617                         return(TLONG);
00618                         break;
00619                 case FLOAT :
00620                         return(TFLOAT);
00621                         break;
00622                 case DOUBLE :
00623                         return(TDOUBLE);
00624                         break;
00625                 }
00626                 break;
00627 
00628 #endif
00629         }
00630         return(UNKNOWN);
00631 }
00632 
00633 /* --- Doxygen Function Comment -------------------------- */
00640 /* ------------------------------------------------------- */
00641 short 
00642 decode_datatype(short data_format, short data_type)
00643 {
00644         switch (data_format) {
00645 #ifdef MIDAS
00646         case MIDAS_FORMAT :
00647                 switch (data_type) {
00648                 case D_C_FORMAT :
00649                         return(CHAR);
00650                         break;
00651                 case D_I2_FORMAT :
00652                         return(SHORT);
00653                         break;
00654                 case D_I4_FORMAT :
00655                         return(LONG);
00656                         break;
00657                 case D_R4_FORMAT :
00658                         return(FLOAT);
00659                         break;
00660                 case D_R8_FORMAT :
00661                         return(DOUBLE);
00662                         break;
00663                 }
00664                 break;
00665 #endif
00666 #ifdef IRAF
00667         case IRAF_FORMAT:
00668         case STSDAS_FORMAT :
00669                 switch (data_type) {
00670                 case TYSHOR :
00671                         return(SHORT);
00672                         break;
00673                 case TYINT :
00674                         return(LONG);
00675                         break;
00676                 case TYLONG :
00677                         return(LONG);
00678                         break;
00679                 case TYREAL :
00680                         return(FLOAT);
00681                         break;
00682                 case TYDOUB :
00683                         return(DOUBLE);
00684                         break;
00685                 default :
00686                         if (data_type < 0) 
00687                                 return(CHAR);
00688                 }
00689                 break;
00690 #endif
00691 #ifdef FITS
00692         case FITS_A_FORMAT :
00693         case FITS_B_FORMAT :
00694                 switch (data_type) {
00695                 case TSTRING :
00696                         return(CHAR);
00697                         break;
00698                 case TUSHORT :
00699                         return(USHORT);
00700                         break;
00701                 case TSHORT :
00702                         return(SHORT);
00703                         break;
00704                 case TINT :
00705                 case TLONG :
00706                         return(LONG);
00707                         break;
00708                 case TFLOAT :
00709                         return(FLOAT);
00710                         break;
00711                 case TDOUBLE :
00712                         return(DOUBLE);
00713                         break;
00714                 }
00715                 break;
00716 #endif
00717         }
00718         return(UNKNOWN);
00719 }
00720 /* --- Doxygen Function Comment -------------------------- */
00726 /* ------------------------------------------------------- */
00727 int 
00728 sizeof_item(short data_type)
00729 {
00730         switch (data_type) {
00731         case CHAR :
00732                 return(sizeof(char));
00733                 break;
00734         case SHORT :
00735                 return(sizeof(short));
00736                 break;
00737         case INT :
00738                 return(sizeof(int));
00739                 break;
00740         case LONG :
00741                 return(sizeof(long));
00742                 break;
00743         case FLOAT :
00744                 return(sizeof(float));
00745                 break;
00746         case DOUBLE :
00747                 return(sizeof(double));
00748                 break;
00749         }
00750         return(UNKNOWN);
00751 }
00752 
00753 /* --- Doxygen Function Comment -------------------------- */
00760                 /* @{ ***** Starting group17 ***** */
00767 /* ------------------------------------------------------- */
00768 int 
00769 alloc_spec_mem(SPECTRUM *spectrum, short type) 
00770 {
00771         spectrum->data_type = type;
00772         switch(type) {
00773                 case SHORT :
00774                         spectrum->data.s_data = (short *)calloc(spectrum->npts,sizeof(short));
00775                         if (spectrum->data.s_data == 0) return(ERR_ALLOC);
00776                         break;
00777                 case INT :
00778                 case LONG :
00779                         spectrum->data.l_data = (long *)calloc(spectrum->npts,sizeof(long));
00780                         if (spectrum->data.l_data == 0) return(ERR_ALLOC);
00781                         break;
00782                 case FLOAT :
00783                         spectrum->data.f_data = (float *)calloc(spectrum->npts,sizeof(float));
00784                         if (spectrum->data.f_data == 0) return(ERR_ALLOC);
00785                         break;
00786                 case DOUBLE :
00787                         spectrum->data.d_data = (double *)calloc(spectrum->npts,sizeof(double));
00788                         if (spectrum->data.d_data == 0) return(ERR_ALLOC);
00789                         break;
00790         }
00791         spectrum->quality = (unsigned long *)calloc(spectrum->npts,sizeof(unsigned long));
00792         return(OK);
00793 }
00794 
00795 /* --- Doxygen Function Comment -------------------------- */
00801 /* ------------------------------------------------------- */
00802 int 
00803 free_spec_mem(SPECTRUM *spectrum) 
00804 {
00805         if (spectrum->data.d_data == NULL)
00806                 return(OK);
00807 ;
00808         switch(spectrum->data_type) {
00809                 case SHORT :
00810                 case INT :
00811                 case LONG :
00812                 case FLOAT :
00813                 case DOUBLE :
00814                         free((char *)spectrum->data.d_data);
00815                         break;
00816         }
00817         spectrum->data.d_data = 0x0;
00818         free((char *)spectrum->quality);
00819         spectrum->quality = 0x0;
00820         return(OK);
00821 }
00822 
00823 /* --- Doxygen Function Comment -------------------------- */
00830 /* ------------------------------------------------------- */
00831 int 
00832 alloc_frame_mem(IMAGE2D *frame, short type) 
00833 {
00834         frame->data_type = type;
00835         switch(type) {
00836                 case USHORT :
00837                 case SHORT :
00838                         frame->data.s_data = (short *)calloc(frame->nx*frame->ny,sizeof(short));
00839                         if (frame->data.s_data == 0) return(ERR_ALLOC); 
00840                         break;
00841                 case INT :
00842                 case LONG :
00843                         frame->data.l_data = (long *)calloc(frame->nx*frame->ny,sizeof(long));
00844                         if (frame->data.l_data == 0) return(ERR_ALLOC); 
00845                         break;
00846                 case FLOAT :
00847                         frame->data.f_data = (float *)calloc(frame->nx*frame->ny,sizeof(float));
00848                         if (frame->data.f_data == 0) return(ERR_ALLOC); 
00849                         break;
00850                 case DOUBLE :
00851                         frame->data.d_data = (double *)calloc(frame->nx*frame->ny,sizeof(double));
00852                         if (frame->data.d_data == 0) return(ERR_ALLOC); 
00853                         break;
00854                 default :
00855                         return(ERR_ALLOC);
00856                         break;
00857         }
00858         frame->quality = (unsigned long *)calloc(frame->nx*frame->ny,sizeof(unsigned long));
00859         return(OK);
00860 }
00861 
00862 /* --- Doxygen Function Comment -------------------------- */
00868 /* ------------------------------------------------------- */
00869 int 
00870 free_frame_mem(IMAGE2D *frame) 
00871 {
00872         if (frame->data.d_data == NULL)
00873                 return(OK);
00874 ;
00875         switch(frame->data_type) {
00876                 case SHORT :
00877                 case INT :
00878                 case LONG :
00879                 case FLOAT :
00880                 case DOUBLE :
00881                         free((char *)frame->data.d_data);
00882                         break;
00883         }
00884         frame->data.d_data = 0x0;
00885         free((char *)frame->quality);
00886         frame->quality = 0x0;
00887         return(OK);
00888 }
00889 
00890 /* --- Doxygen Function Comment -------------------------- */
00897 /* ------------------------------------------------------- */
00898 int 
00899 alloc_cube_mem(IMAGE3D *frame, short type) 
00900 {
00901         int npts = frame->nx*frame->ny*frame->nz;
00902 
00903         frame->data_type = type;
00904         switch(type) {
00905                 case USHORT :
00906                 case SHORT :
00907                         frame->data.s_data = (short *)calloc(npts,sizeof(short));
00908                         if (frame->data.s_data == 0) return(ERR_ALLOC); 
00909                         break;
00910                 case INT :
00911                 case LONG :
00912                         frame->data.l_data = (long *)calloc(npts,sizeof(long));
00913                         if (frame->data.l_data == 0) return(ERR_ALLOC); 
00914                         break;
00915                 case FLOAT :
00916                         frame->data.f_data = (float *)calloc(npts,sizeof(float));
00917                         if (frame->data.f_data == 0) return(ERR_ALLOC); 
00918                         break;
00919                 case DOUBLE :
00920                         frame->data.d_data = (double *)calloc(npts,sizeof(double));
00921                         if (frame->data.d_data == 0) return(ERR_ALLOC); 
00922                         break;
00923                 default :
00924                         return(ERR_ALLOC);
00925                         break;
00926         }
00927         frame->quality = (unsigned long *)calloc(npts,sizeof(unsigned long));
00928         return(OK);
00929 }
00930 
00931 /* --- Doxygen Function Comment -------------------------- */
00937 /* ------------------------------------------------------- */
00938 int 
00939 free_cube_mem(IMAGE3D *frame) 
00940 {
00941         if (frame->data.d_data == NULL)
00942                 return(OK);
00943 ;
00944         switch(frame->data_type) {
00945                 case SHORT :
00946                 case INT :
00947                 case LONG :
00948                 case FLOAT :
00949                 case DOUBLE :
00950                         free((char *)frame->data.d_data);
00951                         break;
00952         }
00953         frame->data.d_data = 0x0;
00954         free((char *)frame->quality);
00955         frame->quality = 0x0;
00956         return(OK);
00957 }
00958 
00959 /* --- Doxygen Function Comment -------------------------- */
00967 /* ------------------------------------------------------- */
00968 int 
00969 alloc_new_desc(Anyfile *pt_file, short data_type, int nb_val) 
00970 {
00971         Descr_Items *desc_items;
00972         Descriptor *descr;
00973         int index;
00974 
00975         if (pt_file->external_info == NULL)
00976               pt_file->external_info = (void *)calloc(1,sizeof(Descr_Items));
00977         desc_items = (Descr_Items *)(pt_file->external_info);
00978         if (desc_items->descr_list == NULL) {
00979                 index = 0;
00980                 desc_items->descr_list = (Descriptor *)malloc(sizeof(Descriptor));
00981         }
00982         else {
00983                 index = desc_items->nb_descr;
00984                 desc_items->descr_list = (Descriptor *)realloc(
00985                         (char *)desc_items->descr_list,(index+1)*sizeof(Descriptor));
00986         }
00987         descr = desc_items->descr_list+index;
00988         descr->data_type = data_type;
00989         descr->nb_values = nb_val;
00990         descr->descr_name = (char *)calloc(lg_label+1,sizeof(char));
00991         if (data_type == CHAR)
00992                 descr->descr_value.c_data = 
00993                         (char *)calloc(nb_val+1,sizeof_item(data_type));
00994         else
00995                 descr->descr_value.c_data = 
00996                         (char *)calloc(nb_val,sizeof_item(data_type));
00997         return(OK);
00998 }
00999 
01000 /* --- Doxygen Function Comment -------------------------- */
01006 /* ------------------------------------------------------- */
01007 int 
01008 free_all_desc(Anyfile *pt_file)
01009 {
01010         int i;
01011         Descr_Items *desc_items;
01012         Descriptor descr;
01013 
01014         if (pt_file->external_info == 0) /* no descriptors */
01015         return(OK);
01016 
01017         desc_items = (Descr_Items *)(pt_file->external_info);
01018         for (i=0; i < desc_items->nb_descr; i++) {
01019                 descr = desc_items->descr_list[i];
01020                 free(descr.descr_name);
01021                 free(descr.descr_value.c_data);
01022         }
01023         free(desc_items->descr_list);
01024         free(desc_items);
01025         return(OK);
01026 }
01027 
01028 
01029 /* --- Doxygen Function Comment -------------------------- */
01035 /* ------------------------------------------------------- */
01036 int read_file_class(void *anyfile)
01037 {
01038     Anyfile *fd;
01039     int type, status;
01040     char text[132];
01041 
01042     disable_user_warnings();
01043     
01044     status = RD_desc(anyfile,"SFCLASS",INT,1,&type);
01045     if (status >= 0) {
01046       if (type > 0) {
01047         type = SUPER_CLASS;
01048         restore_user_warnings();
01049         fd = (Anyfile *)anyfile;
01050         sprintf(text,"You are overriding class type for file %s: "
01051               "unexpected results may occur.",fd->name);
01052         print_warning(text);
01053         return type;
01054       }
01055     }
01056     status = RD_desc(anyfile,"FCLASS",INT,1,&type);
01057     if (status < 0)
01058       type = 0;
01059 
01060     restore_user_warnings();
01061 
01062     return type;
01063 }
01064 
01065 /* --- Doxygen Function Comment -------------------------- */
01071 /* ------------------------------------------------------- */
01072 int set_super_class(void *anyfile)
01073 {
01074     int one=1;
01075 
01076     disable_user_warnings();
01077     WR_desc(anyfile,"SFCLASS",INT,1,&one);
01078     restore_user_warnings();
01079     return(0);
01080 }
01081 
01082 /* --- Doxygen Function Comment -------------------------- */
01088 /* ------------------------------------------------------- */
01089 int unset_super_class(void *anyfile)
01090 {
01091     int zero=0;
01092 
01093     disable_user_warnings();
01094     WR_desc(anyfile,"SFCLASS",INT,1,&zero);
01095     restore_user_warnings();
01096     return(0);
01097 }
01098 
01099 /* --- Doxygen Function Comment -------------------------- */
01106 /* ------------------------------------------------------- */
01107 int write_file_class(void *anyfile, int type)
01108 {
01109     int code, status=0;
01110     code = type;
01111     disable_user_warnings();
01112     
01113     if (code == SUPER_CLASS) 
01114         status = set_super_class(anyfile);
01115     else
01116         status = WR_desc(anyfile,"FCLASS",INT,1,&code);
01117     restore_user_warnings();
01118     return(status);
01119 }
01120 
01121 #ifdef FITS
01122 
01123 /* --- Doxygen Function Comment -------------------------- */
01129 /* ------------------------------------------------------- */
01130 int 
01131 fits_bitpix(short data_type)
01132 {
01133         switch (data_type) {
01134         case CHAR :
01135                 return(BYTE_IMG);
01136                 break;
01137         case SHORT :
01138                 return(SHORT_IMG);
01139                 break;
01140         case INT :
01141         case LONG :
01142                 return(LONG_IMG);
01143                 break;
01144         case FLOAT :
01145                 return(FLOAT_IMG);
01146                 break;
01147         case DOUBLE :
01148                 return(DOUBLE_IMG);
01149                 break;
01150         }
01151         return(ERR_BAD_TYPE);
01152 }
01153 
01154 /* --- Doxygen Function Comment -------------------------- */
01160 /* ------------------------------------------------------- */
01161 int 
01162 fits_datatype(int bitpix)
01163 {
01164         switch (bitpix) {
01165         case BYTE_IMG :
01166                 return(TBYTE);
01167                 break;
01168         case SHORT_IMG :
01169                 return(TSHORT);
01170                 break;
01171         case LONG_IMG :
01172                 return(TLONG);
01173                 break;
01174         case FLOAT_IMG :
01175                 return(TFLOAT);
01176                 break;
01177         case DOUBLE_IMG :
01178                 return(TDOUBLE);
01179                 break;
01180         }
01181         return(ERR_BAD_TYPE);
01182 }
01183 /* --- Doxygen Function Comment -------------------------- */
01189 /* ------------------------------------------------------- */
01190 int 
01191 fits_non_std_desc(char *card)
01192 {
01193         switch (card[0]) {
01194                 case 'B' :
01195                 if (strncmp(card,"BITPIX",6) == 0)
01196                         return(0);
01197                 if (strncmp(card,"BSCALE",6) == 0)
01198                         return(0);
01199                 if (strncmp(card,"BZERO",5) == 0)
01200                         return(0);
01201                 break;
01202                 case 'C' :
01203                 if (strncmp(card,"CRPIX",5) == 0)
01204                         return(0);
01205                 if (strncmp(card,"CRVAL",5) == 0)
01206                         return(0);
01207                 if (strncmp(card,"CDELT",5) == 0)
01208                         return(0);
01209                 break;
01210                 case 'E' :
01211                 if (strncmp(card,"EXTEND",6) == 0)
01212                         return(0);
01213                 if (strncmp(card,"EXTNAME",7) == 0)
01214                         return(0);
01215                 if (strncmp(card,"END",3) == 0)
01216                         return(0);
01217                 if (strncmp(card,"EXTNAME",7) == 0)
01218                 break;
01219                 case 'G' :
01220                 if (strncmp(card,"GCOUNT",6) == 0)
01221                         return(0);
01222                 break;
01223                 case 'H' :
01224                 if (strncmp(card,"HISTORY",7) == 0)
01225                         return(0);
01226                 break;
01227                 case 'L' :
01228                 if (strncmp(card,"LHCUTS",6) == 0)
01229                         return(0);
01230                 break;
01231                 case 'N' :
01232                 if (strncmp(card,"NAXIS",5) == 0)
01233                         return(0);
01234                 break;
01235                 case 'P' :
01236                 if (strncmp(card,"PCOUNT",6) == 0)
01237                         return(0);
01238                 break;
01239                 case 'S' :
01240                 if (strncmp(card,"SIMPLE",6) == 0)
01241                         return(0);
01242                 break;
01243                 case 'T' :
01244                 if (strncmp(card,"TFIELDS",7) == 0)
01245                         return(0);
01246                 if (strncmp(card,"TTYPE",5) == 0)
01247                         return(0);
01248                 if (strncmp(card,"TFORM",5) == 0)
01249                         return(0);
01250                 break;
01251                 case 'X' :
01252                 if (strncmp(card,"XTENSION",8) == 0)
01253                         return(0);
01254                 break;
01255         }               
01256         return(1);
01257 }
01258 #endif
01259 
01260 /* --- Doxygen Function Comment -------------------------- */
01267 /* ------------------------------------------------------- */
01268 int copy_table_desc(void *anyfile_in, void *anyfile_out)
01269 {
01270     int status;
01271     char name[lg_name+1];
01272 
01273     disable_user_warnings();
01274 
01275     status = RD_desc(anyfile_in, "TABLE", CHAR, lg_name, name);
01276     if (status > 0) 
01277       WR_desc(anyfile_out, "TABLE", CHAR, lg_name, name);
01278     restore_user_warnings();
01279     return(0);
01280 }

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