~jstys-z/helioviewer.org/client5

« back to all changes in this revision

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

  • Committer: V. Keith Hughitt
  • Date: 2009-06-12 20:56:04 UTC
  • Revision ID: hughitt1@kore-20090612205604-qbo0zca2vohbn8q3
nightly build 2009-06-12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * @class SubFieldImage - A simple class to keep track of a specific layer's associated parameters.
 
4
 */
 
5
require('JP2Image.php');
 
6
 
 
7
class SubFieldImage extends JP2Image {
 
8
        private $observatory;
 
9
        private $instrument;
 
10
        private $detector;
 
11
        private $measurement;
 
12
        private $opacity;
 
13
        private $timestamp;
 
14
        
 
15
        public function __construct($params, $timestamp, $zoomLevel, $xRange, $yRange, $tileSize, $correlate = NULL) {
 
16
                parent::__construct($zoomLevel, $xRange, $yRange, $tileSize);
 
17
 
 
18
                $this->observatory   = substr($params, 0, 3);
 
19
                $this->instrument    = substr($params, 3, 3);
 
20
                $this->detector      = substr($params, 6,3);
 
21
                $this->measurement   = substr($params, 9,3);
 
22
                $this->opacity       = 100;
 
23
                $this->unixTimestamp = $timestamp;
 
24
                $this->timestamp     = $this->parseTimestamp($timestamp);
 
25
                
 
26
                //$this->timestamps  = $this->buildTimestampsArray($startTime, $endTime);
 
27
                $this->buildImage();
 
28
        }
 
29
        
 
30
        public function observatory() {
 
31
                return $this->observatory;
 
32
        }
 
33
        
 
34
        public function instrument() {
 
35
                return $this->instrument;
 
36
        }
 
37
        
 
38
        public function detector() {
 
39
                return $this->detector;
 
40
        }
 
41
        
 
42
        public function measurement() {
 
43
                return $this->measurement;
 
44
        }
 
45
        
 
46
        public function opacity() {
 
47
                return $this->opacity;
 
48
        }
 
49
        
 
50
        private function buildImage () {                
 
51
                // Retrieve meta-information
 
52
                $imageInfo = $this->getMetaInfo();
 
53
 
 
54
                // Filepaths (For .tif and .png images)
 
55
                $jp2  = $this->getFilePath("input");
 
56
                $tif = $this->getFilePath("output");
 
57
 
 
58
                // If tile already exists in cache, use it
 
59
                //if (file_exists($png)) {
 
60
                //      $this->image = new Imagick($png);
 
61
                //      $this->display();
 
62
                //      exit();
 
63
                //}
 
64
                
 
65
                // kdu_expand command
 
66
                $im = $this->extractRegion($jp2, $tif, $imageInfo["width"], $imageInfo["height"], $imageInfo['imgScaleX'], $this->detector, $this->measurement);
 
67
                //echo $im;
 
68
                //exit();
 
69
                
 
70
                // Store image
 
71
                $this->image = $im;
 
72
                
 
73
                $this->display();
 
74
                
 
75
                //$this->dimensions  = $this->getImageDimensions();
 
76
                
 
77
                // extractRegion($input, $output, $imageWidth, $imageHeight, $imageScale, $colorTable = Null)
 
78
                
 
79
                // Ratio of the desired scale to the actual JP2 image scale
 
80
                //$desiredToActual = $this->desiredScale / $scale;
 
81
                
 
82
                // Number of tiles in scaled image
 
83
                //$imgNumTilesX = ceil($width  / ($desiredToActual * $ts));
 
84
                //$imgNumTilesY = ceil($height / ($desiredToActual * $ts));
 
85
                
 
86
                //print $imgNumTilesX . "<br>";
 
87
                //print $imgNumTilesY . "<br>";
 
88
                
 
89
                // Valid number of tiles for movie
 
90
                //$numTilesX = min($imgNumTilesX, $this->xRange["end"] - $this->xRange["start"] + 1);
 
91
                //$numTilesY = min($imgNumTilesY, $this->yRange["end"] - $this->yRange["start"] + 1);
 
92
                
 
93
                //print "numTilesX: $numTilesX<br>";
 
94
                //print "numTilesY: $numTilesY<br>";
 
95
                
 
96
                // Movie (ROI) Dimensions
 
97
                //$dimensions = array();
 
98
                //$dimensions['width']  = $ts * $numTilesX;
 
99
                //$dimensions['height'] = $ts * $numTilesY;
 
100
                //$dimensions['x'] = (1/2) * ($width  - $dimensions['width']);
 
101
                //$dimensions['y'] = (1/2) * ($height - $dimensions['height']);
 
102
        }
 
103
        
 
104
        /**
 
105
         * buildTimestampsArray
 
106
         * @return 
 
107
         * @param $start Object
 
108
         * @param $end Object
 
109
         */
 
110
        private function buildTimestampsArray($startTime, $endTime) {
 
111
                $timestamps = array();
 
112
                
 
113
                // Case 1: A single image
 
114
                if ($startTime == $endTime) {
 
115
                        array_push($timestamps, $startTime);
 
116
                }
 
117
                
 
118
                // Case 2: A image series / movie
 
119
                else {
 
120
                        
 
121
                }
 
122
                
 
123
                return $timestamps;
 
124
        }
 
125
        
 
126
        /**
 
127
         * @function
 
128
         * @description Determine the dimensions for the layer
 
129
         * @return 
 
130
         */
 
131
        private function getMetaInfo() {
 
132
                // Layer parameters
 
133
                $obs  = $this->observatory;
 
134
                $inst = $this->instrument;
 
135
                $det  = $this->detector;
 
136
                $meas = $this->measurement;
 
137
                
 
138
                // Get full image dimensions
 
139
                $sql = sprintf("SELECT width, height, imgScaleX 
 
140
                                        FROM image
 
141
                                                LEFT JOIN measurement on measurementId = measurement.id
 
142
                                                LEFT JOIN measurementType on measurementTypeId = measurementType.id
 
143
                                                LEFT JOIN detector on detectorId = detector.id
 
144
                                                LEFT JOIN opacityGroup on opacityGroupId = opacityGroup.id
 
145
                                                LEFT JOIN instrument on instrumentId = instrument.id
 
146
                                                LEFT JOIN observatory on observatoryId = observatory.id
 
147
                        WHERE observatory.abbreviation='%s' AND instrument.abbreviation='%s' AND detector.abbreviation='%s' AND measurement.abbreviation='%s' LIMIT 1",
 
148
                                        mysql_real_escape_string($obs), mysql_real_escape_string($inst), mysql_real_escape_string($det), mysql_real_escape_string($meas));
 
149
                                        
 
150
                $result = $this->db->query($sql);
 
151
                $meta = mysql_fetch_array($result, MYSQL_ASSOC);
 
152
                
 
153
                return $meta;
 
154
        }
 
155
        
 
156
        private function parseTimestamp ($ts) {
 
157
                $d = getdate($ts);
 
158
                
 
159
                $year = $d['year'];
 
160
                $mon  = str_pad($d['mon'], 2 , "0", STR_PAD_LEFT);
 
161
                $day  = str_pad($d['mday'], 2 , "0", STR_PAD_LEFT);
 
162
                $hour = str_pad($d['hours'], 2 , "0", STR_PAD_LEFT);
 
163
                $min  = str_pad($d['minutes'], 2 , "0", STR_PAD_LEFT);
 
164
                $sec  = str_pad($d['seconds'], 2 , "0", STR_PAD_LEFT);
 
165
                
 
166
                return "$year-$mon-$day $hour:$min:$sec";
 
167
        }
 
168
        
 
169
        private function getFilePath($type) {
 
170
                // Date information
 
171
                $year  = substr($this->timestamp,0,4);
 
172
                $month = substr($this->timestamp,5,2);
 
173
                $day   = substr($this->timestamp,8,2);
 
174
                $hour  = substr($this->timestamp,11,2);
 
175
                $min   = substr($this->timestamp,14,2);
 
176
                $sec   = substr($this->timestamp,17,2);
 
177
                
 
178
                // Observation
 
179
                $obs  = $this->observatory;
 
180
                $inst = $this->instrument;
 
181
                $det  = $this->detector;
 
182
                $meas = $this->measurement;
 
183
 
 
184
                // JP2 File
 
185
                if ($type == "input") {
 
186
                        $filepath  = $this->jp2Dir . implode("/", array($year, $month, $day, $obs, $inst, $det, $meas)) . "/";
 
187
                        $filepath .= implode("_", array($year, $month, $day, $hour . $min . $sec, $obs, $inst, $det, $meas)) . ".jp2";
 
188
                }
 
189
 
 
190
                // Output File
 
191
                else {
 
192
                        // Starting point
 
193
                        $filepath = $this->cacheDir . "movies" . "/";
 
194
                        if (!file_exists($filepath))
 
195
                                mkdir($filepath);
 
196
        
 
197
                        // Create necessary directories
 
198
                        $filepath .= $year . "/";
 
199
                        if (!file_exists($filepath))
 
200
                                mkdir($filepath);
 
201
        
 
202
                        $filepath .= $month . "/";
 
203
                        if (!file_exists($filepath))
 
204
                                mkdir($filepath);
 
205
        
 
206
                        $filepath .= $day . "/";
 
207
                        if (!file_exists($filepath))
 
208
                                mkdir($filepath);
 
209
        
 
210
                        // Convert coordinates to strings
 
211
                        $xStartStr = "+" . str_pad($this->xRange["start"], 2, '0', STR_PAD_LEFT);
 
212
                        if (substr($this->xRange["start"],0,1) == "-")
 
213
                                $xStartStr = "-" . str_pad(substr($this->xRange["start"], 1), 2, '0', STR_PAD_LEFT);
 
214
        
 
215
                        $yStartStr = "+" . str_pad($this->yRange["start"], 2, '0', STR_PAD_LEFT);
 
216
                        if (substr($this->yRange["start"],0,1) == "-")
 
217
                                $yStartStr = "-" . str_pad(substr($this->yRange["start"], 1), 2, '0', STR_PAD_LEFT);
 
218
                                
 
219
                        $xEndStr = "+" . str_pad($this->xRange["end"], 2, '0', STR_PAD_LEFT);
 
220
                        if (substr($this->xRange["end"],0,1) == "-")
 
221
                                $xEndStr = "-" . str_pad(substr($this->xRange["end"], 1), 2, '0', STR_PAD_LEFT);
 
222
        
 
223
                        $yEndStr = "+" . str_pad($this->yRange["end"], 2, '0', STR_PAD_LEFT);
 
224
                        if (substr($this->yRange["end"],0,1) == "-")
 
225
                                $yEndStr = "-" . str_pad(substr($this->yRange["end"], 1), 2, '0', STR_PAD_LEFT);        
 
226
        
 
227
                        $filepath .= implode("_", array($this->unixTimestamp, $obs, $inst, $det, $meas, $this->zoomLevel, $xStartStr, $xEndStr, $yStartStr, $yEndStr, ".tif"));
 
228
                }
 
229
 
 
230
                return $filepath;
 
231
        }
 
232
}
 
233
?>
 
 
b'\\ No newline at end of file'