#include "TKheaders.h"
#include "TK_3DPR.h"

#define LTL 28  /* number of low resolution 5°grid intervals of latitude from 70°S to 70°N. */ 
#define LHL 72  /* number of low resolution 5°grid intervals of longitude from 180°W to 180°E. */
#define CHN  3  /* number of channels: KuFS, KaMS, KaHS, DPRMS, KuMS, KaFS, DPRFS */
#define RT   3  /* number of rain types: all, stratiform, convective */
#define ST   3  /* number of surface types: all, ocean, land */

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


    /* declare Data struct */
    TKINFO granuleHandle3DPR;
    L3DPR_FS  L3DPR;

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

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

    /* declare variables */
    int i, j, k, l, m, n;
    int status;
    float  lat,lon;
    /* declare array */
    float  precipESurf[ST][RT][CHN][LHL][LTL];

    /* HDF file open */
    /*status = TKopen(inputfile, "3DPR", TKREAD, "HDF5", job, &granuleHandle3DPR, 1);*/
    status = TKopen(inputfile, "3DPR", TKREAD, "NETCDF4", job, &granuleHandle3DPR, 1);
    if(status != TK_SUCCESS) {
        fprintf(stderr, "error:file open error[%s] input3DPR\n", inputfile);
    }

    /* Grid data read */
    status = TKreadGridMulti( &granuleHandle3DPR, &L3DPR, "L3DPR_FS");

    /* precipRateESurface.mean data read */
    for (j=0; j<ST; j++){
	    for (k=0; k<RT; k++){
        for (l=0; l<CHN; l++){
          for (m=0; m<LHL; m++){
            for (n=0; n<LTL; n++){
	  	        precipESurf[j][k][l][m][n] = L3DPR.G1.precipRateESurface.mean[j][k][l][m][n];
		          if( m==63 && n==14 && l==0 && j==0 ){
                lat = (140.0/28.0) * n - 70.0 + (140.0/28.0/2);
                lon = (360.0/72.0) * m - 180.0 + (360.0/72.0/2);
                printf("lat=%f, lon=%f\n",lat,lon);
		            printf("precipRateESurface.mean[%d][%d][%d][%d][%d}= %f (mm/hr)\n",
                                 j, k, l, m, n, precipESurf[j][k][l][m][n]);	
		          } 
		        }
		      }
	      }
	    }
    }

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

    return 0;
}

