Пример #1
0
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;
}
Пример #2
0
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;
}