def test_fsf_model(tmpdir): cubename = get_data_file('sdetect', 'subcub_mosaic.fits') cube = Cube(cubename) # Read FSF model with the old method with pytest.warns(MpdafWarning): PSF, fwhm_pix, fwhm_arcsec = get_FSF_from_cube_keywords(cube, 13) # Read FSF model from file fsf = FSFModel.read(cubename) assert len(fsf) == 9 assert fsf[0].model == 2 assert_allclose(fsf[0].get_fwhm(cube.wave.coord()), fwhm_arcsec[0]) # Read FSF model from cube fsf = FSFModel.read(cube) assert len(fsf) == 9 assert fsf[0].model == 2 assert_allclose(fsf[0].get_fwhm(cube.wave.coord()), fwhm_arcsec[0]) # Read FSF model from header and for a specific field hdr = cube.primary_header.copy() hdr.update(cube.data_header) fsf = FSFModel.read(hdr, field=2) assert fsf.model == 2 assert_allclose(fsf.get_fwhm(cube.wave.coord()), fwhm_arcsec[1]) # test to_header assert [str(x).strip() for x in fsf.to_header().cards] == [ 'FSFMODE = 2 / Circular MOFFAT beta=poly(lbda) fwhm=poly(lbda)', 'FSFLB1 = 5000 / FSF Blue Ref Wave (A)', 'FSFLB2 = 9000 / FSF Red Ref Wave (A)', 'FSF00FNC= 2 / FSF00 FWHM Poly Ncoef', 'FSF00F00= -0.1204 / FSF00 FWHM Poly C00', 'FSF00F01= 0.6143 / FSF00 FWHM Poly C01', 'FSF00BNC= 1 / FSF00 BETA Poly Ncoef', 'FSF00B00= 2.8 / FSF00 BETA Poly C00' ] testfile = str(tmpdir.join('test.fits')) outcube = cube.copy() fsf.to_header(hdr=outcube.primary_header) outcube.write(testfile) fsf3 = FSFModel.read(testfile, field=0) assert fsf3.model == 2 assert fsf3.get_beta(7000) == fsf.get_beta(7000) assert fsf3.get_fwhm(7000) == fsf.get_fwhm(7000) assert fsf3.get_fwhm(7000, unit='pix') == fsf.get_fwhm(7000, unit='pix')
def remove_bg(cube_in,mask,cube_out,invert=False): """ Corrects background of a cube to zero (per wavelengh plane) Strong emission (e.g. cluster members) should be masked. Parameters: ---------- cube_in: string path to input cube to be corrected mask: string path to mask to be used. Image with 1 for masked regions (objects) and 0 for regions to be used to calculate the meadian background (sky)' cube_out: string path to the output (corrected) cube invert: boolean if True, 0 for masked regions (objects) and 1 for sky (old ZAP version) Returns: ---------- mpdaf.obj.Cube corrected cube """ c=Cube(cube_in) immask = Image(mask) c2=c.copy() mask = immask.data.astype(bool) if invert: mask = np.invert(mask) c.data.mask[:, mask] = True tstart = time.time() for k in range(c.shape[0]): posmin=max(k-25,0) posmax=min(c.shape[0],k+25) for p in range(c.shape[1]): med=np.ma.median(c.data[posmin:posmax,p,:]) c2.data.data[k,p,:]-=med for q in range(c.shape[2]): med=np.ma.median(c.data[posmin:posmax,:,q]) c2.data.data[k,:,q]-=med tend = time.time() print('ellapsed time %s'%(tend-tstart)) ## Update Header c2.primary_header.add_comment('This cube has been median subtracted') c2.write(cube_out) return c2
def masked_cube(inp, mask, value=0., method='value'): """Returns a masked version of the input cube inp : str or Cube Input filename or Cube object mask : boolean np.array 2D of same shape (ny, nx) than cube spatial Pixels to mask value : float Values to replaces masked spaxels if method = 'value' method : str Method to perform the masking value : replaces by the given value madian : replaces by the median of all contiguous pixels including the value of the pixel itself """ if not isinstance(inp, Cube): # assume str cube = Cube(inp) else: cube = inp newcube = cube.copy() nw, ny, nx = cube.shape for ii in range(nw): image = cube.data[ii, :, :] if method == 'value': aux = np.where(mask, value, image) elif method == 'median': inds_y, inds_x = np.where(mask) aux = image.data for yx in zip(inds_y, inds_x): # import pdb;pdb.set_trace() median = np.median(image[yx[0] - 1:yx[0] + 1, yx[1] - 1:yx[1] + 1]) aux[yx[0], yx[1]] = median newcube[ii, :, :] = aux # import pdb;pdb.set_trace() return newcube
def masked_cube(inp, mask, value=0., method='value'): """Returns a masked version of the input cube inp : str or Cube Input filename or Cube object mask : boolean np.array 2D of same shape (ny, nx) than cube spatial Pixels to mask value : float Values to replaces masked spaxels if method = 'value' method : str Method to perform the masking value : replaces by the given value madian : replaces by the median of all contiguous pixels including the value of the pixel itself """ if not isinstance(inp, Cube): # assume str cube = Cube(inp) else: cube = inp newcube = cube.copy() nw, ny, nx = cube.shape for ii in range(nw): image = cube.data[ii,:,:] if method == 'value': aux = np.where(mask, value, image) elif method == 'median': inds_y, inds_x = np.where(mask) aux = image.data for yx in zip(inds_y,inds_x): # import pdb;pdb.set_trace() median = np.median(image[yx[0]-1:yx[0]+1, yx[1]-1:yx[1]+1]) aux[yx[0],yx[1]] = median newcube[ii,:,:] = aux # import pdb;pdb.set_trace() return newcube