# [AstroPy] Plotting .fits data using filters

E. Madison Bray erik.m.bray at gmail.com
Thu Apr 15 02:42:04 EDT 2021

```Hi Didrik,

Perhaps before doing that you could just explain more about exactly
what you're trying to do with this code.  I wrote:

> If I had to guess, you want to set every row in data_mg and data_dg to NaN where any value in the row is either less than 2 or NaN.  But instead it is only setting the first element of each such row to NaN.

Is that correct? Or not?

On Sun, Apr 11, 2021 at 6:22 AM Didrik Nilsen <didrik.nilsen at live.no> wrote:
>
>
> Thanks for the feedback. I kind of understand what you mean with the code, but I am not sure what to replace it with. This is a little bit of a long shot, but is there any way you would be able to help me with this over zoom?
>
> Didrik
>
> -----Original Message-----
> From: AstroPy <astropy-bounces+didrik.nilsen=live.no at python.org> On Behalf Of E. Madison Bray
> Sent: Friday, April 9, 2021 3:12 AM
> To: Astronomical Python mailing list <astropy at python.org>
> Subject: Re: [AstroPy] Plotting .fits data using filters
>
> Hi Didrik,
>
> I wonder what this part of your code is supposed to do:
>
> counter1 = 0
> for i in ratio:
>     counter2 = 0
>     for j in i:
>         if j < 2:
>             data_mg[counter1][counter2] = 'nan'
>             data_dg[counter1][counter2] = 'nan'
>         elif j == 'nan':
>             data_mg[counter1][counter2] = 'nan'
>             data_dg[counter1][counter2] = 'nan'
>
>     counter1 = counter1 + 1
>
> I'm curious, because I see you never advance "counter2"  It is always 0.
>
> If I had to guess, you want to set every row in data_mg and data_dg to NaN where any value in the row is either less than 2 or NaN.  But instead it is only setting the first element of each such row to NaN.
>
> Assuming I guessed correctly, here's a way you can do that with a bit of Numpy:
>
> This will give the row indexes for rows containing a value < 2 or NaN:
>
> >>> filt = np.where(np.any((ratio < 2) | np.isnan(ratio), axis=1))
>
> You can use this to set the corresponding rows in data_mg and data_dg:
>
> >>> data_mg[filt] = np.nan
> >>> data_dg[filt] = np.nan
>
> Hope that helps, though I'm not sure if that's what you want.  Please follow up with clarification otherwise.
>
> On Thu, Apr 8, 2021 at 2:14 AM Didrik Nilsen <didrik.nilsen at live.no> wrote:
> >
> > I am doing a research project in Astronomy where I am supposed to analyze three different pictures. I am using several different packages to extract the data from every pixel in every picture. To make sure that my data is reliable I am dividing one picture of one galaxy by another picture of the same galaxy, but the second picture is an error picture. To make sure my data is reliable I am dividing the two arrays by each other to get a ratio in order to plot and calculate the median with proper data. I think I have gotten the median right, but my plots are not. There are far too many plots based on the elements in the ratio array which is 23. Please help me. Here is my code. I can supply more info about the project if needed.
> >
> > glx_list = os.listdir(r'C:\Users\didri\Documents\Spring2021\PHYS481
> > Individual Research\H2_Maps-20210201T082125Z-001\H2_Maps')
> >
> > hello = 0
> >
> > for i in glx_list:
> >
> >         image_data_dg = 0
> >
> >         print(i)
> >
> >         map_gau = r'C:\Users\didri\Documents\Spring2021\PHYS481 Individual Research\SiVI_Maps-20210201T084648Z-001\SiVI_Maps' + '\\' + i + '\\SiVI\\Spat3\\map_gau.fits'
> >
> >         map_gau_err = r'C:\Users\didri\Documents\Spring2021\PHYS481 Individual Research\SiVI_Maps-20210201T084648Z-001\SiVI_Maps' + '\\' + i + '\\SiVI\\Spat3\\map_gau_err.fits'
> >
> >         disp_gau = r'C:\Users\didri\Documents\Spring2021\PHYS481 Individual Research\H2_Maps-20210201T082125Z-001\H2_Maps' + '\\' + i + '\\H2\\Spat3\\disp_gau.fits'
> >
> >         image_data_dg = fits.open(disp_gau)
> >
> >         image_data_mg = fits.open(map_gau)
> >
> >         image_data_err = fits.open(map_gau_err)
> >
> >         dshape = image_data_mg[0].data.shape
> >
> >         dshape = np.subtract(dshape, (1, 1))
> >
> >         y, x = dshape
> >
> >         data_mg = image_data_mg[0].data[0:x, 0:y].astype('float')
> >
> >         data_err = image_data_err[0].data[0:x, 0:y].astype('float')
> >
> >         data_dg = image_data_dg[0].data[0:x, 0:y].astype('float')
> >
> >         data_mg[data_mg == 0] = 'nan'
> >
> >         data_err[data_err == 0] = 'nan'
> >
> >         data_dg[data_dg == 0] = 'nan'
> >
> >         ratio = np.divide(data_mg[0:x, 0:y], data_err[0:x, 0:y])
> >
> >         low_snr = (ratio < 2)
> >
> >         bad_pixel = (ratio == np.nan)
> >
> >
> >         counter1 = 0
> >
> >         for i in ratio:
> >
> >                 counter2 = 0
> >
> >                 for j in i:
> >
> >                         if j < 2:
> >
> >                                 data_mg[counter1][counter2] = 'nan'
> >
> >                                 data_dg[counter1][counter2] = 'nan'
> >
> >                         elif j == 'nan':
> >
> >                                 data_mg[counter1][counter2] = 'nan'
> >
> >                                 data_dg[counter1][counter2] = 'nan'
> >
> >                 counter1 = counter1 + 1
> >
> >         plt.scatter(data_mg, data_dg)
> >
> >         plt.xlim(0, .2)
> >
> >         plt.ylim(0, 500)
> >
> >         #plt.savefig('plot' + str(hello))
> >
> >         hello = hello + 1
> >
> >         plt.show()
> >
> >         if hello == 1:
> >
> >                 break
> >
> >         plt.clf()
> >
> >
> >
> >
> >
> > data_mg = np.ndarray.flatten(data_mg)
> >
> > data_mg = data_mg[np.logical_not(np.isnan(data_mg))]
> >
> > data_dg = np.ndarray.flatten(data_dg)
> >
> > data_dg = data_dg[np.logical_not(np.isnan(data_dg))]
> >
> >
> >
> > b = np.where(np.array(data_dg > 100))
> >
> >
> >
> > median = np.median(data_mg[b])
> >
> > print('Flux median at dispersion > 100 km/s is ' + str(median))
> >
> > a = np.where(data_dg <= 100)
> >
> > median1 = np.median(data_mg[a])
> >
> > print('Flux median at dispersion <= 100 km/s is ' + str(median1))
> >
> >
> >
> >
> >
> > Best regards,
> >
> >
> >
> > Didrik Langmoen Nilsen
> >
> > University of Alaska Anchorage Ski Team
> >
> > BBA Global Logistics & Supply Chain Managment
> >
> >
> >
> > C | +1 (907) 301 5507
> >
> > E | dlnilsen at alaska.edu
> >
> >
> >
> >
> >
> > _______________________________________________
> > AstroPy mailing list
> > AstroPy at python.org
> > https://mail.python.org/mailman/listinfo/astropy
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy
```