<jupyter_start><jupyter_text>Download Daymet Daymet provides gridded meteorological data for North American at 1km spatial resolution with daily timestep from 1980 ~ present. [website](https://daac.ornl.gov/cgi-bin/dsviewer.pl?ds_id=1328) and [user guide](https://daac.ornl.gov/DAYMET/guides/Daymet_V3_CFMosaics.html)Available variables:| Variable | Description (units) || ---- | ---- || tmax | Daily maximum 2-meter air temperature (°C) || tmin | Daily minimum 2-meter air temperature (°C) || prcp | Daily total precipitation (mm/day) || srad | Incident shortwave radiation flux density (W/m2) || vp   | Water vapor pressure (Pa) || swe  | Snow water equivalent (kg/m2) || dayl | Duration of the daylight period (seconds/day) |Notes: - The Daymet calendar is based on a standard calendar year. All Daymet years, including leap years, have 1 - 365 days. For leap years, the Daymet database includes leap day (February 29) and values for December 31 are discarded from leap years to maintain a 365-day year.<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 150
import workflow
import workflow.ui
import logging
workflow.ui.setup_logging(1,None)

import numpy as np
import rasterio
import fiona
import workflow.daymet
watershed_shapefile = '../../watershed-workflow/data/hydrologic_units/others/Coweeta/coweeta_basin.shp'<jupyter_output><empty_output><jupyter_text>import watershed<jupyter_code>crs, watershed = workflow.get_split_form_shapes(watershed_shapefile)
logging.info(f'crs: {crs}')

bounds = watershed.exterior()
bounds<jupyter_output>2021-08-30 18:25:24,211 - root - INFO: 
2021-08-30 18:25:24,213 - root - INFO: Loading shapes
2021-08-30 18:25:24,214 - root - INFO: ------------------------------
2021-08-30 18:25:24,215 - root - INFO: Loading file: '../../watershed-workflow/data/hydrologic_units/others/Coweeta/coweeta_basin.shp'
2021-08-30 18:25:24,248 - root - INFO: ... found 1 shapes
2021-08-30 18:25:24,249 - root - INFO: Converting to shapely
2021-08-30 18:25:24,266 - root - INFO: crs: +init=epsg:26917 +type=crs<jupyter_text>Download Daymet returned raw data has `dim(nband, ncol, nrow)`<jupyter_code>startdate = "1-2020" # format "doy-YYYY"
enddate = "365-2020" # format "doy-YYYY"
# setting vars = None to download all available variables
raw, x, y = workflow.daymet.collectDaymet( bounds, start = startdate, end = enddate, 
                                          crs = crs, vars=None)<jupyter_output>2021-08-30 18:25:24,286 - root - INFO: downloading variables: ['tmin', 'tmax', 'prcp', 'srad', 'vp', 'swe', 'dayl']
2021-08-30 18:25:24,370 - root - INFO: Collecting DayMet file to tile bounds: [-83.4885, 35.0173, -83.4117, 35.0838]
2021-08-30 18:25:24,372 - root - INFO:   Using existing: /home/ats_sc_user/short-course/02_model_setup/data/meteorology/daymet/daymet_tmin_2020_35.0838x-83.4885_35.0173x-83.4117.nc
2021-08-30 18:25:24,465 - root - INFO: Collecting DayMet file to tile bounds: [-83.4885, 35.0173, -83.4117, 35.0838]
2021-08-30 18:25:24,466 - root - INFO:   Using existing: /home/ats_sc_user/short-course/02_model_setup/data/meteorology/daymet/daymet_tmax_2020_35.0838x-83.4885_35.0173x-83.4117.nc
2021-08-30 18:25:24,554 - root - INFO: Collecting DayMet file to tile bounds: [-83.4885, 35.0173, -83.4117, 35.0838]
2021-08-30 18:25:24,556 - root - INFO:   Using existing: /home/ats_sc_user/short-course/02_model_setup/data/meteorology/daymet/daymet_prcp_2020_35.0838x-83.4885_35.0173x-8[...]<jupyter_text>Reproject Daymet CRS (optional)Reproject daymet CRS to the same as the watershed. This is necessary if watershed meshes are using watershed CRS that is different from Daymet CRS.<jupyter_code>new_x, new_y, new_extent, new_dat, daymet_profile = workflow.daymet.reproj_Daymet(x, y, raw,  dst_crs = crs)<jupyter_output>/opt/conda/lib/python3.8/site-packages/pyproj/crs/crs.py:1216: UserWarning: You will likely lose important projection information when converting to a PROJ string from another format. See: https://proj.org/faq.html#what-is-the-best-format-for-describing-coordinate-reference-systems
  return self._crs.to_proj4(version=version)
2021-08-30 18:25:25,065 - root - INFO: reprojecting to new crs: +init=epsg:26917 +type=crs
2021-08-30 18:25:25,405 - root - INFO: new profile: {'driver': 'GTiff', 'dtype': 'float32', 'nodata': -9999.0, 'width': 9, 'height': 9, 'count': 365, 'crs': CRS.from_epsg(26917), 'transform': Affine(1269.247147171576, 0.0, 270906.5914069802,
       0.0, -1269.247147171576, 3886667.3291073246), 'tiled': False, 'interleave': 'pixel'}
2021-08-30 18:25:25,406 - root - INFO: new extent[xmin, ymin, xmax, ymax]: (270906.5914069802, 3875244.1047827806, 282329.8157315244, 3886667.3291073246)<jupyter_text>plot Daymet<jupyter_code>variables = ['tmin', 'tmax', 'prcp', 'srad']
# ivar = 'tmax'
islice = 125
fig, axes = plt.subplots(4, 2, figsize = (5, 8))


with fiona.open(watershed_shapefile, mode='r') as fid:
    bnd_profile = fid.profile
    bnd = [r for (i,r) in fid.items()]
daymet_crs = workflow.crs.daymet_crs()
# convert to destination crs
native_crs = workflow.crs.from_fiona(bnd_profile['crs'])
reproj_bnd = workflow.warp.shape(bnd[0], native_crs, daymet_crs)
reproj_bnd_shply = workflow.utils.shply(reproj_bnd)

for i,ivar in zip(range(4),variables):
    
    ax = axes[i, 0]
    iraster = raw[ivar][islice, :, :]
    extent = rasterio.transform.array_bounds(daymet_profile['height'], daymet_profile['width'], daymet_profile['transform']) # (x0, y0, x1, y1)
    plot_extent = extent[0], extent[2], extent[1], extent[3]
    
    ax.matshow(iraster, extent=plot_extent, alpha=1)
    ax.plot(*reproj_bnd_shply.exterior.xy, 'r')
    ax.set_title("Raw Daymet")
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    ax = axes[i, 1]
    iraster = new_dat[ivar][islice, :, :]
    extent = new_extent # (x0, y0, x1, y1)
    plot_extent = extent[0], extent[2], extent[1], extent[3] # (x0, x1, y0, y1)

    # set nodata to NaN to avoid plotting
    iraster[iraster == -9999] = np.nan
    workflow.plot.hucs(watershed, crs, ax=ax, color='r', linewidth=1)
    im = ax.matshow(iraster, extent=plot_extent)
    ax.set_title("Reprojected Daymet")
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    cb = plt.colorbar(im, ax=ax)
    cb.ax.set_ylabel(ivar)
fig.tight_layout()<jupyter_output><empty_output><jupyter_text>save daymet Save raw Daymet data to HDF5 file<jupyter_code>workflow.daymet.writeHDF5(raw, x, y, 
                         workflow.daymet.getAttrs(bounds.bounds, startdate, enddate), 
                         'coweeta_daymet-raw.h5')<jupyter_output>2021-08-30 18:25:26,180 - root - INFO: Writing HDF5 file: coweeta_daymet-raw.h5<jupyter_text>Write to ATS formatThis will write daymet in a format that ATS can read. E.g., this will partition precipitation into rain and snow, convert vapor pressure to relative humidity, get mean air temperature and so on.- dout has dims of `(ntime, nrow, ncol)` or `(ntime, ny, nx)`- input XML file showing how Daymet is used in the model```                                                                                                                                                                                                                                                    ```<jupyter_code>workflow.daymet.writeATS(raw, x, y, 
                         workflow.daymet.getAttrs(bounds.bounds, startdate, enddate), 
                         'coweeta_daymet-ats.h5')<jupyter_output>2021-08-30 18:25:28,523 - root - INFO: Writing ATS file: coweeta_daymet-ats.h5
2021-08-30 18:25:28,527 - root - INFO: input dat shape: (365, 9, 9)
2021-08-30 18:25:28,528 - root - INFO: Converting to ATS met input
{'fraction_non_alphanumeric': 0.13612903225806453, 'fraction_numerical': 0.09987096774193549, 'mean_word_length': 3.6720916214587103, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '72101', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 3339, 'n_tokens_neox': 2804, 'n_words': 650, 'path': '02_model_setup/get_Daymet.ipynb', 'repo_name': 'amanzi/ats-short-course', 'size': '7750'}