home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / plotio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  2.7 KB  |  122 lines

  1. /* type specified spectrum (use it to convert spectra types)
  2. */
  3.  
  4. #include <stdio.h>
  5. #include <spec.h>
  6.  
  7. float x,y,*spc,*err,*tim;
  8.  
  9. help()
  10. {
  11.   printf("plotio spectrum [-l2p] [-p2l]\n");
  12.   printf("  converts spectra from LISE to MULTIPLOT and vice versa\n");
  13.   printf("  options:\n");
  14.   printf("     -l2p     convert from LISE to MULTIPLOT\n");
  15.   printf("     -p2l     convert from MULTIPLOT to LISE\n");
  16. }
  17.  
  18. main(argc,argv)
  19. int argc;
  20. char *argv[];
  21. {
  22. int n,m,i,max;
  23. char z[80],comment[80];
  24.  
  25.  
  26.    spc= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  27.    err= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  28.    tim= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  29.    if(tim==NULL) {
  30.       printf("sorry, not enough memory\n");
  31.       exit(-1);
  32.    }
  33.  
  34.    if(checkopt(argc,argv,"-l2p",z)) {
  35.       max=readspec(argv[1],spc,err,tim,comment);
  36.       strcpy(z,argv[1]); n = strlen(z);
  37.       for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
  38.       writeplot(z,spc,err,tim,max,comment);
  39.    }
  40.  
  41.    if(checkopt(argc,argv,"-p2l",z)) {
  42.       max = readplot(argv[1],spc,err,tim,comment);
  43.       strcpy(z,argv[1]); n = strlen(z);
  44.       for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
  45.       writespec(z,spc,err,max,2,comment);
  46.       strcat(z,".tim");
  47.       writespec(z,tim,err,max,2,comment);
  48.    }
  49.    free(err); free(spc); free(tim);
  50.    exit(0);
  51. }
  52.  
  53. writeplot(name,spc,err,tim,max,comment)
  54. char *name, *comment;
  55. float *spc, *err, *tim;
  56. int max;
  57. {
  58. FILE *fp;
  59. int i,n,m;
  60. char *s;
  61.  
  62.    s = (char *) malloc(256);
  63.  
  64.    sprintf(s,"%s.dat",name);
  65.    fp = fopen(s,"w");
  66.    if(fp == NULL) {
  67.       fprintf(stderr,"plotio: could not open >%s< for write\n",s);
  68.       free(s);
  69.       return(0);
  70.    }
  71.    fprintf(fp,"*TITLE* %s\n*XLABEL* Time\n*LEGEND* %s\n",comment,name);
  72.    for(n = 0; n < max; n++) {
  73.       fprintf(fp,"%f %f %f\n",tim[n],spc[n],err[n]);
  74.    }
  75.    fclose(fp);
  76.  
  77.    free(s);
  78. }
  79.  
  80. readplot(name,spc,err,tim,comment)
  81. char *name, *comment;
  82. float *spc, *err, *tim;
  83. {
  84. FILE *fp;
  85. int i,n,max;
  86. char *s, *z;
  87.  
  88.    s = (char *) malloc(256); z = (char *) malloc(256);
  89.  
  90.    strcpy(s,name);
  91.    fp = fopen(s,"r");
  92.    if(fp == NULL) {
  93.       strcat(s,".dat");
  94.       fp = fopen(s,"r");
  95.       if(fp == NULL) {
  96.          fprintf(stderr,"plotio: could not open >%s< for read\n",name);
  97.          free(s); free(z);
  98.          return(0);
  99.       }
  100.    }
  101.    while(!feof(fp)) {
  102.       fgets(s,200,fp); sscanf(s,"%s",z);
  103.       if(strcmp(z,"*TITLE*") == 0) {
  104.          n = strlen(z); i = 0; while(s[n] != 0) comment[i++] = s[n++];
  105.          comment[i] = 0;
  106.       }
  107.       if(strcmp(z,"*LEGEND*") == 0) break;
  108.    }
  109.    max = 0;
  110.    while(!feof(fp)) {
  111.       fgets(s,200,fp);
  112.       if(s[0] == '*') continue;
  113.       if(strlen(s) < 3) break;
  114.       sscanf(s,"%f %f %f\n",&tim[max],&spc[max],&err[max]);
  115.       max = max + 1;
  116.    }
  117.    fclose(fp);
  118.  
  119.    free(s); free(z);
  120. }
  121.  
  122.