/*--unpack_tovs---------------------------------------------------------
 *
 *  This is a sample program that demonstrates the use of the the C functions
 *  to access HDF formatted pathP data files
 *
 *  This utility unpacks the contents of a TOVS Pathfinder Path-P HDF file
 *  and generates 26 Scientific Data Sets in binary format. Refer to the
 *  documentation for the TOVS PAthfinder Path-P data products for a
 *  description of the data contained in each of the binary files.
 *
 *  Interfaces:
 *              creadpathP()
 *
 *---------------------------------------------------------------------------*/

# include <hdf.h>
# include <mfhdf.h>
# include <stdio.h>

#define usage_message "usage: unpack_tovs infile"


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

  FILE    *fd;
  char	  hdf_file[256];
  char	  out_file[256];
  char	  c_date[6];
  char    c_2[2];
  int32   ret,hdf_id;


/* Meta Data variables */

  char    project[256], dataset_name[256],source_name[256],
          start_date[20],stop_date[20],position_type[256],
          grid_type[256],grid_name[256],projection[256],
          reference_date[256], processing_level[256],
          Temporal_Res[256],Spatial_X_Res[256],Spatial_Y_Res[256];


  float32 lat[4],lon[4];

/* Scientific data sets */

  float32 temp[9][67][67], wvapor[5][67][67],sktemp[67][67],fcld[67][67],
          clpress[67][67],zangle[67][67], emiss[67][67],pbl_strat[67][67],
          Cd[67][67],alpha[67][67],cltemp[67][67],
          latgrid[67][67],longrid[67][67],
          temp_sd[9][67][67],
wvapor_sd[5][67][67],sktemp_sd[67][67],fcld_sd[67][67],
          clpress_sd[67][67],zangle_sd[67][67],
emiss_sd[67][67],pbl_strat_sd[67][67],
          Cd_sd[67][67],alpha_sd[67][67],cltemp_sd[67][67];

  int32   obs[67][67],IIIreject[67][67];

/* Validate utility usage */

          switch (argc)
          {
           case 2:
              ++argv;
              strcpy (hdf_file, *argv);
              break;
           default:
              printf("%s\n",usage_message);
              exit(-1);
           }

/* Read the Scientific Data Sets   */


  ret=creadpathP(hdf_file, project,dataset_name,source_name,&processing_level,
		reference_date,start_date,stop_date,position_type,grid_type,
		grid_name,projection,lat,lon,Temporal_Res,Spatial_X_Res,
		Spatial_Y_Res,latgrid,longrid,temp,wvapor,sktemp,fcld,clpress,
		cltemp,zangle,emiss,pbl_strat,Cd,alpha,obs,IIIreject,
                temp_sd,wvapor_sd,sktemp_sd,fcld_sd,clpress_sd,
		cltemp_sd,zangle_sd,emiss_sd,pbl_strat_sd,Cd_sd,alpha_sd);

/* Use the 6-digit descriptor for date as the first part of the file name */

/*    Create the 6-digit date descriptor (yymmdd) */


  printf ("%s\n", reference_date);
  sscanf(&reference_date[2],"%2s",c_2);
  strcpy(c_date,c_2);
  sscanf(&reference_date[5],"%2s",c_2);
  strcat(c_date,c_2);
  sscanf(&reference_date[8],"%2s",c_2);
  strcat(c_date,c_2);

/* The name of the file will be the 6-digit date descriptor
   concatenated with the the scientific data set descriptor

       For example, 880723_latgrid.dat contains the latitude
                    coordinates for each of the 67 x 67 grid cells       */

  strcpy(out_file, c_date);
  strcat(out_file,"_latgrid.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(latgrid,sizeof(latgrid),1,fd);
  fclose(fd);

  strcpy(out_file, c_date);
  strcat(out_file,"_longrid.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(longrid,sizeof(longrid),1,fd);
  fclose(fd);

  strcpy(out_file, c_date);
  strcat(out_file,"_temp.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(temp,sizeof(temp),1,fd);
  fclose(fd);

  strcpy(out_file, c_date);
  strcat(out_file,"_wvapor.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(wvapor,sizeof(wvapor),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sktemp.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(sktemp,sizeof(sktemp),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_fcld.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(fcld,sizeof(fcld),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_clpress.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(clpress,sizeof(clpress),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_cltemp.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(cltemp,sizeof(cltemp),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_zangle.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(zangle,sizeof(zangle),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_emiss.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(emiss,sizeof(emiss),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_pbl_strat.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(pbl_strat,sizeof(pbl_strat),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_Cd.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(Cd,sizeof(Cd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_alpha.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(alpha,sizeof(alpha),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdtemp.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(temp_sd,sizeof(temp_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdwvapor.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(wvapor_sd,sizeof(wvapor_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdsktemp.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(sktemp_sd,sizeof(sktemp_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdfcld.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(fcld_sd,sizeof(fcld_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdclpress.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(clpress_sd,sizeof(clpress_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdcltemp.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(cltemp_sd,sizeof(cltemp_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdzangle.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(zangle_sd,sizeof(zangle_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdemiss.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(emiss_sd,sizeof(emiss_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdpblstrat.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(pbl_strat_sd,sizeof(pbl_strat_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdCg.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(Cd_sd,sizeof(Cd_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_sdalpha.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(alpha_sd,sizeof(alpha_sd),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_IIIreject.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(IIIreject,sizeof(IIIreject),1,fd);
  fclose(fd);

  strcpy(out_file,c_date);
  strcat(out_file,"_obs.dat");
  fd = fopen(out_file,"w");
  ret= fwrite(obs,sizeof(obs),1,fd);
  fclose(fd);

/* Print the metadata to standard input                           */

  fprintf(stdout,"%s\n",project);
  fprintf(stdout,"%s\n",dataset_name);
  fprintf(stdout,"%s\n",source_name);
  fprintf(stdout,"%s\n",processing_level);
  fprintf(stdout,"%s\n",reference_date);
  fprintf(stdout,"%s\n",start_date);
  fprintf(stdout,"%s\n",stop_date);
  fprintf(stdout,"%s\n",position_type);
  fprintf(stdout,"%s\n",grid_name);
  fprintf(stdout,"%s\n",grid_type);
  fprintf(stdout,"%s\n",projection);
  fprintf(stdout,"%s\n",Temporal_Res);
  fprintf(stdout,"%s\n",Spatial_X_Res);
  fprintf(stdout,"%s\n",Spatial_Y_Res);

/* Print the latitude and longitude coordinates which reference the
   corner boundaries of the grid                                      */


  fprintf(stdout,"Lat: %f-%f-%f-%f\n",lat[0],lat[1],lat[2],lat[3]);
  fprintf(stdout,"Lon: %f-%f-%f-%f\n",lon[0],lon[1],lon[2],lon[3]);

/* Print the status of the unpacking process                          */

  if (ret < 0) {
    fprintf(stderr,"Error reading pathP hdf file : %s\n",hdf_file);
    exit(1);
  }
  else {
    fprintf(stderr,"Sucessfully read pathP hdf file : %s\n",hdf_file);
    exit(0);
  }
}













