Beispiel #1
0
void RGISAnNetworkToGridCBK (Widget widget,RGISWorkspace *workspace,XmAnyCallbackStruct *callData)

	{
	char *selection;
	DBInt dataType;
	DBDataset *dataset = UIDataset ();
	DBObjData *netData = dataset->Data ();
	DBObjTable *cellTable = netData->Table (DBrNCells);
	DBObjTableField *field;
	DBObjData *grdData;
	static Widget select = NULL;

	if (select == NULL) select = UISelectionCreate ((char *) "Field Selection");
	selection = UISelectObject (select,(DBObjectLIST<DBObject> *) (cellTable->Fields ()),DBTableFieldIsSimple);
	if (selection == (char *) NULL) return;
	if ((field = cellTable->Field (selection)) == (DBObjTableField *) NULL)
		{ CMmsgPrint (CMmsgAppError,"Corrupt Network Dataset in: %s %d",__FILE__,__LINE__); return; }

	if (DBTableFieldIsCategory (field) && DBTableFieldIsNumeric (field))
		{
		DBObjData *tmpData;
		tmpData = new DBObjData ("",DBTypeGrid);
		tmpData->Document (DBDocGeoDomain,netData->Document (DBDocGeoDomain));
		tmpData->Document (DBDocSubject,field->Name ());
		if (UIDataHeaderForm (tmpData))
			{
			grdData = DBNetworkToGrid (netData,tmpData->Type ());
			grdData->Name (tmpData->Name ());
			grdData->Document (DBDocGeoDomain,tmpData->Document (DBDocGeoDomain));
			grdData->Document (DBDocSubject,tmpData->Document (DBDocSubject));
			grdData->Document (DBDocVersion,tmpData->Document (DBDocVersion));
			grdData->Document (DBDocCitationRef,tmpData->Document (DBDocCitationRef));
			grdData->Document (DBDocCitationInst,tmpData->Document (DBDocCitationInst));
			grdData->Document (DBDocSourceInst,tmpData->Document (DBDocSourceInst));
			grdData->Document (DBDocSourcePerson,tmpData->Document (DBDocSourcePerson));
			grdData->Document (DBDocOwnerPerson,tmpData->Document (DBDocOwnerPerson));
			grdData->Document (DBDocComment,tmpData->Document (DBDocComment));
			delete tmpData;
			}
		else { return; }
		}
	else
		{
		if (DBTableFieldIsCategory (field))		dataType = DBTypeGridDiscrete;
		else if (DBTableFieldIsNumeric (field))	dataType = DBTypeGridContinuous;
		else
			{
			CMmsgPrint (CMmsgAppError,"Invalid field type in: %s %d",__FILE__,__LINE__);
			return;
			}
		grdData = DBNetworkToGrid (netData,dataType);
		if (UIDataHeaderForm (grdData) == false) { delete grdData; return; }
		}

	UIPauseDialogOpen ((char *) "Converting Network to Grid");
	if (RGlibNetworkToGrid (netData,field,grdData) == DBSuccess) workspace->CurrentData (grdData);
	else delete grdData;
	UIPauseDialogClose ();
	}
Beispiel #2
0
int main (int argc,char *argv [])

	{
	int argPos, argNum = argc, ret, verbose = false;
	char *title      = (char *) NULL,   *subject = (char *) NULL;
	char *domain     = (char *) NULL,   *version = (char *) NULL;
	char *fieldName  = (char *) NULL;
	DBInt shadeSet   = DBFault;
	DBInt gridType   = DBTypeGridContinuous;
	DBObjData  *netData, *grdData;
	DBObjTable *cellTable;
	DBObjTableField *field;

	for (argPos = 1;argPos < argNum; )
		{
		if (CMargTest (argv [argPos],"-f","--fieldname"))
			{
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing discharge field!"); return (CMfailed); }
			fieldName = argv [argPos];
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest (argv [argPos],"-g","--gridtype"))
			{
			int codes [] = { DBTypeGridContinuous, DBTypeGridDiscrete };
			const char *options [] = { "continuous", "discrete", (char *) NULL };

			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError, "Missing correcition mode!"); return (CMfailed); }
			if ((gridType = CMoptLookup (options, argv [argPos],true)) == DBFault)
				{ CMmsgPrint (CMmsgUsrError, "Invalid correction mode!");  return (CMfailed); }
			gridType = codes [gridType];
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest (argv [argPos],"-t","--title"))
			{
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing title!");        return (CMfailed); }
			title = argv [argPos];
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest (argv [argPos],"-u","--subject"))
			{
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing subject!");      return (CMfailed); }
			subject = argv [argPos];
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest (argv [argPos],"-d","--domain"))
			{
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing domain!");       return (CMfailed); }
			domain  = argv [argPos];
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest (argv [argPos],"-v","--version"))
			{
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing version!");      return (CMfailed); }
			version  = argv [argPos];
			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if ((gridType == DBTypeGridContinuous) && CMargTest (argv [argPos],"-s","--shadeset"))
			{
			int codes [] = { DBDataFlagDispModeContStandard,
			                 DBDataFlagDispModeContGreyScale,
			                 DBDataFlagDispModeContBlueScale,
			                 DBDataFlagDispModeContBlueRed,
			                 DBDataFlagDispModeContElevation };
			const char *options [] = { "standard", "grey", "blue", "blue-to-red", "elevation", (char *) NULL };

			if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing shadeset!");     return (CMfailed); }
			if ((shadeSet = CMoptLookup (options,argv [argPos],true)) == DBFault)
				{ CMmsgPrint (CMmsgUsrError,"Invalid shadeset!");     return (CMfailed); }
			shadeSet = codes [shadeSet];
			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 network> <output grid>",CMprgName(argv[0]));
			CMmsgPrint (CMmsgInfo,"     -f,--fieldname   [field name]");
			CMmsgPrint (CMmsgInfo,"     -g,--gridtype    [continuous|discrete]");
			CMmsgPrint (CMmsgInfo,"     -t,--title       [dataset title]");
			CMmsgPrint (CMmsgInfo,"     -u,--subject     [subject]");
			CMmsgPrint (CMmsgInfo,"     -d,--domain      [domain]");
			CMmsgPrint (CMmsgInfo,"     -v,--version     [version]");
			CMmsgPrint (CMmsgInfo,"     -s,--shadeset    [standard|grey|blue|blue-to-red|elevation]");
			CMmsgPrint (CMmsgInfo,"     -V,--verbose");
			CMmsgPrint (CMmsgInfo,"     -h,--help");
			return (DBSuccess);
			}
		if ((argv [argPos][0] == '-') && (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]);

	if (fieldName == (char *) NULL)
		{ CMmsgPrint (CMmsgUsrError,"Missing field name!"); return (CMfailed); }
	if ((gridType == DBTypeGridDiscrete) && (shadeSet != DBFault))
		{
		shadeSet = DBFault;
		CMmsgPrint (CMmsgUsrError,"Ignoring shadeset for discrete grid!");
		}

	netData = new DBObjData ();
	ret = (argNum > 1) && (strcmp (argv [1],"-") != 0) ? netData->Read (argv [1]) : netData->Read (stdin);
	if ((ret == DBFault) || (netData->Type () != DBTypeNetwork))
		{
		CMmsgPrint (CMmsgUsrError,"Non-network coverage!");
		delete netData;
		return (CMfailed);
		}
	cellTable = netData->Table (DBrNCells);
	if ((field = cellTable->Field (fieldName)) == (DBObjTableField *) NULL)
		{
		CMmsgPrint (CMmsgUsrError,"Non-existing field [%s]!",fieldName);
		delete netData;
		return (CMfailed);
		}
	if ((gridType == DBTypeGridDiscrete) && !DBTableFieldIsCategory (field))
		{
		CMmsgPrint (CMmsgUsrError,"Non-category field");
		delete netData;
		return (CMfailed);
		}
	else if (!DBTableFieldIsNumeric  (field))
		{
		CMmsgPrint (CMmsgUsrError,"Non-numeric field");
		delete netData;
		return (CMfailed);
		}

	if (title	== (char *) NULL)   title = (char *) "Converted Grid";
	if (subject == (char *) NULL) subject = (char *) "fieldName";
	if (domain	== (char *) NULL)  domain = netData->Document (DBDocGeoDomain);
	if (version == (char *) NULL) version = (char *) "0.01pre";

	grdData = DBNetworkToGrid (netData,gridType);
	grdData->Name (title);
	grdData->Document (DBDocSubject,subject);
	grdData->Document (DBDocGeoDomain,domain);
	grdData->Document (DBDocVersion,version);
	if (shadeSet != DBFault)
		{
		grdData->Flags (DBDataFlagDispModeContShadeSets,DBClear);
		grdData->Flags (shadeSet,DBSet);
		}

	if ((ret = RGlibNetworkToGrid (netData,field,grdData)) == DBSuccess)
		ret = (argNum > 2) && (strcmp (argv [2],"-") != 0) ? grdData->Write (argv [2]) : grdData->Write (stdout);

	delete netData; delete grdData;
	if (verbose) RGlibPauseClose ();
	return (ret);
	}
Beispiel #3
0
int main (int argc,char *argv [])
	{
	int argPos, argNum = argc, numGrpNames = 0, i = 0;
	char **groupnames, *rename = (char *) NULL, *tableName = (char *) NULL;
	bool ascii = false;
	FieldOptions *head = (FieldOptions *) NULL, *p = (FieldOptions *) NULL, *temp = (FieldOptions *) NULL;
	Groups **groups = (Groups **) NULL;
	FILE *outFile = (FILE *) NULL;
	DBObjData *inData, *outData;
	DBObjTable *inTable, *outTable;
	DBObjTableField *field;
	DBObjRecord *inRecord, *outRecord;
	DBObjectLIST<DBObjTableField> *fields;

	if(argc <= 2) { doHelp(false,argv[0]); return(DBSuccess); }
	outData = new DBObjData("Untitled", DBTypeTable);
	outTable = outData->Table(DBrNItems);

	head = new FieldOptions(BAD,"","", (FieldOptions *) NULL);
	groupnames = (char **) malloc(sizeof(char *));

	for (argPos = 1;argPos < argNum;)
		{
		if (CMargTest(argv[argPos],"-f","--field"))
			{
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing operation and field after -f!"); return (CMfailed); }
			if(!strcmp(argv[argPos],"pct"))
				{
				if (argNum <= argPos + 2)
					{ CMmsgPrint (CMmsgUsrError,"Missing field and/or percentage after -f pct!"); return (CMfailed); }
				p = FOHierarchy(argv[argPos],argv[argPos+1],rename,atoi(argv[argPos+2]),head);
				argNum = CMargShiftLeft(argPos,argv,argNum);
				argNum = CMargShiftLeft(argPos,argv,argNum);
				}
			else if(!strcmp(argv[argPos],"num"))
				{
				char *num = new char[4];
				strcpy(num,"Num");
				p = FOHierarchy(argv[argPos],num,rename,-1,head);
				}
			else
				{
				if (argNum < argPos + 1)
					{ CMmsgPrint (CMmsgUsrError,"Missing operation or field after -f %s!",argv[argPos]); return (CMfailed); }
				p = FOHierarchy(argv[argPos],argv[argPos+1],rename,-1,head);
				argNum = CMargShiftLeft(argPos,argv,argNum);
				}
			p->setPrint(true);
			rename = (char *) NULL;
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest(argv[argPos],"-g","--group"))
			{
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing groupname!"); return (CMfailed); }
			if((groupnames = (char **) realloc(groupnames,(numGrpNames + 1) * sizeof(char *))) == (char **) NULL)
				{ CMmsgPrint (CMmsgSysError, "Memory allocation error in: %s %d",__FILE__,__LINE__); return(DBFault); }
			groupnames[numGrpNames] = argv[argPos];
			numGrpNames++;
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest(argv[argPos],"-h","--help"))
			{
			argNum = CMargShiftLeft (argPos,argv,argNum);
			if(CMargTest(argv[argPos],"e","extend"))
				{
				doHelp(true,argv[0]);
				argNum = CMargShiftLeft (argPos,argv,argNum);
				}
			else doHelp(false,argv[0]);
			}
		if (CMargTest(argv[argPos],"-c","--ascii"))
			{
			ascii = true;
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		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],"-r","--rename"))
			{
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing field after -r!"); return (CMfailed); }
			rename = argv[argPos];
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest(argv[argPos],"-o","--output"))
			{
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing output filename!"); return (CMfailed); }
			if((outFile = fopen(argv[argPos],"w")) == (FILE *) NULL)
				{ CMmsgPrint (CMmsgUsrError,"Cannot open file %s",argv[argPos]); return (CMfailed); }
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest(argv[argPos],"-t","--title"))
			{
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing title!"); return (CMfailed); }
			outData->Name(argv[argPos]);
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest(argv[argPos],"-s","--subject"))
			{
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing subject!"); return (CMfailed); }
			outData->Document(DBDocSubject,argv[argPos]);
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest(argv[argPos],"-d","--domain"))
			{
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing domain!"); return (CMfailed); }
			outData->Document(DBDocGeoDomain,argv[argPos]);
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if (CMargTest(argv[argPos],"-v","--version"))
			{
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos)
				{ CMmsgPrint (CMmsgUsrError,"Missing version!"); return (CMfailed); }
			outData->Document(DBDocVersion,argv[argPos]);
			if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break;
			continue;
			}
		if ((argv[argPos][0] == '-') && (strlen (argv[argPos]) > 1))
			{ CMmsgPrint (CMmsgUsrError, "Unknown option: %s!",argv[argPos]); return (CMfailed); }
		argPos++;
		}

	if(outFile == (FILE *) NULL) outFile = stdout;
	if(head->next == (FieldOptions *) NULL) return(DBSuccess);

	inData = new DBObjData();
	if ((argNum > 1) && (strcmp(argv[1],"-") != 0)) inData->Read(argv[1]);
	else inData->Read(stdin);

	if (outData->Name() == (char *) NULL) outData->Name("Untitled");
	if (outData->Document(DBDocSubject) == (char *) NULL) outData->Document(DBDocSubject,inData->Document(DBDocSubject));
	if (outData->Document(DBDocGeoDomain) == (char *) NULL) outData->Document(DBDocGeoDomain,inData->Document(DBDocGeoDomain));
	if (outData->Document(DBDocVersion) == (char *) NULL) outData->Document(DBDocVersion,inData->Document(DBDocVersion));

	if(tableName == (char *) NULL) tableName = DBrNItems;
	if((inTable = inData->Table(tableName)) == (DBObjTable *) NULL)
		{ CMmsgPrint (CMmsgUsrError,"Invalid table!"); delete inData; return (CMfailed); }

	if((groups = (Groups **) malloc(numGrpNames * sizeof(Groups *))) == (Groups **) NULL)
		{ CMmsgPrint (CMmsgSysError, "Memory allocation error in: %s %d",__FILE__,__LINE__); return(DBFault); }
	for(i = 0; i < numGrpNames; i++)
		{
		if((field = inTable->Field(groupnames[i])) == (DBObjTableField *) NULL)
			{ CMmsgPrint (CMmsgUsrError, "Invalid group name: %s",groupnames[i]); return (CMfailed); }
		if(DBTableFieldIsCategory(field))
			{
			groups[i] = new Groups();
			groups[i]->srcFLD = field;
			groups[i]->dstFLD = new DBObjTableField(*field);
			outTable->AddField(groups[i]->dstFLD);
//			CMmsgPrint (CMmsgUsrError, "Added Group: %s",groups[i]->dstFLD->Name());
			}
		else CMmsgPrint (CMmsgUsrError, "Group %s is not Category!",groupnames[i]);
		}
	delete groupnames;

	p = head->next;
	temp = head;
	while(p->next)
		{
		FieldOptions *duplicate = (FieldOptions *) NULL, *prev = p;
		if(!p->getPrint() && FLDExists(p,p->getOldName(),p->getFunc(),&duplicate))
			{ temp->next = p->next; delete p; p = temp->next; continue; }
		while(FLDExists(prev,prev->getOldName(),prev->getFunc(),&prev,&duplicate) && !duplicate->getPrint())
			{ prev->next = duplicate->next; delete duplicate; }
		temp = p;
		p = p->next;
		}
//	p = head->next;
//	while(p) { CMmsgPrint (CMmsgUsrError, "Added: o:%s n:%s p:",p->getOldName(),p->getNewName()); if(p->getPrint()) CMmsgPrint (CMmsgUsrError, "true"); else CMmsgPrint (CMmsgUsrError, "false"); p = p->next; }

	fields = inTable->Fields();
	p = head->next;
	while(p)
		{
		field = fields->Item (p->getOldName());
		if (p->getFunc() == MIN || p->getFunc() == MAX)
			p->field = new DBObjTableField(p->getNewName(),field->Type(),field->Format(),field->Length());
		else if(p->getFunc() == NUM || p->getFunc() == NONNULL)
			p->field = new DBObjTableField(p->getNewName(), DBTableFieldInt, DBHiddenField, sizeof (DBInt));
		else p->field = new DBObjTableField(p->getNewName(), DBTableFieldFloat, DBHiddenField, sizeof(DBFloat));
		if(p->getFunc() != NUM && p->getFunc() != NONNULL)
			{
//			if ((field = inTable->Field(p->getOldName())) == (DBObjTableField *) NULL)
			if (field == (DBObjTableField *) NULL)
				{
				CMmsgPrint (CMmsgUsrError, "Invalid field name: %s",p->getOldName());
				return(DBFault);
				}
			if (!DBTableFieldIsNumeric(field))
				{
				CMmsgPrint (CMmsgUsrError, "Field is not Numeric: %s",p->getOldName());
				return(DBFault);
				}
			}
		outTable->AddField(p->field);
		p = p->next;
		}
// MAKE SURE TO TEST FOR SPEED BY DECLARING INTS OUTSIDE OF FOR LOOPS!!!

	for (int inRecID = 0;inRecID < inTable->ItemNum();++inRecID)
		{
		inRecord = inTable->Item(inRecID);
		if ((outRecord = FindMatch(inRecord, outTable,(const Groups**) groups, numGrpNames)) != (DBObjRecord *) NULL)
			{
			p = head->next;
			while(p)
				{
				field = fields->Item (p->getOldName());
				switch(p->getFunc())
					{
					default:
						break;
					case NUM:
						p->field->Int(outRecord,p->field->Int(outRecord) + 1);
						break;
					case NONNULL:
						if (p->isInt())
							{
							if (field->Int(inRecord) != field->IntNoData()) p->field->Int(outRecord,p->field->Int(outRecord) + 1);
							}
						else
							{
							if (!CMmathEqualValues(field->Float(inRecord),field->FloatNoData()))
								p->field->Int(outRecord,p->field->Int(outRecord) + 1);
							}
						break;
					case MIN:
						if (p->isInt())
							{
							if(field->Int(inRecord) != field->IntNoData())
								{
								if (p->field->Int(outRecord) != p->field->IntNoData())
									{ if (field->Int(inRecord) < p->field->Int(outRecord)) p->field->Int(outRecord,field->Int(inRecord)); }
								else { p->field->Int(outRecord,field->Int(inRecord)); }
								}
							}
						else
							{
							if (!CMmathEqualValues(field->Float(inRecord),field->FloatNoData()))
								{
								if (!CMmathEqualValues(p->field->Float(inRecord),p->field->FloatNoData()))
									{ if (field->Float(inRecord) < p->field->Float(outRecord)) p->field->Float(outRecord,field->Float(inRecord)); }
								else { p->field->Float(outRecord,field->Float(inRecord)); }
								}
							}
						break;
					case MAX:
						if (p->isInt())
							{
							if(field->Int(inRecord) != field->IntNoData())
								{
								if (p->field->Int(outRecord) != p->field->IntNoData())
									{ if (field->Int(inRecord) > p->field->Int(outRecord)) p->field->Int(outRecord,field->Int(inRecord)); }
								else { p->field->Int(outRecord,field->Int(inRecord)); }
								}
							}
						else
							{
							if (!CMmathEqualValues(field->Float(inRecord),field->FloatNoData()))
								{
								if (!CMmathEqualValues(p->field->Float(inRecord),p->field->FloatNoData()))
									{ if (field->Float(inRecord) > p->field->Float(outRecord)) p->field->Float(outRecord,field->Float(inRecord)); }
								else { p->field->Float(outRecord,field->Float(inRecord)); }
								}
							}
						break;
					case SUM:
						if (p->isInt())
							{
							if(field->Int(inRecord) != field->IntNoData())
								p->field->Int(outRecord,p->field->Int(outRecord) + field->Int(inRecord));
							}
							else
							{
							if (!CMmathEqualValues(field->Float(inRecord),field->FloatNoData()))
								p->field->Float(outRecord,p->field->Float(outRecord) + field->Float(inRecord));
							}
						break;
					case DEV:
					case PCT:
					case MED:
						p->tailVal = p->tailVal->next = new Values();
						p->tailVal->val = field->Float(inRecord);
						p->tailVal->next = 0;
						break;
					case MOD:
						Values *cur = p->getHead();
						while(cur->next && !CMmathEqualValues(cur->val,field->Float(inRecord))) cur = cur->next;
						if(cur->next) cur->occur++;
						else
							{
							p->tailVal->val = field->Float(inRecord);
							p->tailVal->occur = 1;
							p->tailVal = p->tailVal->next = new Values();
							}
						break;
					}
				p = p->next;
				}
			}
		else
			{
			outRecord = outTable->Add();

			for(i = 0; i < numGrpNames; i++)
				{
				switch (groups[i]->srcFLD->Type())
					{
					default:
					case DBTableFieldString:
						groups[i]->dstFLD->String(outRecord,groups[i]->srcFLD->String(inRecord));
						break;
					case DBTableFieldInt:
						groups[i]->dstFLD->Int(outRecord,groups[i]->srcFLD->Int(inRecord));
						break;
					}
				}
			p = head->next;
			while(p)
				{
				field = fields->Item (p->getOldName());
				switch(p->getFunc())
					{
					default:
					case BAD:
						break;
					case NUM:
						p->setInt();
						p->field->Int(outRecord,1);
						break;
					case NONNULL:
						if (field->Type() == DBTableFieldInt)
							{
							p->setInt();
							if (field->Int(inRecord) != field->IntNoData()) p->field->Int(outRecord,1);
							else p->field->Int(outRecord,0);
							}
						else
							{
							if (!CMmathEqualValues(field->Float(inRecord),field->FloatNoData())) p->field->Int(outRecord,1);
							else p->field->Int(outRecord,0);
							}
						break;
					case MIN:
						if (field->Type() == DBTableFieldInt)
							{ p->setInt(); if (field->Int(inRecord) != field->IntNoData()) p->field->Int(outRecord,field->Int(inRecord)); }
						else
							{
							if(!CMmathEqualValues(field->Float(inRecord),field->FloatNoData()))
								p->field->Float(outRecord,field->Float(inRecord));
							}
						break;
					case MAX:
						if (field->Type() == DBTableFieldInt)
							{ p->setInt(); if (field->Int(inRecord) != field->IntNoData()) p->field->Int(outRecord,field->Int(inRecord)); }
						else
							{
							if(!CMmathEqualValues(field->Float(inRecord),field->FloatNoData()))
								p->field->Float(outRecord,field->Float(inRecord));
							}
						break;
					case SUM:
						if (field->Type() == DBTableFieldInt)
							{
							p->setInt();
							if (field->Int(inRecord) != field->IntNoData()) p->field->Int(outRecord,field->Int(inRecord));
							else p->field->Int(outRecord,0);
							}
						else
							{
							if(!CMmathEqualValues(field->Float(inRecord),field->FloatNoData())) p->field->Float(outRecord,field->Float(inRecord));
							else p->field->Float(outRecord,0.0);
							}
						break;
					case DEV:
					case PCT:
					case MED:
						p->tailVal = p->tailVal->next = new Values();
						p->tailVal->val = field->Float(inRecord);
						p->tailVal->next = 0;
						break;
					case MOD:
						p->tailVal->val = field->Float(inRecord);
						p->tailVal->occur = 1;
						p->tailVal = p->tailVal->next = new Values();
						break;
					}
				p = p->next;
				}
			}
		}

	for(int outRecID = 0; outRecID < outTable->ItemNum();++outRecID)
		{
		outRecord = outTable->Item(outRecID);
		p = head->next;
		FieldOptions *sum, *num;
		DBFloat mod;
		int occurrance;
		float i;
		bool mult;
		Values *cur;
		while(p)
			{
			field = fields->Item (p->getOldName());
			switch(p->getFunc())
				{
				case AVG:
					if((FLDExists(head,p->getOldName(),SUM,&sum)) && (FLDExists(head,p->getOldName(),NONNULL,&num)))
						p->field->Float(outRecord,sum->field->Float(outRecord) / (DBFloat) (num->field->Int(outRecord)));
					else CMmsgPrint (CMmsgUsrError, "Program Error! Could not find SUM or NONNULL in linked list! Please contact the GHAAS developers group!");
					break;
				case NAVG:
					if((FLDExists(head,p->getOldName(),SUM,&sum)) && (FLDExists(head,"NUM",NUM,&num)))
						p->field->Float(outRecord,sum->field->Float(outRecord) / (DBFloat) (num->field->Int(outRecord)));
					else CMmsgPrint (CMmsgUsrError, "Program Error! Could not find SUM or NUM in linked list! Please contact the GHAAS developers group!");
					break;
				case PCT:
				case MED:
					i = (float) (inTable->ItemNum() * p->getHead()->occur * 0.01) - 1;
					cur = p->getHead()->next;
					while(i > 0) { cur = cur->next; i--; }
					p->field->Float(outRecord,(cur->val + cur->next->val) / 2);
					break;
				case MOD:
					mod = 0.0;
					occurrance = 0;
					mult = false;
					cur = p->getHead();
					while(cur)
						{
						if(cur->occur > occurrance)
							{
							mod = cur->val;
							occurrance = cur->occur;
							mult = false;
							}
						else if(cur->occur == occurrance) mult = true;
						cur = cur->next;
						}
					if(mult) CMmsgPrint (CMmsgUsrError, "** Warning, multiple answers for MOD, listing first found!");
					p->field->Float(outRecord,mod);
					break;
				default:
					break;
				}
			p = p->next;
			}
		}

	p = head->next;
	while(p)
		{
		if(p->getFunc() == DEV)
			{
			FieldOptions *avg, *num;
			field = fields->Item (p->getOldName());
			if((FLDExists(head,p->getOldName(),AVG,&avg)) && (FLDExists(head,"NUM",NUM,&num)))
				{
				for(int outRecID = 0; outRecID < outTable->ItemNum();++outRecID)
					{
					outRecord = outTable->Item(outRecID);
					DBFloat sum = 0.0;
					Values *cur = p->getHead()->next;
					while(cur)
						{
//						sum += (DBFloat) (pow((cur->val - avg->field->Float(outRecord)),2));
						DBFloat add = (cur->val - avg->field->Float(outRecord));
						sum += (DBFloat) (add * add);
						cur = cur->next;
						}
					sum = sqrt(sum /(DBFloat) num->field->Int(outRecord));
					p->field->Float(outRecord,sum);
					}
				}
			else CMmsgPrint (CMmsgUsrError, "Program Error! Could not find AVG or NUM in linked list! Please contact the GHAAS developers group!");
			}
		p = p->next;
		}
// DELETE unnecessary fields which were for temp storage
	fields = outTable->Fields();
	p = head->next;
	while(p)
		{
		if(!p->getPrint()) outTable->DeleteField(fields->Item(p->getNewName()));
		p = p->next;
		}

	p = head->next;
	while(p)
		{
		if ((strcmp(p->field->Format(),DBHiddenField) == 0) && p->getPrint())
			{
			if(p->isInt())
				{
				int maxval = 0;
				for(int outRecID = 0; outRecID < outTable->ItemNum();++outRecID)
					{
					int val = p->field->Int(outTable->Item(outRecID));
					if (maxval < val) maxval = val; else if (maxval < -val) maxval = -val;
					}
				p->field->Format(DBMathIntAutoFormat(maxval));
				}
			else
				{
				float maxval = 0.0;
				for(int outRecID = 0; outRecID < outTable->ItemNum();++outRecID)
					{
					float val = p->field->Float(outTable->Item(outRecID));
					if (maxval < val) maxval = val; else if (maxval < -val) maxval = -val;
					}
				p->field->Format(DBMathFloatAutoFormat(maxval));
				}
			}
//		CMmsgPrint (CMmsgUsrError, "Format(%s)%d,%d: '%s'",p->getFuncName(),p->isInt(),p->getPrint(),p->field->Format());
		p = p->next;
		}

	DBObjectLIST<DBObjTableField> *list = new DBObjectLIST<DBObjTableField> ("Sorted Field List");
	for(i = 0; i < numGrpNames; i++) list->Add(groups[i]->dstFLD);
	outTable->ListSort(list);

if (ascii) DBExportASCIITable(outTable,outFile); else outData->Write(outFile);

/* CLEANUP ************************************************/
	if(outFile != stdout) fclose(outFile);
	for(i = 0; i < numGrpNames; i++) delete groups[i]->dstFLD;
	delete groups;
	p = head;
	while(p)
		{
		head = head->next;
		delete p;
		p = head;
		}
	return(DBSuccess);

}