nctimeinfo.c 2.09 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netcdf.h>


/**************************
   Function prototypes
**************************/
 
void handle_netcdf_error (int);

main(int argc, char **argv)
{
      int ncID, timeID, dimids[NC_MAX_DIMS];
      size_t nrecs;
      double time_double[3], sampling, sampling_min = 100000;      
      int i, status, nvars;
      
      char varname[NC_MAX_NAME];
	
      size_t TimeStart = 0;
      size_t TimeCount = 3;  
      
      
        if (argc <= 1) {
            printf("Incorrect number of arguments\n"); 
   	    exit(0);
	}
	
	if((status = nc_open(argv[1],0,&ncID)) != NC_NOERR){
	    printf("%d",status);
	    exit(0);
	}
		
	if ((status = nc_inq_varid (ncID, "Time", &timeID)) != NC_NOERR) {
	      printf("%d",status);
	      exit(0);                
	} 
	
	if ((status = nc_inq_vardimid(ncID, timeID, dimids)) != NC_NOERR){
	      printf("%d", status);
	      exit(0); 
	}
	
	if ((status = nc_inq_dimlen(ncID,dimids[0], &nrecs)) != NC_NOERR){	 
	      printf("%d", status);
	      exit(0);  
	} 
	
	if ((status =  nc_get_vara_double(ncID, timeID, &TimeStart, &TimeCount,  time_double)) != NC_NOERR){
		printf("%d", status);
		exit(0);  
	}  
		 
	for (i = 0; i < 2; i++) {
	   sampling = time_double[i+1] - time_double[i];
	   if (sampling < sampling_min && sampling > 0.0) sampling_min = sampling;	  
	 }
	  
	 printf("%lf:", time_double[0]);        
	 
	 TimeStart = nrecs - 3;
	 if ((status =  nc_get_vara_double(ncID, timeID, &TimeStart, &TimeCount,  time_double)) != NC_NOERR){
		printf("%d", status);
		exit(0);  
	} 	
	printf("%lf#", time_double[2]);
	
	for (i = 0; i < 2; i++) {
	   sampling = time_double[i+1] - time_double[i];
	   if (sampling < sampling_min && sampling > 0.0) sampling_min = sampling;	  
	 }
	 
	printf("%lf", sampling_min);
 
	status = nc_close(ncID);
}
/*--------------------------------------------------------------------------
 *  Handles a netCDF error.
 *--------------------------------------------------------------------------*/

void handle_netcdf_error(int status) {
  fprintf(stderr, "%s\n", nc_strerror(status));  
}