~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to api/lib/helioviewer/API.php

  • Committer: V. Keith Hughitt
  • Date: 2009-05-29 18:04:54 UTC
  • Revision ID: hughitt1@kore-20090529180454-1q1r4pxa3nw881os
nightly build 2009-05-29: fixed uri field performance issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/**
3
3
 * @package Helioviewer API
4
4
 * @author Keith Hughitt <Vincent.K.Hughitt@nasa.gov>
 
5
 *
 
6
 * TODO: Move JP2 Image Series functionality to ImageSeries class
5
7
 */
6
8
/**
7
9
 * @package Helioviewer API
168
170
        $imgIndex = new ImgIndex(new DbConnection());
169
171
 
170
172
        // find the closest image
171
 
        $queryForField = 'abbreviation';
172
173
        foreach(array('observatory', 'instrument', 'detector', 'measurement') as $field) {
173
 
            $src["$field.$queryForField"] = $this->params[$field];
 
174
            $src["$field.abbreviation"] = $this->params[$field];
174
175
        }
175
176
 
176
177
        // file name and location
177
 
        $filename = $imgIndex->getJP2Location($this->params['timestamp'], $src);
 
178
        $filename = $imgIndex->getJP2Filename($this->params['timestamp'], $src);
178
179
        $filepath = $this->getFilepath($filename);
179
180
 
180
181
        // regex for URL construction
203
204
            $contents = fread($fp, filesize($filepath));
204
205
 
205
206
            echo $contents;
206
 
             fclose($fp);
 
207
            fclose($fp);
207
208
        }
208
209
 
209
210
        return 1;
211
212
 
212
213
    /**
213
214
     * @return int Returns "1" if the action was completed successfully.
 
215
     * 
 
216
     *  Converting timestamp to a PHP DateTime:
 
217
     *     $dt = new DateTime("@$startTime");
 
218
     *     echo $dt->format("U");
 
219
     *     date_add($dt, new DateInterval("T" . $cadence . "S"));
 
220
     *  (See http://us2.php.net/manual/en/function.date-create.php)
214
221
     */
215
222
    private function _getJP2ImageSeries () {
 
223
        $startTime   = $this->params['startTime'];
 
224
        $endTime     = $this->params['endTime'];
 
225
        $cadence     = $this->params['cadence'];
 
226
        $format      = $this->params['format'];
 
227
        
 
228
        $observatory = $this->params['observatory'];
 
229
        $instrument  = $this->params['instrument'];
 
230
        $detector    = $this->params['detector'];
 
231
        $measurement = $this->params['measurement'];
 
232
 
 
233
        // Create a temporary directory to store image-series (TODO: Move this + other directory creation to installation script)
 
234
        $tmpdir = Config::TMP_ROOT_DIR . "/movies/";
 
235
        if (!file_exists($tmpdir)) {
 
236
            mkdir($tmpdir);
 
237
            chmod($tmpdir, 0777);
 
238
        }
 
239
 
 
240
        // Filename
 
241
        $filename = implode("_", array($observatory, $instrument, $detector, $measurement, "F$startTime", "T$endTime", "B$cadence")) . "." . strtolower($format);
 
242
        
 
243
        // Filepath
 
244
        $filepath = "$tmpdir" . $filename;
 
245
 
 
246
        // URL
 
247
        $url = Config::TMP_ROOT_URL . "/movies/" . $filename;
 
248
 
 
249
        // If the file doesn't exist already, create it
 
250
        if (!file_exists($filepath)) {
 
251
            $this->buildJP2ImageSeries($filepath);
 
252
        }
 
253
 
 
254
        // Output the file/jpip URL
 
255
        if ((isset($this->params['getJPIP'])) && ($this->params['getJPIP'] == "true")) {
 
256
            $webRootRegex = "/" . preg_replace("/\//", "\/", Config::WEB_ROOT_DIR) . "/";
 
257
            $mj2 = "jpip" . substr(preg_replace($webRootRegex, Config::WEB_ROOT_URL, $url), 4);
 
258
            echo $mj2;
 
259
        } else {
 
260
            echo $url;
 
261
        }
 
262
        return 1;
 
263
    }
 
264
    
 
265
    /**
 
266
     * @param string The filename to use
 
267
     * Constructs a JPX/MJ2 image series
 
268
     */
 
269
    private function buildJP2ImageSeries ($output_file) {
 
270
        //date_default_timezone_set('UTC');
216
271
        require_once('ImgIndex.php');
217
 
        //date_default_timezone_set('UTC');
218
272
        
219
 
        $startTime = $this->params['startTime'];
220
 
        $endTime   = $this->params['endTime'];
221
 
        $cadence   = $this->params['cadence'];
222
 
        $format    = $this->params['format'];
 
273
        $startTime   = $this->params['startTime'];
 
274
        $endTime     = $this->params['endTime'];
 
275
        $cadence     = $this->params['cadence'];
 
276
        $format      = $this->params['format'];
223
277
 
224
278
        // Layer information
225
279
        foreach(array('observatory', 'instrument', 'detector', 'measurement') as $field) {
232
286
        // Determine number of frames to grab
233
287
        $timeInSecs = $endTime - $startTime;
234
288
        $numFrames  = min(Config::MAX_MOVIE_FRAMES, ceil($timeInSecs / $cadence));
235
 
 
236
 
        // Convert timestamp to a PHP DateTime (See http://us2.php.net/manual/en/function.date-create.php)
237
 
        //$dt = new DateTime("@$startTime");
238
 
        //echo $dt->format("U");
239
 
        //date_add($dt, new DateInterval("T" . $cadence . "S"));
240
 
 
 
289
        
 
290
        // Timer
241
291
        $time = $startTime;
242
292
 
243
293
        $images = array();
244
294
 
245
295
        // Get nearest JP2 images to each time-step
246
296
        for ($i = 0; $i < $numFrames; $i++) {
247
 
            $jp2 = $this->getFilepath($imgIndex->getJP2Location($time, $src));
248
 
            //$url = preg_replace($this->web_root_url_regex, $this->web_root_dir, $url);
 
297
            $jp2 = $this->getFilepath($imgIndex->getJP2Filename($time, $src));
249
298
            array_push($images, $jp2);
250
299
            $time += $cadence;
251
300
        }
259
308
        // Drop trailing comma
260
309
        $cmd = substr($cmd, 0, -1);
261
310
 
262
 
        // Create a temporary directory to store image-series
263
 
        $now = time();
264
 
        $tmpdir = Config::TMP_ROOT_DIR . "/jp2-image-series/";
265
 
        if (!file_exists($tmpdir)) {
266
 
            mkdir($tmpdir);
267
 
            chmod($tmpdir, 0777);
268
 
        }
269
 
 
270
 
        $tmpdir .= "/$now/";
271
 
        if (!file_exists($tmpdir)) {
272
 
            mkdir($tmpdir);
273
 
            chmod($tmpdir, 0777);
274
 
        }
275
 
 
276
 
        $filename = "jhv_image_series." . strtolower($format);
277
 
        $tmpurl = Config::TMP_ROOT_URL . "/jp2-image-series/$now/" . $filename;
278
 
        $output_file = "$tmpdir" . $filename;
279
 
 
280
311
        $cmd .= " -o $output_file";
281
 
 
282
 
        // MJ2
 
312
    
 
313
        // MJ2 Creation
283
314
        if ($format == "MJ2")
284
315
            $cmd .= " -mj2_tracks P:0-@25";
285
 
 
 
316
    
286
317
        // Execute kdu_merge command
287
318
        exec('export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:' . Config::KDU_LIBS_DIR . "; " . escapeshellcmd($cmd), $output, $return);
288
319
 
289
 
        //echo $cmd;
290
 
 
291
 
        if ((isset($this->params['getJPIP'])) && ($this->params['getJPIP'] == "true")) {
292
 
            $webRootRegex = "/" . preg_replace("/\//", "\/", Config::WEB_ROOT_DIR) . "/";
293
 
            $mj2 = "jpip" . substr(preg_replace($webRootRegex, Config::WEB_ROOT_URL, $tmpurl), 4);
294
 
            echo $mj2;
295
 
        } else {
296
 
            echo $tmpurl;
297
 
        }
298
 
        return 1;
299
320
    }
300
321
    
301
322
    /**
368
389
     */
369
390
    private function _launchJHelioviewer () {
370
391
        require_once('lib/helioviewer/JHV.php');
371
 
        $jhv = new JHV();
372
 
        $jhv->launch($this->params["files"]);
 
392
        if ((isset($this->params['files'])) && ($this->params['files'] != "")) {
 
393
            $jhv = new JHV($this->params['files']);
 
394
        } else {
 
395
            $jhv = new JHV();
 
396
        }
 
397
        $jhv->launch();
373
398
    }
374
399
 
375
400
    /**
403
428
                throw new Exception("Invalid layer choices! You must specify 1-3 command-separate layernames.");
404
429
            }
405
430
 
406
 
            //Limit number of frames to 100
 
431
            //Limit number of frames
407
432
            if (($numFrames < 10) || ($numFrames > Config::MAX_MOVIE_FRAMES)) {
408
 
                throw new Exception("Invalid number of frames. Number of frames should be at least 10 and no more than $maxFrames.");
 
433
                throw new Exception("Invalid number of frames. Number of frames should be at least 10 and no more than " . Config::MAX_MOVIE_FRAMES . ".");
409
434
            }
410
435
 
411
436
            $imgSeries = new ImageSeries($layers, $startDate, $zoomLevel, $numFrames, $frameRate, $hqFormat, $xRange, $yRange, $options);
617
642
                break;
618
643
            case "launchJHelioviewer":
619
644
                break;
 
645
            case "buildQuickMovie":
 
646
                break;
620
647
            default:
621
648
                throw new Exception("Invalid action specified. See the <a href='http://www.helioviewer.org/api/'>API Documentation</a> for a list of valid actions.");        
622
649
        }