#include "TKheaders.h"
#include "TK_2HSLH.h"

#define NRAY 49   /* number of Anglebin in one scan */
#define NBIN 80  /* number of Rangebin in one Anglebin */

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

    /* declare Data struct */
    TKINFO granuleHandle2HSLH;
    L2HSLH_SWATH  L2HSLH;

    /* 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, k;
    int numOfScan;   
    int status;
 
    /* declare array */
    float lat[NRAY], lon[NRAY];
    float latentHeating[NRAY][NBIN];

    /* HDF file open */
    /*status = TKopen(inputfile, "2HSLH", TKREAD, "HDF5", job, &granuleHandle2HSLH, 1);*/
    status = TKopen(inputfile, "2HSLH", TKREAD, "NETCDF4", job, &granuleHandle2HSLH, 1);
    if(status != TK_SUCCESS) {
        fprintf(stderr, "error:file open error[%s] input2HSLH\n", inputfile);
    }
    
    /* META data read */
    status = TKgetMetaInt(&granuleHandle2HSLH, "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( &granuleHandle2HSLH, &L2HSLH);

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

      for (j=0; j<NRAY; j++){

	    /* Latitude and Longitude Read */
        lat[j]  = L2HSLH.Latitude[j];
        lon[j]  = L2HSLH.Longitude[j];

	    /* latentHeating Read */
        for (k=0; k<NBIN; k++){
          latentHeating[j][k] = L2HSLH.latentHeating[j][k];
        }

	      /* Print the value */
	      if(i == 160 && j == 40){
	        printf("Scan=%d, Angle=%d\n", i, j);
	        printf("lat=%f, lon=%f\n", lat[j], lon[j]);
	        printf("latentHeating= %f (K/hr)\n", latentHeating[j][10]);
	      } 
      }
    }

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

    return 0;
}

