Commit 4119d51d authored by Mario Morales Hernandez's avatar Mario Morales Hernandez
Browse files

Fix SWMM node output unit labels and data bugs

This fixes critical bugs in the custom report_Nodes_TRITON() function
that was causing unit label mismatches and data inconsistencies between
the .rpt file and individual node output files.

Bug fixes:
- Fix reversed unit labels: US units were showing metric labels (M3/S, M)
 and metric units were showing imperial labels (FT3/S, FT)
- Fix wrong output index: Was using sequential 'k' counter instead of
 Node[j].rptFlag - 1, causing data to be read from wrong nodes
- Only create output files for nodes where rptFlag is set

Changes:
- Use FlowUnitWords[FlowUnits] for correct flow unit labels (CFS, CMS)
- Use proper conditional (UnitSystem == US ? "FT" : "M") for length units
- Use k = Node[j].rptFlag - 1 for correct binary output file index
- Move rptFlag check before file creation to avoid unnecessary empty files
- Add NULL check for file opening
- Fix inconsistent indentation
parent c7e5f32f
Loading
Loading
Loading
Loading
+49 −35
Original line number Diff line number Diff line
@@ -1307,9 +1307,10 @@ void report_Nodes()

void report_Nodes_TRITON(const char *f1) // NEW
//
//  Input:   none
//  Input:   f1 = prefix for output file names
//  Output:  none
//  Purpose: writes results for selected nodes to report file with an ASCII format.
//  Purpose: writes results for selected nodes to individual ASCII files.
//           TRITON-SWMM coupling function.
//
{
    int      j, p, k;
@@ -1323,10 +1324,15 @@ void report_Nodes_TRITON(const char *f1) // NEW
    char     filename[1024];
    TExtInflow* inflow;
    FILE *outfile;
    const char* flowUnits;
    const char* lenUnits;

    if ( Nobjects[NODE] == 0 ) return;

    k = 0;
    // Get the correct unit labels from SWMM's unit system
    flowUnits = FlowUnitWords[FlowUnits];  // CFS, CMS, etc.
    lenUnits = (UnitSystem == US) ? "FT" : "M";

    for (j = 0; j < Nobjects[NODE]; j++)
    {
        inflow = Node[j].extInflow;
@@ -1334,21 +1340,29 @@ void report_Nodes_TRITON(const char *f1) // NEW
        {
            if ( inflow->type == FLOW_INFLOW )
            {
					sprintf(filename,"%snode%d.out",f1,j+1);	// Write the numbering in the file name with the corresponding extension.
					outfile=fopen(filename,"w");
                // Only create output file if this node's report flag is set
                if ( Node[j].rptFlag != FALSE )
                {
                    // Get the correct output index for this node
                    // (rptFlag is 1-based, so subtract 1 to get 0-based index)
                    k = Node[j].rptFlag - 1;

                    sprintf(filename,"%snode%d.out",f1,j+1);	// File naming: node1.out, node2.out, etc.
                    outfile = fopen(filename,"w");
                    if (outfile != NULL)
                    {
                        // Write header with correct unit labels
                        fprintf(outfile, "NODE %s\n", Node[j].ID);
						 if (UnitSystem == US){
						 	fprintf(outfile, "Time (s) INFLOW (M3/S) OVERFLOW (M3/S) DEPTH (M), HEAD(M) DATE TIME\n");
						 }else{
						 	fprintf(outfile, "Time (s) INFLOW (FT3/S) OVERFLOW (FT3/S) DEPTH (FT), HEAD(FT) DATE TIME\n");	
						 }
                        fprintf(outfile, "Time (s) Inflow (%s) Overflow (%s) Depth (%s) Head (%s) Date Time\n",
                            flowUnits, flowUnits, lenUnits, lenUnits);

                        // Write time series data
                        for ( period = 1; period <= Nperiods; period++ )
                        {
                            output_readDateTime(period, &currentTime);
                            datetime_dateToStr(currentTime, theDate);
                            datetime_timeToStr(currentTime, theTime);
                            // Read results using the correct node index
                            output_readNodeResults(period, k);

                            datetime_decodeTime(currentTime, &hour, &min, &sec);
@@ -1360,9 +1374,9 @@ void report_Nodes_TRITON(const char *f1) // NEW
                                NodeResults[NODE_HEAD], theDate, theTime);
                        }
                        fprintf(outfile, "\n");
						 k++;
					}
                        fclose(outfile);
                    }
                }
                break;

            }