/*
 * This program is Free Software under the GNU GPL (>=v2).
 * Conversion of LANDSAT-TM5 DNs to at-sensor radiances
 */

#include <stdio.h>
#include <string.h>
#include <math.h>
#include "gis.h"

int main(int argc, char *argv[])
{
    CELL *cellbuf;
    DCELL *result_cell;
    int nrows, ncols;
    int row, col;
    char *groupname;
    int fd;
    struct GModule *module;
    struct
    {
        struct Option *group;
    } parm;
    struct
    {
        struct Flag *quiet;
    } flag;

    module = G_define_module();
    module->description = "Module to convert LANDSAT-TM5 "
        "digital numbers to at-sensor radiances";

    parm.group = G_define_option();
    parm.group->key = "group";
    parm.group->type = TYPE_STRING;
    parm.group->required = YES;
    parm.group->description = "Imagery group of images to\
                               be converted";

    flag.quiet = G_define_flag();
    flag.quiet->key = 'q';
    flag.quiet->description = "Run quietly";

    G_gisinit (argv[0]);

    if (G_parser(argc,argv))
        exit(-1);
    groupname = parm.group->answer;
    
    /* function defined in other file */
    open_file(groupname, fd);

    nrows = G_window_rows();
    ncols = G_window_cols();
    cellbuf = G_allocate_raster_buf(CELL_TYPE);
    result_cell = G_allocate_raster_buf(DCELL_TYPE);

    /* go row wise and col wise through image */
    for (row = 0; row < nrows; row++)        /* rows loop */
    {
          /* read integer satellite channel */
          G_get_raster_row (fd, cellbuf, row, CELL_TYPE);
          for (col = 0; col < ncols; col++)  /* cols loop */
           {
              /* the formula is defined in another file: */
              result_cell[col] = calc_new_pixel(cellbuf);
           }  /* end cols loop */

           G_put_raster_row(fd, result_cell, DCELL_TYPE);
    } /* end rows loop */

    G_close_cell (fd);
    return 0;
}
