next up previous
Next: My file has been converted to NDF. How do I access FITS header keywords?
Up: Writing csh scripts
Previous: How do I overplot contours from one image on to another?

How do I use scientific notation in bc?

If you want to make use of the bc utility to carry out floating-point calculations in csh scripts you may come across a problem with scientific notation. In many cases applications return scientific notation in the form 3.0E+08, or 3.0E-0.8, while bc requires these numbers to be of the form 3.0*10^08, or 3.0*10^-08. The following example segment of code takes two numbers in the first format and passes them to bc in the correct manner so that it can do some arithmetic with them. bc will return a floating-point number (not in scientific notation).

   # first number
   set num1 = `echo ${num1} | sed 's/E/\\*10\\^/' | sed 's/+//'`
   
   # second number
   set num2 = `echo ${num2} | sed 's/E/\\*10\\^/' | sed 's/+//'`
   
   # answer
   set num3 = `echo "scale = 15; ${num1}-${num2}" | bc`

The scale specifies the number of decimal places.

An alternative to using bc inside your scripts is the KAPPA calc command which can evaluate many arbitrary mathematical expressions, as in this extract.

   # calculate velocity
   set delta_lambda = \
        `calc exp="'${centre_fit} - ${line_centre}'" prec=_double`

   set velocity = \
        `calc exp="'${delta_lambda}/${line_centre}'" prec=_double`

   set velocity = \
        `calc exp="'${velocity}*3.0E+08'" prec=_double`

here we evaluate the Doppler velocity of an emission line using three calls to calc, although the velocity could have been derived in a single expression.


next up previous
Next: My file has been converted to NDF. How do I access FITS header keywords?
Up: Writing csh scripts
Previous: How do I overplot contours from one image on to another?

The IFU Data-Product Cookbook
Starlink Cookbook 16
A. Allan & Malcolm J. Currie
2008 July 4
E-mail:ussc@star.rl.ac.uk

Copyright © 2009 Science and Technology Facilities Council