Skip to content

Commit

Permalink
cosmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfromearth committed Sep 19, 2023
1 parent 9da19ab commit a6ab772
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
33 changes: 17 additions & 16 deletions concatenator/concat_with_nco.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
"""Concatenation service that appends data along an existing dimension, using NCO operators."""
from logging import getLogger

import netCDF4 as nc # type: ignore
import netCDF4 as nc
from nco import Nco # type: ignore

from concatenator.stitchee import _validate_workable_files

default_logger = getLogger(__name__)


def concat_netcdf_files(original_input_files,
output_file,
dim_for_record_dim='mirror_step',
decompress_datasets=False,
logger=default_logger):
def concat_netcdf_files(
original_input_files,
output_file,
dim_for_record_dim="mirror_step",
decompress_datasets=False,
logger=default_logger,
):
"""
Main entrypoint to merge implementation. Merges n >= 2 granules together as a single
granule. Named in reference to original Java implementation.
Expand All @@ -30,7 +32,7 @@ def concat_netcdf_files(original_input_files,
logger object
"""
nco = Nco()
logger.info('Preprocessing data...')
logger.info("Preprocessing data...")

# Proceed to concatenate only files that are workable (can be opened and are not empty).
input_files, _ = _validate_workable_files(original_input_files, logger)
Expand All @@ -48,17 +50,16 @@ def concat_netcdf_files(original_input_files,

# Convert an existing dimension to be a record dimension if one did not already exist.
if not contains_record_dim:
print(f" No record dimension exists; making <{dim_for_record_dim}> into a record dimension..")
nco.ncks(input=file, output=file,
options=["-O", f"--mk_rec_dmn {dim_for_record_dim}"])
print(
f" No record dimension exists; making <{dim_for_record_dim}> into a record dimension.."
)
nco.ncks(input=file, output=file, options=["-O", f"--mk_rec_dmn {dim_for_record_dim}"])

if decompress_datasets:
nco.ncks(input=file, output=file,
options=["-L 0"])
nco.ncks(input=file, output=file, options=["-L 0"])

# -- concatenate datasets --
logger.info('Concatenating datasets...')
nco.ncrcat(input=input_files, output=output_file,
options=["--no_tmp_fl"])
logger.info("Concatenating datasets...")
nco.ncrcat(input=input_files, output=output_file, options=["--no_tmp_fl"])

logger.info('Done.')
logger.info("Done.")
2 changes: 1 addition & 1 deletion concatenator/group_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
import re

import netCDF4 as nc # type: ignore
import netCDF4 as nc
import numpy as np
import xarray as xr

Expand Down
2 changes: 1 addition & 1 deletion concatenator/stitchee.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from logging import Logger

import netCDF4 as nc # type: ignore
import netCDF4 as nc
import xarray as xr

from concatenator import GROUP_DELIM
Expand Down
41 changes: 21 additions & 20 deletions tests/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tempfile import mkdtemp
from unittest import TestCase

import netCDF4 as nc # type: ignore
import netCDF4 as nc
import pytest

from concatenator import concat_with_nco
Expand All @@ -22,18 +22,17 @@ class TestConcat(TestCase):
@classmethod
def setUpClass(cls):
cls.__test_path = Path(__file__).parent.resolve()
cls.__test_data_path = cls.__test_path.joinpath('data')
cls.__output_path = Path(mkdtemp(prefix='tmp-', dir=cls.__test_data_path))
cls.__test_data_path = cls.__test_path.joinpath("data")
cls.__output_path = Path(mkdtemp(prefix="tmp-", dir=cls.__test_data_path))

@classmethod
def tearDownClass(cls):
if not cls.KEEP_TMP: # pylint: disable=no-member
rmtree(cls.__output_path)

def run_verification_with_stitchee(self,
data_dir,
output_name,
record_dim_name: str = 'mirror_step'):
def run_verification_with_stitchee(
self, data_dir, output_name, record_dim_name: str = "mirror_step"
):
output_path = str(self.__output_path.joinpath(output_name)) # type: ignore
data_path = self.__test_data_path.joinpath(data_dir) # type: ignore

Expand All @@ -44,11 +43,13 @@ def run_verification_with_stitchee(self,
shutil.copyfile(filepath, copied_input_new_path)
input_files.append(str(copied_input_new_path))

output_path = stitchee(files_to_concat=input_files,
output_file=output_path,
write_tmp_flat_concatenated=True,
keep_tmp_files=True,
concat_dim=record_dim_name)
output_path = stitchee(
files_to_concat=input_files,
output_file=output_path,
write_tmp_flat_concatenated=True,
keep_tmp_files=True,
concat_dim=record_dim_name,
)

merged_dataset = nc.Dataset(output_path)

Expand All @@ -59,7 +60,7 @@ def run_verification_with_stitchee(self,
length_sum += len(nc.Dataset(file).variables[record_dim_name])
assert length_sum == len(merged_dataset.variables[record_dim_name])

def run_verification_with_nco(self, data_dir, output_name, record_dim_name='mirror_step'):
def run_verification_with_nco(self, data_dir, output_name, record_dim_name="mirror_step"):
output_path = str(self.__output_path.joinpath(output_name))
data_path = self.__test_data_path.joinpath(data_dir)

Expand All @@ -70,9 +71,9 @@ def run_verification_with_nco(self, data_dir, output_name, record_dim_name='mirr
shutil.copyfile(filepath, copied_input_new_path)
input_files.append(str(copied_input_new_path))

concat_with_nco.concat_netcdf_files(input_files, output_path,
dim_for_record_dim=record_dim_name,
decompress_datasets=True)
concat_with_nco.concat_netcdf_files(
input_files, output_path, dim_for_record_dim=record_dim_name, decompress_datasets=True
)

merged_dataset = nc.Dataset(output_path)

Expand All @@ -84,16 +85,16 @@ def run_verification_with_nco(self, data_dir, output_name, record_dim_name='mirr
assert length_sum == len(merged_dataset.variables[record_dim_name])

def test_tempo_no2_concat_with_stitchee(self):
self.run_verification_with_stitchee('tempo/no2', 'tempo_no2_bee_concatenated.nc')
self.run_verification_with_stitchee("tempo/no2", "tempo_no2_bee_concatenated.nc")

def test_tempo_hcho_concat_with_stitchee(self):
self.run_verification_with_stitchee('tempo/hcho', 'tempo_hcho_bee_concatenated.nc')
self.run_verification_with_stitchee("tempo/hcho", "tempo_hcho_bee_concatenated.nc")

def test_tempo_cld04_concat_with_stitchee(self):
self.run_verification_with_stitchee('tempo/cld04', 'tempo_cld04_bee_concatenated.nc')
self.run_verification_with_stitchee("tempo/cld04", "tempo_cld04_bee_concatenated.nc")

def test_tempo_o3prof_concat_with_stitchee(self):
self.run_verification_with_stitchee('tempo/o3prof', 'tempo_o3prof_bee_concatenated.nc')
self.run_verification_with_stitchee("tempo/o3prof", "tempo_o3prof_bee_concatenated.nc")

# def test_icesat_concat_with_stitchee(self):
# self.run_verification_with_stitchee('icesat', 'icesat_concat_with_stitchee.nc')
Expand Down

0 comments on commit a6ab772

Please sign in to comment.