Skip to content

Commit 17cb79c

Browse files
NeoKishhaileyajohnson
authored andcommitted
Partial fix to issue #21
1 parent e0e0f2f commit 17cb79c

1 file changed

Lines changed: 79 additions & 17 deletions

File tree

cdippy/ncstats.py

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44

55

66
class NcStats(StnData):
7-
"""For a given station, produces data availability.
7+
"""Produces data availability statistics for a given station.
88
9-
There are methods to return counts for the entire station record to be
10-
used diretly by a web app, and there are methods to save to disk availabililty
11-
counts (e.g. xyz counts) for individual nc files. In that case updates
12-
to totals would be calculated by re-summarizing any files that have changed
13-
and adding up all the files to produce new totals.
9+
This class provides methods to:
10+
* Return counts for the entire station record, intended for use by web applications.
11+
* Save availability counts (e.g., xyz counts) for individual NetCDF files.
12+
Updates to totals are calculated by re-summarizing any files that have changed
13+
and aggregating all files to produce new totals.
1414
"""
1515

1616
QC_flags = ["waveFlagPrimary", "sstFlagPrimary", "gpsStatusFlags"]
1717

1818
def __init__(self, stn: str, data_dir: str = None):
19-
"""
20-
PARAMETERS: See StnData
21-
"""
19+
"""Initializes an NcStats instance.
2220
21+
Args:
22+
stn (str): Station identifier.
23+
data_dir (str, optional): Path to the data directory. Defaults to None.
24+
"""
2325
StnData.__init__(self, stn, data_dir)
2426

2527
self.date_modifieds = {}
@@ -28,14 +30,26 @@ def __init__(self, stn: str, data_dir: str = None):
2830
self.pub_set = "all"
2931

3032
def make_stats(self) -> dict:
31-
"""Returns various statistics off the given station."""
33+
"""Computes station-level statistics.
34+
35+
Returns:
36+
dict: A dictionary containing:
37+
- "flag_counts" (dict): Flag count summaries for the station.
38+
- "deployments" (dict): Deployment summary statistics.
39+
"""
3240
result = {}
3341
result["flag_counts"] = self.flag_counts()
3442
result["deployments"] = self.deployment_summary()
3543
return result
3644

3745
def deployment_summary(self) -> dict:
38-
"""Returns deployment summary statistics."""
46+
"""Generates deployment summary statistics.
47+
48+
Returns:
49+
dict: A dictionary containing:
50+
- Deployment IDs as keys, with values containing start and end coverage times.
51+
- "number_of_deployments" (int): The number of deployments.
52+
"""
3953
self.load_nc_files()
4054
result = {}
4155
dep_cnt = 0
@@ -54,11 +68,26 @@ def deployment_summary(self) -> dict:
5468
return result
5569

5670
def load_nc_files(self, types: list = ["realtime", "historic", "archive"]) -> dict:
57-
"""Returns netCDF4 objects of a station's netcdf files"""
71+
"""Loads NetCDF files for the station.
72+
73+
Args:
74+
types (list, optional): List of file categories to load. Defaults to
75+
["realtime", "historic", "archive"].
76+
77+
Returns:
78+
dict: Dictionary of NetCDF file objects keyed by filename.
79+
"""
5880
self.nc_files = self.get_nc_files(types)
5981

6082
def load_file(self, nc_filename: str):
61-
"""Sets self.nc for a given nc_filename"""
83+
"""Loads a specific NetCDF file into the instance.
84+
85+
Args:
86+
nc_filename (str): Filename of the NetCDF file.
87+
88+
Sets:
89+
self.nc: Loaded NetCDF file object.
90+
"""
6291
if nc_filename in self.nc_files:
6392
self.nc = self.nc_files[nc_filename]
6493
else:
@@ -78,7 +107,15 @@ def nc_file_summaries(self) -> dict:
78107
return result
79108

80109
def nc_file_summary(self, nc_filename: str) -> dict:
81-
"""Returns statistical summaries given an nc file name."""
110+
"""Computes a summary for a given NetCDF file.
111+
112+
Args:
113+
nc_filename (str): Name of the NetCDF file.
114+
115+
Returns:
116+
dict: Summary statistics for the file, including:
117+
- "flag_counts" (dict): Flag count statistics.
118+
"""
82119
if self.nc is None:
83120
self.load_file(nc_filename)
84121
result = {}
@@ -87,7 +124,17 @@ def nc_file_summary(self, nc_filename: str) -> dict:
87124
return result
88125

89126
def flag_counts(self, QC_flags: list = None) -> dict:
90-
"""Returns pandas dataframe of counts of flag variables for the entire station record."""
127+
"""Computes counts of flag variables for the entire station record.
128+
129+
Args:
130+
QC_flags (list, optional): List of quality-control flag variable names.
131+
Defaults to `self.QC_flags`.
132+
133+
Returns:
134+
dict: A dictionary containing:
135+
- "totals" (dict[str, pandas.DataFrame]): Total counts per flag.
136+
- "by_month" (dict[str, pandas.DataFrame]): Monthly counts per flag.
137+
"""
91138
result = {"totals": {}, "by_month": {}}
92139
if not QC_flags:
93140
QC_flags = self.QC_flags
@@ -100,11 +147,26 @@ def flag_counts(self, QC_flags: list = None) -> dict:
100147
return result
101148

102149
def total_count(self, cat_var) -> pd.DataFrame:
103-
"""Returns count totals for a given flag variable."""
150+
"""Counts totals for a given categorical flag variable.
151+
152+
Args:
153+
cat_var (pandas.Categorical): Categorical flag variable.
154+
155+
Returns:
156+
pandas.DataFrame: DataFrame with counts grouped by category.
157+
"""
104158
return pd.DataFrame({"cnt": cat_var}).groupby(cat_var).count()
105159

106160
def by_month_count(self, cat_var, dim: str) -> pd.DataFrame:
107-
"""Returns pandas dataframe of Counts by month for a given flag variable."""
161+
"""Counts observations by month for a given flag variable.
162+
163+
Args:
164+
cat_var (pandas.Categorical): Categorical flag variable.
165+
dim (str): Dimension name prefix for the time variable.
166+
167+
Returns:
168+
pandas.DataFrame: DataFrame with counts grouped by month and flag value.
169+
"""
108170
df = pd.DataFrame(
109171
{"cnt": cat_var}, index=pd.to_datetime(self.data[dim + "Time"], unit="s")
110172
)

0 commit comments

Comments
 (0)