home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / DataScope 2.0.3 / DataScope2l / DSSource / fget.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  14.9 KB  |  662 lines  |  [TEXT/MPS ]

  1. #include "macfview.h"
  2. #include "df.h"
  3.  
  4. /****************************************************************************/
  5.  
  6. dfpal(filename,rgb)
  7.     char *filename,*rgb;
  8.     {
  9.     DF *fdf;
  10.     int ret;
  11.     DFdesc dfd;
  12.     
  13.     if (NULL == (fdf = DFopen(filename,DFACC_READ,16))) {
  14.         return(-1);
  15.     }
  16.  
  17.     DFsetfind(fdf, DFTAG_IP8, DFTAG_WILDCARD);            /* the first palette in the file */
  18.     if (DFfind(fdf, &dfd))
  19.         return(-1);
  20.  
  21.     if (0 > DFaccess( fdf, DFTAG_IP8, dfd.ref, "r"))    /* open section of file */
  22.         return(-1);
  23.         
  24.     if (0 > (ret = DFread( fdf, rgb, 768)))
  25.         return(-1);
  26.  
  27.     DFclose(fdf);
  28.     
  29.     return(0);
  30. }
  31.  
  32.  
  33.  
  34. extern struct {
  35.     int
  36.         sref,                    /* return flag for sdg */
  37.         bref,                    /* ref #'s for each of these */
  38.         iref,
  39.         pref,
  40.         bxx,bxy,                /* expansion factors for block image */
  41.         ixx,ixy,                /* interpolated image factors */
  42.         pxx,                    /* expansion for polar image */
  43.         ashift,
  44.         dleft,
  45.         dtop,                    /* viewport for polar image */
  46.         dright,
  47.         dbottom,
  48.         dxsize,                    /* sizes for interp image windows */
  49.         dysize;
  50.         
  51. } dsinfo;
  52.  
  53. /****************************************************************************/
  54. /*  getdf
  55. *   Given a filename, we are responsible for reading in all of the relevant
  56. *   data from that file.  This may include, floating point data, images, and
  57. *   notes which are stored with the dataset.
  58. *
  59. *   For each type of data in the file, call the allocation routines, read the
  60. *   data and then activate the data by storing it into the window list.
  61. */
  62.  
  63. extern unsigned char *rgbsave;
  64.  
  65. getdf(filename)
  66.     char *filename;
  67.     {
  68.     int prank,sizes[2],xdim,ydim,i,filestat;
  69.     
  70.     filestat = -1;
  71.     DFSDrestart();
  72.     
  73.     
  74. /*
  75. *  open DF file to read in special record
  76. */
  77.     while (0 <= (i = DFSDgetdims(filename, &prank, sizes, 2)) && prank == 2) {
  78.     
  79.         if (filestat <= 0) {
  80.             /* dialog prompt */
  81.                 filestat = 0;        /* ok */
  82.                 /* filestat = 1;  read them all */
  83.         }
  84.         
  85.         readdf(filename,sizes);
  86.  
  87.     }
  88.     
  89.     if (filestat < 0) {                    /* no files were read */
  90.         extern int DFerror;
  91.         paramtext(filename,"","","");
  92.         NoteAlert(1001, nil);
  93.         return(-1);
  94.     }
  95.     
  96.     return(0);
  97. }
  98.  
  99. readdf(filename,sizes)
  100.     char *filename;
  101.     int sizes[];
  102.     {
  103.     char s[255],*p,*q,*qbase;
  104.     DFdesc dfd,dfd2;
  105.     DF *fdf;
  106.     struct fdatawin *td,*newdatawin();
  107.     int xdim,ydim,i,j,sref,ispal;
  108.     float *f;
  109.     
  110.     ydim = sizes[0];
  111.     xdim = sizes[1];
  112.  
  113. /*
  114. *   create the dataset, read it in and install it into the window 
  115. *   structure for DataScope.
  116. */
  117.     td = newdatawin(filename,xdim,ydim);                /* allocates all mem */
  118.     if (NULL == td)
  119.         return(-1);
  120.         
  121.     strncpy(td->fname,filename,sizeof(td->fname));        /* keep file name */
  122.     td->valmax = td->valmin = 0.0;
  123.         
  124.     DFSDgetdata(filename, 2, sizes, td->vals);
  125.     
  126.     DFSDgetmaxmin(&td->valmax, &td->valmin);
  127.  
  128.     if (0 > DFSDgetdimscale(1, ydim, td->yvals)) {        /* load scale values */
  129.         f = td->yvals;
  130.         for (i=0; i<ydim ; i++)
  131.             *f++ = (float)i;                            /* defaults if they are not there */
  132.     }
  133.  
  134.     if (0 > DFSDgetdimscale(2, xdim, td->xvals)) {        /* load scale values */
  135.         f = td->xvals;
  136.         for (i=0; i<xdim ; i++)
  137.             *f++ = (float)i;                            /* defaults if they are not there */
  138.     }
  139.     
  140.     DFSDsetlengths(sizeof(td->dvar), 255, sizeof(td->fmt), 255);    
  141.     DFSDgetdatastrs(td->dvar, s, td->fmt, s);    
  142.  
  143.     DFSDsetlengths(sizeof(td->xvar), 255, sizeof(td->labfmt), 255);    
  144.     DFSDgetdimstrs(1, td->yvar, s, td->labfmt);
  145.     DFSDgetdimstrs(2, td->xvar, s, td->labfmt);
  146.  
  147. /*
  148. *  check for what we were expecting from the labels.  Accepts C or Fortran formats.
  149. */
  150.     if (td->fmt[0] != '%')
  151.         ftn2c(td->fmt,td->fmt,"%11.4e");
  152.     if (td->labfmt[0] != '%')
  153.         ftn2c(td->labfmt,td->labfmt,"%5.1f");
  154.         
  155.     sref = DFSDlastref();
  156.  
  157. /*
  158. *   Now that we have all of the data, install it into the window list.
  159. */
  160.     ctextwin(td);
  161.  
  162. /*
  163. *  If max and min are wrong, re-calculate.
  164. */
  165.     if (td->valmax <= td->valmin) {
  166.         maxmin( td->vals, xdim, ydim,
  167.                 &td->valmax, &td->valmin, 0,0, ydim, xdim);
  168.     }
  169.  
  170. /*
  171. *   Get the information from the DataScope text record.
  172. *   This will fill the dsinfo record with the ref #s of the records which
  173. *   we are concerned with and will provide the expansion factors which should
  174. *   be kept for the images.
  175. */
  176.     if (NULL == (fdf = DFopen(filename,DFACC_READ,16))) {
  177.         return(-1);
  178.     }
  179.     
  180.     if (0 <= DFaccess( fdf, 8000, sref, "r")) {     /* open annotation */
  181.         i = DFread(fdf, s, 250);
  182.         Sconf(s, i);
  183.     }
  184.  
  185.     DFclose(fdf);
  186.     
  187.     DFR8restart();
  188. /*
  189. *  Read in any image windows which are stored.  Display them, etc.
  190. */
  191.     if (dsinfo.bref > 0) {
  192.         td->exx = dsinfo.bxx;                /* set the expansion factors */
  193.         td->exy = dsinfo.bxy;
  194.         td->xsize = td->exx*td->xdim;        /* set sizes */
  195.         td->ysize = td->exy*td->ydim;
  196.         
  197.         if (0 <= DFR8getdims(filename, &i, &j, &ispal)) {
  198.             emptyimage(td->text,FIMG);        /* create an empty image window */
  199.             DFR8getimage(filename, td->image->vdev.bp, i, j, rgbsave);
  200.             if (ispal)
  201.                 setpal(td->image,rgbsave);
  202.         }
  203.     }
  204.  
  205.     if (dsinfo.iref > 0) {                    /* interpolated image */
  206.         td->exx = dsinfo.ixx;                /* set the expansion factors */
  207.         td->exy = dsinfo.ixy;
  208.         td->xsize = td->exx*td->xdim;        /* set sizes */
  209.         td->ysize = td->exy*td->ydim;
  210.         if (dsinfo.dysize > 0)    {            /* is the image size in the file? */
  211.             td->ysize = dsinfo.dysize;
  212.             td->exy = td->ysize/td->ydim;
  213.         }
  214.         if (dsinfo.dxsize > 0) {
  215.             td->xsize = dsinfo.dxsize;
  216.             td->exx = td->xsize/td->xdim;
  217.         }
  218.  
  219.         if (0 <= DFR8getdims(filename, &i, &j, &ispal)) {
  220.             emptyimage(td->text,FBI);        /* create an empty image window */
  221.             DFR8getimage(filename, td->interp->vdev.bp, i, j, rgbsave);
  222.             if (ispal)
  223.                 setpal(td->interp,rgbsave);
  224.         }
  225.     }
  226.  
  227.     if (dsinfo.pref > 0) {            /* interpolated image */
  228.         td->exy = td->exx = dsinfo.pxx;        /* set the expansion factors */
  229.         if (dsinfo.dright > dsinfo.dleft) {    /* we have a viewport */
  230.             td->viewport.left = dsinfo.dleft;
  231.             td->viewport.top = dsinfo.dtop;
  232.             td->viewport.right = dsinfo.dright;
  233.             td->viewport.bottom = dsinfo.dbottom;
  234.             td->angleshift = dsinfo.ashift;
  235.         }
  236.         else {
  237.             SetRect(&td->viewport, -j/2, -j/2, j/2, j/2);
  238.             td->angleshift = 0;
  239.         }
  240.         if (0 <= DFR8getdims(filename, &i, &j, &ispal)) {
  241.             emptyimage(td->text,FPOL);        /* create an empty image window */
  242.             DFR8getimage(filename, td->polar->vdev.bp, 
  243.                 i, j, rgbsave);
  244.             if (ispal)
  245.                 setpal(td->polar,rgbsave);
  246.                 
  247.         }
  248.     }
  249.  
  250. /*
  251. *  If there are notes, read it from the file and create the notes window.
  252. */
  253.  
  254.     if (NULL == (fdf = DFopen(filename,DFACC_READ,16))) {
  255.         return(-1);
  256.     }
  257.         
  258.     DFsetfind(fdf, DFTAG_DIA, sref);                /* annotation for the SDG we just read */
  259.     if (DFfind(fdf, &dfd))
  260.         {
  261.         DFclose(fdf);
  262.         return(-1);
  263.         }
  264.  
  265.     if (0 <= DFaccess( fdf, DFTAG_DIA, dfd.ref, "r")) {     /* open annotation */
  266.  
  267.         DFread( fdf, &dfd2.tag, 4);                            /* read tag/ref from DIA */
  268. /*
  269. *  for the length of the annotation, read it in and change EOLs
  270. */
  271.         td->contentlen = dfd.length-4;
  272.         td->content = (CharsHandle)NewHandle(td->contentlen);
  273.         
  274.         HLock((Handle) td->content);
  275.         
  276.         DFread( fdf, *td->content, td->contentlen);
  277.         /*
  278.         *  convert EOLs from CRLF to CR
  279.         */
  280.         q = p = qbase = (char *) *td->content;
  281.         i = td->contentlen;
  282.         while (--i > 0 && *p) {
  283.             *q++ = *p++;
  284.             if (*p == 10) {                            /* skip LFs */
  285.                 p++; --i;
  286.             }
  287.         }
  288.         
  289.         td->contentlen = q - qbase;                    /* new length */
  290.         
  291.         HUnlock((Handle) td->content);
  292.         
  293.         SetHandleSize( (Handle) td->content,td->contentlen);    /* set new length */
  294.         
  295.         /*
  296.         *  Use the just-created text window as a jumping off point and
  297.         *  create a visible notes window to display the notes we just read.
  298.         */
  299.         if (td->text)
  300.             newnotes(td->text);
  301.         
  302.     }
  303.     
  304.     DFclose(fdf);
  305.     return(0);
  306. }
  307.  
  308. #ifdef DEBUGxx
  309. /***************************************************************************/
  310. /* specialdebug
  311. *  create a dataset from the image portion of a window.
  312. */
  313. specialdebug(tw)
  314.     struct Mwin *tw;
  315.     {
  316.     unsigned char *p;
  317.     int wid,i,j;
  318.     float *f;
  319.     struct fdatawin *td;
  320.     
  321.     p = tw->vdev.bp;
  322.     
  323.     /* wid = (*(gp->portPixMap))->rowBytes & 0x7fff;     resetting high flag bit */
  324.     wid = 100;
  325.     if (wid < 0 || wid > 1000)
  326.         wid = 100;
  327.         
  328.     td = newdatawin("test",wid,10);
  329.     
  330.     f = td->xvals;
  331.     for (i=0; i<td->xdim; i++)
  332.         *f++ = (float)i;
  333.         
  334.     f = td->yvals;
  335.     for (i=0; i<td->ydim; i++)
  336.         *f++ = (float)i;
  337.         
  338.     f = td->vals;
  339.     for (i=0; i<td->xdim*td->ydim; i++)
  340.         *f++ = (float)*p++;
  341.         
  342.     ctextwin(td);
  343.     
  344. }
  345. #endif
  346.  
  347. /***************************************************************************/
  348. /*  gettext
  349. *  Read in a text file which has the correct format.  Install it into the
  350. *  window list as a text array.
  351. */
  352. gettext(filename)
  353.     char *filename;
  354.     {
  355.     FILE *fp;
  356.     int i,j,xdim,ydim;
  357.     float *f;
  358.     struct fdatawin *td;
  359.     
  360.     if (NULL == (fp = fopen(filename,"r"))) 
  361.         return(-1);
  362.  
  363.     if (2 != fscanf(fp,"%d %d",&ydim,&xdim)
  364.         || xdim < 2 || ydim < 2) {
  365.         return(-1);
  366.     }
  367.     
  368. /*
  369. *   create the dataset, read it in and install it into the window 
  370. *   structure for DataScope.
  371. */
  372.     if (NULL == (td = newdatawin(filename,xdim,ydim)))
  373.         return(-1);
  374.     
  375.     strncpy(td->fname,filename,sizeof(td->fname));        /* keep file name */
  376.     
  377.     if (calcidle()) {
  378.         losedat(td);
  379.         return(-1);
  380.     }
  381. /*
  382. *  look for max/min
  383. */
  384.     if (2 != fscanf(fp,"%e %e",&td->valmax, &td->valmin)) {
  385.         losedat(td);
  386.         return(-1);
  387.     }
  388.  
  389. /*
  390. *  the x and y scale arrays
  391. */
  392.     f = td->yvals;
  393.     for (i=0 ; i<ydim; i++)
  394.         if (1 != fscanf(fp,"%e",f++)) {
  395.             losedat(td);
  396.             return(-1);
  397.         }
  398.     *f = *(f-1);
  399.  
  400.     f = td->xvals;
  401.     for (i=0 ; i<xdim; i++)
  402.         if (1 != fscanf(fp,"%e",f++)) {
  403.             losedat(td);
  404.             return(-1);
  405.         }
  406.     *f = *(f-1);
  407.     
  408.     if (calcidle()) {
  409.         losedat(td);
  410.         return(-1);
  411.     }
  412.         
  413. /*
  414. *  the data array 
  415. */
  416.     f = td->vals;
  417.  
  418.     for (i=0; i< ydim; i++) {
  419.         if (calcidle()) {
  420.             losedat(td);
  421.             return(-1);
  422.         }
  423.         for (j=0; j<xdim; j++)
  424.             if (1 != fscanf(fp,"%e",f++)) {
  425.                 losedat(td);
  426.                 return(-1);
  427.             }
  428.     }
  429.  
  430.  
  431.     fclose(fp);
  432.     
  433. /*
  434. *  If we get this far, then we assume things are ok and we start 'er up.
  435. */
  436.     ctextwin(td);
  437.     
  438. /*
  439. *  If max and min are wrong, re-calculate.
  440. */
  441.     if (td->valmax <= td->valmin) {
  442.         maxmin( td->vals, xdim, ydim,
  443.                 &td->valmax, &td->valmin, 0,0, ydim, xdim);
  444.     }
  445.  
  446.     return(0);
  447.     
  448. }
  449.  
  450. /***************************************************************************/
  451. /* setapal
  452. *  Take a window pointer and save its color palette into a 768 byte look-up
  453. *  table for association with a saved image.
  454. */
  455. setapal(gp)
  456.     WindowPtr gp;
  457.     {
  458.     register int i;
  459.     unsigned char *p,rgb[770];
  460.     RGBColor clor;
  461.     PaletteHandle pal;
  462. /*
  463. *  prep the data structure to be saved.
  464. *  If there is a color map, make arrangements to save it too.
  465. */
  466.         p = NULL;
  467.         pal = GetPalette(gp);
  468.         p = rgb;
  469.         for (i=0; i<255; i++) {
  470.             GetEntryColor(pal, i, &clor);
  471.             *p++ = clor.red >> 8;
  472.             *p++ = clor.green >> 8;
  473.             *p++ = clor.blue >> 8;
  474.         }
  475.         
  476.     DFR8setpalette(rgb);
  477. }
  478.  
  479.  
  480. /***************************************************************************/
  481. /*
  482. *  Save the floating point numbers as an SDG and each image window as a
  483. *  RIG image with appropriate palette.
  484. */
  485.  
  486. savedf(filename,td)
  487.     unsigned char *filename;
  488.     struct fdatawin *td;
  489.     {
  490.     int dims[2],sref,trefs,i,buflen;
  491.     DF *fdf;
  492.     DFdesc dfd;
  493.     char *sp,*spbase,*nt,s[200];
  494.     
  495.     dsinfo.sref = -1;
  496.     dsinfo.bref = -1;
  497.     dsinfo.iref = -1;
  498.     dsinfo.pref = -1;
  499.  
  500.     dims[0] = td->ydim;
  501.     dims[1] = td->xdim;
  502.     
  503.     strncpy(td->fname,filename,sizeof(td->fname));        /* keep file name as we save it */
  504.     td->needsave = 0;                                    /* don't need save anymore */
  505.     
  506.     DFSDsetdims(2, dims);
  507.     
  508. /*
  509. *  Use SDG to store the floating point data.
  510. */
  511.     DFSDsetdatastrs(td->dvar, "", td->fmt, "cartesian");
  512.     DFSDsetdimstrs(1, td->yvar, "", td->labfmt);
  513.     DFSDsetdimstrs(2, td->xvar, "", td->labfmt);
  514.     
  515.     DFSDsetdimscale(1, dims[0], td->yvals);
  516.     DFSDsetdimscale(2, dims[1], td->xvals);
  517.     
  518.     DFSDsetmaxmin(td->valmax, td->valmin);
  519.             
  520.     DFSDputdata(filename, 2, dims, td->vals);
  521.     
  522.     dsinfo.sref = sref = DFSDlastref();            /* ref # for SDG we just put out */
  523.     dfd.tag    = DFTAG_SDG;
  524.     dfd.ref = sref;                                /* save info for DIA storage */
  525. /*
  526. *  If there are associated images, save them, too. 
  527. */
  528.     
  529.     DFR8restart();
  530.     if (td->image) {
  531.         setapal(td->image->win);
  532.         DFR8addimage(filename, td->image->vdev.bp, 
  533.         td->image->vdev.bounds.right - td->image->vdev.bounds.left, 
  534.         td->image->vdev.bounds.bottom - td->image->vdev.bounds.top, 0);
  535.         dsinfo.bref = DFR8lastref();
  536.         dsinfo.bxx = td->image->iw->exx;
  537.         dsinfo.bxy = td->image->iw->exy;
  538.     }
  539.         
  540.     if (td->interp) {
  541.         setapal(td->interp->win);
  542.         DFR8addimage(filename, td->interp->vdev.bp, 
  543.             (long) td->interp->bw->xsize, 
  544.             (long) td->interp->bw->ysize, (long) 0);
  545.         dsinfo.iref = DFR8lastref();
  546.         dsinfo.dxsize = td->interp->bw->xsize;
  547.         dsinfo.dysize = td->interp->bw->ysize;
  548.     }
  549.  
  550.     if (td->polar) {
  551.         setapal(td->polar->win);
  552.         DFR8addimage(filename, td->polar->vdev.bp, 
  553.         td->polar->vdev.bounds.right - td->polar->vdev.bounds.left, 
  554.         td->polar->vdev.bounds.bottom - td->polar->vdev.bounds.top, 0);
  555.         dsinfo.pref = DFR8lastref();
  556.         dsinfo.pxx = td->polar->pw->exx;
  557.         dsinfo.dleft = td->polar->pw->viewport.left;
  558.         dsinfo.dtop = td->polar->pw->viewport.top;
  559.         dsinfo.dright = td->polar->pw->viewport.right;
  560.         dsinfo.dbottom = td->polar->pw->viewport.bottom;
  561.         dsinfo.ashift = td->polar->pw->angleshift;
  562.     }
  563.     
  564.         
  565. /*
  566. *  save notebook too, if we can
  567. */
  568.     if (td->notes) {
  569.         td->content = TEGetText(td->notes->nw->trec);            /* get the text handle */
  570.         td->contentlen = (*td->notes->nw->trec)->teLength;        /* length of that text */
  571.             
  572.         if (NULL == (fdf = DFopen(filename,DFACC_WRITE,16))) {
  573.             return(-1);
  574.         }
  575.     
  576.         DFaccess( fdf, DFTAG_DIA, sref, "w");        /* open annotation */
  577.     
  578.         DFwrite( fdf, &dfd.tag, 4);                    /* write tag/ref into DIA */
  579.         
  580.         spbase = sp = (char *) NewPtr(2010);
  581.         HLock(( Handle) td->content);
  582.         nt = (char *) *td->content;                    /* pointer to notebook */
  583.         buflen = td->contentlen;
  584.         i = 0;                                        /* make sure all init'ed, no stupidity */
  585.         while (buflen > 0) {
  586.             while (buflen > 0 && i < 2000) {
  587.                 *sp++ = *nt;
  588.                 if (*nt++ == 13) {                    /* augment EOLs with LFs */
  589.                     *sp++ = 10;
  590.                     i++;
  591.                 }
  592.                 --buflen;
  593.                 i++;
  594.             }
  595.             DFwrite( fdf, spbase, i);
  596.             sp = spbase;
  597.             i=0;
  598.         }
  599.             
  600.         DFwrite( fdf, &i, 4);                        /* write out zeros */
  601.                 
  602.         DisposPtr(spbase);                                /* give back temporary mem */
  603.         HUnlock((Handle) td->content);
  604.     
  605.         DFclose(fdf);
  606.  
  607.     }
  608.  
  609. /*
  610. *  Write out custom record.
  611. *  sprintf it into a string and then write it into the HDF file.
  612. */
  613.     if (NULL == (fdf = DFopen(filename,DFACC_WRITE,16))) {
  614.         return(-1);
  615.     }
  616.     sprintf(s,"# DataScope record \n block=%d,%d,%d\n vimage=%d,%d,%d\n polar=%d,%d\n pview=%d,%d,%d,%d,%d\n",
  617.         dsinfo.bref,dsinfo.bxx,dsinfo.bxy,
  618.         dsinfo.iref,dsinfo.dxsize,dsinfo.dysize,
  619.         dsinfo.pref,dsinfo.pxx,
  620.         dsinfo.ashift,dsinfo.dleft,dsinfo.dtop,dsinfo.dright,dsinfo.dbottom);
  621.  
  622.     DFputelement(fdf, 8000, dsinfo.sref, s, strlen(s));
  623.     
  624.     DFclose(fdf);
  625.     
  626.     return(0);
  627. }
  628.  
  629. /***************************************************************************/
  630. /*  maxmin
  631. *   Calculate the maximum and minimum values for a region in the array.
  632. *
  633. */
  634. maxmin( arr, xd, yd, mx, mn, ptop, pleft, pbottom, pright )
  635.     float *arr, *mx, *mn;
  636.     int xd,yd,ptop,pleft,pbottom,pright;
  637.     {
  638.     register int i;
  639.     int skip,line;
  640.     register float *f,tmax,tmin;
  641.  
  642.     skip = xd - (pright - pleft);        /*  remainder of each row */
  643.     
  644.     f = arr + ptop*xd + pleft;            /* offset of start */
  645.     tmax = tmin = *f;                    /* set to first value */
  646.  
  647.     for (line=ptop; line < pbottom; line++) {
  648.         for (i=pleft; i<pright; i++) {
  649.             if (*f > tmax)                /*  new max ? */
  650.                 tmax = *f;
  651.             else if (*f < tmin)            /*  new min ? */
  652.                 tmin = *f;
  653.             f++;
  654.         }
  655.         f += skip;
  656.     }
  657.  
  658.     *mx = tmax;
  659.     *mn = tmin;
  660. }
  661.  
  662.