diff --git a/Depth2SWE.ipynb b/Depth2SWE.ipynb index 96fd2a8..2d4d253 100644 --- a/Depth2SWE.ipynb +++ b/Depth2SWE.ipynb @@ -5,6 +5,9 @@ "metadata": {}, "source": [ "# SWE Calculator \n", + "\n", + "This is a python implementation of a matlab script by D. Hill. See full details at: Hill, D.F., Burakowski, E.A., Crumley, R.L., Keon, J., Hu, J.M., Arendt, A.A., Wikstrom Jones, K., Wolken G.J., 2019, \"Converting snow depth to snow water equivalent using climatological variables,\" The Cryosphere, v.13, pp.1767-1784 https://doi.org/10.5194/tc-13-1767-2019\n", + "\n", "This script implements a power law regression in order to produce a value of snow water equivalent (SWE) based on various parameters associated with a snow depth measurement. The input variables can be scalars (single measurement) or vectors (batch measurements). The following information on units, etc., is critical. To use this script, edit inputs with your data.\n", "\n", "Christina Aragon, Oregon State University \n", @@ -43,64 +46,53 @@ "from scipy.interpolate import interp2d\n", "from datetime import date\n", "\n", - "#***Edit input data***. Input variables can be a list or array. \n", - "#Below is an example of manually entered inputs, or data can be imported from .txt, .csv, etc. \n", - "Y = [2018,2018,2018]\n", - "M = [1,11,3]\n", - "D = [1,1,1]\n", - "H = [30,40,50]\n", - "LAT = [43.5,43.5,43.5]\n", - "LON = [-110.8,-110.8,-110.8]\n", - "\n", - "#Build lat/lon array \n", - "ncols = 7300\n", - "nrows = 2839\n", - "xll = -168.00051894775\n", - "yll = 30.002598288104\n", - "clsz = 0.014795907586\n", - "#Longitudes\n", - "ln = np.arange(xll, xll+ncols*clsz, clsz)\n", - "#Latitudes\n", - "lt = np.arange(yll, yll+nrows*clsz, clsz)\n", - "la = np.flipud(lt)\n", - "\n", - "#Import temperature difference data\n", - "geo = gdal.Open('td_final.txt')\n", - "td = geo.ReadAsArray()\n", - "#Import winter precipitation data\n", - "geo = gdal.Open('ppt_wt_final.txt')\n", - "pptwt = geo.ReadAsArray()\n", - " \n", "def swe_calc(Y,M,D,H,LAT,LON):\n", - " #Interpolate temp to input\n", - " f_td = interp2d(ln, la, td, kind='cubic')\n", - " TD = f_td(LON, LAT)\n", - " #Interpolate temp to input\n", - " f_ppt = interp2d(ln, la, pptwt, kind='cubic')\n", - " PPTWT = f_ppt(LON, LAT)\n", - " #Determine day of year\n", - " DOY = date.toordinal(date(Y,M,D))-date.toordinal(date(Y,9,30))\n", - " if DOY < 0:\n", - " DOY = DOY+365\n", - " #Apply regression equation \n", - " a = [0.0533,0.948,0.1701,-0.1314,0.2922] #accumulation phase\n", - " b = [0.0481,1.0395,0.1699,-0.0461,0.1804]; #ablation phase\n", - " SWE = a[0]*H**a[1]*PPTWT**a[2]*TD**a[3]*DOY**a[4]* \\\n", - " (-np.tanh(.01*(DOY-180))+1)/2 + b[0]*H**b[1]* \\\n", - " PPTWT**b[2]*TD**b[3]*DOY**b[4] * (np.tanh(.01*(DOY-180))+1)/2;\n", - " return SWE,DOY\n", + " #Build lat/lon array \n", + " ncols = 7300\n", + " nrows = 2839\n", + " xll = -168.00051894775\n", + " yll = 30.002598288104\n", + " clsz = 0.014795907586\n", + " #Longitudes\n", + " ln = np.arange(xll, xll+ncols*clsz, clsz)\n", + " #Latitudes\n", + " lt = np.arange(yll, yll+nrows*clsz, clsz)\n", + " la = np.flipud(lt)\n", + "\n", + " #Import temperature difference data\n", + " geo = gdal.Open('td_final.txt')\n", + " td = geo.ReadAsArray()\n", + " #Import winter precipitation data\n", + " geo = gdal.Open('ppt_wt_final.txt')\n", + " pptwt = geo.ReadAsArray()\n", "\n", - "#Create output arrays \n", - "SWE = np.zeros(len(H))*np.nan\n", - "DOY = np.zeros(len(H))*np.nan\n", - "for i in np.arange(len(H)):\n", - " swe, doy = swe_calc(Y[i],M[i],D[i],H[i],LAT[i],LON[i])\n", - " SWE[i] = swe\n", - " DOY[i] = doy\n", + " def calc(Y,M,D,H,LAT,LON):\n", + " #Interpolate temp to input\n", + " f_td = interp2d(ln, la, td, kind='cubic')\n", + " TD = f_td(LON, LAT)\n", + " #Interpolate temp to input\n", + " f_ppt = interp2d(ln, la, pptwt, kind='cubic')\n", + " PPTWT = f_ppt(LON, LAT)\n", + " #Determine day of year\n", + " DOY = date.toordinal(date(Y,M,D))-date.toordinal(date(Y,9,30))\n", + " if DOY < 0:\n", + " DOY = DOY+365\n", + " #Apply regression equation \n", + " a = [0.0533,0.948,0.1701,-0.1314,0.2922] #accumulation phase\n", + " b = [0.0481,1.0395,0.1699,-0.0461,0.1804]; #ablation phase\n", + " SWE = a[0]*H**a[1]*PPTWT**a[2]*TD**a[3]*DOY**a[4]*(-np.tanh(.01*\\\n", + " (DOY-180))+1)/2 + b[0]*H**b[1]*PPTWT**b[2]*TD**b[3]*DOY**b[4]*\\\n", + " (np.tanh(.01*(DOY-180))+1)/2;\n", + " return SWE,DOY\n", "\n", - "#Optional: print output SWE and DOY \n", - "#print(SWE)\n", - "#print(DOY)" + " #Create output arrays \n", + " SWE = np.zeros(len(H))*np.nan\n", + " DOY = np.zeros(len(H))*np.nan\n", + " for i in np.arange(len(H)):\n", + " swe, doy = calc(Y[i],M[i],D[i],H[i],LAT[i],LON[i])\n", + " SWE[i] = swe\n", + " DOY[i] = int(doy)\n", + " return SWE, DOY" ] } ], @@ -120,9 +112,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.8.3" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/Depth2SWE.py b/Depth2SWE.py index 5ce89fb..6bba9a6 100644 --- a/Depth2SWE.py +++ b/Depth2SWE.py @@ -1,19 +1,21 @@ #!/usr/bin/env python # coding: utf-8 -#################### SWE Calculator #################### +# # SWE Calculator +# +# This is a python implementation of a matlab script by D. Hill. See full details at: Hill, D.F., Burakowski, E.A., Crumley, R.L., Keon, J., Hu, J.M., Arendt, A.A., Wikstrom Jones, K., Wolken G.J., 2019, "Converting snow depth to snow water equivalent using climatological variables," The Cryosphere, v.13, pp.1767-1784 https://doi.org/10.5194/tc-13-1767-2019 +# # This script implements a power law regression in order to produce a value of snow water equivalent (SWE) based on various parameters associated with a snow depth measurement. The input variables can be scalars (single measurement) or vectors (batch measurements). The following information on units, etc., is critical. To use this script, edit inputs with your data. - +# # Christina Aragon, Oregon State University # December 2019 -####################################################################### - -#################### Data #################### - -# Gridded winter precipitation (PPTWT) and temperature difference (TD) data is used in this script. See https://adaptwest.databasin.org/pages/adaptwest-climatena for info on data. See http://www.cacpd.org.s3-website-us-west-2.amazonaws.com/climate_normals/NA_ReadMe.txt for specific metadata. +# +# ## Data +# Gridded winter precipitation (PPTWT) and temperature difference (TD) data is used in this script. See https://adaptwest.databasin.org/pages/adaptwest-climatena for info on data. See http://www.cacpd.org.s3-website-us-west-2.amazonaws.com/climate_normals/NA_ReadMe.txt for specific metadata. +# # The grids that are included with this calculator have been put into geographic coords and they have been clipped to (72>lat>30) and (-168