def init_summary(night, outfilename, clobber=False, verbose=False): ''' Create an empty fits table will contain summary information for each night. Parameters ---------- night : int night in form 20210320 for 20 March 2021 clobber : bool overwrite existing file verbose : bool provide verbose output Returns ------- none ''' outdir = utils.get_outdir() outputfile = os.path.join(outdir, outfilename) if os.path.isfile(outputfile) and not clobber: print("init_summary(): {} already exists and clobber=False".format(outputfile)) return t = QTable(calc_row(night), names=('NIGHT', 'TWIBEG', 'TWIEND', 'DOMEOPEN', 'DOMECLOSE', 'DOMEFRAC', 'DOMEHRS', 'SCSTART', 'SCSTOP', 'SCTWTOT', 'SCTOT', 'SCTWFRAC'), meta={'Name': 'Nightly Efficiency Summary Table'}) t.write(outputfile, overwrite=clobber) if verbose: print("Initialized summary table {}".format(outputfile))
def update_table(night, outfilename, clobber=False, verbose=False): ''' Update the summary table with data for 'night' Parameters ---------- night : int night in form 20210320 for 20 March 2021 clobber : bool overwrite existing data verbose : bool provide verbose output Returns ------- none ''' outdir = utils.get_outdir() outputfile = os.path.join(outdir, outfilename) t = QTable.read(outputfile) if int(night) in t['NIGHT']: print("Found {} in table".format(night)) if not clobber: print("Data for {} already in table and clobber=False".format(night)) return else: print("clobber=True") indx = np.where(t['NIGHT'] == night)[0][0] t.remove_row(indx) newrow = calc_row(night) t.add_row(newrow) t.write(outputfile, overwrite=True) if verbose: print("Added new row: ", newrow)
def calc_guidelist(night, clobber=False, verbose=False): ''' Create json files with start times, exptimes, and other information about guiding for a single night. Outputs have the form guidedata20201224.json. Parameters ---------- night : int night in form 20210320 for 20 March 2021 clobber : bool overwrite existing file verbose : bool provide verbose output Returns ------- none ''' try: outdir = utils.get_outdir() except RuntimeError: exit(1) try: datadir = utils.get_rawdatadir() except RuntimeError: exit(1) nightdir = os.path.join(datadir, str(night)) twibeg_mjd, twiend_mjd = utils.get_twilights(int(night)) # Names for output json file: filename = "guidedata" + str(night) + ".json" guidedatafile = os.path.join(outdir, 'NightlyData', filename) # See if json file for this night already exists: if not os.path.isfile(guidedatafile) or clobber: # List of all directories and expids: expdirs = glob(nightdir + "/*") expids = expdirs.copy() for i in range(len(expdirs)): expids[i] = expdirs[i][expdirs[i].find('000')::] # Print warning if there are no data if len(expids) == 0: print("Warning: No observations found") guidedata = {} for expid in expids: # tmpdir = os.path.join(nightdir, expid, "guide-" + expid + ".fits.fz") # guidefiles = (glob(tmpdir + "/guide-" + expid + ".fits.fz")) # guidefile = tmpdir + "/guide-" + expid + ".fits.fz" guidefile = os.path.join(nightdir, expid, "guide-" + expid + ".fits.fz") #if len(guidefiles) > 0: if os.path.isfile(guidefile): try: hhh = fits.open(guidefile) except OSError: if verbose: print("OSError with {}".format(guidefile)) continue guidedata[expid] = {} t1 = Time( utils.find_dateobs(hhh) ).mjd try: t2 = Time(hhh['GUIDE0T'].data[-1]['DATE-OBS']).mjd except KeyError: t2 = t1 # Need to fix this guidedata[expid]['GUIDE-START'] = t1 guidedata[expid]['GUIDE-STOP'] = t2 try: guidedata[expid]['DOMSHUTU'] = hhh[0].header['DOMSHUTU'] except KeyError: guidedata[expid]['DOMSHUTU'] = 'None' try: guidedata[expid]['PMCOVER'] = hhh[0].header['PMCOVER'] except KeyError: guidedata[expid]['PMCOVER'] = 'UNKNOWN' try: guidedata[expid]['OBSTYPE'] = hhh[0].header['OBSTYPE'] except KeyError: guidedata[expid]['OBSTYPE'] = 'None' try: guidedata[expid]['FLAVOR'] = hhh[0].header['FLAVOR'] except KeyError: guidedata[expid]['FLAVOR'] = 'None' try: guidedata[expid]['EXPTIME'] = hhh[0].header['EXPTIME'] except KeyError: guidedata[expid]['EXPTIME'] = 0. with open(guidedatafile, 'w') as fp: json.dump(guidedata, fp) if verbose: print("Wrote", guidedatafile)
def calc_interexp(night, minexptime=300., clobber=False, verbose=False): ''' Calculate the interexposure times Parameters ---------- night : int night in form 20210320 for 20 March 2021 minexptime : float minimum science exposure time to consider in calculation verbose : bool print verbose output? clobber : bool overwrite existing file Returns ------- interexp : 2-d float array columns are DATE-OBS in MJD, DATE-OBS in UT hrs, EXPTIME in s, EXPID, interexposure time in s ''' # Read in the data try: outdir = utils.get_outdir() except RuntimeError: exit(1) specfilename = "specdata" + str(night) + ".json" specdatafile = os.path.join(outdir, 'NightlyData', specfilename) if os.path.isfile(specdatafile) and not clobber: specdata = utils.read_json(specdatafile) else: print("Note: {} not found ... creating it".format(specdatafile)) calc_sciencelist(night) specdata = utils.read_json(specdatafile) # Take about the dict dateobs_mjd = [] exptime = [] expid = [] for i, key in enumerate(specdata.keys()): if specdata[key]['EXPTIME'] > minexptime: dateobs_mjd.append(specdata[key]['DATE-OBS']) exptime.append(specdata[key]['EXPTIME']) expid.append(key) twibeg_mjd, twiend_mjd = utils.get_twilights(int(night)) startdate = int(twibeg_mjd) scidata = np.zeros([len(dateobs_mjd), 5]) for i in range(len(dateobs_mjd)): dateobs_hrs = (dateobs_mjd[i] - startdate)*24. scidata[i] = np.asarray([dateobs_mjd[i], dateobs_hrs, exptime[i], expid[i], 0.]) # Sort by expid: columnIndex = 3 sortdata = scidata[scidata[:,columnIndex].argsort()] # Calculate interexposure time between each successive pair secperday = 24.*3600. interexp = [] for i in range(len(sortdata) - 1): sortdata[i][4] = secperday*(sortdata[i+1][0] - sortdata[i][0] - sortdata[i][1]/secperday) # interexp.append(secperday*(sortdata[i+1][0] - sortdata[i][0] - sortdata[i][1]/secperday )) return sortdata[:-1]
def calc_obstimes(night, verbose=False, clobber=False): ''' Calculate start and lengths of science, dither, and guide exposures Parameters ---------- night : int night in form 20210320 for 20 March 2021 verbose : bool print verbose output? clobber : bool overwrite existing file Returns ------- science_start : float start times of science exposures in UT hours science_width : float length of science exposures in UT hours dither_start : float start times of dither exposures in UT hours dither_width : float length of dither exposures in UT hours guide_start : float start times of guide exposures in UT hours guide_width : float length of guide exposures in UT hours ''' try: outdir = utils.get_outdir() except RuntimeError: exit(1) # Read in the data specfilename = "specdata" + str(night) + ".json" specdatafile = os.path.join(outdir, 'NightlyData', specfilename) if os.path.isfile(specdatafile) and not clobber: specdata = utils.read_json(specdatafile) else: print("Note: {} not found ... creating it".format(specdatafile)) calc_sciencelist(night) specdata = utils.read_json(specdatafile) guidefilename = "guidedata" + str(night) + ".json" guidedatafile = os.path.join(outdir, 'NightlyData', guidefilename) if os.path.isfile(guidedatafile) and not clobber: guidedata = utils.read_json(guidedatafile) else: print("Note: {} not found ... creating it".format(guidedatafile)) calc_guidelist(night) guidedata = utils.read_json(guidedatafile) twibeg_mjd, twiend_mjd = utils.get_twilights(int(night)) startdate = int(twibeg_mjd) # Calculate the start and duration for the science observations: science_start = [] science_width = [] for item in specdata: if specdata[item]['OBSTYPE'] == 'SCIENCE' and specdata[item]['FLAVOR'] == 'science' and 'Dither' not in specdata[item]['PROGRAM'] and specdata[item]['DOMSHUTU'] == 'open' and specdata[item]['PMCOVER'] == 'open': science_start.append( (specdata[item]['DATE-OBS'] - startdate)*24. ) science_width.append( specdata[item]['EXPTIME']/3600. ) # Separately account for time spent on dither tests dither_start = [] dither_width = [] for item in specdata: if specdata[item]['OBSTYPE'] == 'SCIENCE' and specdata[item]['FLAVOR'] == 'science' and 'Dither' in specdata[item]['PROGRAM']: dither_start.append( (specdata[item]['DATE-OBS'] - startdate)*24. ) dither_width.append( specdata[item]['EXPTIME']/3600. ) # Times for guiding: guide_start = [] guide_width = [] for item in guidedata: # if guidedata[item]['OBSTYPE'] == 'SCIENCE' and guidedata[item]['FLAVOR'] == 'science' and guidedata[item]['PMCOVER'] == 'open' and guidedata[item]['DOMSHUTU'] == 'open': if guidedata[item]['OBSTYPE'] == 'SCIENCE' and guidedata[item]['FLAVOR'] == 'science' and guidedata[item]['DOMSHUTU'] == 'open': guide_start.append( (guidedata[item]['GUIDE-START'] - startdate)*24. ) guide_width.append( (guidedata[item]['GUIDE-STOP'] - guidedata[item]['GUIDE-START'])*24. ) return science_start, science_width, dither_start, dither_width, guide_start, guide_width
def calc_sciencelist(night, clobber=False, verbose=False): ''' Create json files with start times, exptimes, and other information for spectra for a single night. Outputs have the form specdata20201224.json. Parameters ---------- night : int night in form 20210320 for 20 March 2021 clobber : bool overwrite existing file verbose : bool provide verbose output Returns ------- none ''' # Output directory try: outdir = utils.get_outdir() except RuntimeError: exit(1) # Directory with raw data try: datadir = utils.get_rawdatadir() except RuntimeError: exit(1) nightdir = os.path.join(datadir, str(night)) # Names for output json file: filename = "specdata" + str(night) + ".json" specdatafile = os.path.join(outdir, 'NightlyData', filename) # See if json file for this night already exists: if not os.path.isfile(specdatafile) or clobber: # List of all directories and expids: expdirs = glob(nightdir + "/*") expids = expdirs.copy() for i in range(len(expdirs)): expids[i] = expdirs[i][expdirs[i].find('000')::] # Print warning if there are no data if len(expids) == 0: print("Warning: No observations found") # Get all spec observations: specdata = {} for expid in expids: tmpdir = os.path.join(nightdir, expid) scifiles = (glob(tmpdir + "/desi*")) if len(scifiles) > 0: hhh = fits.open(scifiles[0]) if hhh[1].header['DATE-OBS'] == None: continue specdata[expid] = {} specdata[expid]['DATE-OBS'] = Time(hhh[1].header['DATE-OBS']).mjd specdata[expid]['OBSTYPE'] = hhh[1].header['OBSTYPE'] specdata[expid]['FLAVOR'] = hhh[1].header['FLAVOR'] specdata[expid]['PROGRAM'] = hhh[1].header['PROGRAM'] specdata[expid]['EXPTIME'] = hhh[1].header['EXPTIME'] try: specdata[expid]['DOMSHUTU'] = hhh[1].header['DOMSHUTU'] except KeyError: specdata[expid]['DOMSHUTU'] = 'None' try: specdata[expid]['PMCOVER'] = hhh[1].header['PMCOVER'] except KeyError: specdata[expid]['PMCOVER'] = 'None' with open(specdatafile, 'w') as fp: json.dump(specdata, fp) if verbose: print("Wrote", specdatafile)