Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions ghg_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import target_generation
import parallel_mf
import scale
import apply_glt
from spectral.io import envi
import numpy as np
from utils import envi_header, convert_to_cog
from utils import convert_to_cog
from files import Filenames
import spec_io

metadata = {
'ch4': {
Expand Down Expand Up @@ -93,15 +92,6 @@ def main(input_args=None):
if args.wavelength_range is not None and len(args.wavelength_range) % 2 != 0:
raise ValueError('wavelength_range must have an even number of elements')

radiance_file = args.radiance_file
radiance_file_hdr = envi_header(radiance_file)

obs_file = args.obs_file
obs_file_hdr = envi_header(obs_file)

loc_file = args.loc_file
loc_file_hdr = envi_header(loc_file)

logging.basicConfig(format='%(levelname)s:%(asctime)s ||| %(message)s', level=args.loglevel,
filename=args.logfile, datefmt='%Y-%m-%d,%H:%M:%S')

Expand All @@ -115,18 +105,26 @@ def main(input_args=None):
print(files.target_file)

if os.path.isfile(files.target_file) is False or args.overwrite:
sza = envi.open(obs_file_hdr).open_memmap(interleave='bip')[...,4]
_, obs = spec_io.load_data(args.obs_file)
sza = obs[...,4]

mean_sza = np.mean(sza[sza != -9999])

elevation = envi.open(loc_file_hdr).open_memmap(interleave='bip')[...,2]
_, elevation = spec_io.load_data(args.loc_file, return_loc_from_l1b_rad_nc=True)
elevation = elevation[:,:,:2]
mean_elevation = np.mean(elevation[elevation != -9999]) / 1000.
mean_elevation = min(max(0, mean_elevation),3)

if args.state_subs is not None:
state_ds = envi.open(envi_header(args.state_subs))
band_names = state_ds.metadata['band names']
h2o = state_ds.open_memmap(interleave='bip')[...,band_names.index('H2OSTR')]
mean_h2o = np.mean(h2o[h2o != -9999])

m_state, d_state = spec_io.load_data(args.state_subs, mask_type='mask')
if args.state_subs.endswith('.nc'):
ind = m_state.band_names.index('H2O (g cm-2)')
mean_h2o = np.mean(d_state[:,:,ind])
else:
ind = m_state.band_names.index('H2OSTR')
mean_h2o = np.mean(d_state[:,0,ind])

else:
# Just guess something...
exit()
Expand All @@ -143,7 +141,7 @@ def main(input_args=None):
'-g', str(mean_elevation),
'-w', str(mean_h2o),
'--output', files.target_file,
'--hdr', radiance_file_hdr,
'--hdr', args.radiance_file,
'--lut_dataset', args.lut_file]
if args.co2:
target_params.append('--co2')
Expand Down Expand Up @@ -176,18 +174,26 @@ def main(input_args=None):
if args.ace_filter:
subargs.append('--use_ace_filter')
parallel_mf.main(subargs)

def do_ortho_with_spec_io(obs_for_glt_filename, unortho_input_filename, ortho_output_filename):
m_obs, _ = spec_io.load_data(obs_for_glt_filename, load_glt=True)
m, d = spec_io.load_data(unortho_input_filename)
d_ort = spec_io.ortho_data(d, m_obs.glt)
m.geotransform = m_obs.geotransform
m.projection = m_obs.projection
spec_io.write_envi_file(d_ort, m, ortho_output_filename)

# ORT MF
if (os.path.isfile(files.mf_ort_file) is False or args.overwrite):
apply_glt.main([args.glt_file, files.mf_file, files.mf_ort_file])
do_ortho_with_spec_io(args.glt_file, files.mf_file, files.mf_ort_file)
convert_to_cog(files.mf_ort_file,
files.mf_ort_cog,
metadata[gas]['mf'],
args.software_version,
args.product_version)
# ORT Sensitivity
if os.path.isfile(files.sens_ort_file) is False or args.overwrite:
apply_glt.main([args.glt_file, files.mf_sens_file, files.sens_ort_file])
do_ortho_with_spec_io(args.glt_file, files.mf_sens_file, files.sens_ort_file)
if os.path.isfile(files.sens_ort_cog) is False or args.overwrite:
convert_to_cog(files.sens_ort_file,
files.sens_ort_cog,
Expand All @@ -196,7 +202,7 @@ def main(input_args=None):
args.product_version)
# ORT Uncertainty
if os.path.isfile(files.uncert_ort_file) is False or args.overwrite:
apply_glt.main([args.glt_file, files.mf_uncert_file, files.uncert_ort_file])
do_ortho_with_spec_io(args.glt_file, files.mf_uncert_file, files.uncert_ort_file)
if os.path.isfile(files.uncert_ort_cog) is False or args.overwrite:
convert_to_cog(files.uncert_ort_file,
files.uncert_ort_cog,
Expand Down
Loading