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

Annotation of /MITgcm_contrib/darwinview/src/darwin.c

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


Revision 1.1 - (hide annotations) (download)
Wed Jul 11 14:34:55 2007 UTC (17 years, 11 months ago) by marissa
Branch: MAIN
CVS Tags: version_0
File MIME type: text/plain
Adding the latest and greatest darwinview source

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

  ViewVC Help
Powered by ViewVC 1.1.22