diff options
author | Joe Hunkeler <jhunkeler@gmail.com> | 2015-08-11 16:51:37 -0400 |
---|---|---|
committer | Joe Hunkeler <jhunkeler@gmail.com> | 2015-08-11 16:51:37 -0400 |
commit | 40e5a5811c6ffce9b0974e93cdd927cbcf60c157 (patch) | |
tree | 4464880c571602d54f6ae114729bf62a89518057 /pkg/utilities/nttools/stxtools/tpcount.x | |
download | iraf-osx-40e5a5811c6ffce9b0974e93cdd927cbcf60c157.tar.gz |
Repatch (from linux) of OSX IRAF
Diffstat (limited to 'pkg/utilities/nttools/stxtools/tpcount.x')
-rw-r--r-- | pkg/utilities/nttools/stxtools/tpcount.x | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/pkg/utilities/nttools/stxtools/tpcount.x b/pkg/utilities/nttools/stxtools/tpcount.x new file mode 100644 index 00000000..f233aa26 --- /dev/null +++ b/pkg/utilities/nttools/stxtools/tpcount.x @@ -0,0 +1,134 @@ +include "template.h" + +# TP_COUNT -- Count the number of groups in an image +# +# B.Simon 02-Oct-98 Original +# B.Simon 26-Apr-99 Check value of NEXTEND before using + +int procedure tp_count (root) + +char root[ARB] # i: image name minus any sections +#-- +int imtype, count, lo, hi, mid +pointer sp, image, im + +int imgeti(), imaccf(), tp_imtype(), tp_hasgroup() +pointer immap() + +begin + # If the image is not a geis or fits file it can only have one group + + imtype = tp_imtype (root) + if (imtype != TP_GEIS && imtype != TP_FITS) + return (1) + + # Open the image to read the number of groups or extensions + # as recorded in the appropriate header keyword + + call smark (sp) + call salloc (image, SZ_FNAME, TY_CHAR) + + call strcpy (root, Memc[image], SZ_FNAME) + call strcat ("[0]", Memc[image], SZ_FNAME) + + iferr (im = immap (Memc[image], READ_ONLY, NULL)) { + # If image can't be opened, report an error condition + + count = ERR + + } else if (imtype == TP_GEIS) { + # Number of groups is trustworthy, report it to user + + count = imgeti (im, "GCOUNT") + call imunmap (im) + + } else { + # Number of extensions is not, it must be checked + + lo = 1 + hi = 0 + + # Check number of extensions + + if (imaccf (im, "NEXTEND") == YES) { + mid = imgeti (im, "NEXTEND") + + if (mid > 0) { + if (tp_hasgroup (root, mid) == NO) { + hi = mid + + } else { + lo = mid + mid = mid + 1 + + if (tp_hasgroup (root, mid) == NO) { + hi = mid + } else { + lo = mid + } + } + } + } + + # Find bracket for number of extensions + + while (hi < lo) { + mid = 2 * lo + + if (tp_hasgroup (root, mid) == NO) { + hi = mid + } else { + lo = mid + } + } + + # Use binary search to find actual number of extensions + + while (hi - lo > 1) { + mid = (hi + lo) / 2 + + if (tp_hasgroup (root, mid) == NO) { + hi = mid + } else { + lo = mid + } + } + + count = lo + call imunmap (im) + } + + call sfree (sp) + return (count) +end + +# TP_HASGROUP -- Determine if group is preent in image + +int procedure tp_hasgroup (root, index) + +char root[ARB] # i: image name +int index # i: index of group to check +#-- +int has +pointer sp, image, im + +pointer immap() + +begin + call smark (sp) + call salloc (image, SZ_FNAME, TY_CHAR) + + call sprintf (Memc[image], SZ_FNAME, "%s[%d]") + call pargstr (root) + call pargi (index) + + iferr (im = immap (Memc[image], READ_ONLY, NULL)) { + has = NO + } else { + call imunmap (im) + has = YES + } + + call sfree (sp) + return (has) +end |