#include "TKheaders.h"
#include "TK_1BKu.h"

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

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

  /* declare Data struct */
  TKINFO granuleHandle1BKu;
  L1BKu_FS  L1BKu;

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

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

  /* declare variables */
  int year, month, dayOfMon, hour, min;
  int i, j, k;
  int numOfScan;   
  int status;

  /* declare array */
  float lat[NRAY], lon[NRAY];
  short noisePower[NRAY], echoPower[NRAY][NBIN];

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

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

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

      /* noisePower Read */
      noisePower[j] = L1BKu.Receiver.noisePower[j];

      /* echoPower Read */
      for (k=0; k<NBIN; k++){
        echoPower[j][k] = L1BKu.Receiver.echoPower[j][k];
      }

      /* Print the value */
      if(i == 3946 && j == 19){
        printf("Scan=%d, Angle=%d\n", i, j);
        printf("lat=%f, lon=%f\n", lat[j], lon[j]);
        printf("echoPower[259]= %d \n", echoPower[j][259]);
        printf("noisePower= %d \n", noisePower[j]);
      } 
    }
  }

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

  return 0;
}

