int readStdLine(Project* project, char *line, DateTime day1, DateTime day2) // // Input: line = line of data from a standard rainfall data file // day1 = starting day of record of interest // day2 = ending day of record of interest // Output: returns -1 if past end of desired record, 0 if data line could // not be read successfully or 1 if line read successfully // Purpose: reads a line of data from a standard rainfall data file and // writes its data to the rain interface file. // { DateTime date1; DateTime date2; int year, month, day, hour, minute; float x; // --- parse data from input line if (!parseStdLine(project,line, &year, &month, &day, &hour, &minute, &x)) return 0; // --- see if date is within period of record requested date1 = datetime_encodeDate(year, month, day); if ( day1 != NO_DATE && date1 < day1 ) return 0; if ( day2 != NO_DATE && date1 > day2 ) return -1; // --- see if record is out of sequence date2 = date1 + datetime_encodeTime(hour, minute, 0); if ( date2 <= project->PreviousDate ) { report_writeErrorMsg(project,ERR_RAIN_FILE_SEQUENCE, project->Gage[project->GageIndex].fname); //(5.1.010) report_writeLine(project,line); return -1; } project->PreviousDate = date2; switch (project->RainType) { case RAINFALL_INTENSITY: x = x * project->Interval / 3600.0f; break; case CUMULATIVE_RAINFALL: if ( x >= project->RainAccum ) { x = x - project->RainAccum; project->RainAccum += x; } else project->RainAccum = x; break; } x *= (float)project->UnitsFactor; // --- save rainfall to binary interface file saveRainfall(project, date1, hour, minute, x, FALSE); return 1; }
int readStdLine(char *line, DateTime day1, DateTime day2) // // Input: line = line of data from a standard rainfall data file // day1 = starting day of record of interest // day2 = ending day of record of interest // Output: returns -1 if past end of desired record, 0 if data line could // not be read successfully or 1 if line read successfully // Purpose: reads a line of data from a standard rainfall data file and // writes its data to the rain interface file. // { DateTime date1; int year, month, day, hour, minute; float x; // --- parse data from input line if (!parseStdLine(line, &year, &month, &day, &hour, &minute, &x)) return 0; // --- see if date is within period of record requested date1 = datetime_encodeDate(year, month, day); if ( day1 != NO_DATE && date1 < day1 ) return 0; if ( day2 != NO_DATE && date1 > day2 ) return -1; switch (RainType) { case RAINFALL_INTENSITY: x = x * Interval / 3600.0; break; case CUMULATIVE_RAINFALL: if ( x >= RainAccum ) { x = x - RainAccum; RainAccum += x; } else RainAccum = x; break; } x *= UnitsFactor; // --- save rainfall to binary interface file saveRainfall(date1, hour, minute, x, FALSE); return 1; }
int findFileFormat(Project* project, FILE *f, int i, int *hdrLines) // // Input: f = ptr. to rain gage's rainfall data file // i = rain gage index // Output: hdrLines = number of header lines found in data file; // returns type of format used in a rainfall data file // Purpose: finds the format of a gage's rainfall data file. // { int fileFormat; int lineCount; int maxCount = 5; int n; int div; long sn2; char recdType[4]; char elemType[5]; char coopID[6]; char line[MAXLINE]; int year, month, day, hour, minute; int elem; float x; // --- check first few lines for known formats fileFormat = UNKNOWN_FORMAT; project->hasStationName = FALSE; project->UnitsFactor = 1.0; project->Interval = 0; *hdrLines = 0; for (lineCount = 1; lineCount <= maxCount; lineCount++) { if ( fgets(line, MAXLINE, f) == NULL ) return fileFormat; // --- check for NWS space delimited format n = sscanf(line, "%6ld %2d %4s", &sn2, &div, elemType); if ( n == 3 ) { project->Interval = getNWSInterval(elemType); project->TimeOffset = project->Interval; if ( project->Interval > 0 ) { fileFormat = NWS_SPACE_DELIMITED; break; } } // --- check for NWS space delimited format w/ station name n = sscanf(&line[37], "%2d %4s %2s %4d", &div, elemType, recdType, &year); if ( n == 4 ) { project->Interval = getNWSInterval(elemType); project->TimeOffset = project->Interval; if ( project->Interval > 0 ) { fileFormat = NWS_SPACE_DELIMITED; project->hasStationName = TRUE; break; } } // --- check for NWS coma delimited format n = sscanf(line, "%6ld,%2d,%4s", &sn2, &div, elemType); if ( n == 3 ) { project->Interval = getNWSInterval(elemType); project->TimeOffset = project->Interval; if ( project->Interval > 0 ) { fileFormat = NWS_COMMA_DELIMITED; break; } } // --- check for NWS comma delimited format w/ station name n = sscanf(&line[37], "%2d,%4s,%2s,%4d", &div, elemType, recdType, &year); if ( n == 4 ) { project->Interval = getNWSInterval(elemType); project->TimeOffset = project->Interval; if ( project->Interval > 0 ) { fileFormat = NWS_COMMA_DELIMITED; project->hasStationName = TRUE; break; } } // --- check for NWS TAPE format n = sscanf(line, "%3s%6ld%2d%4s", recdType, &sn2, &div, elemType); if ( n == 4 ) { project->Interval = getNWSInterval(elemType); project->TimeOffset = project->Interval; if ( project->Interval > 0 ) { fileFormat = NWS_TAPE; break; } } // --- check for NWS Online Retrieval format n = sscanf(line, "%5s%6ld", coopID, &sn2); if ( n == 2 && strcmp(coopID, "COOP:") == 0 ) { fileFormat = findNWSOnlineFormat(project,f, line); break; } // --- check for AES type n = sscanf(line, "%7ld%3d%2d%2d%3d", &sn2, &year, &month, &day, &elem); if ( n == 5 ) { if ( elem == 123 && strlen(line) >= 185 ) { fileFormat = AES_HLY; project->Interval = 3600; project->TimeOffset = project->Interval; project->UnitsFactor = 1.0/MMperINCH; break; } } // --- check for CMC types n = sscanf(line, "%7ld%4d%2d%2d%3d", &sn2, &year, &month, &day, &elem); if ( n == 5 ) { if ( elem == 159 && strlen(line) >= 691 ) { fileFormat = CMC_FIF; project->Interval = 900; } else if ( elem == 123 && strlen(line) >= 186 ) { fileFormat = CMC_HLY; project->Interval = 3600; } if ( fileFormat == CMC_FIF || fileFormat == CMC_HLY ) { project->TimeOffset = project->Interval; project->UnitsFactor = 1.0/MMperINCH; break; } } // --- check for standard format if ( parseStdLine(project,line, &year, &month, &day, &hour, &minute, &x) ) { fileFormat = STD_SPACE_DELIMITED; project->RainType = project->Gage[i].rainType; project->Interval = project->Gage[i].rainInterval; if ( project->Gage[i].rainUnits == SI ) project->UnitsFactor = 1.0/MMperINCH; project->TimeOffset = 0; project->StationID = project->Gage[i].staID; break; } (*hdrLines)++; } if ( fileFormat != UNKNOWN_FORMAT ) project->Gage[i].rainInterval = project->Interval; return fileFormat; }
int findFileFormat(FILE *f, int i, int *hdrLines) // // Input: f = ptr. to rain gage's rainfall data file // i = rain gage index // Output: hdrLines = number of header lines found in data file; // returns type of format used in a rainfall data file // Purpose: finds the format of a gage's rainfall data file. // { int fileFormat; int lineCount; int maxCount = 5; int n; int div; long sn2; char recdType[4]; char elemType[4]; char line[MAXLINE]; int year, month, day, hour, minute; int elem; float x; // --- check first few lines for known formats fileFormat = UNKNOWN_FORMAT; UnitsFactor = 1.0; Interval = 0; *hdrLines = 0; for (lineCount = 1; lineCount <= maxCount; lineCount++) { if ( fgets(line, MAXLINE, f) == NULL ) return fileFormat; // --- check for NWS space delimited format n = sscanf(line, "%6d %2d %4s", &sn2, &div, elemType); if ( n == 3 ) { Interval = getNWSInterval(elemType); TimeOffset = Interval; if ( Interval > 0 ) { fileFormat = NWS_SPACE_DELIMITED; break; } } // --- check for NWS coma delimited format n = sscanf(line, "%6d,%2d,%4s", &sn2, &div, elemType); if ( n == 3 ) { Interval = getNWSInterval(elemType); TimeOffset = Interval; if ( Interval > 0 ) { fileFormat = NWS_COMMA_DELIMITED; break; } } // --- check for NWS TAPE format n = sscanf(line, "%3s%6d%2d%4s", recdType, &sn2, &div, elemType); if ( n == 4 ) { Interval = getNWSInterval(elemType); TimeOffset = Interval; if ( Interval > 0 ) { fileFormat = NWS_TAPE; break; } } // --- check for AES type n = sscanf(line, "%7d%3d%2d%2d%3d", &sn2, &year, &month, &day, &elem); if ( n == 5 ) { if ( elem == 123 && strlen(line) >= 185 ) { fileFormat = AES_HLY; Interval = 3600; TimeOffset = Interval; UnitsFactor = 1.0/MMperINCH; break; } } // --- check for CMC types n = sscanf(line, "%7d%4d%2d%2d%3d", &sn2, &year, &month, &day, &elem); if ( n == 5 ) { if ( elem == 159 && strlen(line) >= 691 ) { fileFormat = CMC_FIF; Interval = 900; } else if ( elem == 123 && strlen(line) >= 186 ) { fileFormat = CMC_HLY; Interval = 3600; } if ( fileFormat == CMC_FIF || fileFormat == CMC_HLY ) { TimeOffset = Interval; UnitsFactor = 1.0/MMperINCH; break; } } // --- check for standard format if ( parseStdLine(line, &year, &month, &day, &hour, &minute, &x) ) { fileFormat = STD_SPACE_DELIMITED; RainType = Gage[i].rainType; Interval = Gage[i].rainInterval; if ( Gage[i].rainUnits == SI ) UnitsFactor = 1.0/MMperINCH; TimeOffset = 0; //////////////////////////////////// //// New line added. (LR - 7/5/06 ) //////////////////////////////////// StationID = Gage[i].staID; break; } (*hdrLines)++; } if ( fileFormat != UNKNOWN_FORMAT ) Gage[i].rainInterval = Interval; return fileFormat; }