;****************************************************************** ; D. Shea ; converts bytes to floats using the "scale" and "offset" attributes (if present) ; Note: the CF and COARDS conventions require that ; "if both scale_factor and add_offset ; attributes are present, the data are first scaled before the offset is added" ; This follows these conventions. ; ; This function handles an input type of "ubyte". ;****************************************************************** undef("byte2flt") function byte2flt (xB) local xF, oNames, sNames, offset, scale, xAtts, nAtts, n begin xF = new ( dimsizes(xB), float) copy_VarAtts (xB, xF) copy_VarCoords (xB, xF) ; should data be 'scaled' and/or 'offset' ? ; names to check oNames = (/"add_offset", "offset", "OFFSET", "Offset", "_offset" \ ,"Intercept", "intercept", "add_off", "ADD_OFF"/) sNames = (/"scale", "SCALE", "Scale", "_scale", "scale_factor" , "factor" \ ,"Scale_factor", "Slope" , "slope", "ScaleFactor", "Scale_Factor" /) offset = 0.0 ; establish as type float scale = 1.0 xAtts = getvaratts(xB) nAtts = dimsizes(xAtts) do n=0,nAtts-1 if (any(oNames.eq.xAtts(n))) then if (typeof(xB@$xAtts(n)$).eq."float") then offset = xB@$xAtts(n)$ else if (typeof(xB@$xAtts(n)$).eq."double") then offset = doubletofloat(xB@$xAtts(n)$) end if if (typeof(xB@$xAtts(n)$).eq."string") then offset = stringtofloat(xB@$xAtts(n)$) end if end if delete(xF@$xAtts(n)$) ; xF will no longer have offset att break end if end do do n=0,nAtts-1 if (any(sNames.eq.xAtts(n))) then if (typeof(xB@$xAtts(n)$).eq."float") then scale = xB@$xAtts(n)$ else if (typeof(xB@$xAtts(n)$).eq."double") then scale = doubletofloat(xB@$xAtts(n)$) end if if (typeof(xB@$xAtts(n)$).eq."string") then scale = stringtofloat(xB@$xAtts(n)$) end if end if delete(xF@$xAtts(n)$) ; xF will no longer have scale att break end if end do if (scale.eq.1.0 .and. offset.eq.0.) then xF = (/ xB /) else xF = xB*scale + offset end if if (isatt(xB,"valid_range") .and. \ (typeof(xB@valid_range).eq."byte" .or. \ typeof(xB@valid_range).eq."ubyte") ) then vrB = xB@valid_range vrF = new ( dimsizes(vrB), float) vrF = vrB*scale + offset delete(xF@valid_range) ; delete the "byte" valid_range xF@valid_range = vrF ; recreate with float end if return (xF) end