def remeasure(mpc_in, recentroided=False): """ re-measure the astrometry and photometry of the given mpc line """ if mpc_in.null_observation: return mpc_in mpc_obs = deepcopy(mpc_in) logging.debug("rm start: {}".format(mpc_obs.to_string())) if not isinstance(mpc_obs.comment, mpc.OSSOSComment): logging.error( "Failed to convert comment line") return mpc_in parts = re.search('(?P<expnum>\d{7})(?P<type>\S)(?P<ccd>\d\d)', mpc_obs.comment.frame) if not parts: logging.error( "Failed to parse expnum from frame info in comment line") return mpc_in start_coordinate = mpc_in.coordinate #assert isinstance(start_coordinate, ICRSCoordinates) try: header = storage._get_sghead(parts.group('expnum'))[int(parts.group('ccd')) + 1] except IOError as ioerr: logging.error(str(ioerr)) logging.error("Failed to get astrometric header for: {}".format(mpc_obs)) return mpc_in this_wcs = wcs.WCS(header) try: #(x , y ) = this_wcs.sky2xy(mpc_obs.coordinate.ra.degree, mpc_obs.coordinate.dec.degree, usepv=True) x = mpc_obs.comment.x y = mpc_obs.comment.y (ra, dec) = this_wcs.xy2sky(x, y, usepv=True) mpc_obs.coordinate = (ra, dec) sep = mpc_in.coordinate.separation(mpc_obs.coordinate) if sep > TOLERANCE*20 and mpc_in.discovery and (int(parts.group('ccd')) < 18 or int(parts.group('ccd')) in [36, 37]): logging.warn("Large ({}) offset using X/Y in comment line to compute RA/DEC".format(sep)) logging.warn("This is a discovery line so flipping/flopping the x/y position recorded in comment as that " "may be taken from a flip/flopped image.") x = header['NAXIS1'] - x + 1 y = header['NAXIS2'] - y + 1 sep = mpc_in.coordinate.separation(mpc_obs.coordinate) (ra, dec) = this_wcs.xy2sky(x, y, usepv=True) # print "--> x/y coordinates ({},{}) and recomputing ra/dec ({},{})".format(x, y, ra, dec) mpc_obs.coordinate = (ra, dec) if sep > TOLERANCE*20 and mpc_in.discovery: logging.warn( "Large ({}) offset after flipping, so reverting.".format(sep)) # Try un-flipping. x = float(mpc_in.comment.x) y = float(mpc_in.comment.y) (ra, dec) = this_wcs.xy2sky(x, y, usepv=True) # print "--> x/y coordinates ({},{}) and recomputing ra/dec ({},{})".format(x, y, ra, dec) mpc_obs.coordinate = (ra, dec) except: logging.warn("Failed to get the X Y coordinate from comment line.") return mpc_in # (ra, dec) = this_wcs.xy2sky(x, y, usepv=True) # mpc_obs.coordinate = (ra, dec) # sep = mpc_in.coordinate.separation(mpc_obs.coordinate) if sep > TOLERANCE and not recentroided: # use the old header RA/DEC to predict the X/Y and then use that X/Y to get new RA/DEC logging.warn("sep: {} --> large offset when using comment line X/Y to compute RA/DEC") logging.warn("Using RA/DEC and original WCS to compute X/Y and replacing X/Y in comment.".format(sep)) header2 = storage.get_astheader(parts.group('expnum'), int(parts.group('ccd'))) image_wcs = wcs.WCS(header2) (x, y) = image_wcs.sky2xy(mpc_in.coordinate.ra.degree, mpc_in.coordinate.dec.degree, usepv=False) (ra, dec) = this_wcs.xy2sky(x, y, usepv=True) logging.warn("Coordinate changed: ({:5.2f},{:5.2f}) --> ({:5.2f},{:5.2f})".format(mpc_obs.comment.x, mpc_obs.comment.y, x, y)) mpc_obs.coordinate = (ra, dec) mpc_obs.comment.x = x mpc_obs.comment.y = y try: merr = float(mpc_obs.comment.mag_uncertainty) fwhm = float(storage.get_fwhm(parts.group('expnum'), int(parts.group('ccd')))) * header['PIXSCAL1'] centroid_err = merr * fwhm logging.debug("Centroid uncertainty: {} {} => {}".format(merr, fwhm, centroid_err)) except Exception as err: logging.error(str(err)) logging.error("Failed to compute centroid_err for observation:\n" "{}\nUsing default of 0.2".format(mpc_obs.to_string())) centroid_err = 0.2 mpc_obs.comment.astrometric_level = header.get('ASTLEVEL', "0") try: asterr = float(header['ASTERR']) residuals = (asterr ** 2 + centroid_err ** 2) ** 0.5 logging.debug("Residuals: {} {} => {}".format(asterr, centroid_err, residuals)) except Exception as err: logging.error(str(err)) logging.error("Failed while trying to compute plate uncertainty for\n{}".format(mpc_obs.to_stirng())) logging.error('Using default of 0.25') residuals = 0.25 mpc_obs.comment.plate_uncertainty = residuals logging.debug("sending back: {}".format(mpc_obs.to_string())) return mpc_obs
def recompute_mag(mpc_in): """ Get the mag of the object given the mpc.Observation """ # TODO this really shouldn't need to build a 'reading' to get the cutout... from ossos.downloads.cutouts import downloader dlm = downloader.ImageCutoutDownloader() mpc_obs = deepcopy(mpc_in) assert isinstance(mpc_obs, mpc.Observation) if mpc_obs.null_observation: return mpc_obs parts = re.search('(?P<expnum>\d{7})(?P<type>\S)(?P<ccd>\d\d)', mpc_obs.comment.frame) if parts is None: return mpc_obs expnum = parts.group('expnum') ccd = parts.group('ccd') file_type = parts.group('type') observation = astrom.Observation(expnum, file_type, ccd) assert isinstance(observation, astrom.Observation) ast_header = storage._get_sghead(int(expnum))[int(ccd)+1] filter_value = None for keyword in ['FILTER', 'FILT1 NAME']: filter_value = ast_header.get(keyword, None) if filter_value is not None: if filter_value.startswith('gri'): filter_value = 'w' else: filter_value = filter_value[0] break # The ZP for the current astrometric lines is the pipeline one. The new ZP is in the astheader file. new_zp = ast_header.get('PHOTZP') # The .zeropoint.used value is likely the one used for the original photometry. old_zp = storage.get_zeropoint(int(expnum), int(ccd)) reading = astrom.SourceReading(float(mpc_obs.comment.x), float(mpc_obs.comment.y), float(mpc_obs.comment.x), float(mpc_obs.comment.y), mpc_obs.coordinate.ra.degree, mpc_obs.coordinate.dec.degree, float(mpc_obs.comment.x), float(mpc_obs.comment.y), observation, ssos=True, from_input_file=True, null_observation=False, discovery=mpc_obs.discovery) cutout = dlm.download_cutout(reading, needs_apcor=True) cutout._zmag = new_zp # try: # if mpc_in.discovery and ccdnum < 18 or ccdnum in [36, 37]: # logging.warn("This is a discovery line so flipping/flopping the x/y position " # "recorded in comment as that is likely taken from a flip/flopped image, and we now work" # "in unflip/flop frame" ) # # Get the full header of the image so we can flip/flop the coordinates # header = storage._get_sghead(parts.group('expnum'))[ccdnum + 1] # # Get the X/Y position of the source from the MPC line comment. # x = mpc_obs.comment.x # y = mpc_obs.comment.y # # Do the coordinate flip/flop # x = header['NAXIS1'] - x + 1 # y = header['NAXIS2'] - y + 1 # # Now x/y are in the full frame of the MEF image which is stored in cutout. # # so we transform to the pixel coordinates of the cutout. # (x_p, y_p, hdu_list_idx) = cutout.get_pixel_coordinates((x, y), ccdnum) # # Now turn those in RA/DEC coordinates. # mpc_obs.coordinate = cutout.pix2world(x_p, y_p, hdu_list_idx) # if mpc_in.coordinate.separation(mpc_obs.coordinate) > TOLERANCE: # logger.warning("Flipping coordinates don't match RA/DEC. reverting to oringal values.") # # Try reverting back to the orignal X/Y pixel locations from the input astrometry line. # (x_p, y_p, hdu_list_idx) = cutout.get_pixel_coordinates((mpc_obs.comment.x, mpc_in.comment.y), ccdnum) # mpc_obs.coordinate = cutout.pix2world(x_p, y_p, hdu_list_idx) # if mpc_in.coordinate.separation(mpc_obs.coordinate) > TOLERANCE: # # Something is not good. # raise ValueError("Can't determine x/y to RA/DEC mapping on discovery images.") # else: # logger.warn("Updating X/Y to use the not flipped coordinates frame.") # # OK, we have the correct pixel coordinates for the cutout we are looking, store the orignal observerd coord # cutout.update_pixel_location((x_p, y_p), hdu_list_idx) # except Exception as ex: # logging.warn(ex) # logging.warn("Failed to get the X Y coordinate from comment line.") # return mpc_in # if math.fabs(cutout.zmag - old_zp) > 0.3: logging.warn("Large change in zeropoint detected: {} -> {}".format(old_zp, cutout.zmag)) try: PHOT = cutout.get_observed_magnitude() x = PHOT['XCENTER'] y = PHOT['YCENTER'] mag = PHOT['MAG'] merr = PHOT['MERR'] cutout.update_pixel_location((x, y), hdu_index=cutout.extno) x, y = cutout.observed_source_point except Exception as ex: logging.error("ERROR: {}".format(str(ex))) logging.warn("Failed to do photometry.") return mpc_obs if mpc_obs.comment.mag is not None and math.fabs(mpc_obs.comment.mag - mag) > 3.5 * mpc_obs.comment.mag_uncertainty: logging.warn("recomputed magnitude shift large: {} --> {}".format(mpc_obs.mag, mag[0])) if math.sqrt((x.value - mpc_obs.comment.x) ** 2 + (y.value - mpc_obs.comment.y) ** 2) > 1.9: logging.warn("Centroid shifted ({},{}) -> ({},{})".format(mpc_obs.comment.x, mpc_obs.comment.y, x.value, y.value)) # Don't use the new X/Y for Hand measured entries. if str(mpc_obs.note1) != "H": mpc_obs.comment.x = x.value mpc_obs.comment.y = y.value mpc_obs._band = filter_value mpc_obs.comment.mag = mag mpc_obs.comment.mag_uncertainty = merr logging.warn("Previous mag: {} {}".format( mpc_in.mag,mpc_in.comment.photometry_note)) if mpc_obs.mag is not None or mpc_in.comment.photometry_note[0] == "Z": mpc_obs.mag = mag logging.warn("Resulting mag: {} {}".format( mpc_obs.mag,mpc_obs.comment.photometry_note)) return mpc_obs