/* masksnc.c
 *              Emilio          4/1/99
 *                Updated program.
 *              Emilio          4/22/98
 *                *
 * COMPILE AS: gccemu masksnc
 * RUN AS: masksnc
 ******************************************************************************/

#include "emu.h"

const char * cpszUsage = {"usage: masksnc \n\
Create netcdf Mask file from binary (2-byte) files.\n\
\n\
"};

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

/* local variables */
short *vars, *maskdata_s;
unsigned char *maskdata_uc;
int ncID;     /* netCDF file IDs  */
int errnc;


int main(int argc, char *argv[])
{
  Initialize(&argc, argv, NOMASK);

  Setup();
  ProcessAndOutput();

  CleanUp();

  return(0);
}

/* ================================================================= */
void Setup(void)
{
  vars = alloc1d_s(0,nRows*nCols-1);
  maskdata_s = alloc1d_s(0,nRows*nCols-1);
  maskdata_uc = alloc1d_uc(0,nRows*nCols-1);
}

/* ================================================================= */
void ProcessAndOutput(void)
{
  FILE *binfin;
  int i, irev, col, row, nmasks;
  int maskID=1;    /* mask ID    */
  size_t strlen;
  nc_type ncType;
  char maskvarname[FILENAMELEN], file[FILENAMELEN], *filename;
  char ncfile[FILENAMELEN];       /* cdf file to be written  */
  char baseCDLtoNC[FILENAMELEN];  /* ncgen command to generate header nc file  */


  /* TRANSLATE DEFAULT CDL HEADER FILE TO netcdf FORMAT USING ncgen */
  strcpy(ncfile, "/oc7/casa/modelinput/nc/masks.nc");
  sprintf(baseCDLtoNC, "%s -o %s /oc7/casa/results/netcdf/cdls/masks.cdl", pszNCGenFile, ncfile);
  system(baseCDLtoNC);

  /* OPEN AND INITIALIZE netCDF FILE DEFINITIONS */
  errnc = nc_open(ncfile, NC_WRITE, &ncID);
  errnc = nc_inq_nvars(ncID, &nmasks);
  if (!bQuiet)  printf("ncinquire: nmasks=%d, Number of mask variables: %d\n", nmasks, nmasks-2);


  while (++maskID <= nmasks-1)
  {
    if (!bQuiet)  printf("maskID=%d\n", maskID);

    /* EXTRACT VARTYPE (NC_BYTE or NC_SHORT), THEN NUMBER OF BYTES (1 or 2) */
    errnc = nc_inq_varname(ncID, maskID, maskvarname);
    errnc = nc_inq_vartype(ncID, maskID, &ncType);

    /* EXTRACT FILENAME FROM CDL/NC FILE ITSELF */
    errnc = nc_inq_attlen(ncID, maskID, "long_name_mod", &strlen);
    filename = (char *) malloc(strlen * sizeof(char));
    errnc = nc_get_att_text(ncID, maskID, "long_name_mod", filename);
    strcpy(file, "/oc7/casa/modelinput/input/mask/0.05deg/");
    strcat(file, filename);
    if (bDebug)
      printf("ncvarinq: maskvarname=%s, %s, ncType=%d\n", maskvarname,filename, ncType);

    /* OPEN BINARY FILE, READ DATA */
    if ((binfin = fopen(file, "r")) == NULL)
      nrerror("Couldn't open file %s for reading\n", filename);

    if (fread(vars, sizeof(short), nRows*nCols, binfin) != nRows*nCols)
      nrerror("Could not do fread in %s\n", filename);
    fclose(binfin);


    /* WRITE MASK DATA */
    for (i = 0; i < nRows*nCols; i++)
    {
      irev = getRowRevCol(i, &col, &row);

      /* handle fill values (areas outside mask) */
      if (ncType == NC_BYTE)
        maskdata_uc[irev] = (vars[i] <= 0) ? 255 : (char) vars[i];
      else if (ncType == NC_SHORT)
        maskdata_s[irev]  = (vars[i] <= 0) ? -999 : vars[i];
    } /* END i FOR-LOOP */

    /* write data to NC file */
    if (ncType == NC_BYTE)
      errnc = nc_put_var_uchar(ncID, maskID, maskdata_uc);
    else if (ncType == NC_SHORT)
      errnc = nc_put_var_short(ncID, maskID, maskdata_s);
  } /* END maskID-LOOP */

  errnc = nc_close(ncID);
}

/* ================================================================= */
void ProcessCommandLineArgs(int * pArgc, char * argv[])  {}
/* ================================================================= */
void LocalCleanUp(void)
{
  free1d_s(vars,0,nRows*nCols-1);
  free1d_s(maskdata_s,0,nRows*nCols-1);
  free1d_uc(maskdata_uc,0,nRows*nCols-1);
}