Commit 616e9e0b8921f6ce5894cec1566e355ea71bb41c
1 parent
c1f7435f
Exists in
master
and in
9 other branches
Fix SetTime to support files with time as a timestamp + add script to convert a data file
Showing
2 changed files
with
230 additions
and
6 deletions
Show diff stats
... | ... | @@ -0,0 +1,226 @@ |
1 | +/* $Id: TimesUpdate.c,v 1.3 2008/02/19 16:24:58 elena Exp $*/ | |
2 | +/*===================================================================== | |
3 | + * DD SYSTEM base package | |
4 | + * DD_Server library | |
5 | + * TimesUpdate.c | |
6 | + * V.4.6 | |
7 | + * | |
8 | + * usage TimesUpdate -r/-i[-u]/-d nc_times_file nc_data_file[*.gz] nc_data_file[*.gz] .... | |
9 | + * Note that in most of cases at the end of activity you have to run ClearTimes nc_times_file | |
10 | + * | |
11 | + * Versions: | |
12 | + * Apr 23 1997, Usung *.gz files V.2.0 | |
13 | + * Jan 4, 2006, V.3.0 - insert file and check overlaping | |
14 | + * Jan 13,2006, V.3.1 - error is corrected | |
15 | + * Feb 02,2006, V.3.2 - Corrected removing database in case of error | |
16 | + * Sep 20, 2007, V.4.0, Fedorov, from rehash.nc. New NetCDF V.3, syncronization | |
17 | + * Sep 25, 2007 V.4.1 The very first file and first file insert | |
18 | + * Nov 29, 2007 V.4.2 If several first records - bug correction | |
19 | + * Feb 17, 2008 V.4.5 Fedorov: New options , remake, insert, delete | |
20 | + * Feb 19, 2008 V.4.6 delete bugs corrected | |
21 | + *=====================================================================*/ | |
22 | + /*===================================================================== | |
23 | + * Description: | |
24 | + * Program reads list of nc_data_files, probabbly gzipped, reads StartTime and Stop time | |
25 | + * and create or update nc_times_file. The sequence of data files are not important | |
26 | + * -r remove nc_times_file and create new one | |
27 | + * -i insert new file(s) | |
28 | + * -d remove file(s) from nc_times | |
29 | + * | |
30 | + * Dimensions: | |
31 | + * TimeLingth TIMELENGTH | |
32 | + * NameLength NAME_DIM | |
33 | + * Record Unlimited | |
34 | + * Variables: | |
35 | + * | |
36 | + * char StartTime[TIMELENGTH] | |
37 | + * char EndTime[TIMELENGTH] | |
38 | + * | |
39 | + * Attributes: | |
40 | + * none | |
41 | + *======================================================================*/ | |
42 | +#include <stdio.h> | |
43 | +#include <stdlib.h> | |
44 | +#include <string.h> | |
45 | +#include <ctype.h> | |
46 | +#include <dirent.h> | |
47 | +#include <netcdf.h> | |
48 | +#include <math.h> | |
49 | +#include <DD.h> | |
50 | + | |
51 | +/*================================================================================== | |
52 | + * MAIN | |
53 | + *==================================================================================*/ | |
54 | + | |
55 | +#define COMMAND_MAX_LEN 1000 | |
56 | +#define MAX_FILENAME_LEN 100 | |
57 | +#define TEMPUNZIP "ConvertDDTimeToDoubleTemp.nc\0" | |
58 | + | |
59 | +main(int argc, char **argv) | |
60 | +{ | |
61 | + static char usage[] = "usage: ConvertDDTimeToDouble nc_data_file[*.gz]"; | |
62 | + | |
63 | + if (argc < 2) {fprintf(stderr,"%s\n",usage); exit(1); } | |
64 | + | |
65 | + char inputFileName[MAX_FILENAME_LEN+1]; | |
66 | + memset(inputFileName, 0, (MAX_FILENAME_LEN+1) * sizeof(char)); | |
67 | + | |
68 | + char workingFileName[] = "ConvertDDTimeToDouble.nc"; | |
69 | + | |
70 | + char command[COMMAND_MAX_LEN+1]; | |
71 | + memset(command, 0, (COMMAND_MAX_LEN+1) * sizeof(char)); | |
72 | + | |
73 | + int i, j; | |
74 | + int zippedFile = 0; | |
75 | + | |
76 | + int status = NC_NOERR; | |
77 | + int ncID = 0; | |
78 | + int timeVarID = 0; | |
79 | + int newTimeVarID = 0; | |
80 | + nc_type timeType; | |
81 | + | |
82 | + | |
83 | + char TimeName[] = "Time"; | |
84 | + char TimeNameTmp[] = "TimeTmp"; | |
85 | + | |
86 | + int TimeDims[1]; | |
87 | + int ndims; | |
88 | + int dims[NC_MAX_DIMS]; | |
89 | + char dimName[NC_MAX_NAME]; | |
90 | + size_t nbRec; | |
91 | + | |
92 | + size_t DDTimeStart[2] = {0,0}; | |
93 | + size_t DDTimeCount[2] = {1, TIMELENGTH}; | |
94 | + char DDTimeValue[TIMELENGTH]; | |
95 | + | |
96 | + size_t TimeStampStart[1] = {0}; | |
97 | + size_t TimeStampCount[1] = {1}; | |
98 | + double TimeStampValue = 0.; | |
99 | + | |
100 | + for (i = 1; i < argc; ++i) { | |
101 | + if (strlen(argv[i]) > MAX_FILENAME_LEN) | |
102 | + { | |
103 | + fprintf(stderr,"File name size exceed the max size : %s\n",argv[i]); | |
104 | + continue; | |
105 | + } | |
106 | + | |
107 | + strcpy(inputFileName,argv[i]); | |
108 | + | |
109 | + if((strlen(inputFileName) > 3) && (strcmp(inputFileName+strlen(inputFileName)-3,".gz") == 0)) // Zipped file | |
110 | + { | |
111 | + sprintf(command,"gunzip -c %s > %s",inputFileName,workingFileName); | |
112 | + zippedFile = 1; | |
113 | + } | |
114 | + else { | |
115 | + sprintf(command,"cp -p %s %s",inputFileName,workingFileName); | |
116 | + zippedFile = 0; | |
117 | + } | |
118 | + | |
119 | + status = system(command); | |
120 | + if (status != 0) { | |
121 | + fprintf(stderr,"Cannot create working file for %s\n",argv[i]); | |
122 | + continue; | |
123 | + } | |
124 | + | |
125 | + status = nc_open(workingFileName,NC_WRITE,&ncID); | |
126 | + if (status != NC_NOERR) { | |
127 | + fprintf(stderr,"Cannot open data file : %s\n", inputFileName); | |
128 | + continue; | |
129 | + } | |
130 | + | |
131 | + status = nc_inq_varid(ncID, TimeName, &timeVarID); | |
132 | + if (status != NC_NOERR) { | |
133 | + status = nc_close(ncID); | |
134 | + fprintf(stderr,"Cannot retrieve time var. : %s\n", inputFileName); | |
135 | + continue; | |
136 | + } | |
137 | + | |
138 | + status = nc_inq_var (ncID, timeVarID, NULL, &timeType, &ndims, dims, NULL); | |
139 | + if (status != NC_NOERR) { | |
140 | + status = nc_close(ncID); | |
141 | + fprintf(stderr,"Cannot get time var. type : %s\n", inputFileName); | |
142 | + continue; | |
143 | + } | |
144 | + | |
145 | + if (timeType == NC_DOUBLE) { | |
146 | + //Time is already in double format | |
147 | + status = nc_close(ncID); | |
148 | + continue; | |
149 | + } | |
150 | + else if (timeType != NC_CHAR) { | |
151 | + status = nc_close(ncID); | |
152 | + fprintf(stderr,"Unknown type for time var : %s\n", inputFileName); | |
153 | + continue; | |
154 | + } | |
155 | + | |
156 | + //Put file into define mode | |
157 | + status = nc_redef(ncID); | |
158 | + if (status != NC_NOERR) { | |
159 | + status = nc_close(ncID); | |
160 | + fprintf(stderr,"Cannot put file into define mode : %s\n", inputFileName); | |
161 | + continue; | |
162 | + } | |
163 | + | |
164 | + //Rename old Time var | |
165 | + status = nc_rename_var(ncID, timeVarID, TimeNameTmp); | |
166 | + if (status != NC_NOERR) { | |
167 | + status = nc_close(ncID); | |
168 | + fprintf(stderr,"Cannot rename time var : %s\n", inputFileName); | |
169 | + continue; | |
170 | + } | |
171 | + | |
172 | + //Create new Time var | |
173 | + ndims = 1; | |
174 | + status = nc_def_var(ncID, TimeName, NC_DOUBLE, ndims, dims, &newTimeVarID); | |
175 | + if (status != NC_NOERR) { | |
176 | + status = nc_close(ncID); | |
177 | + fprintf(stderr,"Cannot create new time var : %s\n", inputFileName); | |
178 | + continue; | |
179 | + } | |
180 | + | |
181 | + //Out file in define mode | |
182 | + status = nc_enddef(ncID); | |
183 | + if (status != NC_NOERR) { | |
184 | + status = nc_close(ncID); | |
185 | + fprintf(stderr,"Cannot out file in define mode : %s\n", inputFileName); | |
186 | + continue; | |
187 | + } | |
188 | + | |
189 | + //Get number of records | |
190 | + status = nc_inq_dim(ncID, dims[0], dimName, &nbRec); | |
191 | + if (status != NC_NOERR) { | |
192 | + status = nc_close(ncID); | |
193 | + fprintf(stderr,"Cannot get info about Time dim : %s\n", inputFileName); | |
194 | + continue; | |
195 | + } | |
196 | + | |
197 | + for (j = 0; j < nbRec; ++j) { | |
198 | + DDTimeStart[0] = j; | |
199 | + status = nc_get_vara_text(ncID, timeVarID, DDTimeStart, DDTimeCount, (char *)DDTimeValue); | |
200 | + if (status != NC_NOERR) { | |
201 | + fprintf(stderr,"Cannot get a Time value : %s\n", inputFileName); | |
202 | + break; | |
203 | + } | |
204 | + TimeStampStart[0] = j; | |
205 | + TimeStampValue = DD_Time2Double(DDTimeValue); | |
206 | + status = nc_put_vara_double(ncID, newTimeVarID, TimeStampStart, TimeStampCount, &TimeStampValue); | |
207 | + } | |
208 | + | |
209 | + status = nc_close(ncID); | |
210 | + | |
211 | + if (status == NC_NOERR) { | |
212 | + //Post process | |
213 | + //Remove old time variable | |
214 | + sprintf(command,"/opt/local/bin/ncks -h -O -x -v %s %s %s",TimeNameTmp,workingFileName,workingFileName); | |
215 | + status = system(command); | |
216 | + if (status != 0) { | |
217 | + fprintf(stderr,"Cannot remove old DDTime variable : %s\n", inputFileName); | |
218 | + } | |
219 | + //Replace old file by new one | |
220 | + | |
221 | + } | |
222 | + } | |
223 | + | |
224 | + return(0); | |
225 | +} | |
226 | +/*=========================================================================================================*/ | ... | ... |
src/SERVER/DD_GetData.c
... | ... | @@ -1241,6 +1241,7 @@ int SetTime(DD_Var_t *DD_VarL, double VCTime) |
1241 | 1241 | More = 1; |
1242 | 1242 | Rmin = 0; Rmax = DD_VarL->Maxnc_rec - 1; |
1243 | 1243 | |
1244 | + int TimeIsDouble = Time_IsDoubleVar(DD_VarL->ncID, TimeID); | |
1244 | 1245 | do |
1245 | 1246 | { |
1246 | 1247 | start[0] = (Rmin + Rmax)/2; |
... | ... | @@ -1248,8 +1249,7 @@ int SetTime(DD_Var_t *DD_VarL, double VCTime) |
1248 | 1249 | if(start[0] > Rmax) { DD_VarL->nc_rec = Rmax; More = 0;} |
1249 | 1250 | if(More > 0) |
1250 | 1251 | { |
1251 | - status = nc_get_vara_text(DD_VarL->ncID, TimeID,start,TimeCount,StartTime); | |
1252 | - TestTime = DD_Time2Double(StartTime); | |
1252 | + TestTime = Time_GetValueFromVar(DD_VarL->ncID, TimeID, TimeIsDouble, start[0]); | |
1253 | 1253 | |
1254 | 1254 | if(CTime >= TestTime) |
1255 | 1255 | { |
... | ... | @@ -1265,8 +1265,7 @@ int SetTime(DD_Var_t *DD_VarL, double VCTime) |
1265 | 1265 | return 1; |
1266 | 1266 | } |
1267 | 1267 | start[0]++; |
1268 | - status = nc_get_vara_text(DD_VarL->ncID, TimeID,start,TimeCount,StartTime); | |
1269 | - TestTime = DD_Time2Double(StartTime); | |
1268 | + TestTime = Time_GetValueFromVar(DD_VarL->ncID, TimeID, TimeIsDouble, start[0]); | |
1270 | 1269 | if(CTime <= TestTime) |
1271 | 1270 | { |
1272 | 1271 | DD_VarL->nc_rec = start[0]; |
... | ... | @@ -1289,8 +1288,7 @@ int SetTime(DD_Var_t *DD_VarL, double VCTime) |
1289 | 1288 | else |
1290 | 1289 | { |
1291 | 1290 | start[0]--; |
1292 | - status = nc_get_vara_text(DD_VarL->ncID, TimeID,start,TimeCount,StartTime); | |
1293 | - TestTime = DD_Time2Double(StartTime); | |
1291 | + TestTime = Time_GetValueFromVar(DD_VarL->ncID, TimeID, TimeIsDouble, start[0]); | |
1294 | 1292 | if(CTime >= TestTime) |
1295 | 1293 | { |
1296 | 1294 | DD_VarL->nc_rec = start[0] +1; | ... | ... |