Last active
August 29, 2015 14:03
-
-
Save simonrw/174c7908b5115afb1e67 to your computer and use it in GitHub Desktop.
Code examples for the NGTS wiki page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Read in the catalogue RA and DEC positions and plot as a scatter plot | |
| ''' | |
| from astropy.io import fits | |
| import matplotlib.pyplot as plt | |
| with fits.open("example.fits") as hdulist: | |
| catalogue = hdulist['catalogue'].data | |
| ra = catalogue['ra'] | |
| dec = catalogue['dec'] | |
| plt.plot(ra, dec, 'k.') | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Read in the FWHM as a function of time from the imagelist hdu, and plot the time series | |
| ''' | |
| from astropy.io import fits | |
| import matplotlib.pyplot as plt | |
| with fits.open("example.fits") as hdulist: | |
| imagelist = hdulist['imagelist'].data | |
| mjd = imagelist['tmid'] | |
| fwhm = imagelist_hdu['fwhm'] | |
| mjd0 = int(mjd.min()) | |
| mjd -= mjd0 | |
| plt.plot(mjd, fwhm, 'k.') | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <fitsio.h> | |
| void check_fits_error(int status); | |
| int main() { | |
| fitsfile *fptr; | |
| int status = 0, i = 0; | |
| long naxes[] = {0, 0}; | |
| long napertures = 0, nimages = 0; | |
| float *image_data = NULL; | |
| long chosen_aperture = 100; // Which aperture to extract | |
| long fpixel[] = {chosen_aperture + 1, 1}; | |
| fits_open_file(&fptr, "example.fits", READONLY, &status); | |
| check_fits_error(status); | |
| // Move to the flux hdu | |
| fits_movnam_hdu(fptr, IMAGE_HDU, "flux", 0, &status); | |
| check_fits_error(status); | |
| // Get the image dimensions | |
| fits_get_img_size(fptr, 2, naxes, &status); | |
| check_fits_error(status); | |
| nimages = naxes[0]; | |
| napertures = naxes[1]; | |
| printf("Found %ld images and %ld apertures\n", nimages, napertures); | |
| // Read the flux lightcurve | |
| image_data = malloc(nimages * sizeof(float)); | |
| fits_read_pix(fptr, TFLOAT, fpixel, nimages, NULL, image_data, NULL, &status); | |
| check_fits_error(status); | |
| for (i=0; i<nimages; i++) { | |
| printf("%f\n", image_data[i]); | |
| } | |
| /* And so on reading the other image hdus | |
| * ... | |
| */ | |
| // Cleanup | |
| free(image_data); | |
| fits_close_file(fptr, &status); | |
| check_fits_error(status); | |
| return 0; | |
| } | |
| void check_fits_error(int status) { | |
| if (status) { | |
| fits_report_error(stderr, status); | |
| exit(status); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| This code reads in the example fits file, extracts the chosen aperture index from | |
| the file and plots the lightcurve with errors. | |
| The method below avoids reading in the full (potentially large) images into memory. | |
| ''' | |
| from astropy.io import fits | |
| import matplotlib.pyplot as plt | |
| chosen_aperture_index = 100 | |
| s = slice(chosen_aperture_index, chosen_aperture_index + 1) | |
| with fits.open("example.fits") as hdulist: | |
| hjd = hdulist['hjd'].data[s].flatten() | |
| flux = hdulist['flux'].data[s].flatten() | |
| fluxerr = hdulist['fluxerr'].data[s].flatten() | |
| plt.errorbar(hjd, flux, fluxerr, ls='None', marker='.') | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment