#include "TKheaders.h"
#include "TK_2ADPR.h"

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

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

    /* declare Data struct */
    TKINFO granuleHandle2ADPR;
    L2ADPR_SWATHS  L2ADPR;

    /* 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], precipESurf[NRAY];
    float precipWater[NRAY][NBIN];

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

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

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

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

	      /* precipRateESurface Read */
        precipESurf[j] = L2ADPR.FS.SLV.precipRateESurface[j];

	      for (k=0; k<NBIN; k++){
	        precipWater[j][k] = L2ADPR.FS.SLV.precipWater[j][k];
	      }

	      /* Print the value */
	      if(i == 5210 && j == 43){
	        printf("Scan=%d, Angle=%d\n", i, j);
	        printf("lat=%f, lon=%f\n", lat[j], lon[j]);
	        printf("precipRateESurface= %f (mm/hr)\n", precipESurf[j]);
	        printf("precipWater[43][170]= %f (g/m~3)\n", precipWater[j][170]);
	      } 
      }
    }

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

    return 0;
}

