Exemple #1
0
int sub_image (imageptr iptr, regionptr rptr, imageptr *optr)
{
  int nx,ny,nz, nx1,ny1,nz1, ix,iy,iz, ix0,iy0,iz0;
  size_t np, np1;

  nx  = Nx(iptr);
  ny  = Ny(iptr);
  nz  = Nz(iptr);
  np = nx*ny*nz;
  /* grab the bounding box */
  ix0 = BLC(rptr)[0];
  iy0 = BLC(rptr)[1];
  iz0 = BLC(rptr)[2];
  nx1 = TRC(rptr)[0] - ix0;
  ny1 = TRC(rptr)[1] - iy0;
  nz1 = TRC(rptr)[2] - iz0;
  np1 = nx1*ny1*nz1;

  *optr = (imageptr ) allocate(sizeof(image));
  dprintf (DLEV,"copy_image:Allocated image @ %d size=%d * %d * %d",*optr,nx1,ny1,nz1);
    	
  Frame(*optr) = (real *) allocate(np1*sizeof(real));	
  dprintf (DLEV,"Frame allocated @ %d ",Frame(*optr));
  Nx(*optr) = nx1;
  Ny(*optr) = ny1;
  Nz(*optr) = nz1;
  Xmin(*optr) = Xmin(iptr) + ix0*Dx(iptr);
  Ymin(*optr) = Ymin(iptr) + iy0*Dy(iptr);
  Zmin(*optr) = Zmin(iptr) + iz0*Dz(iptr);
  Dx(*optr) = Dx(iptr);
  Dy(*optr) = Dy(iptr);
  Dz(*optr) = Dz(iptr);
  Namex(*optr) = mystrcpy(Namex(iptr));
  Namey(*optr) = mystrcpy(Namey(iptr));
  Namez(*optr) = mystrcpy(Namez(iptr));
  Xref(*optr) = Xref(iptr) + ix0;
  Yref(*optr) = Yref(iptr) + iy0;
  Zref(*optr) = Zref(iptr) + iz0;
  for (iz=0; iz<nz1; iz++)
    for (iy=0; iy<ny1; iy++)
      for (ix=0; ix<nx1; ix++)
	CubeValue(*optr,ix,iy,iz) = CubeValue(iptr,ix-ix0,iy-iy0,iz-iy0);
  Storage(*optr) = matdef[idef];
  Axis(*optr) = Axis(iptr);
  set_iarray(*optr);
  
  return 1;		/* succes return code  */
}
Exemple #2
0
void ini_matrix(imageptr *iptr, int nx, int ny)
{
  int ix,iy;
	
  printf ("iptr @ %d    (sizeof = %d)\n",*iptr,sizeof(image));
	
  if (*iptr==0) {
    *iptr = (imageptr ) allocate((sizeof(image)));
    printf ("allocated image 'iptr' @ %d\n",*iptr);
    Frame(*iptr) = (real *) allocate (nx*ny*sizeof(real));
    printf ("Allocated frame @ %d\n",Frame(*iptr));
  } else {
    printf ("Image already allocated @ %d\n",*iptr);
    printf ("with Frame @ %d\n",Frame(*iptr));
  }
  Nx(*iptr) = nx;
  Ny(*iptr) = ny;
  Nz(*iptr) = 1;
  Xmin(*iptr) = 1.0;
  Ymin(*iptr) = 2.0;
  Zmin(*iptr) = 3.0;
  Dx(*iptr) = 0.1;
  Dy(*iptr) = 0.1;
  Dz(*iptr) = 0.1;
  Namex(*iptr) = NULL;        /* no axis names */
  Namey(*iptr) = NULL;
  Namez(*iptr) = NULL;
  Unit(*iptr)  = NULL;        /* no units */
  Mask(*iptr)  = NULL;

  set_iarray(*iptr);
  for (ix=0; ix<nx; ix++)
    for (iy=0; iy<ny; iy++)
      MapValue(*iptr,ix,iy)  = (ix*ny+iy)*0.1;
}
Exemple #3
0
void nemo_main ()
{
    setparams();			/* set par's in globals */

    instr = stropen (infile, "r");
    read_image (instr,&iptr);
				/* set some global paramters */
    nx = Nx(iptr);	
    ny = Ny(iptr);
    nz = Nz(iptr);
    xmin = Xmin(iptr);
    ymin = Ymin(iptr);
    zmin = Zmin(iptr);
    dx = Dx(iptr);
    dy = Dy(iptr);
    dz = Dz(iptr);

    if(hasvalue("gauss"))
        make_gauss_beam(getparam("dir"));

    outstr = stropen (outfile,"w");
    if (hasvalue("wiener"))
      wiener();
    else
      smooth_it();
    write_image (outstr,iptr);

    strclose(instr);
    strclose(outstr);
}
Exemple #4
0
int copy_image (imageptr iptr, imageptr *optr)
{
  int nx,ny,nz;
  size_t np;
  nx = Nx(iptr);
  ny = Ny(iptr);
  nz = Nz(iptr);
  np = nx*ny*nz;
  

  *optr = (imageptr ) allocate(sizeof(image));
  dprintf (DLEV,"copy_image:Allocated image @ %d size=%d * %d * %d",*optr,nx,ny,nz);
    	
  Frame(*optr) = (real *) allocate(np*sizeof(real));	
  dprintf (DLEV,"Frame allocated @ %d ",Frame(*optr));
  Nx(*optr) = nx;
  Ny(*optr) = ny;
  Nz(*optr) = nz;
  Xmin(*optr) = Xmin(iptr);
  Ymin(*optr) = Ymin(iptr);
  Zmin(*optr) = Zmin(iptr);
  Dx(*optr) = Dx(iptr);
  Dy(*optr) = Dy(iptr);
  Dz(*optr) = Dz(iptr);
  Namex(*optr) = mystrcpy(Namex(iptr));
  Namey(*optr) = mystrcpy(Namey(iptr));
  Namez(*optr) = mystrcpy(Namez(iptr));
  Xref(*optr) = Xref(iptr);
  Yref(*optr) = Yref(iptr);
  Zref(*optr) = Zref(iptr);
  Storage(*optr) = matdef[idef];
  Axis(*optr) = Axis(iptr);
  set_iarray(*optr);
  
  return 1;		/* succes return code  */
}
Exemple #5
0
std::string LaczeDoGNUPlota::ZapiszUstawienieZakresu(char Os) const
{
  ostringstream strm;
  float Min, Max;

  switch (Os) {
   case 'x':  Min = Xmin();  Max = Xmax();  break;
   case 'y':  Min = Ymin();  Max = Ymax();  break;
   case 'z':  Min = Zmin();  Max = Zmax();  break;
   default: cerr << "!!! Blad w 'ZapiszUstawienieZakresu' niedozwolony"
                    " znak: " << Os << endl;
            return "";
  }
  strm << "set " << Os << "range [" <<  Min << ":" << Max << "]\n";
  return strm.str();
}
Exemple #6
0
int write_image (stream outstr, imageptr iptr)
{
  put_history(outstr);
  put_set (outstr,ImageTag);
    put_set (outstr,ParametersTag);
      put_data (outstr,NxTag,  IntType,  &(Nx(iptr)),   0);
      put_data (outstr,NyTag,  IntType,  &(Ny(iptr)),   0);
      put_data (outstr,NzTag,  IntType,  &(Nz(iptr)),   0);
      put_data (outstr,XminTag,RealType, &(Xmin(iptr)), 0);
      put_data (outstr,YminTag,RealType, &(Ymin(iptr)), 0);
      put_data (outstr,ZminTag,RealType, &(Zmin(iptr)), 0);
      put_data (outstr,DxTag,  RealType, &(Dx(iptr)),   0);
      put_data (outstr,DyTag,  RealType, &(Dy(iptr)),   0);
      put_data (outstr,DzTag,  RealType, &(Dz(iptr)),   0);
      put_data (outstr,XrefTag,RealType, &(Xref(iptr)), 0);
      put_data (outstr,YrefTag,RealType, &(Yref(iptr)), 0);
      put_data (outstr,ZrefTag,RealType, &(Zref(iptr)), 0);
      put_data (outstr,MapMinTag, RealType, &(MapMin(iptr)), 0);
      put_data (outstr,MapMaxTag, RealType, &(MapMax(iptr)), 0);
      put_data (outstr,BeamTypeTag, IntType, &(BeamType(iptr)), 0);
      put_data (outstr,BeamxTag, RealType, &(Beamx(iptr)), 0);
      put_data (outstr,BeamyTag, RealType, &(Beamy(iptr)), 0);
      put_data (outstr,BeamzTag, RealType, &(Beamz(iptr)), 0);
      if (Namex(iptr))
         put_string (outstr,NamexTag,Namex(iptr));
      if (Namey(iptr))
         put_string (outstr,NameyTag,Namey(iptr));
      if (Namez(iptr))
         put_string (outstr,NamezTag,Namez(iptr));
      if (Unit(iptr))
         put_string (outstr,UnitTag,Unit(iptr));
      put_data (outstr,TimeTag,  RealType, &(Time(iptr)), 0);
      put_string(outstr,StorageTag,matdef[idef]);
      put_data (outstr,AxisTag,  IntType, &(Axis(iptr)), 0);
    put_tes (outstr, ParametersTag);
         
    put_set (outstr,MapTag);
    if (Nz(iptr)==1)
      put_data (outstr,MapValuesTag,RealType,
		Frame(iptr),Nx(iptr),Ny(iptr),0);
    else
      put_data (outstr,MapValuesTag,RealType,
		Frame(iptr),Nx(iptr),Ny(iptr),Nz(iptr),0);
    put_tes (outstr, MapTag);
  put_tes (outstr, ImageTag);
  return 1;
}
Exemple #7
0
void fixheader(imageptr iptr, string options, real t0, real dt)
{
  char *ap = allocate(sizeof(options) + sizeof("Attribute") + 10);
  sprintf(ap,"Attribute: %s",options);

  Namex(iptr) = strdup("Particle");
  Xmin(iptr)  = 0.0;
  Dx(iptr)    = 1.0;

  Namey(iptr) = strdup(ap);
  Ymin(iptr)  = 0.0;
  Dy(iptr)    = 1.0;

  Namez(iptr) = strdup("Time");
  Zmin(iptr)  = t0;
  Dz(iptr)    = dt;

  free(ap);
}
Exemple #8
0
/*
 * CREATE_CUBE: create a blank cube Nx by Ny by Nz in size
 */
int create_cube (imageptr *iptr, int nx, int ny, int nz)
{
    *iptr = (imageptr ) allocate(sizeof(image));
    dprintf (DLEV,"CREATE_CUBE: Allocated image @ cube %d size=%d * %d * %d",
		*iptr,nx,ny,nz);
    if (*iptr == NULL)
	return 0;	/* no memory available */
    	
    Frame(*iptr) = (real *) allocate(nx*ny*nz*sizeof(real));	
    dprintf (DLEV,"Frame allocated @ %d ",Frame(*iptr));
    Nx(*iptr) = nx;             /* cube dimension */
    Ny(*iptr) = ny;
    Nz(*iptr) = nz;
    Xmin(*iptr) = 0.0;          /* start lower left corner at 0.0 */
    Ymin(*iptr) = 0.0;
    Zmin(*iptr) = 0.0;
    Xref(*iptr) = 0.0;
    Yref(*iptr) = 0.0;
    Zref(*iptr) = 0.0;
    Dx(*iptr) = 1.0;            /* unity pixels */
    Dy(*iptr) = 1.0;
    Dz(*iptr) = 1.0;
    MapMin(*iptr) = 0.0;
    MapMax(*iptr) = 0.0;
    BeamType(*iptr) = 0;
    Beamx(*iptr) = 0.0;         /* name beams */
    Beamy(*iptr) = 0.0;
    Beamz(*iptr) = 0.0;
    Namex(*iptr) = NULL;        /* no axis names yet */
    Namey(*iptr) = NULL;
    Namez(*iptr) = NULL;
    Unit(*iptr)  = NULL;        /* no units */
    Time(*iptr)  = 0.0;
    Storage(*iptr) = matdef[idef];
    Axis(*iptr) = 0;
    Mask(*iptr) = NULL;
    set_iarray(*iptr);
    
    return 1;		/* succes return code  */
}
Exemple #9
0
int create_image (imageptr *iptr, int nx, int ny)
{
    *iptr = (imageptr ) allocate(sizeof(image));
    dprintf (DLEV,"create_image:Allocated image @ %d size=%d * %d",*iptr,nx,ny);
    Frame(*iptr) = (real *) allocate(nx*ny*sizeof(real));	
    dprintf (DLEV,"Frame allocated @ %d ",Frame(*iptr));
    Axis(*iptr) = 0;            /* old style axis with no reference pixel */
    Nx(*iptr) = nx;             /* old style ONE map, no cube */
    Ny(*iptr) = ny;
    Nz(*iptr) = 1;
    Xmin(*iptr) = 0.0;          /* start lower left corner at 0.0 */
    Ymin(*iptr) = 0.0;
    Zmin(*iptr) = 0.0;
    Dx(*iptr) = 1.0;            /* unity pixels */
    Dy(*iptr) = 1.0;
    Dz(*iptr) = 1.0;
    Xref(*iptr) = 0.0;
    Yref(*iptr) = 0.0;
    Zref(*iptr) = 0.0;
    MapMin(*iptr) = 0.0;
    MapMax(*iptr) = 0.0;
    BeamType(*iptr) = 0;
    Beamx(*iptr) = 0.0;         /* name beams */
    Beamy(*iptr) = 0.0;
    Beamz(*iptr) = 0.0;
    Namex(*iptr) = NULL;        /* no axis names */
    Namey(*iptr) = NULL;
    Namez(*iptr) = NULL;
    Unit(*iptr)  = NULL;        /* no units */
    Time(*iptr)  = 0.0;
    Storage(*iptr) = matdef[idef];
    Axis(*iptr) = 0;
    Mask(*iptr) = NULL;
    set_iarray((*iptr));

    return 1;		/* succes return code  */
}
Exemple #10
0
void make_rawheader(FITS *fitsfile, imageptr iptr, bool Qrel)
{
    int ndim, n;
    double tmpr[3];
    
    ndim = (Nz(iptr) > 1) ? 3 : 2;

    n = nemoinpd(getparam("cdelt"),tmpr,3);
    if (n == 0) {
        Dx(iptr) = 2.0/Nx(iptr);
        Dy(iptr) = 2.0/Ny(iptr);
        Dz(iptr) = 2.0/Nz(iptr);
    } else if (n == 3) {
        Dx(iptr) = tmpr[0];
        Dy(iptr) = tmpr[1];
        Dz(iptr) = tmpr[2];
    } else
        error("cdelt= needs either 3 or 0 entries");

    Xmin(iptr) = 0.0;
    Ymin(iptr) = 0.0;
    Zmin(iptr) = 0.0;

}
Exemple #11
0
void nemo_main()
{
    stream  instr, outstr;
    int     nx, ny, nz;
    int     nstep,nstep1;
    int     i,j,k, n, n1, i1, j1, m;
    int     ix[2], iy[2];
    imageptr iptr=NULL, optr;      /* pointer to images */
    real    *vals, fraction;
    string  mode = getparam("mode");
    bool Qmedian = (*mode == 'm');
    bool Qmean = (*mode == 'a');

    nstep = getiparam("nstep");
    if (nstep%2 != 1) error("step size %d needs to be odd",nstep);
    nstep1 = (nstep-1)/2;

    n = getiparam("n");
    if (Qmedian)
      dprintf(1,"Median filter size %d\n",n);
    else if (Qmean) 
      dprintf(1,"Mean filter size %d\n",n);
    else
      dprintf(1,"Subtraction filter size %d\n",n);
    if (n%2 != 1) error("filter size %d needs to be odd",n);
    n1 = (n-1)/2;
    vals = (real *) allocate (sizeof(real) * (n*n + 1));

    instr = stropen(getparam("in"), "r");
    read_image( instr, &iptr);
    nx = Nx(iptr);	
    ny = Ny(iptr);
    nz = Nz(iptr);
    if (nz > 1) error("Cannot do 3D cubes properly; use 2D");

    if (hasvalue("x") && hasvalue("y")) {
      get_range("x",ix);
      get_range("y",iy);
    } else {
      ix[0] = 0;
      ix[1] = nx-1;
      iy[0] = 0;
      iy[1] = ny-1;
    }
    dprintf(1,"Xrange: %d - %d   Yrange: %d - %d\n",ix[0],ix[1],iy[0],iy[1]);
      
    outstr = stropen(getparam("out"), "w");
    create_cube(&optr,nx,ny,nz);
    Dx(optr) = Dx(iptr);
    Dy(optr) = Dy(iptr);
    Dz(optr) = Dz(iptr);
    Xmin(optr) = Xmin(iptr);
    Ymin(optr) = Ymin(iptr);
    Zmin(optr) = Zmin(iptr);

    if (nstep > 1) {
      warning("Cheat mode nstep=%d",nstep);

      for (j=nstep1; j<ny-nstep1; j+=nstep) {
	for (i=nstep1; i<nx-nstep1; i+=nstep) {
	  if (j<n1 || j >= ny-n1 || j < iy[0] || j > iy[1]) {
	    CVO(i,j) = CVI(i,j);
	    continue;
	  }
	  if (i<n1 || i >= nx-n1 || i < ix[0] || i > ix[1]) {
	    CVO(i,j) = CVI(i,j);
	    continue;
	  }
	  m = 0;
	  for (j1=j-n1; j1<=j+n1; j1++)
	    for (i1=i-n1; i1<=i+n1; i1++)
	      vals[m++] = CVI(i1,j1);
	  CVO(i,j) = median(m,vals,fraction);
	  for (j1=j-nstep1; j1<=j+nstep1; j1++)
	    for (i1=i-nstep1; i1<=i+nstep1; i1++)
	      CVO(i1,j1) = CVO(i,j);
	}
      }
    } else {

      for (j=0; j<ny; j++) {
	for (i=0; i<nx; i++) {
	  if (j<n1 || j >= ny-n1 || j < iy[0] || j > iy[1]) {
	    CVO(i,j) = CVI(i,j);
	    continue;
	  }
	  if (i<n1 || i >= nx-n1 || i < ix[0] || i > ix[1]) {
	    CVO(i,j) = CVI(i,j);
	    continue;
	  }
	  m = 0;
	  for (j1=j-n1; j1<=j+n1; j1++)
	    for (i1=i-n1; i1<=i+n1; i1++)
	      vals[m++] = CVI(i1,j1);

	  if (Qmedian)
	    CVO(i,j) = median(m,vals,fraction);
	  else if (Qmean)
	    CVO(i,j) = mean(m,vals,fraction);
	  else
	    CVO(i,j) = subtract(m,vals,fraction);
	}
      }

    }
    write_image(outstr, optr);
}
Exemple #12
0
void write_fits(string name,imageptr iptr)
{
    FLOAT tmpr,xmin[3],xref[3],dx[3],mapmin,mapmax;   /* fitsio FLOAT !!! */
    FITS *fitsfile;
    char *cp, origin[80];
    string *hitem, axname[3];
    float *buffer, *bp;
    int i, j, k, axistype, bitpix, keepaxis[3], nx[3], p[3], nx_out[3], ndim=3;
    double bscale, bzero;
    
    if (hasvalue("ndim")) ndim = getiparam("ndim");
    nx[0] = Nx(iptr);
    nx[1] = Ny(iptr);
    nx[2] = Nz(iptr);   if (nx[2] <= 0) nx[2] = 1;
    xmin[0] = Xmin(iptr)*scale[0];
    xmin[1] = Ymin(iptr)*scale[1];
    xmin[2] = Zmin(iptr)*scale[2];
    dx[0] = Dx(iptr)*scale[0];
    dx[1] = Dy(iptr)*scale[1];
    dx[2] = Dz(iptr)*scale[2];
    xref[0] = Xref(iptr)+1.0;
    xref[1] = Yref(iptr)+1.0;
    xref[2] = Zref(iptr)+1.0;
    axistype = Axis(iptr);
    axname[0] = (Namex(iptr) ? Namex(iptr) : xyz[0]);
    axname[1] = (Namey(iptr) ? Namey(iptr) : xyz[1]);
    axname[2] = (Namez(iptr) ? Namez(iptr) : xyz[2]);
    mapmin = MapMin(iptr);
    mapmax = MapMax(iptr);
    if (Qdummy) 
      for (i=0; i<3; i++) p[i] = i;
    else {
      if (Qrefmap) warning("dummy=f and usage of refmap will result in bad headers");
      permute(nx,p,3);
      dprintf(0,"Reordering axes: %d %d %d\n",p[0],p[1],p[2]);
    }
#if 1
    for (i=0; i<3; i++)  nx_out[i] = nx[p[i]];
    /* fix this so CubeValue works */
    Nx(iptr) = nx_out[0];
    Ny(iptr) = nx_out[1];
    Nz(iptr) = nx_out[2];
#else
    for (i=0; i<3; i++)  nx_out[i] = nx[i];
#endif
    sprintf(origin,"NEMO ccdfits %s",getparam("VERSION"));

    dprintf(1,"NEMO Image file written to FITS disk file\n");
    dprintf(1,"%d %d %d   %f %f %f   %f %f %f  %f %f %f   %f %f \n",
	    nx[0],nx[1],nx[2],xmin[0],xmin[1],xmin[2],dx[0],dx[1],dx[2],xref[0],xref[1],xref[2],
	    mapmin,mapmax);
    dprintf(1,"keepaxis(%d,%d,%d)\n",keepaxis[0],keepaxis[1],keepaxis[2]);
    
    fit_setblocksize(2880*getiparam("blocking"));
    bitpix = getiparam("bitpix");
    fit_setbitpix(bitpix);
    if (bitpix == 16) {      /* scale from -2^(bitpix-1) .. 2^(bitpix-1)-1 */
        bscale = (mapmax - mapmin) / (2.0*32768.0 - 1.0);
        bzero = mapmax - bscale*32767.0;
        fit_setscale(bscale,bzero);
    } else if (bitpix == 32) {
        bscale = (mapmax - mapmin) / (2.0*2147483648.0 - 1.0);
        bzero = mapmax - bscale*2147483647.0;
        fit_setscale(bscale,bzero);
    } else if (bitpix == 8) {
    	bscale = (mapmax - mapmin) / (2.0*128.0 - 1.0);
    	bzero = mapmin;
    	fit_setscale(bscale,bzero);
    }
    dprintf(1,"bscale,bzero=%g %g\n",bscale,bzero);

    fitsfile = fitopen(name,"new",ndim,nx_out);
    if (fitsfile==NULL) error("Could not open fitsfile %s for writing\n",name);

    if (Qrefmap || Qcrpix) {
      fitwrhdr(fitsfile,"CRPIX1",ref_crpix[0]);       
      fitwrhdr(fitsfile,"CRPIX2",ref_crpix[1]);       
      if (ndim>2) fitwrhdr(fitsfile,"CRPIX3",ref_crpix[2]);
    } else {
      if (axistype==1) {
	fitwrhdr(fitsfile,"CRPIX1",xref[0]);      
	fitwrhdr(fitsfile,"CRPIX2",xref[1]);
	if (ndim>2) fitwrhdr(fitsfile,"CRPIX3",xref[2]);
      } else {
	fitwrhdr(fitsfile,"CRPIX1",1.0);        /* CRPIX = 1 by Nemo definition */
	fitwrhdr(fitsfile,"CRPIX2",1.0);
	if (ndim>2) fitwrhdr(fitsfile,"CRPIX3",1.0);
      }
    }
    if (Qrefmap || Qcrval) {
      fitwrhdr(fitsfile,"CRVAL1",ref_crval[0]);
      fitwrhdr(fitsfile,"CRVAL2",ref_crval[1]);
      if (ndim>2) fitwrhdr(fitsfile,"CRVAL3",ref_crval[2]);
    } else {
      fitwrhdr(fitsfile,"CRVAL1",xmin[p[0]]);
      fitwrhdr(fitsfile,"CRVAL2",xmin[p[1]]);
      if (ndim>2) fitwrhdr(fitsfile,"CRVAL3",xmin[p[2]]);
    }

    if (Qcdmatrix) {
      fitwrhdr(fitsfile,"CD1_1",dx[p[0]]);    
      fitwrhdr(fitsfile,"CD2_2",dx[p[1]]);    
      if (ndim>2) fitwrhdr(fitsfile,"CD3_3",dx[p[2]]);    
    } else {
      if (Qrefmap || Qcdelt) {
	fitwrhdr(fitsfile,"CDELT1",ref_cdelt[0]*scale[0]);
	fitwrhdr(fitsfile,"CDELT2",ref_cdelt[1]*scale[1]);
	if (ndim>2) fitwrhdr(fitsfile,"CDELT3",ref_cdelt[2]*scale[2]);
      } else {
	fitwrhdr(fitsfile,"CDELT1",dx[p[0]]);    
	fitwrhdr(fitsfile,"CDELT2",dx[p[1]]);    
	if (ndim>2) fitwrhdr(fitsfile,"CDELT3",dx[p[2]]);
      }
    }

    if (Qradecvel) {
      dprintf(0,"[Axes names written as %s, %s, %s\n",
	      radeve[p[0]],radeve[p[1]],radeve[p[2]]);
      fitwrhda(fitsfile,"CTYPE1",radeve[p[0]]);
      fitwrhda(fitsfile,"CTYPE2",radeve[p[1]]);
      if (ndim>2) fitwrhda(fitsfile,"CTYPE3",radeve[p[2]]);
    } else {
      if (Qrefmap) {
        fitwrhda(fitsfile,"CTYPE1",ref_ctype[0]);
        fitwrhda(fitsfile,"CTYPE2",ref_ctype[1]);
        if (ndim>2) fitwrhda(fitsfile,"CTYPE3",ref_ctype[2]);
      } else {
	fitwrhda(fitsfile,"CTYPE1",axname[p[0]]);
	fitwrhda(fitsfile,"CTYPE2",axname[p[1]]);
	if (ndim>2) fitwrhda(fitsfile,"CTYPE3",axname[p[2]]);
      }
    }

    fitwrhdr(fitsfile,"DATAMIN",mapmin);
    fitwrhdr(fitsfile,"DATAMAX",mapmax);
    fitwrhda(fitsfile,"ORIGIN",origin);

    cp = getenv("USER");                                /* AUTHOR */
    if (cp)
        fitwrhda(fitsfile,"AUTHOR",cp);
    else
        fitwrhda(fitsfile,"AUTHOR","NEMO");

    if (object)                                        /* OBJECT */
        fitwrhda(fitsfile,"OBJECT",object);

    if (comment)                                       /* COMMENT */
        stuffit(fitsfile,"COMMENT",comment);
    if (headline)
        stuffit(fitsfile,"COMMENT",headline);

    hitem = ask_history();                              /* HISTORY */
    fitwra(fitsfile,"HISTORY","NEMO: History in reversed order");
    for (i=0, cp=hitem[0]; cp != NULL; i++) {
    	stuffit(fitsfile,"HISTORY",cp);
        cp = hitem[i+1];
    }

    for(i=0; i<nfill; i++)   /* debugging header I/O */
        fitwra(fitsfile,"COMMENT","Dummy filler space");

    buffer = (float *) allocate(nx[p[0]]*sizeof(float));

    for (k=0; k<nx_out[2]; k++) {          /* loop over all planes */
        fitsetpl(fitsfile,1,&k);
        for (j=0; j<nx_out[1]; j++) {      /* loop over all rows */
            for (i=0, bp=buffer; i<nx_out[0]; i++, bp++)
                *bp =  iscale[0] * CubeValue(iptr,i,j,k) + iscale[1];
            fitwrite(fitsfile,j,buffer);
        }
    }
    free(buffer);
    fitclose(fitsfile);
}
Exemple #13
0
void make_fitheader(FITS *fitsfile, imageptr iptr, bool Qrel, bool Qout, int axistype,
		    FLOAT *data_min, FLOAT *data_max)
{
    int nz, tmpi, i, j;
    real crpix1, crpix2, crpix3;
    FLOAT tmpr, cd[3][3];
    char cdname[10], ctype[32];

    nz = Nz(iptr);

    fitrdhdr(fitsfile,"CRVAL1",&tmpr,0.0); Xmin(iptr) = tmpr;
    fitrdhdr(fitsfile,"CRVAL2",&tmpr,0.0); Ymin(iptr) = tmpr;
    fitrdhdr(fitsfile,"CRVAL3",&tmpr,0.0); Zmin(iptr) = tmpr;
    if (Qrel) {
      Xmin(iptr) = Ymin(iptr) = Zmin(iptr) = 0.0;
    }

    fitrdhdr(fitsfile,"CDELT1",&tmpr,1.0); Dx(iptr)=tmpr;
    fitrdhdr(fitsfile,"CDELT2",&tmpr,1.0); Dy(iptr)=tmpr;
    fitrdhdr(fitsfile,"CDELT3",&tmpr,1.0); Dz(iptr)=tmpr;
    for (i=1; i<=3; i++)
      for (j=1; j<=3; j++) {
	sprintf(cdname,"CD%d_%d",i,j);
	fitrdhdr(fitsfile,cdname,&tmpr, (i==j ? 1.0 : 0.0));
	dprintf(1,"%s: %g\n",cdname,tmpr); 
	cd[i-1][j-1] = tmpr;
      }
    if (fitexhd(fitsfile,"CD1_1")) {
      if (fitexhd(fitsfile,"CDELT1"))
	  warning("CDELT1 as well as CD_1_1 have been specified - using CD");
      Dx(iptr) = cd[0][0];      
      Dy(iptr) = cd[1][1];
      Dz(iptr) = cd[2][2];
    }
	
    fitrdhdr(fitsfile,"CRPIX1",&tmpr,1.0); crpix1 = tmpr;
    fitrdhdr(fitsfile,"CRPIX2",&tmpr,1.0); crpix2 = tmpr;
    if (nz>1) {
      fitrdhdr(fitsfile,"CRPIX3",&tmpr,1.0); crpix3 = tmpr;
    } else
      crpix3 = 0.0;

    if (!Qout) {
      print_axis(1, Nx(iptr), crpix1, Xmin(iptr), Dx(iptr));
      print_axis(2, Ny(iptr), crpix2, Ymin(iptr), Dy(iptr));
      print_axis(3, Nz(iptr), crpix3, Zmin(iptr), Dz(iptr));
      return;
    }

    if (axistype==0) {
      Axis(iptr) = 0;
      if (crpix1 != 1.0)
        Xmin(iptr) -= (crpix1-1.0)*Dx(iptr);
      if (crpix2 != 1.0)
        Ymin(iptr) -= (crpix2-1.0)*Dy(iptr);
      if (nz>1 && crpix3 != 1.0)
        Zmin(iptr) -= (crpix3-1.0)*Dz(iptr);
    } else if (axistype==1) {
      Axis(iptr) = 1;
      Xref(iptr) = crpix1-1;
      Yref(iptr) = crpix2-1;
      Zref(iptr) = crpix3-1;
    } else
      error("Illegal axistype=%d",axistype);

    fitrdhda(fitsfile,"CTYPE1",ctype,"");
    Namex(iptr) = scopy(ctype);

    fitrdhda(fitsfile,"CTYPE2",ctype,"");
    Namey(iptr) = scopy(ctype);

    if (nz>1) {
           fitrdhda(fitsfile,"CTYPE3",ctype,"");
	   Namez(iptr) = scopy(ctype);
    }


    fitrdhdr(fitsfile,"DATAMIN",&tmpr,0.0); 
    MapMin(iptr) = *data_min = tmpr;
    dprintf(1,"DATAMIN: %g %g\n",*data_min,MapMin(iptr));
    fitrdhdr(fitsfile,"DATAMAX",&tmpr,0.0); 
    MapMax(iptr) = *data_max = tmpr;
    dprintf(1,"DATAMAX: %g %g\n",*data_max,MapMax(iptr));
    if (fitexhd(fitsfile,"BLANK")) {
        dprintf(1,"BLANK keyword exists\n");
        fitrdhdi(fitsfile,"BITPIX",&tmpi,-1);
        if (tmpi<0) {
            warning("FITS keyword BLANK not interpreted - BITPIX<0");
        } else {
            fitrdhdi(fitsfile,"BLANK",&tmpi,0);
            dprintf(1,"BLANK = %d\n",tmpi);
        }
    }
}
Exemple #14
0
/*
 * create a velocity field (GIPSY method)
 *              0..nx-1 and 0..ny-1 
 *      start from pixel, work back to gal plane and interpolate
 *      (a.k.a. retracing method)
 *
 */
local void cube_create(stream outstr)
{
  int  i, j, k, n, nx, ny;
  real m_min, m_max, sum;
  real den, vel, sig, velk, x;
  imageptr vptr;
  real f = 4.0;
  
  m_min = HUGE; m_max = -HUGE;
  nx = Nx(velptr);
  ny = Ny(velptr);
  
  if (!create_cube(&vptr, nx, ny, nz))   /* output data cube */
    error("Could not create cube from scratch");
  
  for (j=0; j<ny; j++)            /* Loop over all pixels */
    for (i=0; i<nx; i++) {
      for (k=0; k<nz; k++)
	CubeValue(vptr,i,j,k) = undef;       /* first set all to 'undefined' */
      sum = 0.0;
      vel = MapValue(velptr,i,j);

      if (denptr) {
	den = MapValue(denptr,i,j);
	if (den <= 0.0) continue;
      } else
	den = 1.0;

      if (sigptr) {
	sig = MapValue(sigptr,i,j);
	if (sig < 0.0) continue;
      } else
	sig = sigdef;
      if (sig == 0.0) {      /* special case, only populate 1 cell */
	k = (int) floor(    (vel-zrange[0])/zrange[2]   );
	if (k<0 || k>nz-1) continue;
	CubeValue(vptr,i,j,k) = den;
	sum = den;
      } else {               /* else use a gaussian profile */
	for (k=0, velk=zrange[0]+0.5*zrange[2]; k<nz; k++, velk += zrange[2]) {
	  x = (velk-vel)/sig;
	  x = ABS(x);
	  if (x > f) continue;
	  CubeValue(vptr,i,j,k) = den * exp(-0.5*x*x);
	  sum += CubeValue(vptr,i,j,k);
	}
      }
      sum = den/sum;              /* now normalize the spectrum so the sum is 'den' */
      for (k=0; k<nz; k++) {
	if (CubeValue(vptr,i,j,k) != undef)
	  CubeValue(vptr,i,j,k) *= sum;
      }
    }
      
  n=0;
  for (k=0; k<nz; k++)        /* get min and max in map */
    for (j=0; j<ny; j++)
      for (i=0; i<nx; i++) 
	if (CubeValue(vptr,i,j,k) != undef) {
	  m_min = MIN(CubeValue(vptr,i,j,k),m_min);
	  m_max = MAX(CubeValue(vptr,i,j,k),m_max);
	} else
	  n++;
  MapMin(vptr) = m_min;
  MapMax(vptr) = m_max;
  Dx(vptr) = Dx(velptr);
  Dy(vptr) = Dy(velptr);
  Dz(vptr) = zrange[2];
  Xmin(vptr) = Xmin(velptr);
  Ymin(vptr) = Ymin(velptr);
  Zmin(vptr) = zrange[0];
  
  if (n>0) warning("%d/%d cells with no signal",n,nx*ny*nz);
  printf("Min and max in map: %g %g\n",m_min,m_max);
  write_image (outstr,vptr);  
}
Exemple #15
0
void nemo_main()
{
    stream  instr, outstr;
    int     nx, ny, nz, nx1, ny1, nz1;
    int     axis, mom;
    int     i,j,k, apeak, cnt;
    imageptr iptr=NULL, iptr1=NULL, iptr2=NULL;      /* pointer to images */
    real    tmp0, tmp1, tmp2, tmp00, newvalue, peakvalue, scale, offset;
    bool    Qpeak;

    instr = stropen(getparam("in"), "r");
    mom = 0;
    axis = 3;
    Qpeak = getbparam("peak");

    read_image( instr, &iptr);

    nx1 = nx = Nx(iptr);	
    ny1 = ny = Ny(iptr);
    nz1 = 1;
    nz  = Nz(iptr);

    outstr = stropen(getparam("out"), "w");

    create_cube(&iptr1,nx1,ny1,nz1);
    create_cube(&iptr2,nx1,ny1,nz1);


    scale = Dz(iptr);
    offset = Zmin(iptr);
    for(j=0; j<ny; j++)
      for(i=0; i<nx; i++) {
	tmp0 = tmp00 = tmp1 = tmp2 = 0.0;
	cnt = 0;
	peakvalue = CubeValue(iptr,i,j,0);
	for(k=0; k<nz; k++) {
	  if (out_of_range(CubeValue(iptr,i,j,k))) continue;
	  cnt++;
	  tmp0 += CubeValue(iptr,i,j,k);
	  tmp00 += sqr(CubeValue(iptr,i,j,k));
	  if (CubeValue(iptr,i,j,k) > peakvalue) {
	    apeak = k;
	    peakvalue = CubeValue(iptr,i,j,k);
	  }
	}
	if (cnt==0 || tmp0==0.0) {
	  newvalue = 0.0;
	} else {
	  if (Qpeak) 
	    newvalue = peakvalue;
	  else
	    newvalue = tmp0;
	}
	CubeValue(iptr1,i,j,1) = newvalue;
      }
    
     
    Xmin(iptr1) = Xmin(iptr);
    Ymin(iptr1) = Ymin(iptr);
    Zmin(iptr1) = Zmin(iptr) + 0.5*(nz-1)*Dz(iptr);
    Dx(iptr1) = Dx(iptr);
    Dy(iptr1) = Dy(iptr);
    Dz(iptr1) = nz * Dz(iptr);
    
    Namex(iptr1) = Namex(iptr); /* care: we're passing a pointer */
    Namey(iptr1) = Namey(iptr);
    Namez(iptr1) = Namez(iptr);
    
    write_image(outstr, iptr1);
}
Exemple #16
0
void nemo_main()
{
    stream  instr, outstr;
    int     nx, ny, nz;        /* size of scratch map */
    int     nx1, ny1, nz1, nz2;
    int     ni, i, ix, iy, iz, iz1;
    real    dmin, dmax;
    imageptr iptr[MAXIM], optr;        /* pointer to image */
    string  flipmode;

    instr = stropen(getparam("in"), "r");
    outstr = stropen(getparam("out"), "w");

    for (i=0; i<MAXIM; i++) {               /* loop over all to gather data */
      iptr[i] = 0;
      if (read_image( instr, &iptr[i]) == 0) break;
      nx1 = Nx(iptr[i]);	
      ny1 = Ny(iptr[i]);
      nz1 = Nz(iptr[i]);
      dprintf(1,"Image %d: %d x %d x %d\n",i,nx1,ny1,nz1);
      if (i==0) {
	nx = nx1;
	ny = ny1;
	nz = nz1;
	dmin = MapMin(iptr[i]);
	dmax = MapMax(iptr[i]);
      } else {
	if (nx != nx1) error("size nx: %d != %d",nx,nx1);
	if (ny != ny1) error("size ny: %d != %d",ny,ny1);
	nz += nz1;
	dmin = MIN(dmin,MapMin(iptr[i]));
	dmax = MAX(dmax,MapMax(iptr[i]));
      }
    }
    ni = i;
    dprintf(0,"Final cube: %d x %d x %d\n",nx,ny,nz);
    dprintf(0,"Data min/max: %g %g\n",dmin,dmax);
    create_cube(&optr,nx,ny,nz);
    MapMin(optr) = dmin;
    MapMax(optr) = dmax;
    Xmin(optr) = Xmin(iptr[0]);
    Ymin(optr) = Ymin(iptr[0]);
    Zmin(optr) = Zmin(iptr[0]);
    Xref(optr) = Xref(iptr[0]);
    Yref(optr) = Yref(iptr[0]);
    Zref(optr) = Zref(iptr[0]);
    Dx(optr) = Dx(iptr[0]);
    Dy(optr) = Dy(iptr[0]);
    Dz(optr) = Dz(iptr[0]);

    for (i=0, iz=0; i<ni; i++) {       /* grab all data in output cube */
      nz1 = Nz(iptr[i]);
      for (iz1=0; iz1< nz1; iz1++, iz++) {
	for (iy=0; iy<ny; iy++) {
	  for (ix=0; ix<nx; ix++) {
	    CubeValue(optr,ix,iy,iz) = CubeValue(iptr[i],ix,iy,iz1);
	  }
	}
      }
    }

    write_image(outstr, optr);
}
Exemple #17
0
int read_image (stream instr, imageptr *iptr)
{
    string read_matdef;
    int nx=0, ny=0, nz=0;
    size_t  nxyz;

    get_history(instr);         /* accumulate history */

    if (!get_tag_ok (instr,ImageTag))
        return 0;			/* not an image available */
        
    if (*iptr==NULL) {		/* allocate image if neccessary */
    	*iptr = (imageptr ) allocate(sizeof(image));
	dprintf (DLEV,"Allocated image @ %d ",*iptr);
    } else {
        nx = Nx(*iptr);
        ny = Ny(*iptr);
        nz = Nz(*iptr);
    	dprintf (DLEV,"Image %dx%dx%d already allocated @ %d\n",
		 nx,ny,nz,*iptr);
    }
    	
    get_set (instr,ImageTag);
        get_set (instr,ParametersTag);
            get_data (instr,NxTag,IntType, &(Nx(*iptr)), 0);
            get_data (instr,NyTag,IntType, &(Ny(*iptr)), 0);
            get_data (instr,NzTag,IntType, &(Nz(*iptr)), 0);
	    if ((nx>0 || ny>0 || nz>0) &&
		(nx != Nx(*iptr) || ny != Ny(*iptr) || nz != Nz(*iptr)))
	      error("Cannot read different sized images in old pointer yet");
	    if (get_tag_ok(instr,AxisTag))
	      get_data (instr,AxisTag,IntType, &(Axis(*iptr)), 0);
	    else
	      Axis(*iptr) = 0;
	    if (Axis(*iptr) == 1) {
	      get_data_coerced (instr,XrefTag,RealType, &(Xref(*iptr)), 0);
	      get_data_coerced (instr,YrefTag,RealType, &(Yref(*iptr)), 0);
	      get_data_coerced (instr,ZrefTag,RealType, &(Zref(*iptr)), 0);
	    } else {
	      Xref(*iptr) = 0.0;
	      Yref(*iptr) = 0.0;
	      Zref(*iptr) = 0.0;
	    }

            get_data_coerced (instr,XminTag,RealType, &(Xmin(*iptr)), 0);
            get_data_coerced (instr,YminTag,RealType, &(Ymin(*iptr)), 0);
            get_data_coerced (instr,ZminTag,RealType, &(Zmin(*iptr)), 0);
            get_data_coerced (instr,DxTag,RealType, &(Dx(*iptr)), 0);
            get_data_coerced (instr,DyTag,RealType, &(Dy(*iptr)), 0);
            get_data_coerced (instr,DzTag,RealType, &(Dz(*iptr)), 0);
	    get_data_coerced (instr,MapMinTag, RealType, &(MapMin(*iptr)), 0);
	    get_data_coerced (instr,MapMaxTag, RealType, &(MapMax(*iptr)), 0);
	    get_data (instr,BeamTypeTag, IntType, &(BeamType(*iptr)), 0);
	    get_data_coerced (instr,BeamxTag, RealType, &(Beamx(*iptr)), 0);
	    get_data_coerced (instr,BeamyTag, RealType, &(Beamy(*iptr)), 0);
	    get_data_coerced (instr,BeamzTag, RealType, &(Beamz(*iptr)), 0);
            if (get_tag_ok(instr,NamexTag))             /* X-axis name */
                Namex(*iptr) = get_string(instr,NamexTag);
            else
                Namex(*iptr) = NULL;
            if (get_tag_ok(instr,NameyTag))             /* Y-axis name */
                Namey(*iptr) = get_string(instr,NameyTag);
            else
                Namey(*iptr) = NULL;
            if (get_tag_ok(instr,NamezTag))             /* Z-axis name */
                Namez(*iptr) = get_string(instr,NamezTag);
            else
                Namez(*iptr) = NULL;
            if (get_tag_ok(instr,UnitTag))             /* units  */
                Unit(*iptr) = get_string(instr,UnitTag);
            else
                Unit(*iptr) = NULL;
            if (get_tag_ok(instr,TimeTag))             /* time  */
   	    	get_data_coerced (instr,TimeTag, RealType, &(Time(*iptr)), 0);
   	    else
   	    	Time(*iptr) = 0.0;
            read_matdef = get_string(instr,StorageTag);
	    if (!streq(read_matdef,matdef[idef]))
                dprintf(0,"read_image: StorageTag = %s, compiled with %s\n",
		        read_matdef, matdef[idef]);
         get_tes (instr,ParametersTag);

         get_set (instr,MapTag);
            if (Frame(*iptr)==NULL) {        /* check if allocated */
	        nxyz = Nx(*iptr)*Ny(*iptr)*Nz(*iptr);
                Frame(*iptr) = (real *) allocate(nxyz * sizeof(real));
                dprintf (DLEV,"Frame allocated @ %d ",Frame(*iptr));
            } else
                dprintf (DLEV,"Frame already allocated @ %d\n",Frame(*iptr));
	    if (Nz(*iptr)==1)
                get_data_coerced (instr,MapValuesTag,RealType, Frame(*iptr), 
                                Nx(*iptr), Ny(*iptr), 0);
            else
                get_data_coerced (instr,MapValuesTag,RealType, Frame(*iptr),
                                Nx(*iptr), Ny(*iptr), Nz(*iptr), 0);
         get_tes (instr,MapTag);
      get_tes (instr,ImageTag);

      set_iarray(*iptr);

      dprintf (DLEV,"Frame size %d * %d \n",Nx(*iptr), Ny(*iptr));
      
      return 1;		/* succes return code  */
}
Exemple #18
0
void nemo_main()
{
  stream  instr, outstr;
  int     nx, ny, nz, mode;
  int     i,j,k;
  imageptr iptr1=NULL, iptr2=NULL, optr;      /* pointer to images */
  real    d1, d2, d3, d4, d5, d6, dx, dy, dz;
  bool    Qsym = TRUE;            /* symmetric derivates w.r.t. pixel point */
  
  match(getparam("mode"),valid_modes,&mode);
  if (mode==0) error("Not a valid mode; valid:%s",valid_modes);
  dprintf(0,"Image sharpening method #%d\n",mode);
  
  instr = stropen(getparam("in"), "r");
  read_image( instr, &iptr1);
  nx = Nx(iptr1);	
  ny = Ny(iptr1);
  nz = Nz(iptr1);
  dx = Dx(iptr1);
  dy = Dy(iptr1);
  dz = Dz(iptr1);
  if (mode & MODE_DIV || mode & MODE_VORT) {
    if (read_image(instr,&iptr2) == 0)
      error("No second image found in %s\n",getparam("in"));
    if (nx != Nx(iptr2))  
      error("Second image doesn't match in NX: %d <> %d\n",Nx(iptr2),nx);
    if (ny != Ny(iptr2))  
      error("Second image doesn't match in NY: %d <> %d\n",Ny(iptr2),ny);
    if (nz != Nz(iptr2))  
      error("Second image doesn't match in NZ: %d <> %d\n",Nz(iptr2),nz);
  }
  
  outstr = stropen(getparam("out"), "w");
  create_cube(&optr,nx,ny,nz);
  Dx(optr) = Dx(iptr1);
  Dy(optr) = Dy(iptr1);
  Dz(optr) = Dz(iptr1);
  Xmin(optr) = Xmin(iptr1);
  Ymin(optr) = Ymin(iptr1);
  Zmin(optr) = Zmin(iptr1);
  
  if (mode & MODE_LAPLACE) {
    for (k=1; k<nz-1; k++) {
      for (j=1; j<ny-1; j++) {
	for (i=1; i<nx-1; i++) {
	  d1 = CV1(i,j,k) - CV1(i-1,j,k);
	  d2 = CV1(i,j,k) - CV1(i+1,j,k);
	  d3 = CV1(i,j,k) - CV1(i,j-1,k);
	  d4 = CV1(i,j,k) - CV1(i,j+1,k);
	  d5 = CV1(i,j,k) - CV1(i,j,k-1);
	  d6 = CV1(i,j,k) - CV1(i,j,k+1);
	  CVO(i,j,k) = sqrt(d1*d1+d2*d2+d3*d3+d4*d4+d5*d5+d6*d6);
	}
	CVO(0,j,k)    = 0.0;
	CVO(nx-1,j,k) = 0.0;
      }
      for (i=0; i<nx; i++)
	CVO(i,0,k)    = CVO(i,ny-1,k) = 0.0;
    }
    for(j=0; j<ny; j++)
      for(i=0; i<nx; i++)
	CVO(i,j,0)    = CVO(i,j,nz-1) = 0.0;
  } else if (mode & MODE_DIV || mode & MODE_VORT) {
    warning("only 2D implemented");
    for (k=0; k<nz; k++) {
      for (j=0; j<ny-1; j++) {
	for (i=0; i<nx-1; i++) {
	  if (Qsym) {
	    if (i>0 && j>0) {
	      d1 = 0.5*(CV1(i+1,j,k) - CV1(i-1,j,k));         /* dv_x/dx */
	      d2 = 0.5*(CV1(i,j+1,k) - CV1(i,j-1,k));         /* dv_x/dy */
	      d3 = 0.5*(CV2(i+1,j,k) - CV2(i-1,j,k));         /* dv_y/dx */
	      d4 = 0.5*(CV2(i,j+1,k) - CV2(i,j-1,k));         /* dv_y/dy */
	    } else
	      d1 = d2 = d3 = d4 = 0.0;
	  } else {
	    d1 = CV1(i+1,j,k) - CV1(i,j,k);         /* dv_x/dx */
	    d2 = CV1(i,j+1,k) - CV1(i,j,k);         /* dv_x/dy */
	    d3 = CV2(i+1,j,k) - CV2(i,j,k);         /* dv_y/dx */
	    d4 = CV2(i,j+1,k) - CV2(i,j,k);         /* dv_y/dy */
	  }
	  if (mode&MODE_DIV)
	    CVO(i,j,k) = d1/dx + d4/dy;
	  else if (mode&MODE_VORT)
	    CVO(i,j,k) = d3/dx - d2/dy;
	}
	CVO(nx-1,j,k) = 0.0;
      }
      for (i=0; i<nx; i++) {
	CVO(i,ny-1,k) = 0.0;
      }
    }
  } else if (mode & MODE_AREGAN || mode & MODE_PREGAN) {
    warning("only 2D implemented");
    for (k=0; k<nz; k++) {
      for (j=0; j<ny-1; j++) {
	for (i=0; i<nx-1; i++) {
	  d1 = CV1(i,j,k)   - CV1(i+1,j,k);
	  d2 = CV1(i,j+1,k) - CV1(i+1,j+1,k);
	  d3 = CV1(i,j,k)   - CV1(i,j+1,k);
	  d4 = CV1(i+1,j,k) - CV1(i+1,j+1,k);
	  if (mode&MODE_AREGAN)
	    CVO(i,j,k) = sqrt(sqr(d1+d2)+sqr(d3+d4))/2;
	  else {
	    if (d3+d4==0.0 && d1+d2==0.0)
	      CVO(i,j,k) = 0.0;
	    else
	      CVO(i,j,k) = atan2(d3+d4,d1+d2) * 180 / PI;
	  }
	}
	CVO(nx-1,j,k) = 0.0;
      }
      for (i=0; i<nx; i++) {
	CVO(i,ny-1,k) = 0.0;
      }
    }
  }
  write_image(outstr, optr);
}
Exemple #19
0
image *xyopen(int *handle, string name, string status, int naxis, int *axes)
{
    int i, access;
    string read_matdef;
    image *iptr;
    stream str;

    if(naxis>MAXNAXIS) 
        error("naxis=%d not supported: MAXNAXIS=%d",naxis, MAXNAXIS);

    if (first) xy_init();

    str = stropen(name,status);                       /* open file */
    switch (*status) {
        case 'r':               /* "r", "old" */
        case 'o':
            access = GET;
            break;
        case 'w':               /* "w", "new" */
        case 'n':
            access = PUT;
            break;
        default:
            error("xyopen: Unsupported mode %s",status);
    }
    iptr = (imageptr )allocate(sizeof(image));  /* new image */

    if(access == GET){
      get_history(str);
      get_set (str,ImageTag);
        get_set (str,ParametersTag);
          get_data (str,NxTag,IntType, &(Nx(iptr)), 0);
          get_data (str,NyTag,IntType, &(Ny(iptr)), 0);
          get_data (str,NzTag,IntType, &(Nz(iptr)), 0);
          get_data_coerced (str,XminTag,RealType, &(Xmin(iptr)), 0);
          get_data_coerced (str,YminTag,RealType, &(Ymin(iptr)), 0);
          get_data_coerced (str,ZminTag,RealType, &(Zmin(iptr)), 0);
          get_data_coerced (str,DxTag,RealType, &(Dx(iptr)), 0);
          get_data_coerced (str,DyTag,RealType, &(Dy(iptr)), 0);
          get_data_coerced (str,DzTag,RealType, &(Dz(iptr)), 0);
	  get_data_coerced (str,MapMinTag, RealType, &(MapMin(iptr)), 0);
	  get_data_coerced (str,MapMaxTag, RealType, &(MapMax(iptr)), 0);
	  get_data (str,BeamTypeTag, IntType, &(BeamType(iptr)), 0);
	  get_data_coerced (str,BeamxTag, RealType, &(Beamx(iptr)), 0);
	  get_data_coerced (str,BeamyTag, RealType, &(Beamy(iptr)), 0);
	  get_data_coerced (str,BeamzTag, RealType, &(Beamz(iptr)), 0);
          if (get_tag_ok(str,NamexTag))             /* X-axis name */
            Namex(iptr) = get_string(str,NamexTag);
          else
            Namex(iptr) = NULL;
          if (get_tag_ok(str,NameyTag))             /* Y-axis name */
            Namey(iptr) = get_string(str,NameyTag);
          else
            Namey(iptr) = NULL;
          if (get_tag_ok(str,NamezTag))             /* Z-axis name */
            Namez(iptr) = get_string(str,NamezTag);
          else
            Namez(iptr) = NULL;
          if (get_tag_ok(str,UnitTag))             /* units  */
            Unit(iptr) = get_string(str,UnitTag);
          else
            Unit(iptr) = NULL;
          read_matdef = get_string(str,StorageTag);
	  if (!streq(read_matdef,matdef[idef]))
             dprintf(0,"read_image: StorageTag = %s, compiled with %s\n",
		        read_matdef, matdef[idef]);
        get_tes(str,ParametersTag);
        get_set(str,MapTag);
        if(Nz(iptr)<=1)
            get_data_set(str,MapValuesTag,RealType,Nx(iptr),Ny(iptr),0);
        else
            get_data_set(str,MapValuesTag,RealType,Nx(iptr),Ny(iptr),Nz(iptr),0);
        for (i=0; i<naxis; i++) axes[i] = 1;
        axes[0] = Nx(iptr); axes[1] = Ny(iptr); axes[2] = Nz(iptr);
    } else { /* PUT */
      Nx(iptr) = Ny(iptr) = Nz(iptr) = 1;
      if (naxis>0) Nx(iptr)  = axes[0];
      if (naxis>1) Ny(iptr)  = axes[1];
      if (naxis>2) Nz(iptr)  = axes[2];

      put_history(str);
      put_set (str,ImageTag);
        put_set (str,ParametersTag);
          put_data (str,NxTag,  IntType,  &(Nx(iptr)),   0);
          put_data (str,NyTag,  IntType,  &(Ny(iptr)),   0);
          put_data (str,NzTag,  IntType,  &(Nz(iptr)),   0);
          put_data (str,XminTag,RealType, &(Xmin(iptr)), 0);
          put_data (str,YminTag,RealType, &(Ymin(iptr)), 0);
          put_data (str,ZminTag,RealType, &(Zmin(iptr)), 0);
          put_data (str,DxTag,  RealType, &(Dx(iptr)),   0);
          put_data (str,DyTag,  RealType, &(Dy(iptr)),   0);
          put_data (str,DzTag,  RealType, &(Dz(iptr)),   0);
          put_data (str,MapMinTag, RealType, &(MapMin(iptr)), 0);
          put_data (str,MapMaxTag, RealType, &(MapMax(iptr)), 0);
          put_data (str,BeamTypeTag, IntType, &(BeamType(iptr)), 0);
          put_data (str,BeamxTag, RealType, &(Beamx(iptr)), 0);
          put_data (str,BeamyTag, RealType, &(Beamy(iptr)), 0);
          put_data (str,BeamzTag, RealType, &(Beamz(iptr)), 0);
          if (Namex(iptr))
            put_string (str,NamexTag,Namex(iptr));
          if (Namey(iptr))
            put_string (str,NameyTag,Namey(iptr));
          if (Namez(iptr))
            put_string (str,NamezTag,Namez(iptr));
      	  if (Unit(iptr))
            put_string (str,UnitTag,Unit(iptr));
          put_string(str,StorageTag,matdef[idef]);
        put_tes(str, ParametersTag);
        put_set(str, MapTag);
        if(Nz(iptr)<=1)
          put_data_set(str,MapValuesTag,RealType,Nx(iptr),Ny(iptr),0);
        else
          put_data_set(str,MapValuesTag,RealType,Nx(iptr),Ny(iptr),Nz(iptr),0);
    }

    *handle = -1;
    for(i=0; i<MAXOPEN; i++) {        /* look for a new table entry */
      if(images[i].str == NULL) *handle = i;
    }
    if(*handle < 0) 
        error("xyopen: No more free slots; too many open images");
    for (i=0; i<MAXNAXIS; i++)
        images[*handle].axes[i] = 1;

    images[*handle].str     = str;
    images[*handle].iptr    = iptr;
    images[*handle].offset  = 0;
    images[*handle].access  = access;
    images[*handle].naxis   = (Nz(iptr)<=1 ? 2 : 3);
    images[*handle].axes[0] = Nx(iptr);
    images[*handle].axes[1] = Ny(iptr);
    images[*handle].axes[2] = Nz(iptr);

    return iptr;
}
Exemple #20
0
nemo_main()
{
    int  i, j, k, ki;
    real x, y, z, xmin, xmax, mean, sigma, skew, kurt,  bad, w, *data;
    real dmin, dmax;
    real sum, sov, q1, q2, q3;
    Moment m;
    bool Qmin, Qmax, Qbad, Qw, Qmedian, Qrobust, Qtorben, Qmmcount = getbparam("mmcount");
    bool Qx, Qy, Qz, Qone, Qall, Qign = getbparam("ignore");
    bool Qhalf = getbparam("half");
    bool Qmaxpos = getbparam("maxpos");
    real nu, nppb = getdparam("nppb");
    int npar = getiparam("npar");
    int ngood;
    int ndat = 0;
    int nplanes;
    int min_count, max_count;
    int maxmom = getiparam("maxmom");
    int maxpos[2];
    char slabel[32];

    instr = stropen (getparam("in"), "r");
    read_image (instr,&iptr);
    strclose(instr);
    nx = Nx(iptr);	
    ny = Ny(iptr);
    nz = Nz(iptr);
    dprintf(1,"# data order debug:  %f %f\n",Frame(iptr)[0], Frame(iptr)[1]);
    if (hasvalue("tab")) tabstr = stropen(getparam("tab"),"w");

    planes = (int *) allocate((nz+1)*sizeof(int));
    nplanes = nemoinpi(getparam("planes"),planes,nz+1);
    Qall = (planes[0]==-1);
    if (planes[0]==0) {
      Qone = FALSE;
      nplanes = nz;
      for (k=0; k<nz; k++)
	planes[k] = k;
    } else if (!Qall) {
      Qone = (!Qall && nplanes==1);
      for (k=0; k<nplanes; k++) {
	if (planes[k]<1 || planes[k]>nz) error("%d is an illegal plane [1..%d]",planes[k],nz);
	planes[k]--;
      }
    }
 
    if (hasvalue("win")) {
      instr = stropen (getparam("win"), "r");
      read_image (instr,&wptr);
      strclose(instr);
      if (Nx(iptr) != Nx(wptr)) error("X sizes of in=/win= don't match");
      if (Ny(iptr) != Ny(wptr)) error("Y sizes of in=/win= don't match");
      if (Nz(iptr) != Nz(wptr)) error("Z sizes of in=/win= don't match");
      Qw = TRUE;
    } else
      Qw = FALSE;

    Qmin = hasvalue("min");
    if (Qmin) xmin = getdparam("min");
    Qmax = hasvalue("max");
    if (Qmax) xmax = getdparam("max");
    Qbad = hasvalue("bad");
    if (Qbad) bad = getdparam("bad");
    Qmedian = getbparam("median");
    Qrobust = getbparam("robust");
    Qtorben = getbparam("torben");
    if (Qtorben) Qmedian = TRUE;
    if (Qmedian || Qrobust || Qtorben) {
      ndat = nx*ny*nz;
      data = (real *) allocate(ndat*sizeof(real));
    }

    sov = 1.0;       /* volume of a pixel/voxel */
    Qx = Qign && Nx(iptr)==1;
    Qy = Qign && Ny(iptr)==1;
    Qz = Qign && Nz(iptr)==1;
    sov *= Qx ? 1.0 : Dx(iptr);
    sov *= Qy ? 1.0 : Dy(iptr);
    sov *= Qz ? 1.0 : Dz(iptr);
    strcpy(slabel,"*");
    if (!Qx) strcat(slabel,"Dx*");
    if (!Qy) strcat(slabel,"Dy*");
    if (!Qz) strcat(slabel,"Dz*");
    
    if (maxmom < 0) {
      warning("No work done, maxmom<0");
      stop(0);
    }

    if (Qall) {                 /* treat cube as one data block */

      ini_moment(&m,maxmom,ndat);
      ngood = 0;
      for (k=0; k<nz; k++) {
	for (j=0; j<ny; j++) {
	  for (i=0; i<nx; i++) {
            x =  CubeValue(iptr,i,j,k);
	    if (Qhalf && x>=0.0) continue;
            if (Qmin  && x<xmin) continue;
            if (Qmax  && x>xmax) continue;
            if (Qbad  && x==bad) continue;
	    w = Qw ? CubeValue(wptr,i,j,k) : 1.0;
            accum_moment(&m,x,w);
	    if (Qhalf && x<0) accum_moment(&m,-x,w);
	    if (Qmedian) data[ngood++] = x;
	    if (tabstr) fprintf(tabstr,"%g\n",x);
	  }
	}
      }
      
      if (npar > 0) {
	nu = n_moment(&m)/nppb - npar;
	if (nu < 1) error("%g: No degrees of freedom",nu);
	printf("chi2= %g\n", show_moment(&m,2)/nu/nppb);
	printf("df= %g\n", nu);
      } else {
	nsize = nx * ny * nz;
	sum = mean = sigma = skew = kurt = 0;
	if (maxmom > 0) {
	  mean = mean_moment(&m);
	  sum = show_moment(&m,1);
	}
	if (maxmom > 1)
	  sigma = sigma_moment(&m);
	if (maxmom > 2)
	  skew = skewness_moment(&m);
	if (maxmom > 3)
	  kurt = kurtosis_moment(&m);
	
	printf ("Number of points      : %d\n",n_moment(&m));
	printf ("Min and Max           : %f %f\n",min_moment(&m), max_moment(&m));
	printf ("Mean and dispersion   : %f %f\n",mean,sigma);
	printf ("Skewness and kurtosis : %f %f\n",skew,kurt);
	printf ("Sum and Sum*%s        : %f %f\n",slabel, sum, sum*sov);
	if (Qmedian) {
	  if (Qtorben) {
	    printf ("Median Torben         : %f (%d)\n",median_torben(ngood,data,min_moment(&m),max_moment(&m)),ngood);
	  } else {
	    printf ("Median                : %f\n",get_median(ngood,data));
	    q2 = median(ngood,data);
	    q1 = median_q1(ngood,data);
	    q3 = median_q3(ngood,data);
	    printf ("Q1,Q2,Q3              : %f %f %f\n",q1,q2,q3);
	  }
#if 1
	  if (ndat>0)
	    printf ("MedianL               : %f\n",median_moment(&m));
#endif
	}
	if (Qrobust) {
	  compute_robust_moment(&m);
	  printf ("Mean Robust           : %f\n",mean_robust_moment(&m));
	  printf ("Sigma Robust          : %f\n",sigma_robust_moment(&m));
	  printf ("Median Robust         : %f\n",median_robust_moment(&m));
	}

	if (Qmmcount) {
	  min_count = max_count = 0;
	  xmin = min_moment(&m);
	  xmax = max_moment(&m);
	  for (i=0; i<nx; i++) {
	    for (j=0; j<ny; j++) {
	      for (k=0; k<nz; k++) {
		x =  CubeValue(iptr,i,j,k);
		if (x==xmin) min_count++;
		if (x==xmax) max_count++;
	      }
	    }
	  } /* i */
	  printf("Min_Max_count         : %d %d\n",min_count,max_count);
	}
	printf ("%d/%d out-of-range points discarded\n",nsize-n_moment(&m), nsize);
      }

    } else {             /* treat each plane seperately */

      /* tabular output, one line per (selected) plane */

      printf("# iz z min  max  N  mean sigma skew kurt sum sumsov ");
      if (Qmedian) printf(" [med med]");
      if (Qrobust) printf(" robust[N mean sig med]");
      if (Qmaxpos) printf(" maxposx maxposy");
      printf("\n");

      ini_moment(&m,maxmom,ndat);
      for (ki=0; ki<nplanes; ki++) {
	reset_moment(&m);
	k = planes[ki];
	z = Zmin(iptr) + k*Dz(iptr);
	ngood = 0;
	for (j=0; j<ny; j++) {
	  for (i=0; i<nx; i++) {
            x =  CubeValue(iptr,i,j,k);
            if (Qmin && x<xmin) continue;
            if (Qmax && x>xmax) continue;
            if (Qbad && x==bad) continue;
	    if (Qmaxpos) {
	      if (i==0 && j==0) { dmax = x; maxpos[0] = 0; maxpos[1] = 0;}
	      else if (x>dmax) {  dmax = x; maxpos[0] = i; maxpos[1] = j;}
	    }
	    w = Qw ? CubeValue(wptr,i,j,k) : 1.0;
            accum_moment(&m,x,w);
	    if (Qmedian) data[ngood++] = x;
	  }
	}

	nsize = nx * ny * nz;
	sum = mean = sigma = skew = kurt = 0;
	if (maxmom > 0) {
	  mean = mean_moment(&m);
	  sum = show_moment(&m,1);
	}
	if (maxmom > 1)
	  sigma = sigma_moment(&m);
	if (maxmom > 2)
	  skew = skewness_moment(&m);
	if (maxmom > 3)
	  kurt = kurtosis_moment(&m);
	if (n_moment(&m) == 0) {
	  printf("# %d no data\n",k+1);
	  continue;
	}
	printf("%d %f  %f %f %d  %f %f %f %f  %f %f",
	       k+1, z, min_moment(&m), max_moment(&m), n_moment(&m),
	       mean,sigma,skew,kurt,sum,sum*sov);
	if (Qmedian) {
	  printf ("   %f",get_median(ngood,data));
	  if (ndat>0) printf (" %f",median_moment(&m));
	}
	if (Qrobust) {
	  compute_robust_moment(&m);
	  printf ("   %d %f %f %f",n_robust_moment(&m), mean_robust_moment(&m),
		  sigma_robust_moment(&m), median_robust_moment(&m));
	}
	if (Qmaxpos) {
	  printf("   %d %d",maxpos[0],maxpos[1]);
	}
#if 0	  
	if (Qmmcount) {
	  min_count = max_count = 0;
	  xmin = min_moment(&m);
	  xmax = max_moment(&m);
	  for (i=0; i<nx; i++) {
	    for (j=0; j<ny; j++) {
	      x =  CubeValue(iptr,i,j,k);
	      if (x==xmin) min_count++;
	      if (x==xmax) max_count++;
	    }
	  } /* i,j */
	  printf(" %d %d",min_count,max_count);
	}
	printf ("%d/%d out-of-range points discarded\n",nsize-n_moment(&m), nsize);
#endif
	printf("\n");
      } /* ki */
    }
}