Merge pull request #812 from Mariosmsk/add-EN_loadpatternfile
Add the `EN_loadpatternfile` function to load pattern data from a `.pat` file into INP
This commit is contained in:
@@ -328,7 +328,7 @@ extern "C" {
|
|||||||
********************************************************************/
|
********************************************************************/
|
||||||
|
|
||||||
int DLLEXPORT ENaddpattern(const char *id);
|
int DLLEXPORT ENaddpattern(const char *id);
|
||||||
|
|
||||||
int DLLEXPORT ENdeletepattern(int index);
|
int DLLEXPORT ENdeletepattern(int index);
|
||||||
|
|
||||||
int DLLEXPORT ENgetpatternindex(const char *id, int *index);
|
int DLLEXPORT ENgetpatternindex(const char *id, int *index);
|
||||||
@@ -346,6 +346,8 @@ extern "C" {
|
|||||||
int DLLEXPORT ENgetaveragepatternvalue(int index, EN_API_FLOAT_TYPE *value);
|
int DLLEXPORT ENgetaveragepatternvalue(int index, EN_API_FLOAT_TYPE *value);
|
||||||
|
|
||||||
int DLLEXPORT ENsetpattern(int index, EN_API_FLOAT_TYPE *values, int len);
|
int DLLEXPORT ENsetpattern(int index, EN_API_FLOAT_TYPE *values, int len);
|
||||||
|
|
||||||
|
int DLLEXPORT ENloadpatternfile(const char *filename, const char *id);
|
||||||
|
|
||||||
/********************************************************************
|
/********************************************************************
|
||||||
|
|
||||||
|
|||||||
@@ -1478,6 +1478,15 @@ typedef struct Project *EN_Project;
|
|||||||
use @ref EN_setpatternvalue to revise pattern factors one at a time.
|
use @ref EN_setpatternvalue to revise pattern factors one at a time.
|
||||||
*/
|
*/
|
||||||
int DLLEXPORT EN_setpattern(EN_Project ph, int index, double *values, int len);
|
int DLLEXPORT EN_setpattern(EN_Project ph, int index, double *values, int len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Loads time patterns from a file into a project under a specific pattern ID.
|
||||||
|
@param ph an EPANET project handle.
|
||||||
|
@param filename the name of the file containing pattern data.
|
||||||
|
@param id the ID name of the new pattern to load.
|
||||||
|
@return an error code.
|
||||||
|
*/
|
||||||
|
int DLLEXPORT EN_loadpatternfile(EN_Project p, const char *filename, const char *id);
|
||||||
|
|
||||||
/********************************************************************
|
/********************************************************************
|
||||||
|
|
||||||
|
|||||||
64
src/epanet.c
64
src/epanet.c
@@ -4514,6 +4514,70 @@ int DLLEXPORT EN_addpattern(EN_Project p, const char *id)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int DLLEXPORT EN_loadpatternfile(EN_Project p, const char *filename, const char *id)
|
||||||
|
/*----------------------------------------------------------------
|
||||||
|
** Input: filename = name of the file containing pattern data
|
||||||
|
** id = ID for the new pattern
|
||||||
|
** Output: none
|
||||||
|
** Returns: error code
|
||||||
|
** Purpose: loads time patterns from a file into a project under a specific pattern ID
|
||||||
|
**----------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
FILE *file;
|
||||||
|
char line[MAXLINE+1];
|
||||||
|
char *tok;
|
||||||
|
int err = 0;
|
||||||
|
int i;
|
||||||
|
int len = 0;
|
||||||
|
double value;
|
||||||
|
double *values = NULL;
|
||||||
|
int CHUNK = 50;
|
||||||
|
|
||||||
|
if (!p->Openflag) return 102;
|
||||||
|
|
||||||
|
file = fopen(filename, "r");
|
||||||
|
if (file == NULL) return 302;
|
||||||
|
|
||||||
|
// Add a new pattern or use an existing pattern.
|
||||||
|
err = EN_getpatternindex(p, id, &i);
|
||||||
|
if (err == 205) {
|
||||||
|
if ((err = EN_addpattern(p, id)) != 0) {
|
||||||
|
fclose(file);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
i = p->network.Npats;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read pattern values
|
||||||
|
while (fgets(line, sizeof(line), file) != NULL) {
|
||||||
|
|
||||||
|
// Skip lines that don't contain valid numbers
|
||||||
|
tok = strtok(line, SEPSTR);
|
||||||
|
if (tok == NULL) continue;
|
||||||
|
if (!getfloat(tok, &value)) continue;
|
||||||
|
|
||||||
|
// Resize multiplier array if it's full
|
||||||
|
if (len % CHUNK == 0) {
|
||||||
|
values = (double *) realloc(values, (len + CHUNK) * sizeof(double));
|
||||||
|
|
||||||
|
// Abort if memory allocation error
|
||||||
|
if (values == NULL) {
|
||||||
|
fclose(file);
|
||||||
|
return 101;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
values[len] = value;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
// Transfer multipliers to pattern
|
||||||
|
err = EN_setpattern(p, i, values, len);
|
||||||
|
free(values);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
int DLLEXPORT EN_deletepattern(EN_Project p, int index)
|
int DLLEXPORT EN_deletepattern(EN_Project p, int index)
|
||||||
/*----------------------------------------------------------------
|
/*----------------------------------------------------------------
|
||||||
** Input: index = index of the pattern to delete
|
** Input: index = index of the pattern to delete
|
||||||
|
|||||||
@@ -676,6 +676,11 @@ int DLLEXPORT ENsetpattern(int index, EN_API_FLOAT_TYPE *values, int len)
|
|||||||
return errcode;
|
return errcode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int DLLEXPORT ENloadpatternfile(const char *filename, const char *id)
|
||||||
|
{
|
||||||
|
return EN_loadpatternfile(_defaultProject, filename, id);
|
||||||
|
}
|
||||||
|
|
||||||
/********************************************************************
|
/********************************************************************
|
||||||
|
|
||||||
Data Curve Functions
|
Data Curve Functions
|
||||||
|
|||||||
Reference in New Issue
Block a user