Parent Directory
|
Revision Log
|
Revision Graph
|
Patch
--- MITgcm_contrib/darwinview/src/darwin.c 2007/07/11 14:34:55 1.1
+++ MITgcm_contrib/darwinview/src/darwin.c 2007/07/12 18:30:19 1.2
@@ -18,146 +18,182 @@
float data[MAX][MAX*MAX], mxval, mnval, jet[MAX][MAX];
float globalmx=0, globalmn=100;
-int win[MAX], ilev=1, howmany, count=0, glo=0, usr=0, anim=0;
+int win[MAX], ilev=1, howmany, count=0, glo=0, usr=0, anim=0, logscale=0;
char initfns[MAX][MAX], fns[MAX][MAX][MAX];
-void menu(int value){
- int i;
+void menu(int value){ // called when menu is opened on right click
switch( value ){
- case 1: usr=glo=0; // unset glo & usr, sets local max/min
- glutPostRedisplay();
+ case 1: usr=glo=0; // unset glo & usr, sets local max/min
+ glutPostRedisplay(); // recall display func with new values
break;
- case 2: glo=1; // enables global max/min
- usr=0; // unsets usr
- mxval=globalmx; // sets max to globalmx
- mnval=globalmn; // sets min to globalmn
- glutPostRedisplay(); // display with new values
+ case 2: glo=1; // enables global max/min
+ usr=0; // unsets usr
+ mxval=globalmx; // sets max to globalmx
+ mnval=globalmn; // sets min to globalmn
+ glutPostRedisplay(); // recall display func with new values
break;
- case 3: usr=1; // switch to user-set max/min
- glo=0; // unset glo
- printf( "Max=" ); scanf( "%f", &mxval ); // prompt for new max/min
- printf( "Min=" ); scanf( "%f", &mnval );
- glutPostRedisplay(); // display with new values
+ case 3: usr=1; // switch to user-set max/min
+ glo=0; // unset glo
+ printf( "Max=" ); scanf( "%f", &mxval ); // prompt user for new max
+ printf( "Min=" ); scanf( "%f", &mnval ); // prompt user for new min
+ glutPostRedisplay(); // recall display func with new values
+ break;
+ case 4: logscale=(logscale+1)%2; // switch log scale on/off
+ glutPostRedisplay();
break;
}
}
-void display(void){
+void display(void){ // called on glutPostRedisplay
int i, j, ioff, q;
- float r, g, b, num, k, y;
+ float r, g, b, k, y;
+ double num, logmx, logmn;
char str[MAX];
- for( q=0; q<setsx*setsy; q++ ){ //runs display func for each window
- glutSetWindow( win[q] );
- glClear( GL_COLOR_BUFFER_BIT ); // background to black
- if( glo || usr )
- readarray( data[q], fns[q][count], ilev );
- else
- local( q, count, ilev );
-
- ioff=0; // ioff will count thru both i&j b/c data is 1D
- for( j=0; j<NY; j++ ){
- for( i=0; i<NX; i++ ){
- r=g=b=0; // set color values to black
- if( data[q][ioff]==0 ); // if data=0, values stay black
+ for( q=0; q<setsx*setsy; q++ ){ // runs display func for each subwindow
+ glutSetWindow( win[q] ); // sets which subwindow to display to
+ glClear( GL_COLOR_BUFFER_BIT ); // background to black
+ if( glo || usr ) // if global or user-set max/min
+ readarray( data[q], fns[q][count], ilev ); // read new array w/o calculating local max/min
+ else // if local max/min is set
+ local( q, count, ilev ); // read new array and calculate local max/min
+
+ if( logscale ){
+ if( usr ){
+ logmx=(double)mxval;
+ logmn=(double)mnval;
+ }
+ else{
+ logmx=log10( mxval );
+ logmn=log10( mnval );
+ }
+ }
+
+ ioff=0; // ioff will count both i&j b/c data is 1D
+ for( j=0; j<NY; j++ ){ // cycles through y values
+ for( i=0; i<NX; i++ ){ // cycles through x values
+ r=g=b=0; // set color values to black
+ if( data[q][ioff]==0 ); // if data=0, values stay black
else{
- if( data[q][ioff]<mnval ) num=0; // if data is less than min, =min
+ if( logscale ){
+ num=(double)data[q][ioff];
+ num=log10( num );
+ if( num<logmn ) num=0;
+ if( num>logmx ) num=63;
+ else
+ num=63*( num-logmn )/( logmx-logmn );
+ }
else{
- if( data[q][ioff]>mxval ) num=63; // if data is more than max, =max
- else
- num=63*( data[q][ioff]-mnval )/( mxval-mnval ); //scale num from 0-63
+ if( data[q][ioff]<mnval ) num=0; // if data is less than min, =min
+ else
+ if( data[q][ioff]>mxval ) num=63; // if data is more than max, =max
+ else
+ num=63*( data[q][ioff]-mnval )/( mxval-mnval ); // scale num from 0-63 (not defined for 64)
}
- r=jet[(int)num][0]; // set red val
- g=jet[(int)num][1]; // set green val
- b=jet[(int)num][2]; // set blue val
+ r=jet[(int)num][0]; // set red val
+ g=jet[(int)num][1]; // set green val
+ b=jet[(int)num][2]; // set blue val
}
-
- glColor3f( r, g, b ); // put r, g, b into effect
- glRectf( i, j, i+1, j+1 ); // draws a square for data value
+
+ glColor3f( r, g, b ); // put r, g, b into effect
+ glRectf( i, j, i+1, j+1 ); // draws a square for data value
ioff++;
}
}
- glColor3f( 1, 1, 1 ); // set color to white
- glRectf( NX, 0, NX+1, NY+1 ); // draws a border
- glRectf( 0, NY, NX, NY+1 ); // draws a border
- for( i=0; i<64; i++ ){ //draws color bar
- glColor3f( jet[i][0], jet[i][1], jet[i][2]); //sets color
- k=(float)i; // turns i into a float
- y=(float)NY/64; // sets height of rectangles
- glRectf( NX+10, y*k, NX+20, (k+1)*y ); // draws rectangle
+ glColor3f( 1, 1, 1 ); // set color to white
+ glRectf( NX, 0, NX+1, NY+1 ); // draws a border
+ glRectf( 0, NY, NX, NY+1 ); // draws a border
+ for( i=0; i<64; i++ ){ // draws color bar
+ glColor3f( jet[i][0], jet[i][1], jet[i][2]); // sets color
+ k=(float)i; // turns i into a float
+ y=(float)NY/64; // sets height of rectangles
+ glRectf( NX+10, y*k, NX+20, (k+1)*y ); // draws rectangle
}
- glColor3f( 1, 1, 1 ); // color to white
+ glColor3f( 1, 1, 1 ); // color to white
- sprintf( str, "%.2e", mxval ); // labels color bar with max val
+ if( logscale )
+ sprintf( str, "%.2f", logmx );
+ else
+ sprintf( str, "%.2e", mxval ); // labels color bar with max val
bitmap( str, NX+2, NY-1 );
- sprintf( str, "%.2e", mnval ); // labels color bar with min val
+ if( logscale )
+ sprintf( str, "%.2f", logmn );
+ else
+ sprintf( str, "%.2e", mnval ); // labels color bar with min val
bitmap( str, NX+2, 1 );
- sprintf( str, "Level %d", ilev ); // labels current level
- bitmap( str, 1, NY+3);
+ sprintf( str, "Level %d", ilev ); // labels current level
+ bitmap( str, 1, NY+3 );
- if( glo ) // labels how max/min have been set
- sprintf( str, "Global" ); // if glo is set, display Global
+ sprintf( str, "Time %d", count+1 ); // labels current time
+ bitmap( str, 50, NY+3 );
+
+ if( glo ) // labels how max/min have been set
+ sprintf( str, "Global" ); // if glo is set, display Global
if( usr )
- sprintf( str, "User-set" ); // if usr is set, display User-set
+ sprintf( str, "User-set" ); // if usr is set, display User-set
if( !usr && !glo )
- sprintf( str, "Local" ); // else display Local
- bitmap( str, NX+12, NY+3);
+ sprintf( str, "Local" ); // else display Local
+ bitmap( str, NX+12, NY+3 );
- if( anim ){ // tell user if autoplay is on
+ if( anim ){ // tell user if autoplay is on
sprintf( str, "Autoplay" );
- bitmap( str, NX-15, NY+3 );
+ bitmap( str, NX-25, NY+3 );
+ }
+
+ if( logscale ){
+ sprintf( str, "Log Scale" ); // tell user if log scale is on
+ bitmap( str, NX-25, NY+10 );
}
- bitmap( fns[q][count], 1, NY+6 ); // labels current file
+ bitmap( fns[q][count], 1, NY+10 ); // labels current file
- glutSwapBuffers();
+ glutSwapBuffers(); // double buffering set to animate smoothly
glFlush();
}
}
-void bitmap( char str[], int x, int y ){
+void bitmap( char str[], int x, int y ){ // called to display text onscreen
int i;
- glRasterPos2f( x, y );
- for( i=0; i<strlen( str ); i++)
+ glRasterPos2f( x, y ); // set position of text
+ for( i=0; i<strlen( str ); i++) // display each character of str
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, str[i] );
}
-void key( unsigned char key, int x, int y ){ //called on key press
+void key( unsigned char key, int x, int y ){ // called on key press
int i;
switch(key){
- case 'q': exit(0); //quits on 'q'
+ case 'q': exit(0); // quits on 'q'
break;
- case 'r': count=0;
- ilev=1;
+ case 'r': count=0; // resets back to first time
+ ilev=1; // and first level
glutPostRedisplay();
break;
- case 'a': anim++; // switches autoplay off/on
- anim=anim%2; // when a is pressed repeatedly
+ case 'a': anim=(anim+1)%2; // turns anim on/off
glutPostRedisplay();
break;
}
}
-void TimerFunction( int value ){
+void TimerFunction( int value ){ // called when anim is set and arrow key is pressed
int i;
- switch(value){ // increments in the correct direction
+ switch(value){ // increments in the correct direction
case DOWN : if( ilev<MAXNZ )
- ilev++; // if down arrow pressed, moves down levels
+ ilev++; // if down arrow pressed, move down a level
break;
case UP : if( ilev>1 )
- ilev--; // if up arrow is pressed, moves up levels
+ ilev--; // if up arrow is pressed, move up a level
break;
case LEFT : if( count>0 )
- count--; // if left arrow is pressed, moves back in time
+ count--; // if left arrow is pressed, move back one time step
break;
case RIGHT: if( count<howmany-1 )
- count++; // if right arrow is pressed, moves forward in time
+ count++; // if right arrow is pressed, moves forward one time step
break;
}
@@ -165,16 +201,16 @@
if (anim )
switch( value ){
- case DOWN : if( ilev==MAXNZ ) ilev=1; // sets limit that you can go down
+ case DOWN : if( ilev==MAXNZ ) ilev=1; // if end reached, restart
glutTimerFunc( 100, TimerFunction, value ); // recalls itself
break;
- case UP : if( ilev==1 ) ilev=MAXNZ; // sets limit that you can go up
+ case UP : if( ilev==1 ) ilev=MAXNZ; // if end reached, restart
glutTimerFunc( 100, TimerFunction, value ); // recalls itself
break;
- case LEFT : if( count==0 ) count=howmany-1;// sets limit that you can go left
+ case LEFT : if( count==0 ) count=howmany-1; // if end reached, restart
glutTimerFunc( 100, TimerFunction, value ); // recalls itself
break;
- case RIGHT: if( count==howmany-1 ) count=0; //sets limit that you can go right
+ case RIGHT: if( count==howmany-1 ) count=0; // if end reached, restart
glutTimerFunc( 100, TimerFunction, value ); // recalls itself
break;
}
@@ -183,15 +219,15 @@
void specialkey( int key, int x, int y ){
int i;
- if( anim ) // if animation is set, call the timer function
- glutTimerFunc( 100, TimerFunction, key);
+ if( anim ) // if animation is set, call the timer function
+ glutTimerFunc( 100, TimerFunction, key); // to scroll automatically
switch(key){
- case DOWN : if( ilev<MAXNZ ) // if you haven't reached the bottom
- ilev++; // keep going down
+ case DOWN : if( ilev<MAXNZ ) // if you haven't reached the bottom
+ ilev++; // keep going down
break;
- case UP : if( ilev>1 ) // if you haven't reached the top
- ilev--; // keep going up
+ case UP : if( ilev>1 ) // if you haven't reached the top
+ ilev--; // keep going up
break;
case RIGHT : if( count<howmany-1 ) // if you haven't reached the last file
count++; // keep going right
@@ -203,64 +239,66 @@
glutPostRedisplay();
}
-void global(){ // calculates the max/min for the total data set
+void global(){ // calculates the max/min for the total data set
FILE* fp;
int h, i, j;
- for( h=0; h<setsx*setsy; h++) // cycles through each data set
- for( i=0; i<howmany; i++ ) // cycles through each time
- for( j=0; j<MAXNZ; j++ ){ // cycles through each level for each file
- local( h, i, j ); // calculates local/max/min for specific data set
+ for( h=0; h<setsx*setsy; h++) // cycles through each window
+ for( i=0; i<howmany; i++ ) // cycles through each time
+ for( j=0; j<MAXNZ; j++ ){ // cycles through each level for each file
+ local( h, i, j ); // calculates local max/min for specific data set
if( mxval > globalmx ) globalmx = mxval; // sets highest value to globalmx
if( mnval < globalmn ) globalmn = mnval; // sets lowest value to globalmn
}
}
-void local( int i, int time, int lev ){ // calculates local max/min
+void local( int i, int time, int lev ){ // calculates local max/min
int j;
mxval=0;
mnval=100;
- readarray( data[i], fns[i][time], lev ); // read new array of data
+ readarray( data[i], fns[i][time], lev ); // read new array of data
for( j=0; j<NX*NY; j++ ){
- if( data[i][j] > mxval ) mxval=data[i][j]; // set largest val to mxval
- if( data[i][j] < mnval ) mnval=data[i][j]; // set smallest val to mnval
+ if( data[i][j] > mxval )
+ mxval=data[i][j]; // set largest val to mxval
+ if( data[i][j] < mnval && data[i][j] > 0 )
+ mnval=data[i][j]; // set smallest positive val to mnval
}
}
-void readnames( char filename[] ){ // reads in list of filenames
+void readnames( char filename[] ){ // reads in list of filenames
FILE* fp;
int i=0, j=0;
- fp=fopen( filename, "r" ); // opens list
- while( !feof(fp) ){ // reads until the end of the file
- fscanf( fp, "%s", initfns[i] ); // places filename into an array of strings
- i++; // counts how many filenames there are
+ fp=fopen( filename, "r" ); // opens list of filenames
+ while( !feof(fp) ){ // reads until the end of the file
+ fscanf( fp, "%s", initfns[i] ); // places filename into an array of strings
+ i++; // counts how many filenames there are
}
- fclose( fp ); // close file
- howmany=i-1;
+ fclose( fp ); // close file
+ howmany=i-1; // saves number of initial filenames
- for(i=0; i<howmany; i++){
- fp=fopen( initfns[i], "r" );
+ for(i=0; i<howmany; i++){ // goes through each read-in filename
+ fp=fopen( initfns[i], "r" ); // opens each filename
j=0;
- while( !feof(fp) ){
- fscanf( fp, "%s", fns[i][j]);
+ while( !feof(fp) ){ // reads in filenames from each filename
+ fscanf( fp, "%s", fns[i][j]);
j++;
}
- fclose(fp);
+ fclose(fp); // close file
}
- howmany=j-1;
+ howmany=j-1; // saves number of data filenames
}
-void readjet(){ //reads in color scale values
+void readjet(){ //reads in color scale values
FILE* fp;
int i, j;
fp=fopen( "jet.dat", "r" ); // opens file containing values
- for( i=0; i<64; i++ ) // reads in 64 sets of r, g, b values
+ for( i=0; i<64; i++ ) // reads in 64 sets of r, g, b values
for( j=0; j<3; j++ )
fscanf( fp, "%f", &jet[i][j] );
- fclose( fp ); // closes file
+ fclose( fp ); // closes file
}
@@ -268,11 +306,14 @@
FILE *fp;
int i;
- fp=fopen( filename, "r" ); // opens the file containing data
- fseek( fp, (il-1)*NX*NY*4, SEEK_SET ); // seeks to the correct level using ilev
+ fp=fopen( filename, "r" ); // opens the file containing data
+ fseek( fp, (il-1)*NX*NY*4, SEEK_SET ); // seeks to the correct place using ilev
fread( arr, sizeof( arr[0] ), NX*NY, fp ); // reads in data to fill one level
- fclose( fp ); // close file
- //do_byteswap_f32( arr, NX*NY ); // swaps the binary data
+ fclose( fp ); // close file
+ for( i=0; i<NX*NY; i++ )
+ if( arr[i] < 0 ) arr[i] = pow(10, -20); // sets negative values to 10^-20
+
+ //do_byteswap_f32( arr, NX*NY ); // switches endian
}
void do_byteswap_f32( float arr[], int nel ) // switches endian
@@ -290,62 +331,57 @@
}
}
-void parentfunc(){
- glClear( GL_COLOR_BUFFER_BIT ); // background to black
- glutSwapBuffers();
- glFlush();
-}
-
int main( int argc, char *argv[] ){
int i, tmpx, tmpy, winx, winy, parent;
char filename[MAX];
- sscanf(argv[1], "%dx%d", &winx, &winy);
- winy-=20;
+ sscanf(argv[1], "%dx%d", &winx, &winy); // reads screen resolution from command line
+ winy-=60; winx-=20; // adjusts resolution so edges won't get cut off
printf( "Please enter x, y and z dimensions.\n" );
- scanf( "%d %d %d", &NX, &NY, &MAXNZ ); //user-set NX, NY, NZ
+ scanf( "%d %d %d", &NX, &NY, &MAXNZ ); // prompts user for NX, NY, NZ
printf( "Please enter filename.\n" );
- scanf( "%s", filename ); //filename contains names of data files
+ scanf( "%s", filename ); // prompts user for initial filenames
printf( "Please enter dimensions of data sets.\n" );
- scanf( "%dx%d", &setsx, &setsy);
+ scanf( "%dx%d", &setsx, &setsy); // prompts user for dimensions of subwindows
- readjet(); // stores color values
- readnames( filename ); // gets list of filenames to read from
- global(); // calculates max and min for all data
-
- glutInit( &argc, argv );
- glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
- glutInitWindowSize( winx, winy );
- glutInitWindowPosition( 10, 10 );
- parent=glutCreateWindow( WINDOW );
+ readjet(); // stores color values
+ readnames( filename ); // gets list of filenames to read from
+ global(); // calculates max and min for all data
+
+ glutInit( &argc, argv );
+ glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); // sets rgb mode and double buffering
+ glutInitWindowSize( winx, winy ); // parent window will cover screen
+ glutInitWindowPosition( 10, 10 ); // location of parent window
+ parent=glutCreateWindow( WINDOW ); // creates parent window
- glClearColor( 0, 0, 0, 0 ); // sets clear color to black
- gluOrtho2D( 0, winx , winy, 0 ); // sets how data is mapped to window
+ glClearColor( 0, 0, 0, 0 ); // sets clear color to black
+ gluOrtho2D( 0, winx , winy, 0 ); // sets(0,0) as top left corner
glutDisplayFunc( display );
- glutKeyboardFunc( key ); // called on key press
- glutSpecialFunc( specialkey ); // called on special key press (arrow keys)
+ glutKeyboardFunc( key ); // called on key press
+ glutSpecialFunc( specialkey ); // called on special key press (arrow keys)
for( i=0; i<setsx*setsy; i++ ){
- tmpx = (i%setsx)*(winx/setsx);
- tmpy = (i/setsx)*(winy/setsy);
+ tmpx = (i%setsx)*(winx/setsx); // x coordinate of top left corner of subwindow
+ tmpy = (i/setsx)*(winy/setsy); // y coordinate of top left corner of subwindow
- local( i, count, ilev ); // sets curr data array, finds local max/min
+ local( i, count, ilev ); // reads curr data array, finds local max/min
- win[i]=glutCreateSubWindow( parent, tmpx, tmpy, winx/setsx, winy/setsy );
- gluOrtho2D( 0, NX+35, 0, NY+15 ); // sets how data is mapped to window
- glutDisplayFunc( display );
- glutKeyboardFunc( key ); // called on key press
- glutSpecialFunc( specialkey ); // called on special key press (arrow keys)
-
- glutCreateMenu( menu ); // adds a menu
- glutAddMenuEntry( "Local Color Scale", 1 ); // describes choices
- glutAddMenuEntry( "Global Color Scale", 2 );
- glutAddMenuEntry( "User-Set Color Scale", 3 );
- glutAttachMenu( GLUT_RIGHT_BUTTON ); // menu called on right click
+ win[i]=glutCreateSubWindow( parent, tmpx, tmpy, winx/setsx, winy/setsy ); // creates subwindow
+ gluOrtho2D( 0, NX+35, 0, NY+15 ); // sets how data is mapped to subwindow
+ glutDisplayFunc( display ); // sets display func for subwindow
+ glutKeyboardFunc( key ); // called on key press
+ glutSpecialFunc( specialkey ); // called on special key press (arrow keys)
+
+ glutCreateMenu( menu ); // adds a menu
+ glutAddMenuEntry( "Local Color Scale", 1 ); // adds a menu entry
+ glutAddMenuEntry( "Global Color Scale", 2 ); // adds a menu entry
+ glutAddMenuEntry( "User-Set Color Scale", 3 ); // adds a menu entry
+ glutAddMenuEntry( "Log Scale (on/off)", 4 ); // adds a menu entry
+ glutAttachMenu( GLUT_RIGHT_BUTTON ); // menu called on right click
}
- glutMainLoop();
+ glutMainLoop(); // begin processing events
return 0;
}
| ViewVC Help | |
| Powered by ViewVC 1.1.22 |