Skip to content
Draft
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
42 changes: 32 additions & 10 deletions pygac_fdr/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,52 @@
)


def read_gac(filename, reader_kwargs=None):
"""Read and calibrate AVHRR GAC level 1b data using satpy.
def read_file(filename, reader_kwargs=None):
"""Read and calibrate AVHRR GAC/LAC level 1b data using satpy.

Args:
filename (str): AVHRR GAC level 1b file
filename (str): AVHRR GAC/LAC level 1b file
reader_kwargs (dict): Keyword arguments to be passed to the reader.

Returns:
The loaded data in a satpy.Scene object.
"""
scene = satpy.Scene(
filenames=[filename], reader="avhrr_l1b_gaclac", reader_kwargs=reader_kwargs
)
scene = satpy.Scene(filenames=[filename], reader="avhrr_l1b_gaclac", reader_kwargs=reader_kwargs)
scene.load(BANDS)
if reader_kwargs.get("dem") is not None:
AUX_DATA.append("tc_latitude")
AUX_DATA.append("tc_longitude")
if reader_kwargs.get("reference_image") is not None:
AUX_DATA.append("gcp_x")
AUX_DATA.append("gcp_y")
AUX_DATA.append("gcp_longitude")
AUX_DATA.append("gcp_latitude")
AUX_DATA.append("gcp_x_corrected")
AUX_DATA.append("gcp_y_corrected")
AUX_DATA.append("gcp_x_displacement")
AUX_DATA.append("gcp_y_displacement")
if reader_kwargs.get("compute_uncertainties"):
AUX_DATA.extend(("random_uncertainty",
"systematic_uncertainty",
"channel_covariance_ratio",
"uncertainty_flags"))
scene.load(AUX_DATA)

# Add additional metadata
basename = os.path.basename(filename)
fname_info = trollsift.parse(GAC_FORMAT, basename)
orbit_number_end = (
fname_info["orbit_number"] // 100 * 100 + fname_info["end_orbit_last_digits"]
)
scene.attrs.update(
{
"gac_filename": basename,
}
)
try:
fname_info = trollsift.parse(GAC_FORMAT, basename)
except ValueError:
return scene

orbit_number_end = fname_info["orbit_number"] // 100 * 100 + fname_info["end_orbit_last_digits"]
scene.attrs.update(
{
"orbit_number_start": fname_info["orbit_number"],
"orbit_number_end": orbit_number_end,
"ground_station": fname_info["station"],
Expand Down
3 changes: 2 additions & 1 deletion bin/pygac-fdr-crop → pygac_fdr/runners/crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

from pygac_fdr.crop import CROP_OVERLAP_BEGINNING, CROP_OVERLAP_END, crop

if __name__ == "__main__":

def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("filename", type=str, help="pygac-fdr output file")
parser.add_argument(
Expand Down
3 changes: 2 additions & 1 deletion bin/pygac-fdr-dump → pygac_fdr/runners/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

from pygac_fdr.metadata import MetadataCollector

if __name__ == "__main__":

def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("dbfile", help="Metadata database")
parser.add_argument("-n", help="Maximum number of rows to be printed", type=int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
for reading a long list of filenames from an arguments file.
"""

if __name__ == "__main__":
def main():
parser = argparse.ArgumentParser(
description=__doc__, fromfile_prefix_chars="@", epilog=tooltip
)
Expand Down
3 changes: 2 additions & 1 deletion bin/pygac-fdr-mda-update → pygac_fdr/runners/mda_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from pygac_fdr.metadata import MetadataUpdater, read_metadata_from_database
from pygac_fdr.utils import logging_on

if __name__ == "__main__":

def main():
parser = argparse.ArgumentParser(description="Update metadata in level 1c files")
parser.add_argument(
"--dbfile",
Expand Down
49 changes: 28 additions & 21 deletions bin/pygac-fdr-run → pygac_fdr/runners/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import logging
import re
import tarfile
from contextlib import closing
from contextlib import closing, suppress

import satpy
from satpy.readers import FSFile

from pygac_fdr.config import read_config
from pygac_fdr.reader import read_gac
from pygac_fdr.reader import read_file
from pygac_fdr.utils import LOGGER_NAME, TarFileSystem, logging_on
from pygac_fdr.writer import NetcdfWriter

Expand All @@ -44,9 +44,7 @@ def __fspath__(self):
def process_file(filename, config):
LOG.info("Processing file {}".format(filename))
try:
scene = read_gac(
filename, reader_kwargs=config["controls"].get("reader_kwargs")
)
scene = read_file(filename, reader_kwargs=config["controls"].get("reader_kwargs"))
writer = NetcdfWriter(
output_dir=config["output"].get("output_dir"),
global_attrs=config.get("global_attrs"),
Expand All @@ -56,8 +54,18 @@ def process_file(filename, config):
engine=config["netcdf"].get("engine"),
debug=config["controls"].get("debug"),
)

writer.write(scene=scene)
success = True
if image_config := config["output"].get("image"):
composite = image_config["composite"]
scene.load([composite])
area = image_config["area"]
scn = scene.resample(area)
overlay = None
with suppress(KeyError):
overlay = {'coast_dir': image_config["coastlines_dir"], 'color': 'red'}
scn.save_dataset(composite, base_dir=config["output"].get("output_dir"), overlay=overlay)
except Exception as err:
if config["controls"]["debug"]:
raise
Expand All @@ -81,23 +89,17 @@ def process_tarball(tarball, config):
else:
LOG.info("Successfully processed all files in {}".format(tarball))


if __name__ == "__main__":
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(
description="Read & calibrate AVHRR GAC data and write results to netCDF"
)
parser.add_argument(
"--cfg", required=True, type=str, help="Path to pygac-fdr configuration file."
)
parser = argparse.ArgumentParser(description="Read & calibrate AVHRR GAC data and write results to netCDF")
parser.add_argument("--cfg", required=True, type=str, help="Path to pygac-fdr configuration file.")
parser.add_argument(
"--output-dir",
help="Output directory. Overrides entry in the configuration file.",
)
parser.add_argument(
"--tle-dir",
help="Directory containing TLE files. Overrides entry in the configuration"
" file.",
help="Directory containing TLE files. Overrides entry in the configuration file.",
)
parser.add_argument(
"--debug",
Expand All @@ -106,12 +108,11 @@ def process_tarball(tarball, config):
"next file. Overrides entry in the configuration file.",
)
parser.add_argument("--verbose", action="store_true", help="Increase verbosity")
parser.add_argument(
"--log-all", action="store_true", help="Enable logging for all modules"
)
parser.add_argument(
"filenames", nargs="+", help="AVHRR GAC level 1b files to be processed"
)
parser.add_argument("--log-all", action="store_true", help="Enable logging for all modules")
parser.add_argument("filenames", nargs="+", help="AVHRR GAC level 1b files to be processed")
parser.add_argument("--georef", type=str, help="Path to reference GeoTiff file for georeferencing")
parser.add_argument("--dem", type=str, help="Path to digital elevation model GeoTiff file for orthocorrection")
parser.add_argument("--with-uncertainties", action="store_true", help="Compute uncertainties")
args = parser.parse_args()
logging_on(logging.DEBUG if args.verbose else logging.INFO, for_all=args.log_all)

Expand All @@ -122,6 +123,12 @@ def process_tarball(tarball, config):
config["output"]["output_dir"] = args.output_dir
if args.tle_dir:
config["controls"]["reader_kwargs"]["tle_dir"] = args.tle_dir
if args.georef:
config["controls"]["reader_kwargs"]["reference_image"] = args.georef
if args.dem:
config["controls"]["reader_kwargs"]["dem"] = args.dem
if args.with_uncertainties:
config["controls"]["reader_kwargs"]["compute_uncertainties"] = args.with_uncertainties
if args.debug:
config["controls"]["debug"] = args.debug

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
for reading a long list of filenames from an arguments file.
"""

if __name__ == "__main__":

def main():
parser = argparse.ArgumentParser(
description=__doc__, fromfile_prefix_chars="@", epilog=tooltip
)
Expand Down
2 changes: 1 addition & 1 deletion bin/pygac-fdr-val-plot → pygac_fdr/runners/val_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def draw_hovmoeller(channel, data):
return fig


if __name__ == "__main__":
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("dbfiles", nargs="+", help="Statistics databases")
parser.add_argument("--cache", help="Cache directory, defaults to %(default)s", default='./cache')
Expand Down
Loading
Loading