Merge pull request #408 from LRossman/lrossman-13

Give external apps access to binary output file (#407)
This commit is contained in:
Lew Rossman
2019-03-06 13:48:58 -05:00
committed by GitHub
5 changed files with 74 additions and 66 deletions

View File

@@ -7,7 +7,7 @@
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 02/08/2019
Last Updated: 03/05/2019
******************************************************************************
*/
@@ -332,24 +332,12 @@ int DLLEXPORT EN_close(EN_Project p)
**----------------------------------------------------------------
*/
{
Outfile *out;
// Free all project data
if (p->Openflag) writetime(p, FMT105);
freedata(p);
// Close output file
out = &p->outfile;
if (out->TmpOutFile != out->OutFile)
{
if (out->TmpOutFile != NULL) fclose(out->TmpOutFile);
out->TmpOutFile = NULL;
}
if (out->OutFile != NULL)
{
fclose(out->OutFile);
out->OutFile = NULL;
}
closeoutfile(p);
// Close input file
if (p->parser.InFile != NULL)
@@ -366,10 +354,10 @@ int DLLEXPORT EN_close(EN_Project p)
}
// Close hydraulics file
if (out->HydFile != NULL)
if (p->outfile.HydFile != NULL)
{
fclose(out->HydFile);
out->HydFile = NULL;
fclose(p->outfile.HydFile);
p->outfile.HydFile = NULL;
}
// Reset system flags
@@ -819,6 +807,7 @@ int DLLEXPORT EN_closeQ(EN_Project p)
if (!p->Openflag) return 102;
closequal(p);
p->quality.OpenQflag = FALSE;
closeoutfile(p);
return 0;
}
@@ -1565,6 +1554,7 @@ int DLLEXPORT EN_setqualtype(EN_Project p, int qualType, char *chemName,
double ccf = 1.0;
if (!p->Openflag) return 102;
if (qual->OpenQflag) return 262;
if (qualType < EN_NONE || qualType > EN_TRACE) return 251;
qual->Qualflag = (char)qualType;
qual->Ctol *= Ucf[QUALITY];

View File

@@ -24,6 +24,7 @@ void freedata(Project *);
int openfiles(Project *, const char *, const char *,const char *);
int openhydfile(Project *);
int openoutfile(Project *);
void closeoutfile(Project *);
int buildadjlists(Network *);
void freeadjlists(Network *);

View File

@@ -7,7 +7,7 @@
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 01/01/2019
Last Updated: 03/05/2019
******************************************************************************
*/
@@ -43,13 +43,18 @@ int openfiles(Project *pr, const char *f1, const char *f2, const char *f3)
pr->report.RptFile = NULL;
pr->outfile.OutFile = NULL;
pr->outfile.HydFile = NULL;
pr->outfile.TmpOutFile = NULL;
// Save file names
strncpy(pr->parser.InpFname, f1, MAXFNAME);
strncpy(pr->report.Rpt1Fname, f2, MAXFNAME);
strncpy(pr->outfile.OutFname, f3, MAXFNAME);
if (strlen(f3) > 0) pr->outfile.Outflag = SAVE;
else pr->outfile.Outflag = SCRATCH;
else
{
pr->outfile.Outflag = SCRATCH;
strcpy(pr->outfile.OutFname, pr->TmpOutFname);
}
// Check that file names are not identical
if (strlen(f1) > 0 && (strcomp(f1, f2) || strcomp(f1, f3))) return 301;
@@ -95,6 +100,7 @@ int openhydfile(Project *pr)
{
if (pr->outfile.Hydflag == SCRATCH) return 0;
fclose(pr->outfile.HydFile);
pr->outfile.HydFile = NULL;
}
// Use Hydflag to determine the type of hydraulics file to use.
@@ -167,30 +173,12 @@ int openoutfile(Project *pr)
int errcode = 0;
// Close output file if already opened
if (pr->outfile.OutFile != NULL)
{
fclose(pr->outfile.OutFile);
pr->outfile.OutFile = NULL;
}
if (pr->outfile.TmpOutFile != NULL)
{
fclose(pr->outfile.TmpOutFile);
pr->outfile.TmpOutFile = NULL;
}
closeoutfile(pr);
// If output file name was supplied, then attempt to
// open it. Otherwise open a temporary output file.
if (pr->outfile.Outflag == SAVE)
{
pr->outfile.OutFile = fopen(pr->outfile.OutFname, "w+b");
if (pr->outfile.OutFile == NULL) errcode = 304;
}
else
{
strcpy(pr->outfile.OutFname, pr->TmpOutFname);
pr->outfile.OutFile = fopen(pr->outfile.OutFname, "w+b");
if (pr->outfile.OutFile == NULL) errcode = 304;
}
// Save basic network data & energy usage results
ERRCODE(savenetdata(pr));
@@ -211,6 +199,33 @@ int openoutfile(Project *pr)
return errcode;
}
void closeoutfile(Project *pr)
/*----------------------------------------------------------------
** Input: none
** Output: none
** Purpose: closes binary output file.
**----------------------------------------------------------------
*/
{
if (pr->outfile.TmpOutFile != pr->outfile.OutFile)
{
if (pr->outfile.TmpOutFile != NULL)
{
fclose(pr->outfile.TmpOutFile);
pr->outfile.TmpOutFile = NULL;
}
}
if (pr->outfile.OutFile != NULL)
{
if (pr->outfile.OutFile == pr->outfile.TmpOutFile)
{
pr->outfile.TmpOutFile = NULL;
}
fclose(pr->outfile.OutFile);
pr->outfile.OutFile = NULL;
}
}
void initpointers(Project *pr)
/*----------------------------------------------------------------
** Input: none

View File

@@ -27,14 +27,7 @@ Last Updated: 11/27/2018
// Stagnant flow tolerance
const double Q_STAGNANT = 0.005 / GPMperCFS; // 0.005 gpm = 1.114e-5 cfs
// Exported functions (declared in FUNCS.H)
//int openqual(Project *);
//void initqual(Project *);
//int runqual(Project *, long *);
//int nextqual(Project *, long *);
//int stepqual(Project *, long *);
//int closequal(Project *);
//double avgqual(Project *, int);
// Exported functions
double findsourcequal(Project *, int, double, long);
// Imported functions
@@ -68,6 +61,9 @@ int openqual(Project *pr)
int errcode = 0;
int n;
// Return if no quality analysis requested
if (qual->Qualflag == NONE) return errcode;
// Build nodal adjacency lists if they don't already exist
if (net->Adjlist == NULL)
{
@@ -401,12 +397,15 @@ int closequal(Project *pr)
Quality *qual = &pr->quality;
int errcode = 0;
if (qual->Qualflag != NONE)
{
if (qual->SegPool) mempool_delete(qual->SegPool);
FREE(qual->FirstSeg);
FREE(qual->LastSeg);
FREE(qual->PipeRateCoeff);
FREE(qual->FlowDir);
FREE(qual->SortedNodes);
}
return errcode;
}

View File

@@ -7,7 +7,7 @@
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 11/27/2018
Last Updated: 03/05/2019
******************************************************************************
*/
@@ -98,12 +98,7 @@ int copyreport(Project* pr, char *filename)
// Copy contents of project's report file
if (rpt->RptFile)
{
c = fgetc(rpt->RptFile);
while (c != EOF)
{
fputc(c, tfile);
c = fgetc(rpt->RptFile);
}
while ((c = fgetc(rpt->RptFile)) != EOF) fputc(c, tfile);
fclose(rpt->RptFile);
}
@@ -539,9 +534,6 @@ int writeresults(Project *pr)
// at each reporting time.
//-----------------------------------------------------------
// Return if no output file
if (outFile == NULL) return 106;
// Return if no nodes or links selected for reporting
// or if no node or link report variables enabled
if (!rpt->Nodeflag && !rpt->Linkflag) return errcode;
@@ -552,6 +544,10 @@ int writeresults(Project *pr)
for (j = LENGTH; j <= FRICTION; j++) nlv += rpt->Field[j].Enabled;
if (nnv == 0 && nlv == 0) return errcode;
// Return if no output file
if (outFile == NULL) outFile = fopen(pr->outfile.OutFname, "rb");
if (outFile == NULL) return 106;
// Allocate memory for output variables:
// m = larger of # node variables & # link variables
// n = larger of # nodes & # links
@@ -592,6 +588,13 @@ int writeresults(Project *pr)
}
}
// Free output file
if (outFile != NULL)
{
fclose(outFile);
outFile = NULL;
}
// Free allocated memory
for (j = 0; j < m; j++) free(x[j]);
free(x);