DrylandEcology / STEPWAT2

folder
4 stars 5 forks source link

Function `_Output_AllCellAvgBmass` creates invalid "csv" output #500

Closed dschlaep closed 4 years ago

dschlaep commented 4 years ago

Function _Output_AllCellAvgBmass adds a sep (currently ",") at the end of each data line (where there should be none),:

        if(BmassFlags.sppb){
            ForEachSpecies(sp){
                sprintf(tbuf, "%f%c", spp[sp], sep);
                strcat(buf, tbuf);
                if(BmassFlags.indv){
                    sprintf(tbuf, "%f%c", indv[sp], sep);
                    strcat(buf, tbuf);
                }
            }
        }
        /* --------------- End generate output string ---------------- */

        fprintf(file, "%s\n", buf); // Finally, print this line

This leads to output that looks like this, e.g.,

Year,PPT,StdDev,Temp, [...] ,chvi,chvi_Indivs,oppo,oppo_Indivs
1,372.750000,106.531296,8.548158, [...], 0.000000,0.000000,0.000000,0.000000,
2,283.750000,36.845398,8.570971, [...], 0.748720,0.250000,0.000000,0.000000,
3,302.000000,47.714428,8.545592, [...], ,0.000000,0.000000,0.823861,0.250000,

This is invalid for the "csv" standard and reading in such output causes trouble, e.g.,

x <- read.csv("Ouptut/g_bmass_cell_avg.csv)

x[1:3, 1:4]
    Year       PPT   StdDev     Temp
1 372.75 106.53130 8.548158 0.152039
2 283.75  36.84540 8.570971 0.112957
3 302.00  47.71443 8.545592 0.145258

x[1:3, 99:102]
  chvi chvi_Indivs oppo oppo_Indivs
1 0.00    0.000000 0.00          NA
2 0.25    0.000000 0.00          NA
3 0.00    0.823861 0.25          NA

Note: Values for columns are shifted, i.e., values of "Year" are dropped (or used as row names) and take on values that belonged to "PPT" instead, values of "PPT" are those of "StdDev", etc. and the last column "oppo_Indivs" has NAs.

All other output functions, e.g., output_Bmass_Yearly, make_header_with_std, and _Output_AllCellAvgMort do not add a superfluous separator at the end of each line.

--> Possible approaches A complication is that every column is optional BmassFlags.