Commit 32dba1626ae897c4ddf79fa51719eaf418614835

Authored by Nathanael Jourdane
1 parent 802f8727
Exists in master

Add the PHP converter.

Showing 1 changed file with 70 additions and 0 deletions   Show diff stats
get_cdf.php 0 → 100755
... ... @@ -0,0 +1,70 @@
  1 +<?php
  2 +
  3 +/**
  4 +* Look for a NetCDF file in the local base dir, then convert it using the NetCDF to CDF Python converter.
  5 +*/
  6 +
  7 +$converterPath = './nc2cdf.py';
  8 +$tmpDir = '/tmp/';
  9 +$log_path = $tmpDir . 'get_cdf.log';
  10 +
  11 +if (!$_GET['id']) {
  12 + exit('INPUT ERROR');
  13 +}
  14 +
  15 +require_once './DDserverWeb_ini.php';
  16 +
  17 +$name = preg_replace('/\..*/', '', $_GET['id']); // ie. ACE/SW/sw19980204
  18 +$inputFilePath = baseDir . $name . '.nc.gz'; // ie. /data/DDBASE/DATA/ACE/SW/sw19980204.nc.gz
  19 +$outputFilePath = $tmpDir . str_replace('/', '_', $name) . '.cdf'; // ie. /tmp/ACE_SW_sw19980204.cdf
  20 +
  21 +function send_file($mime_type, $send_file_path) {
  22 + header('Content-Description: File Transfer');
  23 + header('Content-Type: ' . $mime_type);
  24 + header('Content-Disposition: attachment; filename="' . basename($send_file_path) . '"');
  25 + header('Expires: 0');
  26 + header('Cache-Control: must-revalidate');
  27 + header('Pragma: public');
  28 + header('Content-Length: ' . filesize($send_file_path));
  29 + readfile($send_file_path);
  30 +}
  31 +
  32 +function error($log_msg) {
  33 + global $inputFilePath, $log_path;
  34 + file_put_contents($log_path, date("Y-m-d H:i:s") . " with id='" . $_GET['id'] . "': $log_msg\n", FILE_APPEND);
  35 + // If the file can not be converted, return the NetCDF file.
  36 + send_file('application/x-gzip', basename($inputFilePath));
  37 + exit;
  38 +}
  39 +
  40 +if (!file_exists($inputFilePath)) {
  41 + exit('NO SUCH FILE');
  42 +}
  43 +
  44 +if (!is_dir(pathinfo($outputFilePath, PATHINFO_DIRNAME))) {
  45 + error("Can not create $outputFilePath because the folder does not exist.");
  46 +}
  47 +
  48 +try {
  49 + $res = shell_exec("$converterPath $inputFilePath $outputFilePath");
  50 +} catch (Exception $e) {
  51 + error("Error on shell_exec():\n$e");
  52 +}
  53 +
  54 +
  55 +// The converter is not supposed to output something if the file has been successfully converted
  56 +if(trim($res) != '') {
  57 + error("The converter said:\n$res");
  58 +}
  59 +
  60 +if(! file_exists($outputFilePath)) {
  61 + error("The output file $outputFilePath has not been created.");
  62 +}
  63 +
  64 +send_file('application/x-cdf', $outputFilePath);
  65 +
  66 +sleep(600); // Wait 10 minutes before to delete the converted file.
  67 +unlink($outputFilePath);
  68 +
  69 +exit;
  70 +?>
... ...