astroquery icon indicating copy to clipboard operation
astroquery copied to clipboard

ESASky get_images Leaves Chandra Images open?

Open sengle01 opened this issue 1 year ago • 5 comments

Hello,

I am using a Jupyter notebook in Windows 10 to query several coordinate pairs. If there are two coordinates in the same field, say:

ra dec 19.67 -0.868 19.66 -0.874

it appears that the Chandra image downloaded for the first coordinate pair isn't closed properly, so an error is returned when an attempt is made to download the same image for the 2nd coordinate pair.

Here is my code (meant to run on a longer list of coordinates, not simply the two I gave as an example):

for i in range(len(ra)):
    coord_obj = coord.SkyCoord(ra=ra[i], dec=dec[i], unit=(u.deg, u.deg), frame='icrs')
    images = ESASky.get_images(position=coord_obj, missions='Chandra')
    if bool(images) is False:
        print(f"Object {i} not found in database.")
    else:
        print(r"entry number {}".format(i))
        wcs = WCS(images["CHANDRA"][0][0].header)
        fig, ax = plt.subplots(1,1, figsize=(12,12), subplot_kw={'projection': wcs})
        im, norm = imshow_norm(images["CHANDRA"][0][0].data, ax, origin='lower',interval=PercentileInterval(99.7), stretch=LinearStretch())
        ax.scatter(ra[i], dec[i], s=200, edgecolor='red', facecolor='none', transform=ax.get_transform('icrs'),label=str(starname[i]))
        ax.legend()
        fig.colorbar(im)
        plt.savefig(f"ADAPimages/{starname[i]}_CHANDRA.png", dpi=300, bbox_inches='tight', pad_inches=0.25)

The code worked fine with XMM images (and the full coordinate list), but now I'm getting the following error when I try it with Chandra images:

INFO: Starting download of CHANDRA data. (1 files) [astroquery.esasky.core]
INFO: Downloading Observation ID: 4963 from https://cdaftp.cfa.harvard.edu/cgi-bin/chaser_ftp_retrieve_file.cgi?filename=science/ao05/cat8/4963/primary/acisf04963N004_full_img2.fits [astroquery.esasky.core]
INFO: [Done] [astroquery.esasky.core]
INFO: Downloading of CHANDRA data complete. [astroquery.esasky.core]
INFO: Maps available at C:\Users\scott\Maps [astroquery.esasky.core]
entry number 15
WARNING: FITSFixedWarning: 'datfix' made the change 'Set DATEREF to '1998-01-01' from MJDREF.
Set MJD-END to 53415.180162 from DATE-END'. [astropy.wcs.wcs]
INFO: Starting download of CHANDRA data. (1 files) [astroquery.esasky.core]
INFO: Downloading Observation ID: 4963 from https://cdaftp.cfa.harvard.edu/cgi-bin/chaser_ftp_retrieve_file.cgi?filename=science/ao05/cat8/4963/primary/acisf04963N004_full_img2.fits [astroquery.esasky.core]
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[3], line 4
      2 coord_obj = coord.SkyCoord(ra=ra[i], dec=dec[i], unit=(u.deg, u.deg), frame='icrs')
      3 #images = ESASky.get_images(position=coord_obj, missions=['XMM', 'Chandra', 'eROSITA']) #trying default radius=0
----> 4 images = ESASky.get_images(position=coord_obj, missions='Chandra', cache=False)
      5 if bool(images) is False:
      6     print(f"Object {i} not found in database.")

File ~\anaconda3\envs\basenew\Lib\site-packages\astroquery\esasky\core.py:1140, in ESASkyClass.get_images(self, position, observation_ids, radius, missions, download_dir, cache)
   1137 json = self._get_observation_json()
   1138 for query_mission in map_query_result.keys():
   1139     maps[query_mission] = (
-> 1140         self._get_maps_for_mission(
   1141             map_query_result[query_mission],
   1142             query_mission,
   1143             download_dir,
   1144             cache, json))
   1146 if all([maps[mission].count(None) == len(maps[mission])
   1147         for mission in maps]):
   1148     log.info("No maps got downloaded, check errors above.")

File ~\anaconda3\envs\basenew\Lib\site-packages\astroquery\esasky\core.py:1509, in ESASkyClass._get_maps_for_mission(self, maps_table, mission, download_dir, cache, json, is_spectra)
   1506 fits_data = response.content
-> 1507 with open(directory_path + file_name, 'wb') as fits_file:
   1508     fits_file.write(fits_data)
   1509     fits_file.flush()

OSError: [Errno 22] Invalid argument: 'Maps/CHANDRA/acisf04963N004_full_img2.fits'

If anyone knows what I am doing wrong (or if this is simply a known bug when using Jupyter in Windows), I'd would appreciate any help you could offer.

Thank you.

sengle01 avatar May 06 '24 17:05 sengle01

I'm not sure, but this could be a Windows issue? This path: 'Maps/CHANDRA/acisf04963N004_full_img2.fits' uses /'s, while windows uses \'s for its path specification. This might be a bug in how directory_path is generated.

keflavich avatar May 06 '24 17:05 keflavich

Thank you for the quick reply @keflavich

I should also add that, after the code runs and I receive the error, I can't go to the folder and delete the Chandra image file. It gives me an error saying the file is in use by another program. It's only after I either restart the notebook's kernel, or shut the notebook down, that I can delete the image file. (Sorry I didn't include that before)

sengle01 avatar May 06 '24 18:05 sengle01

Update:

I can confirm that it is 'a windows thing' but I don't believe it has to do with the path.

To check and see if the path's slashes were causing the problem, I replaced them with double backslash, and received the same error.

"OSError: [Errno 22] Invalid argument: 'Maps\CHANDRA\acisf04963N004_full_img2.fits'"

With the error remaining, and since everything worked fine with XMM images, it seems like the path is working fine.

Then I went into an Ubuntu VM, and everything worked just fine. I don't know if there could be something specific about the Chandra image structure, or the method used to obtain/store them?

sengle01 avatar May 06 '24 18:05 sengle01

Hi @sengle01,

I am not able to reproduce the error. Could you try again with the latest version of astroquery? The issue may have been solved by this commit: a8bca46de8419e7cf87e120051e2708b459111ef

If that does not work, could you try to set the download_dir manually? Something like this ESASky.get_images(position=coord_obj, missions='Chandra', download_dir='C:\\Users\\Henrik')

imbasimba avatar Aug 09 '24 10:08 imbasimba

@sengle01 could you confirm whether this is still a problem? It seems that it should have been resolved in a more recent version of astroquery

keflavich avatar Sep 07 '24 18:09 keflavich