<commit_before>var config = require("../config/app");
var helpers = require("./helpers")
var exec = require('child_process').exec;
var fs = require('fs');
var request = require('request');
var Archive = require('../fabricator/archive.coffee')

process.on('message', function(message) {
    // Process data

    process.send({id: message.id, data: 'some result'});
});

ShapeWaysClient = require('../fabricator/shapeways.coffee');
swClient = new ShapeWaysClient(
  config.shapewaysAPI.consumerKey,
  config.shapewaysAPI.consumerSecret,
  config.shapewaysAPI.callbackURL
);

/*
 * GET home page.
 */

exports.index = function(req, res){
  res.render('index', {
      title: 'Terrafab',
      version: require("../package.json").version
    }
  );
};

/*
 * GET preview page.
 */

exports.preview = function(req, res){
  res.render('preview', {
      title: 'Preview model',
      cartURL: '/cart?box='+req.query.box,
      version: require("../package.json").version
    }
  );
};


/*
 * GET DTM data.
 */

exports.dtm = function(req, res){

  // Guards
  var dtm_file = config.lib.dtmFile;
  var box = helpers.boxFromParam(req.query.box, res);
  if(!box) {
    return;
  }
  var outsize = helpers.outsizeFromParam(req.query.outsize, res);
  if(!outsize) {
    return;
  }
  if(!helpers.numericParams(outsize.concat(box), res)) {
    return;
  }

  // File generation with gdal_translate
  var out_format;
  var out_extension;
  var out_type;
  var out_scale;
  var fast_pipeline = true;
  switch(req.query.format) {
    case "bin":
      out_extension  = "bin";
      out_format = "ENVI";
      out_type = "UInt16";
      out_scale = "0 2469 0 32767";
      out_options = null
      out_content_type = {'Content-Type': 'application/octet-stream'};
      fast_pipeline = false;
      break;
    default:
      out_extension  = "png";
      out_format = "PNG";
      out_type = "Byte";
      out_scale = "0 2469 0 255";
      out_options = null;
      out_content_type = {'Content-Type': 'image/png'};
  }
  // Set up gdal_translate command
  var out_file = config.files.tmpPath +
    "/"+
    helpers.fileHash(
        "dtm_"+box.join("_")+outsize.join('_'), out_extension);

  var tif_file = out_file.replace('.'+out_extension, '.tif');

  var command = false
  if (fast_pipeline) {
    command = "bash -c 'gdal_translate -q -outsize "+outsize[0]+' '+outsize[1]+" -a_srs EPSG:32633 -scale " + out_scale +
      " -ot "+out_type+" -of "+out_format+" -projwin "+box.join(' ')+' '+dtm_file+' '+out_file+"'"
  } else {
    command = "bash -c '" +
        "GDAL_CACHEMAX=1000 gdalwarp -wm 1000 -s_srs EPSG:32633 -t_srs EPSG:32633" +
        " -r cubic -ts "+ outsize[0] + " " + outsize[1] +
        " -of GTiff " +
        "-te " + box.join(' ') + " " +
        dtm_file + " " + tif_file +
        " && " +
        "gdal_translate -q" +
          " -scale " + out_scale +
          " -ot " + out_type +
          (out_options ? " -co " + out_options + " " : "") +
          " -of " + out_format +
  //      " -outsize " + outsize[0] + " " + outsize[1] +
          " -projwin " + box.join(' ') +
          " " + tif_file + " " + out_file +
        " && rm " + tif_file +"'";
  }
  console.log("Running command:" + command);

  // Output cached file if exists
  if(config.files.cache && fs.existsSync(out_file)) {
    res.writeHead(200, out_content_type);
    var img = fs.readFileSync(out_file);
    res.end(img, 'binary');
  } else {
    // Generate file
    exec(command, function (err, stdout, stderr) {
        if (err) {
          if(stderr && stderr.match(
            'Computed -srcwin falls outside raster size')) {<commit_msg>Robustified temporary tif-file thing<commit_after>var config = require("../config/app");
var helpers = require("./helpers")
var exec = require('child_process').exec;
var fs = require('fs');
var request = require('request');
var Archive = require('../fabricator/archive.coffee')

process.on('message', function(message) {
    // Process data

    process.send({id: message.id, data: 'some result'});
});

ShapeWaysClient = require('../fabricator/shapeways.coffee');
swClient = new ShapeWaysClient(
  config.shapewaysAPI.consumerKey,
  config.shapewaysAPI.consumerSecret,
  config.shapewaysAPI.callbackURL
);

/*
 * GET home page.
 */

exports.index = function(req, res){
  res.render('index', {
      title: 'Terrafab',
      version: require("../package.json").version
    }
  );
};

/*
 * GET preview page.
 */

exports.preview = function(req, res){
  res.render('preview', {
      title: 'Preview model',
      cartURL: '/cart?box='+req.query.box,
      version: require("../package.json").version
    }
  );
};


/*
 * GET DTM data.
 */

exports.dtm = function(req, res){

  // Guards
  var dtm_file = config.lib.dtmFile;
  var box = helpers.boxFromParam(req.query.box, res);
  if(!box) {
    return;
  }
  var outsize = helpers.outsizeFromParam(req.query.outsize, res);
  if(!outsize) {
    return;
  }
  if(!helpers.numericParams(outsize.concat(box), res)) {
    return;
  }

  // File generation with gdal_translate
  var out_format;
  var out_extension;
  var out_type;
  var out_scale;
  var fast_pipeline = true;
  switch(req.query.format) {
    case "bin":
      out_extension  = "bin";
      out_format = "ENVI";
      out_type = "UInt16";
      out_scale = "0 2469 0 32767";
      out_options = null
      out_content_type = {'Content-Type': 'application/octet-stream'};
      fast_pipeline = false;
      break;
    default:
      out_extension  = "png";
      out_format = "PNG";
      out_type = "Byte";
      out_scale = "0 2469 0 255";
      out_options = null;
      out_content_type = {'Content-Type': 'image/png'};
  }
  // Set up gdal_translate command
  var out_file = config.files.tmpPath +
    "/"+
    helpers.fileHash(
        "dtm_"+box.join("_")+outsize.join('_'), out_extension);

  var tif_file = out_file.replace('.'+out_extension, '.tif');

  var command = false
  if (fast_pipeline) {
    command = "bash -c 'gdal_translate -q -outsize "+outsize[0]+' '+outsize[1]+" -a_srs EPSG:32633 -scale " + out_scale +
      " -ot "+out_type+" -of "+out_format+" -projwin "+box.join(' ')+' '+dtm_file+' '+out_file+"'"
  } else {
    command = "bash -c '" +
        "rm "+tif_file+" && GDAL_CACHEMAX=1000 gdalwarp -wm 1000 -s_srs EPSG:32633 -t_srs EPSG:32633" +
        " -r cubic -ts "+ outsize[0] + " " + outsize[1] +
        " -of GTiff " +
        "-te " + box.join(' ') + " " +
        dtm_file + " " + tif_file +
        " && " +
        "gdal_translate -q" +
          " -scale " + out_scale +
          " -ot " + out_type +
          (out_options ? " -co " + out_options + " " : "") +
          " -of " + out_format +
  //      " -outsize " + outsize[0] + " " + outsize[1] +
          " -projwin " + box.join(' ') +
          " " + tif_file + " " + out_file +
        " && rm " + tif_file +"'";
  }
  console.log("Running command:" + command);

  // Output cached file if exists
  if(config.files.cache && fs.existsSync(out_file)) {
    res.writeHead(200, out_content_type);
    var img = fs.readFileSync(out_file);
    res.end(img, 'binary');
  } else {
    // Generate file
    exec(command, function (err, stdout, stderr) {
        if (err) {
          if(stderr && stderr.match(
            'Computed -srcwin falls outside raster size')) {
{'fraction_non_alphanumeric': 0.17, 'fraction_numerical': 0.014794520547945205, 'mean_word_length': 2.6143564356435642, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1626129', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2569, 'n_words': 517}