# [AstroPy] Plotting .fits data using filters

E. Madison Bray erik.m.bray at gmail.com
Fri Apr 9 07:11:41 EDT 2021

```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.data.shape
>
>         dshape = np.subtract(dshape, (1, 1))
>
>         y, x = dshape
>
>         data_mg = image_data_mg.data[0:x, 0:y].astype('float')
>
>         data_err = image_data_err.data[0:x, 0:y].astype('float')
>
>         data_dg = image_data_dg.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)
>
>         mask = ~(low_snr & bad_pixel)
>
>         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
```

More information about the AstroPy mailing list