bool NFtimeLineAddStep(NFtimeLine_p timeLine, NFtime_p timePtr) { NFtimeStep_t timeStep; if (timeLine == (NFtimeLine_p) NULL) { CMmsgPrint(CMmsgAppError, "Invalid timeline object in %s:%d!\n", __FILE__, __LINE__); return (false); } if ((timeLine->TimeLine = (NFtime_p *) realloc(timeLine->TimeLine, (timeLine->TimeStepNum + 2) * sizeof(NFtime_p))) == (NFtime_p *) NULL) { CMmsgPrint(CMmsgSysError, "Memory allocation error in %s:%d!\n", __FILE__, __LINE__); goto Abort; } if (timeLine->TimeStepNum == 0) { if ((timeLine->TimeLine[timeLine->TimeStepNum] = NFtimeCreate()) == (NFtime_p) NULL) { CMmsgPrint(CMmsgAppError, "Time creation error in %s:%d!\n", __FILE__, __LINE__); goto Abort; } } else { if (NFtimeStepSetFromTimes(&timeStep, timeLine->TimeLine[timeLine->TimeStepNum - 1], timePtr)) { if (timeLine->TimeStep == (NFtimeStep_p) NULL) { if ((timeLine->TimeStep = NFtimeStepCreate()) == (NFtimeStep_p) NULL) { CMmsgPrint(CMmsgAppError, "Time step creation error in %s:%d!\n", __FILE__, __LINE__); goto Abort; } NFtimeStepCopy(&timeStep, timeLine->TimeStep); } else { if ((timeLine->TimeStep->Unit != timeStep.Unit) || (timeLine->TimeStep->Length != timeStep.Length)) timeLine->Regular = false; } } } NFtimeCopy(timePtr, timeLine->TimeLine[timeLine->TimeStepNum]); timeLine->TimeStepNum += 1; if ((timeLine->TimeLine[timeLine->TimeStepNum] = NFtimeCreate()) == (NFtime_p) NULL) { CMmsgPrint(CMmsgAppError, "Time creation error in %s:%d!\n", __FILE__, __LINE__); goto Abort; } return (true); Abort: return (false); }
/**************************************************************************************************************** * Model *****************************************************************************************************************/ static CMreturn _NFcompModelInit(NFobject_p object) { NFcompModel_p model = (NFcompModel_p) object; model->Version = (char *) NULL; model->Begin = NFtimeCreate(); model->End = NFtimeCreate(); model->TimeStep = NFtimeStepCreate(); model->Domain = (NFdomain_p) NULL; model->Parameters = NFobjListCreate(object); model->Components = NFobjListCreate(object); model->IOPlugins = NFobjListCreate(object); model->ModPlugins = NFobjListCreate(object); model->UtSystem = ut_read_xml((char *) NULL); model->TimeSteps = NFlistCreate(); model->Couplers = NFlistCreate(); return (CMsucceeded); }
NFtimeLine_p NCtimeLineExpand (NCaxis_p timeAxis, NFtime_p beginTime, NFtime_p endTime, cv_converter *cvConverter, NFtimeLine_p timeLine) { size_t timeStep = 0; NFtime_p timePtr = (NFtime_p) NULL; double variable; if ((timePtr = (NFtimeCreate ())) == (NFtime_p) NULL) { CMmsgPrint (CMmsgAppError,"Time object creation error in %s:%d!\n",__FILE__,__LINE__); return ((NFtimeLine_p) NULL); } if (timeLine == (NFtimeLine_p) NULL) timeLine = NFtimeLineCreate (); for (timeStep = 0;timeStep < timeAxis->N; ++timeStep) { variable = cv_convert_double (cvConverter, timeAxis->Bounds [timeStep * 2]); if (NFtimeSetFromVariable (timePtr, variable) == false) { CMmsgPrint (CMmsgAppError, "Time reading error in %s:%d!\n",__FILE__,__LINE__); NFtimeLineFree (timeLine); return ((NFtimeLine_p) NULL); } if (NFtimeCompare (beginTime, timePtr) <= 0) break; } if (timeStep < timeAxis->N) { for ( ;timeStep < timeAxis->N; ++timeStep) { variable = cv_convert_double (cvConverter, timeAxis->Bounds [timeStep * 2]); if (NFtimeSetFromVariable (timePtr, variable) == false) { CMmsgPrint (CMmsgAppError, "Time reading error in %s:%d!\n",__FILE__,__LINE__); NFtimeLineFree (timeLine); return ((NFtimeLine_p) NULL); } if (NFtimeLineAddStep (timeLine,timePtr) == false) { CMmsgPrint (CMmsgAppError, "Time step insertion error in %s:%d!\n",__FILE__,__LINE__); return ((NFtimeLine_p) NULL); } variable = cv_convert_double (cvConverter, timeAxis->Bounds [timeStep * 2 + 1]); if (NFtimeSetFromVariable (timePtr, variable) == false) { CMmsgPrint (CMmsgAppError, "Time reading error in %s:%d!\n",__FILE__,__LINE__); NFtimeLineFree (timeLine); return ((NFtimeLine_p) NULL); } if (NFtimeCompare (endTime, timePtr) < 0) break; } if (NFtimeSetFromVariable (timePtr, variable) == false) { CMmsgPrint (CMmsgAppError, "Time reading error in %s:%d!\n",__FILE__,__LINE__); NFtimeLineFree (timeLine); return ((NFtimeLine_p) NULL); } NFtimeCopy (timePtr,timeLine->TimeLine [timeLine->TimeStepNum]); } else { CMmsgPrint (CMmsgAppError, "Time axis outside of time period in %s:%d!\n",__FILE__,__LINE__); NFtimeLineFree (timeLine); return ((NFtimeLine_p) NULL); } if (timePtr != (NFtime_p) NULL) NFtimeFree (timePtr); return (timeLine); }
NFio_p Open_NCgrid(const char *url, NFtime_p beginTime, NFtime_p endTime, ut_system *utSystem) { int status, ncid; size_t i; const char *urlAddress; NCurlType urlType; NFio_p io = (NFio_p) NULL; NFtime_p timePtr = (NFtime_p) NULL; NFtime_p bundleTime = (NFtime_p) NULL; NFtimeUnit bundleTimeUnit; NFtimeStep_p bundleTimeStep = (NFtimeStep_p) NULL; NCgrid_p ncGrid = (NCgrid_p) NULL; ut_unit *baseTimeUnit = (ut_unit *) NULL; cv_converter *cvConverter = (cv_converter *) NULL; if ((baseTimeUnit = ut_parse(utSystem, "seconds since 2001-01-01 00:00:00", UT_ASCII)) == (ut_unit *) NULL) { CMmsgPrint(CMmsgAppError, "Total metal gebasz in %s:%d!\n", __FILE__, __LINE__); switch (ut_get_status()) { case UT_BAD_ARG: CMmsgPrint(CMmsgAppError, "System or string is NULL!\n"); break; case UT_SYNTAX: CMmsgPrint(CMmsgAppError, "String contained a syntax error!n"); break; case UT_UNKNOWN: CMmsgPrint(CMmsgAppError, "String contained an unknown identifier!\n"); break; default: CMmsgPrint(CMmsgSysError, "System error in %s:%d!n", __FILE__, __LINE__); } goto Abort; } if ((bundleTime = NFtimeCreate()) == (NFtime_p) NULL) { CMmsgPrint(CMmsgAppError, "Time object creation error in %s:%d!\n", __FILE__, __LINE__); goto Abort; } if ((bundleTimeStep = NFtimeStepCreate()) == (NFtimeStep_p) NULL) { CMmsgPrint(CMmsgAppError, "Timestep object creation error in %s:%d!\n", __FILE__, __LINE__); goto Abort; } bundleTimeUnit = NCurlGetBundleTimeUnit(url); if (((urlType = NCurlGetType(url)) == NCurlInvalid) || ((urlAddress = NCurlGetAddress(url)) == (const char *) NULL)) { CMmsgPrint(CMmsgAppError, "Ivalid URL: %s!\n", url); goto Abort; } if ((io = NFioCreate()) == (NFio_p) NULL) { CMmsgPrint(CMmsgAppError, "Layout creation error in: %s:%d\n", __FILE__, __LINE__); goto Abort; } if ((ncGrid = _NCgridCreate()) == (NCgrid_p) NULL) { CMmsgPrint(CMmsgSysError, "Plugin data allocation error in: %s:%d\n", __FILE__, __LINE__); goto Abort; } ncGrid->FileNames[0] = CMstrDuplicate(urlAddress); if (bundleTimeUnit != NFtimeUnitUnset) { ncGrid->FileNames[0] = NCcompleteURLaddress(urlAddress, ncGrid->FileNames[0], bundleTimeUnit, beginTime); NFtimeStepSet(bundleTimeStep, bundleTimeUnit, 1); // TODO handle return value } if ((status = nc_open(ncGrid->FileNames[0], NC_NOWRITE, &(ncGrid->NCid))) != NC_NOERR) { CMmsgPrint(CMmsgAppError, "NetCDF (%s) Openining error \"%s\"!\n", ncGrid->FileNames[0], nc_strerror(status)); goto Abort; } ncGrid->Open = 0; if (!NCaxisInitialize(ncGrid->NCid, ncGrid->X[0], NCaxisX, utSystem)) goto Abort; if (!NCaxisInitialize(ncGrid->NCid, ncGrid->Y[0], NCaxisY, utSystem)) goto Abort; if (!NCaxisInitialize(ncGrid->NCid, ncGrid->Z[0], NCaxisZ, utSystem)) goto Abort; if (!NCaxisInitialize(ncGrid->NCid, ncGrid->Time[0], NCaxisTime, utSystem)) goto Abort; io->ItemNum = ncGrid->X[0]->N * ncGrid->Y[0]->N * ncGrid->Z[0]->N; io->ItemBoxMin.X = ncGrid->X[0]->IntervalMin; io->ItemBoxMin.Y = ncGrid->Y[0]->IntervalMin; io->ItemBoxMax.X = ncGrid->X[0]->IntervalMax; io->ItemBoxMax.Y = ncGrid->Y[0]->IntervalMax; io->Extent.LowerLeft.X = ncGrid->X[0]->Bounds[ncGrid->X[0]->N * 2 - 1] - ncGrid->X[0]->Bounds[0]; io->Extent.LowerLeft.Y = ncGrid->Y[0]->Bounds[ncGrid->Y[0]->N * 2 - 1] - ncGrid->Y[0]->Bounds[0]; if ((cvConverter = ut_get_converter(ncGrid->Time[0]->Unit, baseTimeUnit)) == (cv_converter *) NULL) { CMmsgPrint(CMmsgAppError, "Time converter error!n"); switch (ut_get_status()) { case UT_BAD_ARG: CMmsgPrint(CMmsgAppError, "unit1 or unit2 is NULL.\n"); break; case UT_NOT_SAME_SYSTEM: CMmsgPrint(CMmsgAppError, "unit1 and unit2 belong to different unit-systems."); break; default: CMmsgPrint(CMmsgAppError, "Conversion between the units is not possible."); break; } } io->TimeLine = NCtimeLineExpand(ncGrid->Time[0], beginTime, endTime, cvConverter, io->TimeLine); if (io->TimeLine == (NFtimeLine_p) NULL) { CMmsgPrint(CMmsgAppError, "Timeline expansion error in %s:%d!\n", __FILE__, __LINE__); goto Abort; } cv_free(cvConverter); if ((ncGrid->TimeBundleIDs = (size_t *) realloc(ncGrid->TimeBundleIDs, io->TimeLine->TimeStepNum * sizeof(size_t))) == (size_t *) NULL) { CMmsgPrint(CMmsgSysError, "Memory allocation error in %s:%d!\n", __FILE__, __LINE__); goto Abort; } for (i = 0; i < io->TimeLine->TimeStepNum; ++i) ncGrid->TimeBundleIDs[i] = 0; if (bundleTimeUnit != NFtimeUnitUnset) { NFtimeCopy(beginTime, bundleTime); // TODO handle return value if ((ncGrid = _NCgridRealloc(ncGrid)) == (NCgrid_p) NULL) goto Abort; for (NFtimeAdvance(bundleTime, bundleTimeStep); NFtimeCompare(bundleTime, endTime) < 0; NFtimeAdvance(bundleTime, bundleTimeStep)) { ncGrid->FileNames[ncGrid->FileNum - 1] = NCcompleteURLaddress(urlAddress, CMstrDuplicate(urlAddress), bundleTimeUnit, bundleTime); if ((status = nc_open(ncGrid->FileNames[ncGrid->FileNum - 1], NC_NOWRITE, &ncid)) != NC_NOERR) { CMmsgPrint(CMmsgAppError, "NetCDF (%s) Openining error \"%s\"!\n", ncGrid->FileNames[ncGrid->FileNum - 1], nc_strerror(status)); goto Abort; } if (!NCaxisInitialize(ncid, ncGrid->X[ncGrid->FileNum - 1], NCaxisX, utSystem)) goto Abort; if (!NCaxisInitialize(ncid, ncGrid->Y[ncGrid->FileNum - 1], NCaxisY, utSystem)) goto Abort; if (!NCaxisInitialize(ncid, ncGrid->Z[ncGrid->FileNum - 1], NCaxisZ, utSystem)) goto Abort; if (!NCaxisInitialize(ncid, ncGrid->Time[ncGrid->FileNum - 1], NCaxisTime, utSystem)) goto Abort; nc_close(ncid); if ((ncGrid->X[0]->N != ncGrid->X[ncGrid->FileNum - 1]->N) || (ncGrid->Y[0]->N != ncGrid->Y[ncGrid->FileNum - 1]->N) || (ncGrid->Z[0]->N != ncGrid->Z[ncGrid->FileNum - 1]->N) || (ncGrid->Time[0]->N != ncGrid->Time[ncGrid->FileNum - 1]->N)) { CMmsgPrint(CMmsgUsrError, "Inconsisten NetCDF [%s] bundle!\n", urlAddress); goto Abort; } if ((cvConverter = ut_get_converter(ncGrid->Time[ncGrid->FileNum - 1]->Unit, baseTimeUnit)) == (cv_converter *) NULL) { CMmsgPrint(CMmsgAppError, "Time converter error!n"); switch (ut_get_status()) { case UT_BAD_ARG: CMmsgPrint(CMmsgAppError, "unit1 or unit2 is NULL.\n"); break; case UT_NOT_SAME_SYSTEM: CMmsgPrint(CMmsgAppError, "unit1 and unit2 belong to different unit-systems."); break; default: CMmsgPrint(CMmsgAppError, "Conversion between the units is not possible."); break; } } io->TimeLine = NCtimeLineExpand(ncGrid->Time[ncGrid->FileNum - 1], beginTime, endTime, cvConverter, io->TimeLine); if (io->TimeLine == (NFtimeLine_p) NULL) { CMmsgPrint(CMmsgAppError, "Timeline expansion error in %s:%d!\n", __FILE__, __LINE__); goto Abort; } cv_free(cvConverter); if ((ncGrid->TimeBundleIDs = (size_t *) realloc(ncGrid->TimeBundleIDs, io->TimeLine->TimeStepNum * sizeof(size_t))) == (size_t *) NULL) { CMmsgPrint(CMmsgSysError, "Memory allocation error in %s:%d!\n", __FILE__, __LINE__); goto Abort; } for (; i < io->TimeLine->TimeStepNum; ++i) ncGrid->TimeBundleIDs[i] = ncGrid->FileNum - 1; } } io->PluginData = (void *) ncGrid; io->GetItem = _NCgridGetItem; io->GetItemList = _NCgridGetItemList; io->GetVertexes = _NCgridGetVertexes; io->ProjectXY2UV = NFioDefaultProjectXY2UV; // TODO Handle proj4 entries io->Close = _NCgridClose; io->VarHandleFunc = _NCgridVariableHandle; io->VarTypeFunc = _NCgridVariableType; io->VarLoadFunc = _NCgridVariableLoad; NFtimeFree(timePtr); NFtimeFree(bundleTime); NFtimeStepFree(bundleTimeStep); ut_free(baseTimeUnit); return (io); Abort: nc_close(ncGrid->NCid); if (ncGrid != (NCgrid_p) NULL) _NCgridFree(ncGrid); if (io != (NFio_p) NULL) NFioFree(io); if (timePtr != (NFtime_p) NULL) NFtimeFree(timePtr); if (bundleTime != (NFtime_p) NULL) NFtimeFree(bundleTime); if (bundleTimeStep != (NFtimeStep_p) NULL) NFtimeStepFree(bundleTimeStep); if (baseTimeUnit != (ut_unit *) NULL) ut_free(baseTimeUnit); return ((NFio_p) NULL); }