Sponsored Content
Top Forums Shell Programming and Scripting Using IF statements with maths where the input is not an integer Post 302546812 by StudentFitz on Friday 12th of August 2011 08:29:21 AM
Old 08-12-2011
Using IF statements with maths where the input is not an integer

Hi All

I've made a few scripts which using GDAL extract the value of a pixel within a given raster. The purpose is to work out the combine value of every pixel. I thought there may have been an easier way to do this but alas!

The code below extracts the pixel value at position X Y. The value is a floating point rather than an integer and thats where my problem lies. The if statement simply exports the value to a txt file. Other scripts take care of the rest ie change values of X Y and summing the resultant txt files.
Code:
#!/bin/sh
input=`gdallocationinfo -valonly 210_1515_volume.env X Y`  # The value of the pixel at coordinate X Y
if [ $input -gt 0 ] ;then
	echo $input > colrow.txt
fi

However, I get the following message back when ever my statement is true because my value isn't an integer;

extract2.sh: line 4: [: 2.15787172317505: integer expression expected

Please note the raster file mostly contains zero values, and its only the values greater than zero I'm interested in, which is why I added the expression.

Any ideas how I can alter this code so the IF statement excepts integers? Alternatively if anyone knows of a faster method of quickly summing the values of a raster file, that would be equally useful.

Many thanks in advance for any assistance you can offer.

Andy

Last edited by vbe; 08-12-2011 at 09:50 AM.. Reason: please use code tags!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with Maths

Heres a script i wrote as a bit of practise. What it does is insert a line in the middle of a file. The line being $1 and the file being $2 #!/bin/bash rm tempfile touch tempfile count=1 linenum= `wc -l < $2` if then echo $1 >> $2 else even=`expr "$linenum" % 2` if then... (3 Replies)
Discussion started by: Quesa
3 Replies

2. Shell Programming and Scripting

isql input file with multiple sql statements

I've got: isql -U $USERID -S $SERVER -D $DATABASE -i inputfile.sql -o outputfile.txt in inputfile I have: go sql#1 go sql#2 go sql#3 go I also tried without "go" and with";" instead which did not work SQL statements will work if I paste them directly into the script and use EOF ... (0 Replies)
Discussion started by: Cailet
0 Replies

3. Shell Programming and Scripting

Perl - maths equation - need help

if input to the perl program is ' ( p * ((a+b) * (c+d))) + q ' it shuld give the output as ' pac + pad + pbc + pbd + q ' .can anyone suggest a way to do this ? (7 Replies)
Discussion started by: Anuj8584
7 Replies

4. Programming

How i can read a long integer from standar input and a string with as many characters as specified..

how i can read a long integer from standar input and a string with as many characters as specified in the number? i thing that i must use the read command ofcourse.... (6 Replies)
Discussion started by: aintour
6 Replies

5. Shell Programming and Scripting

Maths with variables

Hello, I'm trying to write a while loop for a decimal value in tcsh which I know can't be done. Instead I want my increments to be one order of magnitude too large and then divide it by 10 when I use the variable. However, I don't know how to divide my variable and set it as another. set... (1 Reply)
Discussion started by: DFr0st
1 Replies

6. UNIX for Dummies Questions & Answers

Check if input is an integer or a floating point?

Hiii I actually intent to check the integer or floating point number input by user i.e. 23, 100, 55.25, 12.50 ..etc. However, when someone input strings or alpha character, my program has to show invalid input.!! Is there any Unix shell script syntax can help me to check ? Thanking you (2 Replies)
Discussion started by: krishnampkkm
2 Replies

7. Programming

I don't know how to replace input char with appropriate integer

Hi guys, I asked for help on programming forums and no one didn't helped me so I ask for help here. I am playing with some tasks from my book and I can't figure where did I get wrong. From the first program I get a blank screen, program won't generate 10*10 matrix. And second problem is I... (6 Replies)
Discussion started by: solaris_user
6 Replies

8. Shell Programming and Scripting

Simple maths calculator loop.

Hi, I am trying to make a maths calculator that: 1. Prompts the user for a number. 2. Prompts the user for an operation (add, subtract, divide or multiply) 3. Prompts the user for a number. 4. Prompts the user for another operation (same as above) OR the option to get the result for the... (4 Replies)
Discussion started by: johnthebaptist
4 Replies

9. Shell Programming and Scripting

Maths in shell scripts

Hi, Need help on this. I need to increment a variable by 1 but retain as 2 characters. I am using expr to do additions: NEWSERIAL=`expr $SERIAL + 1` $SERIAL can range from 01-99. After adding "1", I need the result to be 2 characters, eg: 02+1 = 03. By default expr will truncate the... (4 Replies)
Discussion started by: vchee
4 Replies

10. Shell Programming and Scripting

Calculate the constant e to 14+ decimal places using integer maths.

Hi guys... I am loving this integer maths thing. 64 bit systems are certainly easier than 32 bit, but hey, I don't intend to leave out my fav' platform. Using one of the 'Brothers' methods, URL inside the code. #!/bin/sh # # #!/usr/local/bin/dash # e_constant.sh # Brother's formula . #... (2 Replies)
Discussion started by: wisecracker
2 Replies
GLCOPYPIXELS(3G)														  GLCOPYPIXELS(3G)

NAME
glCopyPixels - copy pixels in the frame buffer C SPECIFICATION
void glCopyPixels( GLint x, GLint y, GLsizei width, GLsizei height, GLenum type ) PARAMETERS
x, y Specify the window coordinates of the lower left corner of the rectangular region of pixels to be copied. width, height Specify the dimensions of the rectangular region of pixels to be copied. Both must be nonnegative. type Specifies whether color values, depth values, or stencil values are to be copied. Symbolic constants GL_COLOR, GL_DEPTH, and GL_STENCIL are accepted. DESCRIPTION
glCopyPixels copies a screen-aligned rectangle of pixels from the specified frame buffer location to a region relative to the current raster position. Its operation is well defined only if the entire pixel source region is within the exposed portion of the window. Results of copies from outside the window, or from regions of the window that are not exposed, are hardware dependent and undefined. x and y specify the window coordinates of the lower left corner of the rectangular region to be copied. width and height specify the dimensions of the rectangular region to be copied. Both width and height must not be negative. Several parameters control the processing of the pixel data while it is being copied. These parameters are set with three commands: glPixelTransfer, glPixelMap, and glPixelZoom. This reference page describes the effects on glCopyPixels of most, but not all, of the parameters specified by these three commands. glCopyPixels copies values from each pixel with the lower left-hand corner at (x + i, y + j) for 0 <= i < width and 0 <= j < height. This pixel is said to be the ith pixel in the jth row. Pixels are copied in row order from the lowest to the highest row, left to right in each row. type specifies whether color, depth, or stencil data is to be copied. The details of the transfer for each data type are as follows: GL_COLOR Indices or RGBA colors are read from the buffer currently specified as the read source buffer (see glReadBuffer). If the GL is in color index mode, each index that is read from this buffer is converted to a fixed-point with an unspecified number of bits to the right of the binary point. Each index is then shifted left by GL_INDEX_SHIFT bits, and added to GL_INDEX_OFFSET. If GL_INDEX_SHIFT is negative, the shift is to the right. In either case, zero bits fill otherwise unspecified bit locations in the result. If GL_MAP_COLOR is true, the index is replaced with the value that it references in lookup table GL_PIXEL_MAP_I_TO_I. Whether the lookup replacement of the index is done or not, the integer part of the index is then ANDed with (2^b) -1, where b is the number of bits in a color index buffer. If the GL is in RGBA mode, the red, green, blue, and alpha components of each pixel that is read are converted to an inter- nal floating-point with unspecified precision. The conversion maps the largest representable component value to 1.0, and component value 0 to 0.0. The resulting floating-point color values are then multiplied by GL_c_SCALE and added to GL_c_BIAS, where c is RED, GREEN, BLUE, and ALPHA for the respective color components. The results are clamped to the range [0,1]. If GL_MAP_COLOR is true, each color component is scaled by the size of lookup table GL_PIXEL_MAP_c_TO_c, then replaced by the value that it references in that table. c is R, G, B, or A. If the GL_ARB_imaging extension is supported, the color values may be additionally processed by color-table lookups, color- matrix transformations, and convolution filters. The GL then converts the resulting indices or RGBA colors to fragments by attaching the current raster position z coordinate and texture coordinates to each pixel, then assigning window coordinates (xr + i , yr + j), where (xr , yr) is the current raster position, and the pixel was the ith pixel in the jth row. These pixel fragments are then treated just like the frag- ments generated by rasterizing points, lines, or polygons. Texture mapping, fog, and all the fragment operations are applied before the fragments are written to the frame buffer. GL_DEPTH Depth values are read from the depth buffer and converted directly to an internal floating-point with unspecified precision. The resulting floating-point depth value is then multiplied by GL_DEPTH_SCALE and added to GL_DEPTH_BIAS. The result is clamped to the range [0,1]. The GL then converts the resulting depth components to fragments by attaching the current raster position color or color index and texture coordinates to each pixel, then assigning window coordinates (xr + i , yr + j), where (xr , yr) is the current raster position, and the pixel was the ith pixel in the jth row. These pixel fragments are then treated just like the fragments generated by rasterizing points, lines, or polygons. Texture mapping, fog, and all the fragment operations are applied before the fragments are written to the frame buffer. GL_STENCIL Stencil indices are read from the stencil buffer and converted to an internal fixed-point with an unspecified number of bits to the right of the binary point. Each fixed-point index is then shifted left by GL_INDEX_SHIFT bits, and added to GL_INDEX_OFFSET. If GL_INDEX_SHIFT is negative, the shift is to the right. In either case, zero bits fill otherwise unspecified bit locations in the result. If GL_MAP_STENCIL is true, the index is replaced with the value that it references in lookup table GL_PIXEL_MAP_S_TO_S. Whether the lookup replacement of the index is done or not, the integer part of the index is then ANDed with (2^b)-1, where b is the number of bits in the stencil buffer. The resulting stencil indices are then written to the stencil buffer such that the index read from the ith location of the jth row is written to location (xr + i , yr + j), where (xr , yr) is the current raster position. Only the pixel ownership test, the scissor test, and the stencil writemask affect these write operations. The rasterization described thus far assumes pixel zoom factors of 1.0. If glPixelZoom is used to change the x and y pixel zoom factors, pixels are converted to fragments as follows. If (xr, yr) is the current raster position, and a given pixel is in the ith location in the jth row of the source pixel rectangle, then fragments are generated for pixels whose centers are in the rectangle with corners at (xr + zoomx*i, yr + zoomy*j) and (xr + zoomx*(i+1), yr + zoomy*(j+1)) where zoomx is the value of GL_ZOOM_X and zoomy is the value of GL_ZOOM_Y. EXAMPLES
To copy the color pixel in the lower left corner of the window to the current raster position, use glCopyPixels(0, 0, 1, 1, GL_COLOR); NOTES
Modes specified by glPixelStore have no effect on the operation of glCopyPixels. ERRORS
GL_INVALID_ENUM is generated if type is not an accepted value. GL_INVALID_VALUE is generated if either width or height is negative. GL_INVALID_OPERATION is generated if type is GL_DEPTH and there is no depth buffer. GL_INVALID_OPERATION is generated if type is GL_STENCIL and there is no stencil buffer. GL_INVALID_OPERATION is generated if glCopyPixels is executed between the execution of glBegin and the corresponding execution of glEnd. ASSOCIATED GETS
glGet with argument GL_CURRENT_RASTER_POSITION glGet with argument GL_CURRENT_RASTER_POSITION_VALID SEE ALSO
glColorTable, glConvolutionFilter1D, glConvolutionFilter2D, glDepthFunc, glDrawBuffer, glDrawPixels, glMatrixMode, glPixelMap, glPixelTransfer, glPixelZoom, glRasterPos, glReadBuffer, glReadPixels, glSeparableFilter2D, glStencilFunc GLCOPYPIXELS(3G)
All times are GMT -4. The time now is 05:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy