/* geosgame.c */
/* GEOS transformation to GAME format */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define INSIZE 7562500L
#define INWIDTH 2750L

/* conversion parameters */
double degree, radian;
/* globals */
int    COFF, LOFF;
double CFAC, LFAC, sub_lon;

/*
degree = M_PI / 180.;
radian = 1./degree;
*/

void
geos(double lon, double lat, int *c, int *l) {
    /* return (col, line) numbers */
    /* inputs are in degree */
    double c_lat, rl, r1, r2, r3, rn, x, y;

    c_lat = atan(0.993243 * tan(lat*degree));
    rl = 6356.5838/sqrt(1-0.00675701*cos(c_lat)*cos(c_lat));
    r1 = 42164.-rl*cos(c_lat)*cos((lon-sub_lon)*degree);
    r2 = -rl*cos(c_lat)*sin((lon-sub_lon)*degree);
    r3 = rl*sin(c_lat);
    rn = sqrt(r1*r1 + r2*r2 + r3*r3);
    x = atan(-r2/r1) * radian;
    y = asin(-r3/rn) * radian;
    *c = COFF + (int)(x*CFAC+0.5);
    *l = LOFF + (int)(y*LFAC+0.5);
}

/*
def geos(lon, lat):
    # return (col, line) numbers
    # inputs are in degree
    c_lat = math.atan(0.993243 * math.tan(lat*degree))
    rl = 6356.5838/math.sqrt(1-0.00675701*math.cos(c_lat)**2)
    r1 = 42164.-rl*math.cos(c_lat)*math.cos((lon-sub_lon)*degree)
    r2 = -rl*math.cos(c_lat)*math.sin((lon-sub_lon)*degree)
    r3 = rl*math.sin(c_lat)
    rn = math.sqrt(r1**2 + r2**2 + r3**2)
    x = math.atan(-r2/r1) * radian
    y = math.asin(-r3/rn) * radian
    c = COFF + int(x*CFAC+0.5)
    l = LOFF + int(y*LFAC+0.5)
    return c,l
*/

main(int argc, char *argv[]) {
    /* imgfile ... original IMG_...pgm */
    /* outfile ... GAME file name */
    /* lon_tl, lat_tl, lon_br, lat_br ... mapping box */
    /* width, height .................... its size    */
    /* COFF, LOFF, CFAC, LFAC, sub_lon .. parameters  */

    int lon_tl, lat_tl, lon_br, lat_br;
    int width, height;
    double lat_inc, lon_inc;
    FILE *fi, *fo;
    char dummy[100], inbuf[INSIZE];
    float lon, lat;
    int   x, y, l, c;

    /* globals */
    degree = M_PI / 180.;
    radian = 1./degree;

    if (argc < 14) {
        fprintf(stderr, "Usage: %s imgfile outfile lon_tl lat_tl lon_br lat_br width height COFF LOFF CFAC LFAC sub_lon\n", argv[0]);
        exit(1);
    }
    fi = fopen(argv[1], "r");
    fo = fopen(argv[2], "w");
    lon_tl = atoi(argv[3]);
    lat_tl = atoi(argv[4]);
    lon_br = atoi(argv[5]);
    lat_br = atoi(argv[6]);
    /* printf("%d %d %d %d\n", lon_tl, lat_tl, lon_br, lat_br); */
    width = atoi(argv[7]);
    height = atoi(argv[8]);
    lat_inc = (float)(lat_br - lat_tl) / height;
    lon_inc = (float)(lon_br - lon_tl) / width;
    /* globals for conversion factor */
    COFF = atoi(argv[9]); LOFF = atoi(argv[10]);
    CFAC = atof(argv[11]); LFAC = atof(argv[12]);
    sub_lon = atof(argv[13]);

    fgets(dummy, 80, fi); /* P5 */
    fgets(dummy, 80, fi); /* 2750 2750 */
    fgets(dummy, 80, fi); /* 255 */
    fread(inbuf, 1, INSIZE, fi);
    fclose(fi);

    fprintf(fo, "P5\n#N%dN%dE%dE%d %5.3f %5.3f\n%d %d\n255\n",
             lat_tl, lat_br, lon_tl, lon_br, -lat_inc, lon_inc,
             width, height);
    for (y=0; y<height; y++) {
        lat = lat_tl + lat_inc * y;
        for (x=0; x<width; x++) {
            lon = lon_tl + lon_inc * x;
            geos(lon, lat, &c, &l);
            fputc(inbuf[l*INWIDTH+c], fo);
        }
    }
    fclose(fo);
}
/*
def geotrans(imgfile, box, size):
    # imgfile ... original image file IMG_...pgm
    # box ....... lon_tl, lat_tl, lon_br, lat_br
    # size ...... width, height in pixel
    lon_tl, lat_tl, lon_br, lat_br = box
    width, height = size
    lat_inc = float(lat_br - lat_tl)/height
    lon_inc = float(lon_br - lon_tl)/width
    img = Image.open(imgfile)
    imgdata = list(img.getdata())
    oimgdata = []
    for y in xrange(height):
        lat = lat_tl + lat_inc * y
        for x in xrange(width):
            lon = lon_tl + lon_inc * x
            c, l = geos(lon, lat)
            oimgdata.append(imgdata[l*img.size[0]+c])
    oimg = Image.new('L', size)
    oimg.putdata(oimgdata)
    oimg = Image.new('L', size)
    oimg.putdata(oimgdata)
    return oimg

def writegame(img, box, size, ch):
    lon_tl, lat_tl, lon_br, lat_br = box
    width, height = size
    lat_inc = float(lat_br - lat_tl)/height
    lon_inc = float(lon_br - lon_tl)/width
    fname = 'MT1R%02d%02d%02d%02dIR%d.pgm' % (
               nomtime[0]%100, nomtime[1], nomtime[2], nomtime[3], ch)
    fo = file(fname, 'w')
    print >> fo, """P5
#N%dN%dE%dE%d %5.3f %5.3f
%d %d
255""" % (lat_tl, lat_br, lon_tl, lon_br, -lat_inc, lon_inc, size[0], size[1])
    imgdata = list(img.getdata())
    imgstr = ''.join(
                  [chr(imgdata[i]) for i in xrange(img.size[0]*img.size[1])])
    fo.write(imgstr)
    fo.close()
    os.system('gzip %s' % fname)
    return fname+'.gz'
*/
