Main Page   Modules   Data Structures   File List   Data Fields   Globals  

C_libs/io/std_io/io_utils.c

Go to the documentation of this file.
00001 /* === Doxygen File Comment ============================== */
00009 /* ======================================================= */
00010 
00011 #include <gendef.h>
00012 #include <items.h>
00013 #include <data_io.h>
00014 #include <stdarg.h>
00015 
00016 int wrcr = 0;
00017 
00018 /* --- Doxygen Function Comment -------------------------- */
00022                 /* @{ ***** Starting group18 ***** */
00029 /* ------------------------------------------------------- */
00030 int 
00031 print_msg(char *string, ...)
00032 {
00033    va_list args ;
00034    char *format;
00035    
00036    va_start(args, string);
00037    format = malloc((strlen(string) + 20)*sizeof(char));
00038 
00039    if (DEBUG) {
00040         sprintf(format,"%s\n",string);
00041         vfprintf(stderr,format,args);
00042    }
00043 
00044    if (VERBOSE) {
00045         if(!TK) {
00046             if (wrcr) {
00047                 sprintf(format,"\n%s\n",string);
00048                 vprintf(format,args);
00049             }
00050             else {
00051                 sprintf(format,"%s\n",string);
00052                 vprintf(format,args);
00053             }
00054             wrcr = 0;
00055         }
00056         else {
00057             sprintf(format,"%s\n",string);
00058             vprintf(format,args);
00059         }
00060         fflush(stdout);
00061    }
00062     
00063    free(format);
00064    va_end(args);
00065 
00066    return(0);
00067 }
00068 
00069 /* --- Doxygen Function Comment -------------------------- */
00075 /* ------------------------------------------------------- */
00076 int 
00077 print_msg_nocr(char *text)
00078 {
00079     int status;
00080 
00081     status = 0;
00082     if (VERBOSE) {
00083       printf("%s",text);
00084       fflush(stdout);
00085       wrcr = 1;
00086     }
00087     return(status);
00088 }
00089 
00090 /* --- Doxygen Function Comment -------------------------- */
00096 /* ------------------------------------------------------- */
00097 int 
00098 print_label(char *text)
00099 {
00100    int status=0;
00101 
00102    if(!TK) {
00103        if (VERBOSE) {
00104            printf("\r%s",text);
00105            fflush(stdout);
00106            wrcr = 1;
00107          }
00108         
00109        if (DEBUG) fprintf(stderr,"%s\n",text);
00110    }
00111    else {
00112      if(VERBOSE) {
00113        printf("@ L \"%s\"\n",text);
00114        fflush(stdout);
00115      }
00116    }
00117  
00118    return(status);
00119 }
00120 
00121 /* --- Doxygen Function Comment -------------------------- */
00129 /* ------------------------------------------------------- */
00130 int print_progress(char *text,float pourcent,float step)
00131 {
00132   static float last = 0.0;
00133 
00134   if(step<0)
00135         {
00136          last=0.0;
00137          return 0;
00138         }
00139 
00140   if((pourcent-last)>=step) 
00141   {
00142     if(TK)
00143       {
00144         printf("@ P \"%s\" %4.1f\n",text,pourcent);
00145         fflush(stdout);
00146       }
00147     else
00148       {
00149         if(VERBOSE)
00150           {     
00151             printf("\r%s %4.1f%%",text,pourcent);
00152             fflush(stdout);
00153             wrcr = 1;
00154           }
00155       }
00156     last=pourcent; 
00157   }
00158   else
00159     if (pourcent>=100.0)
00160       {
00161         if(TK)
00162           {
00163             printf("@ P \"%s\" %4.1f\n",text,100.0);
00164             fflush(stdout);
00165           }
00166         else
00167           {
00168             if(VERBOSE)
00169               { 
00170                 printf("\r%s %4.1f%%",text,100.0);
00171                 fflush(stdout);
00172                 wrcr = 1;
00173               }
00174           }
00175         last=100.0;
00176       }  
00177    return(0);
00178 }
00179 int reset_print_progress()
00180 {
00181   return(print_progress("", 0, -1.0));
00182 }
00183 
00184 /* --- Doxygen Function Comment -------------------------- */
00191 /* ------------------------------------------------------- */
00192 int 
00193 print_warning(char *string, ...)
00194 {
00195    va_list args ;
00196    char *format;
00197    
00198    va_start(args, string);
00199    format = malloc((strlen(string) + 20)*sizeof(char));
00200 
00201    if (DEBUG) {
00202         sprintf(format,"%s\n",string);
00203         vfprintf(stderr,format,args);
00204    }
00205 
00206    if(TK) {
00207         sprintf(format,"@ W \"%s\"\n",string);
00208         vprintf(format,args);
00209    }
00210    else {
00211         if(VERBOSE) {
00212             if (wrcr) {
00213                 sprintf(format,"\nWARNING: %s\n",string);
00214                 vprintf(format,args);
00215             }
00216             else {
00217                 sprintf(format,"WARNING: %s\n",string);
00218                 vprintf(format,args);
00219             }
00220             wrcr = 0;
00221         }
00222    }
00223    free(format);
00224    va_end(args);
00225    return(0);
00226 }
00227 
00228 /* --- Doxygen Function Comment -------------------------- */
00235 /* ------------------------------------------------------- */
00236 int 
00237 print_error(char *string, ...)
00238 {
00239    va_list args ;
00240    char *format;
00241    
00242    va_start(args, string);
00243    format = malloc((strlen(string) + 20)*sizeof(char));
00244 
00245    if (DEBUG) {
00246         sprintf(format,"%s\n",string);
00247         vfprintf(stderr,format,args);
00248    }
00249 
00250    if(TK) {
00251         sprintf(format,"@ R \"%s\"\n",string);
00252         vprintf(format,args);
00253    }
00254    else {
00255         if(VERBOSE) {
00256             if (wrcr) {
00257                 sprintf(format,"\nERROR: %s\n",string);
00258                 vprintf(format,args);
00259             }
00260             else {
00261                 sprintf(format,"ERROR: %s\n",string);
00262                 vprintf(format,args);
00263             }
00264             wrcr = 0;
00265         }
00266    }
00267    free(format);
00268    va_end(args);
00269    return(0);
00270 }
00271 
00272 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00273 !                                                                                     
00274 !.blk           Transformations between pixels and word coordinates
00275 
00276               (transformation for spectra are described in funcdef.h)
00277 !                                                       
00278 !.func                       coord_spec()
00279 !                                                       
00280 !.purp          transformation in world coordinates for a spectrum
00281 !
00282 !.desc
00283 ! float world_coord = coord_spec(spectrum,pixel) 
00284 !
00285 ! SPECTRUM *spectrum;   spectrum structure
00286 ! int pixel;            pixel
00287 !.ed                                                            
00288 !                                                       
00289 !.func                       pixel_spec()
00290 !                                                       
00291 !.purp          transformation in pixels for a spectrum
00292 !
00293 !.desc
00294 ! int pixel = pixel_spec(spectrum,world_coord) 
00295 !
00296 ! SPECTRUM *spectrum;   spectrum structure
00297 ! float world_coord;    world coordinate
00298 !.ed                                                            
00299 !                                                       
00300 !.func                       coord_frame()
00301 !                                                       
00302 !.purp          transformation in world coordinates for a frame
00303 !
00304 !.desc
00305 ! void coord_frame(image,pixel_x,pixel_y,*world_x,*world_y) 
00306 !
00307 ! IMAGE2D *image;       image structure
00308 ! int pixel_x;          pixel along x axis
00309 ! int pixel_y;          pixel along y axis
00310 ! float *x;             coordinate along x axis
00311 ! float *y;             coordinate along y axis
00312 !.ed                                                            
00313 --------------------------------------------------------------------*/
00314 
00315 void 
00316 coord_frame(IMAGE2D *frame,int pixel_x,int pixel_y, float *x, float *y)
00317 {
00318         *x = frame->startx + pixel_x * frame->stepx;
00319         *y = frame->starty + pixel_y * frame->stepy;
00320 }
00321 
00322 /* --- Doxygen Function Comment -------------------------- */
00332 /* ------------------------------------------------------- */
00333 
00334 void 
00335 pixel_frame(IMAGE2D *frame, float x, float y, int *pixel_x, int *pixel_y)
00336 {
00337         *pixel_x = 0.5 + (x - frame->startx)/frame->stepx;
00338         *pixel_y = 0.5 + (y - frame->starty)/frame->stepy;
00339 }
00340 
00341 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00342 !                                                       
00343 !.func                       in_frame()
00344 !                                                       
00345 !.purp          checks if pixel of coordinates (x,y) is inside frame
00346 !
00347 !.desc
00348 ! int in_frame(frame,world_x,world_y)
00349 !
00350 ! IMAGE2D *image;       image structure
00351 ! float world_x;        coordinate along x axis
00352 ! float world_y;        coordinate along y axis
00353 !.ed                                                            
00354 -------------------------------------------------------------------- */
00355 
00356 /* --- Doxygen Function Comment -------------------------- */
00363                 /* @{ ***** Starting group19 ***** */
00371 /* ------------------------------------------------------- */
00372 
00373 int 
00374 set_subspec(SPECTRUM *spectrum,double binf,double bsup)
00375 {
00376         char errtext[132];
00377         
00378         if (binf > bsup ) {             
00379                 sprintf(errtext,"set_subspec: spec %s",spectrum->name);
00380                 Handle_Error(errtext,ERR_IMA_BOUND);
00381                 return(ERR_IMA_BOUND);
00382         }       
00383         if (binf < (spectrum->start - (spectrum->step/2))) {
00384                 sprintf(errtext,"set_subspec: spec %s",spectrum->name);
00385                 Handle_Error(errtext,ERR_IMA_BOUND);
00386                 return(ERR_IMA_BOUND);
00387         }       
00388         if (bsup > (spectrum->end + (spectrum->step/2))) {
00389                 sprintf(errtext,"set_subspec: spec %s",spectrum->name);
00390                 Handle_Error(errtext,ERR_IMA_BOUND);
00391                 return(ERR_IMA_BOUND);
00392         }
00393         /* Redundant with 1st test ??? YC */
00394         /* if ((binf > bsup )) {                
00395            sprintf(errtext,"set_subspec: spec %s",spectrum->name);
00396            Handle_Error(errtext,ERR_IMA_BOUND);
00397            return(ERR_IMA_BOUND);
00398         } */
00399         spectrum->iwstart = (int)(((binf - spectrum->start) / spectrum->step)
00400                                    + 0.5);
00401         spectrum->iwend = (int)(((bsup - spectrum->start) / spectrum->step)
00402                                    + 0.5);
00403         spectrum->wstart = spectrum->start +
00404                                   spectrum->iwstart*spectrum->step;
00405         spectrum->wend = spectrum->start +
00406                                 spectrum->iwend*spectrum->step;
00407         return(OK);
00408 }               
00409 /* --- Doxygen Function Comment -------------------------- */
00415 /* ------------------------------------------------------- */
00416 
00417 int 
00418 spec_minmax(SPECTRUM *spectrum) 
00419 {
00420         long i;
00421         
00422         spectrum->wmin = MAXFLOAT;
00423         spectrum->wmax = -MAXFLOAT;
00424         switch(spectrum->data_type) {
00425         case SHORT :
00426                 for (i=spectrum->iwstart;i<=spectrum->iwend;i++){
00427                         if (spectrum->data.s_data[i] < spectrum->wmin) 
00428                                         spectrum->wmin= (float)spectrum->data.s_data[i];
00429                         if (spectrum->data.s_data[i] > spectrum->wmax) 
00430                                         spectrum->wmax= (float)spectrum->data.s_data[i];
00431                 }
00432                 break;
00433 
00434         case LONG :
00435                 for (i=spectrum->iwstart;i<=spectrum->iwend;i++){
00436                         if (spectrum->data.l_data[i] < spectrum->wmin) 
00437                                         spectrum->wmin= (float)spectrum->data.l_data[i];
00438                         if (spectrum->data.l_data[i] > spectrum->wmax) 
00439                                         spectrum->wmax= (float)spectrum->data.l_data[i];
00440                 }
00441                 break;
00442 
00443         case FLOAT :
00444                 for (i=spectrum->iwstart;i<=spectrum->iwend;i++){
00445                         if (spectrum->data.f_data[i] < spectrum->wmin) 
00446                                         spectrum->wmin= (float)spectrum->data.f_data[i];
00447                         if (spectrum->data.f_data[i] > spectrum->wmax) 
00448                                         spectrum->wmax= (float)spectrum->data.f_data[i];
00449                 }
00450                 break;
00451 
00452         case DOUBLE :
00453                 for (i=spectrum->iwstart;i<=spectrum->iwend;i++){
00454                         if (spectrum->data.d_data[i] < spectrum->wmin) 
00455                                         spectrum->wmin= (float)spectrum->data.d_data[i];
00456                         if (spectrum->data.d_data[i] > spectrum->wmax) 
00457                                         spectrum->wmax= (float)spectrum->data.d_data[i];
00458                 }
00459                 break;
00460 
00461         default :
00462                 return(ERR_BAD_TYPE);
00463         }
00464         return(OK);
00465 }
00466 /* --- Doxygen Function Comment -------------------------- */
00473 /* ------------------------------------------------------- */
00474 
00475 int 
00476 inter_spec(SPECTRUM *spec1,SPECTRUM *spec2)                     
00477 {
00478         double l1, l2;
00479         l1 = MAX(spec1->start,spec2->start);
00480         l2 = MIN(spec1->end,spec2->end);
00481         if (l1 >= l2) return(ERR_BAD_PARAM);
00482         spec1->iwstart = ((l1 - spec1->start) / spec1->step) + 0.5;
00483         spec1->iwend = ((l2 - spec1->start) / spec1->step) + 0.5;
00484         spec1->wstart = spec1->start + spec1->iwstart*spec1->step;
00485         spec1->wend = spec1->start + spec1->iwend*spec1->step;
00486         spec2->iwstart = ((l1 - spec2->start) / spec2->step) + 0.5;
00487         spec2->iwend = ((l2 - spec2->start) / spec2->step) + 0.5;
00488         spec2->wstart = spec2->start + spec2->iwstart*spec2->step;
00489         spec2->wend = spec2->start + spec2->iwend*spec2->step;
00490         return(OK);
00491 }
00492 /* --- Doxygen Function Comment -------------------------- */
00499                 /* @{ ***** Starting group20 ***** */
00505 /* ------------------------------------------------------- */
00506 
00507 int 
00508 image_minmax(IMAGE2D *frame) 
00509 {
00510         int i,j;
00511         double max, min;
00512         double intens;
00513 
00514         max=-HUGE; min=HUGE;
00515         for (i=0;i<frame->nx;i++) {
00516                 for (j=0;j<frame->ny;j++) {
00517                         intens =(double)RD_frame(frame,i,j);
00518                         if (intens > max) max=intens;
00519                         if (intens < min) min=intens;
00520                 }
00521         }       
00522         frame->max = max;
00523         frame->min = min;
00524         return(OK);
00525 }
00526 
00527 /* --- Doxygen Function Comment -------------------------- */
00533 /* ------------------------------------------------------- */
00534  
00535 int flip_frame(IMAGE2D *image)
00536 {
00537         int half_frame, i, i1, j;
00538         short sval;
00539         int ival;
00540         float fval;
00541         double dval;
00542 
00543         half_frame = (int)(image->nx/2);
00544 
00545         switch (image->data_type) {
00546 
00547         case SHORT : 
00548                 for (j=0; j<image->ny; j++) {
00549                         for (i=0; i<half_frame; i++) {
00550                                 i1 = image->nx -1 -i;
00551                                 sval = image->data.s_data[j*image->nx+i];
00552                                 image->data.s_data[j*image->nx+i] = image->data.s_data[j*image->nx+i1];
00553                                 image->data.s_data[j*image->nx+i1] = sval;
00554                         }
00555                 }
00556                 break;
00557 
00558         case LONG : 
00559         case INT : 
00560                 for (j=0; j<image->ny; j++) {
00561                         for (i=0; i<half_frame; i++) {
00562                                 i1 = image->nx -1 -i;
00563                                 ival = image->data.l_data[j*image->nx+i];
00564                                 image->data.l_data[j*image->nx+i] = image->data.l_data[j*image->nx+i1];
00565                                 image->data.l_data[j*image->nx+i1] = ival;
00566                         }
00567                 }
00568                 break;
00569 
00570         case FLOAT : 
00571                 for (j=0; j<image->ny; j++) {
00572                         for (i=0; i<half_frame; i++) {
00573                                 i1 = image->nx -1 -i;
00574                                 fval = image->data.f_data[j*image->nx+i];
00575                                 image->data.f_data[j*image->nx+i] = image->data.f_data[j*image->nx+i1];
00576                                 image->data.f_data[j*image->nx+i1] = fval;
00577                         }
00578                 }
00579                 break;
00580 
00581         case DOUBLE : 
00582                 for (j=0; j<image->ny; j++) {
00583                         for (i=0; i<half_frame; i++) {
00584                                 i1 = image->nx -1 -i;
00585                                 dval = image->data.d_data[j*image->nx+i];
00586                                 image->data.d_data[j*image->nx+i] = image->data.d_data[j*image->nx+i1];
00587                                 image->data.d_data[j*image->nx+i1] = dval;
00588                         }
00589                 }
00590                 break;
00591 
00592         default :
00593                 return(ERR_BAD_TYPE);
00594         }
00595         return(0);
00596 }              
00597 
00598 /* --- Doxygen Function Comment -------------------------- */
00605                 /* @{ ***** Starting group21 ***** */
00611 /* ------------------------------------------------------- */
00612 
00613 int 
00614 cube_minmax(IMAGE3D *cube) 
00615 {
00616         int i,j,k;
00617         double max, min;
00618         double intens;
00619 
00620         max=-HUGE; min=HUGE;
00621         for (i=0;i<cube->nx;i++) {
00622                 for (j=0;j<cube->ny;j++) {
00623                         for (k=0;k<cube->nz;k++) {
00624                                 intens =(double)RD_cube(cube,i,j,k);
00625                                 if (intens > max) max=intens;
00626                                 if (intens < min) min=intens;
00627                         }
00628                 }
00629         }       
00630         cube->max = max;
00631         cube->min = min;
00632         return(OK);
00633 }
00634 /* --- Doxygen Function Comment -------------------------- */
00641                 /* @{ ***** Starting group22 ***** */
00649 /* ------------------------------------------------------- */
00650 int 
00651 search_in_col (TABLE *table, int num, void *item)
00652 {
00653     int  status, i, len, row, nocol=num, nbbytes, type;
00654     char form[20], unit[lg_unit+1], *pt_items;
00655     short S_item;
00656     int I_item;
00657     float R_item;
00658     double D_item;
00659 
00660         if (table->row <= 0)
00661                 return(ERR_NODATA);
00662         
00663     status = get_col_info(table,nocol,&type,form,unit);
00664     if (status) {
00665                 status = get_tiger_errcode(table->data_format,status);  
00666         Handle_Error("Search_in_col",status);
00667         return(status);
00668     }
00669     nbbytes = table->row*sizeof_item((short)type);
00670         if ((short)type == CHAR) {
00671         sscanf(form,"A%d",&len);
00672         nbbytes *= len;
00673     }
00674     pt_items = (char *)malloc(nbbytes);
00675     status = RD_col(table,nocol,pt_items);
00676     if (status) {
00677                 status = get_tiger_errcode(table->data_format,status);  
00678         Handle_Error("Search_in_col",status);
00679         return(status);
00680     }
00681     row = -1;
00682     switch((short)type) {
00683         case CHAR :
00684             for (i=0; i<table->row && row <0; i++) {
00685                 if (strcmp(&(pt_items[i*len]),(char *)item) == 0)
00686                                         row = i;
00687             }
00688             break;
00689         case SHORT :
00690             S_item = *((short *)item);
00691             for (i=0; i<table->row && row <0; i++) {
00692                 if (*((short*)(pt_items)+i) == S_item) row = i;
00693             }
00694             break;
00695         case INT :
00696         case LONG :
00697             I_item = *((int *)item);
00698             for (i=0; i<table->row && row <0; i++) {
00699                 if (*((int*)(pt_items)+i) == I_item) row = i;
00700             }
00701             break;
00702         case FLOAT :
00703             R_item = *((float *)item);
00704             for (i=0; i<table->row && row <0; i++) {
00705                 if (*((float*)(pt_items)+i) == R_item) row = i;
00706             }
00707             break;
00708         case DOUBLE :
00709             D_item = *((double *)item);
00710             for (i=0; i<table->row && row <0; i++) {
00711                 if (*((double*)(pt_items)+i) == D_item) row = i;
00712             }
00713             break;
00714         default :
00715             status = -1;
00716             break;
00717     }
00718     free((char *)pt_items);
00719     return(row);
00720 }
00721 
00722 /* --- Doxygen Function Comment -------------------------- */
00729 /* ------------------------------------------------------- */
00730 int get_col_width(TABLE *table, int colno) {
00731 
00732     char key[lg_label+1], format[lg_label+1], c;
00733     int npts;
00734     
00735     sprintf(key,"TFORM%d",colno);
00736     RD_desc(table,key,CHAR,lg_label,format);
00737     sscanf(format,"%d%c",&npts,&c);
00738     return(npts);
00739 };
00740 
00741 /* --- Doxygen Function Comment -------------------------- */
00748                 /* @{ ***** Starting group23 ***** */
00755 /* ------------------------------------------------------- */
00756 
00757 int 
00758 RD_catalog( char *catalog, char *filename)
00759 {
00760         int      i, status;
00761         char text[132];
00762         static FILE **fd_catal = 0;
00763         static char **nameof_catal = 0;
00764         static int nb_catal = 0;
00765         
00766         i = 0; status = 0;
00767         filename[0] = '\0';
00768         if (nb_catal != 0)
00769                 for (i=0;i<nb_catal && (strcmp(catalog,nameof_catal[i]) != 0); i++);
00770         else {
00771                 fd_catal = (FILE **)malloc(sizeof(FILE *));
00772                 nameof_catal = (char **)malloc(sizeof(char *));
00773         }
00774         if (i >= nb_catal) {
00775                 fd_catal=
00776                         (FILE **)realloc((char *)fd_catal,(nb_catal+1)*sizeof(FILE *));
00777                 nameof_catal=
00778                         (char **)realloc((char *)nameof_catal,(nb_catal+1)*sizeof(char *));
00779                 nameof_catal[nb_catal] = (char *)malloc(lg_name*sizeof(char));
00780                 strcpy(nameof_catal[nb_catal],catalog);
00781                 fd_catal[nb_catal] = fopen(catalog,"r");
00782                 if (fd_catal[nb_catal] == NULL) {
00783                         sprintf(text,"RD_catalog %s",catalog);
00784                         Handle_Error(text,ERR_BAD_CAT);
00785                         return(FALSE);              
00786                 }
00787                 else {
00788                         fgets(text,80,fd_catal[i]); /* get catalog description line */
00789                         nb_catal++;
00790                 }
00791         }
00792         if (feof(fd_catal[i])) 
00793                 fclose(fd_catal[i]);
00794         else {
00795                 fgets(text,80,fd_catal[i]);
00796                 if (feof(fd_catal[i])) {
00797                         fclose(fd_catal[i]);
00798                 }
00799                 else {
00800                         text[first_blk(text)] = '\0';
00801                         strcpy(filename,text);
00802                 }
00803         }
00804         if (filename[0] == '\0') {
00805                 nb_catal = 0; i =0;
00806                 return(FALSE);
00807         }
00808         return(TRUE);
00809 }

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