Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mfisher87 committed Jul 19, 2024
1 parent 9b67220 commit 3a1e2cd
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 80 deletions.
6 changes: 4 additions & 2 deletions antarctica_today/generate_antarctica_today_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class AT_map_generator:
"""A class for generating both daily, annual, and annual-anomaly melt maps.
Stores internal information for creating the maps in order to help facilitate
ease and re-use of matplotlib base figures, and speed up execution.
ease and reuse of matplotlib base figures, and speed up execution.
"""

def __init__(
Expand Down Expand Up @@ -1724,7 +1724,9 @@ def generate_annual_melt_map(

if mmdd_of_year is not None:
datetime_of_year = datetime.datetime(
year=(y + 1) if (tuple(mmdd_of_year) <= tuple(melt_end_mmdd)) else y,
year=(y + 1)
if (tuple(mmdd_of_year) <= tuple(melt_end_mmdd))
else y,
month=mmdd_of_year[0],
day=mmdd_of_year[1],
)
Expand Down
122 changes: 77 additions & 45 deletions antarctica_today/generate_plots_for_given_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,36 @@

import argparse
import datetime
import dateutil.parser
import matplotlib.pyplot
import os
import re
import shutil

import dateutil.parser
import matplotlib.pyplot

from antarctica_today import (
generate_daily_melt_file,
generate_antarctica_today_map,
tb_file_data,
generate_daily_melt_file,
map_filedata,
plot_daily_melt_and_climatology,
map_filedata
tb_file_data,
)


def generate_maps_and_plots_for_a_date(
dt: datetime.datetime,
region="ALL",
dpi: int=300,
copy_to_gathered_dir: bool=True,
):
dpi: int = 300,
copy_to_gathered_dir: bool = True,
):
"""Generate 3 maps and one line-plot for a given date.
If a region number is given (0-7), produce them just for that region. If 'ALL', produce them for all the regions.
Plots will be placed in their respective sub-directories under /plots.
If copy_to_gathered_dir, after plots are produced for that day, put them in the "/plots/daily_plots_gathered"
sub-directory (under a sub-dir matching that date) for easy fetching.
This function does not do any of the processing for a given date nor update the database. For that, use
the update_data.py script.
"""
Expand All @@ -45,7 +47,7 @@ def generate_maps_and_plots_for_a_date(
region = int(region)

if region == "ALL":
regions = list(range(0,8))
regions = list(range(0, 8))
else:
assert type(region) == int and 0 <= region <= 7
regions = [region]
Expand All @@ -60,7 +62,7 @@ def generate_maps_and_plots_for_a_date(
region_number=region_num,
mmdd_of_year=(dt.month, dt.day),
dpi=dpi,
message_below_year=date_message
message_below_year=date_message,
)

mapper.generate_anomaly_melt_map(
Expand All @@ -73,17 +75,21 @@ def generate_maps_and_plots_for_a_date(

# Find the daily melt .bin file for this particular day, to create the daily melt map for that day.
daily_dir = tb_file_data.model_results_dir
matching_binfiles = \
[os.path.join(daily_dir, fname)
for fname in os.listdir(daily_dir) if
fname.find("antarctica_melt_" + dt.strftime("%Y%m%d")) == 0 and os.path.splitext(fname)[1] == ".bin"
]
matching_binfiles = [
os.path.join(daily_dir, fname)
for fname in os.listdir(daily_dir)
if fname.find("antarctica_melt_" + dt.strftime("%Y%m%d")) == 0
and os.path.splitext(fname)[1] == ".bin"
]
# We really should only find one melt .bin file for that particular day.
assert len(matching_binfiles) <= 1
# Only generate a map if we've found a file for it.
if len(matching_binfiles) == 1:
mapper.generate_daily_melt_map(
infile=matching_binfiles[0], outfile="auto", region_number=region_num, dpi=dpi
infile=matching_binfiles[0],
outfile="auto",
region_number=region_num,
dpi=dpi,
)

lineplot_outfile = os.path.join(
Expand Down Expand Up @@ -113,9 +119,15 @@ def generate_maps_and_plots_for_a_date(
# Get latest file from melt anomaly directory. Also, get the date we're computing up to.
date_string = dt.strftime("%Y.%m.%d")
if region == "ALL":
search_str = "R[0-7]_{0}-{1}_{2}".format(year, year + 1, date_string) + r"(\w)*\.png\Z"
search_str = (
"R[0-7]_{0}-{1}_{2}".format(year, year + 1, date_string)
+ r"(\w)*\.png\Z"
)
else:
search_str = "R{3}_{0}-{1}_{2}".format(year, year + 1, date_string, region) + r"(\w)*\.png\Z"
search_str = (
"R{3}_{0}-{1}_{2}".format(year, year + 1, date_string, region)
+ r"(\w)*\.png\Z"
)

# Get all the files that match our search string. May be different dates and possibly different regions.

Expand Down Expand Up @@ -149,15 +161,17 @@ def generate_maps_and_plots_for_a_date(
]

files_to_move = (
files_in_anomaly_maps_dir
+ files_in_sum_maps_dir
+ files_in_daily_maps_dir
+ files_in_line_plots_dir
files_in_anomaly_maps_dir
+ files_in_sum_maps_dir
+ files_in_daily_maps_dir
+ files_in_line_plots_dir
)

date_string = dt.strftime("%Y.%m.%d")
# If the sub-directory with this latest date doesn't exist, create it.
dest_dir_location = os.path.join(tb_file_data.daily_plots_gathered_dir, date_string)
dest_dir_location = os.path.join(
tb_file_data.daily_plots_gathered_dir, date_string
)
if not os.path.exists(dest_dir_location):
os.mkdir(dest_dir_location)
print("Created directory '{0}'.".format(dest_dir_location))
Expand All @@ -174,30 +188,48 @@ def generate_maps_and_plots_for_a_date(


def define_and_parse_args():
parser = argparse.ArgumentParser("Produce all plots for the season on a particular date. Does not download any new"
" data or update the database, just produces the plots and maps.")
parser.add_argument("DATE", type=str, help="A date in a format readable by the python dateutil.parser module."
"YYYY-MM-DD suffices."
"Date should be within the melt season from 01 Oct thru 30 Apr."
"Behavior is undefined for dates outside that melt-season range.")
parser.add_argument("-region", "-r", default="ALL",
help="Generate for a specific Antarctic region (0-7), or 'ALL'. Default 'ALL' will produce all"
"regions 0 (Antarctic continent) and 1-7 (or each sub-region).")
parser.add_argument("-dpi", "-d", type=int, default=300, help="DPI of output figures (in PNG format)."
" Default 300.")
parser.add_argument("--gather", "--g", default=False, action="store_true",
help="Copy all plots produced into a dated sub-dir of the plots/daily_plots_gathered for easy"
"fetching after execution.")
parser = argparse.ArgumentParser(
"Produce all plots for the season on a particular date. Does not download any new"
" data or update the database, just produces the plots and maps."
)
parser.add_argument(
"DATE",
type=str,
help="A date in a format readable by the python dateutil.parser module."
"YYYY-MM-DD suffices."
"Date should be within the melt season from 01 Oct thru 30 Apr."
"Behavior is undefined for dates outside that melt-season range.",
)
parser.add_argument(
"-region",
"-r",
default="ALL",
help="Generate for a specific Antarctic region (0-7), or 'ALL'. Default 'ALL' will produce all"
"regions 0 (Antarctic continent) and 1-7 (or each sub-region).",
)
parser.add_argument(
"-dpi",
"-d",
type=int,
default=300,
help="DPI of output figures (in PNG format)." " Default 300.",
)
parser.add_argument(
"--gather",
"--g",
default=False,
action="store_true",
help="Copy all plots produced into a dated sub-dir of the plots/daily_plots_gathered for easy"
"fetching after execution.",
)

return parser.parse_args()


if __name__ == "__main__":
args = define_and_parse_args()
this_dt = dateutil.parser.parse(args.DATE)

generate_maps_and_plots_for_a_date(
this_dt,
region=args.region,
dpi=args.dpi,
copy_to_gathered_dir=args.gather
)
this_dt, region=args.region, dpi=args.dpi, copy_to_gathered_dir=args.gather
)
2 changes: 1 addition & 1 deletion antarctica_today/melt_array_picklefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def _filter_out_erroneous_swaths(model_array, datetimes_dict):
"""Nullify particular false-positive satellite swaths in the data.
These are hand-outlined to nullify data (primarily from the 1980s) in which the satellite was
giving false-positive readings and producing melt extents that were unreasonable and ficticious.
giving false-positive readings and producing melt extents that were unreasonable and fictitious.
We outline those regions and set false "melt" (2) values to "no data" (0).
Expand Down
16 changes: 9 additions & 7 deletions antarctica_today/nsidc_download_Tb_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
from __future__ import print_function

import datetime
import dateutil.parser
import math
import os.path
import sys
import time

import dateutil.parser
import earthaccess

from antarctica_today.constants.paths import DATA_TB_DIR
Expand Down Expand Up @@ -232,16 +232,16 @@ def _results_with_links(results: list) -> list:


def _get_mmdd_from_earthdata_granule(granule):
date_str = granule['umm']['TemporalExtent']['RangeDateTime']['BeginningDateTime']
date_str = granule["umm"]["TemporalExtent"]["RangeDateTime"]["BeginningDateTime"]
dt = dateutil.parser.parse(date_str)
return dt.month, dt.day


def filter_data_only_in_melt_season(results: list,
mmdd_start: tuple = (10, 1),
mmdd_end: tuple = (4, 30)) -> list:
def filter_data_only_in_melt_season(
results: list, mmdd_start: tuple = (10, 1), mmdd_end: tuple = (4, 30)
) -> list:
"""For Antarctica Today, we're interested only in dates that correspond with the melt season, defined here from
1st of October thru 30th of April of the following year (the Antartic melt season).
1st of October thru 30th of April of the following year (the Antarctic melt season).
Results outside of that date range will be omitted and not downloaded.
Tb values in the cold frozen winter are used to calibrate the model and set thresholds before the beginning of
Expand Down Expand Up @@ -300,7 +300,9 @@ def download_new_files(

if only_in_melt_season:
results = filter_data_only_in_melt_season(results)
print(f"Found {len(results)} downloadable granules within the Antarctic melt season.")
print(
f"Found {len(results)} downloadable granules within the Antarctic melt season."
)
else:
print(f"Found {len(results)} downloadable granules.")

Expand Down
2 changes: 1 addition & 1 deletion antarctica_today/read_NSIDC_bin_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def read_NSIDC_bin_file(
):
return_array = numpy.array(int_array, dtype=return_type)
# Else, if it's meant to be a floating-point array, scale by the multiplier
# and return the floating-point array. If the mutiplier is a float (i.e. 0.1),
# and return the floating-point array. If the multiplier is a float (i.e. 0.1),
# numpy will convert and return an array of floats
else:
return_array = numpy.array(int_array * multiplier, dtype=return_type)
Expand Down
2 changes: 1 addition & 1 deletion antarctica_today/src_baseline/download_NSIDC_Tb_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# where 'myusername' and 'mypassword' are your Earthdata credentials.
#
# Update: 2020.06.02: Mike MacFerrin
# Create a command-line version of this for flexible re-use
# Create a command-line version of this for flexible reuse

from __future__ import print_function

Expand Down
Loading

0 comments on commit 3a1e2cd

Please sign in to comment.