GetFileNames.c 4.12 KB
/* $Id: GetFileNames.c,v 1.2 2008/06/16 12:11:58 elena Exp $ */

/**
*   @file GetFileNames.c
*   @brief Print File names
*   @arg dataSetID, StartTime, StopTime
*
*
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <netcdf.h>
#include <DD.h>


#define NAME_LENGTH 100
#define NAME_DIM 32L

int DataID, RecID,  StartID, StopID, NameID;;
size_t RecNum;
char StartT[TIMELENGTH], StopT[TIMELENGTH];
size_t count[2] = {1L,TIMELENGTH};
size_t start[2] = {0L,0L};
size_t NameCount[2] = {1, NAME_DIM};
double StartTime, StopTime;
char Name[NAME_LENGTH];

/*---------------- NC ERROR --------------------------------------*/
void nc_handle_error(int status)
{
  fprintf(stderr, "%s\n", nc_strerror(status));
  exit(1);
}

/* ----------------------- MAIN ------------------------------------*/
  int main(int argc, char *argv[])
{
    
    int status, len, StartRecord, StopRecord;
    int i, hi, lo = 0;
    double StartD0, StartD1 = 0.0, StopD0, StopD1;
 
   if (argc < 4)
    {
       fprintf(stderr,"Usage: GetFileNames ViId StartTime StopTime\n");
       exit(0);
    }

/* find VI in reference file */
     
     
     StartTime = atof(argv[2]);
     StopTime = atof(argv[3]);
        
/* Open VI_times.nc */
      
       if ((status = nc_open(argv[1], NC_NOWRITE, &DataID)) != NC_NOERR)
                                                nc_handle_error(status);

       if ((status = nc_inq_dimid(DataID, "record", &RecID)) != NC_NOERR)
                                          nc_handle_error(status);
     
/*   Get Number of Records */
   
      if ((status = nc_inq_dimlen(DataID, RecID, &RecNum)) != NC_NOERR)
                                          nc_handle_error(status);

      if ((status = nc_inq_varid(DataID, "StartTime", &StartID)) != NC_NOERR)
                                          nc_handle_error(status);
      if ((status = nc_inq_varid(DataID, "StopTime", &StopID)) != NC_NOERR)
                                          nc_handle_error(status);

     if ((status = nc_inq_varid(DataID, "FileName", &NameID)) != NC_NOERR)
                                          nc_handle_error(status);
/* Get The First Start and The Last Stop Times */      
      start[0] = 0;     

     if((status = nc_get_vara_text(DataID, StartID, start, count, StartT)) != NC_NOERR)
                                          nc_handle_error(status);

      start[0] = RecNum-1; 
      if((status = nc_get_vara_text(DataID, StopID, start, count, StopT)) != NC_NOERR)
                                          nc_handle_error(status);
   
      if (StartTime > DD_Time2Double(StopT) || StopTime < DD_Time2Double(StartT)) 
       {
          fprintf(stdout, "OUTOFTIME\0"); 
          nc_close(DataID);
          exit(0);
        }

      hi = RecNum; 

 /* Find Start Record */
      while (lo <  hi)
       {               
               i = (lo+hi)/2; 
               start[0] = i + 1;
               status = nc_get_vara_text(DataID,StartID,start,count,(char *)StartT);
               StartD1 = DD_Time2Double(StartT);
               if (StartD1 > StartTime) hi--;  
               start[0] = i;
               status = nc_get_vara_text(DataID,StartID,start,count,(char *)StartT);
               StartD0 = DD_Time2Double(StartT);                
               if (StartD0 < StartTime) lo++;              
               if (StartD1 <=  StartTime && StartD0 >= StartTime) break;
       }        
       status = nc_get_vara_text(DataID, NameID, start, NameCount,(char *)Name);
       fprintf(stdout, "%s;",Name);
       status = nc_get_vara_text(DataID,StopID,start,count,(char *)StopT);
       StopD0 = DD_Time2Double(StopT);
 
       while (StopTime > StopD0  &&  start[0] <= RecNum) {
              status = nc_get_vara_text(DataID,StopID,start,count,(char *)StopT); 
              status = nc_get_vara_text(DataID, NameID, start, NameCount,(char *)Name);
              if (start[0] > i) fprintf(stdout, "%s;", Name); 
              StopD0 = DD_Time2Double(StopT);
              start[0]++;
        }
 
      nc_close(DataID);
    
}