niViz

niViz Git Source Tree

Root/convert.js

1/*
2 * niViz -- snow profiles visualization
3 * Copyright (C) 2015 WSL/SLF - Fluelastrasse 11 - 7260 Davos Dorf - Switzerland.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/* global phantom */
20/* eslint-disable no-shadow */
21
22(function (phantom) {
23 'use strict';
24
25 window.process = {};
26
27 var console = {log: function () {}};
28 var system = require('system');
29 var fs = require('fs');
30 var path = require('path');
31
32 var manifest;
33 var input, inputFilename;
34 var type; //type of graph, e.g. SimpleProfile
35 var outputType; //SVG, PNG or CAAML
36
37 var options = {
38 require: [
39 'application.js'
40 ]
41 };
42
43 var sandbox = function (data, graphType, outputType) {
44 try {
45 niviz.sync = true;
46
47 var canvas = $('<svg id="canvas"></svg>');
48 canvas.height(900);
49 canvas.width(900);
50
51 var station = niviz.parse(data, 'caaml');
52
53 var graph = niviz.draw(graphType, station, canvas);
54
55 niviz.convert(outputType, graph, function (data, error) {
56 window.callPhantom(data);
57 });
58
59 } catch (error) {
60 window.console.log(error.message);
61 }
62 };
63
64 function render(callback) {
65 var page = require('webpage').create();
66
67 page.settings.userAgent = 'PhantomJS';
68
69 page.onConsoleMessage = function (message) {
70 //window.console.log(message);
71 };
72
73 page.onCallback = function (data) {
74
75 if (outputType === 'PNG') {
76 var outputFile = path.basename(inputFilename);
77 outputFile = outputFile.substr(0, outputFile.lastIndexOf('.')) + '.png';
78
79 var b = [].map.call(data, function(v){
80 return String.fromCharCode(v);
81 }).join("");
82
83 fs.write(outputFile, b, "wb");
84 callback('PNG image written to: ' + outputFile);
85 } else {
86 callback(data);
87 }
88
89 };
90
91 page.open('about:blank', function (status) {
92 var i, ii, js;
93
94 for (i = 0, ii = options.require.length; i < ii; ++i) {
95 js = options.require[i];
96
97 page.injectJs(assetpath(js)) ||
98 fail('Failed to inject ', js);
99 }
100
101 var svg = page.evaluate(sandbox, input, type, outputType);
102 });
103 }
104
105 function fail() {
106 window.console.error.apply(window.console, arguments);
107 phantom.exit();
108 }
109
110 function usage() {
111 window.console.log('Usage: convert.js <manifest> <file> [SimpleProfile|SLFProfile|StructureProfile] [SVG|PNG|CAAML]');
112 phantom.exit();
113 }
114
115 function assetpath(name) {
116 return [
117 manifest.root,
118 manifest.assets[name]
119 ].join('');
120 }
121
122 function parse(args) {
123 try {
124 if (!fs.isFile(args[0]))
125 return fail('Manifest file not found: ', args[0]);
126
127 if (!fs.isFile(args[1]))
128 return fail('Input file not found: ', args[1]);
129
130 manifest = JSON.parse(fs.read(args[0]));
131 manifest.root = args[0].replace(/[^\/]+$/, '');
132
133 inputFilename = args[1];
134 input = fs.read(args[1]);
135
136 type = args[2] || 'SimpleProfile';
137
138 outputType = args[3] || 'SVG';
139
140 return true;
141
142 } catch (error) {
143 fail('Failed to parse options: %s', error.message);
144 }
145 }
146
147 if (system.args.length < 3)
148 return usage();
149
150 if (parse(system.args.slice(1)))
151 render(function (result) {
152 window.console.log(result);
153 phantom.exit();
154 });
155
156
157}(phantom));

Archive Download this file

Branches