/* clclimate.c
 *                *
 *              Emilio          4/1/99
 *                Updated program.
 *              Emilio          3/11-15/99
 *                Wrote and debugged program.
 * COMPILE AS: gccemu clclimate
 ******************************************************************************/

#include "emu.h"

const char * cpszUsage = {"usage: lcclimate \n\
.\n\
"};

/* local functions */
void Setup(void);
void ProcessAndOutput(void);

/* local variables */
#define     TOTPIXELS  62483
int errnc;
int ncID, tID;
float *map;
static size_t count[] = {1,0,0}, start[] = {0,0,0};


/* The coordinates are in degree-decimals and indicate the SW corner of each pixel */
typedef struct _climate
{
  float   lon;
  float   lat;
  float   data[12];
} CLIMATE;

CLIMATE clim[TOTPIXELS];


int main(int argc, char *argv[])
{
  AssignString(&pszNCtitle, "Cramer & Leemans CLIMATE v2.1 Climatology");
  AssignString(&pszNChist, "Created by EMU/clclimate.c., March 15, 1999");

  Initialize(&argc, argv, NOMASK);

  Setup();
  ProcessAndOutput();

  CleanUp();

  return(0);
}

/* ================================================================= */
void Setup(void)
{
  map = alloc1d_f(0,nRows*nCols-1);

  /* Create output netcdf file */
  SetupDefaultAxes(TRUE, TRUE);
  count[1] = stAxes[1].length;
  count[2] = stAxes[0].length;
  ncID = newNCsetup(&tID, pszOutputFile, pszNCtitle, pszNChist);
}

/* ================================================================= */
void ProcessAndOutput(void)
{
  int iout, i, col, row, v, p, mo;
  short sFillValue = -999, FillValue;
  BYTE  byFillValue = 255;
  float fFillValue = -999.0;
  float scale_factor, lon;
  emu_type VarEmuType;
  FILE *fin;
  int varID;


  for (v=0; v < nVars; v++)
  {
    if (bDebug)   printf("\n Var %d, %s \n", v,pstVars[v].name);
    if ((fin = fopen(pstVars[v].path,"r")) == NULL)
      nrerror("Couldn't open file %s for writing\n", pstVars[v].path);

    for (p=0; p < TOTPIXELS; p++)
      fscanf(fin, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f", &clim[p].lon, &clim[p].lat,
          &clim[p].data[0], &clim[p].data[1], &clim[p].data[2], &clim[p].data[3],
          &clim[p].data[4], &clim[p].data[5], &clim[p].data[6], &clim[p].data[7],
          &clim[p].data[8], &clim[p].data[9], &clim[p].data[10], &clim[p].data[11]);

    fclose(fin);


    VarEmuType = pstVars[v].tEmuType;
    if (VarEmuType == EMU_FLOAT)
      FillValue = fFillValue;
    else if (VarEmuType == EMU_SHORT)
      FillValue = sFillValue;
    else if (VarEmuType == EMU_BYTE)
      FillValue = byFillValue;

    varID = DefNCvar(ncID, pstVars[v].name, VarEmuType, "xyt", pstVars[v].szP[0], pstVars[v].szP[1]);
    if (pstVars[v].nParams > 0)
    {
      scale_factor = pstVars[v].P[0];
      AddNCCompressionAttributes(ncID, varID, scale_factor, 0.0);
    }
    else
      scale_factor = 1.0;


    for (mo=0; mo < 12; mo++)
    {
      for (i=0; i < nRows*nCols; i++)
        map[i] = FillValue;

      for (p=0; p < TOTPIXELS; p++)
      {
        /* adjust lon * lat from SW corner (LC format) to center of pixel, shift lon,
           then convert to col,row and then to array index                             */
        climate.c.html#lon">lon = clim[p].climate.c.html#lon">lon+0.5*fHScale;
        lon += (lon < 0) ? 360.0 : 0.0;
        col = (int)floor((lon - fLonLeft)/fHScale + 0.5);
        row = (int)floor(((clim[p].lat+0.5*fVScale) - fLatBtm)/fVScale + 0.5);
        iout = nCols*row + col;

        climate.c.html#map">map[iout] = clim[p].data[mo] / scale_factor;
      }

      start[0] = mo;
      errnc = nc_put_vara_float(ncID, varID, start, count, map);
    }
  }

  errnc = nc_close(ncID);
}

/* ================================================================= */
void ProcessCommandLineArgs(int * pArgc, char * argv[])
{
}
/* ================================================================= */
void LocalCleanUp(void)
{
  free1d_f(map,0,nRows*nCols-1);
}