This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
MAINT: update munging #7
Open
stsievert
wants to merge
5
commits into
holoviz-topics:carbon_flux_notebook
Choose a base branch
from
stsievert:update-munging
base: carbon_flux_notebook
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,8 @@ | |
"outputs": [], | ||
"source": [ | ||
"# %read in FluxNet CSVs and replace NaNs with zeros\n", | ||
"\n", | ||
"import numpy as np\n", | ||
"import datetime\n", | ||
"import os\n", | ||
"sites = [fname.split('_')[1] for fname in os.listdir('./flux_data/dailies/')]\n", | ||
"\n", | ||
|
@@ -55,24 +56,62 @@ | |
" return None, None\n", | ||
" return float(location['lat']), float(location['lon'])\n", | ||
"\n", | ||
"def _parse_days(integer):\n", | ||
" \"\"\" `integer` date as `20180704` to represent July 4th, 2018\"\"\"\n", | ||
" x = str(integer)\n", | ||
" d = {'year': int(x[:4]), 'month': int(x[4:6]), 'day': int(x[6:])}\n", | ||
" day_of_year = datetime.datetime(d['year'], d['month'], d['day']).timetuple().tm_yday\n", | ||
" return day_of_year\n", | ||
"\n", | ||
"def clean(df, timestamp_col=\"TIMESTAMP\", keep=[], drop=[], predict=''):\n", | ||
" limit = -9990\n", | ||
" for i in range(50):\n", | ||
" df = df.replace(limit - i, np.nan)\n", | ||
" \n", | ||
" assert all([col in df.columns for col in drop])\n", | ||
" df.drop(columns=drop, inplace=True)\n", | ||
" assert not all([col in df.columns for col in drop])\n", | ||
" for col in keep:\n", | ||
" if col not in df.columns:\n", | ||
" if 'SWC_F' in col or 'TS_F' in col:\n", | ||
" df[col] = 0\n", | ||
" \n", | ||
" df = df.fillna(0)\n", | ||
" df['DOY'] = df['TIMESTAMP'].apply(_parse_days) \n", | ||
" df.pop('TIMESTAMP')\n", | ||
" X = df[keep]\n", | ||
" y = df[predict]\n", | ||
" return X, y\n", | ||
"\n", | ||
"def load_fluxnet_site(site):\n", | ||
" # dataRaw(dataRaw <= -9990) = 0/0;\n", | ||
" # dataRaw(dataRaw <= -9990) = 0/0 (is NaN?)\n", | ||
" # NaN -> zero\n", | ||
" prefix = 'FLX_{site}_FLUXNET'.format(site=site)\n", | ||
" filename = [fname for fname in os.listdir('./flux_data/dailies/') if fname.startswith(prefix)][0]\n", | ||
" filenames = [fname for fname in os.listdir('./flux_data/dailies/')\n", | ||
" if fname.startswith(prefix)]\n", | ||
" assert len(filenames) == 1\n", | ||
" filename = filenames[0]\n", | ||
" \n", | ||
" raw_daily = pd.read_csv('./flux_data/dailies/{filename}'.format(filename=filename)) \n", | ||
" derived_cols = ['YEAR','DOY']\n", | ||
" fluxcols = ['P_ERA','TA_ERA','PA_ERA','SW_IN_ERA','LW_IN_ERA'\n", | ||
" ,'WS_ERA','LE_F_MDS','H_F_MDS','NEE_CUT_USTAR50','NEE_VUT_USTAR50',\n", | ||
" 'SWC_F_MDS_1','SWC_F_MDS_2','SWC_F_MDS_3','TS_F_MDS_1','TS_F_MDS_2'\n", | ||
" 'TS_F_MDS_3','VPD_ERA','GPP_DT_VUT_USTAR50','GPP_DT_CUT_USTAR50']\n", | ||
" available = [col for col in fluxcols if col in raw_daily.columns]\n", | ||
" return raw_daily[['TIMESTAMP']+available]\n", | ||
"\n", | ||
"\n", | ||
"def map_to_days(data):\n", | ||
" return data" | ||
" \n", | ||
" keep = ['P_ERA',\n", | ||
" 'TA_ERA',\n", | ||
" 'PA_ERA',\n", | ||
" 'SW_IN_ERA',\n", | ||
" 'LW_IN_ERA',\n", | ||
" 'WS_ERA',\n", | ||
" 'SWC_F_MDS_1', 'SWC_F_MDS_2', 'SWC_F_MDS_3',\n", | ||
" 'TS_F_MDS_1', 'TS_F_MDS_2', 'TS_F_MDS_3',\n", | ||
" 'VPD_ERA',\n", | ||
" 'DOY']\n", | ||
" drop = [\"GPP_DT_VUT_USTAR50\",\n", | ||
" \"GPP_DT_CUT_USTAR50\",\n", | ||
" \"LE_F_MDS\",\n", | ||
" \"H_F_MDS\"]\n", | ||
" predict = [\"NEE_CUT_USTAR50\",\n", | ||
" \"NEE_VUT_USTAR50\"]\n", | ||
" X, y = clean(raw_daily, keep=keep, drop=drop, predict=predict[0])\n", | ||
" return X, y" | ||
] | ||
}, | ||
{ | ||
|
@@ -81,9 +120,10 @@ | |
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"dailies = load_fluxnet_site(sites[0])\n", | ||
"X, y = load_fluxnet_site(sites[0])\n", | ||
"print('Example FLUXNET data for site {site}:'.format(site=sites[0]))\n", | ||
"dailies.head()" | ||
"print('Observations: {}, variables per observation: {}'.format(*X.shape))\n", | ||
"X.describe()" | ||
] | ||
}, | ||
{ | ||
|
@@ -158,7 +198,7 @@ | |
"outputs": [], | ||
"source": [ | ||
"import scipy.io\n", | ||
"rsif = scipy.io.loadmat('RSIF_2007_2016_05N_01L.mat')" | ||
"rsif = scipy.io.loadmat('./flux_data/RSIF_2007_2016_05N_01L.mat')" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I note you've changed where to expect this |
||
] | ||
}, | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment explaining what this bit does (i.e. what the intention is; here and below)?