Welcome to pyatmlab’s documentation!¶
Contents:
This is pyatmlab. You can find the code on github.
Pyatmlab needs a number of other packages: numpy, scipy, matplotlib, basemap, h5py, pyhdf, pyproj, pytz, statsmodels, joblib. Pip knows them all.
This is pyatmlab
arts package¶
Functionality related to ARTS.
arts.xml module¶
Read and write Arts XML types
This module contains classes for reading and writing Arts XML types. It is built on top of xml.etree.ElementTree, available since Python 2.5, but this module relies on the version 3.3 or newer. Most users will only need the ‘parse’ function.
FIXME: - Does <arts> tag always contain exactly one child?
collocations module¶
config module¶
Handle specific configuration settings, such as data locations.
Configuration is handled with a .pyatmlabrc file with a
configparser
syntax.
constants module¶
Definitions for physical constants
dataset module¶
Module containing classes abstracting datasets
-
class
pyatmlab.dataset.
Dataset
(*, memory=None, **kwargs)[source]¶ Represents a dataset.
This is an abstract class. More specific subclasses are SingleFileDataset and MultiFileDataset. Do not subclass Dataset directly.
Attributes
- start_date
- Starting date for dataset. May be used to search through ALL granules. WARNING! If this is set at a time t_0 before the actual first measurement t_1, then the collocation algorith (see CollocatedDataset) will conclude that there are 0 collocations in [t_0, t_1], and will not realise if data in [t_0, t_1] are actually added later!
- end_date
- Similar to start_date, but for ending.
- name
- Name for the dataset. May be used for logging purposes and so.
- aliases
- Aliases for field. Dictionary can be useful if you want to programmatically loop through the same field for many different datasets, but they are named differently. For example, an alias could be “ch4_profile”.
- unique_fields
- Set of fields that make any individual measurement unique. For example, the default value is {“time”, “lat”, “lon”}.
- related
- Dictionary whose keys may refer to other datasets with related information, such as DMPs or flags.
- timezone
- Refers to a pytz timezone object. Defaults to UTC.
-
combine
(my_data, other_obj)[source]¶ Combine with data from other dataset.
Combine a set of measurements from this dataset with another dataset, where each individual measurement correspond to exactly one from the other one, as identified by time/lat/lon, orbitid, or measurument id, or other characteristics. The object attribute unique_fields determines how those are found.
The other dataset may contain flags, DMPs, or different information altogether.
Parameters: - my_data (ndarray) – Data as returned from self.read.
- other_obj (Dataset) – Other dataset from which to read corresponding data.
-
static
extend_with_dofs
(M)[source]¶ Calculate DOFs
Parameters: M – ndarray with dtype as for self.read Returns M: M with added field dof
-
static
extend_with_doy_localtime
(M)[source]¶ Calculate DOY and mean local solar time
Parameters: M – ndarray with dtype as returned by self.read Returns M: ndarray with dtype extended with doy and mlst
-
find_granules
(start=datetime.datetime(1, 1, 1, 0, 0), end=datetime.datetime(9999, 12, 31, 23, 59, 59, 999999))[source]¶ Loop through all granules for indicated period.
This is a generator that will loop through all granules from start to end, inclusive.
See also: find_granules_sorted
Parameters: - start (datetime) – Starting datetime, defaults to any
- end (datetime) – Ending datetime, defaults to any
Yields: pathlib.Path objects for all granules
-
find_granules_sorted
(start=None, end=None)[source]¶ Yield all granules sorted by starting time then ending time.
For details, see find_granules.
-
get_additional_field
(M, fld)[source]¶ Get additional field.
Get field from other dataset, original objects, or otherwise. To be implemented by subclass implementations.
Exact fields depend on subclass.
-
read
(f=None, fields='all')[source]¶ Read granule in file and do some other fixes
Uses self._read. Do not override, override _read instead.
Parameters: - f (str) – Path to file
- fields (list) – Fields to read or “all” (default)
-
read_all
(fields='all', onerror='skip')[source]¶ Read all data in one go.
Warning: for some datasets, this may cause memory problems.
Parameters: - fields – List of fields to read, or “all” (default)
- onerror – What to do in case of an error. See read_period.
-
read_period
(start=None, end=None, onerror='skip', fields='all')[source]¶ Read all granules between start and end, in bulk.
Parameters: - start (datetime) – Starting time, None for any time
- end (datetime) – Ending time, None for any time
- onerror (str) – What to do with unreadable files. Defaults to “skip”, can be set to “raise”.
- fields (list) – List of fields to read, or “all” (default).
Returns: Masked array with all data in period.
-
class
pyatmlab.dataset.
HomemadeDataset
(**kwargs)[source]¶ For any dataset created by pyatmlab.
No content yet.
-
class
pyatmlab.dataset.
HyperSpectral
(*, memory=None, **kwargs)[source]¶ Superclass for any hyperspectral instrument
-
exception
pyatmlab.dataset.
InvalidFileError
[source]¶ Raised when the requested information cannot be obtained from the file
-
class
pyatmlab.dataset.
MultiFileDataset
(**kwargs)[source]¶ Represents a dataset where measurements are spread over multiple files.
If filenames contain timestamps, this information is used to determine the time for a granule or measurement. If filenames do not contain timestamps, this information is obtained from the file contents.
Contains the following attributes:
basedir: Describes the directory under which all granules are located. Can be either a string or a pathlib.Path object. subdir:: Describes the directory within basedir where granules are located. May contain string formatting directives where particular fields are replaces, such as `year`, `month`, and `day`. For example: `subdir = '{year}/{month}'. Sorting cannot be more narrow than by day. re:: Regular expression that should match valid granule files within `basedir` / `subdir`. Should use symbolic group names to capture relevant information when possible, such as starting time, orbit number, etc. For time identification, relevant fields are contained in MultiFileDataset.date_info, where each field also exists in a version with "_end" appended. MultiFileDataset.refields contains all recognised fields. If any *_end fields are found, the ending time is equal to the beginning time with any *_end fields replaced. If no *_end fields are found, the `granule_duration` attribute is used to determine the ending time, or the file is read to get the ending time (hopefully the header is enough). granule_cache_file:: If set, use this file to cache information related to granules. This is used to cache granule times if those are not directly inferred from the filename. Otherwise, this is not used. The full path to this file shall be `basedir` / `granule_cache_file`. granule_duration:: If the filename contains starting times but no ending times, granule_duration is used to determine the ending time. This should be a datetime.timedelta object.
-
find_dir_for_time
(dt)[source]¶ Find the directory containing granules/measurements at (date)time
For a given datetime object, find the directory that contains granules/measurument files for this particular time.
Parameters: dt (datetime) – Timestamp for inquiry. Any object with year, month, day attributes works. Returns: pathlib.Path object to relevant directory
-
get_subdir_resolution
()[source]¶ Return the resolution for the subdir precision.
Returns “year”, “month”, “day”, or None (if there is no subdir).
-
get_time_from_granule_contents
(p)[source]¶ Get datetime objects for beginning and end of granule
If it returns None, then use same as start time.
-
get_times_for_granule
(p, **kwargs)[source]¶ For granule stored in path, get start and end times.
May take hints for year, month, day, hour, minute, second, and their endings, according to self.date_fields
-
iterate_subdirs
(d_start, d_end)[source]¶ Iterate through all subdirs in dataset.
Note that this does not check for existance of those directories.
Yields a 2-element tuple where the first contains information on year(/month/day), and the second is the path.
Parameters: - d_start (date) – Starting date
- d_end (date) – Ending date
-
-
class
pyatmlab.dataset.
ProfileDataset
(*, memory=None, **kwargs)[source]¶ Abstract superclass with stuff for anything having profiles
Additional attributes compared to its parent:
- range::
If applicable, a tuple with (lo, hi) vertical limits of sensitivity range in metre.-
get_z
(dt)[source]¶ Get z-profile in metre.
Takes as argument a single measurement, with the dtype returned by its own reading routine.
-
-
class
pyatmlab.dataset.
SingleFileDataset
(*, memory=None, **kwargs)[source]¶ Represents a dataset where all measurements are in one file.
-
find_granules
(start=datetime.datetime(1, 1, 1, 0, 0), end=datetime.datetime(9999, 12, 31, 23, 59, 59, 999999))[source]¶ Loop through all granules for indicated period.
This is a generator that will loop through all granules from start to end, inclusive.
See also: find_granules_sorted
param datetime start: Starting datetime, defaults to any param datetime end: Ending datetime, defaults to any yields: pathlib.Path objects for all granules Docstring inherited from parent
-
-
class
pyatmlab.dataset.
SingleMeasurementPerFileDataset
(**kwargs)[source]¶ Represents datasets where each file contains one measurement.
An example of this would be ACE, or some RO datasets.
Special attributes:
filename_fields:: dict with {name, dtype} for fields that should be copied from the filename (as obtained with self.re) into the header
datasets package¶
This package contains modules defining datasets
datasets.groundbased module¶
Ground-based remote sensing datasets
-
class
pyatmlab.datasets.groundbased.
Eureka_PRL_CH4_HDF
(**kwargs)[source]¶ Represents a dataset where measurements are spread over multiple files.
If filenames contain timestamps, this information is used to determine the time for a granule or measurement. If filenames do not contain timestamps, this information is obtained from the file contents.
Contains the following attributes:
basedir: Describes the directory under which all granules are located. Can be either a string or a pathlib.Path object. subdir:: Describes the directory within basedir where granules are located. May contain string formatting directives where particular fields are replaces, such as `year`, `month`, and `day`. For example: `subdir = '{year}/{month}'. Sorting cannot be more narrow than by day. re:: Regular expression that should match valid granule files within `basedir` / `subdir`. Should use symbolic group names to capture relevant information when possible, such as starting time, orbit number, etc. For time identification, relevant fields are contained in MultiFileDataset.date_info, where each field also exists in a version with "_end" appended. MultiFileDataset.refields contains all recognised fields. If any *_end fields are found, the ending time is equal to the beginning time with any *_end fields replaced. If no *_end fields are found, the `granule_duration` attribute is used to determine the ending time, or the file is read to get the ending time (hopefully the header is enough). granule_cache_file:: If set, use this file to cache information related to granules. This is used to cache granule times if those are not directly inferred from the filename. Otherwise, this is not used. The full path to this file shall be `basedir` / `granule_cache_file`. granule_duration:: If the filename contains starting times but no ending times, granule_duration is used to determine the ending time. This should be a datetime.timedelta object.
-
class
pyatmlab.datasets.groundbased.
NDACCAmes
(**kwargs)[source]¶ NDACC Ames-style file
Documented at http://www.ndsc.ncep.noaa.gov/data/formats/
datasets.tanso module¶
-
class
pyatmlab.datasets.tanso.
TansoFTSBase
(*, memory=None, **kwargs)[source]¶ Applicable to both Tanso FTS versions
-
class
pyatmlab.datasets.tanso.
TansoFTSv001
(*, memory=None, **kwargs)[source]¶ Represents a dataset where all measurements are in one file.
-
get_z
(meas)[source]¶ Get z-profile in metre.
Takes as argument a single measurement, with the dtype returned by its own reading routine.Docstring inherited from parent
-
classmethod
vmr_to_column_density
(data)[source]¶ Convert VMR to column density
Currently hardcoded to CH4 profiles and the dtype by this classes’ read method.
Returns number column density and its error
Note that a small error may be introduced if profiles do not go down to the reference provided by (z0, p0), because the p2z function needs a reference inside the p-profile. This is mitigated by adding a dummy level with a dummy temperature, then removing this after the z-profile is calculated. This has a minor (<0.1%) effect on the remaining levels.
-
-
class
pyatmlab.datasets.tanso.
TansoFTSv10x
(**kwargs)[source]¶ Tanso FTS v1.00, 1.01
-
get_time_from_granule_contents
(p)[source]¶ Get datetime objects for beginning and end of granule
If it returns None, then use same as start time.Docstring inherited from parent
-
datasets.tovs module¶
Datasets for TOVS/ATOVS
-
class
pyatmlab.datasets.tovs.
HIRS
(**kwargs)[source]¶ High-resolution Infra-Red Sounder.
This class can read HIRS l1b as published in the NOAA CLASS archive.
Work in progress.
-
classmethod
get_definition_from_PDF
(path_to_pdf)[source]¶ Get HIRS definition from NWPSAF PDF.
This method needs the external program pdftotext. Put the result in header_dtype manually, but there are some corrections (see comments in source code).
Parameters: path_to_pdf (str) – Path to document NWPSAF-MF-UD-003_Formats.pdf Returns: (head_dtype, head_format, line_dtype, line_format)
-
classmethod
-
class
pyatmlab.datasets.tovs.
HIRS2
(**kwargs)[source]¶ High-resolution Infra-Red Sounder.
This class can read HIRS l1b as published in the NOAA CLASS archive.
Work in progress.
-
class
pyatmlab.datasets.tovs.
HIRS3
(**kwargs)[source]¶ High-resolution Infra-Red Sounder.
This class can read HIRS l1b as published in the NOAA CLASS archive.
Work in progress.
-
class
pyatmlab.datasets.tovs.
HIRS4
(**kwargs)[source]¶ High-resolution Infra-Red Sounder.
This class can read HIRS l1b as published in the NOAA CLASS archive.
Work in progress.
-
class
pyatmlab.datasets.tovs.
IASI
(**kwargs)[source]¶ Represents a dataset where measurements are spread over multiple files.
If filenames contain timestamps, this information is used to determine the time for a granule or measurement. If filenames do not contain timestamps, this information is obtained from the file contents.
Contains the following attributes:
basedir: Describes the directory under which all granules are located. Can be either a string or a pathlib.Path object. subdir:: Describes the directory within basedir where granules are located. May contain string formatting directives where particular fields are replaces, such as `year`, `month`, and `day`. For example: `subdir = '{year}/{month}'. Sorting cannot be more narrow than by day. re:: Regular expression that should match valid granule files within `basedir` / `subdir`. Should use symbolic group names to capture relevant information when possible, such as starting time, orbit number, etc. For time identification, relevant fields are contained in MultiFileDataset.date_info, where each field also exists in a version with "_end" appended. MultiFileDataset.refields contains all recognised fields. If any *_end fields are found, the ending time is equal to the beginning time with any *_end fields replaced. If no *_end fields are found, the `granule_duration` attribute is used to determine the ending time, or the file is read to get the ending time (hopefully the header is enough). granule_cache_file:: If set, use this file to cache information related to granules. This is used to cache granule times if those are not directly inferred from the filename. Otherwise, this is not used. The full path to this file shall be `basedir` / `granule_cache_file`. granule_duration:: If the filename contains starting times but no ending times, granule_duration is used to determine the ending time. This should be a datetime.timedelta object.
db module¶
Describe batches of data: atmospheric db, scattering db, etc.
This module contains a class with functionality to read atmospheric data from a variety of formats and write it to a variety of formats.
Mostly obtained from PyARTS.
-
class
pyatmlab.db.
AtmosphericDatabase
(**kwargs)[source]¶ Represents an atmospheric database
Apart from the default constructor, those constructors may be useful:
Attributes:
data
instrument
name
-
classmethod
from_evans07
(dbfile, instrument=None)[source]¶ Read from db in style for Evans study in 2006-2007
Parameters: - dbfile – Path to database-file
- instrument – Optional argument, pass instrument.
-
classmethod
from_evans12
(dbfile, instrument=None, profile=False, radar=False, maxlen=None, ext=False)[source]¶ Read from db in style for Evans study in 2012
Parameters: - dbfile – Path to database-file
- instrument – Optional argument, pass instrument.
- profile – Include profile information. Defaults to False.
- radar – Include radar information. Defaults to False.
- maxlen (int) – Optional maximum length of db to read.
- ext (bool) – Read extended version, with manual additions by Gerrit 2014-01-16 (i.e. also outputting non-retrieved quantities used in the RTM)
-
write_evans_obs
(fn, sel=None)[source]¶ Write as evans-obs file
Parameters: - fn – File to write to
- sel – Selection to write to file, None (default) to write all
-
write_mat
(fn, fields=None, sel=None)[source]¶ Write to MAT file.
Use
scipy.io.savemat()
to write the database to a MATLAB-style .mat file.Parameters: - fn – Filename to write to
- fields – List of fieldnames to write. Must be subset of
self.data.dtype.names
. Defaults toNone
, which means to write all fields. - sel – Indices to write. Defaults to
None
, which means all scenes.
-
class
pyatmlab.db.
CountingLookupTable
[source]¶ Provide counting only, effectively creating a histogram.
-
class
pyatmlab.db.
LargeLookupTable
[source]¶ Lookup table too large in memory, mapped to directory
Needs caching/flushing!
-
class
pyatmlab.db.
LookupTable
[source]¶ Use a lookup table to consider similar measurements
This table is used to represent a large set of measurements by a small set. It takes as input a particular measurement, and returns as output a canonical measurements. A measurement is n-dimensional, consisting of lat, lon, time of day, day of year, partial column, degrees of freedom.
A use case is when we have a large set of measurements, but only error estimates for a subset of those.
Implemented using a lookup table based on stats.bin_nd. Bins are based on training data. If newly presented data does not look like any pre-trained data, an error is raised.
Attributes:
axdata. Dictionary with keys corresponding to the axes to be considered in the lookup table; names should correspond to fields in data. Each value is itself a dictionary with keys:: nsteps: Number of steps in linspace bins db
- TODO:
- Need more intelligent binning when using multiple dimensions. Bins for variable n might depend on the value of the bins in n-1. In practice, this may be easiest to implement by binning the difference between variable n and n-1. Which raises the question if n+1 should be differenced to n or to n-1. Needs some thinking.
-
class
pyatmlab.db.
SmallLookupTable
[source]¶ Lookup table small enough to be in memory
-
classmethod
fromData
(data, axdata)[source]¶ Build lookup table from data
data
should be a structured ndarray with dtype fieldsaxdata
should be acollections.OrderedDict
where the keys refer to fields from data to use, and the values are dictionaries with the keys:- nsteps (mandatory)
- number of steps in binning data
- mode
- string that can be either “linear” (use linspace between extremes) or “optimal” (choose bins based on percentiles so 1-D binning would create equal content in each). Defaults to linear.
- range
- tuple with (min, max) of range within which to bin data. Defaults to extremes of data.
-
classmethod
-
pyatmlab.db.
now
()¶ Returns new datetime object representing current time local to tz.
- tz
- Timezone object.
If no tz is specified, uses local timezone.
geo module¶
Various small geo functions
graphics module¶
Interact with matplotlib and other plotters
-
pyatmlab.graphics.
now
()¶ Returns new datetime object representing current time local to tz.
- tz
- Timezone object.
If no tz is specified, uses local timezone.
-
pyatmlab.graphics.
plotdir
()[source]¶ Returns todays plotdir.
Configuration ‘plotdir’ must be set. Value is expanded with strftime.
-
pyatmlab.graphics.
print_or_show
(fig, show, outfile, in_plotdir=True, tikz=None, data=None)[source]¶ Either print or save figure, or both, depending on arguments.
Taking a figure, show and/or save figure in the default directory, obtained with :func:plotdir. Creates plot directory if needed.
Parameters: - fig (matplotlib.Figure object) – Figure to store.
- show (boolean) – Show figure or not
- outfile (string or list of strings) – File to write figure to, or list of files. If the string ends in a ‘.’, write to x.png and x.pdf.
- in_plotdir (boolean) – If true, write to default plot directory. If false, write to currect directory or use absolute path.
- tikz (boolean) – Try to write tikz code with matplotlib2tikz. Requires that the latter is installed.
- data (ndarray or list thereof) – Store associated data in .dat file (useful for pgfplots). May be a list of ndarrays, which results in multiple numbered datafiles.
instruments module¶
Classes to represent instruments in an abstract way.
Describe a channel by its centre frequency, width, shape, no. of sidebands, etc., and write out its SRF. The same for an entire instrument, which is essentially a collection of channels.
Those are theoretical, idealised instruments, not real instruments with real SRFs. For real SRFs, look in physics.SRF.
-
class
pyatmlab.instruments.
Channel
(freq, noise, width, sideband=0)[source]¶ Represents an idealised (rectangular) radiometer channel.
-
get_chanstr
(full=False)[source]¶ Get the shortest string representation of frequency in GHz
Parameters: full (bool) – Be verbose in the channel description. Defaults to False.
-
srf
(step=99000000.0)[source]¶ Returns SRF (x, y) with stepsize step
Parameters: step – Stepsize (resolution) [Hz]
-
write_srf
(to, quantity='frequency', factor: numbers.Number=1.0)[source]¶ Write SRF to file.
Parameters: - to – Output file. Can be an open file, a string containing a filename, or a directory. If it is a directory, the filename will be calculated intelligently.
- quantity – What unit the SRF is described in. This should be a string, and can be ‘frequency’ (the defailt)’, ‘wavelength’, and ‘wavenumber’.
- factor (float) – Multiplication factor for the quantity. Normally, everything is described in SI units. However, if you want to describe the SRF in cm^-1 instead of m^-1, you should pass factor=0.01.
-
-
class
pyatmlab.instruments.
Radiometer
(channels, name='(noname)')[source]¶ Represents an ideal radiometer.
Initialise with list of Channel-objects.
-
channel_string
(pre='', full=False)[source]¶ Get string with channel descriptions.
Channel descriptions are compact string specifications of channel frequencies in GHz. Those are concatenated together separated by whitespace.
Parameters: - pre (str) – String to prepend to each channel.
- full (bool) – Be verbose in the channel descriptions. Defaults to False.
-
io module¶
Read and write particular datasets.
This module contains a collection of functions to locate, read, and possibly write particular datasets, such as Chevallier data, NICAM data, and others.
-
pyatmlab.io.
collect_values
(fp, N, dtp)[source]¶ Collect N values from stream
Must be contained in exact number of lines. This will advance the stream forward by the number of lines found to contain N numeric values, and return an ndarray of type tp containing those.
Parameters: - fp (file) – Stream
- N (int) – Total no. expected values
- tp (dtype) – dtype for values
Returns: ndarray of type dtype with values found in file
-
pyatmlab.io.
datadir
()[source]¶ Returns todays datadir.
Configuration ‘datadir’ must be set. Value is expanded with strftime.
-
pyatmlab.io.
get_chevallier_arts_path
(var, tp='all')[source]¶ Get path to ArtsXML Chevallier data extract.
Require configuration variable cheval_arts set. See also :function get_chevallier_path:.
Parameters: var – What variable it is maximised on. Returns: A string with the path to the Chevallier data file.
-
pyatmlab.io.
get_chevallier_path
(var, tp)[source]¶ Get path to original Chevallier data.
Requires that in ~/.pyatmlabrc, the configuration variable cheval in the section [main] is set to a directory where the Chevallier data are contained.
Parameters: - var – What variable the Chevallier-data is maximised on.
- tp – Get atmosphere (‘atm’) or surface (‘sfc’)
Returns: A string with the path to the Chevallier data file.
-
pyatmlab.io.
plotdatadir
()[source]¶ Returns todays plotdatadir
Configuration ‘plotdatadir’ must be set. Value is expanded with strftime.
-
pyatmlab.io.
read_arts_srf
(f_backend, backend_channel_response)[source]¶ Read spectral response function from ArtsXML format
Would rather have more generic functionality for reading ArtsXML but I don’t have time to implement something I don’t need right now.
Parameters: - f_backend (str) – Path to backend (such as NOAA18_HIRS.f_backend.xml). Expect a single Vector.
- backend_channel_response (str) – Path to channel response (such as NOAA18_HIRS.backend_channel_ressponse.xml).
Returns (centres, response): ndarray with centres and list of tuples (frequency, weight) where frequency and weight are ndarrays of dtype float32.
-
pyatmlab.io.
read_chevallier
(tp)[source]¶ Read Chevallier data file.
So far, reads only the atmospheric file.
Parameters: tp – variable that Chevallier is maximised upon Returns: An nd-array with a complicated dtype
-
pyatmlab.io.
savetxt_3d
(fname, data, *args, **kwargs)[source]¶ Write 3D-array to file that pgfplots likes
-
pyatmlab.io.
write_data_to_files
(data, fn)[source]¶ Write data to one or more files.
Write single ndarray or dictionary of ndarrays to a set of files. Files are written inside
config.get_config('plotdatadir')
.data
is a dictionary with ndarrays. Each element is written to its own file. It can also be a dictionary of dictionaries, etc., of arbitrary depth.fn
should contain ``{}``s corresponding to the depth of the dictionary. Should not end in .dat as this is added by the function.Parameters: - data – Dictionary with ndarrays.
- fn – Filename, including ``{}``s corresponding to depth, excluding .dat extention.
-
pyatmlab.io.
write_matrix_as_latex_table
(x, y, M, outfile, xlabel=None, ylabel=None)[source]¶ Write Matrix as LaTeX table.
This is currently very inflexible, but may become more flexible in the future. It’s currently hardcoded for use with siunitx and booktabs.
Needs packages booktabs, siunitx, multirow
Parameters: - x (array) – Horizonal dimension
- y (array) – Vertical dimension
- M (2d-array) – Matrix to write
- outfile (str) – Output file
- xlabel (str) – Label corresponding x-data
- ylabel (str) – Label corresponding to y-data
math module¶
Various small mathematical functions
-
pyatmlab.math.
apply_W_A
(W, A)[source]¶ Regrid averaging kernel matrix using W
If interpolation matrix W is already calculated, apply to averaging kernel matrix here.
This follows the methodology outlined by Calisesi, Soebijanta and Van Oss (2005).
-
pyatmlab.math.
average_position_sphere
(*args: <function <lambda> at 0x7fafb5d11620>)[source]¶ Calculate the average position for a set of angles on a sphere
This is quite imprecise, errors can be dozens of km. For more advanced calculations, use proj4/pyproj.
Input can be either:
Parameters: - lat – Vector of latitudes
- lon – Vector of longitudes
Or:
Parameters: locs – Nx2 ndarray with lats in column 0, lons in column 1
-
pyatmlab.math.
convert_ak_ap2vmr
(AKx, aprf)[source]¶ Convert averaging kernel from SFIT4 units to vmr
Parameters: - AKx – Averaging kernel from SFIT4
- aprf – A-priori
Returns: Averaging kernel in VMR units
-
pyatmlab.math.
cum_integrate_with_height
(z, q, ignore_negative=False)[source]¶ Calculate cumulative integral with height
Parameters: - z (ndarray) – Height
- q (ndarray) – Quantity, dim 0 must be height.
- ignore_negative (bool) – Set negative values to 0
Returns: Cumulatively vertically integrated value
-
pyatmlab.math.
get_transformation_matrix
(f, n)[source]¶ Obtain standard matrix for the linear transformaton
For a given linear function taking a vector valued argument, obtain the standard matrix for the linear transformation.
See Lay (2003), Linear Algebra and its Transformations, 3rd edition, Theorem 10 (page 83).
Parameters: - f (callable) – Function for which to get the transformation matrix. This might be a function or a functools.partial object, for example. This function should take as input a vector of length n and return as output a vector of length m (m>0). Of course, this function should be linear.
- n (int) – Size of transformation matrix needed
Returns: (m x n) transformation matrix.
-
pyatmlab.math.
integrate_with_height
(z, q, ignore_negative=False)[source]¶ Calculate vertically integrated value
Parameters: - z (ndarray) – Height
- q (ndarray) – Quantity, dim 0 must be height.
- ignore_negative (bool) – Set negative values to 0
Returns: Vertically integrated value
-
pyatmlab.math.
layer2level
(z, q, ignore_negative=False)[source]¶ Converts layer to level. First dim. must be height.
Parameters: - z (ndarray) – Height
- q (ndarray) – Quantity, dim 0 must be height.
- ignore_negative (bool) – Set negative values to 0
Returns: Level-valued quantity.
-
pyatmlab.math.
linear_interpolation_matrix
(x_old, x_new)[source]¶ Get transformation matrix for linear interpolation.
This is denoted by W in Calisesi, Soebijanta and Van Oss (2005).
Does note extrapolate; values outside the range are equal to the outermost values.
Parameters: - x_old – Original 1-D grid
- x_new – New 1-D grid for interpolation
Returns ndarray W: Interpolation transformation matrix.
-
pyatmlab.math.
regrid_ak
(A, z_old, z_new, cut=False)[source]¶ Regrid averaging kernel matrix.
Actual regridding done in apply_W_A, following Calisesi, Soebijanta and Van Oss (2005).
Parameters: - A – Original averaging kernel
- z_old – Original z-grid
- z_new – New z-grid
- cut (bool) – Cut off, i.e. flag, when any z in the new grid is outside the old grid.
Returns: (New averaging kernel, W)
meta module¶
Functions related to pyatmlab itself, or for internal use
physics module¶
Various small physics functions
Mostly obtained from PyARTS
-
class
pyatmlab.physics.
SRF
(f, W)[source]¶ Respresents a spectral response function
-
blackbody_radiance
(T)[source]¶ Calculate integrated radiance for blackbody at temperature T
Parameters: T – Temperature [K]
-
channel_radiance2bt
(L)[source]¶ Convert channel radiance to brightness temperature
Using the lookup table, convert channel radiance to brightness temperature. Will construct lookup table on first call.
Parameters: L – Radiance [W m^-2 sr^-1]
-
integrate_radiances
(f, L)[source]¶ From a spectrum of radiances and a SRF, calculate channel radiance
The spectral response function may not be specified on the same grid as the spectrum of radiances. Therefore, this function interpolates the spectral response function onto the grid of the radiances. This is less bad than the reverse, because a spectral response function tends to be more smooth than a spectrum.
Approximations:
- Interpolation of spectral response function onto frequency grid on which radiances are specified.
Parameters: - f (ndarray) – Frequencies for spectral radiances [Hz]
- L (ndarray) – Spectral radiances [W m^-2 sr^-1 Hz^-1]. Can be in radiance units of various kinds. Make sure this is consistent with the spectral response function. Innermost dimension must correspond to frequencies.
Returns: Channel radiance [W m^-2 sr^-1]
-
make_lookup_table
()[source]¶ Construct lookup table radiance <-> BT
To convert a channel radiance to a brightness temperature, applying the (inverse) Planck function is not correct, because the Planck function applies to monochromatic radiances only. Instead, to convert from radiance (in W m^-2 sr^-1 Hz^-1) to brightness temperature, we use a lookup table. This lookup table is constructed by considering blackbodies at a range of temperatures, then calculating the channel radiance. This table can then be used to get a mapping from radiance to brightness temperature.
This method does not return anything, but fill self.lookup_table.
-
-
pyatmlab.physics.
estimate_effective_temperature
(f, W, f_c, T)[source]¶ Estimate effective temperature for SRF
For a monochromatic radiance, one can easily convert radiance to brightness temperature using the Planck function. For a polychromatic radiance such as a channel radiance, this is incorrect. Weinreb et al. (1981) propose a solution in calculating an effective temperature:
T_e = B^{-1}(int_{f_1}^{f_2} phi B df)
where $T_e$ is the effective temperature, $B$ is the Planck function, $f_1$ and $f_2$ are the lower and upper limit of the channel, $phi$ is the normalised channel radiance. This, used with the central wavenumber, is correct FIXME VERIFY. According to Weinreb et al. (1981) this is “from the theorem”... I should understand this!
Weinreb, M.P., Fleming, H.E., McMillin, L.M., Neuendorffer, A.C, Transmittances for the TIROS operational vertical sounder, 1981, NOAA Technical Report NESS 85.
Parameters: - f (ndarray) – Frequency [Hz] for spectral response function.
- W (ndarray) – Weight [1] for spectral response function. Should be normalised to be integrated to 1.
- f_c – Central frequency to be used with Planck function.
- T – Temperature [K]
Returns: Effective temperature [K].
-
pyatmlab.physics.
frequency2wavelength
(frequency)[source]¶ Converts frequency [Hz] to wave length [m]
Parameters: frequency – Frequency [Hz] Returns: Wave length [m]
-
pyatmlab.physics.
frequency2wavenumber
(frequency)[source]¶ Converts frequency [Hz] to wave number [m^-1]
Parameters: frequency – Frequency [Hz] Returns: Wave number [m^-1]
-
pyatmlab.physics.
lat2g0
(lat)[source]¶ Calculate surface gravitational acceleration for latitude
This function is stolen from atmlab: https://www.sat.ltu.se/trac/rt/browser/atmlab/trunk/geophysics/pt2z.m
From the original description:
Expression below taken from Wikipedia page “Gravity of Earth”, that is stated to be: International Gravity Formula 1967, the 1967 Geodetic Reference System Formula, Helmert’s equation or Clairault’s formula.
Parameters: lat – Latitude [degrees] Returns: gravitational acceleration [m/s]
-
pyatmlab.physics.
mixingratio2density
(mixingratio, p, T)[source]¶ Converts mixing ratio (e.g. kg/kg) to density (kg/m^3) for dry air.
Uses the ideal gas law and the effective molar mass of Earth air.
Parameters: - mixingratio – Mixing ratio [1]
- p – Pressure [Pa]
- T – Temperature [K]
Returns: Density [kg/m^3]
-
pyatmlab.physics.
mixingratio2iwv
(z, r, p, T)[source]¶ Calculate integrated water vapour [kg/m^2] from z, r
Parameters: - z – Height profile [m]
- r – mixing ratio profile [kg/kg]
- p – Pressure profile [Pa]
- T – Temperature profile [T]
Returns: Integrated water vapour [kg/m^2]
-
pyatmlab.physics.
mixingratio2rh
(w, p, T)[source]¶ For water on Earth, convert mixing-ratio to relative humidity
Parameters: - w – water vapour mixing ratio [1]
- p – pressure [Pa]
- T – temperature [K]
Returns: relative humidity [1]
-
pyatmlab.physics.
p2z_hydrostatic
(p: numpy.ndarray, T: numpy.ndarray, h2o, p0: (<class 'numpy.number'>, <class 'numbers.Number'>, <class 'numpy.ndarray'>), z0: (<class 'numpy.number'>, <class 'numbers.Number'>, <class 'numpy.ndarray'>), lat: (<class 'numpy.number'>, <class 'numbers.Number'>, <class 'numpy.ndarray'>)=45, z_acc: (<class 'numpy.number'>, <class 'numbers.Number'>, <class 'numpy.ndarray'>)=-1, ellps='WGS84', extend=False)[source]¶ Calculate hydrostatic elevation
Translated from https://www.sat.ltu.se/trac/rt/browser/atmlab/trunk/geophysics/pt2z.m
WARNING: seems to get siginificant errors. Testing with an ACE profile between 8.5 and 150 km, I get errors from 10 up to +100 metre between 10 and 50 km, increasing to +300 metre at 100 km, after which the bias changes sign, crosses 0 at 113 km and finally reaches -4000 metre at 150 km. This is not due to humidity. Atmlabs pt2z version differs only 30 metre from mine. In %, this error is below 0.3% up to 100 km, then changes sign and reaching -3% at 150 km. For many purposes this is good enough, though, and certainly better than p2z_oversimplified.
Parameters: - p (array) – Pressure [Pa]
- T (array) – Temperature [K]. Must match the size of p.
- h2o – Water vapour [vmr]. If negligible, set to 0. Must be either scalar, or match the size of p and T.
- p0 –
- z0 –
- lat – Latitude [degrees]. This has some effect on the vertical distribution of gravitational acceleration, leading to difference of some 500 metre at 150 km. Defaults to 45°.
- z_acc – Up to what precision to iteratively calculate the z-profile. If -1, run two iterations, which should be accurate, according to the comment below.
- ellps (str) – Ellipsoid to use. The function relies on pyproj.Geod, which is an interface to the proj library. For a full table of ellipsoids, run ‘proj -le’.
- extend (bool) – If p0, z0 outside of p, z range, extend artificially. WARNING: This will assume CONSTANT T, h2o!
Returns array z: Array of altitudes [m]. Same size as p and T.
-
pyatmlab.physics.
p2z_oversimplified
(p)[source]¶ Convert pressure to altitude with oversimplified assumptions.
Neglects the virtual temperature correction, assumes isothermal atmosphere with pressure dropping factor 10 for each 16 km. Use a better function...
Parameters: p – Pressure [Pa] Returns: Altitude [m]
-
pyatmlab.physics.
planck_f
(f, T)[source]¶ Planck law expressed in frequency
Parameters: - f – Frequency [Hz]
- T – Temperature [K]
-
pyatmlab.physics.
rh2iwv
(z, rh, p, T)[source]¶ Calculate integrated water vapour [kg/m^2] from z, rh
Parameters: - z – Height profile [m]
- rh – Relative humidity profile [1]
- p – Pressure profile [Pa]
- T – Temperature profile [T]
Returns: Integrated water vapour [kg/m^2]
-
pyatmlab.physics.
rh2mixingratio
(rh, p, T)[source]¶ Convert relative humidity to water vapour mixing ratio
Based on atmlabs h2o/thermodynomics/relhum_to_vmr.m.
Parameters: - rh – Relative humidity [1]
- p – Pressure [Pa]
- T – Temperature [K]
Returns: Water vapour mixing ratio [1]
-
pyatmlab.physics.
specific2iwv
(z, q)[source]¶ Calculate integrated water vapour [kg/m^2] from z, q
Parameters: - z – Height profile [m]
- q – specific humidity profile [kg/kg]
Returns: Integrated water vapour [kg/m^2]
-
pyatmlab.physics.
specific2mixingratio
(q)[source]¶ Convert specific humidity [kg/kg] to volume mixing ratio
-
pyatmlab.physics.
specrad_frequency_to_planck_bt
(L, f)[source]¶ Convert spectral radiance per frequency to brightness temperature
This function converts monochromatic spectral radiance per frequency to Planck brightness temperature. This is calculated by inverting the Planck function.
Note that this function is NOT correct to estimate polychromatic brightness temperatures such as channel brightness temperatures. For this, you need the spectral response function — see the SRF class.
Parameters: - L – Spectral radiance [W m^-2 sr^-1 Hz^-1]
- f – Corresponding frequency [Hz]
Returns: Planck brightness temperature [K].
-
pyatmlab.physics.
specrad_wavenumber2frequency
(specrad_wavenum)[source]¶ Convert spectral radiance from per wavenumber to per frequency
Parameters: specrad_wavenum – Spectral radiance per wavenumber [W·sr^{-1}·m^{-2}·{m^{-1}}^{-1}] Returns: Spectral radiance per frequency [W⋅sr−1⋅m−2⋅Hz−1]
-
pyatmlab.physics.
spectral_to_channel
(f_L, L_f, f_srf, w_srf)[source]¶ From a spectrum of radiances and a SRF, calculate channel radiance
The spectral response function may not be specified on the same grid as the spectrum of radiances. Therefore, this function interpolates the spectral response function onto the grid of the radiances. This is less bad than the reverse, because a spectral response function tends to be more smooth than a spectrum.
Approximations:
- Interpolation of spectral response function onto frequency grid on which radiances are specified.
FIXME BUG! The conversion is incorrect and should be done in terms of spectral radiances, not brightness temperatures. Consider discussion with Jon. Need to read up on this. FIXME BUG!
Parameters: - f_L (ndarray) – Frequencies for spectral radiances [Hz]
- L_f (ndarray) – Spectral radiances [various]. Can be in radiance units or brightness temperatures. Innermost dimension must correspond to frequencies.
- f_srf (ndarray) – Frequencys for spectral response function [Hz]
- w_srf (ndarray) – Weights for spectral response function []
Returns: Channel radxiance (same unit as L_f).
-
pyatmlab.physics.
vapour_P
(T)[source]¶ Calculate saturation vapour pressure.
Calculates the saturated vapour pressure (Pa) of water using the Hyland-Wexler eqns (ASHRAE Handbook).
(Originally in PyARTS)
Parameters: T – Temperature [K] Returns: Vapour pressure [Pa]
-
pyatmlab.physics.
vmr2nd
(vmr, T, p)[source]¶ Convert volume mixing ratio [] to number density
Parameters: - vmr – Volume mixing ratio or volume fraction. For example, taking methane density in ppmv, first multiply by constants.ppm, then pass here.
- T – Temperature [K]
- p – Pressure [Pa]
Returns: Number density in molecules per m^3
-
pyatmlab.physics.
wavelength2frequency
(wavelength)[source]¶ Converts wavelength (in meters) to frequency (in Hertz)
Parameters: wavelength – Wavelength [m] Returns: Frequency [Hz]
-
pyatmlab.physics.
wavelength2wavenumber
(wavelength)[source]¶ Converts wavelength (in m) to wavenumber (in m^-1)
Parameters: wavelength – Wavelength [m] Returns: Wavenumber [m^-1]
-
pyatmlab.physics.
wavenumber2frequency
(wavenumber)[source]¶ Converts wavenumber (in m^-1) to frequency (in Hz)
Parameters: wavenumber – Wave number [m^-1] Returns: Frequency [Hz]
-
pyatmlab.physics.
wavenumber2wavelength
(wavenumber)[source]¶ Converts wavenumber (in m^-1) to wavelength (in m)
Parameters: wavenumber – Wave number [m^-1] Returns: Wavelength [m]
-
pyatmlab.physics.
z2g
(r_geoid, g0, z)[source]¶ Calculate gravitational acceleration at elevation
Derived from atmlabs equivalent function https://www.sat.ltu.se/trac/rt/browser/atmlab/trunk/geophysics/pt2z.m
Parameters: - r – surface radius at point [m]
- g0 – surface gravitational acceleration at point [m/s^2]
- z – elevation [m]
Returns: gravitational acceleration at point [m/s^2]
stats module¶
Various small statistical functions
-
pyatmlab.stats.
bin
(x, y, bins)[source]¶ Bin/bucket y according to values of x.
Returns list of arrays, one element with values for bin. Digitising happens with numpy.digitize.
Parameters: - x (ndarray (1-D)) – Coordinate that y is binned along
- y (ndarray) – Data to be binned. First dimension must match length of x. All subsequent dimensions are left untouched.
- bins (nd-array (1-D)) – Bins according to which sort data.
Returns: List of arrays, one element per bin
-
pyatmlab.stats.
bin_nd
(binners, bins, data=None)[source]¶ Bin/bucket data in arbitrary number of dimensions
For example, one can bin geographical data according to lat/lon through:
>>> binned = bin_nd([lats, lons], [lat_bins, lon_bins])
The actually binned data are the indices for the arrays lats/lons, which hopefully corresponds to indices in your actual data.
Data that does not fit in any bin, is not binned anywhere.
Note: do NOT pass the 3rd argument, data. This is used purely for the implementation using recursion. Passing anything here explicitly is a recipe for disaster.
Parameters: - binners (list of 1-D ndarrays) – Axes that data is binned at. This is akin to the x-coordinate in :function bin:.
- bins (list of 1-D ndarrays) – Edges for the bins according to which bin data
Returns: n-D ndarray, type ‘object’
-
pyatmlab.stats.
bin_nd_sparse
(binners, bins)[source]¶ Similar to bin_nd, but for very sparse data
When the number of entries to be binned is much smaller than the number of bins, it is very inefficient to go through all the bins as bin_nd does. This function takes the same arguments but instead of returning an n-dimensional array, it will return an n*p array where n are the number of dimensions and p the number of datapoints, returning the coordinates that each point would be in.
-
pyatmlab.stats.
binsnD_to_2d
(bins, ax1, ax2)[source]¶ Collapse n-D bucketing array to 2-D, merging buckets for other dims.
Take a n-D bucketing array (n>=2), such as returned by bin_nD, and merge all buckets thus reducing it to 2D, retaining ax1 and ax2. For examble, binsnD_flatiter(bins, 1, 2) will return a bins-array of shape (bins.shape[1], bins.shape[2]), where each bucket [i, j] will contain all elements in bins[:, i, j, ...].
Parameters: - bins – n-Dimensional array containing buckets. As returned by bin_nD.
- ax1 – First axis to keep.
- ax2 – Second axis to keep.
Returns: 2-Dimensional array of shape (bins.shape[ax1], bins.shape[ax2]) with all other dimensions merged.
time module¶
Various time utilities
-
pyatmlab.time.
dt_to_doy_mlst
(dt, lon)[source]¶ From a datetime object, get day of year and MLST
From a datetime object, calculate the day of year and the mean local solar time.
Parameters: - dt (datetime.datetime) – Datetime object
- lon (float) – Longitude (needed for mean local solar time)
Returns: (doy, mlst) where doy is a float between 0 and 366, and mlst is a float between 0 and 24.
-
pyatmlab.time.
mean_local_solar_time
(utctime, lon)[source]¶ Calculate mean local solar time.
Calculates the mean local solar time for a specific longitude. This is not the true local solar time because it does not take into account the equation of time. It is purely based on the hour angle of the Sun. Do not use for astronomical calculations!
Parameters: - utctime (time) – Time in UTC. Should be a datetime.time object.
- lon (float) – Longitude in degrees. Can be either in [-180, 180] or in [0, 360].
Returns time: Time in mean local solar time.
tools module¶
Contains functionality that doesn’t fit elsewhere
-
class
pyatmlab.tools.
DocStringInheritor
[source]¶ A variation on http://groups.google.com/group/comp.lang.python/msg/26f7b4fcb4d66c95 by Paul McGuire
-
class
pyatmlab.tools.
NotTrueNorFalseType
[source]¶ Not true, nor false.
A singleton class whose instance can be used in place of True or False, to represent a value which has no true-value nor false-value, e.g. a boolean flag the value of which is unknown or undefined.
By Stack Overflow user shx2, http://stackoverflow.com/a/25330344/974555
-
pyatmlab.tools.
cat
(*args)[source]¶ Concatenate either ndarray or ma.MaskedArray
Arguments as for numpy.concatenate or numpy.ma.concatenate. First argument determines type.
-
pyatmlab.tools.
disk_lru_cache
(path)[source]¶ Like functools.lru_cache, but stored on disk
Returns a decorator.
Parameters: path (str) – File to use for caching. Returns function: Decorator
-
pyatmlab.tools.
rec_concatenate
(seqs, ax=0)[source]¶ Concatenate record arrays even if name order differs.
Takes the first record array and appends data from the rest, by name, even if the name order or the specific dtype differs.
-
pyatmlab.tools.
setmem
(obj, memory)[source]¶ Process marks set by mark_for_disk_cache on a fresh instance
Meant to be called from
__init__
assetmem(self, memory)
.
-
pyatmlab.tools.
validate
(func, locals)[source]¶ Validate function with arguments.
Inside an annotated function (see PEP-3107), do type checking on the arguments. An annotation may be either a type or a callable. Use like this:
def f(x: str, b: int): validate(f, locals()) ... # proceed
or use the validator annotation:
@validator def f(x: str, b: int): ... # proceed
-
pyatmlab.tools.
validator
(func)[source]¶ Decorator to automagically validate a function with arguments.
Uses functionality in ‘validate’, required types/values determined from decorations. Example:
@validator def f(x: numbers.Number, y: numbers.Number, mode: str): return x+y
Does not currently work for
*args
and**kwargs
style arguments.