/*! * numaWriteStream() * * Input: stream, na * Return: 0 if OK, 1 on error */ l_int32 numaWriteStream(FILE *fp, NUMA *na) { l_int32 i, n; l_float32 startx, delx; PROCNAME("numaWriteStream"); if (!fp) return ERROR_INT("stream not defined", procName, 1); if (!na) return ERROR_INT("na not defined", procName, 1); n = numaGetCount(na); fprintf(fp, "\nNuma Version %d\n", NUMA_VERSION_NUMBER); fprintf(fp, "Number of numbers = %d\n", n); for (i = 0; i < n; i++) fprintf(fp, " [%d] = %f\n", i, na->array[i]); fprintf(fp, "\n"); /* Optional data */ numaGetXParameters(na, &startx, &delx); if (startx != 0.0 || delx != 1.0) fprintf(fp, "startx = %f, delx = %f\n", startx, delx); return 0; }
/*! * numaCopyXParameters() * * Input: nad (destination Numa) * nas (source Numa) * Return: 0 if OK, 1 on error */ l_int32 numaCopyXParameters(NUMA *nad, NUMA *nas) { l_float32 start, binsize; PROCNAME("numaCopyXParameters"); if (!nas || !nad) return ERROR_INT("nas and nad not both defined", procName, 1); numaGetXParameters(nas, &start, &binsize); numaSetXParameters(nad, start, binsize); return 0; }
/*! * gplotAddPlot() * * Input: nax (<optional> numa: set to null for Y_VS_I; * required for Y_VS_X) * nay (numa: required for both Y_VS_I and Y_VS_X) * plotstyle (GPLOT_LINES, GPLOT_POINTS, GPLOT_IMPULSES, * GPLOT_LINESPOINTS, GPLOT_DOTS) * plottitle (<optional> title for individual plot) * Return: 0 if OK, 1 on error * * Notes: * (1) There are 2 options for (x,y) values: * o To plot an array vs the index, set nax = NULL. * o To plot one array vs another, use both nax and nay. * (2) If nax is defined, it must be the same size as nay. * (3) The 'plottitle' string can have spaces, double * quotes and backquotes, but not single quotes. */ l_int32 gplotAddPlot(GPLOT *gplot, NUMA *nax, NUMA *nay, l_int32 plotstyle, const char *plottitle) { char buf[L_BUF_SIZE]; char emptystring[] = ""; char *datastr, *title; l_int32 n, i; l_float32 valx, valy, startx, delx; SARRAY *sa; PROCNAME("gplotAddPlot"); if (!gplot) return ERROR_INT("gplot not defined", procName, 1); if (!nay) return ERROR_INT("nay not defined", procName, 1); if (plotstyle != GPLOT_LINES && plotstyle != GPLOT_POINTS && plotstyle != GPLOT_IMPULSES && plotstyle != GPLOT_LINESPOINTS && plotstyle != GPLOT_DOTS) return ERROR_INT("invalid plotstyle", procName, 1); n = numaGetCount(nay); numaGetXParameters(nay, &startx, &delx); if (nax) { if (n != numaGetCount(nax)) return ERROR_INT("nax and nay sizes differ", procName, 1); } /* Save plotstyle and plottitle */ numaAddNumber(gplot->plotstyles, plotstyle); if (plottitle) { title = stringNew(plottitle); sarrayAddString(gplot->plottitles, title, L_INSERT); } else sarrayAddString(gplot->plottitles, emptystring, L_COPY); /* Generate and save data filename */ gplot->nplots++; snprintf(buf, L_BUF_SIZE, "%s.data.%d", gplot->rootname, gplot->nplots); sarrayAddString(gplot->datanames, buf, L_COPY); /* Generate data and save as a string */ sa = sarrayCreate(n); for (i = 0; i < n; i++) { if (nax) numaGetFValue(nax, i, &valx); else valx = startx + i * delx; numaGetFValue(nay, i, &valy); snprintf(buf, L_BUF_SIZE, "%f %f\n", valx, valy); sarrayAddString(sa, buf, L_COPY); } datastr = sarrayToString(sa, 0); sarrayAddString(gplot->plotdata, datastr, L_INSERT); sarrayDestroy(&sa); return 0; }