Commit 4b95fb97ec18f2fb6c67618e85c2240997b8cbd9

Authored by Benjamin Renard
1 parent b1aa926a

Add TimesUpdate & TimesUpdateNoData

CMakeLists.txt
... ... @@ -45,6 +45,8 @@ add_subdirectory(src/DECODERS/themis/esafull2nc)
45 45 add_subdirectory(src/DECODERS/themis/esamom2nc)
46 46 add_subdirectory(src/DECODERS/themis/fgm2nc)
47 47 add_subdirectory(src/DECODERS/themis/sst2nc)
  48 +add_subdirectory(src/TIMESUPDATE)
  49 +add_subdirectory(src/TIMESUPDATENODATA)
48 50 add_subdirectory(tests)
49 51  
50 52 install(FILES "scripts/StartServer" DESTINATION . PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
... ...
scripts/StartServer.in
... ... @@ -6,7 +6,7 @@ DDBASE=/home/budnik/AMDA-NG.core/DDBASE/DATA
6 6 DDPATH=/home/budnik/AMDA-NG.core/DDBASE
7 7 DDLIB=@CMAKE_INSTALL_PREFIX@/lib
8 8 DDBASEBIN=@CMAKE_INSTALL_PREFIX@/bin
9   -LD_LIBRARY_PATH=$DDLIB/:@NETCDFLIB_DIR@:@USRLIB_DIR@
  9 +LD_LIBRARY_PATH=$DDLIB/:@NETCDFLIB_DIR@:@libcdf_LIBRARIES@:@USRLIB_DIR@
10 10 export DDBASE DDPATH DDBASEBIN DDLIB LD_LIBRARY_PATH
11 11  
12 12 #$DDBASEBIN/DD_Server 1>/dev/null 2>/dev/null&
... ...
src/TIMESUPDATE/CMakeLists.txt 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +
  2 +PROJECT(TimesUpdate)
  3 +
  4 +include_directories(
  5 + ${CMAKE_HOME_DIRECTORY}/src/INCLUDE/
  6 + ${NETCDFINCLUDE_DIR}
  7 +)
  8 +
  9 +#Configuration de l'exécutable
  10 +file(
  11 + GLOB_RECURSE
  12 + source_files
  13 + ./*
  14 +)
  15 +
  16 +ADD_EXECUTABLE (TimesUpdate ${source_files} )
  17 +
  18 +target_link_libraries(
  19 + TimesUpdate
  20 + DD_Client
  21 + ${NETCDFLIBRARY}
  22 +)
  23 +
  24 +install (TARGETS TimesUpdate DESTINATION bin)
... ...
src/TIMESUPDATE/TimesUpdate.c 0 → 100755
... ... @@ -0,0 +1,431 @@
  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 +/* Global Constant definitions */
  52 +
  53 +#define TIME_LENGTH_NAME "TimeLength"
  54 +#define TIME_LENGTH 17L
  55 +#define REC_DIM_NAME "record"
  56 +#define REC_DIM NC_UNLIMITED
  57 +#define NAME_DIM_NAME "NameLength"
  58 +#define NAME_DIM 32L
  59 +#define NAME_LENGTH 100
  60 +
  61 +#define STARTTIME "StartTime"
  62 +#define STOPTIME "StopTime"
  63 +#define FILENAME "FileName"
  64 +
  65 +#define TIME_TYPE NC_CHAR
  66 +
  67 +#define TEMPUNZIP "TimesUpdateTemp.nc\0"
  68 +#define NODATA "NODATA\0"
  69 +
  70 +/*----------- Conditions --------------------------*/
  71 +enum Conditions {ALLLEFT,LEFTOVER,WELLIN,RIGHTOVER,ALLRIGHT,LEFTRIGHTOVER,LEFTIN};
  72 +
  73 +/*----------- Modes -------------------------------*/
  74 +enum Modes {REMAKE, INSERT, DELETE};
  75 +
  76 +/*----------- Type definition ------------------------------*/
  77 +typedef struct
  78 +{
  79 + char Name[NAME_LENGTH]; // current data file
  80 + char StartS[TIME_LENGTH]; // Start Time String
  81 + char StopS[TIME_LENGTH]; // Stop Time String
  82 + double StartD; // Start double
  83 + double StopD; // Stop double
  84 +} t_StartStop;
  85 +
  86 +/*---------- Global variables (IDs) of open times file -------------*/
  87 + int tmID; /* nc file descriptor */
  88 + int StartID, StopID, NameID;
  89 + size_t RecordsNumber = 0; // Records Number of the existing times file
  90 + static char ZeroTime[TIME_LENGTH] = "0000000000000000\0";
  91 + static int ToZipFlag = 0;
  92 + static char UnzipName[NAME_LENGTH];
  93 +
  94 +/*========================================================================
  95 + * FUNCTIONS
  96 + *=======================================================================*/
  97 +/*========================================================================
  98 + * GetFileInfo
  99 + *=======================================================================*/
  100 +t_StartStop *GetFileInfo(char *FileName)
  101 +{
  102 + static t_StartStop StartStop;
  103 + int dataID;
  104 + int DataStartID, DataStopID;
  105 + static size_t DataStart[1] = {0};
  106 + static size_t DataCount[1] = {TIME_LENGTH};
  107 + char command[200];
  108 + int Status;
  109 +
  110 + /*---- Check if this file exist ------------*/
  111 + if(strcmp(FileName+strlen(FileName)-3,".gz") == 0) // Zipped file
  112 + {
  113 + strcpy(UnzipName, TEMPUNZIP);
  114 + sprintf(command,"gunzip -c %s > %s",FileName,UnzipName);
  115 + system(command);
  116 + ToZipFlag = 0;
  117 + strncpy(StartStop.Name,FileName,strlen(FileName)-3);
  118 + } else
  119 + {
  120 + ToZipFlag = 1;
  121 + strcpy(UnzipName,FileName);
  122 + strcpy(StartStop.Name,FileName);
  123 + }
  124 +
  125 + if((Status = nc_open(UnzipName,NC_NOWRITE,&dataID)) == NC_NOERR)
  126 + {
  127 + Status = nc_inq_varid(dataID,STARTTIME,&DataStartID);
  128 + Status = nc_inq_varid(dataID,STOPTIME, &DataStopID);
  129 +
  130 + Status = nc_get_vara_text(dataID,DataStartID,DataStart,DataCount,(char *)StartStop.StartS);
  131 + Status = nc_get_vara_text(dataID,DataStopID,DataStart,DataCount,(char *)StartStop.StopS);
  132 + Status = nc_close(dataID);
  133 + }
  134 + else if(Status == 2)
  135 + {
  136 + fprintf(stderr,"File %s does not exist\n",UnzipName);
  137 + return NULL;
  138 + }
  139 + else
  140 + {
  141 + fprintf(stderr,"File %s is corrupted, Error = %d\n",UnzipName,Status);
  142 + sprintf(command,"rm %s*",StartStop.Name);
  143 + system(command);
  144 + return NULL;
  145 + }
  146 +
  147 + if((isdigit(StartStop.StartS[0]) == 0) || (isdigit(StartStop.StopS[0]) == 0))
  148 + {
  149 + fprintf(stderr,"File %s is corrupted\n",UnzipName);
  150 + sprintf(command,"rm %s*",StartStop.Name);
  151 + system(command);
  152 + return NULL;
  153 + }
  154 +
  155 + StartStop.StartD = DD_Time2Double(StartStop.StartS);
  156 + StartStop.StopD = DD_Time2Double(StartStop.StopS);
  157 +
  158 + return &StartStop;
  159 +}
  160 +/*-----------------------------------------------------------*/
  161 +
  162 +/*------------------------------------------------------------
  163 + * RemoveRecord()
  164 + *-----------------------------------------------------------*/
  165 +int RemoveRecord(size_t RecordPos, t_StartStop *StartStop)
  166 +{
  167 + static size_t TimeStart[2] = {0,0};
  168 + static size_t TimeCount[2] = {1,TIME_LENGTH};
  169 + static size_t NameCount[2] = {1, NAME_DIM};
  170 + char CStart[TIME_LENGTH],CStop[TIME_LENGTH], CName[NAME_DIM];
  171 + size_t Crec;
  172 + char command[200];
  173 + int status;
  174 + char NewName[NAME_LENGTH];
  175 +
  176 +/*------- Remove corresponding file -------------------*/
  177 + TimeStart[0] = RecordPos;
  178 + nc_get_vara_text(tmID,NameID,TimeStart,NameCount,(char *)CName);
  179 + if(StartStop == NULL) NewName[0] = '\0'; else strcpy(NewName,StartStop->Name);
  180 + if((strcmp(CName,NODATA) != 0)&&(strcmp(CName,NewName) != 0))
  181 + { // Remove old file which overlap with new one */
  182 + sprintf(command,"rm %s*",CName);
  183 + system(command);
  184 + }
  185 +
  186 +/*--- replacing Variable -----------------------*/
  187 + for(Crec = RecordPos + 1; Crec < RecordsNumber; Crec++)
  188 + {
  189 + TimeStart[0] = Crec;
  190 + status = nc_get_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)CStart);
  191 + status = nc_get_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)CStop);
  192 + status = nc_get_vara_text(tmID,NameID,TimeStart,NameCount,(char *)CName);
  193 + TimeStart[0]--;
  194 + status = nc_put_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)CStart);
  195 + status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)CStop);
  196 + status = nc_put_vara_text(tmID,NameID,TimeStart,NameCount,(char *)CName);
  197 + }
  198 +
  199 +/*----- Set Zero at the end -------------------------*/
  200 + TimeStart[0] = RecordsNumber - 1;
  201 + status = nc_put_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)ZeroTime);
  202 + status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)ZeroTime);
  203 +
  204 + RecordsNumber--;
  205 + nc_sync(tmID);
  206 + return 1;
  207 +}
  208 +/*-----------------------------------------------------------*/
  209 +
  210 +/*------------------------------------------------------------
  211 + * InsertRecord()
  212 + *-----------------------------------------------------------*/
  213 +int InsertRecord(size_t RecordPos, t_StartStop *StartStop)
  214 +{
  215 + static size_t TimeStart[2] = {0,0};
  216 + static size_t TimeCount[2] = {1,TIME_LENGTH};
  217 + static size_t NameCount[2] = {1, NAME_DIM};
  218 + char CStart[TIME_LENGTH],CStop[TIME_LENGTH], CName[NAME_DIM];
  219 + int Crec;
  220 + int status;
  221 +
  222 +/*--- Scrall all wariables down-----------------------*/
  223 + for(Crec = RecordsNumber - 1; Crec >= (int)RecordPos; Crec--)
  224 + {
  225 + TimeStart[0] = (size_t)Crec;
  226 + status = nc_get_vara_text (tmID,StartID,TimeStart,TimeCount,(char *)CStart);
  227 + status = nc_get_vara_text (tmID,StopID,TimeStart,TimeCount,(char *)CStop);
  228 + status = nc_get_vara_text (tmID,NameID,TimeStart,NameCount,(char *)CName);
  229 + TimeStart[0]++;
  230 + status = nc_put_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)CStart);
  231 + status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)CStop);
  232 + status = nc_put_vara_text(tmID,NameID,TimeStart,NameCount,(char *)CName);
  233 + }
  234 +
  235 +/*------ Put the new record to empty space -----------------*/
  236 + TimeStart[0] = RecordPos;
  237 + status = nc_put_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)StartStop->StartS);
  238 + status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)StartStop->StopS);
  239 + status = nc_put_vara_text(tmID,NameID,TimeStart,NameCount,(char *)StartStop->Name);
  240 +
  241 + RecordsNumber++;
  242 + nc_sync(tmID);
  243 + return 1;
  244 +}
  245 +/*==================================================================================*/
  246 +
  247 +/*==================================================================================
  248 + * MAIN
  249 + *==================================================================================*/
  250 +main(int argc, char **argv)
  251 +{
  252 + static char usage[] = "usage: TimesUpdate -r/-i[-u]/-d nc_times_file nc_data_file[*.gz] nc_data_file[*.gz]";
  253 +
  254 + char Start[TIME_LENGTH], Stop[TIME_LENGTH], Name[NAME_DIM];
  255 +
  256 +/* NC definitions */
  257 + int TlDimID,RecDimID, NlDimID; /* ID of dimensions */
  258 +
  259 + static size_t TimeStart[2] = {0,0};
  260 + static size_t TimeCount[2] = {1,TIME_LENGTH};
  261 + static size_t NameCount[2] = {1, NAME_DIM};
  262 +
  263 + int DimVector[2];
  264 +
  265 + char tmName[NAME_DIM];
  266 + int i, InsertFlag, FindFlag, ihole;
  267 +
  268 + char command[100];
  269 + char name[100];
  270 + int compress = 0;
  271 + int status;
  272 +
  273 + double LastTime = 0.0, FirstTime = 0.0; // Times of the hall between files
  274 +
  275 + t_StartStop *CurrentRecord; // pointer to the internal static structure
  276 + enum Modes CurrMode;
  277 + enum Conditions condition;
  278 +
  279 +/*=================================================================
  280 + * Check arguments and options
  281 + *=================================================================*/
  282 + if(argc < 4) {fprintf(stderr,"%s\n",usage); exit(1); }
  283 + if((strcmp(argv[1], "-r")) == 0) CurrMode = REMAKE;
  284 + else if((strcmp(argv[1], "-i")) == 0) CurrMode = INSERT;
  285 + else if((strcmp(argv[1], "-u")) == 0) CurrMode = INSERT;
  286 + else if((strcmp(argv[1], "-d")) == 0) CurrMode = DELETE;
  287 + else {fprintf(stderr,"Unknown option %s\n",argv[1]); exit(1);}
  288 +/*-----------------------------------------------------------------*/
  289 +
  290 +/*=================================================================
  291 + * Creating Times ncdf file if we have to make a new file
  292 + *=================================================================*/
  293 + switch(CurrMode)
  294 + {
  295 + case REMAKE: /* Create New File */
  296 + if((status = nc_create(argv[2],NC_SHARE,&tmID)) != NC_NOERR)
  297 + {
  298 + fprintf(stderr,"Can not create file %s\n",argv[2]);
  299 + exit(0);
  300 + }
  301 + status = nc_def_dim(tmID,TIME_LENGTH_NAME,TIME_LENGTH,&TlDimID);
  302 + status = nc_def_dim(tmID,NAME_DIM_NAME,NAME_DIM,&NlDimID);
  303 + status = nc_def_dim(tmID,REC_DIM_NAME,REC_DIM,&RecDimID);
  304 +
  305 + DimVector[0] = RecDimID;
  306 + DimVector[1] = TlDimID;
  307 + status = nc_def_var(tmID, STARTTIME,TIME_TYPE,2,DimVector,&StartID);
  308 + status = nc_def_var(tmID, STOPTIME,TIME_TYPE,2,DimVector,&StopID);
  309 + DimVector[1] = NlDimID;
  310 + status = nc_def_var(tmID, FILENAME,TIME_TYPE,2,DimVector,&NameID);
  311 + RecordsNumber = 0;
  312 + TimeStart[0] = (size_t)RecordsNumber;
  313 + status = nc_enddef(tmID);
  314 + break;
  315 +/*====== Or just open existing file ===================*/
  316 + case INSERT:
  317 + case DELETE: /* Open existing file */
  318 + if((status = nc_open(argv[2],NC_WRITE|NC_SHARE,&tmID)) != NC_NOERR)
  319 + {
  320 + fprintf(stderr,"Can not open file %s to write\n",argv[2]);
  321 + exit(0);
  322 + }
  323 + status = nc_inq_dimid(tmID,REC_DIM_NAME,&RecDimID);
  324 + status = nc_inq_varid(tmID, STARTTIME,&StartID);
  325 + status = nc_inq_varid(tmID, STOPTIME,&StopID);
  326 + status = nc_inq_varid(tmID, FILENAME,&NameID);
  327 + status = nc_inq_dimlen(tmID,RecDimID, &RecordsNumber);
  328 + break;
  329 + }
  330 +/*==========================================================================*/
  331 +
  332 + /*==========================================================================
  333 + * For Each file in the argument list
  334 + *=========================================================================*/
  335 + for(i = 3; i < argc; i++)
  336 + {
  337 + switch(CurrMode)
  338 + {
  339 + case DELETE:
  340 + TimeStart[0] = 0;
  341 + FindFlag = 0;
  342 + while((FindFlag == 0) && (TimeStart[0] < RecordsNumber))
  343 + {
  344 + status = nc_get_vara_text(tmID,NameID,TimeStart,NameCount,(char *)tmName);
  345 +
  346 + if(strncmp(tmName, argv[i], strlen(tmName)) == 0)
  347 + { /* Record is found */
  348 + FindFlag = 1;
  349 + RemoveRecord((size_t)TimeStart[0],(t_StartStop *)NULL);
  350 + }
  351 + else TimeStart[0]++;
  352 + }
  353 + break;
  354 + case REMAKE:
  355 + case INSERT:
  356 + if((CurrentRecord = GetFileInfo(argv[i])) != NULL) /* Read current file information */
  357 + {
  358 + /*----------------- The very first file in VI DIR -------------------*/
  359 + if (RecordsNumber == 0)
  360 + {
  361 + TimeStart[0] = (size_t)0;
  362 + status = nc_put_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)CurrentRecord->StartS);
  363 + status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)CurrentRecord->StopS);
  364 + status = nc_put_vara_text(tmID,NameID,TimeStart,NameCount,(char *)CurrentRecord->Name);
  365 + RecordsNumber++;
  366 + } else
  367 + {
  368 + /*---------------------------------------------------------
  369 + * Searchig the same file or a hole to insert
  370 + *-------------------------------------------------------*/
  371 + InsertFlag = 0;
  372 + ihole = 0;
  373 + while((InsertFlag == 0) && (ihole <= RecordsNumber))
  374 + {
  375 + if(ihole > 0) /* Get the Left Time of the hole */
  376 + {
  377 + TimeStart[0] = (size_t)(ihole-1);
  378 + status = nc_get_vara_text (tmID,StopID,TimeStart,TimeCount,(char *)Stop);
  379 + FirstTime = DD_Time2Double(Stop);
  380 + } else FirstTime = 0.0;
  381 +
  382 + if(ihole < RecordsNumber) /* Get the Right Time of the hole */
  383 + {
  384 + TimeStart[0] = (size_t)ihole;
  385 + status = nc_get_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)Start);
  386 + LastTime = DD_Time2Double(Start);
  387 + }
  388 + else LastTime = 1.0e16;
  389 +
  390 + /*--------- Look the conditions ---------------*/
  391 + condition = ALLRIGHT;
  392 + if((CurrentRecord->StartD < FirstTime - 60.0) &&
  393 + (CurrentRecord->StopD > LastTime + 60.0)) condition = LEFTRIGHTOVER;
  394 + else if((CurrentRecord->StartD < FirstTime - 60.0) &&
  395 + (CurrentRecord->StopD < FirstTime + 60.0)) condition = ALLLEFT;
  396 + else if((CurrentRecord->StartD < FirstTime - 60.0) &&
  397 + (CurrentRecord->StopD > FirstTime) &&
  398 + (CurrentRecord->StopD < LastTime + 60.0)) condition = LEFTOVER;
  399 + else if((CurrentRecord->StartD >= FirstTime - 60.0) &&
  400 + (CurrentRecord->StopD <= LastTime + 60.0)) condition = WELLIN;
  401 + else if((CurrentRecord->StartD >= FirstTime - 60.0) &&
  402 + (CurrentRecord->StartD < LastTime) &&
  403 + (CurrentRecord->StopD > LastTime + 60.0))condition = RIGHTOVER;
  404 + else if((CurrentRecord->StartD > LastTime - 60.0) && (CurrentRecord->StopD > LastTime + 60.0))
  405 + condition = ALLRIGHT;
  406 +
  407 + /* What to do with these conditions ----------------*/
  408 + switch(condition)
  409 + {
  410 + case WELLIN : InsertRecord((size_t)ihole, CurrentRecord); InsertFlag = 1; break;
  411 + case ALLLEFT : RemoveRecord((size_t)(ihole-1),CurrentRecord); ihole--; break;
  412 + case ALLRIGHT : ihole++; break;
  413 + case LEFTOVER : RemoveRecord((size_t)(ihole-1),CurrentRecord); ihole--; break;
  414 + case RIGHTOVER : RemoveRecord((size_t)ihole,CurrentRecord); break;
  415 + case LEFTRIGHTOVER : RemoveRecord((size_t)(ihole-1),CurrentRecord);
  416 + RemoveRecord((size_t)(ihole-1),CurrentRecord);
  417 + ihole--; break;
  418 + }
  419 + } // While InsertFlag
  420 + } // if RecordsNumber > 0
  421 + /*---------- Zip or remove new nc file -----------------*/
  422 + if(ToZipFlag) sprintf(command,"gzip -f %s",CurrentRecord->Name);
  423 + else sprintf(command,"rm %s",UnzipName);
  424 + system(command);
  425 + } // If current argument file is OK
  426 + } // CASE
  427 + } // For several arguments
  428 + status = nc_close(tmID);
  429 + return(0);
  430 +}
  431 +/*=========================================================================================================*/
... ...
src/TIMESUPDATENODATA/CMakeLists.txt 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +
  2 +PROJECT(TimesUpdateNoData)
  3 +
  4 +include_directories(
  5 + ${CMAKE_HOME_DIRECTORY}/src/INCLUDE/
  6 + ${NETCDFINCLUDE_DIR}
  7 +)
  8 +
  9 +#Configuration de l'exécutable
  10 +file(
  11 + GLOB_RECURSE
  12 + source_files
  13 + ./*
  14 +)
  15 +
  16 +ADD_EXECUTABLE (TimesUpdateNoData ${source_files} )
  17 +
  18 +target_link_libraries(
  19 + TimesUpdateNoData
  20 + DD_Client
  21 + ${NETCDFLIBRARY}
  22 +)
  23 +
  24 +install (TARGETS TimesUpdateNoData DESTINATION bin)
... ...
src/TIMESUPDATENODATA/TimesUpdateNoData.c 0 → 100644
... ... @@ -0,0 +1,330 @@
  1 +/**
  2 +* @file TimesUpdateNoData.c
  3 +* @version $Id: TimesUpdateNoData.c,v 1.6 2009/07/07 11:37:40 budnik Exp $
  4 +* @brief Program reads StartTime and StopTime and add to nc_times_file NODATA intervals
  5 +*
  6 +* @arg nc_times_file OrderedStartTime OrderedStopTime
  7 +*/
  8 +
  9 +/*=====================================================================
  10 + * DD SYSTEM base package
  11 + * DD_Server library
  12 + * TimesUpdateNoData.c
  13 + * V.1.5
  14 + *
  15 + * usage TimesUpdateNoData nc_times_file OrderedStartTime OrderedStopTime
  16 + *
  17 + *=====================================================================*/
  18 + /*=====================================================================
  19 + * Description:
  20 + * Program reads StartTime and StopTime and add to nc_times_file NODATA intervals
  21 + *
  22 + *
  23 + *
  24 + * Dimensions:
  25 + * TimeLingth TIMELENGTH
  26 + * NameLength NAME_DIM
  27 + * Record Unlimited
  28 + * Variables:
  29 + *
  30 + * char StartTime[TIMELENGTH]
  31 + * char EndTime[TIMELENGTH]
  32 + *
  33 + * Attributes:
  34 + * none
  35 + *======================================================================*/
  36 +#include <stdio.h>
  37 +#include <stdlib.h>
  38 +#include <string.h>
  39 +#include <ctype.h>
  40 +#include <dirent.h>
  41 +#include <netcdf.h>
  42 +#include <math.h>
  43 +#include <DD.h>
  44 +
  45 +
  46 +/* Global Constant definitions */
  47 +#define ISOTIMER "%d-%d-%dT%d:%d:%d.%dZ" /* Format to READ time in ISO format */
  48 +#define TIME_LENGTH_NAME "TimeLength"
  49 +#define TIME_LENGTH 17L
  50 +#define REC_DIM_NAME "record"
  51 +#define REC_DIM NC_UNLIMITED
  52 +#define NAME_DIM_NAME "NameLength"
  53 +#define NAME_DIM 32L
  54 +#define NAME_LENGTH 100
  55 +#define ISO_LENGTH 25
  56 +
  57 +#define STARTTIME "StartTime"
  58 +#define STOPTIME "StopTime"
  59 +#define FILENAME "FileName"
  60 +
  61 +#define TIME_TYPE NC_CHAR
  62 +
  63 +
  64 +#define DATAGAP 7200
  65 +#define NODATA "NODATA\0"
  66 +
  67 +
  68 +/*----------- Type definition ------------------------------*/
  69 +typedef struct
  70 +{
  71 + char Name[NAME_LENGTH]; // current data file
  72 + char StartS[TIME_LENGTH]; // Start Time String
  73 + char StopS[TIME_LENGTH]; // Stop Time String
  74 +
  75 + double StartD; // Start double
  76 + double StopD; // Stop double
  77 +} t_StartStop;
  78 +
  79 +/*---------- Global variables (IDs) of open times file -------------*/
  80 + int tmID; /* nc file descriptor */
  81 + int StartID, StopID, NameID;
  82 + size_t RecordsNumber = 0; // Records Number of the existing times file
  83 + static char ZeroTime[TIME_LENGTH] = "0000000000000000\0";
  84 +
  85 +
  86 +/*========================================================================
  87 + * FUNCTIONS
  88 + *=======================================================================*/
  89 +/*=====================================================================
  90 + * ISOTime2Double()
  91 + * Covert double time to ISO time to Double
  92 + *
  93 + *=====================================================================*/
  94 +double ISOTime2Double(char* ISO)
  95 +{
  96 + unsigned UT[7];
  97 + dd_tmstr_t *Time;
  98 + sscanf(ISO, ISOTIMER, &UT[0], &UT[1], &UT[2], &UT[3], &UT[4], &UT[5], &UT[6]);
  99 + Time = (dd_tmstr_t *)UT2double(UT);
  100 + return Time->times;
  101 +}
  102 +
  103 +/*-----------------------------------------------------------*/
  104 +
  105 +/*------------------------------------------------------------
  106 + * InsertRecord()
  107 + *-----------------------------------------------------------*/
  108 +int InsertRecord(size_t RecordPos, t_StartStop *StartStop)
  109 +{
  110 + static size_t TimeStart[2] = {0,0};
  111 + static size_t TimeCount[2] = {1,TIME_LENGTH};
  112 + static size_t NameCount[2] = {1, NAME_DIM};
  113 + char CStart[TIME_LENGTH],CStop[TIME_LENGTH], CName[NAME_DIM];
  114 + int Crec;
  115 + int status;
  116 +
  117 +/*--- Scroll all variables down-----------------------*/
  118 + for(Crec = RecordsNumber - 1; Crec >= (int)RecordPos; Crec--)
  119 + {
  120 + TimeStart[0] = (size_t)Crec;
  121 + status = nc_get_vara_text (tmID,StartID,TimeStart,TimeCount,(char *)CStart);
  122 + status = nc_get_vara_text (tmID,StopID,TimeStart,TimeCount,(char *)CStop);
  123 + status = nc_get_vara_text (tmID,NameID,TimeStart,NameCount,(char *)CName);
  124 + TimeStart[0]++;
  125 + status = nc_put_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)CStart);
  126 + status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)CStop);
  127 + status = nc_put_vara_text(tmID,NameID,TimeStart,NameCount,(char *)CName);
  128 + }
  129 +
  130 +/*------ Put the new record to empty space -----------------*/
  131 + TimeStart[0] = RecordPos;
  132 + status = nc_put_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)StartStop->StartS);
  133 + status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)StartStop->StopS);
  134 + status = nc_put_vara_text(tmID,NameID,TimeStart,NameCount,(char *)StartStop->Name);
  135 +
  136 + RecordsNumber++;
  137 + nc_sync(tmID);
  138 + return 1;
  139 +}
  140 +/*==================================================================================*/
  141 +
  142 +/*==================================================================================
  143 + * MAIN
  144 + *==================================================================================*/
  145 +main(int argc, char **argv)
  146 +{
  147 + static char usage[] = "usage: TimesUpdateNoData nc_times_file StartTime StopTime";
  148 +
  149 + char Start[TIME_LENGTH], Stop[TIME_LENGTH], Name[NAME_DIM];
  150 +
  151 +/* NC definitions */
  152 + int TlDimID,RecDimID, NlDimID; /* ID of dimensions */
  153 +
  154 + static size_t TimeStart[2] = {0,0};
  155 + static size_t TimeCount[2] = {1,TIME_LENGTH};
  156 + static size_t NameCount[2] = {1, NAME_DIM};
  157 +
  158 + int DimVector[2];
  159 +
  160 + char tmName[NAME_DIM];
  161 + int InsertFlag;
  162 +
  163 + int i, hi, lo = 0;
  164 + double StartD0, StartD1 = 0.0, StopD0, StopD1, STARTD, STOPD, CurrentStart;
  165 + int StartRecord, StopRecord;
  166 +
  167 + char command[100];
  168 + char name[100];
  169 +
  170 + int status;
  171 +
  172 + double LastTime = 0.0, FirstTime = 0.0; // Times of the hole between files
  173 +
  174 + t_StartStop CurrentRecord; //
  175 +
  176 +
  177 +/*=================================================================
  178 + * Check arguments and options
  179 + *=================================================================*/
  180 + if(argc < 4) {fprintf(stderr,"%s\n",usage); exit(1); }
  181 +
  182 +/*-----------------------------------------------------------------*/
  183 +
  184 +
  185 + /* Open existing file */
  186 + if((status = nc_open(argv[1],NC_WRITE|NC_SHARE,&tmID)) != NC_NOERR)
  187 + {
  188 + fprintf(stderr,"Can not open file %s to write\n",argv[2]);
  189 + exit(0);
  190 + }
  191 + status = nc_inq_dimid(tmID,REC_DIM_NAME,&RecDimID);
  192 + status = nc_inq_varid(tmID, STARTTIME,&StartID);
  193 + status = nc_inq_varid(tmID, STOPTIME,&StopID);
  194 + status = nc_inq_varid(tmID, FILENAME,&NameID);
  195 + status = nc_inq_dimlen(tmID,RecDimID, &RecordsNumber);
  196 +
  197 +/*==========================================================================*/
  198 +
  199 + hi = RecordsNumber;
  200 +
  201 + STARTD = ISOTime2Double(argv[2]);
  202 + STOPD = ISOTime2Double(argv[3]);
  203 +
  204 + while (lo <= hi)
  205 + {
  206 + i = (lo+hi)/2;
  207 + TimeStart[0] = i + 1;
  208 + status = nc_get_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)Start);
  209 + StartD1 = DD_Time2Double(Start);
  210 + if (StartD1 > STARTD) hi--;
  211 + TimeStart[0] = i;
  212 + status = nc_get_vara_text(tmID,StartID,TimeStart,TimeCount,(char *)Start);
  213 + StartD0 = DD_Time2Double(Start);
  214 + if (StartD0 < STARTD) lo++;
  215 +
  216 + if (StartD1 <= STARTD && StartD0 >= STARTD) break;
  217 + }
  218 +
  219 + StartRecord = i;
  220 + status = nc_get_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)Stop);
  221 + StopD0 = DD_Time2Double(Stop);
  222 +
  223 + while (STOPD > StopD0 && TimeStart[0] <= RecordsNumber) {
  224 + status = nc_get_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)Stop);
  225 + if (status != NC_NOERR) fprintf(stderr," %s\n", nc_strerror(status));
  226 + StopD0 = DD_Time2Double(Stop);
  227 + TimeStart[0]++;
  228 + }
  229 +
  230 + StopRecord = TimeStart[0]-1;
  231 +
  232 +// if NODATA is the very FIRST - insert NODATA
  233 + if (StartRecord == 0 && (StartD0 - STARTD) > DATAGAP) {
  234 +
  235 + TimeStart[0] = 0;
  236 + status = nc_get_vara_text(tmID, NameID, TimeStart, NameCount,(char *)Name);
  237 + if (strcmp(Name,NODATA) == 0 && (StartD0 - STOPD) < DATAGAP) {
  238 + status = nc_put_vara_text(tmID,StartID, TimeStart, TimeCount, Double2DD_Time(STARTD));
  239 + }
  240 + else {
  241 + strncpy(CurrentRecord.Name, NODATA, 7);
  242 + strncpy(CurrentRecord.StartS,Double2DD_Time(STARTD),TIMELENGTH);
  243 + if ((StartD0 - STOPD) < DATAGAP) strncpy(CurrentRecord.StopS,Start,TIMELENGTH);
  244 + else strncpy(CurrentRecord.StopS,Double2DD_Time(STOPD),TIMELENGTH);
  245 + InsertRecord((size_t)(0), &CurrentRecord);
  246 + }
  247 + }
  248 +
  249 + for (i = StartRecord; i < StopRecord ; i++) {
  250 +
  251 + InsertFlag = 0;
  252 + TimeStart[0] = i;
  253 + status = nc_get_vara_text(tmID,StopID, TimeStart, TimeCount,(char *)Stop);
  254 + StopD0 = DD_Time2Double(Stop);
  255 + TimeStart[0] = i+1;
  256 + status = nc_get_vara_text(tmID,StartID, TimeStart, TimeCount, (char *)Start);
  257 + StartD1 = DD_Time2Double(Start);
  258 +
  259 +
  260 +// Data Gap -> NODATA
  261 +
  262 + if ((StartD1 - StopD0) > DATAGAP) {
  263 + strncpy(CurrentRecord.Name, NODATA,7);
  264 +
  265 + TimeStart[0] = i;
  266 + status = nc_get_vara_text(tmID, NameID, TimeStart, NameCount,(char *)Name);
  267 +
  268 + if (strcmp(Name,NODATA) == 0) {
  269 + strncpy(Stop,Start,TIMELENGTH);
  270 + status = nc_get_vara_text(tmID,StartID, TimeStart, TimeCount, (char *)Start);
  271 + status = nc_put_vara_text(tmID,StartID, TimeStart, TimeCount, (char *)Start);
  272 + if ((StartD1 - STOPD) < DATAGAP)
  273 + status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount,(char *)Stop);
  274 + else status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount,Double2DD_Time(STOPD));
  275 + status = nc_put_vara_text(tmID,NameID,TimeStart,NameCount,(char *)CurrentRecord.Name);
  276 +
  277 + InsertFlag = 1;
  278 + }
  279 +
  280 + if (!InsertFlag) {
  281 + if ((i == StartRecord) && (StopD0 < STARTD))
  282 + strncpy(CurrentRecord.StartS,Double2DD_Time(STARTD),TIMELENGTH);
  283 + else strncpy(CurrentRecord.StartS,Stop,TIMELENGTH);
  284 + if ((i == StopRecord-1) && (StartD1 > STOPD))
  285 + strncpy(CurrentRecord.StopS,Double2DD_Time(STOPD),TIMELENGTH);
  286 + else strncpy(CurrentRecord.StopS,Start,TIMELENGTH);
  287 +
  288 + if ((DD_Time2Double(CurrentRecord.StopS) -
  289 + DD_Time2Double(CurrentRecord.StartS)) > DATAGAP)
  290 + InsertRecord((size_t)(i+1), &CurrentRecord);
  291 +
  292 + }
  293 + }
  294 +
  295 + }
  296 +
  297 +// if NODATA is the very LAST - add NODATA
  298 +
  299 + if (StopRecord == RecordsNumber) {
  300 + TimeStart[0] = StopRecord-1;
  301 + status = nc_get_vara_text(tmID,StopID, TimeStart, TimeCount, (char *)Stop);
  302 + StopD0 = DD_Time2Double(Stop);
  303 +
  304 + if (STOPD > StopD0) {
  305 + status = nc_get_vara_text(tmID, NameID, TimeStart, NameCount,(char *)Name);
  306 +
  307 + if (strcmp(Name,NODATA) == 0 && (STARTD - StopD0) < DATAGAP) {
  308 + status = nc_get_vara_text(tmID,StartID, TimeStart, TimeCount, (char *)Start);
  309 + strncpy(Stop,Start,TIMELENGTH);
  310 + }
  311 + else {
  312 + strncpy(Name, NODATA, 7);
  313 + TimeStart[0]++;
  314 + }
  315 + if ((STARTD - StopD0) < DATAGAP) CurrentStart = DD_Time2Double(Stop);
  316 + else CurrentStart = STARTD;
  317 + if ((STOPD - CurrentStart) > DATAGAP) {
  318 + status = nc_put_vara_text(tmID,StartID,TimeStart,TimeCount, Double2DD_Time(CurrentStart));
  319 + status = nc_put_vara_text(tmID,StopID,TimeStart,TimeCount, Double2DD_Time(STOPD));
  320 + status = nc_put_vara_text(tmID,NameID,TimeStart,NameCount,(char *)Name);
  321 + }
  322 + }
  323 + }
  324 +
  325 +
  326 + nc_sync(tmID);
  327 + status = nc_close(tmID);
  328 + exit(0);
  329 +}
  330 +/*=========================================================================================================*/
... ...