exp = butler.get("calexp", {"visit":903334, "detector":22, "instrument":"HSC"})
print(exp.getWcs())
wcs = butler.get("calexp.wcs", {"visit":903334, "detector":22, "instrument":"HSC"})
print(wcs)
vinfo = butler.get("calexp.visitInfo", {"visit":903334, "detector":22, "instrument":"HSC"})
print(vinfo)

"""
Explanation: The API is very similar to the Gen2.  We can butler.get with a dict of data IDs like before
End of explanation
"""


for ref in butler.registry.queryDatasets("calexp", collections=['shared/ci_hsc_output']):
    print(ref.dataId)

"""
Explanation: We can get all data IDs/Dimensions. 
Note that ref.dataId is no longer a simple dict; it's a ExpandedDataCoordinate
End of explanation
"""


for ref in butler.registry.queryDatasets("calexp.wcs", collections=['shared/ci_hsc_output']):
    wcs = butler.get(ref)
    uri = butler.datastore.getUri(ref)
    print("calexp has ", wcs, "\nand the file is at \n", uri)

"""
Explanation: In Gen3, we can also get the WCS and the file URI without dumping the images as Python objects, for example
End of explanation
"""


rows = butler.registry.queryDatasets("calexp", collections=['shared/ci_hsc_output'])
ref = list(rows)[0]  # Just to get the first DatasetRef
exp = butler.getDirect(ref)
exp.getWcs()

import lsst.geom as geom

for ref in butler.registry.queryDatasets("calexp", collections=['shared/ci_hsc_output']):#, where="detector = 22"):
    uri = butler.datastore.getUri(ref)
    print("==== For the file of ", ref.dataId, "at \n", uri)
    exp = butler.getDirect(ref)
    wcs = exp.getWcs()

    print("dimensions:", exp.getDimensions())
    print("pixel scale:", wcs.getPixelScale().asArcseconds())
    
    imageBox = geom.Box2D(exp.getBBox())
    corners = [wcs.pixelToSky(pix) for pix in imageBox.getCorners()]
    imageCenter = wcs.pixelToSky(imageBox.getCenter())
    
    print("ra and dec for the center:", imageCenter.getRa().asDegrees(), imageCenter.getDec().asDegrees())
    print("ra and dec for the corners:")
    [print(corner) for corner in corners]

"""
Explanation: With the DatasetRef, we may also use butler.getDirect
End of explanation
"""


