<jupyter_start><jupyter_text><NAME>'s utility example notebook for 1-d filesContains code to show example uses of the functions I add to utils.py which are designed to work with 1-dimensional (i.e., not lat-lon gridded) CTSM output data.Questions or comments? Email me: sam dot rabin at gmail dot com.  Define useful variablesYou will need to customize these to work with your system and data.<jupyter_code># Your path to ctsm_py directory (i.e., where utils.py lives)
my_ctsm_python_gallery = "/Users/sam/Documents/git_repos/ctsm_python_gallery_myfork/ctsm_py/"

# Directory where input file(s) can be found
indir = "/Volumes/Reacher/CESM_runs/f10_f10_mg37/"

# Either the name of a file within $indir, or a pattern that will return a list of files.
pattern = "*h1.*-01-01-00000.nc"

# List of variables to import from file(s) in $indir matching $pattern. Additional variables will be imported as necessary if they will be useful in gridding any of these. So, e.g., since CPHASE 
myVars = ["CPHASE", \
          "GDDHARV", 
          "GDDPLANT", 
          "GPP", 
          "GRAINC_TO_FOOD", 
          "NPP", 
          "TLAI", 
          "TOTVEGC"]<jupyter_output><empty_output><jupyter_text>Import CTSM utils module<jupyter_code>import sys
sys.path.append(my_ctsm_python_gallery)
import utils<jupyter_output><empty_output><jupyter_text>Import a dataset.- Set `myVars=None` to import all variables.- Currently, the `myVegtypes` argument will import only patches with vegetation types that are managed crops. Set `myVegTypes=None` to import all patches. You can also set `myVegTypes=some_list` to import only patches with any of some arbitrary list of vegetation types.<jupyter_code>import glob

# Get list of all files in $indir matching $pattern
filelist = glob.glob(indir + pattern)

# Import
this_ds = utils.import_ds(filelist, myVars=myVars, myVegtypes=utils.define_mgdcrop_list())
this_ds<jupyter_output><empty_output><jupyter_text>Extract one variable from that dataset<jupyter_code># Which variable? CPHASE = crop development phase.
thisVar = "CPHASE"

thisvar_da = utils.get_thisVar_da(thisVar, this_ds)
thisvar_da<jupyter_output><empty_output><jupyter_text>Make a map of one timestep of some variable- Can also specify `thisTime` as an integer (index on `time` dimension).- (I want to rework this to use `xarray`'s built-in plotting functions.)<jupyter_code># Which variable, which vegetation type, and when? CPHASE = crop development phase.
thisVar = "CPHASE"
thisVegtype = "temperate_corn"
thisTime = "2000-07-01"

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

# Extract and grid the variable
thisVar_da_gridded = utils.grid_one_variable(this_ds, thisVar, time=thisTime)

# Make map
thisVar_da_gridded = thisVar_da_gridded.sel(ivt_str=thisVegtype)
if thisVar_da_gridded.shape[0] == 1:
    thisVar_da_gridded = thisVar_da_gridded.squeeze()
else:
    raise ValueError("You must select one time step to plot")
thisVar_da_gridded = utils.cyclic_dataarray(thisVar_da_gridded)
ax = plt.axes(projection=ccrs.PlateCarree())
plt.pcolor(thisVar_da_gridded.lon.values, thisVar_da_gridded.lat.values, thisVar_da_gridded, transform=ccrs.PlateCarree())
ax.coastlines()
plt.show()<jupyter_output>/Users/sam/Applications/anaconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py:1665: MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3.  Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading'].  This will become an error two minor releases later.
  result = matplotlib.axes.Axes.pcolor(self, *args, **kwargs)
/Users/sam/Applications/anaconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py:388: MatplotlibDeprecationWarning: 
The 'inframe' parameter of draw() was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use Axes.redraw_in_frame() instead. If any parameter follows 'inframe', they should be passed as keyword, not positionally.
  inframe=inframe)
{'fraction_non_alphanumeric': 0.09131293188548864, 'fraction_numerical': 0.011105626850937808, 'mean_word_length': 5.206738131699847, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '20101', 'license': 'Apache-2.0', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1352, 'n_words': 409, 'path': 'notebooks/SamRabin_examples.ipynb', 'repo_name': 'katiedagon/ctsm_py', 'size': '4055'}