Blame view

src/DATA/TOOLS/CleanNoData.c 4.31 KB
0ec21281   Elena.Budnik   reorganization + ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* $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);
} 
/*=========================================================================================================*/