/***************************************************************************** * _myWarn() (Private) -- Arthur Taylor / MDL * * PURPOSE * This prints a warning message of level "f_errCode" to the devices that * are allowed to receive those levels of warning messages. * * ARGUMENTS * f_errCode = 1=note, 2=warning, 3=error. (Input) * file = File of initial call to myWarn module (or NULL). (Input) * lineNum = Line number of inital call to myWarn module. (Input) * fmt = Format to define how to print the msg (Input) * ap = The arguments for the message. (Input) * * RETURNS: int * 0 ok * -1 vfprintf or fprintf had problems * -2 allocSprintf had problems * * HISTORY * 3/2007 Arthur Taylor (MDL): Created. * * NOTES ****************************************************************************/ static int _myWarn(uChar f_errCode, const char *file, int lineNum, const char *fmt, va_list ap) { int ierr = 0; /* Error return code */ if (fmt == NULL) { return ierr; } /* Check if the warnDetail level allows this message. */ if (warn.f_stdout && (warn.f_stdout <= f_errCode)) { if (file != NULL) { if (fprintf(stdout, "(%s line %d) ", file, lineNum) < 0) { ierr = -1; } } if (vfprintf(stdout, fmt, ap) < 0) { ierr = -1; } fflush(stdout); } if (warn.f_stderr && (warn.f_stderr <= f_errCode)) { if (file != NULL) { if (fprintf(stderr, "(%s line %d) ", file, lineNum) < 0) { ierr = -1; } } if (vfprintf(stderr, fmt, ap) < 0) { ierr = -1; } fflush(stderr); } if (warn.f_log && (warn.f_log <= f_errCode)) { if (file != NULL) { if (fprintf(warn.fp, "(%s line %d) ", file, lineNum) < 0) { ierr = -1; } } if (vfprintf(warn.fp, fmt, ap) < 0) { ierr = -1; } fflush(warn.fp); } if (warn.f_mem && (warn.f_mem <= f_errCode)) { if (file != NULL) { if (reallocSprintf(&warn.buff, "(%s line %d) ", file, lineNum) < 0) { ierr = -2; } } if (allocSprintf(&warn.buff, &warn.buffLen, fmt, ap) != 0) { ierr = -2; } } return ierr; }
/***************************************************************************** * GRIB2Inventory2to7() -- * * Arthur Taylor / MDL * * PURPOSE * Inventories sections 3 to 7, filling out the inv record with the data in * section 4. (Note: No Call to FORTRAN routines here). * * ARGUMENTS * sectNum = Which section we are currently reading. (Input) * fp = An opened file pointer to the file to the inventory of (In/Out) * gribLen = The total length of the grib message. (Input) * buffLen = length of buffer. (Input) * buffer = Holds a given section. (Input) * inv = The current inventory record to fill out. (Output) * prodType = The GRIB2 type of product: 0 is meteo product, 1 is hydro, * 2 is land, 3 is space, 10 is oceanographic. (Input) * center = Who produced it (Input) * subcenter = A sub group of center that actually produced it (Input) * * FILES/DATABASES: * * RETURNS: int (could use errSprintf()) * 0 = "Ok" * -5 = Problems Reading in section 2 or 3 * -6 = Problems Reading in section 3 * -7 = Problems Reading in section 4 * -8 = Problems Parsing section 4. * -9 = Problems Reading in section 5 * -10 = Problems Reading in section 6 * -11 = Problems Reading in section 7 * * HISTORY * 3/2003 Arthur Taylor (MDL/RSIS): Created. * 4/2003 AAT: Modified to not have prodType, cat, subcat, templat in * inventoryType structure. * 8/2003 AAT: curTot no longer serves a purpose. * 1/2004 AAT: Added center/subcenter. * * NOTES ***************************************************************************** */ static int GRIB2Inventory2to7 (sChar sectNum, DataSource &fp, sInt4 gribLen, uInt4 *buffLen, char **buffer, inventoryType *inv, uChar prodType, unsigned short int center, unsigned short int subcenter) { uInt4 secLen; /* The length of the current section. */ sInt4 foreTime; /* forecast time (NDFD treats as "projection") */ uChar foreTimeUnit; /* The time unit of the "forecast time". */ /* char *element; *//* Holds the name of the current variable. */ /* char *comment; *//* Holds more comments about current variable. */ /* char *unitName; *//* Holds the name of the unit [K] [%] .. etc */ int convert; /* Enum type of unit conversions (metaname.c), * Conversion method for this variable's unit. */ uChar cat; /* General category of Meteo Product. */ unsigned short int templat; /* The section 4 template number. */ uChar subcat; /* Specific subcategory of Product. */ uChar fstSurfType; /* Type of the first fixed surface. */ double fstSurfValue; /* Value of first fixed surface. */ sInt4 value; /* The scaled value from GRIB2 file. */ sChar factor; /* The scaled factor from GRIB2 file */ sChar scale; /* Surface scale as opposed to probability factor. */ uChar sndSurfType; /* Type of the second fixed surface. */ double sndSurfValue; /* Value of second fixed surface. */ sChar f_sndValue; /* flag if SndValue is valid. */ uChar timeRangeUnit; sInt4 lenTime; /* Used by parseTime to tell difference between 8hr * average and 1hr average ozone. */ uChar genID; /* The Generating process ID (used for GFS MOS) */ uChar probType; /* The probability type */ double lowerProb; /* The lower limit on probability forecast if * template 4.5 or 4.9 */ double upperProb; /* The upper limit on probability forecast if * template 4.5 or 4.9 */ uChar timeIncrType; sChar percentile = 0; if ((sectNum == 2) || (sectNum == 3)) { /* Jump past section (2 or 3). */ sectNum = -1; if (GRIB2SectJump (fp, gribLen, §Num, &secLen) != 0) { errSprintf ("ERROR: Problems Jumping past section 2 || 3\n"); return -6; } if ((sectNum != 2) && (sectNum != 3)) { errSprintf ("ERROR: Section 2 or 3 mislabeled\n"); return -5; } else if (sectNum == 2) { /* Jump past section 3. */ sectNum = 3; if (GRIB2SectJump (fp, gribLen, §Num, &secLen) != 0) { errSprintf ("ERROR: Problems Jumping past section 3\n"); return -6; } } } /* Read section 4 into buffer. */ sectNum = 4; if (GRIB2SectToBuffer (fp, gribLen, §Num, &secLen, buffLen, buffer) != 0) { errSprintf ("ERROR: Problems with section 4\n"); return -7; } /* enum { GS4_ANALYSIS, GS4_ENSEMBLE, GS4_DERIVED, GS4_PROBABIL_PNT = 5, GS4_STATISTIC = 8, GS4_PROBABIL_TIME = 9, GS4_PERCENTILE = 10, GS4_RADAR = 20, GS4_SATELLITE = 30 }; */ /* Parse the interesting data out of sect 4. */ MEMCPY_BIG (&templat, *buffer + 8 - 5, sizeof (short int)); if ((templat != GS4_ANALYSIS) && (templat != GS4_ENSEMBLE) && (templat != GS4_DERIVED) && (templat != GS4_PROBABIL_PNT) && (templat != GS4_STATISTIC) && (templat != GS4_PROBABIL_TIME) && (templat != GS4_PERCENTILE) && (templat != GS4_ENSEMBLE_STAT) && (templat != GS4_RADAR) && (templat != GS4_SATELLITE) && (templat != GS4_DERIVED_INTERVAL)) { errSprintf ("This was only designed for templates 0, 1, 2, 5, 8, 9, " "10, 11, 12, 20, 30\n"); return -8; } cat = (*buffer)[10 - 5]; subcat = (*buffer)[11 - 5]; genID = 0; probType = 0; lowerProb = 0; upperProb = 0; if ((templat == GS4_RADAR) || (templat == GS4_SATELLITE) || (templat == 254)) { inv->foreSec = 0; inv->validTime = inv->refTime; timeIncrType = 255; timeRangeUnit = 255; lenTime = 0; } else { genID = (*buffer)[14 - 5]; /* Compute forecast time. */ foreTimeUnit = (*buffer)[18 - 5]; MEMCPY_BIG (&foreTime, *buffer + 19 - 5, sizeof (sInt4)); if (ParseSect4Time2sec (foreTime, foreTimeUnit, &(inv->foreSec)) != 0) { errSprintf ("unable to convert TimeUnit: %d \n", foreTimeUnit); return -8; } /* Compute valid time. */ inv->validTime = inv->refTime + inv->foreSec; timeIncrType = 255; timeRangeUnit = 1; lenTime = (sInt4) (inv->foreSec / 3600); switch (templat) { case GS4_PROBABIL_PNT: /* 4.5 */ probType = (*buffer)[37 - 5]; factor = (sChar) (*buffer)[38 - 5]; MEMCPY_BIG (&value, *buffer + 39 - 5, sizeof (sInt4)); lowerProb = value * pow (10.0, -1 * factor); factor = (sChar) (*buffer)[43 - 5]; MEMCPY_BIG (&value, *buffer + 44 - 5, sizeof (sInt4)); upperProb = value * pow (10.0, -1 * factor); break; case GS4_DERIVED_INTERVAL: /* 4.12 */ if (InventoryParseTime (*buffer + 37 - 5, &(inv->validTime)) != 0) { printf ("Warning: Investigate Template 4.12 bytes 37-43\n"); inv->validTime = inv->refTime + inv->foreSec; } timeIncrType = (*buffer)[50 - 5]; timeRangeUnit = (*buffer)[51 - 5]; MEMCPY_BIG (&lenTime, *buffer + 52 - 5, sizeof (sInt4)); /* If lenTime == missing (2^32 -1) we might do something, but not with 255.*/ /* if (lenTime == 255) { lenTime = (inv->validTime - (inv->refTime + inv->foreSec)) / 3600; } */ break; case GS4_PERCENTILE: /* 4.10 */ percentile = (*buffer)[35 - 5]; if (InventoryParseTime (*buffer + 36 - 5, &(inv->validTime)) != 0) { printf ("Warning: Investigate Template 4.10 bytes 36-42\n"); inv->validTime = inv->refTime + inv->foreSec; } timeIncrType = (*buffer)[49 - 5]; timeRangeUnit = (*buffer)[50 - 5]; MEMCPY_BIG (&lenTime, *buffer + 51 - 5, sizeof (sInt4)); /* If lenTime == missing (2^32 -1) we might do something, but not with 255.*/ /* if (lenTime == 255) { lenTime = (inv->validTime - (inv->refTime + inv->foreSec)) / 3600; } */ break; case GS4_STATISTIC: /* 4.8 */ if (InventoryParseTime (*buffer + 35 - 5, &(inv->validTime)) != 0) { printf ("Warning: Investigate Template 4.8 bytes 35-41\n"); inv->validTime = inv->refTime + inv->foreSec; } timeIncrType = (*buffer)[48 - 5]; timeRangeUnit = (*buffer)[49 - 5]; MEMCPY_BIG (&lenTime, *buffer + 50 - 5, sizeof (sInt4)); /* If lenTime == missing (2^32 -1) we might do something, but not with 255.*/ /* if (lenTime == 255) { lenTime = (inv->validTime - (inv->refTime + inv->foreSec)) / 3600; } */ break; case GS4_ENSEMBLE_STAT: /* 4.11 */ if (InventoryParseTime (*buffer + 38 - 5, &(inv->validTime)) != 0) { printf ("Warning: Investigate Template 4.11 bytes 38-44\n"); inv->validTime = inv->refTime + inv->foreSec; } timeIncrType = (*buffer)[51 - 5]; timeRangeUnit = (*buffer)[52 - 5]; MEMCPY_BIG (&lenTime, *buffer + 53 - 5, sizeof (sInt4)); /* If lenTime == missing (2^32 -1) we might do something, but not with 255.*/ /* if (lenTime == 255) { lenTime = (inv->validTime - (inv->refTime + inv->foreSec)) / 3600; } */ break; case GS4_PROBABIL_TIME: /* 4.9 */ probType = (*buffer)[37 - 5]; if ((uChar) (*buffer)[38 - 5] > 128) { factor = 128 - (uChar) (*buffer)[38 - 5]; } else { factor = (*buffer)[38 - 5]; } MEMCPY_BIG (&value, *buffer + 39 - 5, sizeof (sInt4)); lowerProb = value * pow (10.0, -1 * factor); if ((uChar) (*buffer)[43 - 5] > 128) { factor = 128 - (uChar) (*buffer)[43 - 5]; } else { factor = (*buffer)[43 - 5]; } MEMCPY_BIG (&value, *buffer + 44 - 5, sizeof (sInt4)); upperProb = value * pow (10.0, -1 * factor); if (InventoryParseTime (*buffer + 48 - 5, &(inv->validTime)) != 0) { printf ("Warning: Investigate Template 4.9 bytes 48-54\n"); inv->validTime = inv->refTime + inv->foreSec; } timeIncrType = (*buffer)[61 - 5]; timeRangeUnit = (*buffer)[62 - 5]; MEMCPY_BIG (&lenTime, *buffer + 63 - 5, sizeof (sInt4)); /* If lenTime == missing (2^32 -1) we might do something, but not with 255.*/ /* if (lenTime == 255) { lenTime = (inv->validTime - (inv->refTime + inv->foreSec)) / 3600; } */ break; } } if (timeRangeUnit == 255) { timeRangeUnit = 1; lenTime = (sInt4) ((inv->validTime - inv->foreSec - inv->refTime) / 3600); } /* myAssert (timeRangeUnit == 1);*/ /* Try to convert lenTime to hourly. */ if (timeRangeUnit == 0) { lenTime = (sInt4) (lenTime / 60.); /*timeRangeUnit = 1;*/ } else if (timeRangeUnit == 1) { } else if (timeRangeUnit == 2) { lenTime = lenTime * 24; /*timeRangeUnit = 1;*/ } else if (timeRangeUnit == 10) { lenTime = lenTime * 3; /*timeRangeUnit = 1;*/ } else if (timeRangeUnit == 11) { lenTime = lenTime * 6; /*timeRangeUnit = 1;*/ } else if (timeRangeUnit == 12) { lenTime = lenTime * 12; /*timeRangeUnit = 1;*/ } else if (timeRangeUnit == 13) { lenTime = (sInt4) (lenTime / 3600.); /*timeRangeUnit = 1;*/ } else { printf ("Can't handle this timeRangeUnit\n"); //myAssert (timeRangeUnit == 1); return -8; } if (lenTime == GRIB2MISSING_s4) { lenTime = 0; } /* Find out what the name of this variable is. */ ParseElemName (center, subcenter, prodType, templat, cat, subcat, lenTime, timeIncrType, genID, probType, lowerProb, upperProb, &(inv->element), &(inv->comment), &(inv->unitName), &convert, percentile); /* if (strcmp (element, "") == 0) { mallocSprintf (&(inv->element), "unknown"); mallocSprintf (&(inv->unitName), "[%s]", unitName); if (strcmp (comment, "unknown") == 0) { mallocSprintf (&(inv->comment), "(prodType %d, cat %d, subcat %d)" " [%s]", prodType, cat, subcat, unitName); } else { mallocSprintf (&(inv->comment), "%s [%s]", comment, unitName); } } else { if (IsData_MOS (center, subcenter)) { * See : http://www.nco.ncep.noaa.gov/pmb/docs/on388/tablea.html * if (genID == 96) { inv->element = (char *) malloc ((1 + 7 + strlen (element)) * sizeof (char)); sprintf (inv->element, "MOSGFS-%s", element); } else { inv->element = (char *) malloc ((1 + 4 + strlen (element)) * sizeof (char)); sprintf (inv->element, "MOS-%s", element); } } else { inv->element = (char *) malloc ((1 + strlen (element)) * sizeof (char)); strcpy (inv->element, element); } mallocSprintf (&(inv->unitName), "[%s]", unitName); mallocSprintf (&(inv->comment), "%s [%s]", comment, unitName); * inv->unitName = (char *) malloc ((1 + 2 + strlen (unitName)) * sizeof (char)); sprintf (inv->unitName, "[%s]", unitName); inv->comment = (char *) malloc ((1 + 3 + strlen (unitName) + strlen (comment)) * sizeof (char)); sprintf (inv->comment, "%s [%s]", comment, unitName); * } */ if ((templat == GS4_RADAR) || (templat == GS4_SATELLITE) || (templat == 254) || (templat == 1000) || (templat == 1001) || (templat == 1002)) { reallocSprintf (&(inv->shortFstLevel), "0 undefined"); reallocSprintf (&(inv->longFstLevel), "0.000[-] undefined ()"); } else { fstSurfType = (*buffer)[23 - 5]; scale = (*buffer)[24 - 5]; MEMCPY_BIG (&value, *buffer + 25 - 5, sizeof (sInt4)); if ((value == GRIB2MISSING_s4) || (scale == GRIB2MISSING_s1)) { fstSurfValue = 0; } else { fstSurfValue = value * pow (10.0, (int) (-1 * scale)); } sndSurfType = (*buffer)[29 - 5]; scale = (*buffer)[30 - 5]; MEMCPY_BIG (&value, *buffer + 31 - 5, sizeof (sInt4)); if ((value == GRIB2MISSING_s4) || (scale == GRIB2MISSING_s1) || (sndSurfType == GRIB2MISSING_u1)) { sndSurfValue = 0; f_sndValue = 0; } else { sndSurfValue = value * pow (10.0, -1 * scale); f_sndValue = 1; } ParseLevelName (center, subcenter, fstSurfType, fstSurfValue, f_sndValue, sndSurfValue, &(inv->shortFstLevel), &(inv->longFstLevel)); } /* Jump past section 5. */ sectNum = 5; if (GRIB2SectJump (fp, gribLen, §Num, &secLen) != 0) { errSprintf ("ERROR: Problems Jumping past section 5\n"); return -9; } /* Jump past section 6. */ sectNum = 6; if (GRIB2SectJump (fp, gribLen, §Num, &secLen) != 0) { errSprintf ("ERROR: Problems Jumping past section 6\n"); return -10; } /* Jump past section 7. */ sectNum = 7; if (GRIB2SectJump (fp, gribLen, §Num, &secLen) != 0) { errSprintf ("ERROR: Problems Jumping past section 7\n"); return -11; } return 0; }