Exemple #1
0
 void stackmen_buta(Widget iw_temp, XtPointer data, XtPointer call_data)
 {
 char *string = NULL;
 int  vval;

 /* Get the state of toggle */
 all     = XmToggleButtonGadgetGetState(iw_all);
 single  = XmToggleButtonGadgetGetState(iw_single);
 section = XmToggleButtonGadgetGetState(iw_section);

 /* No question for this in this menu, so default it to FALSE */
 montaverage = FALSE;

 /* Get the num for stkgo and stkend */
 if (single)
    {
    string = XmTextGetString(iw_single1);
     
    /* Find file numbers wanted */
    if (sscanf(string,"%d",&vval) <= 0 || vval < 0)
       {   /* Can not interpret the image number */
       spout("*** Can not parse image number! ");
       if (string) XtFree (string); return;
       }
    stkgo  = vval;
    stkend = vval;
    }
 else if (section) 
    {
    /* Get stkgo */
    string = XmTextGetString(iw_stkgo);

    if (sscanf(string,"%d",&vval) <= 0 || vval < 0)
        {   /* Can not interpret vval */
        spout("*** Can not parse starting image number! ");
        if (string) XtFree (string); return;
        }
    else 
	stkgo = vval;

    /* Get stkend */
    string = XmTextGetString(iw_stkend);
    if (sscanf(string,"%d",&vval) <= 0 || vval < 0)
       {   /* Can not interpret vval */
       spout("*** Can not parse ending image number ending! ");
       if (string) XtFree (string); return;
       }
    else 
       stkend = vval;

    if (string) XtFree (string);
    }

 /*  Remove the stack menu */
 XtUnmanageChild(iw_stackmen); 
 expos_cb(NULL,NULL,NULL);

 /*  Display the montage, imagego set in imagemen */
 stackdis(imagego,imageend);
 }
Exemple #2
0
 void fillmen_buta(Widget iw_temp, XtPointer data, XtPointer call_data)
 {
 char *         string;
 char           filtertype[2];

 if (strtoint(iw_ixseed,"X seed",&ixseed) == FALSE) return;

 if (strtoint(iw_iyseed,"Y seed", &iyseed) == FALSE) return;

 if (ixseed  < 1 || ixseed  > nsamold) 
    {
    spout("*** X seed range is: 1...nsam"); 
    return;
    }

 if (iyseed < 1 || iyseed > nrowold) 
    {
    spout("*** Y seed range is: 1...nrow"); 
    return;
    }

 if (strtoint(iw_fillconnect,"Connectivity",&fillconnect) == FALSE) return;
 if (fillconnect != 4 && fillconnect != 8) 
    {
    spout("*** Connectivity must be 4 or 8!"); 
    return;
    }

 /* find threshold */
 string = XmTextGetString(iw_thresh);
 sscanf(string,"%f",&threshval);
 if (threshval > fmaxold) threshval = fmaxold;
 if (threshval < fminold) threshval = fminold;
 if (string) free(string);

 /* find fillval */
 string = XmTextGetString(iw_fillval);
 sscanf(string,"%f",&fillval);
 if (string) free(string);

 XtUnmanageChild(iw_fillmen);

 filtertype[0] = 'x';
 filtertype[1] = '\0';
 filter_but(iw_temp, (XtPointer) filtertype, call_data);
 }
Exemple #3
0
void
main(int argc, char *argv[])
{
	int i, fd;

	Binit(&bout, 1, OWRITE);
	if(argc == 1)
		spout(0, "");
	else
		for(i=1; i<argc; i++){
			fd = open(argv[i], OREAD);
			if(fd < 0){
				fprint(2, "spell: can't open %s: %r\n", argv[i]);
				continue;
			}
			spout(fd, argv[i]);
			close(fd);
		}
	exits(nil);
}
Exemple #4
0
 void pickbackmen_buta(Widget iw_temp, XtPointer data, 
                                       XtPointer call_data)
 {
 char * string;

 /* Find window size */
 string = XmTextGetString(iw_winsiz);
 sscanf(string,"%d",&nsamw);
 if (string) free(string);

 if (nsamw <= 0) 
    { spout("*** Window size must be > 0"); XBell(idispl,50); return; }
 nroww = nsamw;

 /* Find next window key */
 string = XmTextGetString(iw_winkey);
 sscanf(string,"%d",&back_win_now);
 if (string) free(string);

 //printf(" pickbackmen: back_win_now %d\n",back_win_now);
 
 if (back_win_now <= 0) 
    {back_win_now = 1;}

 /* Set default cursor */
 setacursor(0, -1, -1);

 /* Create an xor graphics context if not currently valid */
 if (!icontxor) icontxor = setxor(icontx);

 /* Set default color in xor GC */
 wicolor(icontxor,colorgo+icolorov);

 /* Start background window picking */
 pickback(firstback);

 }
Exemple #5
0
 int filter(float * oldimage, int nsamt, int nrowt, 
            float * kernal,   int nsamk, int nrowk, 
            float * newimage, float * fmint, float * fmaxt)
 {
 int     ix, iy,  ixk, iyk, nrowkd2, nsamkd2, it;
 float   val,valk,vali, ksum;
 int     icon1, icon2, icon3, icon4, icon5;
 int     ixo,iyo;
 int     ip;
 float * pf;
 float * endimage;

 if (nsamk % 2 == 0 || nrowk % 2 == 0)
    {
    spout("*** Filter kernal size must be odd!");
    return FALSE;
    }

 nrowkd2 = nrowk / 2;
 nsamkd2 = nsamk / 2;

 /* sum up kernal factors */
 ksum = 0.0;
 for (pf = kernal; pf < kernal + nsamk * nrowk; pf++)
    ksum += *pf;

/*******************
 printf("ksum: %f \n",ksum);
********/

 *fmint = FLT_MAX;
 *fmaxt = FLT_MIN;

 /* convolve over all positions in the new image except border */

 for (iy = nrowkd2; iy < nrowt - nrowkd2; iy++)
    {
    icon5 = iy * nsamt;
    icon3 = iy - nrowkd2;

    for (ix = nsamkd2; ix < nsamt - nsamkd2; ix++)
       {
       icon2 = ix - nsamkd2;
       val   = 0.0;

       /* convolve over all positions in the kernal */
       for (iyk = 0; iyk < nrowk; iyk++)
          {
          icon1    = iyk * nrowk;
          /* iyo   = icon3 + iyk; */
          /* icon4 = icon2 + iyo * nrowt; */
          icon4    = icon2 + (icon3 + iyk) * nsamt;

          for (ixk = 0; ixk < nsamk; ixk++)
             {
             /* get current kernal value     */
             /* valk = kernal[icon1 + ixk];  */
             /* ixo = ix - nsamkd2 + ixk;    */
             /* get current image value      */
             /* vali = oldimage[icon4 + ixo];*/

             val += kernal[icon1 + ixk] * oldimage[icon4 + ixk];
             }
          }
       newimage[icon5 + ix] = val;

       *fmaxt = MYMAX(*fmaxt,val);
       *fmint = MYMIN(*fmint,val);
       }
     }

  /* fill in border with minimum value */

  endimage = newimage + nsamt * nrowt - 1;

  /* fill top and bottem borders */
  for(ip = 0; ip < nsamt * nrowkd2; ip++)
     {
     *(newimage + ip) = *fmint;
     *(endimage - ip) = *fmint;
     }

  /* fill left and right borders */
   for(it = 0; it < nsamkd2; it++)
     {    
     for(ip = it; ip < nsamt*nrowt; ip=ip+nsamt)
        {
        *(newimage + ip) = *fmint; 
        *(endimage - ip) = *fmint;
        }
     }

  return TRUE;
}
Exemple #6
0
void comap(void)
{
  int     k;
  int     maxkeys, icolort,
          maxreg, ix, iy, numtot, numcor, lastkey,
          doc_read, pkey;
  float   *plist = NULL, *ptr, *dbuf, xt, yt;
  double  davx, davx2, davy, davy2, bx, by, xmin, xmax, ymin, ymax, xr, yr,
          sigx, sigy, xrange, yrange;
  char    outstr[81];
     
  /* Set wait cursor */
  waitcursor(iwin, TRUE, 79);                                  
  
  /* 
  Recover all the lines from the doc file and store in the dbuf array.
  column 1 is the key, following columns are registers. correspondence
  analysis may have up to 24 registers. */

  doc_read = TRUE;
  pkey     = 0;
  maxkeys  = MYMAX(key1,key2) + 1;
  maxreg   = MYMAX(itxreg,ityreg) + 1;
  dbuf     = (float *) NULL;
  if (unsdal(docxnam, &doc_read, pkey, plist, maxreg, &dbuf, &lastkey,
                   maxkeys,maxreg) > 0)
      {
      /* Problem retrieving doc file, assume it does not exist */
      if (dbuf) free(dbuf);
      sprintf(outstr," Could not read document file: %s",docxnam);
      spout(outstr);

      /* unset wait cursor */
      waitcursor(iwin, FALSE, 0);                                  
      return;
      }
  
  if (lastkey < MYMIN(key1,key2))
      {
      if (dbuf) free(dbuf);
      sprintf(outstr," both keys needed %d & %d > lastkey = %d",
         key1,key2,lastkey);
      spout(outstr);

      /* unset wait cursor */
      waitcursor(iwin, FALSE, 0);                                  
      return;
      } 
      
  /* Determine maximum/minimum x and y coordinates, avg, etc; */
  xmin = ymin = FLT_MAX;
  xmax = ymax = -xmin;

  davx = davx2 = davy = davy2 = 0; 

  /* Run time allocate of memory space for nkey[], bufx, bufy */
  if ( (nkey = (int *) calloc((key1 * key2), sizeof(float)) )
             == (int *) NULL)                                       {
     spout(" *** Could not allocate memory");
     waitcursor(iwin, FALSE, 0);  return;                           }
  if ( (bufx = (float *) calloc((key1 * key2), sizeof(float)) )
             == (float *) NULL)                                     {
     spout(" *** Could not allocate memory");
     waitcursor(iwin, FALSE, 0);  return;                           }
  if ( (bufy = (float *) calloc((key1 * key2), sizeof(float)) )
             == (float *) NULL)                                     {
     spout(" ***Could not allocate memory");
     waitcursor(iwin, FALSE, 0);  return;                           }

  /* Remember current color in icontx */
  icolort = icolor;

  /* Set current color to image almost white */
  wicolor(icontx,colorgo+2);

  /* Clear the screen */
  //clearover(NULL,NULL,NULL);

  ptr   = dbuf + (key1-1) * maxreg;
  nline = 0;

  for (k = key1; k <= key2; k++)
      {
      /* Check that the key has some registers. */
      if ( (*ptr) > 0.0) 
          {
          nkey[nline] = k;          /*  image number      */
          bx = *(ptr + itxreg);     /*  dbuf[k][itxreg]   */
          by = *(ptr + ityreg);     /*  dbuf[k][ityreg]   */

          /* Record bx & by */
          bufx[nline] = bx;
          bufy[nline] = by;
          nline++;
    
          /* Record max and min for bx & by   */
          xmin = MYMIN(bx,xmin);
          xmax = MYMAX(bx,xmax);
          ymin = MYMIN(by,ymin);
          ymax = MYMAX(by,ymax);

          /* record average and standard deviation for bx & by */
          davx  += bx;
          davx2 += (double)(bx * bx);
          davy  += by;
          davy2 += (double)( by * by);
          }
      ptr += maxreg;
      }        /*   for (pkey = key1; pkey <= key2; pkey++)  */

  /* Calculate sigma and average */
  davx  /= (float) nline;
  davy  /= (float) nline;

  sigx   = sqrt((davx2 - (davx * davx / (float) nline)) / (double) (nline - 1));
  sigy   = sqrt((davy2 - (davy * davy / (float) nline)) / (double) (nline - 1));

  xt     = 2.0 * stndrt * sigx;
  yt     = 2.0 * stndrt * sigy;

  xr     = xmax - xmin;
  yr     = ymax - ymin;

  xrange = MYMIN(xt, xr);
  yrange = MYMIN(yt, yr);

  /* Set plot size */
  ixsize = iwidex - 2;
  iysize = ihighx - 2;
  ixmar  = iymar = 1;
  numtot = numcor = 0;

  /* Find scale factors */
  factxa = (float) (ixsize -1) / xrange;
  factxb = - (xmin * factxa) + ixmar + 0.5;

  factya = (float) (iysize-1) / yrange;
  factyb = - (ymin * factya) + iymar + 0.5;

  for (k = 0; k < nline; k++)
    {
      ix = (bufx[k] - xmin) * factxa + ixmar + 0.5;
      iy = (bufy[k] - ymin) * factya + iymar + 0.5;

      if (ix >= ixmar && ix <= ixsize && iy >= iymar && iy <= iysize)
          { /* Image is inside the plot extent; find the label number */

          if (sayno)
              {  /* write image number at this location on plot */
              sprintf(outstr,"%d",nkey[k]);
              //           itsize,just,  inten,ipen,  overprint
              witext(icontx,outstr,ix,iy, 0,0, -9,2,  FALSE);
              }
          else
              { 
              /* Draw a permanent circle at this position */
              xorc(iwin,    icontx, TRUE, ix, iy, itrad);
              xorc(imagsav, icontx, TRUE, ix, iy, itrad);
              }

          /* Increment total number of images within plot extent */
          numcor += 1;
          }
      else
        {
        /* Image is outside of plot extent */
        nkey[k] = - nkey[k];
        }

    numtot += 1;   /* Increment total number of images */
    }
  sprintf(outstr," Images: %d   Images within plot: %d",numtot,numcor);
  spout(outstr);
     
  /* Replace current color in icontx */
  wicolor(icontx,icolort);

  /* Initialize bin counter for this map */
  nowbin = 0;

  /* Set flag for each bin mask */
  gotbin = gotavim = gotav = FALSE;

  /* Cancel wait cursor */
  waitcursor(iwin, FALSE, 0);

  /* Start masking of correspondence map */
  corr_mask(0);
}
Exemple #7
0
 void dist_pop(Widget iw_t, XEvent *event, String *params,
               Cardinal *num_params)
 {
 int   dx, dy;	       /* the distance for x-y axis */
 int   ixi, iyi;
 char  outstr[80];     /* print out string */

 if (!(strcmp(*params, "M")))
    {                           /* List location ---- mouse moved */
    getloc(event,'m',&ix1,&iy1);
    if (dist_inimag) 
       {     /* find location relative to last image */
       ixi = ix1 - ixul + 1;     iyi = iy1 - iyul + 1;
       }

    if (ndist < 0)  	/* the starting point flag */
       /* haven't got the starting point yet */
       return;

    else if (dist_inimag && (ixi < 1 || ixi > nsam ||
                             iyi < 1 || iyi > nrow))
       {    /* cursor is outside of image, only want inside */
       sprintf(outstr,"*** Outside: (%d,%d)$", ixi,iyi);
       spout(outstr);

       /* draw line */
       xorline(iwin, icontxx, FALSE, ix0, iy0, ix1, iy1);
       }

    else 
       {      /* report the distance, don't save it */     
       dx = ix1 - ix0;    dy = iy1 - iy0;
       distance = sqrt((float)(dx*dx + dy*dy));
       sprintf(outstr,"*** Distance: %f ****$", distance);
       spout(outstr);

       xorline(iwin, icontxx, FALSE, ix0, iy0, ix1, iy1);
       }
    }

 else if (!(strcmp(*params, "1")))
    {                     /*  List location -------- button 1 pushed */
    getloc(event,'B',&ix1,&iy1);

    if (dist_inimag) 
       {     /* find location relative to last image */
       ixi = ix1 - ixul + 1;     iyi = iy1 - iyul + 1;
       }

    if (ndist < 0)	  
       {   /* set starting point for distance listing */
       ndist = 0;
       ix0   = ix1;     iy0   = iy1;
       return;
       }

    else if (dist_inimag && ( ixi < 1 || ixi > nsam ||
                              iyi < 1 || iyi > nrow))
       {    /* cursor is outside of image, only want inside */
       sprintf(outstr,"*** Outside image: (%d,%d)", ixi,iyi);
       spout(outstr);
       }

    else {
       /* record the distance */

       if (dist_leavit) 
	  { 	/* draw a permanent line along the distance */
	  xorline(iwin,    icontxx, TRUE, ix0, iy0, ix1, iy1);
	  xorline(iwin,    icontxx, TRUE, ix0, iy0, ix1, iy1);
	  xorline(imagsav, icontxx, TRUE, ix0, iy0, ix1, iy1);
	  }
 
       ndist++;

       if (dist_docit)
	  {	/* want to save this distance for doc file */

	  if (ndist > MAXREG )
	     {	/* too many distances from this starting point for doc file */
       	     spout("*** Document key is full, maximum line # is 7");
	     ndist = MAXREG;
	     }

	  else 
	     {	/* save the location for entry in doc file */
	     dlist[ndist] = distance;
       	     sprintf(outstr,"*** Selected: #%d: %d  %f", ikey, 
                            ndist - 1, distance);
       	     spout(outstr);
             if (singledist) 
                {    /* only want one distance from this start */
                if (dist_docit > 0)
                   {      /* save the current distance line */
                   dlist[0] = ikey++;
                   dlist[2] = ix0;
                   dlist[3] = iy0;
                   dlist[4] = ix1;
                   dlist[5] = iy1;
                   fpdocpix = savdn1(docnam, datexc, &fpdocpix, 
                            dlist, 6, &opendistdoc, 
                            TRUE, TRUE);
                   }
                ndist    = 0;
                ix0      = ix1;     iy0   = iy1;
                distance = 0;
                }
	     }
	  }
       }
    }

 else if (!(strcmp(*params, "2")))
    {                     /*  display the dist_menu */
    /* remove current actions */
    XtUninstallTranslations(iw_win);
    xorline(iwin, icontxx, TRUE, ix0, iy0, ix1, iy1);

    /* show menu */
    showbutx(NULL, NULL, NULL, TRUE);
    distmen(NULL, NULL, NULL);
    }

 else if (!(strcmp(*params, "3")))
    {                     /*  set a new starting point */
    if (ndist > 0) 
       /* remove the current line */
       xorline(iwin, icontxx, TRUE, ix0, iy0, ix1, iy1);

    if ( !singledist) 
       {     
       if (dist_docit > 0 && ndist > 0)
          {      /* first save the current distance line */
          dlist[0] = ikey;
          fpdocpix    = savdn1(docnam, datexc, &fpdocpix, dlist, ndist+1, 
                            &opendistdoc, TRUE, TRUE);
          ikey++;
          }
       }
    ndist = -1;
    }
 }
Exemple #8
0
 int ztoxy(FILEDATA *filedata, char *lbuf, 
            char axis, float fn, float fncon, int iskip)
 {  

 int        nsamd, nrowd, nsliced, icon;
 int        npixpsl, miskip, iskipz, icons, irect;
 int        iconr, ival, iloc, l, m, iloct; 
 int        isam, irow, islice;
 float      *buf;

 /* set image size and window */
 if (axis == 'z')
     {        /* 3-d file with z slice */
     dispsize(nsam1,nsam2,nrow1,nrow2,nslice1,nslice2,iskip, &nsamd, &nrowd, &nsliced);
     }
 else if (axis == 'y')
     {        /* y slice */
     dispsize(nsam1,nsam2,nslice1,nslice2,nrow1,nrow2,iskip, &nsamd, &nrowd, &nsliced);
     }
 else if (axis == 'x')
     {        /* x slice */
     dispsize(nrow1,nrow2,nslice1,nslice2,nsam1,nsam2,iskip, &nsamd, &nrowd, &nsliced);
     }
 else 
    {
    spout(" *** IN ZTOXY: UNSUPPORTED AXIS");
    return FALSE;
    }

 buf      = filedata -> buf;
 if (iskip > -1 && iskip < 1) iskip = 1;
 npixpsl  = nrowd * nsamd;
 miskip   = abs(iskip);

 if (iskip >= 1)
    {   /* display at full or reduced size */

    if (axis == 'y')
       {
       icon = -nrow1 * npixpsl - nslice1 * nsamd / iskip -  nsam1 / iskip ;  
       }
    if (axis == 'x')
       {
       icon = -nsam1 * npixpsl - nslice1 * nsamd / iskip -  nrow1 / iskip;
       }
    }
 else if (iskip < -1)
    {   /* display magnified image */
    if (axis == 'y')
       icon = -nrow1 * npixpsl - nslice1 * miskip * nsamd - 1;
    else
       icon = -nsam1 * npixpsl - nslice1 * miskip * nsamd -
               nrow1 * miskip;
    }

 /* find skip factors for z axis */
 iskipz = MYMAX(1,iskip);
 
 for (islice=nslice1; islice<=nslice2; islice+=iskipz)
   {
   if (axis == 'y')
      {                                                     /* y axis */
      if (iskip < 0)
         icons = islice * nsamd * miskip + icon;
      else 
         icons = islice * nsamd / iskip  + icon;

      for (irow=nrow1; irow <= nrow2; irow+=iskipz)
         {
         irect = (islice - 1) * nrow + irow;
         if (redlin(filedata,buf,nsam,irect) == FALSE)  return FALSE;

         if (iskip >= 1)
            {  /* display full or reduced size y slice */

            iconr = irow * npixpsl + icons;
            for (isam=nsam1; isam <= nsam2; isam+=miskip)
               {
               ival  = buf[isam-1] * fn + fncon;
               if (ival >= 128) ival = ival - 256;

               iloc = isam / iskip + iconr;

/***************bug ************/
   if (iloc < 0 || iloc > nsamd * nrowd * nsliced -1)
    printf("illegal iloc: %d, irow: %d, isam: %d \n",iloc,irow,isam);

/********************/

               lbuf[iloc] = ival;
               }         
            }
            
         else if (iskip < -1) 
            {   /* display magnified image y slice */

            iloc = irow * npixpsl + icons;
            
            for (isam=nsam1-1; isam < nsam2; isam++)
               {
               ival  = buf[isam] * fn + fncon;
               if (ival >= 128) ival = ival - 256;

               for (l = 0; l < miskip; l++)
                  {
                  iloc++;
                  for (m = 0; m < miskip; m++)
                     {
/***************bug ************/
   iloct = iloc + m * nsamd;
   if (iloct < 0 || iloct > nsamd * nrowd * nsliced - 1) 
    {
    printf("illegal iloct: %d, irow: %d, isam: %d \n",iloct,irow,isam);
    printf("icons: %d, iconr: %d, npixpsl: %d \n",icons,iconr,npixpsl);
    printf("l: %d, m: %d  \n",l,m);
    }
/********************/
                     lbuf[iloc + m * nsamd] = ival;
                     }
                  }
               }      
            }
         }
      }
      
   else if (axis == 'x')
      {                                                   /*  x axis */ 
      if (iskip >= 1)
         {   /* full or reduced size x slice */
         icons = islice * nsamd / iskip + icon;

         for (irow=nrow1; irow <= nrow2; irow+=miskip)
            {
            irect = (islice - 1) * nrow + irow;
            if (redlin(filedata,buf,nsam,irect) == FALSE)  return FALSE;
            iconr = irow / iskip + icons;

            for (isam=nsam1; isam <= nsam2; isam++)
               {
               ival  = buf[isam-1] * fn + fncon;
               if (ival >= 128) ival = ival - 256;

               iloc = isam * npixpsl + iconr;
/***************bug ************/
   if (iloc < 0 || iloc > nsamd * nrowd * nsliced -1) {
    printf("illegal iloc: %d, irow: %d, isam: %d \n",iloc,irow,isam);
    printf("icons: %d, iconr: %d, npixpsl: %d \n",icons,iconr,npixpsl);
     }
/********************/
               lbuf[iloc] = ival;
               }         
            }
         }
      else if (iskip < -1)
         {   /* magnifed x slice */ 

         icons = islice * nsamd * miskip + icon;

         for (irow=nrow1; irow <= nrow2; irow++)
            {
            irect = (islice - 1) * nrow + irow;
            if (redlin(filedata,buf,nsam,irect) == FALSE) return FALSE;

            iconr = irow * miskip + icons;

            for (isam=nsam1; isam <= nsam2; isam++)
               {
               ival  = buf[isam-1] * fn + fncon;
               if (ival >= 128) ival = ival - 256;

               iloc = isam * npixpsl + iconr;
 
               for (l = 0; l < miskip; l++)
                  {
                  for (m = 0; m < miskip; m++)
                     {
        
/***************bug ************/
   iloct = iloc + m * nsamd;
   if (iloct < 0 || iloct > nsamd * nrowd * nsliced -1) 
    {
    printf("illegal iloct: %d, irow: %d, isam: %d \n",iloct,irow,isam);
    printf("icons: %d, iconr: %d, npixpsl: %d \n",icons,iconr,npixpsl);
    printf("l: %d, m: %d nsamd: %d ,icon %d \n",l,m,nsamd,icon);
    }
/********************/
                     lbuf[iloc + m * nsamd] = ival;
                     }
                  iloc++;
                  }
               }
            }
         }
      }
 

   else if (axis == 'z')
      {                                                  /*  z axis  */ 
      iloc  = (islice - nslice1) * npixpsl;

      if (iskip >= 1)
         {   /* full size z slice or  reduced size z slice */

         for (irow=nrow1; irow <= nrow2; irow+=miskip)
            {
            irect = (islice - 1) * nrow + irow;
            if (redlin(filedata,buf,nsam,irect) == FALSE)  
               {return FALSE;}

            for (isam=nsam1; isam <= nsam2; isam++)
               {
               ival       = buf[isam-1] * fn + fncon;
               if (ival >= 128) ival = ival - 256;
               lbuf[iloc++] = ival;
               }         
            }
         }
      else if (iskip < -1)
         {   /* magnifed z slice */ 

         for (irow=nrow1; irow <= nrow2; irow++)
            {
            irect = (islice - 1) * nrow + irow;
            if (redlin(filedata,buf,nsam,irect) == FALSE) 
               {return FALSE;}

            for (isam=nsam1; isam <= nsam2; isam++)
               {
               ival  = buf[isam-1] * fn + fncon;
               if (ival >= 128) ival = ival - 256;

               for (l = 0; l < miskip; l++)
                  {
                  for (m = 0; m < miskip; m++)
                     {lbuf[iloc + m * nsamd] = ival;}
                  iloc++;
                  }
               }
            iloc += nsamd * (miskip - 1);         
            }
         }
      }
   }

 return TRUE;
 }       
Exemple #9
0
 int filterfill(float * oldimage, int nsamt, int nrowt, 
             int ixseed, int iyseed, float thresh, float fval, 
             float * newimage, float * fmint, float * fmaxt,
             int connectivity)
 {
 int     nrowkd2, nsamkd2;
 float * endimage;
 int   * endimagei;
 float * fptr;
 int     ip, it, iloc;

 /* set border size */
 nrowkd2 = 1;
 nsamkd2 = 1;

 thresht   = thresh;
 fvalt     = fval;
 newimaget = newimage;
 oldimaget = oldimage;
 nsamtt    = nsamt;
 connect8  = TRUE;
 if (connectivity == 4) connect8 = FALSE;

 /* copy oldimage to newimage */
 memcpy((void *) newimage,(void *) oldimage, nsamt*nrowt*4);
 
 /* allocate visited space */
 if ((visited = (int *) calloc(nsamt * nrowt ,
                      sizeof(int))) == (int *) 0)
    {spout("*** Unable to allocate visited in filterfill."); 
     return FALSE;}

 /* fill top and bottem borders of visited to prevent going out */
 /* borders of the image */

 endimagei = visited + nsamt * nrowt - 1;

 for(ip = 0; ip < nsamt * nrowkd2; ip++)
     {
     *(visited   + ip) = TRUE;
     *(endimagei - ip) = TRUE;
     }

 /* fill left and right borders */
 for(it = 0; it < nsamkd2; it++)
     {    
     for(ip = it; ip < nsamt*nrowt; ip=ip+nsamt)
        {
        *(visited   + ip) = TRUE; 
        *(endimagei - ip) = TRUE;
        }
     }

 /* do a flood fill around seed location */

 iloc = (iyseed-1) * nrowt + ixseed - 1;
 flood(iloc);

 /* fill in border with fval value */
 endimage = newimage + nsamt * nrowt - 1;

 /* fill top and bottem borders */
 for(ip = 0; ip < nsamt * nrowkd2; ip++)
     {
     *(newimage + ip) = fvalt;
     *(endimage - ip) = fvalt;
     }

 /* fill left and right borders */
 for(it = 0; it < nsamkd2; it++)
     {    
     for(ip = it; ip < nsamt*nrowt; ip=ip+nsamt)
        {
        *(newimage + ip) = fvalt; 
        *(endimage - ip) = fvalt;
        }
     }

 /* check min and max on final image */
 for (fptr= newimage; fptr < newimage + nsamt * nrowt; fptr++)
      {
      if (*fptr < *fmint) *fmint = *fptr;
      if (*fptr > *fmaxt) *fmaxt = *fptr;
      }

 if (visited) 
     {free(visited); visited = NULL; }

 return TRUE;
 }
Exemple #10
0
 void backmen_butm(Widget iw_temp, XtPointer data, 
                                   XtPointer call_data)

 {
 int             nindex, ival, iflags, indx;  
 float           fval,ft;
 int             ngo_com, nend_com;  

 /* Get new ngo & nend */
 XmScaleGetValue(iw_ngob,  &ngo_com);
 XmScaleGetValue(iw_nendb, &nend_com);

 if (nend_com < ngo_com)
    { spout("*** Upper value must be > lower value"); return;}
 else if (ngo_com < imagego)                                            
    { spout("*** Lower value outside of image ramp"); return;}
 else if (nend_com > imageend)                                         
    { spout("*** Upper value outside of image ramp"); return;}

 // Use ngo to signal ramp info
 ngo  = ngo_com;
 nend = nend_com;
 
#ifdef WEB_TRUE
// WEB_TRUE ------------------------------------------- WEB_TRUE

 // Uses virtual cmap
 newvmap(ngo,nend,vmap);
 neednewmap = TRUE;

 // Applies virtual cmap
 expos_cb(NULL,NULL,NULL);

#else
// WEB_24 --------------------------------------------- WEB_24


 /* Set the colors in the image portion of the colormap, only */ 

 iflags = DoRed | DoGreen | DoBlue; 
 indx   = 0;
 ft     = 65535.0 / ((float) (nend - ngo));

 for (nindex = mapgo; nindex <= imageend; nindex++)
    {
    colort[indx].pixel = ispicol[nindex];
    colort[indx].flags = iflags;

    if (nindex <= ngo) 
       { ival = 0; }     /* Set this color to black */
    else if (nindex < nend)
       {                 
       /* Interpolate shade */
       fval = (float)(nindex - ngo) * ft;
       ival = fval;
       }
    else 
       { ival = 65535; } /* Set this color to white */

    colort[indx].red     = ival;
    colort[indx].green   = ival;
    colort[indx++].blue  = ival;
    }
 
 /* Put the colors in the desired colormap */
 XStoreColors(idispl, map, colort, indx);

// BOTH --------------------------------------------- BOTH
#endif

 XFlush(idispl);
 return;
 }
Exemple #11
0
 int getoldstack(FILEDATA * filedatat, int imgnum, int wantnext, int sayit)

 {   
 float	    header[256];
 char	    outstr[100];
 int        inuse, nsam, nrow, headbyt, nslice;

  //printf("imgnum   %d \n", imgnum);
  if (imgnum > filedatat->maxim)
     { return 0; }

  /* Set offset for reading header from file */
  headbyt = filedatat->headbyt;
  nsam    = filedatat->nsam;
  nrow    = filedatat->nrow;
  nslice  = filedatat->nslice;

  filedatat->offset = headbyt + 
     (imgnum - 1) *  (headbyt + nsam * nrow * nslice * 4);

  inuse = FALSE;

  while (!inuse)
     {    
     /* Read header of stacked image file */
     if (!redlin(filedatat,header,256,0)) return -1;

     inuse = (header[26] != 0);

     if (!inuse && !wantnext) 
        {  /* Desired image does not exist, have error */
        printf("Desired image not in use. \n");
        return -1;
        }
     else if (!inuse && wantnext)
        {  /* Image does not exist, try next one */
        imgnum++; 
        if (imgnum > filedatat->maxim) return 0;
        }
     }  /* Continue until an image is obtained */
 
  filedatat -> imami    = header[5];
  filedatat -> fmax_spi = header[6];
  filedatat -> fmin_spi = header[7];
  filedatat -> av       = header[8];
  filedatat -> sig      = header[9];
	
  filedatat -> iangle   = header[13];
  filedatat -> phi      = header[14];
  filedatat -> theta    = header[15];
  filedatat -> gamma    = header[16];
	
  if (filedatat->imami != 1)  
        { norm(filedatat); }
	
  fmin_spi = filedatat->fmin_spi;
  fmax_spi = filedatat->fmax_spi;
  if (sayit)
     {
     sprintf(outstr, "@ %d:  Range: %f.... %f", imgnum, fmin_spi, fmax_spi);
     spout(outstr);
     }

  return imgnum;
}
Exemple #12
0
 int pickbackave(char dfil[])

 { 
 int      ncount, k,maxback;
 float *  ptr;
 float    sum, avg, dlist[7];
 float *  dbuf     = NULL;
 FILE  *  fp       = NULL;
 char     strwin[] = " Overall-window-avg     #windows"; 
 int      maxregp1 = 6+1;     /* Size for array pointed to by dbuf */
 int      maxkeys  = 1001;    /* Size for array pointed to by dbuf */
 int      openit   = TRUE;

 /* Retrieve window averages from dfil */
 
 if (getdoc((FILE *) NULL, dfil, datexc, maxkeys, maxregp1, &dbuf, &maxback) > 0) 
    {   /* Problem retrieving doc file, assume it does not exist */
    if (dbuf) free(dbuf); dbuf = NULL;
    spouts("*** Unable to read doc file: "); spout(dfil); 
    return -1;
    }

 ptr    = dbuf;
 sum    = 0.0;
 ncount = 0;

 /* Sum window averages from dfil */
 for (k = 0; k < maxback; k++)
    {
    if (*ptr != 0.0) 
       { ncount++; sum += *(ptr+1); }
    ptr += maxregp1; 
    }

 if (dbuf) free(dbuf);  dbuf = NULL;

 if (ncount < 1)
   {
   spouts("*** No windows for averaging in doc file: "); spout(dfil);
   XBell(idispl,50);
   return 0;
   }

 /* Find average */
 avg      = sum / ncount;
 dlist[0] = 999; 
 dlist[1] = avg;
 dlist[2] = ncount; 
 dlist[3] = 0.0; 
 dlist[4] = 0.0; 
 dlist[5] = 0.0;
 dlist[6] = 0.0;
 
 /* Save average in doc file  */

 if (!(fp = savdnc(dfil, datexc, &fp, dlist, 7, &openit, TRUE, FALSE, strwin))) 
    { /* Unable to open the doc file!! */
    XBell(idispl,50); XBell(idispl,50); return 0;
    }

 fclose(fp); fp = NULL;
    
 return ncount;
 }
Exemple #13
0
 int filterskel(float * oldimage, int nsamt, int nrowt, 
             float * newimage, float * fmint, float * fmaxt)
 {
 int     ix, iy,   ipixel;
 int     icon3, iloc, diffpixel, iok;
 float * tmpimage;
 float * eroded;
 float * dilated;
 float * ploc;
 float   fmino,fmaxo;

 fmino = * fmint;
 fmaxo = * fmaxt;

 /* allocate tmpimage, eroded, dilated */
 if ((tmpimage = (float *) malloc(nsamt * nrowt *
                      sizeof(float))) == (float *) 0)
    {spout(" *** Unable to allocate tmpimage in filterskel.");
     return FALSE;}

 if ((eroded = (float *) malloc(nsamt * nrowt *
                      sizeof(float))) == (float *) 0)
    {spout(" *** Unable to allocate eroded in filterskel."); 
     return FALSE;}

 if ((dilated = (float *) malloc(nsamt * nrowt *
                      sizeof(float))) == (float *) 0)
    {spout(" *** Unable to allocate dilated in filterskel."); 
     return FALSE;}

 /* copy oldimage to tmpimage */
 memcpy((void *) tmpimage,(void *) oldimage, nsamt*nrowt*4);
 
 /* set newimage to zero */
 for (ploc = newimage; ploc < newimage + (nsamt * nrowt); ploc++)
     {
     *ploc = 0.0;
     }

 /* convolve over all positions in the new image except border */
 diffpixel = TRUE;

 while (diffpixel) 
    {  
    diffpixel = FALSE;

    iok = filtere(tmpimage, nsamt, nrowt, TRUE, 7, *fmaxt,
                  eroded , fmint, fmaxt);

    iok = filtere(eroded, nsamt, nrowt, FALSE, 0, *fmaxt,
                  dilated , fmint, fmaxt);

    for (iy = 1; iy < nrowt - 1; iy++)
       {
       icon3 = iy * nrowt;

       for (ix = 1; ix < nsamt - 1; ix++)
          {
          iloc           = icon3 + ix;
          ipixel         = tmpimage[iloc] - dilated[iloc];
          newimage[iloc] = (int) newimage[iloc] | ipixel;
          /*  if (ipixel > 0) diffpixel = TRUE;  */
          if (eroded[iloc] > 0.0) diffpixel = TRUE;
          tmpimage[iloc] = eroded[iloc];
          }
       }

    /***
     showbimage(newimage, FALSE, &cimage, FALSE, &imagel, 
                nsamt, nrowt, fmino, fmaxo);
    **********/
    } 

 *fmint = fmino;
 *fmaxt = fmaxo;

 if (tmpimage) free(tmpimage); tmpimage = NULL;
 if (eroded)   free(eroded);   eroded = NULL;
 if (dilated)  free(dilated);  dilated = NULL;

 return TRUE;
 }
Exemple #14
0
 int redvol(FILEDATA *filedata, 
            int want8,    char  ** cptr,
            int want32,   float ** fptr,
            int nsam1,    int nsam2,   
            int nrow1,    int nrow2, 
            int nslice1,  int nslice2, 
            int nmint,    int nmaxt, 
            float fmint,  float fmaxt, 
            float * scal, float * offset,
            int iskipt)
 {
 int         nsam, nrow, irec, j, m, npixlin, miskip, npixcol;
 int         islice, irow, isam, ival, newmin, noff, nsamst, nrowst;
 int         npixpimg;
 float       *buf,  *fptrt,  *fptrtt, *fptrttt;
 char        *cptrt, *cptrtt, *cptrttt;
 float       buft;
 int       ibug,ival1;

 nsam    = filedata -> nsam;
 nrow    = filedata -> nrow;

 /* Find nsams, nrows in displayed image (do not expand nslices)*/
 dispsize(nsam1,nsam2,nrow1,nrow2,nslice1,nslice2,iskipt,
         &nsams, &nrows, &nslices);

 /* Find buf for redlin */
 buf    = filedata -> buf;

 newmin = (fmint != filedata-> fmin_spi || 
           fmaxt != filedata->fmax_spi) ;

 if (want8)
    {
    /* Find normalization constants for 8 bit output data */
    scale32to8(fmint, fmaxt, iskipt, imagego, imageend, scal, offset);

    if (!(*cptr)) 
       {
        /* allocate cbuf space first */
        if ((*cptr = (char *) malloc(nsams * nrows * nslices *
                      sizeof(char))) ==  (char *) 0)
            { 
            spout(" *** Unable to allocate memory in redvol.");
            return FALSE;
            }
       }
    }

 if (want32 && !(*fptr)) 
    {
    /* Allocate float space first */
    nsamst = nsam2 - nsam1 + 1;
    nrowst = nrow2 - nrow1 + 1;
    if ((*fptr = (float *) malloc(nsamst * nrowst * nslices *
                      sizeof(float))) == (float *) 0)
        { 
        spout(" *** Unable to allocate memory in redvol.");
        return FALSE;
        }
    }

 /* Set temporary data pointers */
 fptrt = *fptr;  /* points to start of current slice */
 cptrt = *cptr;  

 if (want32 && nsam1 == 1 && nsam2 == nsam &&
               nrow1 == 1 && nrow2 == nrow)
    {  /* Provides much faster input than code below */
    npixpimg = nsam * nrow;

    /* Fill fptr vol with 32 bit data */
    for (islice = nslice1; islice <= nslice2; islice++)
       {  /* Reads whole image at a time! */
       if (!redlin(filedata,fptrt,npixpimg,islice)) return FALSE;

       if (want8)
          {/* Also fill cptr volume with 8 bit data for this image */
          scaleimg(fptrt, nsam, nrow, iskipt, newmin, 
                   *scal, *offset, cptrt);
          cptrt += npixpimg;
          }
       fptrt += npixpimg;
       }                
    return TRUE;
    }


 /* Fill cptr volume with 8 bit data and fptr vol with 32 bit data */

 if (iskipt > -2 && iskipt < 2)
    {    /************************************************* Full size */

    for (islice = nslice1; islice <= nslice2; islice++)
       {
       for (irow = nrow1; irow <= nrow2; irow++)
          {
          irec = (islice - 1) * nrow + irow;
          if (!redlin(filedata,buf,nsam,irec)) return FALSE;
        
          if (newmin)
             { /* Want to alter min & max  definition*/
             if (want8 && want32)
                {  
                for (isam =nsam1-1; isam < nsam2; isam++)
                   {
                   ival     = (int) (buf[isam] * *scal + *offset);
                   *cptrt++ = MYMIN(MYMAX(ival,imagego),imageend);
                   *fptrt++ = buf[isam];
                   }  /* for isam           */
                }     /* if want8 && want32 */

             else 
                {  /* Want8 (can not have want32 only and newmin) */
                for (isam =nsam1-1; isam < nsam2; isam++)
                   {
                   ival = (int) (buf[isam] * *scal + *offset);
                   *cptrt++ = MYMIN(MYMAX(ival,imagego),imageend);

//#ifdef WEB_TRUE
//                   if (ival < 0 || ival > 255)
//                      {
//                      ibug  = MYMIN(MYMAX(ival,imagego),imageend);
//                      ival1 = (int) (buf[isam] * *scal + *offset);
//                      printf(" ival1: %d  cval: %d ibug: %d\n",
//                             ival1,ival,ibug);
//                      }
//#endif

                   }  /* For isam      */
                }     /* Else  (want8) */
             }        /* If newmin     */

          else
             {  /* Not newmin */
             if (want8 && want32)
                {  
                for (isam =nsam1-1; isam < nsam2; isam++)
                   {
                   *cptrt++ = (int) (buf[isam] * *scal + *offset);
                   *fptrt++ = buf[isam];
                   }  /* For isam   */
                }     /* If want8 && want32 */

             else if (want8)
                {  
                for (isam =nsam1-1; isam < nsam2; isam++)
                   {
                   *cptrt++ = (int) (buf[isam] * *scal + *offset);
                   }  /* For isam   */
                }     /* if want8   */

             else
                {  /* want32 */
                for (isam =nsam1-1; isam < nsam2; isam++)
                   {
                   *fptrt++ = buf[isam];
                   }  /* For isam   */
                }     /* If want32  */
             }        /* Not newmin */
          }           /* For irow   */
       }              /* For islice */
    }                 /* If iskipt  */


 else if (iskipt > 1)
    {    /************************************** Reduced size image */
    for (islice = nslice1; islice <= nslice2; islice++)
        {
       for (irow = nrow1; irow <= nrow2; irow += iskipt)
          {
          irec = (islice - 1) * nrow + irow;
          if (!redlin(filedata,buf,nsam,irec)) return FALSE;
          
          if (newmin)
             { /* Want to alter min & max  definition*/
             if (want8 && want32)
                {  
                for (isam =nsam1-1; isam < nsam2; isam += iskipt)
                   {
                   ival = (int)(int) (buf[isam] * *scal + *offset);
                   ival = MYMIN(MYMAX(ival,imagego),imageend);
                   *cptrt++ = ival;
                   *fptrt++ = buf[isam];
                   }  /* For isam           */
                }     /* If want8 && want32 */

             else if (want8)
                {  
                for (isam =nsam1-1; isam < nsam2; isam += iskipt)
                   {
                   ival = (int) (buf[isam] * *scal + *offset);
                   ival = MYMIN(MYMAX(ival,imagego),imageend);
                   *cptrt++ = ival;
                   }  /* For isam   */
                }     /* If want8   */
             }        /* If newmin  */
          else
             {  /* Not newmin */
             if (want8 && want32)
                {  
                for (isam =nsam1-1; isam < nsam2; isam += iskipt)
                   {
                   *cptrt++ = (int) (buf[isam] * *scal + *offset);
                   *fptrt++ = buf[isam];
                   }  /* For isam   */
                }     /* If want8 && want32 */

             else if (want8)
                {  
                for (isam =nsam1-1; isam < nsam2; isam += iskipt)
                   {
                   *cptrt++ = (int) (buf[isam] * *scal + *offset);
                   }  /* For isam      */
                }     /* If want8      */

             else
                { /* want32 only */ 
                for (isam =nsam1-1; isam < nsam2; isam += iskipt)
                   {
                   *fptrt++ = buf[isam];
                   }  /* For isam      */
                }     /* Else (want32) */
             }        /* Newmin        */
          }           /* For irow      */
       }              /* For islice    */
    }                 /* If iskipt > 1 */
   

 else if (iskipt < -1)
    {    /************************************* Magnified size image */
    miskip  = -iskipt;
    npixlin = 0;

    for (islice = nslice1; islice <= nslice2; islice++)
       {
       for (irow = nrow1; irow <= nrow2; irow++)
          {
          irec = (islice - 1) * nrow + irow;
          if (!redlin(filedata,buf,nsam,irec)) return FALSE;
          cptrtt  = cptrt + npixlin;
          fptrtt  = fptrt + npixlin;

          /* Npixlin & npixcol used to set corner of active image area*/
          npixlin += miskip * nsams;
          npixcol = 0; 

          if (newmin)
             { /* Want to alter min & max  definition*/
             if (want8 && want32)
                { 
                for (isam =nsam1-1; isam < nsam2; isam++)
                   {
                   buft    = buf[isam];
                   ival    = (int) (buft * *scal + *offset);
                   ival    = MYMIN(MYMAX(ival,imagego),imageend);
                   cptrttt = cptrtt + npixcol - 1;
                   fptrttt = fptrtt + npixcol - 1;
                   npixcol += miskip;

                   for (j=0; j < miskip; j++)
                      {
                      noff = 0;
                      cptrttt++;
                      fptrttt++;
                      for (m=0; m < miskip; m++)
                         {
                         *(cptrttt + noff) = ival;
                         *(fptrttt + noff) = buft;
                         noff += nsams;
                         }  /* For m              */
                      }     /* For j              */
                   }        /* For isam           */
                }           /* If want8 && want32 */

             else if (want8)
                {  
                for (isam =nsam1-1; isam < nsam2; isam++)
                   {
                   ival    = (int) (buf[isam] * *scal + *offset);
                   ival    = MYMIN(MYMAX(ival,imagego),imageend);
                   cptrttt = cptrtt + npixcol - 1;
                   npixcol += miskip;

                   for (j=0; j < miskip; j++)
                      {
                      noff = 0;
                      cptrttt++;
                      for (m=0; m < miskip; m++)
                         {
                         *(cptrttt + noff) = ival;
                         noff += nsams;
                         }  /* for m      */
                      }     /* for j      */
                   }        /* for isam   */
                }           /* for want8  */
             }              /* if newmin  */

          else
             { /* Not newmin */
             if (want8 && want32)
                {  
                for (isam =nsam1-1; isam < nsam2; isam++)
                   {
                   buft    = buf[isam];
                   ival    = (int) (buft * *scal + *offset);
                   cptrttt = cptrtt + npixcol - 1;
                   fptrttt = fptrtt + npixcol - 1;
                   npixcol += miskip;

                   for (j=0; j < miskip; j++)
                      {
                      noff = 0;
                      cptrttt++;
                      fptrttt++;
                      for (m=0; m < miskip; m++)
                         {
                         *(cptrttt + noff) = ival;
                         *(fptrttt + noff) = buft;
                         noff += nsams;
                         }  /* For m              */
                      }     /* For j              */
                   }        /* For isam           */
                }           /* If want8 && want32 */

             else if (want8)
                {  
                for (isam =nsam1-1; isam < nsam2; isam++)
                   {
                   ival    = (int) (buf[isam] * *scal + *offset);
                   cptrttt = cptrtt + npixcol - 1;
                   npixcol += miskip;

                   for (j=0; j < miskip; j++)
                      {
                      noff = 0;
                      cptrttt++;
                      for (m=0; m < miskip; m++)
                         {
                         *(cptrttt + noff) = ival;
                         noff += nsams;
                         }  /* For m      */
                      }     /* For j      */
                   }        /* For isam   */
                }           /* For want8  */

             else
                {     /* Want32 only */
                for (isam =nsam1-1; isam < nsam2; isam++)
                   {
                   buft    = buf[isam];
                   fptrttt = fptrtt + npixcol -1;
                   npixcol += miskip;

                   for (j=0; j < miskip; j++)
                      {
                      noff = 0;
                      fptrttt++;
                      for (m=0; m < miskip; m++)
                         {
                         *(fptrttt + noff) = buft;
                         noff += nsams;
                         }  /* For m          */
                      }     /* For j          */
                   }        /* For isam       */
                }           /* Else (want32)  */ 
             }              /* Not newmin     */
          }                 /* For irow       */
       }                    /* For islice     */
    }                       /* If iskipt      */

 return TRUE;
 }
Exemple #15
0
 FILE * savdn1(char * docnam, char * datexct, FILE ** fpt,
               float * dlist, int nlist, int * openit, int append,
               int dateit)
 {
 FILE     * fp;
 time_t     now;
 char     * datentime, * docname;
 int        icount, ikey, i;
 size_t     slen;

 if (*openit) 
    {
    /* first call of this routine */

    /* add datexct to docnam if not already there */
    slen = strlen(docnam) + strlen(datexct) + (size_t) 2;

    if (!(docname = (char *) malloc(slen * sizeof(char))))
       {
       spout("*** Unable to allocate memory in savdn1!");
       return (FILE *) 0;
       }  

    strcpy(docname,docnam);
    if (strstr(docname,datexct) != 
               (docname + strlen(docname) - strlen(datexct)))
       {   /* add extension now */
       strcat(docname,".");  strcat(docname,datexct);
       }

    if (append )
       {fp = fopen(docname,"a+");}
    else
       { fp = fopen(docname,"w+"); }


    if (fp ==  (FILE *) NULL)
       {    /* file can not be opened */
       spouts("*** Unable to open: "); spout(docname);  
       free(docname); return fp;
       }

    if (dateit) 
       {  /* put date, etc in file now */
       /* find current date and time */
       now       = time(NULL);
       datentime = ctime(&now);
       /* should these pointers be freed later ? */

       fprintf(fp," ; %3.3s/%3.3s  %s   %s",
                datexct,datexct, docname, datentime);
       }

    *openit = FALSE;
    if (docname) free(docname);
    *fpt = fp;
    }
 else if (*fpt)
    {
    /* use file pointer sent from caller */
    fp = *fpt;
    }
 else
    {
    spout("*** Pgm error: NULL fp sent to savdn1 without openit");
    return (FILE *) 0;
    }


 ikey = dlist[0] + 0.5;
 if (nlist > 0 && ikey > 99999) 
    {
    spout("*** Document file keys must be < 100000");
    return fp;
    }

 if (nlist < 0)
    {
    spout("*** Continuation lines not supported by savdn1.c");
    return (FILE *) 0;
    }

 else if (nlist > 0 && nlist <= 7) 
    {
    icount = nlist - 1;

    /* ikey is the key number which identifies each line       */
    /* icount is the number of values (registers) in that line */

    if (ikey < 10000 && icount < 10)
       { fprintf(fp," %4.4d %1.1d",ikey, icount); }
    else
       { fprintf(fp,"%d %d",ikey, icount); }

    for (i = 1; i <= icount; i++)
       { fprintf(fp," %12.6g",dlist[i]);  }
    fprintf(fp,"\n");
    }

 fflush(fp);
 return fp;
 }