Skip to content

Commit

Permalink
multiple -- updated for changes to grab
Browse files Browse the repository at this point in the history
these all use grab and needed updating
  • Loading branch information
trmrsh committed May 24, 2021
1 parent 2f43f8a commit b32013b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 156 deletions.
159 changes: 93 additions & 66 deletions hipercam/scripts/averun.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np

import hipercam as hcam
from hipercam import cline, utils
from hipercam import cline, utils, fringe
from hipercam.cline import Cline

__all__ = [
Expand All @@ -20,7 +20,8 @@

def averun(args=None):
"""``averun [source] (run first last twait tmax | flist) trim ([ncol
nrow]) bias dark flat [method sigma adjust clobber] output``
nrow]) bias dark flat fmap (fpair [nhalf rmin rmax]) [method sigma
adjust clobber] output``
Averages images from a run using median combination, skipping the junk
frames that result from NSKIP / NBLUE options in HiPERCAM and ULTRACAM
Expand Down Expand Up @@ -88,6 +89,32 @@ def averun(args=None):
flat : str
Name of flat field to divide by, 'none' to ignore.
flat : str
Name of flat field to divide by, 'none' to ignore.
fmap : str
Name of fringe map (see e.g. `makefringe`), 'none' to ignore.
fpair : str [if fmap is not 'none']
Name of fringe pair file (see e.g. `setfringe`). Required if
a fringe map has been specified.
nhalf : int [if fmap is not 'none', hidden]
When calculating the differences for fringe measurement,
a region extending +/-nhalf binned pixels will be used when
measuring the amplitudes. Basically helps the stats.
rmin : float [if fmap is not 'none', hidden]
Minimum individual ratio to accept prior to calculating the overall
median in order to reduce the effect of outliers. Although all ratios
should be positive, you might want to set this a little below zero
to allow for some statistical fluctuation.
rmax : float [if fmap is not 'none', hidden]
Maximum individual ratio to accept prior to calculating the overall
median in order to reduce the effect of outliers. Probably typically
< 1 if fringe map was created from longer exposure data.
method : str [hidden, defaults to 'm']
'm' for median, 'c' for clipped mean. See below for pros and cons.
Expand Down Expand Up @@ -129,8 +156,13 @@ def averun(args=None):
cl.register("nrow", Cline.GLOBAL, Cline.HIDE)
cl.register("flist", Cline.LOCAL, Cline.PROMPT)
cl.register("bias", Cline.LOCAL, Cline.PROMPT)
cl.register("dark", Cline.LOCAL, Cline.PROMPT)
cl.register("flat", Cline.LOCAL, Cline.PROMPT)
cl.register("dark", Cline.GLOBAL, Cline.PROMPT)
cl.register("flat", Cline.GLOBAL, Cline.PROMPT)
cl.register("fmap", Cline.GLOBAL, Cline.PROMPT)
cl.register("fpair", Cline.GLOBAL, Cline.PROMPT)
cl.register("nhalf", Cline.GLOBAL, Cline.HIDE)
cl.register("rmin", Cline.GLOBAL, Cline.HIDE)
cl.register("rmax", Cline.GLOBAL, Cline.HIDE)
cl.register("method", Cline.LOCAL, Cline.HIDE)
cl.register("sigma", Cline.LOCAL, Cline.HIDE)
cl.register("adjust", Cline.LOCAL, Cline.HIDE)
Expand Down Expand Up @@ -199,6 +231,30 @@ def averun(args=None):
ignore="none",
)

# fringe file (if any)
fmap = cl.get_value(
"fmap",
"fringe map ['none' to ignore]",
cline.Fname("fmap", hcam.HCAM),
ignore="none",
)

if fmap is not None:
fpair = cl.get_value(
"fpair", "fringe pair file",
cline.Fname("fringe", hcam.FRNG)
)
nhalf = cl.get_value(
"nhalf", "half-size of fringe measurement regions",
2, 0
)
rmin = cl.get_value(
"rmin", "minimum fringe pair ratio", -0.2
)
rmax = cl.get_value(
"rmax", "maximum fringe pair ratio", 1.0
)

cl.set_default("method", "m")
method = cl.get_value(
"method", "c(lipped mean), m(edian)", "c", lvals=("c", "m")
Expand Down Expand Up @@ -229,74 +285,45 @@ def averun(args=None):
if server_or_local:
print("\nCalling 'grab' ...")

# Build argument list
args = [None,"prompt",source,run,"yes",str(first),str(last)]
if trim:
args = [
None,
"prompt",
source,
run,
"yes",
str(first),
str(last),
"yes",
str(ncol),
str(nrow),
str(twait),
str(tmax),
"none",
"f32",
]
args += ["yes",str(ncol),str(nrow)]
else:
args = [
None,
"prompt",
source,
run,
"yes",
str(first),
str(last),
"no",
str(twait),
str(tmax),
"none",
"f32",
]
print("arg =", args)
args += ["no"]

args += [
str(twait),str(tmax),
"none" if bias is None else bias,
"none" if dark is None else dark,
"none" if flat is None else flat,
]
if fmap is None:
args += ["none", "f32"]
else:
args += [fmap,fpair,str(nhalf),str(rmin),str(rmax),"false","f32"]
flist = hcam.scripts.grab(args)

try:
print("\nCalling 'combine' ...")
if method == "m":
args = [
None,
"prompt",
flist,
"none" if bias is None else bias,
"none" if dark is None else dark,
"none" if flat is None else flat,
method,
adjust,
"usemean=yes",
"plot=no",
"yes" if clobber else "no",
output,
]
else:
args = [
None,
"prompt",
flist,
"none" if bias is None else bias,
"none" if dark is None else dark,
"none" if flat is None else flat,
method,
str(sigma),
adjust,
"usemean=yes",
"plot=no",
"yes" if clobber else "no",
output,
]
args = [
None,
"prompt",
flist,
"none", "none", "none",
method
]

if method != "m":
args += [str(sigma)]

args += [
adjust,
"usemean=yes",
"plot=no",
"yes" if clobber else "no",
output,
]
hcam.scripts.combine(args)

# remove temporary files
Expand Down
3 changes: 1 addition & 2 deletions hipercam/scripts/makebias.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sys
import os
import time
import tempfile
import warnings
import numpy as np

Expand Down Expand Up @@ -159,7 +158,7 @@ def makebias(args=None):
"no",
str(twait),
str(tmax),
"none",
"none", "none", "none", "none",
"f32",
]
flist = hcam.scripts.grab(args)
Expand Down
2 changes: 1 addition & 1 deletion hipercam/scripts/makedark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sys
import os
import time
import tempfile
import warnings
import numpy as np

Expand Down Expand Up @@ -157,6 +156,7 @@ def makedark(args=None):
str(twait),
str(tmax),
bias,
"none", "none", "none",
"f32",
]
flist = hcam.scripts.grab(args)
Expand Down
50 changes: 10 additions & 40 deletions hipercam/scripts/makeflat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sys
import os
import tempfile
import getpass

import numpy as np

Expand Down Expand Up @@ -189,6 +188,9 @@ def makeflat(args=None):

if server_or_local:
resource = cl.get_value("run", "run name", "run005")
root = os.path.basename(resource)
cl.set_default('output', cline.Fname(root, hcam.HCAM))

first = cl.get_value("first", "first frame to average", 1, 1)
last = cl.get_value("last", "last frame to average (0 for all)", first, 0)
twait = cl.get_value(
Expand Down Expand Up @@ -289,8 +291,9 @@ def makeflat(args=None):
"no",
str(twait),
str(tmax),
"none",
"f32",
"none" if bias is None else bias,
"none" if dark is None else dark,
"none", "none", "f32",
]
resource = hcam.scripts.grab(args)

Expand All @@ -307,44 +310,14 @@ def makeflat(args=None):
means[cnam] = {}

# We might have a load of temporaries from grab, but we are about to
# make some more to save the bias-subtracted normalised versions.
tdir = os.path.join(
tempfile.gettempdir(), "hipercam-{:s}".format(getpass.getuser())
)
os.makedirs(tdir, exist_ok=True)
# make some more to save the normalised versions.
tdir = utils.temp_dir()

fnames = []
with spooler.HcamListSpool(resource) as spool:

for mccd in spool:

if bias is not None:

# bias subtraction
if bframe is None:
bframe = hcam.MCCD.read(bias)
bframe = bframe.crop(mccd)

mccd -= bframe
bexpose = bframe.head.get("EXPTIME", 0.0)

else:
bexpose = 0.0

if dark is not None:

# dark subtraction
if dframe is None:
dframe = hcam.MCCD.read(dark)
dframe = dframe.crop(mccd)

# Factor to scale the dark frame by before
# subtracting from flat. Assumes that all
# frames have same exposure time.
scale = (mccd.head["EXPTIME"] - bexpose) / dframe.head["EXPTIME"]

# make dark correction
mccd -= scale * dframe

# here we determine the mean levels, store them
# then normalise the CCDs by them and save the files
# to disk
Expand All @@ -367,10 +340,7 @@ def makeflat(args=None):
os.close(fd)

# a bit of progress info
if bias is not None:
print("Saved debiassed, normalised" " flat to {:s}".format(fname))
else:
print("Saved normalised flat to {:s}".format(fname))
print(f"Saved processed flat to {fname}")

# now we go through CCD by CCD, using the first as a template
# for the window names in which we will also store the results.
Expand Down
Loading

0 comments on commit b32013b

Please sign in to comment.