Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AdfData class for streamlined I/O in plotting scripts #269

Merged
merged 52 commits into from
Jul 11, 2024

Conversation

brianpm
Copy link
Collaborator

@brianpm brianpm commented Dec 1, 2023

This PR introduces a new class called ADFData in lib/adf_dataset.py. This class is instantiated with an ADFDiag instance. It is intended to hold most of the boilerplate code that is in the plotting scripts to set paths and load data. It also abstracts the treatment of "obs" and "baseline" into something that looks more unified (at least to the plotting scripts). The upshot is that using this class can reduce the amount of code in plotting scripts. As examples, I've added zonal_mean_B.py and global_latlon_map_B.py. These are refactors of the current versions that now use ADFData, and both are quite a bit shorter in the B versions. In limited testing, I think they are producing the same output.

NOTE I see that create_climo_files.py also appears to be changed here. I don't know why. That should not be part of this PR... I did a sync with ADF so I thought I was completely up to date. I think the changes in that file were developed to help @justin-richling with an issue with the climatology years. The code looks right, but I wasn't intending it to be part of this PR. (It can be if we double check it.)

@nusbaume nusbaume self-requested a review May 10, 2024 19:54
@nusbaume nusbaume added code clean-up Made code simpler and/or easier to read. plotting Related to plot generation labels May 10, 2024
Copy link
Collaborator

@nusbaume nusbaume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic cleanup! However, I did have some change requests, specifically about moving the new AdfData object to inside the AdfDiag class, and simply updating the current plotting scripts instead of adding new *_B.py scripts.

Otherwise the rest of my change requests should be pretty minor, although feel free to push back if something ends up being more difficult than expected. Thanks!

lib/adf_dataset.py Outdated Show resolved Hide resolved
lib/adf_dataset.py Outdated Show resolved Hide resolved
lib/adf_dataset.py Outdated Show resolved Hide resolved
lib/adf_dataset.py Outdated Show resolved Hide resolved
lib/adf_dataset.py Outdated Show resolved Hide resolved
scripts/plotting/global_latlon_map_B.py Outdated Show resolved Hide resolved
scripts/plotting/global_latlon_map_B.py Outdated Show resolved Hide resolved
scripts/plotting/zonal_mean_B.py Outdated Show resolved Hide resolved
scripts/plotting/zonal_mean_B.py Outdated Show resolved Hide resolved
scripts/plotting/zonal_mean_B.py Outdated Show resolved Hide resolved
@brianpm
Copy link
Collaborator Author

brianpm commented Jun 12, 2024

@nusbaume -- Okay. I think I've got an updated version that is working.

I didn't rename the scripts because I'd like @justin-richling to confirm that the plots are actually working correctly.

In my limited testing using recent development runs, I had to also update adf_info.py because of the history file changes. So now it checks for time_bnds OR time_bounds and uses the hist_str to look for time series files. Feel free to exclude these changes is they are being handled by a different PR.

I also ran into the issue where doing the derived time series was hanging, so I added .compute() in adf_diag.py. If there's a more robust fix for that, this change can also be ignored.

Copy link
Collaborator

@nusbaume nusbaume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks good to me now! I have a few, hopefully trivial, change requests, but I trust that hey can be resolved without a re-review from me. I also left the one comment un-resolved about replacing the lat-lon and zonal mean plots, as I figured we won't want to do this until @justin-richling has successfully tested the new routines.

Finally, my hope is that if we can get PR #310 merged in soon, and then merge this PR to the head of the ADF, that it will fix the current test failures. Of course if that turns out not to be the case then I am happy to take on getting those tests fixed before this PR is officially brought in.

Thanks!

@@ -131,7 +129,7 @@ def load_climo_file(self, case, variablename):
"""Return Dataset for climo of variablename"""
fils = self.get_climo_file(case, variablename)
if not fils:
print(f"ERROR: Did not find climo file for variable: {variablename}. Will try to skip.")
warnings.warning(f"ERROR: Did not find climo file for variable: {variablename}. Will try to skip.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (?) this should be warn not warning:

Suggested change
warnings.warning(f"ERROR: Did not find climo file for variable: {variablename}. Will try to skip.")
warnings.warn(f"ERROR: Did not find climo file for variable: {variablename}. Will try to skip.")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Fixed it.

lib/adf_info.py Outdated
Comment on lines 692 to 694
except:
print(" ----------- ERROR ------------")
print(ts_files)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is OK for now, but do you remember what the specific error was that prompted this try/except? In general we want to try and check for a specific type of exception, otherwise it could catch potential errors that we actually want to be exposed (for example if the computer itself has a system/hardware error).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this is a remnant of me trying to do testing. I'm taking it out. I was hitting a problem where multiple time series files had been generated for case (and put in the same directory). It resulted in having multiple files identified in the search that couldn't be concatenated because they had overlapping times. Maybe that needs to be revisited sometime, but I don't think this check needs to be included here.

@@ -83,7 +82,7 @@ def global_latlon_map_B(adfobj):
#
# Use ADF api to get all necessary information
#
data = AdfData(adfobj)
# data = AdfData(adfobj) NO LONGER NEEDED
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming this script works as expected I would just delete this line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -47,7 +46,7 @@ def zonal_mean_B(adfobj):

# Start by instantiating the AdfData object
# and Extract needed quantities from ADF object:
data = AdfData(adfobj)
# data = AdfData(adfobj)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming this script works as expected I would just delete this line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@nusbaume nusbaume requested a review from justin-richling June 16, 2024 22:03
@justin-richling
Copy link
Collaborator

@brianpm Looks like the plots for Lat/Lon and Zonal are the same between the old way and this way. Thanks for the clean up of the code!

Copy link
Collaborator

@justin-richling justin-richling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks for the work on this!

@brianpm
Copy link
Collaborator Author

brianpm commented Jun 21, 2024

Okay, I think I've got everything in. It says there's conflicts with adf_info.py, but I think we want this version (@justin-richling took a look on my previous commit and noted that we would want a couple of the changes that I had accidentally removed that affect hist_str and time_bounds).

@justin-richling justin-richling merged commit f8a8959 into NCAR:main Jul 11, 2024
7 checks passed
@justin-richling justin-richling mentioned this pull request Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code clean-up Made code simpler and/or easier to read. plotting Related to plot generation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants