Ignore:
Timestamp:
04/27/10 15:48:24 (14 years ago)
Author:
Malte Marquarding
Message:

Finishing touches to opacity calculations, docs, plotting and model

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/opacity.py

    r1722 r1725  
    66from asap import selector
    77from asap import rcParams
    8 from asap import xyplotter
     8from asap._asap import atmosphere
     9
     10
     11class model(object):
     12    def _to_pascals(self, val):
     13        if val > 2000:
     14            return val
     15        return val*100
     16
     17    def __init__(self, temperature=288, pressure=101325., humidity=0.5,
     18                 elevation=700.):
     19        """
     20        This class implements opacity/atmospheric brightness temperature model
     21        equivalent to the model available in MIRIAD. The actual math is a
     22        convertion of the Fortran code written by Bob Sault for MIRIAD.
     23        It implements a simple model of the atmosphere and Liebe's model (1985)
     24        of the complex refractive index of air.
     25
     26        The model of the atmosphere is one with an exponential fall-off in
     27        the water vapour content (scale height of 1540 m) and a temperature
     28        lapse rate of 6.5 mK/m. Otherwise the atmosphere obeys the ideal gas
     29        equation and hydrostatic equilibrium.
     30
     31        Note, the model includes atmospheric lines up to 800 GHz, but was not
     32        rigorously tested above 100 GHz and for instruments located at
     33        a significant elevation. For high-elevation sites it may be necessary to
     34        adjust scale height and lapse rate.
     35
     36        Parameters:
     37            temperature:    air temperature at the observatory (K)
     38            pressure:       air pressure at the sea level if the observatory
     39                            elevation is set to non-zero value (note, by
     40                            default is set to 700m) or at the observatory
     41                            ground level if the elevation is set to 0. (The
     42                            value is in Pascals or hPa, default 101325 Pa
     43            humidity:       air humidity at the observatory (fractional),
     44                            default is 0.5
     45            elevation:     observatory elevation about sea level (in meters)
     46        """
     47        self._atm = atmosphere(temp, self._to_pascals(pressure), humidity)
     48        self.set_observatory_elevation(elevation):
     49
     50    def get_opacities(self, freq, elevation=None):
     51        """Get the opacity value(s) for the fiven frequency(ies).
     52        If no elevation is given the opacities for the zenith are returned.
     53        If an elevation is specified refraction is also taken into account.
     54        Parameters:
     55            freq:       a frequency value in Hz, or a list of frequency values.
     56                        One opacity value per frequency is returned as a scalar
     57                        or list.
     58            elevation:  the elevation at which to compute the opacity. If `None`
     59                        is given (default) the zenith opacity is returned.
     60
     61
     62        """
     63        func = None
     64        if isinstance(freq, (list, tuple)):
     65            if elevation is None:
     66                return self._atm.zenith_opacities(freq)
     67            else:
     68                elevation *= math.pi/180.
     69                return self._atm.opacities(freq, elevation)
     70        else:
     71            if elevation is None:
     72                return self._atm.zenith_opacity(freq)
     73            else:
     74                elevation *= math.pi/180.
     75                return self._atm.opacity(freq, elevation)
     76
     77    def set_weather(self, temperature, pressure, humidity):
     78        """Update the model using the given environmental parameters.
     79        Parameters:
     80            temperature:    air temperature at the observatory (K)
     81            pressure:       air pressure at the sea level if the observatory
     82                            elevation is set to non-zero value (note, by
     83                            default is set to 700m) or at the observatory
     84                            ground level if the elevation is set to 0. (The
     85                            value is in Pascals or hPa, default 101325 Pa
     86            humidity:       air humidity at the observatory (fractional),
     87                            default is 0.5
     88        """
     89        pressure = self._to_pascals(pressure)
     90        self._atm.set_weather(temperature, pressure, humidity)
     91
     92    def set_observatory_elevation(self, elevation:
     93        """Update the model using the given the observatory elevation
     94        Parameters:
     95            elevation:  the elevation at which to compute the opacity. If `None`
     96                        is given (default) the zenith opacity is returned.
     97        """
     98        self._atm.set_observatory_elevation(el)
     99
    9100
    10101def _import_data(data):
     
    27118    return merge(tables)
    28119
    29 def skydip(data, averagepol=True, tsky=300., plot=False):
     120def skydip(data, averagepol=True, tsky=300., plot=False,
     121           temperature=288, pressure=101325., humidity=0.5):
    30122    """Determine the opacity from a set of 'skydip' obervations.
    31123    This can be any set of observations over a range of elevations,
     
    60152    rcsave = rcParams['verbose']
    61153    rcParams['verbose'] = False
     154    if plot:
     155        from matplotlib import pylab
    62156    scan = _import_data(data)
    63157    f = fitter()
     
    68162    pnos = scan.getpolnos()
    69163    opacities = []
     164    om = opacitymodel(temperature, pressure, humidity)
    70165    for ino in inos:
    71166        sel.set_ifs(ino)
     
    74169        airms = []
    75170        tsyss = []
    76 
    77171        if plot:
    78             xyplotter.cla()
    79             xyplotter.ioff()
    80             xyplotter.clf()
    81             xyplotter.xlabel("Airmass")
    82             xyplotter.ylabel(r"$T_{sys}$")
     172            pylab.cla()
     173            pylab.ioff()
     174            pylab.clf()
     175            pylab.xlabel("Airmass")
     176            pylab.ylabel(r"$T_{sys}$")
    83177        for pno in pnos:
    84178            sel.set_polarisations(pno)
     
    102196        if plot:
    103197            colors = ['b','g','k']
    104             for i in range(len(airms)):
    105                 xyplotter.plot(airms[i], tsyss[i], 'o', color=colors[i])
    106                 xyplotter.plot(airms[i], fits[i], '-', color=colors[i])
    107                 xyplotter.figtext(0.7,0.3-(i/30.0),
     198            n = len(airms)
     199            for i in range(n):
     200                pylab.plot(airms[i], tsyss[i], 'o', color=colors[i])
     201                pylab.plot(airms[i], fits[i], '-', color=colors[i])
     202                pylab.figtext(0.7,0.3-(i/30.0),
    108203                                  r"$\tau_{fit}=%0.2f$" % opacity[i],
    109204                                  color=colors[i])
    110205            if averagepol:
    111                 xyplotter.figtext(0.7,0.3-(len(airms)/30.0),
    112                                   r"$\tau=%0.2f$" % opacities[-1],
     206                pylab.figtext(0.7,0.3-(n/30.0),
     207                                  r"$\tau_{avg}=%0.2f$" % opacities[-1],
    113208                                  color='r')
    114             xyplotter.title("IF%d : %s" % (ino, freqstr))
    115 
    116             xyplotter.ion()
    117             xyplotter.draw()
     209                n +=1
     210            pylab.figtext(0.7,0.3-(n/30.0),
     211                          r"$\tau_{model}=%0.2f$" % om.get_opacities(freq*1e9),
     212                          color='grey')
     213           
     214            pylab.title("IF%d : %s" % (ino, freqstr))
     215
     216            pylab.ion()
     217            pylab.draw()
    118218            raw_input("Hit <return> for next fit...")
    119219        sel.reset()
     
    122222    rcParams['verbose'] = rcsave
    123223    if plot:
    124         xyplotter.close()
     224        pylab.close()
    125225    return opacities
Note: See TracChangeset for help on using the changeset viewer.