/***************************************************************************/
/**                                                                       **/
/**                      t  x  t  2  g  r  i  d  .  c                     **/
/**                                                                       **/
/**     Program converts text data to grid clm file format. Data must be  **/
/**     in the following format:                                          **/
/**                                                                       **/
/**     Id,Lon,Lat,X_CRU,Y_CRU                                            **/
/**     1,-179.75,-16.25,1,148                                            **/
/**     2,..                                                              **/
/**                                                                       **/
/**     written by Werner von Bloh                                        **/
/**     Potsdam Institute for Climate Impact Research                     **/
/**     PO Box 60 12 03                                                   **/
/**     14412 Potsdam/Germany                                             **/
/**                                                                       **/
/**     Last change: 29.03.2012                                           **/
/**                                                                       **/
/***************************************************************************/

#include "lpj.h"

#define TXT2GRID_VERSION "1.0.001"
#define USAGE "Usage: txt2grid [-h] [-fmt s] [-skip n] gridfile clmfile\n"

int main(int argc,char **argv)
{
  FILE *file,*gridfile;
  const char *fmt;
  Coord grid;
  String line;
  int i,nskip,n;
  float lon,lat;
  Header header;
  fmt="%*d,%f,%f,%*d,%*d";
  nskip=1;
  for(i=1;i<argc;i++)
    if(argv[i][0]=='-')
    {
      if(!strcmp(argv[i],"-h"))
      {
        printf("txt2grid " TXT2GRID_VERSION " (" __DATE__ ") - convert text file to\n"
               "       clm grid file for lpj C version\n");
        printf(USAGE
               "-h        print this help text\n" 
               "-fmt s    format string for text input\n" 
               "-skip n   skip first n lines\n" 
               "gridfile  filename of grid data file\n"
               "clmfile   filename of clm data file\n");
        return EXIT_SUCCESS;
      }
      if(!strcmp(argv[i],"-fmt"))
      {
        if(i==argc-1)
        {
           fputs("Argument missing after '-fmt' option.\n",stderr);
           fprintf(stderr,USAGE);
           return EXIT_FAILURE;
        }
        fmt=argv[++i];
      }
      if(!strcmp(argv[i],"-skip"))
      {
        if(i==argc-1)
        {
           fputs("Argument missing after '-skip' option.\n",stderr);
           fprintf(stderr,USAGE);
           return EXIT_FAILURE;
        }
        nskip=atoi(argv[++i]);
      }
      else
      {
        fprintf(stderr,"Error: invalid option '%s'.\n",argv[i]);
        fprintf(stderr,USAGE);
        return EXIT_FAILURE;
      }
    }
    else 
      break;
  if(argc<i+2)
  {
    fprintf(stderr,"Filenames missing.\n");
    fprintf(stderr,USAGE);
    return EXIT_FAILURE;
  }
  file=fopen(argv[i],"r");
  if(file==NULL)
  {
    fprintf(stderr,"Error opening '%s': %s\n",argv[i],strerror(errno));
    return EXIT_FAILURE;
  }
  /* skip nskip lines */
  for(n=0;n<nskip;n++)
    fgets(line,STRING_LEN,file);
  header.ncell=0;
  header.nbands=2;
  header.order=CELLYEAR;
  header.firstcell=0;
  header.firstyear=0;
  header.nyear=1;
  gridfile=fopen(argv[i+1],"wb");
  if(gridfile==NULL)
  {
    fprintf(stderr,"Error creating '%s': %s\n",argv[i+1],strerror(errno));
    return EXIT_FAILURE;
  }
  fwriteheader(gridfile,header,LPJGRID_HEADER,LPJGRID_VERSION);
  while(fscanf(file,fmt,&lon,&lat)==2)
  {
    grid.lon=lon;
    grid.lat=lat;
    writecoord(gridfile,grid);
    header.ncell++;
  }
  printf("Number of grid cells: %d\n",header.ncell);
  rewind(gridfile);
  /* write header again with correct number of grid cells */
  fwriteheader(gridfile,header,LPJGRID_HEADER,LPJGRID_VERSION);
  fclose(gridfile);
  fclose(file);
  return EXIT_SUCCESS;
} /* of 'main' */
