<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
Hi,
<div class=""><br class="">
</div>
<div class="">
<blockquote type="cite" class="">
<div dir="ltr" class="">
<div class="">That's why I don't understand the problem because I have been told that </div>
<div class="">the <b class="">PIXEL VALUES </b>should remain the same <span class="" style="font-size: 12.8px;">it's just that the dynamic range with the BITPIX=16,BZERO=32767 should be from 0 to 65535. </span></div>
</div>
</blockquote>
</div>
<div class=""><br class="">
</div>
<div class="">I think this is true as long as the data is already in the range 0 to 65535; in that case the data is actually stored in the FITS files as signed 16 bit integers which are transparently changed to unsigned int when you read it with astropy.io.fits
 or other FITS tools.</div>
<div class=""><br class="">
</div>
<div class="">In your case you will need to transform the pixel values because you need to change the range of your data.</div>
<div class=""><br class="">
</div>
<div class="">If I understand your use case, you need to squeeze the data into an int16 range because another tool handles that. </div>
<div class=""><br class="">
</div>
<div class="">Two options are to manually calculate a BZERO/BSCALE and scale the data with that or to convert the data int16 or uint16 and let astropy.io.fits set BZERO/BSCALE for you.</div>
<div class=""><br class="">
</div>
<div class="">I would recommend the latter…it will not affect the dynamic range of the data and saves you the hassle of calculating those things yourself. The other reason is that numpy has a way of representing uint16 or int16. </div>
<div class=""><br class="">
</div>
<div class="">I’ll address how to do want you want by converting the data to uint16. </div>
<div class=""><br class="">
</div>
<div class="">In your case you need to scale the values so that they fit in the range 0 to 65535.</div>
<div class=""><br class="">
</div>
<div class="">I would do something like (using the same example data as before)(have not tested this :)):</div>
<div class=""><br class="">
</div>
<div class="">from __future__ import division  # if you are running Python 2</div>
<div class="">
<div class="">my_data = np.array([[90000, 2.0], [-1.0, -4000]])</div>
<div class=""><span class="Apple-tab-span" style="white-space: pre;"></span>my_hdu = fits.PrimaryHDU(my_data)</div>
</div>
<div class="">data_min = my_hdu.data.min()<br class="Apple-interchange-newline">
data_max = my_hdu.data.max()</div>
<div class=""><br class="">
</div>
<div class=""># Yes, the maximum value should be 65535 not 65534</div>
<div class="">new_data = 65535 * (my_hdu.data - data_min) / (data_max - data_min) </div>
<div class=""><br class="">
</div>
<div class=""># The line below will automatically revise BITPIX/BZERO/BSCALE</div>
<div class="">my_hdu.data = new_data.astype(np.uint16)</div>
<div class=""><br class="">
</div>
<div class="">my_hdu.writeto('ui16_image.fits’)</div>
<div class=""><br class="">
</div>
<div class="">mh_hdu.data</div>
<div class="">
<div class="">array([[65535,  <span class="Apple-tab-span" style="white-space:pre">
</span>2790],</div>
<div class="">       <span class="Apple-tab-span" style="white-space:pre"> </span>
  [ 2788,     <span class="Apple-tab-span" style="white-space:pre"></span>      0]], dtype=uint16)</div>
</div>
<div class=""><br class="">
</div>
<div class=""># Note that the largest and smallest pixel values in the original array are still the largest and smallest </div>
<div class=""># pixels in the result.</div>
<div class=""><br class="">
<div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
Matt Craig</div>
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<br class="">
schedule:  <a href="http://physics.mnstate.edu/craig" class="">http://physics.mnstate.edu/craig</a><br class="">
——
<div class="">Professor<br class="">
Department of Physics and Astronomy </div>
<div class="">Minnesota State University Moorhead<br class="">
1104 7th Ave S, Moorhead MN 56563<br class="">
<br class="">
office: Hagen 307F<br class="">
phone: (218) 477-2439<br class="">
fax: (218) 477-2290</div>
</div>
</div>
</div>
</div>
<br class="">
<div>
<blockquote type="cite" class="">
<div class="">On Oct 28, 2017, at 1:10 PM, Diego Farias <<a href="mailto:dnfarias@uc.cl" class="">dnfarias@uc.cl</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">Hi Matt and everyone:
<div class=""><br class="">
</div>
<div class="">I did the same 'experiments'! and I realized that I 'loose' information if I scale to 'uint16' . That's why I don't understand the problem because I have been told that </div>
<div class="">the <b class="">PIXEL VALUES </b>should remain the same <span style="font-size:12.8px" class="">it's just that the dynamic range with the BITPIX=16,BZERO=32767 should be from 0 to 65535. As</span> I'm not clever enough I'm stuck in this step..
 I'm guessing that later we need to use another astronomical tool that requires the images with BITPIX = 16. In my despair, I found this:</div>
<div class=""><br class="">
</div>
<div class=""><a href="https://github.com/varenius/salsa/blob/master/Control_program/spectrum.py" class="">https://github.com/varenius/salsa/blob/master/Control_program/spectrum.py</a><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">where the important step is<br class="">
</div>
<div class=""><br class="">
</div>
<div class="">
<table class="gmail-tab-size gmail-highlight gmail-js-file-line-container" style="box-sizing:border-box;border-collapse:collapse;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";font-size:14px">
<tbody style="box-sizing:border-box" class="">
<tr style="box-sizing:border-box" class="">
</tr>
<tr style="box-sizing:border-box" class="">
<td id="gmail-LC234" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="gmail-pl-c" style="box-sizing:border-box">#</span>Since using int16 as datatype we use bscale and bzero to keep dynamic range. #SalsaJ cannot read bitpix correctly except
 16 bit. If SalsaJ could read bitpix #</span>we could just have BITPIX -64 and skip Bscale, Bzero, i.e. just remove #astype above.</td>
</tr>
<tr style="box-sizing:border-box" class="">
<td id="gmail-L235" class="gmail-js-line-number gmail-blob-num" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top">
</td>
</tr>
</tbody>
</table>
</div>
<div class=""><br class="">
</div>
<div class="">
<table class="gmail-tab-size gmail-highlight gmail-js-file-line-container" style="box-sizing:border-box;border-collapse:collapse;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";font-size:14px">
<tbody style="box-sizing:border-box" class="">
<tr style="box-sizing:border-box" class="">
<td id="gmail-LC229" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-size:12px;word-wrap:normal;white-space:pre">
datamin <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">
=</span> np.min(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">self</span>.data)
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;white-space:normal" class="">
<table class="gmail-tab-size gmail-highlight gmail-js-file-line-container" style="box-sizing:border-box;border-collapse:collapse;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";font-size:14px">
<tbody style="box-sizing:border-box" class="">
<tr style="box-sizing:border-box" class="">
<td id="gmail-LC230" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-size:12px;word-wrap:normal;white-space:pre">
datamax <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">
=</span> np.max(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">self</span>.data) bscale
<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> (datamax<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-</span>datamin)<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">/</span><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">65534.0
 #shouldn't be 65535.0? bzero <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">
=</span><span style="color:rgb(36,41,46)" class=""> datamin</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">+</span><span style="color:rgb(36,41,46)" class="">bscale</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">*</span><span class="gmail-pl-c1" style="box-sizing:border-box">32767.0
</span>scaledata <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">
=</span><span style="color:rgb(36,41,46)" class=""> (</span><span class="gmail-pl-c1" style="box-sizing:border-box">self</span><span style="color:rgb(36,41,46)" class="">.data
</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-</span><span style="color:rgb(36,41,46)" class=""> bzero)</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">/</span><span style="color:rgb(36,41,46)" class="">bscale
</span>hdu.data <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">
=</span><span style="color:rgb(36,41,46)" class=""> scaledata.reshape(</span><span class="gmail-pl-c1" style="box-sizing:border-box">1</span><span style="color:rgb(36,41,46)" class="">,
</span><span class="gmail-pl-c1" style="box-sizing:border-box">1</span><span style="color:rgb(36,41,46)" class="">,
</span><span class="gmail-pl-c1" style="box-sizing:border-box">self</span><span style="color:rgb(36,41,46)" class="">.nchans).<b class="">astype(np.int16)</b></span></span></td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="">
<table class="gmail-tab-size gmail-highlight gmail-js-file-line-container" style="box-sizing:border-box;border-collapse:collapse;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";font-size:14px">
<tbody style="box-sizing:border-box" class="">
<tr style="box-sizing:border-box" class="">
<td id="gmail-LC234" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><br class="">
</span></td>
</tr>
<tr style="box-sizing:border-box" class="">
<td id="gmail-L235" class="gmail-js-line-number gmail-blob-num" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top">
</td>
<td id="gmail-LC235" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">
</td>
</tr>
</tbody>
</table>
</div>
<div class="">Shouldn't this <b class="">also changes </b>the values? (in this case, spectrum values). Also, </div>
<div class="">bscale ~ 1., bzero ~ 326767.</div>
<div class=""><br class="">
</div>
<div class="">Thanks a lot,</div>
<div class=""><br class="">
</div>
<div class="">Diego</div>
<div class="">
<table class="gmail-tab-size gmail-highlight gmail-js-file-line-container" style="box-sizing:border-box;border-collapse:collapse;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";font-size:14px">
<tbody style="box-sizing:border-box" class="">
<tr style="box-sizing:border-box" class="">
<td id="gmail-LC229" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;white-space:normal" class="">
<br class="">
</div>
</td>
</tr>
<tr style="box-sizing:border-box" class="">
<td id="gmail-L230" class="gmail-js-line-number gmail-blob-num" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top">
</td>
<td id="gmail-LC230" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">
</td>
</tr>
</tbody>
</table>
</div>
<div class=""><br class="">
</div>
<div class="">
<table class="gmail-tab-size gmail-highlight gmail-js-file-line-container" style="box-sizing:border-box;border-collapse:collapse;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";font-size:14px">
<tbody style="box-sizing:border-box" class="">
<tr style="box-sizing:border-box" class="">
<td id="gmail-LC237" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">
<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)"></span></td>
</tr>
<tr style="box-sizing:border-box" class="">
<td id="gmail-L238" class="gmail-js-line-number gmail-blob-num" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top">
<br class="">
<br class="">
</td>
<td id="gmail-LC238" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">
</td>
</tr>
<tr style="box-sizing:border-box" class="">
<td id="gmail-L239" class="gmail-js-line-number gmail-blob-num" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top">
</td>
<td id="gmail-LC239" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">
</td>
</tr>
<tr style="box-sizing:border-box" class="">
<td id="gmail-L240" class="gmail-js-line-number gmail-blob-num" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top">
</td>
<td id="gmail-LC240" class="gmail-js-file-line gmail-blob-code gmail-blob-code-inner" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre">
</td>
</tr>
</tbody>
</table>
</div>
</div>
_______________________________________________<br class="">
AstroPy mailing list<br class="">
<a href="mailto:AstroPy@python.org" class="">AstroPy@python.org</a><br class="">
https://mail.python.org/mailman/listinfo/astropy<br class="">
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>