Пример #1
0
int read_snapshot(
		   bodyptr *btab_ptr,	     /* gets particle array */
		   int *nobj_ptr,	     /* gets number of bodies */
		   stream instr              /* stream to read from */
		   )
{
  int nobj, cs, i;
  real *mbuf, *mp, *pbuf, *pp;
  bodyptr bp;

  for(;;) {                        /* loop until done or proper snapshot */
    if (!get_tag_ok(instr,SnapShotTag))
        return 0;
    get_set(instr, SnapShotTag);
    if (!get_tag_ok(instr,ParametersTag)) {
    	get_tes(instr,SnapShotTag);
        continue;
    }
    get_set(instr, ParametersTag);
    get_data(instr, NobjTag, IntType, &nobj, 0);
    if (nobj < 1)
	error("read_snapshot: %s = %d  is absurd", NobjTag, nobj);
    if (get_tag_ok(instr,TimeTag))
        get_data(instr,TimeTag, RealType, &tsnap, 0);
    else {
        dprintf(0,"No time tag: time=0.0 assumed\n");
        tsnap = 0.0;
    }
    get_tes(instr, ParametersTag);
    if (!get_tag_ok(instr,ParticlesTag)) {
    	get_tes(instr,SnapShotTag);
        continue;
    }
    get_set(instr, ParticlesTag);
    get_data(instr, CoordSystemTag, IntType, &cs, 0);
    if (cs != CSCode(Cartesian, NDIM, 2))
	error("read_snapshot: cannot handle %s = %d", CoordSystemTag, cs);
    mbuf = mp = (real *) allocate(nobj * sizeof(real));
    pbuf = pp = (real *) allocate(nobj * 2 * NDIM * sizeof(real));
    get_data(instr, MassTag, RealType, mbuf, nobj, 0);
    get_data(instr, PhaseSpaceTag, RealType, pbuf, nobj, 2, NDIM, 0);
    get_tes(instr, ParticlesTag);
    get_tes(instr, SnapShotTag);
    *btab_ptr = bp = (bodyptr) allocate(nobj * sizeof(body));
    for (i = 0; i < nobj; i++) {
	Type(bp) = BODY;
	Mass(bp) = *mp++;
	SETV(Pos(bp), pp);
	pp += NDIM;
	SETV(Vel(bp), pp);
	pp += NDIM;
	bp++;
    }
    free(mbuf);
    free(pbuf);
    *nobj_ptr = nobj;
    return 1;
  }
}
Пример #2
0
void print_item(string tag)
{
    string type, *tags, *tp;
    int *dims;

    type = get_type(instr, tag);
    if (streq(type, SetType)) {
	get_set(instr, tag);
	print_set(tag);
	tags = list_tags(instr);
	for (tp = tags; *tp != NULL; tp++)
	    print_item(*tp);
	get_tes(instr, tag);
	print_tes(tag);
	for (tp = tags; *tp != NULL; tp++)
	    free(*tp);
	free((char *)tags);
    } else {
	dims = get_dims(instr, tag);
	print_header(tag, type, dims);
	(void) outstr(" ");
	print_data(tag, type, dims);
	end_line();
	if (dims != NULL)
	    free((char *)dims);
    }
    free(type);
}
Пример #3
0
void print_item(string tag)
{
    string type, stag;
    int *dims;

    type = get_type(instr, tag);
    if (streq(type, SetType)) {
	get_set(instr, tag);
	print_set(tag);
	while ((stag = next_item_tag(instr)) != NULL) {
	    print_item(stag);
	    free(stag);
	}
	get_tes(instr, tag);
	print_tes(tag);
    } else {
	dims = get_dimensions(instr, tag);
	print_head(tag, type, dims);
	print_data(tag, type, dims);
	endline();
	if (dims != NULL)
	    free(dims);
    }
    free(type);
}
Пример #4
0
Файл: xyio.c Проект: jobovy/nemo
void xyclose(int handle)
{
    xy_handle(handle,"xyclose");

    switch(images[handle].access) {
      case PUT:
        put_data_tes(images[handle].str,MapValuesTag);
        put_tes(images[handle].str,MapTag);
        put_tes(images[handle].str,ImageTag);
        break;
      case GET:
        get_data_tes(images[handle].str,MapValuesTag);
        get_tes(images[handle].str,MapTag);
        get_tes(images[handle].str,ImageTag);
        break;
      default:
        error("xyclose: Illegal access mode %d",images[handle].access);
    }
    strclose(images[handle].str);
    images[handle].str=NULL;
    
}
Пример #5
0
gsprof *get_gsprof(stream istr)
{
    string gsptag = "GeneralSphericalProfile";
    gsprof *gsp;

    if (get_tag_ok(istr, "FiniteSphericalProfile"))	/* old-style file?  */
	gsptag = "FiniteSphericalProfile";		/* use old tag      */
    gsp = (gsprof *) allocate(sizeof(gsprof));
    get_set(istr, gsptag);
    get_data(istr, "Npoint", IntType, &gsp->npoint, 0);
    gsp->radius = (real *) allocate(gsp->npoint * sizeof(real));
    gsp->density = (real *) allocate(gsp->npoint * sizeof(real));
    gsp->mass = (real *) allocate(gsp->npoint * sizeof(real));
    get_data(istr, "Radius", RealType, gsp->radius, gsp->npoint, 0);
    get_data(istr, "Density", RealType, gsp->density, gsp->npoint, 0);
    get_data(istr, "Mass", RealType, gsp->mass, gsp->npoint, 0);
    get_data(istr, "Alpha", RealType, &gsp->alpha, 0);
    get_data(istr, "Beta", RealType, &gsp->beta, 0);
    get_data(istr, "Mtot", RealType, &gsp->mtot, 0);
    get_tes(istr, gsptag);
    return (gsp);
}
Пример #6
0
void nemo_main()
{
    stream instr, outstr, out2str;
    string hl;
    real *fbuf, *mbuf, tsnap, mass0, mass1, mass2;
    bool Qswap = getbparam("swap");
    bool Qhead = getbparam("header");
    bool Qmass = FALSE;
    long nread;
    int nbody, i, nbad;
    int cs = CSCode(Cartesian, NDIM, 2);
    int ipar[5];
    float rpar[5], posvel[6];
    

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


    /* read snapshot */

    get_history(instr);
    hl = ask_headline();
    get_set(instr,SnapShotTag);
      get_set(instr,ParametersTag);
       get_data(instr, NobjTag, IntType, &nbody, 0);
       get_data_coerced(instr, TimeTag, RealType, &tsnap, 0);
      get_tes(instr,ParametersTag);

      fbuf = (real *) allocate(nbody*6*sizeof(real)); 
      mbuf = (real *) allocate(nbody*sizeof(real));   

      get_set(instr,ParticlesTag);
        get_data(instr,CoordSystemTag, IntType, &cs, 0);
	if (Qmass)
	  get_data_coerced(instr,MassTag,RealType,mbuf,nbody,0);
        get_data_coerced(instr,PhaseSpaceTag,RealType,fbuf,nbody,2,NDIM,0);
      get_tes(instr,ParticlesTag);
    get_tes(instr,SnapShotTag);


    ipar[0] = nbody;
    ipar[1] = 0;
    ipar[2] = 0;
    /* ipar[3,4] still unused */


    mass1 = mbuf[0];
    mass0 = mass1*nbody;
    rpar[0] = mass0;
    rpar[1] = tsnap;
    rpar[2] = 0;
    /* rpar[3,4] still unused */
    for (i=0, nbad=0, mass2=0.0; i<nbody; i++) {
      mass2 += mbuf[i];
      if (mbuf[i] != mass1) nbad++;
    }
    if (nbad) 
      warning("Found %d masses not equal to first one; total=%g, expected %g",
	      nbad, mass2, mass0);
			      

    if (Qhead) {
      nread = unfwrite(outstr, (char *)ipar, 5*sizeof(int));
      nread = unfwrite(outstr, (char *)rpar, 5*sizeof(float));
    }

    for (i=0; i<nbody; i++) {
      mbuf[i] = mass1;                              
      nread = unfwrite(outstr, (char *) &fbuf[i*6], 6*sizeof(real)); 
      if (nread<1) error("Early EOF for i=%d",i);
    }
    
    strclose(instr);
    strclose(outstr);

    if (hasvalue("out2")) {
      outstr = stropen(getparam("out2"),"w");
      put_history(outstr);
      strclose(outstr);
    }
}
Пример #7
0
bool scansnap(void)
{
    bool success;
    int i;
    real *pptr, *xptr;
    

    success = FALSE;
    while (! success) {
	get_history(instr);
	if (! get_tag_ok(instr, SnapShotTag))
	    return (FALSE);
	get_set(instr, SnapShotTag);
	get_set(instr, ParametersTag);
	if (get_tag_ok(instr, TimeTag)) {
	    if (timeptr == NULL)
		timeptr = (real *) allocate(sizeof(real));
	    get_data_coerced(instr, TimeTag, RealType, timeptr, 0);
	}
	if (get_tag_ok(instr, NobjTag))
	    get_data(instr, NobjTag, IntType, &nbody, 0);
	get_tes(instr, ParametersTag);
	if (get_tag_ok(instr, ParticlesTag) &&
	      (timeptr == NULL || streq(times, "all") ||
	         within(*timeptr, times, TIMEFUZZ))) {
	    get_set(instr, ParticlesTag);
	    if (get_tag_ok(instr, MassTag)) {
		if (massptr == NULL)
		    massptr = (real *) allocate(sizeof(real) * nbody);
		get_data_coerced(instr, MassTag, RealType, massptr,
				 nbody, 0);
	    }
	    if (get_tag_ok(instr, PhaseSpaceTag)) {
		if (phaseptr == NULL)
		    phaseptr = (real *) allocate(sizeof(real) * 2*NDIM * nbody);
		get_data_coerced(instr, PhaseSpaceTag, RealType, phaseptr,
				 nbody, 2, NDIM, 0);
		success = TRUE;
	    } else if (get_tag_ok(instr, PosTag)) {
	        real *ptmp = (real *) allocate(sizeof(real)*nbody*NDIM);
		if (phaseptr == NULL)
		    phaseptr = (real *) allocate(sizeof(real) * 2*NDIM * nbody);
		get_data_coerced(instr, PosTag, RealType, ptmp, nbody, NDIM, 0);

		for (i=0, pptr=phaseptr, xptr=ptmp; i<nbody; i++) {
		  *pptr++ = *xptr++;
		  *pptr++ = *xptr++;
		  if (NDIM==3) *pptr++ = *xptr++;
		  pptr += NDIM;
		}
		get_data_coerced(instr, VelTag, RealType, ptmp, nbody, NDIM, 0);
		for (i=0, pptr=phaseptr+NDIM, xptr=ptmp; i<nbody; i++) {
		  *pptr++ = *xptr++;
		  *pptr++ = *xptr++;
		  if (NDIM==3) *pptr++ = *xptr++;
		  pptr += NDIM;
		}
		free(ptmp);
		success = TRUE;
	    }
	    if (get_tag_ok(instr, PotentialTag)) {
		if (phiptr == NULL)
		    phiptr = (real *) allocate(sizeof(real) * nbody);
		get_data_coerced(instr, PotentialTag, RealType, phiptr,
				 nbody, 0);
	    }
	    if (get_tag_ok(instr, AccelerationTag)) {
		if (accptr == NULL)
		    accptr = (real *) allocate(sizeof(real) * NDIM * nbody);
		get_data_coerced(instr, AccelerationTag, RealType, accptr,
				 nbody, NDIM, 0);
	    }
	    if (get_tag_ok(instr, AuxTag)) {
		if (auxptr == NULL)
		    auxptr = (real *) allocate(sizeof(real) * nbody);
		get_data_coerced(instr, AuxTag, RealType, auxptr,
				 nbody, 0);
	    }
	    get_tes(instr, ParticlesTag);
	    xpnt = (float *) allocate(sizeof(float)*nbody);
	    ypnt = (float *) allocate(sizeof(float)*nbody);
	    zpnt = (float *) allocate(sizeof(float)*nbody);
	}
	get_tes(instr, SnapShotTag);
    }
    return TRUE;
}
Пример #8
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  */
}
Пример #9
0
Файл: xyio.c Проект: jobovy/nemo
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;
}