#include "TKheaders.h"
#include "TK_2AGPROFGMI.h"

int main(int argc, char *argv[]){

    /* declare Data struct */
    TKINFO granuleHandle2AGPROFGMI;
    L2AGPROFGMI_S1	GPROFGMI;

    /* declare character */
    char job[256];
    char inputfile[256];

    strcpy(job, argv[1]);
    strcpy(inputfile, argv[2]);

    /* declare variables */
    int year, month, date, dayOfMon, hour, min;
    int i, j;
    int numOfScan;   
    int status;
 
    /* declare array */
    float lat[221], lon[221], surfacePrecipitation[221];

    /* HDF file open */
    /*status = TKopen(inputfile, "2AGPROFGMI", TKREAD, "HDF5", job, &granuleHandle2AGPROFGMI, 1);*/
    status = TKopen(inputfile, "2AGPROFGMI", TKREAD, "NETCDF4", job, &granuleHandle2AGPROFGMI, 1);
    if(status != TK_SUCCESS) {
        fprintf(stderr, "error:file open error[%s] input2AGPROFGMI\n", inputfile);
    }
    
    /* META data read */
    status = TKgetMetaInt(&granuleHandle2AGPROFGMI, "SwathHeader",
                            "NumberScansGranule", &numOfScan);
    if(status != TK_SUCCESS) {
         fprintf(stderr, "error:file read error[%s]\n", inputfile);
    }

    /* Scan data read */
    for (i=0; i<numOfScan; i++){
      status = TKreadScan( &granuleHandle2AGPROFGMI, &GPROFGMI);

      /* Scantime Read */
      year      = GPROFGMI.ScanTime.Year;
      month     = GPROFGMI.ScanTime.Month;
      dayOfMon  = GPROFGMI.ScanTime.DayOfMonth;
      hour      = GPROFGMI.ScanTime.Hour;
      min       = GPROFGMI.ScanTime.Minute;

      for (j=0; j<221; j++){
	/* Latitude and Longitude Read */
        lat[j]  = GPROFGMI.Latitude[j];
        lon[j]  = GPROFGMI.Longitude[j];

	/* surfacePrecipitation Read */
        surfacePrecipitation[j] = GPROFGMI.surfacePrecipitation[j];

	/* Print the value */
	if(i == 2000 && j == 151){
	  printf("Scan=%d, Angle=%d\n", i, j);
	  printf("lat=%f, lon=%f\n", lat[j], lon[j]);
	  printf("surfacePrecipitation= %f (mm/hr)\n", surfacePrecipitation[j]);
	} 
      }
    }

    /* HDF File close*/
    status = TKclose(&granuleHandle2AGPROFGMI);

    return 0;
}

