/* Edited December 11, 2002.  Fix warning message taht argument is incompativle
 with protoype in line 144 which originally read
 	fwrite(names+k,1,strlen(names+k)+1,stdout);
 Change to
 	fwrite(&names[k][0],1,strlen(&names[k][0])+1,stdout);
 Fix error message due to undefined symbol SOL.  Call to quiet_nan fails.
 Perhaps on systems that do have this routine, the code can be put back
 in place.
 Fixes done by rcg from wjs.
 */

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
/*
#ifdef SOL
#include <sunmath.h>
#endif
*/
/* #define DEBUG 1 */
#if HP || IBM
#define jdbopen_ jdbopen
#define jdbreada_ jdbreada
#define jdbread_ jdbread
#define jdbclose_ jdbclose
#define jdbcomments_ jdbcomments
#define jdblevel_ jdblevel
#define jdbattributes_ jdbattributes
#endif

#define NVALS 250
static int nnames=0;
static int nrow=0;
static char names[NVALS][40];
static int namelen=40;
static int namesonly;

struct rowpntr {
  float *vals;
  struct rowpntr *next;
} *rows;
static int lpntr[11],maxlevel;
double *yp;
static double nan;
static int unit;
static int nanflag;

int getnames(s)
char *s;
{
  nnames= -NVALS;
  maxlevel=jdbopen_(&unit,s,names,&namelen,&nnames);
  return maxlevel;
}

int getdata()
{
static int i,lev,nr;
struct rowpntr *rp,*rpo;
float *v,*vo;

rows=(struct rowpntr *)malloc(sizeof(rows));
rows->vals=(float *)malloc(nnames*sizeof(float));
rp=rows;
v=rp->vals;

nr=0;
while((lev=jdbread_(&unit,v))>=0){
  nr++;
  rp->next=(struct rowpntr *)malloc(sizeof(rows));
  rp=rp->next;
  rp->vals=(float *)malloc(nnames*sizeof(float));
  vo=v;
  v=rp->vals;
  for(i=0;i<nnames;i++)v[i]=vo[i];
  if(lev<maxlevel && nanflag){
    nr++;
    rp->next=(struct rowpntr *)malloc(sizeof(rows));
    rp=rp->next;
    rp->vals=(float *)malloc(nnames*sizeof(float));
    for(i=0;i<nnames;i++)vo[i]=nan;
    vo=v;
    v=rp->vals;
    for(i=0;i<nnames;i++)v[i]=vo[i];
  }
}
jdbclose_(&unit);
return nr;
}

main(argc,argv)
int argc;
char *argv[];
{
static char s[1024],s1[1024];
static char *sp;
static int i,k;
struct rowpntr *rp;
double y;

namesonly=0;
nanflag=0;

/* removed/replaced December 11, 2002  rcg
#if SUN || SOL
nan=quiet_nan((long)0);
#else
nan= 0.0/0.0;
#endif
*/
nan= 0.0/0.0;

if(argc<2){
  fprintf(stderr,"Must have object name.\n");
  fwrite(&nnames,sizeof(int),1,stdout);
  fwrite(&nrow,sizeof(int),1,stdout);
  exit(1);
}

/* get options if present */
if(argc==3){
  if(strstr(argv[2],"NaN"))nanflag=1;
  if(strstr(argv[2],"nan"))nanflag=1;
  if(strstr(argv[2],"var"))namesonly=1;
}

/* get obj name */
strcpy(s,argv[1]);
#ifdef DEBUG
fprintf(stderr,"%s\n",s);
#endif

i=getnames(s);
if(i<0){
  fprintf(stderr,"Open failed on %s\n",s);
  fwrite(&nnames,sizeof(int),1,stdout);
  fwrite(&nrow,sizeof(int),1,stdout);
  exit(1);
}

if(!namesonly){
  nrow=getdata();
  if(nrow==0){
    fprintf(stderr,"No data in %s\n",s);
    fwrite(&nnames,sizeof(int),1,stdout);
    fwrite(&nrow,sizeof(int),1,stdout);
    exit(1);
  }
};

fwrite(&nnames,sizeof(int),1,stdout);
fwrite(&nrow,sizeof(int),1,stdout);
/*
printf("%d\n",nrow);
*/

for(k=0;k<nnames;k++){
  fwrite(&names[k][0],1,strlen(&names[k][0])+1,stdout);
  rp=rows;
  for(i=0;i<nrow;i++){
    y=(double)rp->vals[k];
/*
    if(y == -9999.99) printf("NaN\n"); else printf("%g\n",y);
*/
    if(y <= -9999) y=nan;
    fwrite(&y,1,sizeof(double),stdout);
    rp=rp->next;
  }
}

}
