Generates the Gnuplot script and data files. @param basepath The base path to use. A number of new files will be created and their names will all start with this string. @return The number of data points sent to Gnuplot. This can be less than the number of data points involved in the query due to
(final String basepath)
| 201 | * @throws IOException if there was an error while writing one of the files. |
| 202 | */ |
| 203 | public int dumpToFiles(final String basepath) throws IOException { |
| 204 | int npoints = 0; |
| 205 | final int nseries = datapoints.size(); |
| 206 | final String datafiles[] = nseries > 0 ? new String[nseries] : null; |
| 207 | FileSystem.checkDirectory(new File(basepath).getParent(), |
| 208 | Const.MUST_BE_WRITEABLE, Const.CREATE_IF_NEEDED); |
| 209 | for (int i = 0; i < nseries; i++) { |
| 210 | datafiles[i] = basepath + "_" + i + ".dat"; |
| 211 | final PrintWriter datafile = new PrintWriter(datafiles[i]); |
| 212 | try { |
| 213 | for (final DataPoint d : datapoints.get(i)) { |
| 214 | final long ts = d.timestamp() / 1000; |
| 215 | if (d.isInteger()) { |
| 216 | datafile.print(ts + utc_offset); |
| 217 | datafile.print(' '); |
| 218 | datafile.print(d.longValue()); |
| 219 | } else { |
| 220 | final double value = d.doubleValue(); |
| 221 | |
| 222 | if (Double.isInfinite(value)) { |
| 223 | // Infinity is invalid. |
| 224 | throw new IllegalStateException("Infinity found in" |
| 225 | + " datapoints #" + i + ": " + value + " d=" + d); |
| 226 | } else if (Double.isNaN(value)) { |
| 227 | // NaNs should be skipped. |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | datafile.print(ts + utc_offset); |
| 232 | datafile.print(' '); |
| 233 | datafile.print(value); |
| 234 | } |
| 235 | |
| 236 | datafile.print('\n'); |
| 237 | |
| 238 | if (ts >= (start_time & UNSIGNED) && ts <= (end_time & UNSIGNED)) { |
| 239 | npoints++; |
| 240 | } |
| 241 | } |
| 242 | } finally { |
| 243 | datafile.close(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (npoints == 0) { |
| 248 | // Gnuplot doesn't like empty graphs when xrange and yrange aren't |
| 249 | // entirely defined, because it can't decide on good ranges with no |
| 250 | // data. We always set the xrange, but the yrange is supplied by the |
| 251 | // user. Let's make sure it defines a min and a max. |
| 252 | params.put("yrange", "[0:10]"); // Doesn't matter what values we use. |
| 253 | } |
| 254 | writeGnuplotScript(basepath, datafiles); |
| 255 | return npoints; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Generates the Gnuplot script. |