MFDomain_p MFDomainRead (FILE *inFile) { int objID, i; MFDomain_p domain; if ((domain = (MFDomain_p) calloc (1,sizeof (MFDomain_t))) == (MFDomain_p) NULL) return ((MFDomain_p) NULL); domain->Objects = (MFObject_p) NULL; if (fread (domain,sizeof (MFDomain_t) - sizeof (MFObject_p),1,inFile) != 1) { CMmsgPrint (CMmsgSysError,"File Reading Error in: %s:%d",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_p) NULL); } if (domain->Swap != 1) { MFSwapHalfWord (&(domain->Type)); MFSwapWord (&(domain->ObjNum)); } if ((domain->Objects = (MFObject_p) calloc (domain->ObjNum,sizeof (MFObject_t))) == (MFObject_p) NULL) { CMmsgPrint (CMmsgSysError,"Memory Allocation Error in: %s:%d",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_p) NULL); } for (objID = 0;objID < domain->ObjNum;++objID) { domain->Objects [objID].DLinks = (size_t *) NULL; domain->Objects [objID].ULinks = (size_t *) NULL; domain->Objects [objID].DWeights = (float *) NULL; domain->Objects [objID].UWeights = (float *) NULL; } for (objID = 0;objID < domain->ObjNum;++objID) { if (fread (domain->Objects + objID,sizeof (MFObject_t) - 2 * sizeof (MFObject_p),1,inFile) != 1) { CMmsgPrint (CMmsgSysError,"File Reading Error in: %s:%d",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_p) NULL); } if (domain->Swap != 1) { MFSwapWord (&(domain->Objects [objID].ID)); MFSwapHalfWord (&(domain->Objects [objID].DLinkNum)); MFSwapHalfWord (&(domain->Objects [objID].ULinkNum)); MFSwapWord (&(domain->Objects [objID].XCoord)); MFSwapWord (&(domain->Objects [objID].YCoord)); MFSwapWord (&(domain->Objects [objID].Lon)); MFSwapWord (&(domain->Objects [objID].Lat)); MFSwapWord (&(domain->Objects [objID].Area)); MFSwapWord (&(domain->Objects [objID].Length)); } if (domain->Objects [objID].DLinkNum > 0) { domain->Objects [objID].DLinks = (size_t *) calloc (domain->Objects [objID].DLinkNum,sizeof (size_t *)); if (domain->Objects [objID].DLinks == (size_t *) NULL) { CMmsgPrint (CMmsgSysError,"Memory Allocation Error in: %s:%d",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_p) NULL); } domain->Objects [objID].DWeights = (float *) calloc (domain->Objects [objID].DLinkNum,sizeof (float)); if (domain->Objects [objID].DWeights == (float*) NULL) { CMmsgPrint (CMmsgSysError,"Memory Allocation Error in: %s:%d",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_p) NULL); } if (fread (domain->Objects [objID].DLinks,sizeof (size_t),domain->Objects [objID].DLinkNum,inFile) == domain->Objects [objID].DLinkNum) { if (domain->Swap != 1) for (i = 0;i < domain->Objects [objID].DLinkNum; ++i) MFSwapWord (domain->Objects [objID].DLinks + i); for (i = 0; i < domain->Objects [objID].DLinkNum; ++i) { // assume network originates with single downlink. if (i>1) CMmsgPrint(CMmsgSysError, "Bifurcation code assumption of single inital downlink WRONG at %s:%d",__FILE__,__LINE__); domain->Objects[objID].DWeights[i] = 1.0; } } else { CMmsgPrint (CMmsgSysError,"File Reading Error in: %s:%d",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_p) NULL); } } if (domain->Objects [objID].ULinkNum > 0) { domain->Objects [objID].ULinks = (size_t *) calloc (domain->Objects [objID].ULinkNum,sizeof (size_t)); if (domain->Objects [objID].ULinks == (size_t *) NULL) { CMmsgPrint (CMmsgSysError,"Memory Allocation Error in: %s:%d",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_p) NULL); } domain->Objects [objID].UWeights = (float *) calloc (domain->Objects [objID].ULinkNum,sizeof (float)); if (domain->Objects [objID].UWeights == (float*) NULL) { CMmsgPrint (CMmsgSysError,"Memory Allocation Error in: %s:%d",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_p) NULL); } if (fread (domain->Objects [objID].ULinks,sizeof (size_t),domain->Objects [objID].ULinkNum,inFile) == domain->Objects [objID].ULinkNum) { if (domain->Swap != 1) for (i = 0;i < domain->Objects [objID].ULinkNum; ++i) MFSwapWord (domain->Objects [objID].ULinks + i); for (i = 0; i < domain->Objects [objID].ULinkNum; ++i) { // Weights refer to fraction of UPSTREAM CELL outflow. So all the UWeights could be more or less than 1, but all the DWeights for a cell sum to 1. // again, assuming single downlinks, so initial weight of each downlink is 1 domain->Objects[objID].UWeights[i] = 1.0; } } else { CMmsgPrint (CMmsgSysError,"File Reading Error in: %s:%d",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_p) NULL); } } } return (domain); }
MFDomain_t *MFDomainGet (FILE *inFile) { int objID, i; MFDomain_t *domain; if ((domain = (MFDomain_t *) calloc (1,sizeof (MFDomain_t))) == (MFDomain_t *) NULL) return ((MFDomain_t *) NULL); domain->Objects = (MFObject_t *) NULL; if (fread (domain,sizeof (MFDomain_t) - sizeof (MFObject_t *),1,inFile) != 1) { CMmsgPrint (CMmsgSysError,"File Reading Error in: %s:%d\n",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_t *) NULL); } if (domain->Swap != 1) { MFSwapHalfWord (&(domain->Type)); MFSwapWord (&(domain->ObjNum)); } if ((domain->Objects = (MFObject_t *) calloc (domain->ObjNum,sizeof (MFObject_t))) == (MFObject_t *) NULL) { CMmsgPrint (CMmsgSysError,"Memory Allocation Error in: %s:%d\n",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_t *) NULL); } for (objID = 0;objID < domain->ObjNum;++objID) { domain->Objects [objID].DLinks = (size_t *) NULL; domain->Objects [objID].ULinks = (size_t *) NULL; } for (objID = 0;objID < domain->ObjNum;++objID) { if (fread (domain->Objects + objID,sizeof (MFObject_t) - 2 * sizeof (MFObject_t *),1,inFile) != 1) { CMmsgPrint (CMmsgSysError,"File Reading Error in: %s:%d\n",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_t *) NULL); } if (domain->Swap != 1) { MFSwapWord (&(domain->Objects [objID].ID)); MFSwapHalfWord (&(domain->Objects [objID].DLinkNum)); MFSwapHalfWord (&(domain->Objects [objID].ULinkNum)); MFSwapWord (&(domain->Objects [objID].XCoord)); MFSwapWord (&(domain->Objects [objID].YCoord)); MFSwapWord (&(domain->Objects [objID].Lon)); MFSwapWord (&(domain->Objects [objID].Lat)); MFSwapWord (&(domain->Objects [objID].Area)); MFSwapWord (&(domain->Objects [objID].Length)); } if (domain->Objects [objID].DLinkNum > 0) { domain->Objects [objID].DLinks = (size_t *) calloc (domain->Objects [objID].DLinkNum,sizeof (size_t *)); if (domain->Objects [objID].DLinks == (size_t *) NULL) { CMmsgPrint (CMmsgSysError,"Memory Allocation Error in: %s:%d\n",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_t *) NULL); } if (fread (domain->Objects [objID].DLinks,sizeof (size_t),domain->Objects [objID].DLinkNum,inFile) == domain->Objects [objID].DLinkNum) { if (domain->Swap != 1) for (i = 0;i < domain->Objects [objID].DLinkNum; ++i) MFSwapWord (domain->Objects [objID].DLinks + i); } else { CMmsgPrint (CMmsgSysError,"File Reading Error in: %s:%d\n",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_t *) NULL); } } if (domain->Objects [objID].ULinkNum > 0) { domain->Objects [objID].ULinks = (size_t *) calloc (domain->Objects [objID].ULinkNum,sizeof (size_t)); if (domain->Objects [objID].ULinks == (size_t *) NULL) { CMmsgPrint (CMmsgSysError,"Memory Allocation Error in: %s:%d\n",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_t *) NULL); } if (fread (domain->Objects [objID].ULinks,sizeof (size_t),domain->Objects [objID].ULinkNum,inFile) == domain->Objects [objID].ULinkNum) { if (domain->Swap != 1) for (i = 0;i < domain->Objects [objID].ULinkNum; ++i) MFSwapWord (domain->Objects [objID].ULinks + i); } else { CMmsgPrint (CMmsgSysError,"File Reading Error in: %s:%d\n",__FILE__,__LINE__); MFDomainFree (domain); return ((MFDomain_t *) NULL); } } } return (domain); }
int main (int argc,char *argv []) { FILE *outFile; DBInt argPos, argNum = argc, ret; int objID, size; DBFloat lCorrection = 1.0; MFDomain_t *domain = (MFDomain_t *) NULL; DBCoordinate coord; DBObjRecord *objRec; DBObjData *data; for (argPos = 1;argPos < argNum; ) { if (CMargTest (argv [argPos],"-l","--lengthcorrection")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) { CMmsgPrint (CMmsgUsrError,"Missing length correction!"); return (CMfailed); } if (sscanf (argv [argPos],"%lf", &lCorrection) != 1) { CMmsgPrint (CMmsgUsrError, "Invalid length correction!"); return (CMfailed); } if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-h","--help")) { CMmsgPrint (CMmsgInfo,"%s [options] <input rgisdata> <output domain>",CMprgName(argv[0])); CMmsgPrint (CMmsgInfo," -l,--lengthcorrection"); 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); } outFile = (argNum > 2) && (strcmp (argv [2],"-") != 0) ? fopen (argv [2],"w") : stdout; if (outFile == (FILE *) NULL) { CMmsgPrint (CMmsgUsrError,"Output file Opening error in: %s",CMprgName(argv[0])); exit (DBFault); } data = new DBObjData (); ret = (argNum > 1) && (strcmp (argv [1],"-") != 0) ? data->Read (argv [1]) : data->Read (stdin); if ((domain = (MFDomain_t *) calloc (1,sizeof (MFDomain_t))) != (MFDomain_t *) NULL) { domain->Objects = (MFObject_t *) NULL; switch (data->Type ()) { case DBTypeVectorPoint: { DBVPointIF *pntIF = new DBVPointIF (data); domain->ObjNum = pntIF->ItemNum (); if ((domain->Objects = (MFObject_t *) calloc (domain->ObjNum,sizeof (MFObject_t))) == (MFObject_t *) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); MFDomainFree (domain); goto Stop; } for (objID = 0;objID < domain->ObjNum;++objID) { objRec = pntIF->Item (objID); coord = pntIF->Coordinate (objRec); domain->Objects [objID].ID = objRec->RowID (); domain->Objects [objID].DLinkNum = 0; domain->Objects [objID].ULinkNum = 0; domain->Objects [objID].DLinks = (size_t *) NULL; domain->Objects [objID].ULinks = (size_t *) NULL; domain->Objects [objID].XCoord = domain->Objects [objID].Lon = coord.X; domain->Objects [objID].YCoord = domain->Objects [objID].Lat = coord.Y; domain->Objects [objID].Area = 0.0; domain->Objects [objID].Length = 0.0; } } break; case DBTypeGridContinuous: case DBTypeGridDiscrete: { } break; case DBTypeNetwork: { DBInt dir; DBObjRecord *nextCell; DBNetworkIF *netIF = new DBNetworkIF (data); domain->ObjNum = netIF->CellNum (); if ((domain->Objects = (MFObject_t *) calloc (domain->ObjNum,sizeof (MFObject_t))) == (MFObject_t *) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); MFDomainFree (domain); goto Stop; } for (objID = 0;objID < domain->ObjNum;++objID) { domain->Objects [objID].DLinks = (size_t *) NULL; domain->Objects [objID].ULinks = (size_t *) NULL; } for (objID = 0;objID < domain->ObjNum;++objID) { objRec = netIF->Cell (objID); coord = netIF->Center (objRec); domain->Objects [objID].ID = objRec->RowID (); domain->Objects [objID].DLinkNum = 0; domain->Objects [objID].ULinkNum = 0; domain->Objects [objID].XCoord = domain->Objects [objID].Lon = coord.X; domain->Objects [objID].YCoord = domain->Objects [objID].Lat = coord.Y; domain->Objects [objID].Area = netIF->CellArea (objRec); domain->Objects [objID].Length = netIF->CellLength (objRec) *lCorrection; if ((nextCell = netIF->ToCell (objRec)) != (DBObjRecord *) NULL) { size = (domain->Objects [objID].DLinkNum + 1) * sizeof (size_t); if ((domain->Objects [objID].DLinks = (size_t *) realloc (domain->Objects [objID].DLinks,size)) == (size_t *) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); MFDomainFree (domain); goto Stop; } domain->Objects [objID].DLinks [domain->Objects [objID].DLinkNum] = nextCell->RowID (); domain->Objects [objID].DLinkNum++; } for (dir = 0;dir < 8;++dir) if ((nextCell = netIF->FromCell (objRec,0x01 << dir)) != (DBObjRecord *) NULL) { size = (domain->Objects [objID].ULinkNum + 1) * sizeof (size_t); if ((domain->Objects [objID].ULinks = (size_t *) realloc (domain->Objects [objID].ULinks,size)) == (size_t *) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); MFDomainFree (domain);goto Stop; } domain->Objects [objID].ULinks [domain->Objects [objID].ULinkNum] = nextCell->RowID (); domain->Objects [objID].ULinkNum++; } } } break; } ret = MFDomainWrite (domain,outFile); } Stop: if (outFile != stdout) fclose (outFile); return (ret); }