/[MITgcm]/MITgcm_contrib/darwinview/src/windows.c
ViewVC logotype

Contents of /MITgcm_contrib/darwinview/src/windows.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download)
Thu Jul 5 14:55:02 2007 UTC (16 years, 10 months ago) by cnh
Branch: MAIN
File MIME type: text/plain
Adding stuff for Marissa, since her account is broken.

1 #include <GL/glut.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <math.h>
6 #define WINDOW "image"
7 #define UP 101
8 #define DOWN 103
9 #define RIGHT 102
10 #define LEFT 100
11 #define MAX 200
12
13 int NX, NY, MAXNZ, sets;
14
15 void do_byteswap_f32( float arr[], int nel ), global(), local( int, int );
16 void readnames( char[] ), readarray( float[], char[], int ), readjet();
17 void TimerFunction( int );
18
19 float data[MAX*MAX], mxval, mnval, jet[MAX][MAX];
20 float globalmx=0, globalmn=100;
21 int win[MAX], ilev=1, howmany, count=0, glo=0, usr=0, anim=0;
22 char fns[MAX][MAX];
23
24 void menu(int value){
25 int i;
26
27 switch( value ){
28 case 1: usr=glo=0; // unset glo & usr, sets local max/min
29 local( count, ilev ); // reads new array, set local max/min
30 break;
31 case 2: glo=1; // enables global max/min
32 usr=0; // unsets usr
33 mxval=globalmx; // sets max to globalmx
34 mnval=globalmn; // sets min to globalmn
35 break;
36 case 3: usr=1; // switch to user-set max/min
37 glo=0; // unset glo
38 printf( "Max=" ); scanf( "%f", &mxval ); // prompt for new max/min
39 printf( "Min=" ); scanf( "%f", &mnval );
40 break;
41 }
42 for( i=0; i<sets; i++){
43 glutSetWindow(win[i]);
44 glutPostRedisplay(); // display with new values
45 }
46 }
47
48 void display(void){
49 int i, j, ioff;
50 float r, g, b, num, k, y;
51 char str[MAX];
52 glClear( GL_COLOR_BUFFER_BIT ); // background to black
53
54 ioff=0; // ioff will count thru both i&j b/c data is 1D
55 for( j=0; j<NY; j++ )
56 for( i=0; i<NX; i++ ){
57 r=g=b=0; // set color values to black
58 if( data[ioff]==0 ); // if data=0, values stay black
59 else{
60 if( data[ioff]<mnval ) num=0; // if data is less than min, =min
61 else{
62 if( data[ioff]>mxval ) num=63; // if data is more than max, =max
63 else
64 num=63*( data[ioff]-mnval )/( mxval-mnval ); //scale num from 0-63
65 }
66 r=jet[(int)num][0]; // set red val
67 g=jet[(int)num][1]; // set green val
68 b=jet[(int)num][2]; // set blue val
69 }
70
71 glColor3f( r, g, b ); // put r, g, b into effect
72 glRectf( i, j, i+1, j+1 ); // draws a square for data value
73 ioff++;
74 }
75
76 glColor3f( 1, 1, 1 ); // set color to white
77 glRectf( NX, 0, NX+1, NY+1 ); // draws a border
78 for( i=0; i<64; i++ ){ //draws color bar
79 glColor3f( jet[i][0], jet[i][1], jet[i][2]); //sets color
80 k=(float)i; // turns i into a float
81 y=(float)NY/64; // sets height of rectangles
82 glRectf( NX+10, y*k, NX+20, (k+1)*y ); // draws rectangle
83 }
84
85 glColor3f( 1, 1, 1 ); // color to white
86
87 sprintf( str, "%.4e", mxval ); // labels color bar with max val
88 glRasterPos2f( NX+2, NY-1 ); // sets where to write to
89 for( i=0; i<strlen( str ); i++) // writes each character
90 glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, str[i] );
91
92 str[0]='\0'; // sets str to null
93
94 sprintf( str, "%.4e", mnval ); // labels color bar with min val
95 glRasterPos2f( NX+2, 1 ); // sets where to write to
96 for( i=0; i<strlen( str ); i++ ) // writes each character
97 glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, str[i] );
98
99 glRectf( 0, NY, NX, NY+1 ); // draws a border
100 glRasterPos2f( 1, NY+6 ); // sets where to write to
101 for( i=0; i<strlen( fns[count] ); i++ ) // labels current file name
102 glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, fns[count][i] );
103
104 str[0]='\0'; // sets str to null
105
106 sprintf( str, "Level %d", ilev ); // labels current level
107 glRasterPos2f( 1, NY+3 ); // sets where to write to
108 for( i=0; i<strlen( str ); i++ ) // writes each character
109 glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, str[i] );
110
111 str[0]='\0'; //sets str to null
112
113 if( glo ) // labels how max/min have been set
114 sprintf( str, "Global" ); // if glo is set, display Global
115 if( usr )
116 sprintf( str, "User-set" ); // if usr is set, display User-set
117 if( !usr && !glo )
118 sprintf( str, "Local" ); // else display Local
119 glRasterPos2f( NX+12, NY+3);
120 for( i=0; i<strlen( str ); i++ ) // write each char of label
121 glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, str[i] );
122
123 str[0]='\0';
124
125 if( anim ){ // tell user if autoplay is on
126 sprintf( str, "Autoplay" );
127 glRasterPos2f( NX-8, NY+3 );
128 for(i=0; i<strlen( str ); i++ ) // write each char of label
129 glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, str[i] );
130 }
131
132 glutSwapBuffers();
133 glFlush();
134 }
135
136 void key( unsigned char key, int x, int y ){ //called on key press
137 int i;
138
139 switch(key){
140 case 'q': exit(0); //quits on 'q'
141 break;
142 case 'r': count=0;
143 ilev=1;
144 if( glo || usr )
145 readarray( data, fns[count], ilev );
146 else
147 local( count, ilev );
148 for( i=0; i<sets; i++){
149 glutSetWindow(win[i]);
150 glutPostRedisplay();
151 }
152 break;
153 case 'a': anim++; // switches autoplay off/on
154 anim=anim%2; // when a is pressed repeatedly
155 for( i=0; i<sets; i++){
156 glutSetWindow(win[i]);
157 glutPostRedisplay();
158 }
159 break;
160 }
161 }
162
163 void TimerFunction( int value ){
164 int i;
165
166 switch(value){ // increments in the correct direction
167 case DOWN : if( ilev<MAXNZ )
168 ilev++; // if down arrow pressed, moves down levels
169 break;
170 case UP : if( ilev>1 )
171 ilev--; // if up arrow is pressed, moves up levels
172 break;
173 case LEFT : if( count>0 )
174 count--; // if left arrow is pressed, moves back in time
175 break;
176 case RIGHT: if( count<howmany-1 )
177 count++; // if right arrow is pressed, moves forward in time
178 break;
179 }
180
181 if( glo || usr ) // if glo or usr is set, read new array w/o
182 readarray( data, fns[count], ilev ); // calculating new max/min
183 else // if local max/min is set
184 local( count, ilev ); // read new array, calculate new max/min
185
186 for( i=0; i<sets; i++){
187 glutSetWindow(win[i]);
188 glutPostRedisplay(); // display with new values
189 }
190
191 switch( value ){
192 case DOWN : if( ilev<MAXNZ ) // sets limit that you can go down
193 glutTimerFunc( 100, TimerFunction, value ); // recalls itself
194 break;
195 case UP : if( ilev>1 ) // sets limit that you can go up
196 glutTimerFunc( 100, TimerFunction, value ); // recalls itself
197 break;
198 case LEFT : if( count>0 ) // sets limit that you can go left
199 glutTimerFunc( 100, TimerFunction, value ); // recalls itself
200 break;
201 case RIGHT: if( count<howmany-1 ) // sets limit that you can go right
202 glutTimerFunc( 100, TimerFunction, value ); // recalls itself
203 break;
204 }
205 }
206
207 void specialkey( int key, int x, int y ){
208 int i;
209
210 if( anim ) // if animation is set, call the timer function
211 glutTimerFunc( 100, TimerFunction, key);
212
213 switch(key){
214 case DOWN : if( ilev<MAXNZ ) // if you haven't reached the bottom
215 ilev++; // keep going down
216 break;
217 case UP : if( ilev>1 ) // if you haven't reached the top
218 ilev--; // keep going up
219 break;
220 case RIGHT : if( count<howmany-1 ) // if you haven't reached the last file
221 count++; // keep going right
222 break;
223 case LEFT : if( count>0 ) // if you haven't reached the first file
224 count--; // keep going left
225 break;
226 }
227 if( glo || usr ) // if global or user-set max/min
228 readarray(data, fns[count], ilev); // read new array w/o new max/min
229 else // if local max/min set
230 local(count, ilev); // read new array and calculate local max/min
231
232 for(i=0; i<sets; i++){
233 glutSetWindow(win[i]);
234 glutPostRedisplay(); // display with new values
235 }
236 }
237
238 void global(){ // calculates the max/min for the total data set
239 FILE* fp;
240 int i, j;
241
242 for( i=0; i<howmany; i++ ) // cycles through each file
243 for( j=0; j<MAXNZ; j++ ){ // cycles through each level for each file
244 local( i, j ); // calculates local/max/min for specific data set
245 if( mxval > globalmx ) globalmx = mxval; // sets highest value to globalmx
246 if( mnval < globalmn ) globalmn = mnval; // sets lowest value to globalmn
247 }
248 }
249
250 void local( int time, int lev ){ // calculates local max/min
251 int i;
252
253 mxval=0;
254 mnval=100;
255 readarray( data, fns[time], lev ); // read new array of data
256 for( i=0;i<NX*NY;i++ ){
257 if( data[i] > mxval ) mxval=data[i]; // set largest val to mxval
258 if( data[i] < mnval ) mnval=data[i]; // set smallest val to mnval
259 }
260 }
261
262 void readnames( char filename[] ){ // reads in list of filenames containing data
263 FILE* fp;
264 int i=0;
265
266 fp=fopen( filename, "r" ); // opens list
267 while( !feof(fp) ){ // reads until the end of the file
268 fscanf( fp, "%s",fns[i] ); // places filename into an array of strings
269 i++; // counts how many filenames there are
270 }
271 fclose( fp ); // close file
272 howmany=i-1;
273 }
274
275 void readjet(){ //reads in color scale values
276
277 FILE* fp;
278 int i, j;
279
280 fp=fopen( "jet.dat", "r" ); // opens file containing values
281 for( i=0; i<64; i++ ) // reads in 64 sets of r, g, b values
282 for( j=0; j<3; j++ )
283 fscanf( fp, "%f", &jet[i][j] );
284 fclose( fp ); // closes file
285 }
286
287
288 void readarray( float arr[], char filename[], int il ){ // reads new data
289 FILE *fp;
290 int i;
291
292 fp=fopen( filename, "r" ); // opens the file containing data
293 fseek( fp, (il-1)*NX*NY*4, SEEK_SET ); // seeks to the correct level using ilev
294 fread( arr, sizeof( arr[0] ), NX*NY, fp ); // reads in data to fill one level
295 fclose( fp ); // close file
296 do_byteswap_f32( arr,NX*NY ); // swaps the binary data
297 }
298
299 void do_byteswap_f32( float arr[], int nel ) // switches endian
300 {
301 int i;
302 char src[4];
303 char trg[4];
304 for( i=0; i<nel; i++ ){
305 memcpy( src, &(arr[i]), 4);
306 trg[0]=src[3];
307 trg[1]=src[2];
308 trg[2]=src[1];
309 trg[3]=src[0];
310 memcpy( &(arr[i]), trg, 4);
311 }
312 }
313 int main( int argc, char *argv[] ){
314 int i, x, y, tmp;
315 char filename[MAX];
316
317 printf( "Please enter x, y and z dimensions\n" );
318 scanf( "%d %d %d", &NX, &NY, &MAXNZ ); //user-set NX, NY, NZ
319 printf( "Please enter filename\n" );
320 scanf( "%s", filename ); //filename contains names of data files
321 printf( "how many data sets?\n" );
322 scanf( "%d", &sets);
323
324 readjet(); // stores color values
325 readnames( filename ); // gets list of filenames to read from
326 global(); // calculates max and min for all data
327 local( count, ilev ); // sets curr data array, finds local max/min
328
329 glutInit( &argc, argv ); // initializes stuff
330 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
331 glutInitWindowSize( 1400/3, ((NY+15)*1400)/((NX+25)*3) ); // sets window size from NX, NY
332
333 for(i=0; i<sets; i++){
334 x=(i%3)*(1400/3);
335 y=(i/3)*((NY+15)*1400)/((NX+25)*3);
336 glutInitWindowPosition( x, y );
337 win[i]=glutCreateWindow( WINDOW ); // window is created
338 glClearColor( 0, 0, 0, 0 ); // sets clear color to black
339 gluOrtho2D( 0, NX+25, 0, NY+15 ); // sets how data is mapped to window
340 glutDisplayFunc( display );
341 glutKeyboardFunc( key ); // called on key press
342 glutSpecialFunc( specialkey ); // called on special key press (arrow keys)
343
344 glutCreateMenu( menu ); // adds a menu
345 glutAddMenuEntry( "Local Color Scale", 1 ); // describes choices
346 glutAddMenuEntry( "Global Color Scale", 2 );
347 glutAddMenuEntry( "User-Set Color Scale", 3 );
348 glutAttachMenu( GLUT_RIGHT_BUTTON ); // menu called on right click
349 }
350
351 glutMainLoop();
352 return 0;
353 }
354

  ViewVC Help
Powered by ViewVC 1.1.22