Skip to content

Commit

Permalink
Merge pull request #22 from nsidc/python-docs
Browse files Browse the repository at this point in the history
Python docs
  • Loading branch information
mjbrodzik authored Aug 20, 2024
2 parents e62a3fb + 2e78277 commit dbbe210
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions python/nsidc0630_params.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Grid information used by remove_duplicate_files
resolutions = {
'NS25': {
'projections': ['N25', 'S25'],
Expand Down
51 changes: 35 additions & 16 deletions python/remove_duplicate_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@


def remove_files(prefix, projections, ltods, channels, satellite, date, file_regex):
"""
Deletes all but the most recent duplicates of files with requested
characteristics. This function relies on the processing date part of the
filenames to be sorted so that the most recent file will be last in the
sorted list. Should be called separately clean up sets of data, premet or
spatial metadata files.
prefix : string, NSIDC authID used as filename prefix in the data files
projections : list of projection strings, e.g. 'N25'
ltods : list of ltod letters, any of 'E', 'M', 'A', 'D'
channels : list of channel ids (including polarization)
satellite : list of platform/sensors, any of 'F16', 'F17', 'F18', 'AMSR2', 'SMAP'
date : string, date to search for, yyyymmdd
file_regex : full path and filename regex to look for
"""
for projection in projections:
for ltod in ltods:
for channel in channels:
Expand All @@ -29,10 +45,19 @@ def remove_files(prefix, projections, ltods, channels, satellite, date, file_reg


def datetime_to_date(_ctx, _param, value: dt.datetime) -> dt.date:
"""Click callback that converts a `dt.datetime` to `dt.date`."""
"""
Click callback that converts a `dt.datetime` to `dt.date`
"""
return value.date()

def daterange(start_date, end_date):
"""
Generator to efficiently return the span of dates by day
start_date : dt.datetime, begin date
end_date : dt.datetime, end date
"""
for n in range(int((end_date - start_date).days)):
yield start_date + dt.timedelta(n)

Expand All @@ -44,7 +69,7 @@ def daterange(start_date, end_date):
type=click.DateTime(formats=['%Y%m%d', '%Y-%m-%d']),
default=str(dt.datetime.today().date() - timedelta(days=10)),
show_default=True,
help='Start date of MOD09GA tiles to process.',
help='Start date of files to process.',
callback=datetime_to_date,
)
@click.option(
Expand All @@ -53,7 +78,7 @@ def daterange(start_date, end_date):
type=click.DateTime(formats=['%Y%m%d', '%Y-%m-%d']),
default=str(dt.datetime.today().date() - timedelta(days = 0)),
show_default=True,
help='End date of MOD09GA tiles to process.',
help='End date of files to process.',
callback=datetime_to_date,
)
@click.option(
Expand All @@ -62,17 +87,14 @@ def daterange(start_date, end_date):
type=click.Path(
file_okay=False, dir_okay=True, exists=False, path_type=Path
),
envvar='MOD09GA_NRT_DIR',
show_default=True,
help='Absolute directory to existing MOD09GA granule files set by MOD09GA_NRT_DIR'
' environment variable. Date and tile ID subdirectories will be added'
' (e.g. 2023.10.03/h08v04).',
required=True,
help='Absolute directory to existing NSIDC0630 or NSIDC0738 files ',
)
@click.option(
'-p',
'--platforms',
type=click.Choice(['F16', 'F17', 'F18', 'AMSR2', 'SMAP']),
default=['F16'],
type=click.Choice(['F16_SSMIS', 'F17_SSMIS', 'F18_SSMIS', 'GCOMW1_AMSR2', 'SMAP_LRM']),
default=['F16_SSMIS'],
multiple=True,
help='Platforms for which duplicate files are to be removed.',
)
Expand All @@ -83,17 +105,14 @@ def remove_duplicate_files(start_date, end_date, input_dir, platforms):
end_date.strftime('%m/%d/%Y'))
for day in daterange(start_date, end_date + dt.timedelta(days=1)):
for platform in platforms:
if platform in ['F16', 'F17', 'F18']:
if platform in ['F16_SSMIS', 'F17_SSMIS', 'F18_SSMIS']:
channels = ['19H', '19V', '22V', '37H', '37V', '91H', '91V']
platform = platform + '_SSMIS'
prefix = 'NSIDC0630'
elif platform in ['AMSR2']:
elif platform in ['GCOMW1_AMSR2']:
channels = ['6.9H', '6.9V', '10.7H', '10.7V', '18H', '18V', '23H', '23V', '36H', '36V', '89H', '89V']
platform = 'GCOMW1_' + platform
prefix = 'NSIDC0630'
elif platform in ['SMAP']:
elif platform in ['SMAP_LRM']:
channels = ['1.4H', '1.4V', '1.4F']
platform = platform + '_LRM'
prefix = 'NSIDC0738'
else:
raise ValueError('Unknown platform: ' + platform)
Expand Down

0 comments on commit dbbe210

Please sign in to comment.