| | 1 | Opihi is a generic command-line interpreter which has been used as the front-end for DVO, Pantasks, and other high-level programs provided by the IPP. These programs have been inherited from the CFHT Elixir project. The interpreter has a variety of features of shell-scripting languages: if statements, for loops, macros with command-line arguments, and so forth. The shell also allows for variables, arithmetic on variables, input from source files, and a variety of other useful tools. Opihi has a simple command-line interaction that resembles the UNIX tcsh, but with many additional useful features. It can also be used as a scripting language much like sh or perl. |
| | 2 | |
| | 3 | Opihi includes tools to manipulate and display 1-D (vector) and 2-D (image) data. The image and graphics display tool is an external program called 'Kapa'. Various functions are available to perform math, statistical, and other operations on vectors and images. |
| | 4 | |
| | 5 | == User Interface == |
| | 6 | |
| | 7 | The command-line interaction is based on the readline libraries and behaves like tcsh. Arrows can be used for editing. There is both command and file completion with the TAB key. You can also use emacs-like commands such as ctrl-a to reach the beginning of the line and ctrl-e to reach the end. It is also possible to type just a fraction of a command, as long as it is unique. An ambiguous command will list the possible alternatives. For example: |
| | 8 | |
| | 9 | opihi: c |
| | 10 | ambiguous command: c ( catalog cgrid clear create cursor ) |
| | 11 | |
| | 12 | |
| | 13 | Multiple commands can generally be placed on one line with semi-colons as separators. |
| | 14 | |
| | 15 | == Data Representations == |
| | 16 | |
| | 17 | === Simple Scalar Variables === |
| | 18 | |
| | 19 | Scalar variables in Opihi are prepended with a dollar sign ($). A variable may be created and the value assigned by a line which looks like: |
| | 20 | |
| | 21 | $var = (expression) |
| | 22 | |
| | 23 | where (expression) is some math expression. The math expression may consist of the standard math operators (+,-,*,/) as well as any already-defined variables and the functions log(), ln(), sqrt(), exp(), ten() (10x), sin(), cos(), etc. Other special operators are the carret (^) for exponetiation (eg, 2^3 is 8) and the @ symbol as a binary arc-tangent (eg, 1@3 is 18.43 degrees). In addition, the operator << returns the minimum of the two surrounding values, and >> returns the maximum. Variables can be numeric or character strings. If the shell does not understand the syntax of the line as a math expression, it is assumed to be a string. local variables |
| | 24 | |
| | 25 | If there is a pair of curly brackets {} anywhere on a command line, whatever is inside is assumed to be a math expression and evaluated before the line is executed. This later feature allows functions of variables to be passed as arguments to Opihi functions. (see also the discussion below about temporary vectors and images). For example: |
| | 26 | |
| | 27 | echo {$fred*dcos(45)} |
| | 28 | |
| | 29 | would give the response 7.07107 if $fred had the value of 10.0. There are math functions cos, sin, and tan, which operate on radian expressions, and also dcos, dsin, dtan, which operate on degree expressions. There are also the equivalent inverse functions: eg., asin and dasin return radians and degrees, respectively. The help section on Math defines all of the available math functions. |
| | 30 | |
| | 31 | === Vectors === |
| | 32 | |
| | 33 | The Opihi language supports 1D numerical vectors as a native data type. Vector names are ''not'' prepended with an special characters (ie, no dollar sign unlike for scalars). There are a few rules governing valid vector names: they may not start with a number (''why not?''); they may only use the following characters: letters, numbers (expect the first), underscore, (NOTE: finish this list and validate). |
| | 34 | |
| | 35 | NOTE: as of 2009.01.17, the Opihi parser allows the creation of vectors with invalid characters such as "-" (the minus sign!). Be careful until this bug is fixed. |
| | 36 | |
| | 37 | ==== Vector Manipulation Functions ==== |
| | 38 | |
| | 39 | create -- create a new vector |
| | 40 | hist -- create histogram from a vector |
| | 41 | print -- write vectors to file |
| | 42 | vectors -- list vectors |
| | 43 | accum -- accumulate vector values in another vector |
| | 44 | applyfit -- apply fit to new vector |
| | 45 | applyfit2d -- apply 2-d fit to new vector |
| | 46 | circstats -- circular statistics |
| | 47 | concat -- reduce vector dimension |
| | 48 | datafile -- define file to read vectors |
| | 49 | delete -- delete vectors or matrices |
| | 50 | fft1d -- fft on the pixel-stream in an image |
| | 51 | fit -- fit polynomial to vector pair |
| | 52 | gaussdev -- generate a gaussian deviate vector |
| | 53 | gaussint -- return the integrated gaussian vector |
| | 54 | histogram -- generate histogram from vector |
| | 55 | integrate -- integrate a vector |
| | 56 | interpolate -- interpolate between vector pairs |
| | 57 | vectors -- list vectors |
| | 58 | medacc -- accumulate vector values in another vector |
| | 59 | peak -- find vector peak in range |
| | 60 | read -- read vectors from datafile |
| | 61 | set -- image and vector math |
| | 62 | sort -- sort list of vectors |
| | 63 | subset -- expand vector dimension |
| | 64 | uniq -- create a uniq vector subset from a vector |
| | 65 | vbin -- bin values in a vector to be within a range |
| | 66 | vclip -- clip values in a vector to be within a range |
| | 67 | select -- selective vector assignment |
| | 68 | vgauss |
| | 69 | vgrid |
| | 70 | vload -- load vectors on Kii |
| | 71 | vstat -- get info from imreg database |
| | 72 | vsmooth -- gaussian smooth of a vector |
| | 73 | vroll -- roll vector elements |
| | 74 | vpop -- remove first element |
| | 75 | write -- write vectors to datafile |
| | 76 | |
| | 77 | |
| | 78 | === Images === |
| | 79 | |
| | 80 | |
| | 81 | |
| | 82 | === Lists === |
| | 83 | |
| | 84 | Opihi lists are grouped sets of scalar variables (which may be strings). A list consists of N variables with names of the form name:i, where the value of i ranges from 0 to N-1. In addition, the list length is defined as the value name:n. Since these are just informally grouped, a list may be defined by hand (ie, by defining each element and the length). There is also the command list which builds a list from the following lines until reaching a line consisting of the single word end: |
| | 85 | |
| | 86 | list sample |
| | 87 | value 0 |
| | 88 | value 1 |
| | 89 | test line |
| | 90 | end |
| | 91 | |
| | 92 | will define the variables $sample:0 - $sample:2, and $sample:n, with value of 3. |
| | 93 | |
| | 94 | The list command may also construct a list from the output of a UNIX command: |
| | 95 | |
| | 96 | list sample -x "ls /tmp" |
| | 97 | |
| | 98 | will result in a list consisting of one entry for each file in the listing. |
| | 99 | |
| | 100 | The list command may also be used to split a string by whitespace: |
| | 101 | |
| | 102 | list sample -split this is a test |
| | 103 | |
| | 104 | will result in a list with 4 elements, one for each word. |
| | 105 | |
| | 106 | === Queues === |
| | 107 | |
| | 108 | queuelist -- list existing queues |
| | 109 | queuesize -- return the length of the given queue |
| | 110 | queueinit -- create a zero-length queues |
| | 111 | queuedelete -- delete a queues |
| | 112 | queuepush -- add an item to the end of the queue |
| | 113 | queuepop -- remove and return the first item in the queue |
| | 114 | |
| | 115 | A queue is a data construct consisting of a sequence of lines from which simple selections can be made. Data items are added and removed from the queue with queuepush and queuepop commands which push entries on the end of the queue and pop them off the beginning. The available queues may be obtained with the command queuelist, and the length of a specific queue may be determined with the queuesize command. The contents of a queue may be printed with queueprint. Pushing data onto a non-existent queue will create the queue. An empty queue may be created with the command queueinit and a queue may be deleted with queuedelete. |
| | 116 | |
| | 117 | The queuepush commands allows for additional options which modify how the data is pushed on the queue. The -uniq flag specifies that the queue should be search for an existing match and not add the new data item if a matching item already exists. The -replace flag is similar to the -uniq flag, but instead the new item will replace the existing match, if a match is found. These two options have identical results if the match is made based on the entire line. However, they may be more usefully distinguished by specifying a restriction on the match with the -key flag. This flag specified which whitespace-separated element of the line to use for the match, with the first element being element 0. |
| | 118 | |
| | 119 | Numerical vectors and images (matrices) are discussed later in this document. |
| | 120 | Opihi Shell Programing and Flow-control |
| | 121 | |
| | 122 | input -- read command lines from a file |
| | 123 | macro -- deal with the macros |
| | 124 | for -- for loop |
| | 125 | if -- logical cases |
| | 126 | while -- while loop |
| | 127 | break -- escape from function |
| | 128 | continue -- next loop iteration |
| | 129 | |
| | 130 | |
| | 131 | There are several options for programming in the opihi shell. First, a file which contains a series of commands can be executed with input (filename). When an opihi-based program is started, it loads a file from the user's home directory, with a name of the form ~/.programrc. This resource file may contain any commands, and allows a user to customize his or her environment. |
| | 132 | |
| | 133 | It is also possible to define macros which will behave much like regular commands. A macro is defined by typing macro name or macro create name followed by the commands. Arguments to the macro are assigned to the variables $1 .. $N and the number of arguments is given by $0. Macros may be defined in input files. The following is a sample macro: |
| | 134 | |
| | 135 | macro test |
| | 136 | echo "this is a macro" |
| | 137 | echo "number of arguments $0" |
| | 138 | echo "first argument $1" |
| | 139 | if ($1 = 10) |
| | 140 | echo first argument is 10 |
| | 141 | end |
| | 142 | end |
| | 143 | |
| | 144 | == Flow Control == |
| | 145 | |
| | 146 | Opihi has several types of flow-control features. These include for-loops, while-loops, if-else blocks. These blocks are defined by the corresponding command (for, while, if) and are terminated with by a line with the single word end. |
| | 147 | |
| | 148 | === For loops === |
| | 149 | |
| | 150 | The for-loop syntax is simplistic. The for command specifies the loop variable, the starting value, the ending value, and optionally the delta for each loop. The implicit loop test is always to check if the loop variable is still less than the end value (or greater than if the delta value is negative). The definitions of this loop syntax and the value of the list length ($list:n) and the vector length (vector[]) make for natural loops over all elements of a list or vector. Below are a few examples: |
| | 151 | |
| | 152 | for i 1 10 0.1 |
| | 153 | echo $i |
| | 154 | end |
| | 155 | |
| | 156 | |
| | 157 | This runs the loop with the variable $i running from 1.0, 1.1, up to 9.9 (inclusive). |
| | 158 | |
| | 159 | for i 0 $list:n |
| | 160 | echo $list:$i |
| | 161 | end |
| | 162 | |
| | 163 | |
| | 164 | This would print all the elements of the list. |
| | 165 | |
| | 166 | for i 0 vector[] |
| | 167 | echo vector[$i] |
| | 168 | end |
| | 169 | |
| | 170 | |
| | 171 | This would print all the elements of the vector. |
| | 172 | |
| | 173 | === While loops === |
| | 174 | |
| | 175 | The while loop begins with a line of the form while (condition) and ends with a single end. The conditions follow the same rules as the if conditional statements. continue, break, auto-break concepts |
| | 176 | |
| | 177 | === If blocks === |
| | 178 | |
| | 179 | The if-block begins with a line of the form: if (condition) and ends with a single end. A line with a single else specifies the optional else portion of the block. The conditional expression is a valid math inequality with approximately C-syntax: |
| | 180 | |
| | 181 | (($i < 10) && ($i > 4)) |
| | 182 | |
| | 183 | |
| | 184 | The elements of the inequality may also be string comparisons. The only valid string comparisons are == and !=. |
| | 185 | |
| | 186 | === Other Flow Control === |
| | 187 | |
| | 188 | Additional control commands: return (from within a macro), break, continue, last (yes?) |
| | 189 | |
| | 190 | == Miscellaneous Commands == |
| | 191 | |
| | 192 | ! -- system call |
| | 193 | ? -- list commands |
| | 194 | ?? -- list variables |
| | 195 | echo -- type this line |
| | 196 | exec -- system call |
| | 197 | exit -- exit program |
| | 198 | help -- get help on a function |
| | 199 | config -- reload configuration information |
| | 200 | exit -- exit program |
| | 201 | quit -- exit program |
| | 202 | wait -- wait until return is typed |
| | 203 | sleep -- sleep for N seconds |
| | 204 | usleep -- sleep for N microseconds |
| | 205 | which -- show command |
| | 206 | cd -- change current working directory |
| | 207 | pwd -- report current working directory |
| | 208 | date -- return the current date |
| | 209 | file -- test file existence |
| | 210 | memory -- long listing of the allocated memory |
| | 211 | version -- list version information |
| | 212 | |
| | 213 | |
| | 214 | Most of these commands should be clear from the simple descriptions above, or from the online help. The command ?? prints the system variables. The help command will provide help on a single command or, without any arguments, will list all available help files (this includes general help not associated with a specific command). |
| | 215 | |
| | 216 | == String Manipulation and other data commands == |
| | 217 | |
| | 218 | local -- define local variables |
| | 219 | output -- redirect output to file |
| | 220 | scan -- scan line from keyboard or file to variable |
| | 221 | sprintf -- formatted print to a variable |
| | 222 | fprintf -- formatted print to standard output |
| | 223 | getchr -- find character in string |
| | 224 | strlen -- string length |
| | 225 | substr -- substring |
| | 226 | |
| | 227 | |
| | 228 | These commands provide a few tools to manipulate data in opihi variables. The #local/# command specifies that the given variable is local to its macro. The #output/# command changes the default output destination for functions which print results to the screen. This function does not zero the file length; new output is appended to the file. The command #scan/# reads specific lines from a file, an optionally places them in an opihi variable (EOF is returned at the end of a file). The group of commands #getchr/#, #strlen/#, and #substr/# provide the ability to manipulate the elements of a string. Finally, #sprintf/# and #fprintf/# perform formatted output to a variable and to the screen, respectively. |
| | 229 | |
| | 230 | == Graphing and the Kapa Display == |
| | 231 | |
| | 232 | box -- draw a box on the plot |
| | 233 | clear -- erase plot |
| | 234 | cursor -- get coords from cursor |
| | 235 | grid -- plot cartesian grid |
| | 236 | labels -- define labels for plot |
| | 237 | limits -- define plot limits |
| | 238 | plot -- plot a pair of vectors |
| | 239 | ps -- define labels for plot |
| | 240 | style -- set the style for graph plots |
| | 241 | zplot -- plot scaled points |
| | 242 | device -- set / get current graphics device |
| | 243 | jpeg -- write text line on graph |
| | 244 | line -- plot line |
| | 245 | dot -- plot a single point |
| | 246 | resize -- set graphics/image window size |
| | 247 | section -- define section of graph |
| | 248 | textline -- write text line on graph |
| | 249 | zplot -- plot x y with size scaled by z |
| | 250 | |
| | 251 | == Math and Statistical Functions == |
| | 252 | |
| | 253 | Opihi has a variety of commands to manipulate 1-D data (vectors). Vectors can be loaded from a data file or created with uniform spacing. New vectors can be defined as the arithmetical combinations of other vectors. For example, we could create a sine wave with the following two lines: |
| | 254 | |
| | 255 | A vector can be created based on a number sequence with the command create name Nelements start delta. The resulting vector has #Nelements/# entries, starting at a value of #start/# and running until #start + delta*Nelements/#. If #delta/# is 0.0, all elements will have the value of #start/#. A histogram of a vector may be made with the command hist, which creates a new vector containing the histogram of the first vector. The data range and bin size of the histogram are defined in same way as with create. This makes it easy to create the index vector that goes with a histogram vector: |
| | 256 | |
| | 257 | hist y Ny 1 100 0.1 |
| | 258 | create dx 1 100 0.1 |
| | 259 | |
| | 260 | |
| | 261 | The above will create a histogram of y in Ny and the index in dx. Plotting this with plot dx Ny will show the histogram. |
| | 262 | |
| | 263 | Vector math is performed with a command of the form set new = (expression). The expression is some math function employing vectors and scalars. A complete listing of the math operators available in set can be found in the help for set. |
| | 264 | |
| | 265 | Once vectors are defined, they may be plotted. A pair of vectors can be plotted against each other if they have the same number of entries. The plotting is performed on the graphics window, Kapa. There are actually several graphics windows available to status, any of which may be used to plot at any time. Some of the more complex operations default to either graphics window 0 or 1, depending on the context. Except for those functions with a pre-defined window, all plotting functions apply to the current graphics window unless an option -n N is given to specify a different window. The plotting style is determined by the command style which can set the line width, the line type (solid, dashed, dotted, etc), the point type (box, cross, etc), the point size, the color, and whether a pair of vectors is plotted as a sequence of points, a set of connected lines, or a histogram. Some functions which make plots use their own styles, as discussed below. The function limits lets the user set the range of the plot axes, or check the current setting. The command plot will plot a pair of vectors on the current graphics window using the current plotting style for that window. The command zplot will plot a pair of vectors with the point size scaled by a third vector, with maximum and minimum point sizes representing specified values. The cursor command goes the other way: this command puts the Kapa window in cursor mode and waits for input from Kapa. The user can then type any alphanumeric key on the graphics windows and will be told both the pointer location (in the graphics coordinates) and will have the coordinates stored in status variables. For example, by typing ``1'' in the sky display window, the RA and DEC of the pointer are stored in the variables $R1 and $D1. This command can be used to let the user define locations or regions of interest on the Kapa window. (Future addition: button, which does the same with the mouse buttons). |
| | 266 | |
| | 267 | create x 1 20 0.01 |
| | 268 | set y = sin(x) |
| | 269 | |
| | 270 | |
| | 271 | We can plot this pair of vectors on the Kapa graphing window: |
| | 272 | |
| | 273 | limit x y |
| | 274 | plot x y |
| | 275 | box |
| | 276 | |
| | 277 | |
| | 278 | The Kapa window has a variety of style options to change the plotting type (line, histogram, point), point type, line color, weight and style, and the errorbar style (if errorbars are plotted). |
| | 279 | |
| | 280 | == 2D Data and the Image Displays == |
| | 281 | |
| | 282 | Opihi has many commands to manipulate 2-D data (images). Images can be loaded from FITS files and mathematical operations applied to them. Various other operations, such as rebinning, shifting, rotating, and so forth are also available. Images can be displayed using the Kii image window. In an 8-bit visual, the image window has a dynamic colormap to allow the user to change the relationship between pixel value and the displayed color. There are three colormaps which can be selected by pressing the middle three buttons on the bottom row. The PS button produces a PostScript file from the image. The user can change the magnification and the position of the image with the mouse (left - recenter at cursor; middle - zoom out; right - zoom in). There are four color overlays which the user can draw objects of different shapes (circles, boxes, lines) or draw a contour. The four buttons labeled R, G, B, Y turn on or off the display of the red, green, blue, and yellow overlays. |
| | 283 | Image Manipulation Functions |
| | 284 | |
| | 285 | clip -- clip values in a buffer to be within a range |
| | 286 | contour -- create contour from image |
| | 287 | cut -- extract a cut across an image |
| | 288 | extract -- extract a portion of a buffer into another buffer |
| | 289 | fft2d -- fft on an image |
| | 290 | fit2d -- fit 2-d polynomial to vector triplet |
| | 291 | header -- print buffer header |
| | 292 | imcut -- linear image cut between arbitrary coords |
| | 293 | imhist -- histogram of an image region |
| | 294 | imsmooth -- circular gaussian smoothing |
| | 295 | kern -- convolve with 3x3 kernel |
| | 296 | keyword -- extract a FITS keyword from buffer header |
| | 297 | buffers -- list the currently allocated buffers |
| | 298 | mcreate -- create a matrix |
| | 299 | minterp -- interpolate image pixels |
| | 300 | rd -- load fits image |
| | 301 | rdseg -- read a segment of an image from a file |
| | 302 | rebin -- rebin data by factor of N |
| | 303 | roll -- roll image to new start point |
| | 304 | rotate -- rotate image |
| | 305 | set -- image and vector math |
| | 306 | shift -- shift data in an image |
| | 307 | spline.apply -- apply spline fit to generate an image |
| | 308 | spline.const -- create spline 2nd deriv. terms |
| | 309 | stats -- give statistics on a portion of a buffer |
| | 310 | subraster -- subraster of fits image |
| | 311 | unsign -- toggle the UNSIGN status |
| | 312 | wd -- write an image to a file |
| | 313 | zap -- delete pixels |
| | 314 | |
| | 315 | |
| | 316 | === Image Display Functions === |
| | 317 | |
| | 318 | ps -- define labels for plot |
| | 319 | center -- center image on coords |
| | 320 | device -- set / get current graphics device |
| | 321 | erase -- erase objects on an overlay |
| | 322 | jpeg -- write text line on graph |
| | 323 | load -- load an SAOimage style overlay |
| | 324 | point -- load overlay with single point |
| | 325 | save -- save an SAOimage style overlay |
| | 326 | resize -- set graphics/image window size |
| | 327 | tv -- display an image on the Kii window |
| | 328 | tvcontour -- send contour to overlay |
| | 329 | tvgrid -- wait until return is typed |
| | 330 | |
| | 331 | |
| | 332 | Other data functions |
| | 333 | |
| | 334 | gaussj -- solve Ax = B (N-D) |
| | 335 | gridify -- convert vector triplet to buffer |
| | 336 | ungridify -- convert buffer region to vector triplet |
| | 337 | dimendown -- convert matrix to vector |
| | 338 | dimenup -- convert vector to matrix |
| | 339 | mget -- extract a vector from a matrix |
| | 340 | mset -- insert a vector in a matrix |
| | 341 | svd -- singular value decomposition of a matrix |
| | 342 | swapbytes -- byte swap thing |
| | 343 | |
| | 344 | |
| | 345 | Programming Considerations |
| | 346 | |
| | 347 | The programming structure of the Opihi front-end makes it very easy to add commands to the package. |