CleanNoData.c 4.31 KB
/* $Id: CleanNoData.c,v 1.1 2008/09/16 15:26:27 elena Exp $*/
/*=====================================================================
 *                  DD SYSTEM base package
 *                   DD_Server library
 *                     CleanNoData.c
 *                      
 *
 *  usage CleanNoData nc_times_file 
 *  Note that at the end of activity you have to run ClearTimes nc_times_file
 *=====================================================================*/
 /*=====================================================================
 * Description:
 * Program reads nc_times_file and remove NODATA intervals
 *  
 *
 * Dimensions:
 * TimeLingth   TIMELENGTH
 * NameLength   NAME_DIM
 * Record       Unlimited
 * Variables:
 *
 * char StartTime[TIMELENGTH]
 * char EndTime[TIMELENGTH]
 * 
 * Attributes:
 * none
 *======================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#include <netcdf.h>
#include <math.h>
#include <DD.h>

/* Global Constant definitions */

#define TIME_LENGTH_NAME "TimeLength"
#define TIME_LENGTH 17L
#define REC_DIM_NAME "record"
#define REC_DIM NC_UNLIMITED
#define NAME_DIM_NAME "NameLength"
#define NAME_DIM 32L
#define NAME_LENGTH 100

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

#define TIME_TYPE NC_CHAR
 
#define NODATA "NODATA\0"
#define TEMPORARY_NAME "temporary_name\0"

/*----------- Type definition ------------------------------*/
typedef struct
{
   char Name[NAME_LENGTH];   // current data file 
   char StartS[TIME_LENGTH]; // Start Time String
   char StopS[TIME_LENGTH];  // Stop Time String
   double StartD;            // Start double
   double StopD;             // Stop double 
} t_StartStop;


   int tmID, StartID, StopID, NameID;
   size_t RecordsNumber = 0; // Records Number of the existing times file
   static char ZeroTime[TIME_LENGTH] = "0000000000000000\0";
 

/*==================================================================================
 *                              MAIN
 *==================================================================================*/
main(int argc, char **argv)
{
   static char usage[] = "usage: CleanNoData  nc_times_file ";
   
   char  Name[NAME_DIM];

/* NC definitions */
   int  RecDimID;  /* ID of dimensions */   
   static size_t TimeStart[2] = {0,0};
   static size_t TimeCount[2] = {1,TIME_LENGTH};
   static size_t NameCount[2] = {1, NAME_DIM};
   int i;   
   int status;
         
   
/*=================================================================
 * Check arguments and options
 *=================================================================*/
   if(argc < 2)  {fprintf(stderr,"%s\n",usage); exit(1); }
 
/*-----------------------------------------------------------------*/  
  
/*======  open existing file ===================*/
    
       
         if((status = nc_open(argv[1],NC_WRITE|NC_SHARE,&tmID)) != NC_NOERR)
         {
            fprintf(stderr,"Can not open file %s to write\n",argv[1]);
            exit(0);
         }
         status = nc_inq_dimid(tmID,REC_DIM_NAME,&RecDimID);
         status = nc_inq_varid(tmID, STARTTIME,&StartID);
         status = nc_inq_varid(tmID, STOPTIME,&StopID);
         status = nc_inq_varid(tmID, FILENAME,&NameID);
         status = nc_inq_dimlen(tmID,RecDimID, &RecordsNumber);
   
/*==========================================================================*/

   /*==========================================================================
   * For Each record in the nc_times_file
   *=========================================================================*/
    for (i = 0; i < RecordsNumber; i++) {

        TimeStart[0] = i;
        nc_get_vara_text(tmID,NameID,TimeStart,NameCount,(void *)Name);
        if(strncmp(Name,NODATA,6) == 0) {             /* The record is NODATA */          
                  status = nc_put_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)ZeroTime);
                  status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)ZeroTime);
                  status = nc_put_vara_text(tmID,NameID,TimeStart,NameCount,(char *)TEMPORARY_NAME);
         }
   
      }
   nc_sync(tmID);
   status = nc_close(tmID);
} 
/*=========================================================================================================*/