NCstate NCdsHandleSetFill (NCdsHandle_t *dsh, size_t *index) { switch (dsh->DataType) { default: break; case NCtypeGCont: return (NCdsHandleGContSetFill ((NCdsHandleGCont_t *) dsh, index [0], index [1])); // case NCtypeGDisc: return (NCdsHandleGDiscSetFloat ((NCdsHandleGDisc_t *) dsh, index [0], index [1], val)); // case NCtypePoint: return (NCdsHandleVPoinSetFloat ((NCdsHandleVPoint_t *) dsh, index [0], val)); } return (NCfailed); }
NCstate NCdsHandleGContLoadCache(NCdsHandleGCont_t *gCont, size_t sTStep, size_t nTSteps, size_t sLevel, size_t nLevels) { int status; size_t start[4], count[4]; size_t ncidx, tStep, level, row, col; double val; count[0] = 1; if (gCont->LVarIds[0] == NCundefined) { start[1] = (size_t) 0; count[1] = gCont->RowNum; start[2] = (size_t) 0; count[2] = gCont->ColNum; } else { count[1] = 1; start[2] = (size_t) 0; count[2] = gCont->RowNum; start[3] = (size_t) 0; count[3] = gCont->ColNum; } for (row = 0; row < gCont->RowNum; row++) for (col = 0; col < gCont->ColNum; col++) { gCont->ObsNum[gCont->ColNum * row + col] = (size_t) 0; gCont->Data[gCont->ColNum * row + col] = (double) 0.0; } for (tStep = sTStep; tStep < sTStep + nTSteps; tStep++) for (level = sLevel; level < sLevel + nLevels; level++) { ncidx = gCont->NCindex[tStep]; start[0] = tStep - gCont->NCoffset[ncidx]; if (gCont->LVarIds[ncidx] != NCundefined) start[1] = level; if ((status = nc_sync(gCont->NCIds[ncidx])) != NC_NOERR) { NCprintNCError (status, "NCdsHandleGContLoadCache"); return (NCfailed); } if ((status = nc_get_vara_double(gCont->NCIds[ncidx], gCont->GVarIds[ncidx], start, count, gCont->AuxData)) != NC_NOERR) { NCprintNCError (status, "NCdsHandleGContLoadCache"); return (NCfailed); } for (row = 0; row < gCont->RowNum; row++) for (col = 0; col < gCont->ColNum; col++) if (_NCdsHandleGContTestNodata(gCont, (val = gCont->AuxData[gCont->ColNum * row + col])) == false) { gCont->Data[gCont->ColNum * row + col] += val; gCont->ObsNum[gCont->ColNum * row + col] += 1; } } for (row = 0; row < gCont->RowNum; row++) for (col = 0; col < gCont->ColNum; col++) if (gCont->ObsNum[gCont->ColNum * row + col] == 0) NCdsHandleGContSetFill(gCont, row, col); else if (gCont->ObsNum[gCont->ColNum * row + col] > 1) gCont->Data[gCont->ColNum * row + col] /= (double) gCont->ObsNum[gCont->ColNum * row + col]; return (NCsucceeded); }
NCstate NCdsHandleGContDefine(NCdsHandleGCont_t *gCont, int *ncids, size_t n) { int status, varid, i; int missingInt, fillInt; double missingFloat, fillFloat, scale, offset; size_t unitlen, row, col; char unitstr[NC_MAX_NAME], gunitstr[NC_MAX_NAME]; gCont->Data = (double *) NULL; gCont->AuxData = (double *) NULL; gCont->ObsNum = (size_t *) NULL; gCont->MeanIds = gCont->MinIds = gCont->MaxIds = gCont->StdIds = (int *) NULL; if (n < 1) return (NCfailed); if (NCdsHandleGridDefine((NCdsHandleGrid_t *) gCont, ncids, n) == NCfailed) return (NCfailed); if (gCont->DataType != NCtypeGCont) { CMmsgPrint(CMmsgAppError, "Invalid grid data in: %s %d", __FILE__, __LINE__); NCdsHandleGridClear((NCdsHandleGrid_t *) gCont); return (NCfailed); } if (((gCont->MeanIds = (int *) calloc(n, sizeof(int))) == (int *) NULL) || ((gCont->MinIds = (int *) calloc(n, sizeof(int))) == (int *) NULL) || ((gCont->MaxIds = (int *) calloc(n, sizeof(int))) == (int *) NULL) || ((gCont->StdIds = (int *) calloc(n, sizeof(int))) == (int *) NULL) || ((gCont->Data = (double *) calloc(gCont->ColNum * gCont->RowNum, sizeof(double))) == (double *) NULL) || ((gCont->AuxData = (double *) calloc(gCont->ColNum * gCont->RowNum, sizeof(double))) == (double *) NULL) || ((gCont->ObsNum = (size_t *) calloc(gCont->ColNum * gCont->RowNum, sizeof(size_t))) == (size_t *) NULL)) { CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__); if (gCont->MeanIds != (int *) NULL) free(gCont->MeanIds); if (gCont->MinIds != (int *) NULL) free(gCont->MinIds); if (gCont->MaxIds != (int *) NULL) free(gCont->MaxIds); if (gCont->StdIds != (int *) NULL) free(gCont->StdIds); NCdsHandleGridClear((NCdsHandleGrid_t *) gCont); return (NCfailed); } for (i = 0; i < n; ++i) gCont->MeanIds[i] = gCont->MinIds[i] = gCont->MaxIds[i] = gCont->StdIds[i] = NCundefined; for (i = 0; i < n; ++i) { switch (gCont->GType) { default: gCont->FillValue.Float = gCont->MissingVal.Float = FLOAT_NOVALUE; break; case NC_BYTE: case NC_SHORT: case NC_INT: if ((status = nc_get_att_int(gCont->NCIds[i], gCont->GVarIds[i], NCnameVAFillValue, &fillInt)) != NC_NOERR) { if ((status = nc_get_att_int(gCont->NCIds[i], gCont->GVarIds[i], NCnameVAMissingVal, &missingInt)) != NC_NOERR) missingInt = fillInt = INT_NOVALUE; else fillInt = missingInt; } else if ((status = nc_get_att_int(gCont->NCIds[i], gCont->GVarIds[i], NCnameVAMissingVal, &missingInt)) != NC_NOERR) missingInt = fillInt; break; case NC_FLOAT: case NC_DOUBLE: if ((status = nc_get_att_double(gCont->NCIds[i], gCont->GVarIds[i], NCnameVAFillValue, &fillFloat)) != NC_NOERR) { if ((status = nc_get_att_double(gCont->NCIds[i], gCont->GVarIds[i], NCnameVAMissingVal, &missingFloat)) != NC_NOERR) missingFloat = fillFloat = FLOAT_NOVALUE; else fillFloat = missingFloat; } else if ((status = nc_get_att_double(gCont->NCIds[i], gCont->GVarIds[i], NCnameVAMissingVal, &missingFloat)) != NC_NOERR) missingFloat = fillFloat; break; } if ((status = nc_get_att_double(gCont->NCIds[i], gCont->GVarIds[i], NCnameVAScaleFactor, &scale)) != NC_NOERR) scale = 1.0; if ((status = nc_get_att_double(gCont->NCIds[i], gCont->GVarIds[i], NCnameVAAddOffset, &offset)) != NC_NOERR) offset = 0.0; if (((status = nc_inq_attlen(gCont->NCIds[i], gCont->GVarIds[i], NCnameVAUnits, &unitlen)) == NC_NOERR) && ((status = nc_get_att_text(gCont->NCIds[i], gCont->GVarIds[i], NCnameVAUnits, unitstr)) == NC_NOERR)) unitstr[unitlen] = '\0'; if (i == 0) { if ((status == NC_NOERR) && (utScan(unitstr, &(gCont->GUnit)) == 0)) { gCont->DoGUnit = true; strcpy (gunitstr, unitstr); } else gCont->DoGUnit = false; switch (gCont->GType) { case NC_BYTE: case NC_SHORT: case NC_INT: gCont->FillValue.Int = fillInt; gCont->MissingVal.Int = missingInt; break; default: case NC_FLOAT: case NC_DOUBLE: gCont->FillValue.Float = fillFloat; gCont->MissingVal.Float = missingFloat; break; } gCont->Scale = scale; gCont->Offset = offset; } else { if (gCont->DoGUnit && ((status != NC_NOERR) || strcmp(unitstr, gunitstr) != 0)) { CMmsgPrint(CMmsgAppError, "Inconsistent data bundle (units) in: %s %d", __FILE__, __LINE__); return (NCfailed); } switch (gCont->GType) { case NC_BYTE: case NC_SHORT: case NC_INT: if ((gCont->FillValue.Int != fillInt) || (gCont->MissingVal.Int != missingInt)) { CMmsgPrint(CMmsgAppError, "Inconsistent data bundle (int fill value) in: %s %d", __FILE__, __LINE__); return (NCfailed); } break; default: case NC_FLOAT: case NC_DOUBLE: if ((NCmathEqualValues(gCont->FillValue.Float, fillFloat) == false) || (NCmathEqualValues(gCont->MissingVal.Float, missingFloat) == false)) { CMmsgPrint(CMmsgAppError, "Inconsistent bundle (float fill value) in: %s %d", __FILE__, __LINE__); return (NCfailed); } break; } if ((NCmathEqualValues(gCont->Scale, scale) == false) || (NCmathEqualValues(gCont->Offset, offset) == false)) { CMmsgPrint(CMmsgAppError, "Inconsistent bundle (scale and offset) in: %s %d", __FILE__, __LINE__); return (NCfailed); } } gCont->MeanIds[i] = nc_inq_varid(ncids[i], NCnameVAAverage, &varid) == NC_NOERR ? varid : NCundefined; gCont->MaxIds[i] = nc_inq_varid(ncids[i], NCnameVAMaximum, &varid) == NC_NOERR ? varid : NCundefined; gCont->MinIds[i] = nc_inq_varid(ncids[i], NCnameVAMinimum, &varid) == NC_NOERR ? varid : NCundefined; gCont->StdIds[i] = nc_inq_varid(ncids[i], NCnameVAStdDev, &varid) == NC_NOERR ? varid : NCundefined; } for (row = 0; row < gCont->RowNum; row++) for (col = 0; col < gCont->ColNum; col++) NCdsHandleGContSetFill(gCont, row, col); return (NCsucceeded); }