Skip to content

Instantly share code, notes, and snippets.

@jiaweih
Created January 12, 2014 06:36
Show Gist options
  • Save jiaweih/8381718 to your computer and use it in GitHub Desktop.
Save jiaweih/8381718 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "PRMS_forcing_prep_14HUC"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "# Import all needed libraries \nimport sys, os, datetime, time\nimport numpy as np\nimport matplotlib.dates as mdates\nimport matplotlib.ticker as mticker",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": "def wateryear(inyear):\n if inyear.month >= 10:\n return inyear.year+1\n else:\n return inyear.year",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": "lat_list, lon_list = np.loadtxt('vic.elevation.bull.run.large.txt', dtype = str, usecols = [0,1], unpack = True)\n\n# Load the time stamp\nyear,month,day = np.loadtxt('../../VIC/forcings_input/bc.5pct/time_data_45.40625_-121.78125', dtype = int, usecols = (0,1,2), unpack=True, ndmin=2)\nlangleys, precips, tmaxs, tmins = [],[],[],[]\nfor i,latlon in enumerate(zip(lat_list,lon_list)):\n original_file = '../../VIC/forcings_input/bc.5pct.buffered/data_'+str(latlon[0])+'_'+str(latlon[1])\n derived_file = 'bc/PRMS_reformat_forcings/derived.5pct.bc.ave.by.day/data_'+str(latlon[0])+'_'+str(latlon[1])+'.date'\n temp_shortwave = np.loadtxt(derived_file, dtype = float, usecols = [6], unpack=True, ndmin=2)\n temp_prec, temp_tmax, temp_tmin = np.loadtxt(original_file, dtype = float, usecols = (0,1,2), unpack=True)\n # Convert solar radiation from W/m^2 to langleys\n # convert tmax and tmin from degree Celcius to degree F\n # convert precipitation in mm/day to inch/day\n temp_langley = temp_shortwave*86400/41840\n prec = temp_prec*0.0393701\n tmax_f = (temp_tmax*1.8)+32\n tmin_f = (temp_tmin*1.8)+32\n if langleys == []:\n langleys = temp_langley.reshape(-1,1)\n precips = prec.reshape(-1,1)\n tmaxs = tmax_f.reshape(-1,1)\n tmins = tmin_f.reshape(-1,1)\n else:\n langleys = np.append(langleys, temp_langley.reshape(-1,1), axis =1)\n precips = np.append(precips, prec.reshape(-1,1), axis =1)\n tmaxs = np.append(tmaxs, tmax_f.reshape(-1,1), axis =1)\n tmins = np.append(tmins, tmin_f.reshape(-1,1), axis =1)\nprint tmaxs.shape, tmins.shape, precips.shape, langleys.shape\nforcing_output = np.concatenate([tmaxs, tmins, precips, langleys], axis = 1)\ndate_output = np.concatenate([year.reshape(-1,1), month.reshape(-1,1), day.reshape(-1,1)], axis=1)\n#print date_output.shape\n#print forcing_output.shape\nnp.savetxt('bc/PRMS_reformat_forcings/datetemp',date_output,fmt='%i')\nnp.savetxt('bc/PRMS_reformat_forcings/datatemp',forcing_output,fmt='%.3f')\ndates = np.loadtxt('bc/PRMS_reformat_forcings/datetemp', dtype = str)\nforcings = np.loadtxt('bc/PRMS_reformat_forcings/datatemp',dtype = str)\nfinaldata = np.append(dates,forcings,axis=1)\nnp.savetxt('bc/PRMS_reformat_forcings/PRMS_forcings_1915_2011',finaldata,fmt='%s')\n# Manually process the output files into the desired date range",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "(35429, 20) (35429, 20) (35429, 20) (35429, 20)\n"
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": "data1 = np.loadtxt('bc/PRMS_reformat_forcings/downsizer_gage_data.prms', skiprows = 17, dtype = str)\ndates = data1[:,0:6] \nrunoff = data1[:,6:]\ndata2 = np.loadtxt('bc/PRMS_reformat_forcings/PRMS_forcings_1960_2011', dtype = str)\nforcing = data2[:,3:]\ntotal_data = np.concatenate([dates, forcing, runoff], axis =1)\nnp.savetxt('bc/PRMS_reformat_forcings/PRMS_datafile_no_header', total_data, fmt = '%s')",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": "print dates.shape\nprint runoff.shape",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "(18993, 6)\n(18993, 7)\n"
}
],
"prompt_number": 47
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment