DDClearTimes.c
7.35 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*----------------- 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 =============================*/