From 4244be13cf5ba7b8a89ac06cd415756d18be9faf Mon Sep 17 00:00:00 2001 From: Tammo Jan Dijkema Date: Wed, 19 May 2021 17:36:18 +0200 Subject: [PATCH] implemented using bscale for storing std As suggested by @cbassa in #74 --- RMS/CompressionCy.pyx | 7 ++++--- RMS/Formats/FFbin.py | 3 +-- RMS/Formats/FFfile.py | 2 +- RMS/Formats/FFfits.py | 9 +++++++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/RMS/CompressionCy.pyx b/RMS/CompressionCy.pyx index 60c8bfb15..e7f39224d 100644 --- a/RMS/CompressionCy.pyx +++ b/RMS/CompressionCy.pyx @@ -157,8 +157,9 @@ def compressFrames(np.ndarray[INT8_TYPE_t, ndim=3] frames, int deinterlace_order # Subtract average squared sum of all values (acc*mean = acc*acc/frames_num_minus_four) var -= acc*mean - # Compute the standard deviation - var = sqrt(var/frames_num_minus_five) + # Compute 10 times the standard deviation (to keep one decimal when rounding to uint8) + # In FFbin and FFfits, the standard deviation is divided by 10 again + var = (10 * sqrt(var/frames_num_minus_five)) # Make sure that the stddev is not 0, to prevent divide by zero afterwards if var == 0: @@ -174,4 +175,4 @@ def compressFrames(np.ndarray[INT8_TYPE_t, ndim=3] frames, int deinterlace_order ftp_array[3, y, x] = var - return ftp_array, fieldsum[:frames_num*deinterlace_multiplier] \ No newline at end of file + return ftp_array, fieldsum[:frames_num*deinterlace_multiplier] diff --git a/RMS/Formats/FFbin.py b/RMS/Formats/FFbin.py index e70ae76bf..e4cab37dc 100644 --- a/RMS/Formats/FFbin.py +++ b/RMS/Formats/FFbin.py @@ -130,7 +130,7 @@ def write(ff, directory, filename, version=2): arr[0] = ff.maxpixel arr[1] = ff.maxframe arr[2] = ff.avepixel - arr[3] = ff.stdpixel + arr[3] = np.round(0.1 * ff.stdpixel) # Extract only the number from the camera code camno_num = int(re.findall('\d+', str(ff.camno))[0]) @@ -172,4 +172,3 @@ def write(ff, directory, filename, version=2): # Write image arrays arr.tofile(fid) - \ No newline at end of file diff --git a/RMS/Formats/FFfile.py b/RMS/Formats/FFfile.py index 12aff6914..e732f0074 100644 --- a/RMS/Formats/FFfile.py +++ b/RMS/Formats/FFfile.py @@ -1,4 +1,4 @@ -""" Functions for reading/writing FF files with respet to the rheir format (.bin for .fits). """ +""" Functions for reading/writing FF files with respect to their format (.bin for .fits). """ from __future__ import print_function, division, absolute_import diff --git a/RMS/Formats/FFfits.py b/RMS/Formats/FFfits.py index de7395d9c..e3e57deb1 100644 --- a/RMS/Formats/FFfits.py +++ b/RMS/Formats/FFfits.py @@ -76,6 +76,8 @@ def write(ff, directory, filename): Arguments: ff: [ff bin struct] FF bin file loaded in the FF structure + In this FF structure, the standard deviation is an uint8 with ten + times the actual value directory: [str] path to the directory where the file will be written filename: [str] name of the file which will be written @@ -115,7 +117,10 @@ def write(ff, directory, filename): maxpixel_hdu = fits.ImageHDU(ff.maxpixel, name='MAXPIXEL') maxframe_hdu = fits.ImageHDU(ff.maxframe, name='MAXFRAME') avepixel_hdu = fits.ImageHDU(ff.avepixel, name='AVEPIXEL') - stdpixel_hdu = fits.ImageHDU(ff.stdpixel, name='STDPIXEL') + stdpixel_hdu = fits.ImageHDU(0.1 * ff.stdpixel, name='STDPIXEL') + # Set BSCALE in fits metadata, so FITS readers will understand the scaling and + # return e.g. 4.3 when the number 43 is stored as uint8 + stdpixel_hdu.scale('uint8', bscale=0.1) # Create the primary part prim = fits.PrimaryHDU(header=head) @@ -152,7 +157,7 @@ def write(ff, directory, filename): maxpixel = np.zeros((ht, wid), dtype=np.uint8) avepixel = np.zeros((ht, wid), dtype=np.uint8) + 10 - stdpixel = np.zeros((ht, wid), dtype=np.uint8) + 20 + stdpixel = np.zeros((ht, wid), dtype=np.uint8) + 201 # Scaled by 10, so this represents 20.1 maxframe = np.zeros((ht, wid), dtype=np.uint8) + 30 ff.array = np.stack([maxpixel, maxframe, avepixel, stdpixel], axis=0)