cdfsamplingfromdata.c 6.05 KB
/* $Id: timeResFromData.c,v 1.1 2010/08/31 10:32:55 budnik Exp $ */

/** @file timeResFromData.c
*   @brief Function to get CDAWEB dataset sampling times from data files
*/
#include <stdio.h>
#include <stdlib.h>
#include <cdf.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define fileName "temp.cdf"
#define badFile -100

/*************************************
   Global variables and structures
**************************************/
long  CDFDims,                                         // Number of dimensions in a CDF file
      CDFVars,                                            // Number of variables in a CDF file
      CDFDimSizes[CDF_MAX_DIMS],                   // Dimension Sizes in a CDF file
      CDFencoding,                                    // Data encoding
      CDFmajority,                                   // Variable majority
      CDFmaxRec,                                     // max Record number
      CDFAttrs;                                      // number of CDF Attributes
   //   CDFdatatype;

struct cdfvar {                              // CDF variable structure
     char name[CDF_VAR_NAME_LEN+1];
     long num;                                //variable number
     long datatype;
     long numElem;                     // variable dimensionality
     long recVariance;                 // variable rec Variance
     long dimVariances[CDF_MAX_DIMS];  // # of data values in dimSizes
} cdfVar;



/*--------------------------------------------------------------------------
 *  Handles a CDF error.
 *--------------------------------------------------------------------------*/
void cdf_handle_error(CDFstatus status)
{

  char  message[CDF_STATUSTEXT_LEN+1];

   CDFerror (status, message);              /* Get the appropriate message */
   fprintf  (stderr, "%s\n", message);
   exit(1);
}

void find_min(int* delta, int nRecs, int minFalse, int* minVal)
{
  int i, n, min;

   n = 1;
   i = 0;
   while (abs(delta[i]) <= minFalse) i++;
   if (i < nRecs) {
     min = abs(delta[i]); 
 // printf(" first MIN %d \n", min);
      for (i = 1; i < nRecs; i++) {
//   printf(" delta %d \n", delta[i]);
         if (delta[i] == min) n++;
         if (delta[i] < min && delta[i] > minFalse) {
              min = delta[i];
              n = 1;
        }
       }
      } else { min = -10;
               n = nRecs;
             }
  minVal[0] = min;
  minVal[1] = n; 
   printf(" MIN %d %d\n", min, n);
}

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

    long RecStart = 0, RecCount, RecInt = 1;
    long indices[1] = {0}, intervals[1] = {1}, counts[1] = {1};
    long varN = -1;  
    long datatype = 0, attrN, maxEntry;
    CDFstatus   cstatus;           // CDF status code
    CDFid id;
    double *value;
    int *delta;
    int i, minFalse = 0, numEnough;
    int min[2] = {0, 0};
    char attrValue[300];
    float Sampling;
    double epoch16[2];
    long year, month, day, hour, minute, sec, msec, mksec, nsec, psec;  
    
/*********************** Open CDF file *****************************/
     if ((cstatus = CDFopen(fileName, &id))  != CDF_OK)
                                cdf_handle_error(cstatus);
 
/*********** treat all vars as zVars with eliminated false dimensionality and get Nrecs **********/
    
    if ((cstatus = CDFlib(SELECT_, CDF_zMODE_, zMODEon2, NULL_)) != CDF_OK)
                                                   cdf_handle_error (cstatus);
    while (datatype != CDF_EPOCH && datatype != CDF_EPOCH16){
       varN++;
       if ((cstatus = CDFlib(SELECT_, zVAR_, varN,
                          GET_, zVAR_DATATYPE_, &datatype, NULL_)) != CDF_OK)
                                                    cdf_handle_error (cstatus);
  
  //     if (datatype == CDF_EPOCH16)  break;
      }
  fprintf(stdout,"DATATYPE %d %d\n", varN, datatype);
    if ((cstatus = CDFlib(SELECT_, zVAR_, varN,
                          GET_, zVAR_MAXREC_, &CDFmaxRec, NULL_)) != CDF_OK)
                                                   cdf_handle_error (cstatus);

    fprintf(stdout,"Max Recs %d\n", CDFmaxRec+1);
    if (CDFmaxRec < 10) {
       fprintf(stdout," %d\n", badFile);
       exit (0);
    }
    value = (double *)malloc(sizeof(double)* (CDFmaxRec+1)*((datatype == CDF_EPOCH16)+1));

/******************* Get Epoch  *************************/
    if ((cstatus = CDFlib (SELECT_,
                           zVAR_, varN,
                           zVAR_RECNUMBER_, RecStart,
                           zVAR_RECCOUNT_, CDFmaxRec+1,
                           zVAR_RECINTERVAL_, RecInt,
                           zVAR_DIMINDICES_, indices,
                           zVAR_DIMCOUNTS_, counts,
                           zVAR_DIMINTERVALS_, intervals,
                            GET_, zVAR_HYPERDATA_, value,  NULL_)) != CDF_OK) 
                                                cdf_handle_error (cstatus);
  
     delta = (int *)malloc(sizeof(int)* CDFmaxRec);
     for (i = 1; i < CDFmaxRec+1; i++)  
     {
         if (datatype == CDF_EPOCH16) {
         epoch16[0] = value[2*(i-1)];
         epoch16[1] = value[2*(i-1)+1];
         EPOCH16breakdown(epoch16, &year, &month, &day, &hour, &minute, &sec, &msec, &mksec, &nsec, &psec);
         value[i-1] = computeEPOCH(year, month, day, hour, minute, sec, msec);
         epoch16[0] = value[2*i];
         epoch16[1] = value[2*i+1];
         EPOCH16breakdown(epoch16, &year, &month, &day, &hour, &minute, &sec, &msec, &mksec, &nsec, &psec);
         value[i] = computeEPOCH(year, month, day, hour, minute, sec, msec);
        }
        delta[i-1] = (int)(value[i] - value[i-1] + 50)/100;
    }
 //      fprintf(stdout,"value %f\n", value[11]-value[10]);
     free(value); 
 //  fprintf(stdout,"delta %d\n", delta[0]);
     numEnough = CDFmaxRec/10; 
     while (min[1] <  numEnough) {
         fprintf(stdout,"minFalse %d\n", minFalse);
         find_min(delta, CDFmaxRec, minFalse, min); 
         minFalse = min[0];
     }
     
     free(delta); 
     if ((cstatus = CDFlib(CLOSE_, CDF_, NULL_)) != CDF_OK)
                                 cdf_handle_error (cstatus);

 //   fprintf(stdout,"%d %f\n",min[1], (float)min[0]/100.0);
      fprintf(stdout,"%f\n",(float)min[0]/10.0);
  
}