static int _DBObjTableListSort(const DBObjRecord **obj0, const DBObjRecord **obj1) { DBInt ret = 0; DBObjTableField *field; DBDate date0, date1; for (field = _DBObjTableSortFields->First(); field != (DBObjTableField *) NULL; field = _DBObjTableSortFields->Next()) { switch (field->Type()) { case DBTableFieldString: ret = strcmp(field->String(*obj0), field->String(*obj1)); break; case DBTableFieldInt: ret = field->Int(*obj0) - field->Int(*obj1); break; case DBTableFieldFloat: if (field->Float(*obj0) - field->Float(*obj1) > 0.0) ret = 1; else if (field->Float(*obj0) - field->Float(*obj1) < 0.0) ret = -1; else ret = 0; break; case DBTableFieldDate: date0 = field->Date(*obj0); date1 = field->Date(*obj1); if (date0 > date1) ret = 1; else if (date0 < date1) ret = -1; else ret = 0; break; case DBTableFieldTableRec: ret = strcmp((*obj0)->Name(), (*obj1)->Name()); break; case DBTableFieldDataRec: ret = (*obj0)->RowID() - (*obj1)->RowID(); break; } if ((field->Flags() & DBObjectFlagSortReversed) == DBObjectFlagSortReversed) ret *= -1; if (ret != 0) return (ret); } if (ret == 0) ret = (*obj0)->ListPos() - (*obj1)->ListPos(); return (ret); }
int DBObjTable::Read(FILE *file, int swap) { DBInt id; DBDate date; DBCoordinate coord; DBRegion region; DBPosition pos; DBUShort row, col; DBObjRecord *record; DBObjTableField *field; if (DBObjectLIST<DBObjRecord>::Read(file, swap) != DBSuccess) return (DBFault); if (FieldPTR->Read(file, swap) != DBSuccess) return (DBFault); for (id = 0; id < FieldPTR->ItemNum(); ++id) if (FieldPTR->ReadItem(file, id, swap) == DBFault) return (DBFault); RecordLengthVAR = 0; for (field = FieldPTR->First(); field != (DBObjTableField *) NULL; field = FieldPTR->Next()) RecordLengthVAR = RecordLengthVAR > field->StartByte() + field->Length() ? RecordLengthVAR : field->StartByte() + field->Length(); for (id = 0; id < ItemNum(); ++id) { if (ReadItem(file, id, swap) == DBFault) return (DBFault); record = Item(id); if (swap == false) continue; for (field = FieldPTR->First(); field != (DBObjTableField *) NULL; field = FieldPTR->Next()) switch (field->Type()) { case DBTableFieldInt: case DBTableFieldFloat: case DBTableFieldTableRec: case DBTableFieldDataRec: switch (field->Length()) { case 2: DBByteOrderSwapHalfWord((char *) record->Data() + field->StartByte()); break; case 4: DBByteOrderSwapWord((char *) record->Data() + field->StartByte()); break; case 8: DBByteOrderSwapLongWord((char *) record->Data() + field->StartByte()); break; } break; case DBTableFieldDate: date = field->Date(record); date.Swap(); field->Date(record, date); break; case DBTableFieldCoord: coord = field->Coordinate(record); coord.Swap(); field->Coordinate(record, coord); break; case DBTableFieldRegion: region = field->Region(record); region.Swap(); field->Region(record, region); break; case DBTableFieldPosition: pos = field->Position(record); switch (field->Length()) { case sizeof(DBPosition): pos.Swap(); break; default: row = pos.Row; col = pos.Col; DBByteOrderSwapHalfWord(&row); DBByteOrderSwapHalfWord(&col); pos.Row = row; pos.Col = col; break; } field->Position(record, pos); break; default: break; } } if (MethodPTR->Read(file, swap) != DBSuccess) return (DBFault); for (id = 0; id < MethodPTR->ItemNum(); ++id) if (MethodPTR->ReadItem(file, id, swap) == DBFault) return (DBFault); return (DBSuccess); }
int main (int argc,char *argv []) { int argPos, argNum = argc, ret, verbose = false; DBInt recID; bool padding = false; char *tableName = (char *) NULL; char *fieldName = (char *) NULL; char *yearFieldName = (char *) NULL; char *monthFieldName = (char *) NULL; char *dayFieldName = (char *) NULL; char *hourFieldName = (char *) NULL; char *minFieldName = (char *) NULL; char *tmp = (char *) NULL; DBObjData *data; DBObjTable *table; DBObjTableField *srcField; DBObjTableField *yearField = (DBObjTableField *) NULL; DBObjTableField *monthField= (DBObjTableField *) NULL; DBObjTableField *dayField = (DBObjTableField *) NULL; DBObjTableField *hourField = (DBObjTableField *) NULL; DBObjTableField *minField = (DBObjTableField *) NULL; DBInt dbInputType = DBTableFieldInt; DBInt dbInputSize = sizeof(DBShort); DBDate date; DBObjRecord *record; for (argPos = 1;argPos < argNum; ) { if (CMargTest (argv [argPos],"-a","--table")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) { CMmsgPrint (CMmsgUsrError,"Missing table name!"); return (CMfailed); } tableName = argv [argPos]; if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-f","--field")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) { CMmsgPrint (CMmsgUsrError,"Missing field name!"); return (CMfailed); } fieldName = argv [argPos]; if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-y","--year")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) { CMmsgPrint (CMmsgUsrError,"Missing year field name!"); return (CMfailed); } yearFieldName = argv [argPos]; if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-m","--month")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) { CMmsgPrint (CMmsgUsrError,"Missing month field name!"); return (CMfailed); } monthFieldName = argv [argPos]; if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-d","--day")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) { CMmsgPrint (CMmsgUsrError,"Missing day field name!"); return (CMfailed); } dayFieldName = argv [argPos]; if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-o","--hour")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) { CMmsgPrint (CMmsgUsrError,"Missing hour field name!"); return (CMfailed); } hourFieldName = argv [argPos]; if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-i","--minute")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) { CMmsgPrint (CMmsgUsrError,"Missing minute field name!"); return (CMfailed); } minFieldName = argv [argPos]; if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-p","--padding")) { padding = true; dbInputType = DBTableFieldString; dbInputSize = sizeof(DBByte) * 3; if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-V","--verbose")) { verbose = true; if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-h","--help")) { CMmsgPrint (CMmsgInfo,"%s [options] <input file> <output file>",CMfileName(argv[0])); CMmsgPrint (CMmsgInfo," -a,--table [table name]"); CMmsgPrint (CMmsgInfo," -f,--field [field name]"); CMmsgPrint (CMmsgInfo," -y,--year [year field]"); CMmsgPrint (CMmsgInfo," -m,--month [month field]"); CMmsgPrint (CMmsgInfo," -d,--day [day field]"); CMmsgPrint (CMmsgInfo," -o,--hour [hour field]"); CMmsgPrint (CMmsgInfo," -i,--minute [minute field]"); CMmsgPrint (CMmsgInfo," -p,--padding"); CMmsgPrint (CMmsgInfo," -V,--verbose"); CMmsgPrint (CMmsgInfo," -h,--help"); return (DBSuccess); } if ((argv [argPos][0] == '-') && ((int) strlen (argv [argPos]) > 1)) { CMmsgPrint (CMmsgUsrError,"Unknown option: %s!",argv [argPos]); return (CMfailed); } argPos++; } if (argNum > 3) { CMmsgPrint (CMmsgUsrError,"Extra arguments!"); return (CMfailed); } if (verbose) RGlibPauseOpen (argv[0]); data = new DBObjData (); if (((argNum > 1) && (strcmp (argv [1],"-") != 0) ? data->Read (argv [1]) : data->Read (stdin)) == DBFault) { delete data; return (CMfailed); } if (tableName == (char *) NULL) tableName = DBrNItems; if ((table = data->Table (tableName)) == (DBObjTable *) NULL) { CMmsgPrint (CMmsgUsrError,"Invalid table!"); delete data; return (CMfailed); } if (fieldName == (char *) NULL) fieldName = (char *) "Date"; if ((srcField = table->Field (fieldName)) == (DBObjTableField *) NULL) { CMmsgPrint (CMmsgUsrError,"Missing date field!"); delete data; return (CMfailed); } if ((srcField->Type () != DBTableFieldString) && (srcField->Type () != DBTableFieldDate)) { CMmsgPrint (CMmsgUsrError,"Invalid date field!"); delete data; return (CMfailed); } if (yearFieldName != (char *) NULL) { if ((yearField = table->Field (yearFieldName)) == (DBObjTableField *) NULL) { yearField = new DBObjTableField (yearFieldName,DBTableFieldInt,"%4d",sizeof(DBShort),false); table->AddField (yearField); } } if (monthFieldName != (char *) NULL) { if ((monthField = table->Field (monthFieldName)) == (DBObjTableField *) NULL) { monthField = new DBObjTableField (monthFieldName,dbInputType,"%2d",dbInputSize,false); table->AddField (monthField); } } if (dayFieldName != (char *) NULL) { if (monthField == (DBObjTableField *) NULL) { CMmsgPrint (CMmsgUsrError,"Month field is not set!"); delete data; return (CMfailed); } if ((dayField = table->Field (dayFieldName)) == (DBObjTableField *) NULL) { dayField = new DBObjTableField (dayFieldName,dbInputType,"%2d",dbInputSize,false); table->AddField (dayField); } } if (hourFieldName != (char *) NULL) { if (dayField == (DBObjTableField *) NULL) { CMmsgPrint (CMmsgUsrError,"Day field is not set!"); delete data; return (CMfailed); } if ((hourField = table->Field (hourFieldName)) == (DBObjTableField *) NULL) { hourField = new DBObjTableField (hourFieldName,dbInputType,"%2d",dbInputSize,false); table->AddField (hourField); } } if (minFieldName != (char *) NULL) { if (hourField == (DBObjTableField *) NULL) { CMmsgPrint (CMmsgUsrError,"Hour field is not set!"); delete data; return (CMfailed); } if ((minField = table->Field (minFieldName)) == (DBObjTableField *) NULL) { minField = new DBObjTableField (minFieldName,dbInputType,"%2d",dbInputSize,false); table->AddField (minField); } } for (recID = 0;recID < table->ItemNum ();++recID) { record = table->Item (recID); if (srcField->Type () == DBTableFieldString) date.Set (srcField->String (record)); else date = srcField->Date (record); if (yearField != (DBObjTableField *) NULL) yearField->Int(record,date.Year ()); if(padding) { if (monthField != (DBObjTableField *) NULL) { if (date.Month() != DBDefaultMissingIntVal) monthField->String (record,tmp = _CMDpadit(date.Month() + 1,false)); else monthField->String (record,tmp = _CMDpadit(date.Month(),false)); free(tmp); } if (dayField != (DBObjTableField *) NULL) { dayField->String (record,tmp = _CMDpadit(date.Day(),false)); free(tmp); } if (hourField != (DBObjTableField *) NULL) { hourField->String (record,tmp = _CMDpadit(date.Hour(),false)); free(tmp); } if (minField != (DBObjTableField *) NULL) { minField->String (record,tmp = _CMDpadit(date.Minute(),false)); free(tmp); } } else { if (monthField != (DBObjTableField *) NULL) { if (date.Month() != DBDefaultMissingIntVal) monthField->Int(record,date.Month () + 1); else monthField->Int (record,DBDefaultMissingIntVal); } if (dayField != (DBObjTableField *) NULL) dayField->Int (record,date.Day ()); if (hourField != (DBObjTableField *) NULL) hourField->Int(record,date.Hour ()); if (minField != (DBObjTableField *) NULL) minField->Int (record,date.Minute ()); } } ret = (argNum > 2) && (strcmp (argv [2],"-") != 0) ? data->Write (argv [2]) : data->Write (stdout); delete data; if (verbose) RGlibPauseClose (); return (ret); }
DBInt DBPointToGrid(DBObjData *pntData, DBObjData *netData, DBObjData *grdData) { DBInt i; DBPosition pos; DBObjTable *pntTable = pntData->Table(DBrNItems); DBObjTable *grdTable = grdData->Table(DBrNItems); DBObjTable *symTable = grdData->Table(DBrNSymbols); DBObjectLIST<DBObjTableField> *pntFields = pntTable->Fields(); DBObjTableField *pntFLD; DBObjTableField *grdAttribFLD; DBObjTableField *grdFLD = grdTable->Field(DBrNGridValue); DBObjTableField *symFLD = grdTable->Field(DBrNSymbol); DBObjRecord *cellRec, *toCell, *pntRec, *itemRec; DBObjRecord *symRec = symTable->First(); DBVPointIF *pntIF; DBNetworkIF *netIF; DBGridIF *grdIF; pntIF = new DBVPointIF(pntData); netIF = new DBNetworkIF(netData); grdIF = new DBGridIF(grdData); grdIF->RenameLayer(grdIF->Layer((DBInt) 0), (char *) "Subbasins"); for (pos.Row = 0; pos.Row < grdIF->RowNum(); ++pos.Row) for (pos.Col = 0; pos.Col < grdIF->ColNum(); ++pos.Col) grdIF->Value(pos, DBFault); for (pntFLD = pntFields->First(); pntFLD != (DBObjTableField *) NULL; pntFLD = pntFields->Next()) if (DBTableFieldIsVisible(pntFLD)) grdTable->AddField(new DBObjTableField(*pntFLD)); for (i = 0; i < pntIF->ItemNum(); ++i) { DBPause(i * 100 / pntIF->ItemNum()); pntRec = pntIF->Item(i); if ((pntRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue; if ((cellRec = netIF->Cell(pntIF->Coordinate(pntRec))) == (DBObjRecord *) NULL) continue; itemRec = grdTable->Add(pntRec->Name()); grdFLD->Int(itemRec, pntRec->RowID() + 1); symFLD->Record(itemRec, symRec); for (pntFLD = pntFields->First(); pntFLD != (DBObjTableField *) NULL; pntFLD = pntFields->Next()) if ((grdAttribFLD = grdTable->Field(pntFLD->Name())) != (DBObjTableField *) NULL) switch (pntFLD->Type()) { case DBTableFieldString: grdAttribFLD->String(itemRec, pntFLD->String(pntRec)); break; case DBTableFieldInt: grdAttribFLD->Int(itemRec, pntFLD->Int(pntRec)); break; case DBTableFieldFloat: grdAttribFLD->Float(itemRec, pntFLD->Float(pntRec)); break; case DBTableFieldDate: grdAttribFLD->Date(itemRec, pntFLD->Date(pntRec)); break; } grdIF->Value(netIF->CellPosition(cellRec), itemRec->RowID()); } for (i = 0; i < netIF->CellNum(); ++i) { if ((cellRec = netIF->Cell(i)) == (DBObjRecord *) NULL) continue; if ((itemRec = grdIF->GridItem(netIF->CellPosition(cellRec))) != (DBObjRecord *) NULL) continue; if ((toCell = netIF->ToCell(cellRec)) == (DBObjRecord *) NULL) continue; if ((itemRec = grdIF->GridItem(netIF->CellPosition(toCell))) != (DBObjRecord *) NULL) grdIF->Value(netIF->CellPosition(cellRec), itemRec->RowID()); } grdIF->DiscreteStats(); delete pntIF; delete netIF; delete grdIF; return (DBSuccess); }
DBInt RGlibDataStream2RGIS(DBObjData *outData, DBObjData *tmplData, FILE *inFile) { DBInt layerID = 0, itemSize; DBPosition pos; DBFloat val; void *data = (void *) NULL; MFVarHeader_t header; DBObjRecord *record; switch (tmplData->Type()) { case DBTypeVectorPoint: { DBInt itemID; DBDate date; DBObjTable *itemTable = outData->Table(DBrNItems); DBObjTableField *idField = new DBObjTableField("ItemID", DBTableFieldInt, "%6d", sizeof(DBInt), false); DBObjTableField *dateField = new DBObjTableField("Date", DBTableFieldDate, "%s", sizeof(DBDate), false); DBObjTableField *valField; DBVPointIF *pntIF = new DBVPointIF(tmplData); itemTable->AddField(idField); itemTable->AddField(dateField); while (MFVarReadHeader(&header, inFile)) { if (header.ItemNum != pntIF->ItemNum()) { CMmsgPrint(CMmsgUsrError, "Error: Datastream inconsistency %d %d!", header.ItemNum, pntIF->ItemNum()); return (DBFault); } if (data == (void *) NULL) { itemSize = MFVarItemSize(header.DataType); if ((data = (void *) realloc(data, header.ItemNum * itemSize)) == (void *) NULL) { CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__); return (DBFault); } switch (header.DataType) { case MFByte: valField = new DBObjTableField("Value", DBTableFieldInt, "%2d", sizeof(char), false); case MFShort: valField = new DBObjTableField("Value", DBTableFieldInt, "%4d", sizeof(DBShort), false); case MFInt: valField = new DBObjTableField("Value", DBTableFieldInt, "%8d", sizeof(DBInt), false); case MFFloat: valField = new DBObjTableField("Value", DBTableFieldFloat, "%8.2f", sizeof(DBFloat4), false); case MFDouble: valField = new DBObjTableField("Value", DBTableFieldFloat, "%8.2f", sizeof(DBFloat), false); } itemTable->AddField(valField); } if ((int) fread(data, itemSize, header.ItemNum, inFile) != header.ItemNum) { CMmsgPrint(CMmsgSysError, "Error: Data stream read in: %s %d", __FILE__, __LINE__); return (DBFault); } for (itemID = 0; itemID < header.ItemNum; ++itemID) { record = itemTable->Add(header.Date); date.Set(header.Date); idField->Int(record, itemID); dateField->Date(record, date); /* decDateField->Float (record,date); */ switch (header.DataType) { case MFByte: valField->Int(record, ((char *) data)[itemID]); break; case MFShort: valField->Int(record, ((short *) data)[itemID]); break; case MFInt: valField->Int(record, ((int *) data)[itemID]); break; case MFFloat: valField->Float(record, ((float *) data)[itemID]); break; case MFDouble: valField->Float(record, ((double *) data)[itemID]); break; } } } delete pntIF; } break; case DBTypeGridContinuous: case DBTypeGridDiscrete: { DBGridIF *gridIF = new DBGridIF(outData); while (MFVarReadHeader(&header, inFile)) { if (header.ItemNum != gridIF->RowNum() * gridIF->ColNum()) { CMmsgPrint(CMmsgUsrError, "Error: Datastream inconsistency!"); return (DBFault); } if (layerID == 0) { itemSize = MFVarItemSize(header.DataType); if ((data = (void *) realloc(data, header.ItemNum * itemSize)) == (void *) NULL) { CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__); return (DBFault); } record = gridIF->Layer(layerID); gridIF->RenameLayer(header.Date); } else record = gridIF->AddLayer(header.Date); switch (header.DataType) { case MFByte: case MFShort: case MFInt: gridIF->MissingValue(record, header.Missing.Int); break; case MFFloat: case MFDouble: gridIF->MissingValue(record, header.Missing.Float); break; } if ((int) fread(data, itemSize, header.ItemNum, inFile) != header.ItemNum) { CMmsgPrint(CMmsgSysError, "Error: Data stream read in: %s %d", __FILE__, __LINE__); return (DBFault); } for (pos.Row = 0; pos.Row < gridIF->RowNum(); ++pos.Row) for (pos.Col = 0; pos.Col < gridIF->ColNum(); ++pos.Col) { switch (header.DataType) { case MFByte: val = (DBFloat) (((char *) data)[pos.Row * gridIF->ColNum() + pos.Col]); break; case MFShort: val = (DBFloat) (((short *) data)[pos.Row * gridIF->ColNum() + pos.Col]); break; case MFInt: val = (DBFloat) (((int *) data)[pos.Row * gridIF->ColNum() + pos.Col]); break; case MFFloat: val = (DBFloat) (((float *) data)[pos.Row * gridIF->ColNum() + pos.Col]); break; case MFDouble: val = (DBFloat) (((double *) data)[pos.Row * gridIF->ColNum() + pos.Col]); break; } gridIF->Value(record, pos, val); } layerID++; } gridIF->RecalcStats(); } break; case DBTypeNetwork: { DBInt cellID; DBGridIF *gridIF = new DBGridIF(outData); DBNetworkIF *netIF = new DBNetworkIF(tmplData); while (MFVarReadHeader(&header, inFile)) { if (header.ItemNum != netIF->CellNum()) { CMmsgPrint(CMmsgUsrError, "Error: Datastream inconsistency!"); return (DBFault); } if (layerID == 0) { itemSize = MFVarItemSize(header.DataType); if ((data = (void *) realloc(data, header.ItemNum * itemSize)) == (void *) NULL) { CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__); return (DBFault); } record = gridIF->Layer(layerID); gridIF->RenameLayer(header.Date); } else record = gridIF->AddLayer(header.Date); if ((int) fread(data, itemSize, header.ItemNum, inFile) != header.ItemNum) { CMmsgPrint(CMmsgSysError, "Error: Data stream read in: %s %d", __FILE__, __LINE__); delete netIF; return (DBFault); } for (pos.Row = 0; pos.Row < gridIF->RowNum(); ++pos.Row) for (pos.Col = 0; pos.Col < gridIF->ColNum(); ++pos.Col) gridIF->Value(record, pos, gridIF->MissingValue()); for (cellID = 0; cellID < netIF->CellNum(); ++cellID) { pos = netIF->CellPosition(netIF->Cell(cellID)); switch (header.DataType) { case MFByte: val = (DBFloat) (((char *) data)[cellID]); break; case MFShort: val = (DBFloat) (((short *) data)[cellID]); break; case MFInt: val = (DBFloat) (((int *) data)[cellID]); break; case MFFloat: val = (DBFloat) (((float *) data)[cellID]); break; case MFDouble: val = (DBFloat) (((double *) data)[cellID]); break; } gridIF->Value(record, pos, val); } layerID++; } gridIF->RecalcStats(); } break; } return (DBSuccess); }
int main(int argc, char *argv[]) { int argPos, argNum = argc, ret, verbose = false; DBInt recID, mode = DBTimeStepYear; char *tableName = (char *) NULL; char *srcName = (char *) NULL; char *dstName = (char *) NULL; DBObjData *data; DBObjTable *table; DBObjTableField *srcField; DBObjTableField *dstField = (DBObjTableField *) NULL; DBDate date; DBObjRecord *record; for (argPos = 1; argPos < argNum;) { if (CMargTest (argv[argPos], "-a", "--table")) { if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) { CMmsgPrint(CMmsgUsrError, "Missing table name!"); return (CMfailed); } tableName = argv[argPos]; if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break; continue; } if (CMargTest (argv[argPos], "-f", "--field")) { if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) { CMmsgPrint(CMmsgUsrError, "Missing field name!"); return (CMfailed); } srcName = argv[argPos]; if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break; continue; } if (CMargTest (argv[argPos], "-o", "--outfield")) { if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) { CMmsgPrint(CMmsgUsrError, "Missing output field name!"); return (CMfailed); } dstName = argv[argPos]; if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break; continue; } if (CMargTest (argv[argPos], "-m", "--mode")) { int optCodes[] = {DBTimeStepYear, DBTimeStepMonth}; const char *optStrs[] = {"year", "month", (char *) NULL}; if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) { CMmsgPrint(CMmsgUsrError, "Missing mode!"); return (CMfailed); } if ((mode = CMoptLookup(optStrs, argv[argPos], true)) == DBFault) { CMmsgPrint(CMmsgUsrError, "Invalid decimal date method!"); return (CMfailed); } mode = optCodes[mode]; if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break; continue; } if (CMargTest (argv[argPos], "-V", "--verbose")) { verbose = true; if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break; continue; } if (CMargTest (argv[argPos], "-h", "--help")) { CMmsgPrint(CMmsgInfo, "%s [options] <input file> <output file>", CMfileName(argv[0])); CMmsgPrint(CMmsgInfo, " -a,--table [table name]"); CMmsgPrint(CMmsgInfo, " -f,--field [field name]"); CMmsgPrint(CMmsgInfo, " -o,--outfield [output field]"); CMmsgPrint(CMmsgInfo, " -m,--mode [year|month]"); CMmsgPrint(CMmsgInfo, " -V,--verbose"); CMmsgPrint(CMmsgInfo, " -h,--help"); return (DBSuccess); } if ((argv[argPos][0] == '-') && ((int) strlen(argv[argPos]) > 1)) { CMmsgPrint(CMmsgUsrError, "Unknown option: %s!", argv[argPos]); return (CMfailed); } argPos++; } if (argNum > 3) { CMmsgPrint(CMmsgUsrError, "Extra arguments!"); return (CMfailed); } if (verbose) RGlibPauseOpen(argv[0]); data = new DBObjData(); if (((argNum > 1) && (strcmp(argv[1], "-") != 0) ? data->Read(argv[1]) : data->Read(stdin)) == DBFault) { delete data; return (CMfailed); } if (tableName == (char *) NULL) tableName = DBrNItems; if ((table = data->Table(tableName)) == (DBObjTable *) NULL) { CMmsgPrint(CMmsgUsrError, "Invalid table!"); delete data; return (CMfailed); } if (srcName == (char *) NULL) srcName = (char *) "Date"; if ((srcField = table->Field(srcName)) == (DBObjTableField *) NULL) { CMmsgPrint(CMmsgUsrError, "Missing date field!"); delete data; return (CMfailed); } if ((srcField->Type() != DBTableFieldString) && (srcField->Type() != DBTableFieldDate)) { CMmsgPrint(CMmsgUsrError, "Invalid date field!"); delete data; return (CMfailed); } if (dstName == (char *) NULL) switch (mode) { case DBTimeStepYear: dstName = (char *) "Decimal Year"; break; case DBTimeStepMonth: dstName = (char *) "Decimal Month"; break; } if ((dstField = table->Field(dstName)) == (DBObjTableField *) NULL) { dstField = new DBObjTableField(dstName, DBTableFieldFloat, (char *) "%8.2f", sizeof(DBFloat4), false); table->AddField(dstField); } for (recID = 0; recID < table->ItemNum(); ++recID) { record = table->Item(recID); if (srcField->Type() == DBTableFieldString) date.Set(srcField->String(record)); else date = srcField->Date(record); switch (mode) { case DBTimeStepYear: dstField->Float(record, date.DecimalYear()); break; case DBTimeStepMonth: dstField->Float(record, date.DecimalMonth()); break; } } ret = (argNum > 2) && (strcmp(argv[2], "-") != 0) ? data->Write(argv[2]) : data->Write(stdout); delete data; if (verbose) RGlibPauseClose(); return (ret); }
DBInt RGlibPointInterStationTS(DBObjData *pntData, DBObjData *tsData, char *relateFldName, char *joinFldName) { DBInt first = true, tsIndex, tsRowNum = 0; DBObjTable *pntTBL = pntData->Table(DBrNItems), *tsTBL; DBObjectLIST<DBObjTableField> *fields; DBObjTableField *pntNextFLD = pntTBL->Field(RGlibNextStation); DBObjTableField *pntAreaFLD = pntTBL->Field(RGlibArea); DBObjTableField *pntInterStnFLD = pntTBL->Field(RGlibInterStation); DBObjTableField *pntRelateFLD; DBObjTableField *pntNewNextFLD; DBObjTableField *pntNewInterStnFLD; DBObjTableField *tsTimeFLD; DBObjTableField *tsJoinFLD; DBObjTableField *tsNextStnFLD, *tsInterStnFLD; DBObjRecord *pntRec, *nextPntRec, *tsRec, *tsIndexRec; DBDate curDate, date; if (pntNextFLD == (DBObjTableField *) NULL) { CMmsgPrint(CMmsgUsrError, "Missing Next Station Field!"); return (DBFault); } if (pntAreaFLD == (DBObjTableField *) NULL) { CMmsgPrint(CMmsgUsrError, "Missing STN Area Field!"); return (DBFault); } if (pntInterStnFLD == (DBObjTableField *) NULL) { CMmsgPrint(CMmsgUsrError, "Missing Interfluvial Area Field!"); return (DBFault); } tsTBL = tsData->Table(DBrNItems); tsNextStnFLD = new DBObjTableField(RGlibNextStation, DBTableFieldInt, "%8d", sizeof(DBInt)); tsTBL->AddField(tsNextStnFLD); tsInterStnFLD = new DBObjTableField(RGlibInterStation, DBTableFieldFloat, "%9.1f", sizeof(DBFloat4)); tsTBL->AddField(tsInterStnFLD); fields = tsTBL->Fields(); for (tsTimeFLD = fields->First(); tsTimeFLD != (DBObjTableField *) NULL; tsTimeFLD = fields->Next()) if (tsTimeFLD->Type() == DBTableFieldDate) break; if (tsTimeFLD == (DBObjTableField *) NULL) { CMmsgPrint(CMmsgUsrError, "Missing Date Field!"); return (DBFault); } if ((tsJoinFLD = tsTBL->Field(joinFldName)) == (DBObjTableField *) NULL) { CMmsgPrint(CMmsgUsrError, "Missing Join Field!"); return (DBFault); } fields = new DBObjectLIST<DBObjTableField>("Field List"); fields->Add(new DBObjTableField(*tsTimeFLD)); fields->Add(new DBObjTableField(*tsJoinFLD)); tsTBL->ListSort(fields); delete fields; pntTBL = new DBObjTable(*pntTBL); pntNextFLD = pntTBL->Field(RGlibNextStation); pntAreaFLD = pntTBL->Field(RGlibArea); pntInterStnFLD = pntTBL->Field(RGlibInterStation); pntNewNextFLD = new DBObjTableField("NextStnTS", pntNextFLD->Type(), pntNextFLD->Format(), pntNextFLD->Length()); pntNewInterStnFLD = new DBObjTableField("InterFluTS", pntInterStnFLD->Type(), pntInterStnFLD->Format(), pntInterStnFLD->Length()); pntRelateFLD = pntTBL->Field(relateFldName); pntTBL->AddField(pntNewNextFLD); pntTBL->AddField(pntNewInterStnFLD); pntTBL->ListSort(pntRelateFLD); tsIndexRec = tsTBL->First(&tsIndex); for (tsRec = tsTBL->First(); tsRec != (DBObjRecord *) NULL; tsRec = tsTBL->Next()) { DBPause(tsRowNum++ * 100 / tsTBL->ItemNum()); date = tsTimeFLD->Date(tsRec); if (date != curDate) { if (first) first = false; else { for (pntRec = pntTBL->First(); pntRec != (DBObjRecord *) NULL; pntRec = pntTBL->Next()) { if ((pntRec->Flags() & DBObjectFlagLocked) != DBObjectFlagLocked) continue; for (nextPntRec = pntTBL->Item(pntNextFLD->Int(pntRec) - 1); (nextPntRec != (DBObjRecord *) NULL) && ((nextPntRec->Flags() & DBObjectFlagLocked) != DBObjectFlagLocked); nextPntRec = pntTBL->Item(pntNextFLD->Int(nextPntRec) - 1)); if (nextPntRec != (DBObjRecord *) NULL) { pntNewNextFLD->Int(pntRec, nextPntRec->RowID() + 1); pntNewInterStnFLD->Float(nextPntRec, pntNewInterStnFLD->Float(nextPntRec) - pntAreaFLD->Float(pntRec)); } } pntRec = pntTBL->First(); for (; tsIndexRec != (DBObjRecord *) NULL; tsIndexRec = tsTBL->Next(&tsIndex)) { if (tsRec == tsIndexRec) break; for (; pntRec != (DBObjRecord *) NULL; pntRec = pntTBL->Next()) if (pntRelateFLD->Int(pntRec) == tsJoinFLD->Int(tsIndexRec)) { tsNextStnFLD->Int(tsIndexRec, pntNewNextFLD->Int(pntRec)); tsInterStnFLD->Float(tsIndexRec, pntNewInterStnFLD->Float(pntRec)); break; } if (pntRec == (DBObjRecord *) NULL) pntRec = pntTBL->First(); } } for (pntRec = pntTBL->First(); pntRec != (DBObjRecord *) NULL; pntRec = pntTBL->Next()) { pntNewNextFLD->Int(pntRec, 0); pntNewInterStnFLD->Float(pntRec, pntAreaFLD->Float(pntRec)); pntRec->Flags(DBObjectFlagLocked, DBClear); } curDate = date; pntRec = pntTBL->First(); } for (; pntRec != (DBObjRecord *) NULL; pntRec = pntTBL->Next()) if (pntRelateFLD->Int(pntRec) == tsJoinFLD->Int(tsRec)) { pntRec->Flags(DBObjectFlagLocked, DBSet); break; } if (pntRec == (DBObjRecord *) NULL) pntRec = pntTBL->First(); } if (!first) { for (pntRec = pntTBL->First(); pntRec != (DBObjRecord *) NULL; pntRec = pntTBL->Next()) { if ((pntRec->Flags() & DBObjectFlagLocked) != DBObjectFlagLocked) continue; for (nextPntRec = pntTBL->Item(pntNextFLD->Int(pntRec) - 1); (nextPntRec != (DBObjRecord *) NULL) && ((nextPntRec->Flags() & DBObjectFlagLocked) != DBObjectFlagLocked); nextPntRec = pntTBL->Item(pntNextFLD->Int(nextPntRec) - 1)); if (nextPntRec != (DBObjRecord *) NULL) { pntNewNextFLD->Int(pntRec, nextPntRec->RowID() + 1); pntNewInterStnFLD->Float(nextPntRec, pntNewInterStnFLD->Float(nextPntRec) - pntAreaFLD->Float(pntRec)); } } pntRec = pntTBL->First(); for (; tsIndexRec != (DBObjRecord *) NULL; tsIndexRec = tsTBL->Next(&tsIndex)) { if (tsRec == tsIndexRec) break; for (; pntRec != (DBObjRecord *) NULL; pntRec = pntTBL->Next()) if (pntRelateFLD->Int(pntRec) == tsJoinFLD->Int(tsIndexRec)) { tsNextStnFLD->Int(tsIndexRec, pntNewNextFLD->Int(pntRec)); tsInterStnFLD->Float(tsIndexRec, pntNewInterStnFLD->Float(pntRec)); break; } if (pntRec == (DBObjRecord *) NULL) pntRec = pntTBL->First(); } } delete pntTBL; return (DBSuccess); }