Modtran utility¶
Overview¶
This module provides MODTRAN tape7 file reading. Future development may include a class to write tape5 files, but that is a distant target at present.
See the __main__ function for examples of use.
This package was partly developed to provide additional material in support of students and readers of the book Electro-Optical System Analysis and Design: A Radiometry Perspective, Cornelius J. Willers, ISBN 9780819495693, SPIE Monograph Volume PM236, SPIE Press, 2013. http://spie.org/x648.html?product_id=2021423&origin_id=x646
Module classes¶
- class extracttape6(tp6name)[source]¶
Bases:
objectClass to collate all functions reading tape6 together, automatically export to json and latex.
This file only reads tp6 files created by Modtran 5. Tested on MODTRAN 5.2.0.0
Module functions¶
- fixHeaders(instr)[source]¶
Modifies the column header string to be compatible with numpy column lookup.
- Parameters:
columns (| list) – column name.
- Returns:
fixed column name.
- Return type:
- list columns (string)
- Raises:
| No exception is raised. –
- fixHeadersList(headcol)[source]¶
Modifies the column headers to be compatible with numpy column lookup.
- Parameters:
columns (| list) – list of column names.
- Returns:
fixed list of column names.
- Return type:
- list columns ([string])
- Raises:
| No exception is raised. –
- loadtape7(filename, colspec=[])[source]¶
Read the Modtran tape7 file. This function was tested with Modtran5 files.
- Parameters:
filename (|) – name of the input ASCII flatfile.
colspec (|) – list of column names required in the output the spectral transmittance data.
- Returns:
an array with the selected columns. Col[0] is the wavenumber.
- Return type:
- np.array
- Raises:
| No exception is raised. –
This function reads in the tape7 file from MODerate spectral resolution atmospheric TRANsmission (MODTRAN) code, that is used to model the propagation of the electromagnetic radiation through the atmosphere. tape7 is a primary file that contains all the spectral results of the MODTRAN run. The header information in the tape7 file contains portions of the tape5 information that will be deleted. The header section in tape7 is followed by a list of spectral points with corresponding transmissions. Each column has a different component of the transmission or radiance. For more detail, see the modtran documentation.
The user selects the appropriate columns by listing the column names, as listed below.
The format of the tape7 file changes for different IEMSCT values. For the most part the differences are hidden in the details. The various column headers used in the tape7 file are as follows:
IEMSCT = 0 has two column header lines. Different versions of modtran has different numbers of columns. In order to select the column, you must concatenate the two column headers with an underscore in between. All columns are available with the following column names: [‘FREQ_CM-1’, ‘COMBIN_TRANS’, ‘H2O_TRANS’, ‘UMIX_TRANS’, ‘O3_TRANS’, ‘TRACE_TRANS’, ‘N2_CONT’, ‘H2O_CONT’, ‘MOLEC_SCAT’, ‘AER+CLD_TRANS’, ‘HNO3_TRANS’, ‘AER+CLD_abTRNS’, ‘-LOG_COMBIN’, ‘CO2_TRANS’, ‘CO_TRANS’, ‘CH4_TRANS’, ‘N2O_TRANS’, ‘O2_TRANS’, ‘NH3_TRANS’, ‘NO_TRANS’, ‘NO2_TRANS’, ‘SO2_TRANS’, ‘CLOUD_TRANS’, ‘CFC11_TRANS’, ‘CFC12_TRANS’, ‘CFC13_TRANS’, ‘CFC14_TRANS’, ‘CFC22_TRANS’, ‘CFC113_TRANS’, ‘CFC114_TRANS’, ‘CFC115_TRANS’, ‘CLONO2_TRANS’, ‘HNO4_TRANS’, ‘CHCL2F_TRANS’, ‘CCL4_TRANS’, ‘N2O5_TRANS’,’H2-H2_TRANS’,’H2-HE_TRANS’,’H2-CH4_TRANS’, ‘CH4-CH4_TRANS’]
IEMSCT = 1 has single line column headers. A number of columns have headers, but with no column numeric data. In the following list the columns with header names ** are empty and hence not available: [‘FREQ’, ‘TOT_TRANS’, ‘PTH_THRML’, ‘THRML_SCT’, ‘SURF_EMIS’, SOL_SCAT, SING_SCAT, ‘GRND_RFLT’, DRCT_RFLT, ‘TOTAL_RAD’, REF_SOL, SOL@OBS, ‘DEPTH’, ‘DIR_EM’, TOA_SUN, ‘BBODY_T[K]’]. Hence, these columns do not have valid data: [‘SOL_SCAT’, ‘SING_SCAT’, ‘DRCT_RFLT’, ‘REF_SOL’, ‘SOL@OBS’, ‘TOA_SUN’]
IEMSCT = 2 has single line column headers. All the columns are available: [‘FREQ’, ‘TOT_TRANS’, ‘PTH_THRML’, ‘THRML_SCT’, ‘SURF_EMIS’, ‘SOL_SCAT’, ‘SING_SCAT’, ‘GRND_RFLT’, ‘DRCT_RFLT’, ‘TOTAL_RAD’, ‘REF_SOL’, ‘SOL@OBS’, ‘DEPTH’, ‘DIR_EM’, ‘TOA_SUN’, ‘BBODY_T[K]’]
IEMSCT = 3 has single line column headers. One of these seems to be two words, which, in this code must be concatenated with an underscore. There is also additional column (assumed to be depth in this code). The columns available are [‘FREQ’, ‘TRANS’, ‘SOL_TR’, ‘SOLAR’, ‘DEPTH’]
The tape7.scn file has missing columns, so this function does not work for tape7.scn files. If you need a tape7.scn file with all the columns populated you would have to use the regular tape7 file and convolve this to lower resolution.
UPDATE: Different versions of Modtran have different columns present in the tape7 file, also not all the columns are necessarily filled, some have headings but no data. To further complicate matters, the headings are not always separated by spaces, sometime (not often) heading text runs into each other with no separator.
It seems that the column headings are right aligned with the column data itself, so if we locate the right most position of the column data, we can locate the headings with valid data - even in cases with connected headings.
The algorithm used is as follows:
step down to a data line beyond the heading lines (i.e., the data)
locate the position of the last character of every discrete colummn
From the last column of the previous col, find start of next col
move up into the header again and isolate the header column text for each column
In sptite of all the attempts to isolate special cases, reading of tape7 files remain a challange and may fail in newer versions of Modtran, until fixed.
- variationTape5File(scenario, variation, tape5, varFunc, fmtstr, srcstr, outfile='tape5')[source]¶
Read tape5 in specified folder, modify one or more variable, write out new tape5 in new folder
- One tape5 is written with as many changes as are present in the four lists:
<variation, varFunc,fmtstr, srcstr> with corresponding values.
If only one value is to be changed, the four values may be scalar (not lists). If the values are scalar, lists are formed before processing.
Replace a search string <srcstr> (must be unique in the file) with the value of a variation <variation> formatted by <fmtstr> as required by the tape5 format. The variation input value <variation> is processed by the function <varFunc>. All occurrences of the search string <srcstr> are blindly replaced, and must occur only once in the file and be of the same width as the field to be replaced.
The variation folder name is constructed by joining with ‘-’ the Python str() representation of the variables.
The variation variable must be of the form that can be used to create sub-folders in the master scenario folder. The variation can be processed (scaled, etc.) by <varFunc> before writing out to file. For example, variation can be an integer in metre, then scaled to kilometer. If varFunc is None, the value is used as is.
The variation tape5 is written to a new sub folder with the name variation, constructed in the scenario folder. The template tape5 file must be in the scenario root file.
dir structure:
dir root file domodtran.py dir scenario 1 dir variation 1 dir variation 2 dir variation 3 file tape5 template for scenario 1 dir scenario 2 dir variation 1 dir variation 2 dir variation 3 file tape5 template for scenario 2
The scenario directories must exist, but the variation scenarios are created.
- Parameters:
scenario (|) – path to folder with the scenario tape5
variation (|) – number that defines the variation, will be modified by varFunc
tape5 (|) – tape5 filename, must be in the scenario folder
varFunc (|) – Function to process the variation number before writing, can be None
fmtstr (|) – print format string to be used when writing to tape5
srcstr (|) – the string in the tape5 file to be replaced
outfile (|) – the name to be used when writing the modified tape5
- Returns:
- side effect creates new folders and files.
- Raises:
| No exception is raised. –
- runModtranAndCopy(root, research, pathToModtranBin, execname, clean=False)[source]¶
Look for input files in directories, run modtran and copy results to dir.
Finds all files below the root directory that matches the regex pattern in research. Then runs modtran on these files and write the tape5/6/7/8 back to where the input file was.
Each input file must be in a separate directory, because the results are all written to files with the names ‘tape5’, etc.
- Parameters:
root (|) – path to root dir containing the dirs with modtran input files.
research (|) – regex to use when searching for input files (
'.*.ltn'or'tape5')pathToModtranBin (|) – path to modtran executable directory.
execname (|) – modtran executable filename.
- Returns:
- List of all the files processed.
- Raises:
| No exception is raised. –
- runModtranModrootIn(root, pathToModtranBin, execname, filerootname='atmo')[source]¶
Look for input files in directories, run modtran and copy results to dir.
Finds all
/*.tp5files below the root directory that matches the regex pattern in research. Then runs modtran on these files using modroot.inEach input file must be in a separate directory, because the results are all written to files with the names ‘tape5’, etc.
- Parameters:
root (|) – path to root dir containing the dirs with modtran input files.
pathToModtranBin (|) – path to modtran executable directory.
execname (|) – modtran executable filename.
filerootname (|) – root filename, no extension
- Returns:
- List of all the files processed.
- Raises:
| No exception is raised. –