DDClearTimes.c 7.35 KB
/*----------------- ClearTimes ------------------------------*/
/* name:         ClearTimes.c
 *                  V.3.0
 * author:       Andrey  Fedorov
 * modification list:
 *              Jan 6, 2006, V.1.0 
 *              Aug 3, 2007, V.2.0 , Merging NODATA records
 *              Aug 28, 2009 V.3.0 new netCDF 
 *
 * Descriptions: Remove all zero records from times.nc file after
 * DDrechash processing.
 * usage : ClearTimes file_name
*------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <netcdf.h>
#include <DD.h>
#include <DD_comm.h>

/* Global Constant definitions */
#define TIMELENGTH_NAME "TimeLength"
#define REC_DIM_NAME "record"
#define REC_DIM NC_UNLIMITED
#define NAME_DIM_NAME "NameLength"

#define STARTTIME "StartTime"
#define STOPTIME   "StopTime"
#define FILENAME   "FileName"

#define DATAOK "DATAOK"
#define TEMPNAME "temptimes.nc"

#define TIME_TYPE NC_CHAR

main(int argc, char **argv)
{
   static char description[] = "usage: DDClearTimes  nc_times_file";

   char Start[TIMELENGTH], Stop[TIMELENGTH], Name[MAXFILENAME];
   char StartPrevious[TIMELENGTH], StopPrevious[TIMELENGTH], NamePrevious[MAXFILENAME];

/* NC definitions */
   int SourceID;  /* nc Source descriptor */
   int DestID;    /* nc Destination descriptor */
   int SStartID, SStopID, SNameID; /* Sources variables ID */
   int DStartID, DStopID, DNameID; /* Destination variables ID */
   int STlDimID,SRecDimID, SNlDimID;  /* Source ID of dimensions */
   int DTlDimID,DRecDimID, DNlDimID;  /* Destination ID of dimensions */

   size_t DRecordNumber = 0; // Current record number destination
   size_t SRecordNumber;     // Total record number source
   long ics;                // current source record

/*---------- Universal counters and offsets -----------------*/
   size_t TimeStart[2] = {0L,0L};
   size_t TimeCount[2] = {1L,TIMELENGTH};
   size_t NameCount[2] = {1L, MAXFILENAME};
   int DimVector[2];

   char command[100];
   char name[100];
   int  compress = 0;
   int status;

/*------------ Set value of global variables ------------------*/
 //  ncopts = NC_VERBOSE;

/*-----------------------------------------------------------------*/
/*
 * Check arguments
 */
   if(argc < 2)  {fprintf(stderr,"%s\n",description); exit(1); }
/*-----------------------------------------------------------------*/
/*
 * Creating temporay file to write (destination)
 */
     status = nc_create(TEMPNAME,NC_CLOBBER,&DestID);
     status = nc_def_dim(DestID,TIMELENGTH_NAME,TIMELENGTH,&DTlDimID);
     status = nc_def_dim(DestID,NAME_DIM_NAME,MAXFILENAME,&DNlDimID);
     status = nc_def_dim(DestID,REC_DIM_NAME,REC_DIM,&DRecDimID);
     
     DimVector[0] = DRecDimID;
     DimVector[1] = DTlDimID;
     status = nc_def_var(DestID, STARTTIME, TIME_TYPE,2,DimVector,&DStartID);
     status = nc_def_var(DestID, STOPTIME, TIME_TYPE,2,DimVector,&DStopID);
     DimVector[1] = DNlDimID;
     status = nc_def_var(DestID, FILENAME, TIME_TYPE,2,DimVector,&DNameID);
     DRecordNumber = 0;
     nc_enddef(DestID);
   
/*====== Open source file ===================*/
     status = nc_open(argv[1],NC_WRITE,&SourceID);
     status = nc_inq_dimid(SourceID,REC_DIM_NAME,&SRecDimID);
     status = nc_inq_varid(SourceID, STARTTIME,&SStartID);
     status = nc_inq_varid(SourceID, STOPTIME,&SStopID);
     status = nc_inq_varid(SourceID, FILENAME,&SNameID);
     status = nc_inq_dim(SourceID,SRecDimID,(char *)0, &SRecordNumber);
 
/*--------------- Start Value ----------------------*/
     strcpy(NamePrevious,DATAOK);

/*============ Loop to read Source and to write destination =======*/
    for(ics = 0; ics < SRecordNumber; ics++)
    {
       TimeStart[0] = ics;
       status = nc_get_vara_text(SourceID,SStopID, TimeStart,TimeCount,Stop);
       status = nc_get_vara_text(SourceID,SStartID,TimeStart,TimeCount,Start);
       status = nc_get_vara_text(SourceID,SNameID, TimeStart,NameCount,Name);
      
       if((Start[0] != '0') && (Stop[0] != '0')) // Correct time
       {
/*=================== Data exists==========================*/
	   if(strncmp(Name,NODATASTR,6) != 0)  /* Real data file */
           {
               TimeStart[0] = DRecordNumber;

/*=================Regular Case =======================*/
               if(strncmp(NamePrevious,NODATASTR,6) != 0) /* The previous record was with real data */
               {
	         status = nc_put_vara_text(DestID,DStartID,TimeStart,TimeCount,Start);
	         status = nc_put_vara_text(DestID,DStopID,TimeStart,TimeCount,Stop);
	         status = nc_put_vara_text(DestID,DNameID,TimeStart,NameCount,Name);
	          DRecordNumber++;
                  continue;
               } 
               else                                      /* The previous record has no real data */
               { 
	           status = nc_put_vara_text(DestID,DStartID,TimeStart,TimeCount,StartPrevious);
	           status = nc_put_vara_text(DestID,DStopID,TimeStart,TimeCount,StopPrevious);
	           status = nc_put_vara_text(DestID,DNameID,TimeStart,NameCount,NODATASTR);
                   TimeStart[0]++;
                   status = nc_put_vara_text(DestID,DStartID,TimeStart,TimeCount,Start);
	           status = nc_put_vara_text(DestID,DStopID,TimeStart,TimeCount,Stop);
	           status = nc_put_vara_text(DestID,DNameID,TimeStart,NameCount,Name);
	           DRecordNumber += 2;

                   strcpy(NamePrevious,DATAOK);
                   continue;
              }
           } /* End if currend record has no data */
                
/*============= NODATA First Occurrence: Prepare to Check Next Record ==============*/
           if(strncmp(NamePrevious,NODATASTR,6) != 0) /* Current record has no data, but previous one was OK */
           {
               strncpy(NamePrevious,NODATASTR,6);
               strcpy(StartPrevious,Start);
               strcpy(StopPrevious,Stop); 
               continue;
           }
/*=================== Merge NODATA records ========================*/
           if((DD_Time2Double(Start)-DD_Time2Double(StopPrevious) < MINGAP)) /* No Data in previous record, No Data in the current one*/
           {
               strcpy(StopPrevious,Stop);
           }
/*=================== No Merging Write PREVIOUS NODATA record  ===================?=*/
           else /* There is BIG gap between NODATA records */
           {
               TimeStart[0] = DRecordNumber;
	       status = nc_put_vara_text(DestID,DStartID,TimeStart,TimeCount,StartPrevious);
	       status = nc_put_vara_text(DestID,DStopID,TimeStart,TimeCount,StopPrevious);
	       status = nc_put_vara_text(DestID,DNameID,TimeStart,NameCount,NODATASTR);
	       DRecordNumber++; 
               strcpy(StartPrevious,Start);
               strcpy(StopPrevious,Stop); 
           }   
        }
    } /* FOR all recored */
    
    if(strncmp(Name,NODATASTR,6) == 0) /* The last record has no data */
    {
       TimeStart[0] = DRecordNumber;
       status = nc_put_vara_text(DestID,DStartID,TimeStart,TimeCount,StartPrevious);
       status = nc_put_vara_text(DestID,DStopID,TimeStart,TimeCount,StopPrevious);
       status = nc_put_vara_text(DestID,DNameID,TimeStart,NameCount,NODATASTR);
    } 
    
    nc_close(SourceID);
    nc_close(DestID);
    
/*=============== rename of temporary file ===================*/
    sprintf(command,"mv %s %s",TEMPNAME,argv[1]);
    system(command);
}
/*=============== END OF PROGRAM =============================*/