Exemplo n.º 1
0
int main(int argc,char *argv[]) {
  int status=0;
  struct DataMap *ptr=NULL;
  struct DataMapScalar *sx=NULL,*sy=NULL;
  struct DataMapArray *ax=NULL,*ay=NULL;
  struct DimMap *dx=NULL;

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


  FILE *fp=NULL;
  int c,i;
  gzFile zfp=0;
  int x=0,n=0;

  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);
  }

  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) {
	/* copy the scalar */
        if (sptr==NULL) sptr=malloc(sizeof(struct DataMapScalar *));
        else {
          struct DataMapScalar **tmp;
          tmp=realloc(sptr,(snum+1)*sizeof(struct DataMapScalar *));
          if (tmp==NULL) break;
          sptr=tmp;
	}
        sptr[snum]=DataMapMakeScalar(sx->name,0,sx->type,NULL);
        snum++;
      }
    }
    if (c !=ptr->snum) {      
      status=-1;
      break;
    }
    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) {
        /* check the ranges */
        for (i=0;i<ax->dim;i++)
          if (ax->rng[i]>rptr[roff[n]+i]) rptr[roff[n]+i]=ax->rng[i];
      }

      if (n==anum) {
	/* copy the array */
      
        if (roff==NULL) roff=malloc(sizeof(int32));
        else {
          int32 *tmp;
          tmp=realloc(roff,(anum+1)*sizeof(int32));
          if (tmp==NULL) break;
	  roff=tmp;
	}
        roff[anum]=rnum;
        if (rptr==NULL) rptr=malloc(sizeof(int32)*ax->dim);
        else {
          int32 *tmp;
          tmp=realloc(rptr,(rnum+ax->dim)*sizeof(int32));
          if (tmp==NULL) break;
	  rptr=tmp;
	}
        for (i=0;i<ax->dim;i++) rptr[rnum+i]=ax->rng[i];
        rnum+=ax->dim;
	
        if (aptr==NULL) aptr=malloc(sizeof(struct DataMapArray *));
        else {
          struct DataMapArray **tmp;
          tmp=realloc(aptr,(anum+1)*sizeof(struct DataMapArray *));
          if (tmp==NULL) break;
          aptr=tmp;
	}
        aptr[anum]=DataMapMakeArray(ax->name,0,ax->type,ax->dim,NULL,
				    NULL);
        anum++;
       

      }

    }
    if (c !=ptr->anum) {
       status=-1;
       break;
    }
    DataMapFree(ptr);
  }
  if (zflg) gzclose(zfp);
  else fclose(fp);

  if (status==-1) {
    fprintf(stderr,"Error processing file.\n");
    exit(-1);
  }

  for (n=0;n<anum;n++) { 
     ax=aptr[n];
     ax->rng=rptr+roff[n];
     for (c=0;c<ax->dim;c++) if (ax->rng[c]==0) break; /* bad data */
     if (c !=ax->dim) ax->dim=0;
  }


  cdfsname=malloc(sizeof(char *)*snum);
  if (cdfsname==NULL) {
    fprintf(stderr,"Could not allocate scalar name table.\n");
    exit(-1);
  }

  cdfaname=malloc(sizeof(char *)*anum);
  if (cdfaname==NULL) {
    fprintf(stderr,"Could not allocate array name table.\n");
    exit(-1);
  }



  for (n=0;n<snum;n++) { 
    sx=sptr[n];
    cdfsname[n]=malloc(strlen(sx->name)+1);
    for (c=0;sx->name[c] !=0;c++) 
       if (isalnum(sx->name[c])) cdfsname[n][c]=sx->name[c];
	   else cdfsname[n][c]='_';
    cdfsname[n][c]=0;
    x=0;
    for (c=0;c<n;c++) if (strncmp(cdfsname[c],cdfsname[n],
                          strlen(cdfsname[n]))==0) x++;; 
    if (x !=0) {
      char *tmp;
      char txt[4];
      x++;
      sprintf(txt,"%.3d",x);
      tmp=realloc(cdfsname[n],strlen(cdfsname[n])+4);
      if (tmp==NULL) break;
      cdfsname[n]=tmp;
      strcat(cdfsname[n],txt);
    }
  }

  if (n !=snum) {
    fprintf(stderr,"Error generating names.\n");
    exit(-1);
  }

  for (n=0;n<anum;n++) { 
    ax=aptr[n];
    cdfaname[n]=NULL;
    if (ax->dim==0) continue;
    cdfaname[n]=malloc(strlen(ax->name)+1);
    for (c=0;ax->name[c] !=0;c++) 
       if (isalnum(ax->name[c])) cdfaname[n][c]=ax->name[c];
	   else cdfaname[n][c]='_';
    cdfaname[n][c]=0;
    for (c=0;c<n;c++) if (strncmp(cdfaname[c],cdfaname[n],
                          strlen(cdfaname[n]))==0) x++;
    if (x !=0) {
      char *tmp;
      char txt[4];
      x++;
      sprintf(txt,"%.3d",x);
      tmp=realloc(cdfaname[n],strlen(cdfaname[n])+4);
      if (tmp==NULL) break;
      cdfaname[n]=tmp;
      strcat(cdfaname[n],txt);
    }

  }

  if (n !=anum) {
    fprintf(stderr,"Error generating names.\n");
    exit(-1);
  }


  /* build the dimension table */

  for (n=0;n<snum;n++) {
    sx=sptr[n];
    if (sx->type != DATASTRING) continue;
    if (dnum==0) dptr=malloc(sizeof(struct DimMap *));
    else {
      struct DimMap **tmp;
      tmp=realloc(dptr,(dnum+1)*sizeof(struct DimMap *));
      if (tmp==NULL) break;
      dptr=tmp;
    }
    dx=malloc(sizeof(struct DimMap));
    dptr[dnum]=dx;
    dx->flg=0;
    dx->num=n;
    dx->dim=0;    
    dx->value=STRINGSIZE;  
    dx->name=malloc(strlen(cdfsname[n])+1);
    strcpy(dx->name,cdfsname[n]);
    x=0;
    for (c=0;c<dnum;c++) if (strncmp(dptr[c]->name,dx->name,
                          strlen(dx->name))==0) x++;
    if (x !=0) {
      char *tmp;
      char txt[4];
      x++;
      sprintf(txt,"%.3d",x);
      tmp=realloc(dx->name,strlen(dx->name)+4);
      if (tmp==NULL) break;
      dx->name=tmp;
      strcat(dx->name,txt);
    }
    dnum++;
  }
  if (n !=snum) {
    fprintf(stderr,"Error building dimension table.\n");
    exit(-1);
  }


 for (n=0;n<anum;n++) {
    ax=aptr[n];
    if (ax->dim==0) continue;
    for (i=0;i<ax->dim;i++) {
      if (dnum==0) dptr=malloc(sizeof(struct DimMap *));
      else {
        struct DimMap **tmp;
        tmp=realloc(dptr,(dnum+1)*sizeof(struct DimMap *));
        if (tmp==NULL) break;
        dptr=tmp;
      }
      dx=malloc(sizeof(struct DimMap));
      dptr[dnum]=dx;
      dx->flg=1;
      dx->num=n;
      dx->dim=i;
      dx->value=ax->rng[i];    
      dx->name=malloc(strlen(cdfaname[n])+1);
      strcpy(dx->name,cdfaname[n]);
      x=0;
      for (c=0;c<dnum;c++) if (strncmp(dptr[c]->name,dx->name,
                          strlen(dx->name))==0) x++;
      if (x !=0) {
        char *tmp;
        char txt[4];
        x++;
        sprintf(txt,"%.3d",x);
        tmp=realloc(dx->name,strlen(dx->name)+4);
        if (tmp==NULL) break;
        dx->name=tmp;
        strcat(dx->name,txt);
      }
      dnum++;
    }
    if (ax->type==DATASTRING) {
      if (dnum==0) dptr=malloc(sizeof(struct DimMap *));
      else {
        struct DimMap **tmp;
        tmp=realloc(dptr,(dnum+1)*sizeof(struct DimMap *));
        if (tmp==NULL) break;
        dptr=tmp;
      }
      dx=malloc(sizeof(struct DimMap));
      dptr[dnum]=dx;
      dx->flg=1;
      dx->num=n;
      dx->dim=i;
      dx->value=STRINGSIZE;    
      dx->name=malloc(strlen(cdfaname[n])+1);
      strcpy(dx->name,cdfaname[n]);
      x=0;
      for (c=0;c<dnum;c++) if (strncmp(dptr[c]->name,dx->name,
                          strlen(dx->name))==0) x++;
      if (x !=0) {
        char *tmp;
        char txt[4];
        x++;
        sprintf(txt,"%.3d",x);
        tmp=realloc(dx->name,strlen(dx->name)+4);
        if (tmp==NULL) break;
        dx->name=tmp;
        strcat(dx->name,txt);
      }
      dnum++;
    }
  }

  if (n !=anum) {
    fprintf(stderr,"Error building dimension table.\n");
    exit(-1);
  }


  fp=fopen(argv[2],"w");
  fprintf(fp,"netcdf dmap {\n\n");
  fprintf(fp,"dimensions:\n");
  fprintf(fp,"\tblock=unlimited;\n");
  for (n=0;n<dnum;n++) {
    dx=dptr[n];
    fprintf(fp,"\t%s=%d;\n",dx->name,dx->value);
  }


  fprintf(fp,"variables:\n");
   for (n=0;n<snum;n++) {
     fprintf(fp,"\t");
     sx=sptr[n];
     switch(sx->type) {
     case DATACHAR:
        fprintf(fp,"char");
        break;
     case DATASHORT:
        fprintf(fp,"short");
        break;
     case DATAINT:
        fprintf(fp,"int");
        break;
     case DATAFLOAT:
        fprintf(fp,"float");
        break;
     case DATADOUBLE:
        fprintf(fp,"double");
        break;
     case DATASTRING:
        fprintf(fp,"char"); 
    }
    fprintf(fp," %s",cdfsname[n]);
    if (sx->type==DATASTRING) {
      for (x=0;x<dnum;x++) {
	dx=dptr[x];
        if (dx->flg !=0) continue;
        if (dx->num==n) break;
      }
      fprintf(fp,"(block,%s);\n",dx->name);
    } else fprintf(fp,"(block);\n");

    
  }


  for (n=0;n<anum;n++) {
     fprintf(fp,"\t");
     ax=aptr[n];
     if (ax->dim==0) continue;
     switch(ax->type) {
     case DATACHAR:
        fprintf(fp,"char");
        break;
     case DATASHORT:
        fprintf(fp,"short");
        break;
     case DATAINT:
        fprintf(fp,"int");
        break;
     case DATAFLOAT:
        fprintf(fp,"float");
        break;
     case DATADOUBLE:
        fprintf(fp,"double");
        break;
     case DATASTRING:
        fprintf(fp,"char"); 
    }
    fprintf(fp," %s",cdfaname[n]);
    fprintf(fp,"(block");
    for (x=0;x<dnum;x++) {
      dx=dptr[x];
      if (dx->flg !=1) continue;
      if (dx->num!=n) continue;
      fprintf(fp,",%s",dx->name);
    }
    fprintf(fp,");\n");
    
  }
  fprintf(fp,"}");
  fclose(fp);
  
  fp=fopen(argv[3],"w");
  for (n=0;n<snum;n++) {
   fprintf(fp,"\t");
     sx=sptr[n];
     switch(sx->type) {
     case DATACHAR:
        fprintf(fp,"char 0");
        break;
     case DATASHORT:
        fprintf(fp,"short 0");
        break;
     case DATAINT:
        fprintf(fp,"int 0");
        break;
     case DATAFLOAT:
        fprintf(fp,"float 0");
        break;
     case DATADOUBLE:
        fprintf(fp,"double 0");
        break;
     case DATASTRING:
        fprintf(fp,"string 0"); 
    }
    fprintf(fp," %c%s%c=%s;\n",'"',sx->name,'"',cdfsname[n]);
  }

  for (n=0;n<anum;n++) {
   fprintf(fp,"\t");
     ax=aptr[n];
     if (ax->dim==0) continue;
     switch(ax->type) {
     case DATACHAR:
        fprintf(fp,"char");
        break;
     case DATASHORT:
        fprintf(fp,"short");
        break;
     case DATAINT:
        fprintf(fp,"int");
        break;
     case DATAFLOAT:
        fprintf(fp,"float");
        break;
     case DATADOUBLE:
        fprintf(fp,"double");
        break;
     case DATASTRING:
        fprintf(fp,"string"); 
    }
    fprintf(fp," %d",ax->dim); 
    fprintf(fp," %c%s%c=%s;\n",'"',ax->name,'"',cdfaname[n]);
  }
  fclose(fp);
  return 0;
}
Exemplo n.º 2
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_int(ncid,varid,index,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_int(ncid,varid,start,count,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;
}
Exemplo n.º 3
0
int main(int argc,char *argv[]) {
  int status=0;
  struct DataMap *ptr;
  struct DataMapScalar *sx=NULL,*sy=NULL;
  struct DataMapArray *ax=NULL,*ay=NULL;

  char buf[1024];
  unsigned char vbflg=0;

  FILE *fp;
  int c,i;
  int x=0,n;
  int tab;

  char *cdfname=NULL;
  char *cdfdef={"dmapcdf"};
  int block=0;
  int rnum=0,znum=0;
  int nelm=0;
  unsigned char help=0;
  unsigned char option=0;


  OptionAdd(&opt,"-help",'x',&help);
  OptionAdd(&opt,"-option",'x',&option);

  OptionAdd(&opt,"vb",'x',&vbflg);

  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);
    }

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


  if (cdfname==NULL) cdfname=cdfdef;

  while ((ptr=DataMapFread(fp)) !=NULL) {


    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) {
	/* copy the scalar */
        if (sptr==NULL) sptr=malloc(sizeof(struct DataMapScalar *));
        else {
          struct DataMapScalar **tmp;
          tmp=realloc(sptr,(snum+1)*sizeof(struct DataMapScalar *));
          if (tmp==NULL) break;
          sptr=tmp;
	}
        sptr[snum]=DataMapMakeScalar(sx->name,sx->type,NULL);
        snum++;
      }
    }
    if (c !=ptr->snum) {      
      status=-1;
      break;
    }
    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) {
        /* check the ranges */
        for (i=0;i<ax->dim;i++)
          if (ax->rng[i]>rptr[roff[n]+i]) rptr[roff[n]+i]=ax->rng[i];
      }

      if (n==anum) {
	/* copy the array */
      
        if (roff==NULL) roff=malloc(sizeof(int32));
        else {
          int32 *tmp;
          tmp=realloc(roff,(anum+1)*sizeof(int32));
          if (tmp==NULL) break;
	  roff=tmp;
	}
        roff[anum]=rnum;
        if (rptr==NULL) rptr=malloc(sizeof(int32)*ax->dim);
        else {
          int32 *tmp;
          tmp=realloc(rptr,(rnum+ax->dim)*sizeof(int32));
          if (tmp==NULL) break;
	  rptr=tmp;
	}
        for (i=0;i<ax->dim;i++) rptr[rnum+i]=ax->rng[i];
        rnum+=ax->dim;
	
        if (aptr==NULL) aptr=malloc(sizeof(struct DataMapArray *));
        else {
          struct DataMapArray **tmp;
          tmp=realloc(aptr,(anum+1)*sizeof(struct DataMapArray *));
          if (tmp==NULL) break;
          aptr=tmp;
	}
        aptr[anum]=DataMapMakeArray(ax->name,ax->type,ax->dim,NULL,
				    NULL);
        anum++;
       

      }

    }
    if (c !=ptr->anum) {
       status=-1;
       break;
    }
    DataMapFree(ptr);
    block++;
  }
  fclose(fp);

  if (status==-1) {
    fprintf(stderr,"Error processing file.\n");
    exit(-1);
  }

  for (n=0;n<anum;n++) { 
     ax=aptr[n];
     ax->rng=rptr+roff[n];
     for (c=0;c<ax->dim;c++) if (ax->rng[c]==0) break; /* bad data */
     if (c !=ax->dim) ax->dim=0;
  }


  cdfsname=malloc(sizeof(char *)*snum);
  if (cdfsname==NULL) {
    fprintf(stderr,"Could not allocate scalar name table.\n");
    exit(-1);
  }

  cdfaname=malloc(sizeof(char *)*anum);
  if (cdfaname==NULL) {
    fprintf(stderr,"Could not allocate array name table.\n");
    exit(-1);
  }



  for (n=0;n<snum;n++) { 
    sx=sptr[n];
    cdfsname[n]=malloc(strlen(sx->name)+1);
    for (c=0;sx->name[c] !=0;c++) 
       if (isalnum(sx->name[c])) cdfsname[n][c]=sx->name[c];
	   else cdfsname[n][c]='_';
    cdfsname[n][c]=0;
    x=0;
    for (c=0;c<n;c++) if (strncmp(cdfsname[c],cdfsname[n],
                          strlen(cdfsname[n]))==0) x++;; 
    if (x !=0) {
      char *tmp;
      char txt[4];
      x++;
      sprintf(txt,"%.3d",x);
      tmp=realloc(cdfsname[n],strlen(cdfsname[n])+4);
      if (tmp==NULL) break;
      cdfsname[n]=tmp;
      strcat(cdfsname[n],txt);
    }
  }

  if (n !=snum) {
    fprintf(stderr,"Error generating names.\n");
    exit(-1);
  }

  for (n=0;n<anum;n++) { 
    ax=aptr[n];
    cdfaname[n]=NULL;
    if (ax->dim==0) continue;
    cdfaname[n]=malloc(strlen(ax->name)+1);
    for (c=0;ax->name[c] !=0;c++) 
       if (isalnum(ax->name[c])) cdfaname[n][c]=ax->name[c];
	   else cdfaname[n][c]='_';
    cdfaname[n][c]=0;
    for (c=0;c<n;c++) if (strncmp(cdfaname[c],cdfaname[n],
                          strlen(cdfaname[n]))==0) x++;
    if (x !=0) {
      char *tmp;
      char txt[4];
      x++;
      sprintf(txt,"%.3d",x);
      tmp=realloc(cdfaname[n],strlen(cdfaname[n])+4);
      if (tmp==NULL) break;
      cdfaname[n]=tmp;
      strcat(cdfaname[n],txt);
    }

  }

  if (n !=anum) {
    fprintf(stderr,"Error generating names.\n");
    exit(-1);
  }

  rnum=snum;
  znum=anum;

  fp=fopen(argv[2],"w");
  fprintf(fp,"#header\n");
  fprintf(fp,"                            CDF NAME: %s\n",cdfname);
  fprintf(fp,"                       DATA ENCODING: NETWORK\n");
  fprintf(fp,"                            MAJORITY: ROW\n");
  fprintf(fp,"                              FORMAT: SINGLE\n\n");
  fprintf(fp,
     "! Variables  G.Attributes  V.Attributes  Records  Dims  Sizes\n");
  fprintf(fp,
     "! ---------  ------------  ------------  -------  ----  -----\n");
  fprintf(fp,
     "  %d/%d           0             0         %d/z    0\n",
     rnum,znum,block);

  fprintf(fp,"\n#GLOBALattributes\n");
  fprintf(fp,"\n#VARIABLEattributes\n");
 
  fprintf(fp,"\n#variables\n\n");
  for (n=0;n<snum;n++) {
    sx=sptr[n];
    fprintf(fp,"! Variable          Data      Number    Record   Dimension\n");
    fprintf(fp,"! Name              Type     Elements  Variance  Variances\n");
    fprintf(fp,"! --------          ----     --------  --------  ---------\n");
    fprintf(fp,"\n");
    sprintf(buf,"  %c%s%c",'"',cdfsname[n],'"');
    fprintf(fp,buf);
    tab=18-strlen(buf);
    if (tab>0) for (c=0;c<tab;c++) fprintf(fp," ");
    else fprintf(fp," ");
    switch (sx->type) {
    case DATACHAR:
      fprintf(fp,"CDF_CHAR  ");
      break;
    case DATASHORT:
      fprintf(fp,"CDF_INT2  ");
      break;
    case DATAINT:
      fprintf(fp,"CDF_INT4  ");
      break;
    case DATAFLOAT:
      fprintf(fp,"CDF_FLOAT ");
      break;
    case DATADOUBLE:
      fprintf(fp,"CDF_DOUBLE");
      break;
    case DATASTRING:
      fprintf(fp,"CDF_CHAR  ");
      break;

    }
    nelm=1;
    if (sx->type==DATASTRING) nelm=STRINGSIZE;
    fprintf(fp,"    %d         T          ",nelm);
    fprintf(fp,"\n\n");
    fprintf(fp,"! Attribute         Data     Value\n");
    fprintf(fp,"! Name              Type\n");
    fprintf(fp,"! --------          ----     --------\n");
    fprintf(fp,"\n.\n\n");


  }  
  fprintf(fp,"\n#zVariables\n\n");


  for (n=0;n<anum;n++) {
    ax=aptr[n];

    fprintf(fp,"! Variable          Data      Number");
    fprintf(fp,"                 Record   Dimension\n");
    fprintf(fp,"! Name              Type     Elements  Dims");
    fprintf(fp,"  Sizes  Variance  Variances\n");
    fprintf(fp, "! --------          ----     --------  ----");
    fprintf(fp,"  -----  --------  ---------\n");
    fprintf(fp,"\n");
    sprintf(buf,"  %c%s%c",'"',cdfaname[n],'"');
    fprintf(fp,buf);
    tab=18-strlen(buf);
    if (tab>0) for (c=0;c<tab;c++) fprintf(fp," ");
    else fprintf(fp," ");
    switch (ax->type) {
    case DATACHAR:
      fprintf(fp,"CDF_CHAR  ");
      break;
    case DATASHORT:
      fprintf(fp,"CDF_INT2  ");
      break;
    case DATAINT:
      fprintf(fp,"CDF_INT4  ");
      break;
    case DATAFLOAT:
      fprintf(fp,"CDF_FLOAT ");
      break;
    case DATADOUBLE:
      fprintf(fp,"CDF_DOUBLE");
      break;
    case DATASTRING:
      fprintf(fp,"CDF_CHAR  ");
      break;

    }

    nelm=1;
    if (ax->type==DATASTRING) nelm=STRINGSIZE;
    fprintf(fp,"    %d         %d     ",nelm,ax->dim);
    for (c=0;c<ax->dim;c++) fprintf(fp,"%d ",ax->rng[c]);
    fprintf(fp,"    T     ");
    for (c=0;c<ax->dim;c++) fprintf(fp,"T ");
    fprintf(fp,"\n\n");
    fprintf(fp,"! Attribute         Data     Value\n");
    fprintf(fp,"! Name              Type\n");
    fprintf(fp,"! --------          ----     --------\n");
    fprintf(fp,"\n.\n\n");

  }

  fprintf(fp,"#end\n\n");

  fp=fopen(argv[3],"w");
  for (n=0;n<snum;n++) {
   fprintf(fp,"\t");
     sx=sptr[n];
     switch(sx->type) {
     case DATACHAR:
        fprintf(fp,"char 0");
        break;
     case DATASHORT:
        fprintf(fp,"short 0");
        break;
     case DATAINT:
        fprintf(fp,"int 0");
        break;
     case DATAFLOAT:
        fprintf(fp,"float 0");
        break;
     case DATADOUBLE:
        fprintf(fp,"double 0");
        break;
     case DATASTRING:
        fprintf(fp,"string 0"); 
    }
    fprintf(fp," %c%s%c=%s;\n",'"',sx->name,'"',cdfsname[n]);
  }

  for (n=0;n<anum;n++) {
   fprintf(fp,"\t");
     ax=aptr[n];
     if (ax->dim==0) continue;
     switch(ax->type) {
     case DATACHAR:
        fprintf(fp,"char");
        break;
     case DATASHORT:
        fprintf(fp,"short");
        break;
     case DATAINT:
        fprintf(fp,"int");
        break;
     case DATAFLOAT:
        fprintf(fp,"float");
        break;
     case DATADOUBLE:
        fprintf(fp,"double");
        break;
     case DATASTRING:
        fprintf(fp,"string"); 
    }
    fprintf(fp," %d",ax->dim); 
    fprintf(fp," %c%s%c=%s;\n",'"',ax->name,'"',cdfaname[n]);
  }
  fclose(fp);
  return 0;
}