/* *******************************************************************
 *                                                                   *
 * Copyright (c) L-DGO/MIT/JGOFS                                     *
 *                                                                   *
 *                                                                   *
 * File             : map.c                                          *
 *                                                                   *
 * Purpose          :                                                *         
 *                                                                   *
 * Version Number   : 1.1                                            *
 *                                                                   *
 * Revision History :                                                *
 *                                                                   *
 * Date                              Developer                       *
 * ----                              ---------                       *
 *                                                                   *
 * Sat Oct 17  1992 10.00           Glenn Flierl                     *
 *                                                                   *   
 *                                                                   *
 ******************************************************************* */

#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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
/* #define DEBUG */

#include OPTIONS
#include INNEROPTIONS

int nlevels;
int firstvar[10];
char names[NVAR][TOKEN];
int namesize=TOKEN;
char values[NVAR][TOKEN];
int valuesize=TOKEN;
int lev[NVAR],vpntr[NVAR];
int handle;
int minlevelread;

int ncrit;
char comments[COMMENTSIZE];
int plon,plat;
char lonsav[TOKEN],latsav[TOKEN];
int quadflg;

void docalc()
{
float x,y,alon,alat;
if(isalpha(values[plon][0]) || isalpha(values[plat][0]))return;
alon=atof(values[plon]);
alat=atof(values[plat]);
remap(alon,alat,&x,&y);
sprintf(values[plon],"%7.4f",x);
sprintf(values[plat],"%7.4f",y);
if(quadflg){
  strcpy(lonsav,values[plon]);
  strcpy(latsav,values[plat]);
  strcpy(values[plon],"nd");
  strcpy(values[plat],"nd");
};
}

void newquad()
{
quadflg=1;
}

void doinit(nparams,s)
char *s[];
int *nparams;
{
int j;
plon= -1; plat = -1;
for(j=0;j<firstvar[nlevels+1];j++){
  if(strcmp(names[j],"lon")==0)plon=j;
  if(strcmp(names[j],"long")==0)plon=j;
  if(strcmp(names[j],"lat")==0)plat=j;
};
if(plon == -1 || plat == -1) {
  printf("&x error bad variable names for map");
  exit(1);
};
ncrit=lev[plon];
j=lev[plat];
if(j>ncrit) ncrit=j;
getinfo();
quadflg=0;
}


int ioreadrec_(level)
int *level;
{
char tmp[80];
int i,clevel;

/*
printf("** level %d  minlevelread %d \n",*level,minlevelread);
*/

if(quadflg){
  minlevelread=ncrit;
  strcpy(values[plon],lonsav);
  strcpy(values[plat],latsav);
  quadflg=0;
  if (minlevelread < *level) return 0;
  else {
    minlevelread++;
    return 1;};
};

if(*level == minlevelread)
  {
    minlevelread= *level+1;
    return 1;
  }
else if(*level >minlevelread) return 0;
  minlevelread=jdbreada_(&handle,values,&valuesize);
  if(minlevelread<=ncrit) docalc();
  if (minlevelread < *level) return 0;
  else {
    minlevelread++;
    return 1;
  };
}


void ioclose_()
{
  jdbclose_(&handle);
}

int ioopen_(s,nparams,ntotal)
char *s[];
int *nparams;
int *ntotal;
{
char tmp[1024],*sp;
int i,j,k,m,maxclev,nread;

for (i=0;i<NVAR;i++)lev[i]= -1;
strcpy(tmp,s[0]);
for(i=1;i<*nparams;i++){
      j=strlen(tmp);
      if(*(tmp+j-1)==')')*(tmp+j-1)=','; else strcat(tmp,"(");
      strcat(tmp,s[i]);
      s[i][0]=0;
      strcat(tmp,")");
    };
  nread = -NVAR;
  putenv("QUERY_STRING=");
  maxclev=jdbopen_(&handle,tmp,names,&namesize,&nread);
  if (maxclev<0){printf("&x Bad Object:%s\n",s[0]);exit(1);};
  s[0][0]=0;
  *comments=0;
  while(jdbcomments_(&handle,tmp))
    {strcat(comments,tmp);strcat(comments,"\n");};
  nlevels=0;
  firstvar[nlevels]=0;
  for(i=0;i<nread;i++){
    lev[i]=jdblevel_(&handle,&i);
    if(lev[i]>firstvar[nlevels])firstvar[++nlevels]=lev[i];
  };
  firstvar[nlevels+1]=nread;

  doinit(nparams,s);

  k=0;
  *ntotal=firstvar[nlevels+1];
  for(i=0;i<=nlevels;i++)
    for(j=0;j< *ntotal;j++)if(lev[j]==i)vpntr[k++]=j;
  minlevelread = nlevels+1;
  return nlevels;
}

int iovarlevel_(vn)
int *vn;
{
return lev[vpntr[*vn]];
}

ioattrout_(vn,str)
int *vn;
char *str;
{
  return 0;
}

void iovalreal_(vn,f)
int *vn;
float *f;
{
if(strspn(values[vpntr[*vn]],"0123456789.+-"))
   *f=atof(values[vpntr[*vn]]);
else
   *f= -9999.0;
}

void iovalstr_(vn,tmp)
int *vn;
char *tmp;
{
char *s;
s=values[vpntr[*vn]];
s=s+strspn(s," ");
strcpy(tmp,s);
}

void ioname_(vn,s)
int *vn;
char *s;
{
strcpy(s,names[vpntr[*vn]]);
}

int iocommout_(s)
char *s;
{
  return 0;
}

int iowidth_(vn)
int *vn;
{
return 0;
}
