[ncl-talk] byte2flt

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Fri Jul 14 18:51:11 MDT 2023


A long time, ago, there was ambiguity within Netcdf and NCL, as to whether
type "byte" was signed or unsigned.  Eventually everyone settled on
"signed".  NCL is attempting to interpret this byte array as signed, but
there are some clues that this file may have been originally written as
unsigned bytes.  That would cause NCL to misinterpret the data.  This would
also be consistent with your symptoms.

Unsigned maps into signed like this.  0 to 127 map straight across to 0 to
127.  128 to 255 maps to -128 to -1.  To unscramble, try this custom
formula (untested).  It is important to unscramble BEFORE unpacking with
scale and offset.

dBZ_b = f->Reflectivity          ! read from file
dbz2 = toshort (dBZ_b)           ! convert to a type that can hold full
positive range
dbz3 = where ( (dbz2 .lt. 0), (dbz2 + 256), dbz2)    ! decode negatives
into positives
copy_VarMeta (dBZ_b, dbz3)     ! restore original metadata
dbz4 = short2flt (dbz3)               ! unpack with scale and offset
printMinMax (dbz4,0)

If that doesn't come out right, then use printVarSummary and printMinMax at
every step, to see what is going on.  Another diagnostic would be to count
discrete original values, and examine their distribution curve.


On Fri, Jul 14, 2023 at 5:57 PM Laura Fowler via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:

> Hello:
>
> I am trying to read Nexrad Reflectivity data from a netCDF file in which
> Reflectivity has a "byte" format and is defined as:
>
> radar-data/20120611> ncdump -h 20120611T1900Z.nc
>
> byte Reflectivity(Altitude, Latitude, Longitude) ;
>
> Reflectivity:long_name = "Radar Reflectivity" ;
>
> Reflectivity:units = "dBZ" ;
>
> Reflectivity:scale_factor = 0.5f ;
>
> Reflectivity:add_offset = -33.f ;
>
> Reflectivity:missing_value = 0b ;
>
> A printVarSummary of the dBZ_b variable that points to Reflectivity shows:
>
> Variable: dBZ_b
>
> Type: byte
>
> Total Size: 10064208 bytes
>
>             10064208 values
>
> Number of Dimensions: 3
>
> Dimensions and sizes: [Altitude | 24] x [Latitude | 539] x [Longitude |
> 778]
>
> Coordinates:
>
>             Altitude: [ 1..24]
>
>             Latitude: [30.94..41.7]
>
>             Longitude: [260.12..275.66]
>
> Number Of Attributes: 6
>
>   long_name : Radar Reflectivity
>
>   units : dBZ
>
>   scale_factor : 0.5
>
>   add_offset : -33
>
>   missing_value : 0
>
>   _FillValue : 0
>
> After reading the section on "Coercion of types", and for now ignoring
> scale_factor and add_offset, I simply used the tofloat function to output
> the max and min values of dBZ_b. I got:
>
> (0) --- max dBZ_b = 127
>
> (0) --- min dBZ_b = -128
>
>
> Finally, using scale_factor and add_offset, I used the function byte2flt
> to convert dBZ_b to a float and output the max and min value of dBZ_s, or:
>
> dBZ_s = byte2flt(dBZ_b)
>
> (0) --- max dBZ_s = 30.5
>
> (0) --- min dBZ_s = -97
>
> The maximum value of dBZ_s is much lower than I expected (the maximum
> value of dBZ_s should actually be 58.5 dBZ, as derived by an IDL script
> used to plot the nexrad Reflectivity data for a peer-review publication).
>
> Can somebody help me figure out why I am getting values that are too low
> relative to what I am expecting. I also tried to bypass the byte2flt
> function and use the scale_factor and add_offset variables (x_float =
> x_byte*scale + offset), but to no avail.
>
> If you interested to help me out, my working directory on cheyenne is  /glade/u/home/laura/fromYunYao/radar-data/20120611;
> the data file is 20120611T1900Z.nc; the ncl script is read.ncl; and the
> main idl script is nexrad.pro.
>
> Many thanks,
> Laura
> --
>
> !-------------------------------------------------------------------------------------------------------------
> Laura D. Fowler
>
> Mesoscale and Microscale Meteorology Division (MMM)
> National Center for Atmospheric Research
> P.O. Box 3000, Boulder CO 80307-3000
>
> e-mail: laura at ucar.edu
> phone: 303-497-1628
>
> !-------------------------------------------------------------------------------------------------------------
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230714/d8471831/attachment.htm>


More information about the ncl-talk mailing list