obpg-orm 1.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. obpg_orm-1.1.5.data/data/data/aph_bricaud_1998.txt +163 -0
  2. obpg_orm-1.1.5.data/data/data/f.dat +491 -0
  3. obpg_orm-1.1.5.data/data/data/f0_tsis_1nm_v2.nc +0 -0
  4. obpg_orm-1.1.5.data/data/data/fp.dat +491 -0
  5. obpg_orm-1.1.5.data/data/data/mm01_kbio.nc +0 -0
  6. obpg_orm-1.1.5.data/data/data/mm01_kbio.txt +72 -0
  7. obpg_orm-1.1.5.data/data/data/morel_fq.nc +0 -0
  8. obpg_orm-1.1.5.data/data/data/morel_fq_appb.dat +34 -0
  9. obpg_orm-1.1.5.data/data/data/morel_fq_appb.nc +0 -0
  10. obpg_orm-1.1.5.data/data/data/morel_fq_hyperspectral.nc +0 -0
  11. obpg_orm-1.1.5.data/data/data/morel_mud_f_fp.nc +0 -0
  12. obpg_orm-1.1.5.data/data/data/mud.dat +491 -0
  13. obpg_orm-1.1.5.data/data/data/thuillier.dat +730 -0
  14. obpg_orm-1.1.5.data/data/data/thuillier2003_f0.nc +0 -0
  15. obpg_orm-1.1.5.data/data/data/water_spectra.nc +0 -0
  16. obpg_orm-1.1.5.data/data/docs/get_brdf.md +49 -0
  17. obpg_orm-1.1.5.data/data/docs/get_f0.md +31 -0
  18. obpg_orm-1.1.5.data/data/docs/morel_read.md +34 -0
  19. obpg_orm-1.1.5.data/data/docs/utils.md +30 -0
  20. obpg_orm-1.1.5.dist-info/METADATA +105 -0
  21. obpg_orm-1.1.5.dist-info/RECORD +49 -0
  22. obpg_orm-1.1.5.dist-info/WHEEL +5 -0
  23. obpg_orm-1.1.5.dist-info/entry_points.txt +5 -0
  24. obpg_orm-1.1.5.dist-info/top_level.txt +1 -0
  25. orm/__init__.py +0 -0
  26. orm/data/aph_bricaud_1998.txt +163 -0
  27. orm/data/f.dat +491 -0
  28. orm/data/f0_tsis_1nm_v2.nc +0 -0
  29. orm/data/fp.dat +491 -0
  30. orm/data/mm01_kbio.nc +0 -0
  31. orm/data/mm01_kbio.txt +72 -0
  32. orm/data/morel_fq.nc +0 -0
  33. orm/data/morel_fq_appb.dat +34 -0
  34. orm/data/morel_fq_appb.nc +0 -0
  35. orm/data/morel_fq_hyperspectral.nc +0 -0
  36. orm/data/morel_mud_f_fp.nc +0 -0
  37. orm/data/mud.dat +491 -0
  38. orm/data/thuillier.dat +730 -0
  39. orm/data/thuillier2003_f0.nc +0 -0
  40. orm/data/water_spectra.nc +0 -0
  41. orm/get_brdf.py +185 -0
  42. orm/get_f0.py +150 -0
  43. orm/kbio-netCDF.py +41 -0
  44. orm/model.py +419 -0
  45. orm/morel_fq_appb-netCDF.py +78 -0
  46. orm/morel_read.py +143 -0
  47. orm/mud_f_fp-netCDF.py +77 -0
  48. orm/orm.py +181 -0
  49. orm/utils.py +217 -0
orm/mud_f_fp-netCDF.py ADDED
@@ -0,0 +1,77 @@
1
+ """Script that loads the Morel f, fp, and mud tables and saves as a NetCDF."""
2
+ import numpy as np
3
+ from netCDF4 import Dataset
4
+ from datetime import datetime
5
+ import time
6
+ import pandas as pd
7
+
8
+ mud = pd.read_csv('mud.dat', sep='\t', header=0)
9
+ fdat = pd.read_csv('f.dat', sep='\t', header=0)
10
+ fpdat = pd.read_csv('fp.dat', sep='\t', header=0)
11
+
12
+ mud = mud.drop(mud[mud.solz < 0].index)
13
+ fdat = fdat.drop(fdat[fdat.solz < 0].index)
14
+ fpdat = fpdat.drop(fpdat[fpdat.solz < 0].index)
15
+
16
+ grdwvl = mud.wl.unique()
17
+ grdchl = np.array([0.03, 0.1, 0.3, 1, 3, 10])
18
+ grdsolz = mud.solz.unique()
19
+
20
+ mudval = np.zeros((len(grdsolz), len(grdwvl), len(grdchl)))
21
+ fval = np.zeros((len(grdsolz), len(grdwvl), len(grdchl)))
22
+ fpval = np.zeros((len(grdsolz), len(grdwvl), len(grdchl)))
23
+
24
+ for s in grdsolz:
25
+ x = np.where(grdsolz == s)
26
+ for w in grdwvl:
27
+ y = np.where(grdwvl == w)
28
+
29
+ mudval[x, y, :] = mud[(mud.solz == s) & (mud.wl == w)].iloc[0, 2:8]
30
+ fval[x, y, :] = fdat[(fdat.solz == s) & (fdat.wl == w)].iloc[0, 2:8]
31
+ fpval[x, y, :] = fpdat[(fpdat.solz == s) &
32
+ (fpdat.wl == w)].iloc[0, 2:8]
33
+
34
+ # Create the netCDF file and add dimensions
35
+ dataset = Dataset('morel_mud_f_fp.nc', 'w', format='NETCDF4')
36
+ dataset.createDimension('nsolz', len(grdsolz))
37
+ dataset.createDimension('nwavelengths', len(grdwvl))
38
+ dataset.createDimension('nchl', len(grdchl))
39
+
40
+ # Set global attributes
41
+ dataset.title = "Morel f, fp and mud tables"
42
+ dataset.date_created = (datetime.fromtimestamp(
43
+ time.time()).strftime('%Y-%m-%dT%H:%M:%SZ')).encode('ascii')
44
+ dataset.creator_name = "Sean Bailey, NASA/GSFC/OBPG"
45
+ dataset.creator_email = "data@oceancolor.gsfc.nasa.gov"
46
+ dataset.creator_url = "https://oceancolor.gsfc.nasa.gov"
47
+ dataset.product_name = "morel_mud_f_fp.nc'"
48
+ # dataset.source = 'generate_gas_tables.py'
49
+
50
+ # Create the table data sets
51
+ mudSDS = dataset.createVariable(
52
+ 'mud', np.float32, ('nsolz', 'nwavelengths', 'nchl'))
53
+ fSDS = dataset.createVariable(
54
+ 'f', np.float32, ('nsolz', 'nwavelengths', 'nchl'))
55
+ fpSDS = dataset.createVariable(
56
+ 'fp', np.float32, ('nsolz', 'nwavelengths', 'nchl'))
57
+ solz = dataset.createVariable('solz', np.float32, ('nsolz',))
58
+ wavelength = dataset.createVariable(
59
+ 'wavelength', np.float32, ('nwavelengths',))
60
+ chlorophyll = dataset.createVariable('chlorophyll', np.float32, ('nchl',))
61
+
62
+ # Write the data
63
+ mudSDS[:] = mudval
64
+ fSDS[:] = fval
65
+ fpSDS[:] = fpval
66
+ solz[:] = grdsolz
67
+ wavelength[:] = grdwvl
68
+ chlorophyll[:] = grdchl
69
+
70
+ # Add some variable attributes
71
+ wavelength.units = 'nm'
72
+ chlorophyll.units = 'mg/m3'
73
+ solz.units = 'degrees'
74
+ mudSDS.description = 'mean cosine for downward flux'
75
+
76
+ # Close the file
77
+ dataset.close()
orm/orm.py ADDED
@@ -0,0 +1,181 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Ocean Reflectance Model Script.
4
+
5
+ Generates hyperspectral (1nm) or sensor-specific Rrs (or Lwn) spectra using the
6
+ ocean reflectance model described in:
7
+ P. Jeremy Werdell, Sean W. Bailey, Bryan A. Franz, André Morel, and
8
+ Charles R. McClain, "On-orbit vicarious calibration of ocean color sensors
9
+ using an ocean surface reflectance model" Appl. Opt. 46, 5649-5666 (2007),
10
+ https://doi.org/10.1364/AO.46.005649
11
+ """
12
+
13
+ import argparse
14
+ import numpy as np
15
+ import sys
16
+
17
+ from orm.model import morel_rrs, gordon_rrs
18
+ from orm.utils import convolve_rsr
19
+
20
+ __version__ = '1.1.4'
21
+ verbose = False
22
+
23
+ # Global variables
24
+ wl = np.linspace(300, 900., 601)
25
+ interp_func_q = None
26
+ bricdat = None
27
+
28
+ rsr_wvl = None
29
+ rsr = None
30
+ rsr_bands = None
31
+ rsr_sensor = None
32
+
33
+ verbose = False
34
+
35
+
36
+ def main():
37
+ """Primary driver for stand-alone version."""
38
+ global verbose
39
+ global wl
40
+ global sensors
41
+
42
+ np.set_printoptions(precision=6)
43
+
44
+ parser = argparse.ArgumentParser(description=(
45
+ 'Generates hyperspectral (1nm) or sensor-specific Rrs (or Lwn) '
46
+ 'spectra using the ocean reflectance model described in: P. Jeremy '
47
+ 'Werdell, Sean W. Bailey, Bryan A. Franz, André Morel, and Charles R. '
48
+ 'McClain, "On-orbit vicarious calibration of ocean color sensors '
49
+ 'using an ocean surface reflectance model," Appl. Opt. 46, 5649-5666 '
50
+ '(2007), https://doi.org/10.1364/AO.46.005649'))
51
+
52
+ parser.add_argument('--version', action='version',
53
+ version='%(prog)s ' + __version__)
54
+ parser.add_argument('--sensor', type=str,
55
+ choices=['modisa',
56
+ 'modist',
57
+ 'viirsn',
58
+ 'viirsj1',
59
+ 'viirsj2',
60
+ 'seawifs',
61
+ 'meris',
62
+ 'olcis3a',
63
+ 'olcis3b',
64
+ 'octs',
65
+ 'oci',
66
+ 'czcs',
67
+ 'hawkeye',
68
+ 'hico',
69
+ ],
70
+ default=None, help='Sensor to mimic')
71
+ parser.add_argument('--output_file', type=str,
72
+ help='output netCDF LUT filename; default is STDOUT')
73
+ parser.add_argument('chl', nargs='+', type=float,
74
+ default=0.05, help='Chlorophyll concentration')
75
+ parser.add_argument('--adg', nargs='?', type=float, default=0.005,
76
+ help=('Detrital and Gelbstoff absorption coefficient '
77
+ 'at 443nm'))
78
+ parser.add_argument('--aph', nargs='?', type=float, default=0.005,
79
+ help='Phytoplankton absorption coefficient at 443nm')
80
+ parser.add_argument('--bbp', nargs='?', type=float, default=0.0015,
81
+ help='Particulate backscattering coefficient at 443nm')
82
+ parser.add_argument('--bbp_s', nargs='?', type=float,
83
+ default=2.0, help='Particulate backscattering slope')
84
+ parser.add_argument('--solz', nargs='+', type=float, default=30.,
85
+ help=('Solar zenith angle. If provided, it should '
86
+ 'either be a single value or an array matching '
87
+ 'the size of the input chlorophyll array. The '
88
+ 'default is 30 degrees'))
89
+ parser.add_argument('--wvl', type=float, nargs='+',
90
+ default=None, help='Wavelength(s) to generate')
91
+ parser.add_argument('--bandpass', type=int, default=11,
92
+ help='Bandpass width of wavelength(s) to generate')
93
+ parser.add_argument('--lwn', action='store_true',
94
+ help="output normalized Lw instead of Rrs")
95
+ parser.add_argument('--disable_iter_limit', action='store_false',
96
+ help="allow MM01 iteration to run to convergence")
97
+ parser.add_argument('--gordon', '-g', action='store_true',
98
+ help=('Use the Gordon quadratic formulation with '
99
+ 'input IOPs'))
100
+ parser.add_argument('--tsis', '-t', action='store_true', help='use TSIS extraterrestrial irradiance (default: Thuillier)')
101
+ parser.add_argument('--verbose', '-v', action='store_true')
102
+
103
+ args = parser.parse_args()
104
+
105
+ verbose = args.verbose
106
+ if args.sensor and args.wvl:
107
+ print("Can't do both sensor and an input wavelength set")
108
+ sys.exit(1)
109
+
110
+ wvl = wl
111
+ if args.wvl:
112
+ wvl = args.wvl
113
+ if verbose:
114
+ print("Generating %dnm bandpass spectrum for input wavelengths" %
115
+ args.bandpass)
116
+
117
+ if verbose and args.output_file:
118
+ print("Generating Rrs spectrum %s ..." % args.output_file)
119
+
120
+ otype = 'rrs'
121
+ units = '1/sr'
122
+ if args.lwn:
123
+ otype = 'nLw'
124
+ units = 'W/m^2/nm'
125
+
126
+ solz = np.atleast_1d(args.solz)
127
+ spectrum = None
128
+ if args.gordon:
129
+ spectrum = gordon_rrs(args.adg, args.aph, args.bbp, args.bbp_s,
130
+ args.chl, bandpass=args.bandpass, lwn=args.lwn, tsis=args.tsis)
131
+
132
+ else:
133
+ spectrum = morel_rrs(args.chl, solz=solz, wvl=args.wvl,
134
+ bandpass=args.bandpass, lwn=args.lwn,
135
+ mm01iter=args.disable_iter_limit, tsis=args.tsis)
136
+
137
+ # Convolve with sensor RSR, if desired...
138
+ if args.sensor:
139
+ if verbose:
140
+ print(("Generating spectrum based on the spectral response "
141
+ "functions for %s") % args.sensor)
142
+ (wvl, spectrum) = convolve_rsr(args.sensor, spectrum,verbose=verbose)
143
+
144
+ outarray = np.column_stack((np.asarray(wvl), np.asarray(spectrum)))
145
+
146
+ if args.output_file:
147
+ if np.asanyarray(args.chl).size > 1:
148
+ chlstr = ['{:f}'.format(x) for x in args.chl]
149
+ else:
150
+ chlstr = '{:f}'.format(args.chl)
151
+ if np.asarray(args.solz).size > 1:
152
+ solzstr = ['{:f}'.format(x) for x in args.solz]
153
+ else:
154
+ solzstr = '{:f}'.format(args.solz)
155
+
156
+ ofile = open(args.output_file, 'w')
157
+ ofile.write("/begin_header\n")
158
+ ofile.write("! Output of Ocean Reflectance Model\n")
159
+ ofile.write("! chlorophyll: %s\n" % chlstr)
160
+ ofile.write("! solar zenith angle: %s\n" % solzstr)
161
+ if args.bandpass:
162
+ ofile.write("! band width: %d\n" % args.bandpass)
163
+ if args.sensor:
164
+ ofile.write(("! convolved with the spectral response function "
165
+ "for %s\n") % sensors[args.sensor]['name'])
166
+ ofile.write("/missing=-999\n")
167
+ ofile.write("/delimiter=space\n")
168
+ ofile.write("/fields=wavelength,%s\n" % otype)
169
+ ofile.write("/units=nm,%s\n" % units)
170
+ ofile.write('\n'.join([' '.join(['{:.6f}'.format(value)
171
+ for value in row])
172
+ for row in outarray]))
173
+ ofile.close()
174
+ else:
175
+ print('\n'.join([' '.join(['{:.6f}'.format(value)
176
+ for value in row]) for row in outarray]))
177
+
178
+
179
+ # The following allows the file to be imported without immediately executing.
180
+ # if __name__ == '__main__':
181
+ # sys.exit(main())
orm/utils.py ADDED
@@ -0,0 +1,217 @@
1
+ import collections
2
+ from netCDF4 import Dataset
3
+ import numpy as np
4
+ import os
5
+ from pathlib import Path
6
+ import requests
7
+ from scipy.interpolate import interp1d
8
+ import sys
9
+
10
+ import orm.get_f0 as get_f0
11
+
12
+ rsr_wvl = None
13
+ rsr = None
14
+ rsr_bands = None
15
+ rsr_sensor = None
16
+
17
+ sensors = collections.defaultdict(dict)
18
+
19
+ sensors['modisa']['name'] = "MODIS-Aqua"
20
+ sensors['modisa']['rsr'] = "aqua_modis_RSR.nc"
21
+ sensors['modist']['name'] = "MODIS-Terra"
22
+ sensors['modist']['rsr'] = "terra_modis_RSR.nc"
23
+ sensors['seawifs']['name'] = "SeaWiFS"
24
+ sensors['seawifs']['rsr'] = "orbview-2_seawifs_RSR.nc"
25
+ sensors['meris']['name'] = "MERIS"
26
+ sensors['meris']['rsr'] = "envisat_meris_RSR.nc"
27
+ sensors['olcis3a']['name'] = "OLCI-S3A"
28
+ sensors['olcis3a']['rsr'] = "sentinel-3a_olci_RSR.nc"
29
+ sensors['olcis3b']['name'] = "OLCI-S3B"
30
+ sensors['olcis3b']['rsr'] = "sentinel-3b_olci_RSR.nc"
31
+ sensors['octs']['name'] = "OCTS"
32
+ sensors['octs']['rsr'] = "adeos_octs_RSR.nc"
33
+ sensors['oci']['name'] = "OCI"
34
+ sensors['oci']['rsr'] = "PACE_OCI_RSRs.nc"
35
+ sensors['czcs']['name'] = "CZCS"
36
+ sensors['czcs']['rsr'] = "nimbus-7_czcs_RSR.nc"
37
+ sensors['viirsn']['name'] = "VIIRS-SNPP"
38
+ sensors['viirsn']['rsr'] = "suomi-npp_viirs_RSR.nc"
39
+ sensors['viirsj1']['name'] = "VIIRS-JPSS1"
40
+ sensors['viirsj1']['rsr'] = "jpss-1_viirs_RSR.nc"
41
+ sensors['viirsj2']['name'] = "VIIRS-JPSS1"
42
+ sensors['viirsj2']['rsr'] = "jpss-2_viirs_RSR.nc"
43
+ sensors['hawkeye']['name'] = "Hawkeye"
44
+ sensors['hawkeye']['rsr'] = "seahawk1_hawkeye_RSR.nc"
45
+ sensors['hico']['name'] = "HICO"
46
+ sensors['hico']['rsr'] = "iss_hico_RSR.nc"
47
+ sensors['glimr']['name'] = "GLIMR"
48
+ sensors['glimr']['rsr'] = "glimr_RSR.nc"
49
+
50
+ # aviris_RSR.nc
51
+ # coms_goci_RSR.nc
52
+ # gcom_c_sgli_RSR.nc
53
+ # irs-p4_ocm_RSR.nc
54
+ # kompsat_osmi_RSR.nc
55
+ # landsat-8_oli_RSR.nc
56
+ # oceansat-2_ocm-2_RSR.nc
57
+ # sentinel-2a_msi_RSR.nc
58
+ # sentinel-2b_msi_RSR.nc
59
+ # terra_misr_RSR.nc
60
+
61
+ def get_wavelength_array():
62
+ return np.linspace(300, 900., 601)
63
+
64
+ def get_data_path(subdir=None,envroot=None):
65
+ """Get path to OCDATAROOT and subdirectories."""
66
+ scriptpath = Path(os.path.dirname(__file__)) / 'data'
67
+ if envroot:
68
+ if os.getenv(envroot):
69
+ scriptpath = Path(os.getenv(envroot))
70
+ else:
71
+ sys.exit("${} needs to be defined".format(envroot))
72
+
73
+ if subdir:
74
+ scriptpath = scriptpath / subdir
75
+
76
+ if not Path.exists(scriptpath):
77
+ err_msg = "Cannot access %s" % scriptpath
78
+ sys.exit("Cannot access {scriptpath}".format(scriptpath=str(scriptpath)))
79
+
80
+
81
+ return scriptpath
82
+
83
+ def download_rsrfile(remotefile,localfile):
84
+ with requests.get(remotefile, stream=True) as r:
85
+ r.raise_for_status()
86
+ with open(localfile, 'wb') as f:
87
+ for chunk in r.iter_content(chunk_size=8192):
88
+ f.write(chunk)
89
+
90
+ def read_sensor_RSR (sensor, verbose=False):
91
+ """
92
+ Read a sensor spectral response (RSR) from the OCW through https.
93
+
94
+ Files can be found at oceancolor.gsfc.nasa.gov/resources/docs/rsr_tables/
95
+ Input:
96
+ Sensor name (string)
97
+ Output:
98
+ normalized Rsr (wavelength, sensor band)
99
+ wavelengths of spectral response function
100
+ sensor band labels
101
+ """
102
+ rsrfile = None
103
+ try:
104
+ rsrfile = sensors[sensor]['rsr']
105
+ except:
106
+ print("Unkown sensor")
107
+ sys.exit()
108
+
109
+ webpath = 'https://oceancolor.gsfc.nasa.gov/images/rsr/' + rsrfile
110
+ datapath = get_data_path() / rsrfile
111
+
112
+ if not datapath.exists():
113
+ if verbose:
114
+ print("Local RSR file not found, retrieving: {}".format(sensors[sensor]['rsr']))
115
+ try:
116
+ download_rsrfile(webpath,datapath)
117
+ except:
118
+ print("Unable to retrieve RSR file from:\n\t{}".format(webpath))
119
+ sys.exit()
120
+
121
+ rsr_file = Dataset(datapath,'r')
122
+ Rsr = rsr_file['RSR'][:]
123
+ wavelength = rsr_file['wavelength'][:]
124
+ bands = rsr_file['bands'][:]
125
+ rsr_file.close()
126
+
127
+ return wavelength, Rsr.T, bands
128
+
129
+ def convolve_rsr(sensor, spectrum, spectrum_wvl=None, verbose=False):
130
+ """
131
+ Convolve Rrs with a spectral response function.
132
+ input spectrum assumed to be 1nm resoltion from 300 to 900nm unless
133
+ spectrum_wvl is provided
134
+ """
135
+ global rsr_wvl
136
+ global rsr
137
+ global rsr_bands
138
+ global rsr_sensor
139
+ global sensors
140
+ wl = get_wavelength_array()
141
+ # if provided spectrum is not 601 elements between 300 and 900nm
142
+ if spectrum_wvl is not None:
143
+ wl = spectrum_wvl
144
+ # spline the spectrum to 1nm resolution, if necesssary
145
+ if (np.amax(wl) - np.amin(wl)) / len(wl) != 1:
146
+ spectrum_interp = interp1d(wl, spectrum, kind='cubic', axis=0)
147
+ wl = np.linspace(np.amin(wl), np.amax(wl) , len(wl)+1)
148
+ spectrum = spectrum_interp(wl)
149
+
150
+ if rsr is None or sensor != rsr_sensor:
151
+ print("loading spectral response function for {}".format(
152
+ sensors[sensor]['name']))
153
+ rsr_sensor = sensor
154
+ rsr_wvl, rsr, rsr_bands = read_sensor_RSR(rsr_sensor,verbose=verbose)
155
+
156
+ # Get bands for the sensor and limit to the range of the global wl set
157
+
158
+ bands = rsr_bands
159
+ bidx = np.where((bands >= np.amin(wl)) & (bands <= np.amax(wl)))
160
+ bands = bands[bidx]
161
+
162
+ sidx = np.where((wl >= np.amin(rsr_wvl)) & (wl <= np.amax(rsr_wvl)))
163
+ spectrum = spectrum[sidx]
164
+
165
+ # Handle cases where input RSR is not at 1nm intervals
166
+ if (np.amax(rsr_wvl) - np.amin(rsr_wvl)) / len(rsr_wvl) != 1:
167
+ rsr_interp = interp1d(rsr_wvl, rsr, kind='cubic', axis=0)
168
+ rsr = rsr_interp(wl[sidx])
169
+ rsr_wvl = wl[sidx]
170
+
171
+ rsr_spectrum = np.empty(len(bands))
172
+ idx = np.where((rsr_wvl >= np.amin(wl)) & (rsr_wvl <= np.amax(wl)))
173
+ rsr = rsr[idx]
174
+
175
+ if len(spectrum.shape) == 2:
176
+ rsr_spectrum = np.empty([len(bands), spectrum.shape[1]])
177
+ for j in range(spectrum.shape[1]):
178
+ for i, _ in enumerate(bands):
179
+ rsr_spectrum[i][j] = (np.trapz(spectrum[:, j]
180
+ * rsr.transpose()[i, :])
181
+ / np.trapz(rsr.transpose()[i, :]))
182
+ else:
183
+ for i, _ in enumerate(bands):
184
+ rsr_spectrum[i] = (np.trapz(spectrum*rsr.transpose()[i, :])
185
+ / np.trapz(rsr.transpose()[i, :]))
186
+ return bands, rsr_spectrum
187
+
188
+ def rrs_to_lwn(rrs, tsis=False):
189
+ """Convert rrs to Lwn."""
190
+ wl = get_wavelength_array()
191
+
192
+ f0, f0wl = get_f0.get_f0(tsis=tsis)
193
+ widx = np.where((f0wl >= np.amin(wl)) & (f0wl <= np.amax(wl)))
194
+ f0 = f0[widx]
195
+ if len(rrs.shape) == 2:
196
+ f0 = np.expand_dims(f0, axis=-1)
197
+
198
+ return rrs * f0
199
+
200
+ def bandpass_avg_rrs(rrs, wvl, bandpass=11):
201
+ """Calculate bandpass averaged Rrs centered on discrete wavelengths."""
202
+ wl = get_wavelength_array()
203
+ dim = [len(wvl), 1]
204
+ if len(rrs.shape) == 2:
205
+ dim[1] = rrs.shape[1]
206
+ discrete = np.empty(dim)
207
+ discrete[:] = np.nan
208
+ delta = bandpass / 2.
209
+ for i in range(len(wvl)):
210
+ v = np.where((wl >= wvl[i]-delta) & (wl <= wvl[i]+delta))
211
+ if v:
212
+ if len(rrs.shape) == 2:
213
+ discrete[i, :] = np.mean(rrs[v, :], axis=1)
214
+ else:
215
+ discrete[i, :] = np.mean(rrs[v])
216
+
217
+ return np.squeeze(discrete)