procedure getregion(image,output,format) string image ="" {prompt="name of input image"} string output ="" {prompt="name of output region file"} string format ="" {prompt="output format",enum="full|basic|x|y"} real zlow =INDEF {prompt="lower cut level"} real zhigh =INDEF {prompt="upper cut level"} bool imageid =no {prompt="prepend image name to region string?"} bool boxcen =no {prompt="expand box center to rectangular region?"} int boxxsize=512 {prompt="x size of box region if boxcen=yes"} int boxysize=512 {prompt="y size of box region if boxcen=yes"} bool chkbound=no {prompt="check pixel region bounds?"} bool append =no {prompt="append to existing output file?"} bool redispl =yes {prompt="(re)display image first?"} bool verbose =yes {prompt="verbose messages?"} # @(#) task getregion Author: R.A. Jansen -- June 19 1995 # @(#) # @(#) task to mark rectangular regions on the display by pointing at # @(#) the lower left and upper right corners of the region. The region # @(#) coordinates (two supported formats) are appended to a regionfile. # @(#) The marked region will be colored bright green on the display. # @(#) # @(#) Feb 6 1996 -- Implemented region expansion to square box given # @(#) a center position. Prepending image ID to region # @(#) string is optional in both normal and box mode. # @(#) Box is marked bright red in region expansion mode. # @(#) Jun 9 1997 -- General rewrite of this task: tvmark is used # @(#) instead of scan to read the curser, eliminating # @(#) the mandatory and relocation of the # @#() cursor between regions. The 'b' key is used for # @(#) marking instead of the bar; 'q' is quit. # @(#) Optionally, re-displaying of the image can be sup- # @(#) pressed and output may be appended to an existing # @(#) output file. Added formats "x" and "y" for listing # @(#) a set of x- or y-ranges (e.g. "2:15,21:50,63:99") # @(#) Jun 14 2000 -- Added support for multi-extension / N-dim. images. # @(#) Apr 9 2001 -- Changed square box to rectangular box expansion. # @(#) Added bounds checking switch. RAJ. # @(#) Feb 19 2002 -- Added verbose switch. RAJ begin string img, tmp, out, fm, imtyp, rpstr, rgnstr file tmptvmark string cmd, cursor, key, arg, val, previous, outstr real zlo, zhi int imglen, bxsiz, bysiz, bcolor, ncol,nrow, t, x,y, x0,y0, nr int dx1,dx2, dy1,dy2, store # Initialize... img = image out = output fm = format zlo = zlow zhi = zhigh bxsiz = boxxsize bysiz = boxysize bcolor = 205 if ( fm == "x" || fm == "y" ) { boxcen = no bcolor = 206 } if (boxcen) { bcolor = 204 } nr = 0 imtyp = "."//envget("imtype") rpstr = "" rgnstr = "" # Check whether image exists ... unlearn chkimg chkimg (img, "access", verbose=yes) if (!chkimg.ok) { return } img = chkimg.imroot # The image exists. Check whether a region or image plane was # specified ... cparse (chkimg.imrpstr, delim="]") if (cparse.nfields>0) { if (strlen(cparse.field1) == 2 ) { rpstr = cparse.field1//"]" } else { rgnstr = cparse.field1//"]" } if (cparse.nfields==2) { rgnstr = cparse.field2//"]" } } # Determine the image size ... imgets (img//imtyp//rpstr, "naxis1"); ncol = int(imgets.value) imgets (img//imtyp//rpstr, "naxis1"); ncol = int(imgets.value) imgets (img//imtyp//rpstr, "naxis2"); nrow = int(imgets.value) if (boxcen && (bxsiz>ncol || bysiz>nrow) ) { error(1,"WARNING: boxsize "//str(bxsiz)//"x"//str(bysiz)//" exceeds size of image "//str(ncol)//"x"//str(nrow)//" !") } # Check whether the output file name is given as an extension if ( substr(out,1,1) == "." ) { out = img//out } # If an older version of the output file is present and append=no, # rename it... if (access(out)&&!append) { print ("Renaming older version of output file...") if ( access(out//"~") ) { delete (out//"~", yes, verify=no) } copy (out, out//"~", verbose=no) print (" "//out//" --> "//out//"~") delete (out, yes, verify=no) } else if ( access(out) && verbose ) { print ("Appending to existing region file "//out//" ...") } # Display image if redispl=yes if (redispl) { if (verbose) { print ("Displaying "//img//imtyp//rpstr//rgnstr//" ...") } if ( zlo == INDEF ) { zlo = 0. } if ( zhi == INDEF ) { zhi = 0. } xdisplay (img//imtyp//rpstr//rgnstr, z1=zlo, z2=zhi, zmode="linear") } if (boxcen) { if ( mod(bxsiz,2) == 0 ) { dx1 = bxsiz/2 - 1 dx2 = bxsiz/2 } else { dx1 = int(bxsiz/2) dx2 = dx1 } if ( mod(bysiz,2) == 0 ) { dy1 = bysiz/2 - 1 dy2 = bysiz/2 } else { dy1 = int(bysiz/2) dy2 = dy1 } } tmptvmark = mktemp("tvmark") # All preliminaries have been taken care of; now mark the regions... if (boxcen) { if (verbose) { print ("Wait for the cursor cross to appear in the image display area,") print ("then mark all regions using the \"r\" key. Type \"q\" to quit.") } else { print ("Mark regions using the \"r\" key.") } sleep(1) tvmark (frame=1, coor="", logfile=tmptvmark, autolog=yes, outimage="", deletions="", commands="", mark="rectangle", radii="0", lengths=str(bxsiz)//" "//str(real(bysiz)/bxsiz), font="raster", color=bcolor, label=no, number=no, nxoffset=0, nyoffset=0, pointsize=0, txsize=0, tolerance=1.5, interactive=yes, > "dev$null", >& "STDERR" ) } else if ( fm == "x" || fm == "y" ) { if (verbose) { print ("Wait for the cursor cross to appear in the image display area,") print ("then mark all regions using the \".\" key. Type \"q\" to quit.") } else { print ("Mark regions using the \".\" key.") } sleep(1) outstr = "" if (imageid) { outstr = img//imtyp//rpstr//" " } tvmark (frame=1, coor="", logfile=tmptvmark, autolog=yes, outimage="", deletions="", commands="", mark="none", radii="0", lengths="0", font="raster", color=bcolor, label=no, number=no, nxoffset=0, nyoffset=0, pointsize=1, txsize=1, tolerance=1.5, interactive=yes, > "dev$null", >& "STDERR" ) } else { if (verbose) { print ("Wait for the cursor cross to appear in the image display area,") print ("then mark all regions using the \"b\" key. Type \"q\" to quit.") } else { print ("Mark regions using the \"b\" key.") } sleep(1) tvmark (frame=1, coor="", logfile=tmptvmark, autolog=yes, outimage="", mark="plus", radii="0", lengths="0", label=no, number=no, nxoffset=0, nyoffset=0, txsize=1, pointsize=3, font="raster", color=bcolor, commands="", interactive=yes, > "dev$null", >& "STDERR" ) } list = tmptvmark previous = "" while( fscan(list,x,y,key,cmd,arg,val) != EOF ) { if ( x < 1 ) x = 1 if ( x > ncol ) x = ncol if ( y < 1 ) y = 1 if ( y > nrow ) y = nrow if ( cmd == "r" && boxcen ) { x0 = x - dx1 x = x + dx2 y0 = y - dy1 y = y + dy2 if (chkbound) { if ( x0 < 1 ) { x = x - x0 + 1 x0 = 1 } if ( y0 < 1 ) { y = y - y0 + 1 y0 = 1 } if ( x > ncol ) { x0 = x0 - x + ncol x = ncol } if ( y > ncol ) { y0 = y0 - y + nrow y = nrow } } if ( fm == "basic" ) { if (imageid) { print (img//imtyp//rpstr, x0,x,y0,y, >> out) } else { print (x0,x,y0,y, >> out) } } else { if (imageid) { print (img//imtyp//rpstr//"["//x0//":"//x//","//y0//":"//y//"]", >> out) } else { print ("["//x0//":"//x//","//y0//":"//y//"]", >> out) } } previous = "" } else if ( cmd == "b" && !(fm=="x"||fm=="y") ) { if ( previous == "" ) { previous = cmd x0 = x y0 = y } else if ( previous == "b" ) { if ( x0 > x ) { t = x0 x0 = x x = t } if ( y0 > y ) { t = y0 y0 = y y = t } if ( fm == "basic" ) { if (imageid) { print (img//imtyp//rpstr, x0,x,y0,y, >> out) } else { print (x0,x,y0,y, >> out) } } else { if (imageid) { print (img//imtyp//rpstr//"["//x0//":"//x//","//y0//":"//y//"]", >> out) } else { print ("["//x0//":"//x//","//y0//":"//y//"]", >> out) } } previous = "" } else { print(" Error: invalid sequence ",> "STDERR") previous = "" } } else if ( cmd == "." && (fm=="x"||fm=="y") ) { if ( previous == "" ) { previous = cmd x0 = x y0 = y } else if ( previous == "." ) { nr = nr + 1 if ( nr > 1 ) { outstr = outstr//"," } if ( x0 > x ) { t = x0 x0 = x x = t } if ( y0 > y ) { t = y0 y0 = y y = t } if ( fm == "x" ) { if ( nr == 1 ) { store = y } outstr = outstr//x0//":"//x print (x0,store," 206 .", >> "tmptvcmd") print (x0,(store-3)," 206 s", >> "tmptvcmd") print (x0,(store+3)," 206 s", >> "tmptvcmd") print (x0,store," 206 s", >> "tmptvcmd") print (x,store," 206 s", >> "tmptvcmd") print (x,(store-3)," 206 s", >> "tmptvcmd") print (x,(store+3)," 206 s", >> "tmptvcmd") print (x0,(store+1)," 206 .", >> "tmptvcmd") print (x0,(store-2)," 206 s", >> "tmptvcmd") print (x0,(store+4)," 206 s", >> "tmptvcmd") print (x0,(store+1)," 206 s", >> "tmptvcmd") print (x,(store+1)," 206 s", >> "tmptvcmd") print (x,(store-2)," 206 s", >> "tmptvcmd") print (x,(store+4)," 206 s", >> "tmptvcmd") } else { if ( nr == 1 ) { store = x } outstr = outstr//y0//":"//y print (store,y0," 206 .", >> "tmptvcmd") print ((store-3),y0," 206 s", >> "tmptvcmd") print ((store+3),y0," 206 s", >> "tmptvcmd") print (store,y0," 206 s", >> "tmptvcmd") print (store,y," 206 s", >> "tmptvcmd") print ((store-3),y," 206 s", >> "tmptvcmd") print ((store+3),y," 206 s", >> "tmptvcmd") print ((store+1),y0," 206 .", >> "tmptvcmd") print ((store-2),y0," 206 s", >> "tmptvcmd") print ((store+4),y0," 206 s", >> "tmptvcmd") print ((store+1),y0," 206 s", >> "tmptvcmd") print ((store+1),y," 206 s", >> "tmptvcmd") print ((store-2),y," 206 s", >> "tmptvcmd") print ((store+4),y," 206 s", >> "tmptvcmd") } previous = "" } else { print(" Error: invalid sequence ",> "STDERR") previous = "" } } else { print(" Warning: skipping unrecognized command ",cmd, > "STDERR") previous = "" } } list = "" delete (tmptvmark, yes, verify=no) if ( fm == "x" || fm == "y" ) { print (outstr, >> out) tvmark (frame=1, coor="", logfile="", autolog=no, outimage="", deletions="", commands="tmptvcmd", mark="point", radii="0", lengths="2", font="raster", color=bcolor, label=no, number=no, nxoffset=0, nyoffset=0, pointsize=1, txsize=1, tolerance=1.5, interactive=no, > "dev$null", >& "STDERR" ) sleep(2) delete("tmptvcmd", verify=no) } if (verbose) { print ("Finished. Regionfile is '"//out//"'.") } end