예제 #1
0
파일: bp2ncdxgc.c 프로젝트: Dumbear/ADIOS
int ncd_wdset(int ncid,char *path,char *name, void *val,  \
              enum ADIOS_TYPES type, int rank,              \
              struct adios_bp_dimension_struct *dims)
{
  int i,valid,retval;
  char fullname[100];
  char dimname[100];
  fullname[0]='\0';
  char *result=NULL; 
  int dimids[2];
  dimids[1]=0;

  strcpy(fullname,name);
  if(start[0]==0)
  {
     retval=nc_redef(ncid);
     sprintf(dimname,"%s_%d",name,0);
     retval=nc_inq_unlimdim(ncid,&dimids[1]);
     if(dimids[1]==-1)retval=nc_def_dim(ncid,"timesteps",NC_UNLIMITED,&dimids[0]);
     retval=nc_def_dim(ncid,dimname,dims[0].local_bound,&dimids[1]);
     if(type==adios_real)
        retval=nc_def_var(ncid,fullname,NC_FLOAT,2,dimids,&valid);
     if(type==adios_integer)
        retval=nc_def_var(ncid,fullname,NC_INT,2,dimids,&valid);
     if(type==adios_long)
        retval=nc_def_var(ncid,fullname,NC_LONG,2,dimids,&valid);
     if(type==adios_double)
     {
        retval=nc_def_var(ncid,fullname,NC_DOUBLE,2,dimids,&valid);
        ERR(retval);
     }
     //printf("\t RANK=%d, DIMS:%s[0]=%d dimids[1]=%d\n",rank,dimname,dims[0].local_bound,dimids[1]);
     retval=nc_enddef(ncid);
  }
  else
     retval=nc_inq_varid (ncid, fullname, &valid);
  
  start[1]=0;//dims[0].local_bound;
  count[0]=1;
  count[1]=dims[0].local_bound;

  if(type==adios_double)
     retval=nc_put_vara_double(ncid,valid,start,count,val);
  if(type==adios_real)
     retval=nc_put_vara_float(ncid,valid,start,count,val);
  if(type==adios_integer)
     retval=nc_put_vara_int(ncid,valid,start,count,val);
  if(type==adios_long)
     retval=nc_put_vara_long(ncid,valid,start,count,val);
//#if DEBUG
  //printf("create dataset:%s\n",fullname);
  //printf("start:%dx%d. count:%dx%d\n",start[0],start[1],count[0],count[1]);
  //printf("-------------------\n");
//#endif
  return;
}
예제 #2
0
파일: bp2ncdxgc.c 프로젝트: Dumbear/ADIOS
int ncd_wscalar(int ncid,char *path,char *name, void *val,  \
              enum ADIOS_TYPES type)
{
  int i,valid,retval;
  char fullname[100];
  char dimname[100];
  fullname[0]='\0';
  char *result=NULL; 
  int dimids[1];
  dimids[0]=0;

  strcpy(fullname,name);
  if(start[0]==0)
  {
     retval=nc_redef(ncid);
     sprintf(dimname,"%s_%d",name,0);
     retval=nc_inq_unlimdim(ncid,&dimids[0]);

     if(dimids[0]==-1)retval=nc_def_dim(ncid,"timesteps",NC_UNLIMITED,&dimids[0]);
    
     if(type==adios_real)
        retval=nc_def_var(ncid,fullname,NC_FLOAT,1,dimids,&valid);
     else if(type==adios_double)
        retval=nc_def_var(ncid,fullname,NC_DOUBLE,1,dimids,&valid);
     else if(type==adios_integer)
        retval=nc_def_var(ncid,fullname,NC_INT,1,dimids,&valid);
     else if(type==adios_long)
        retval=nc_def_var(ncid,fullname,NC_LONG,1,dimids,&valid);
     retval=nc_enddef(ncid);
  }
  else
     retval=nc_inq_varid (ncid, fullname, &valid);
  if(type==adios_real)
     retval=nc_put_vara_float(ncid,valid,start_scalar,count_scalar,val);
  else if(type==bp_double)
     retval=nc_put_vara_double(ncid,valid,start_scalar,count_scalar,val);
  else if(type==adios_integer)
     retval=nc_put_vara_int(ncid,valid,start_scalar,count_scalar,val);
  else if(type==adios_long)
     retval=nc_put_vara_long(ncid,valid,start_scalar,count_scalar,val);
  ERR(retval);
  return 0;
}
예제 #3
0
파일: bp2ncdxgc.c 프로젝트: Dumbear/ADIOS
int ncd_wdset1(int ncid,char *path,char *name, void *val,  \
              enum ADIOS_TYPES type, int rank,              \
              struct adios_bp_dimension_struct *dims)
{
  int i,valid,retval;
  char fullname[100];
  char dimname[100];
  fullname[0]='\0';
  char *result=NULL; 
  int dimids[1];

  strcpy(fullname,name);
  retval=nc_redef(ncid);
  sprintf(dimname,"%s_%d",name,0);
  retval=nc_def_dim(ncid,dimname,dims[0].local_bound,&dimids[1]);
  if(type==adios_real)
     retval=nc_def_var(ncid,fullname,NC_FLOAT,1,dimids,&valid);
  if(type==adios_integer)
        retval=nc_def_var(ncid,fullname,NC_INT,1,dimids,&valid);
  if(type==adios_long)
        retval=nc_def_var(ncid,fullname,NC_LONG,1,dimids,&valid);
  if(type==adios_double)
  {
        retval=nc_def_var(ncid,fullname,NC_DOUBLE,1,dimids,&valid);
        ERR(retval);
  }
  retval=nc_enddef(ncid);
  size_t a[1],b[1];
  a[0]=0;
  b[0]=dims[0].local_bound; 
  if(type==adios_double)
     retval=nc_put_vara_double(ncid,valid,a,b,val);
  if(type==adios_real)
     retval=nc_put_vara_float(ncid,valid,a,b,val);
  if(type==adios_integer)
     retval=nc_put_vara_int(ncid,valid,a,b,val);
  if(type==adios_long)
     retval=nc_put_vara_long(ncid,valid,a,b,val);
  return;
}
예제 #4
0
void nc_put_vara(TYPE type,int ncid,int varid,const size_t start[],const size_t count[],const void *dp){
    switch(type){
        case(BYTE):
            nc_put_vara_uchar(ncid,varid,start,count,(const unsigned char  *)dp);
            break;
        case(SHORT):
            nc_put_vara_short(ncid,varid,start,count,(const short *)dp);
            break;
        case(INT):
            nc_put_vara_int(ncid,varid,start,count,(const int *)dp);
            break;
        case(LONG):
            nc_put_vara_long(ncid,varid,start,count,(const long *)dp);
            break;
        case(FLOAT):
            nc_put_vara_float(ncid,varid,start,count,(const float *)dp);
            break;
        case(DOUBLE):
            nc_put_vara_double(ncid,varid,start,count,(const double *)dp);
            break;
        default:
            printf("unkown types in nc_put_vara()!\n");
    }
}
예제 #5
0
/*
 * Write a hypercube of numeric values into a netCDF variable of an open
 * netCDF file.
 */
static void
c_ncvpt (
    int			ncid,	/* netCDF ID */
    int			varid,	/* variable ID */
    const size_t*	start,	/* multidimensional index of hypercube corner */
    const size_t*	count,	/* multidimensional hypercube edge lengths */
    const void*		value,	/* block of data values to be written */
    int*		rcode	/* returned error code */
)
{
    int		status;
    nc_type	datatype;

    if ((status = nc_inq_vartype(ncid, varid, &datatype)) == 0)
    {
	switch (datatype)
	{
	case NC_CHAR:
	    status = NC_ECHAR;
	    break;
	case NC_BYTE:
#	    if NF_INT1_IS_C_SIGNED_CHAR
		status = nc_put_vara_schar(ncid, varid, start, count,
					   (const signed char*)value);
#	    elif NF_INT1_IS_C_SHORT
		status = nc_put_vara_short(ncid, varid, start, count,
					   (const short*)value);
#	    elif NF_INT1_IS_C_INT
		status = nc_put_vara_int(ncid, varid, start, count,
					   (const int*)value);
#	    elif NF_INT1_IS_C_LONG
		status = nc_put_vara_long(ncid, varid, start, count,
					   (const long*)value);
#	    endif
	    break;
	case NC_SHORT:
#	    if NF_INT2_IS_C_SHORT
		status = nc_put_vara_short(ncid, varid, start, count,
					   (const short*)value);
#	    elif NF_INT2_IS_C_INT
		status = nc_put_vara_int(ncid, varid, start, count,
					   (const int*)value);
#	    elif NF_INT2_IS_C_LONG
		status = nc_put_vara_long(ncid, varid, start, count,
					   (const long*)value);
#	    endif
	    break;
	case NC_INT:
#	    if NF_INT_IS_C_INT
		status = nc_put_vara_int(ncid, varid, start, count,
					   (const int*)value);
#	    elif NF_INT_IS_C_LONG
		status = nc_put_vara_long(ncid, varid, start, count,
					   (const long*)value);
#	    endif
	    break;
	case NC_FLOAT:
#	    if NF_REAL_IS_C_FLOAT
		status = nc_put_vara_float(ncid, varid, start, count,
					   (const float*)value);
#	    elif NF_REAL_IS_C_DOUBLE
		status = nc_put_vara_double(ncid, varid, start, count,
					   (const double*)value);
#	    endif
	    break;
	case NC_DOUBLE:
#	    if NF_DOUBLEPRECISION_IS_C_FLOAT
		status = nc_put_vara_float(ncid, varid, start, count,
					   (const float*)value);
#	    elif NF_DOUBLEPRECISION_IS_C_DOUBLE
		status = nc_put_vara_double(ncid, varid, start, count,
					   (const double*)value);
#	    endif
	    break;
	}
    }

    if (status == 0)
	*rcode = 0;
    else
    {
	nc_advise("NCVPT", status, "");
	*rcode = ncerr;
    }
}
예제 #6
0
int main(int argc,char *argv[]) {
  struct DataMap *ptr;
  struct DataMapScalar *sx,*sy;
  struct DataMapArray *ax,*ay;
  size_t index[256];
  size_t start[256];
  size_t count[256];

  int s;
  unsigned char vbflg=0;
  unsigned char help=0;
  unsigned char option=0;
  unsigned char zflg=0;

  FILE *fp=NULL;
  gzFile zfp=0;

  FILE *mapfp;
  int n,c,x;
  int ncid;
  int block=0;
 
  int varid;
  
  int strsze;
  char **strptr;
  char *tmpbuf=NULL;

  OptionAdd(&opt,"-help",'x',&help);
  OptionAdd(&opt,"-option",'x',&option);
  OptionAdd(&opt,"vb",'x',&vbflg);
  OptionAdd(&opt,"z",'x',&zflg);


  if (argc>1) {
    arg=OptionProcess(1,argc,argv,&opt,NULL); 
    if (help==1) {
      OptionPrintInfo(stdout,hlpstr);
      exit(0);
    }
    if (option==1) {
      OptionDump(stdout,&opt);
      exit(0);
    }

    if (zflg) {
      zfp=gzopen(argv[arg],"r");
      if (zfp==0) {
        fprintf(stderr,"File not found.\n");
        exit(-1);
      }
    } else {
      fp=fopen(argv[arg],"r");
      if (fp==NULL) {
        fprintf(stderr,"File not found.\n");
        exit(-1);
      }
    }  

  } else {
    OptionPrintInfo(stdout,errstr);
    exit(-1);
  }


  /* load the map */

  mapfp=fopen(argv[arg+1],"r");
  loadmap(mapfp);
  fclose(mapfp);

 

  s=nc_open(argv[arg+2],NC_WRITE,&ncid);
  if (s !=NC_NOERR) {
    fprintf(stderr,"Error opening CDF file.\n");
    exit(-1);
  }


   


  block=0;
  while (1) {

    if (zflg) ptr=DataMapReadZ(zfp);
    else ptr=DataMapFread(fp);

    if (ptr==NULL) break;

    for (c=0;c<ptr->snum;c++) {
      sx=ptr->scl[c];
      for (n=0;n<snum;n++) {
        sy=sptr[n];
        if (strcmp(sx->name,sy->name) !=0) continue;
        if (sx->type !=sy->type) continue;
        break;
      }
      if (n !=snum) { /* mapped variable */
        s=nc_inq_varid(ncid,cdfsname[n],&varid);
        if (s !=NC_NOERR) {
          fprintf(stderr,"Error accessing CDF file.\n");
          exit(-1);
        }
        index[0]=block;
        switch (sx->type) {
        case DATACHAR:
          s=nc_put_var1_text(ncid,varid,index,sx->data.cptr);
          break;
        case DATASHORT:
          s=nc_put_var1_short(ncid,varid,index,sx->data.sptr);
          break;
        case DATAINT:
          s=nc_put_var1_long(ncid,varid,index,(long *) sx->data.iptr);
          break;
        case DATAFLOAT:
          s=nc_put_var1_float(ncid,varid,index,sx->data.fptr);
          break;
        case DATADOUBLE:
          s=nc_put_var1_double(ncid,varid,index,sx->data.dptr);
          break;
        case DATASTRING:
          start[0]=block;
          start[1]=0;
          count[0]=1;
          count[1]=strlen(*((char **) sx->data.vptr))+1;
          s=nc_put_vara_text(ncid,varid,start,count,
                             *((char **) sx->data.vptr));
          break;
	}
        if (s !=NC_NOERR) {
          fprintf(stderr,"Error writing CDF file (%d).\n",s);
          exit(-1);
        }
       
      }
    }

    for (c=0;c<ptr->anum;c++) {
      ax=ptr->arr[c];
      for (n=0;n<anum;n++) {
        ay=aptr[n];
      
        if (strcmp(ax->name,ay->name) !=0) continue;
        if (ax->type !=ay->type) continue;
        if (ax->dim !=ay->dim) continue;
        break;
      }
      if (n !=anum) { /* mapped variable */
      
        s=nc_inq_varid(ncid,cdfaname[n],&varid);
        if (s !=NC_NOERR) {
          fprintf(stderr,"Error accessing CDF file.\n");
          exit(-1);
        }
        start[0]=block;
        count[0]=1;
        n=1;
        for (x=0;x<ax->dim;x++) {
          start[1+x]=0;
          count[1+x]=ax->rng[x];
          n=n*ax->rng[x];
	}

        if (ax->type==DATASTRING) {
          int ndims;
          int dimids[NC_MAX_VAR_DIMS];
          size_t dimlen;
          s=nc_inq_varndims(ncid,varid,&ndims);
          if (s !=NC_NOERR) {
            fprintf(stderr,"Error accessing CDF file.\n");
            exit(-1);
          }
          s=nc_inq_vardimid(ncid,varid,dimids);
          if (s !=NC_NOERR) {
            fprintf(stderr,"Error accessing CDF file.\n");
            exit(-1);
          }
          if (ndims-2!=ax->dim) {
            fprintf(stderr,"Error matching dimensions.\n");
            exit(-1);
	  }
          
          s=nc_inq_dimlen(ncid,dimids[ndims-1],&dimlen);
          if (s !=NC_NOERR) {
            fprintf(stderr,"Error accessing CDF file.\n");
            exit(-1);
          }
          strsze=dimlen;
          tmpbuf=malloc(n*strsze);
          if (tmpbuf==NULL) {
            fprintf(stderr,"Failed to allocate buffer.\n");
            exit(-1);
	  }
          memset(tmpbuf,0,n*strsze);
          start[1+ax->dim]=0;
          count[1+ax->dim]=strsze;
          strptr=(char **) ax->data.vptr;
          for (x=0;x<n;x++) strncpy(tmpbuf+x*strsze,strptr[x],strsze);
	}               

        switch (ax->type) { 
        case DATACHAR:
           s=nc_put_vara_text(ncid,varid,start,count,ax->data.cptr);
           break;
        case DATASHORT:
           s=nc_put_vara_short(ncid,varid,start,count,ax->data.sptr);
           break;
        case DATAINT:
	  s=nc_put_vara_long(ncid,varid,start,count,(long *) ax->data.iptr);
           break;
        case DATAFLOAT:
           s=nc_put_vara_float(ncid,varid,start,count,ax->data.fptr);
           break;
        case DATADOUBLE:
           s=nc_put_vara_double(ncid,varid,start,count,ax->data.dptr);
           break;
        case DATASTRING:
           s=nc_put_vara_text(ncid,varid,start,count,tmpbuf);
	   break;
	}
        if (tmpbuf !=NULL) {
	  free(tmpbuf);
          tmpbuf=NULL;
	}

        if (s !=NC_NOERR) {
          fprintf(stderr,"Error writing CDF file (%d).\n",s);
          exit(-1);
        }
 
      }
    }
  

    DataMapFree(ptr);
    block++;
  }
  nc_close(ncid);
  if (zflg) gzclose(zfp);
  else fclose(fp);
  return 0;
}