Skip to content

Commit

Permalink
Merge pull request #1 from btschwertfeger/vectorized-computation
Browse files Browse the repository at this point in the history
Vectorized computation
  • Loading branch information
btschwertfeger authored Oct 21, 2022
2 parents c575947 + 8ed1002 commit 2041890
Show file tree
Hide file tree
Showing 12 changed files with 381 additions and 296 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"cppStandard": "c++11",
"intelliSenseMode": "macos-gcc-x64"
}
],
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Bias-Adjustment-Cpp

[![Generic badge](https://img.shields.io/badge/license-MIT-green.svg)](https://shields.io/)
<div style="text-align: center">

[![GitHub](https://badgen.net/badge/icon/github?icon=github&label)](https://github.com/btschwertfeger/Bias-Adjustment-Cpp)
[![Generic badge](https://img.shields.io/badge/license-MIT-green.svg)](https://shields.io/)

</div>
Collection of different scale- and distribution-based bias adjustment techniques for climatic research. Many of these methods have also been implemented in Python. This can be found here: https://github.com/btschwertfeger/Bias-Adjustment-Python.

____
Expand Down
1 change: 1 addition & 0 deletions cmake/modules/FindNetCDFCxx.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# netCDFCxx_INCLUDE_DIRS - path of netCDFCxx includes
# netCDFCxx_LIBRARIES - netCDFCxx libraries

message (STATUS "searching netCDFCxx...")
find_package(netCDF REQUIRED)
find_program(NCXX4_CONFIG "ncxx4-config")

Expand Down
8 changes: 4 additions & 4 deletions example_adjust.run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ declare -a quant_methods=("quantile_mapping" "quantile_delta_mapping")
{ setopt KSH_ARRAYS || : ; } 2> /dev/null

tmp_path=${work_dir}/tmp/$(uuidgen | tr '[:upper:]' '[:lower:]' | head -c 12)
tmp_obs="${tmp_path}/obs"
tmp_ref="${tmp_path}/ref"
tmp_contr="${tmp_path}/contr"
tmp_scen="${tmp_path}/scen"
tmp_results="${tmp_path}/results"

mkdir -p $tmp_obs
mkdir -p $tmp_ref
mkdir -p $tmp_contr
mkdir -p $tmp_scen
mkdir -p $tmp_results

declare -a datasets=("${observations}" "${control}" "${scenario}")
declare -a ds_paths=("${tmp_obs}" "${tmp_contr}" "${tmp_scen}")
declare -a ds_paths=("${tmp_ref}" "${tmp_contr}" "${tmp_scen}")

# * -------------------------------------------------------------------
# * Computation
Expand All @@ -82,7 +82,7 @@ for method in "${month_methods[@]}"; do
# ? Apply scaling-based bias adjustment per month
for (( month=1; month<13; month++ )); do
$exec_file \
--ref "${tmp_obs}/${month}.nc" \
--ref "${tmp_ref}/${month}.nc" \
--contr "${tmp_contr}/${month}.nc" \
--scen "${tmp_scen}/${month}.nc" \
-v $variable \
Expand Down
83 changes: 41 additions & 42 deletions examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@
{
"cell_type": "code",
"execution_count": 7,
"id": "8527dadf-3928-4cc1-8ac2-b7ca30afb7ad",
"id": "9dc582c9-acc7-40c9-9cbc-90467b6bd5c9",
"metadata": {},
"outputs": [],
"source": [
"float* obs_one_loc = new float[ds_obs.n_time]; \n",
"float* simh_one_loc = new float[ds_simh.n_time];\n",
"float* simp_one_loc = new float[ds_simp.n_time];\n",
"std::vector<float> v_obs_one_loc(ds_obs.n_time); // observations (control period)\n",
"std::vector<float> v_simh_one_loc(ds_obs.n_time); // simulated (control period)\n",
"std::vector<float> v_simp_one_loc(ds_obs.n_time); // simulated (scenario period)\n",
"\n",
"float* ls_result = new float[ds_simp.n_time];\n",
"float* qdm_result = new float[ds_simp.n_time];"
"std::vector<float> v_ls_result(ds_obs.n_time); // Lienar Scaling result\n",
"std::vector<float> v_qdm_result(ds_obs.n_time); // Quantile Delta Mapping result"
]
},
{
Expand All @@ -132,9 +132,9 @@
"outputs": [],
"source": [
"// select time series\n",
"ds_obs.fill_timeseries_for_location(obs_one_loc, 0, 0);\n",
"ds_simh.fill_timeseries_for_location(simh_one_loc, 0, 0);\n",
"ds_simp.fill_timeseries_for_location(simp_one_loc, 0, 0);"
"ds_obs.fill_timeseries_for_location(v_obs_one_loc, 0, 0);\n",
"ds_simh.fill_timeseries_for_location(v_simh_one_loc, 0, 0);\n",
"ds_simp.fill_timeseries_for_location(v_simp_one_loc, 0, 0);"
]
},
{
Expand All @@ -153,7 +153,7 @@
"outputs": [],
"source": [
"// select metotds \n",
"CM_Func_ptr_simple apply_adjustment_ls = CMethods::get_cmethod_simple(\"linear_scaling\");\n",
"CM_Func_ptr_scaling apply_adjustment_ls = CMethods::get_cmethod_scaling(\"linear_scaling\");\n",
"CM_Func_ptr_quantile apply_adjustment_qdm = CMethods::get_cmethod_quantile(\"quantile_delta_mapping\");"
]
},
Expand All @@ -174,11 +174,10 @@
"source": [
"// apply linear scaling\n",
"apply_adjustment_ls(\n",
" ls_result, \n",
" obs_one_loc, \n",
" simh_one_loc, \n",
" simp_one_loc, \n",
" ds_simp.n_time,\n",
" v_ls_result, \n",
" v_obs_one_loc, \n",
" v_simh_one_loc, \n",
" v_simp_one_loc, \n",
" \"+\"\n",
")"
]
Expand All @@ -192,11 +191,10 @@
"source": [
"// apply quantile delta mapping\n",
"apply_adjustment_qdm(\n",
" qdm_result, \n",
" obs_one_loc, \n",
" simh_one_loc, \n",
" simp_one_loc, \n",
" ds_simp.n_time,\n",
" v_qdm_result, \n",
" v_obs_one_loc, \n",
" v_simh_one_loc, \n",
" v_simp_one_loc, \n",
" \"+\",\n",
" 100\n",
")"
Expand Down Expand Up @@ -235,7 +233,7 @@
}
],
"source": [
"for(unsigned ts = start; ts < end; ts++) std::cout << obs_one_loc[ts] << \" \";"
"for(unsigned ts = start; ts < end; ts++) std::cout << v_obs_one_loc[ts] << \" \";"
]
},
{
Expand All @@ -253,7 +251,7 @@
}
],
"source": [
"std::cout << MyMath::mean(obs_one_loc, ds_obs.n_time);"
"std::cout << MyMath::mean(v_obs_one_loc);"
]
},
{
Expand All @@ -279,7 +277,7 @@
}
],
"source": [
"for(unsigned ts = start; ts < end; ts++) std::cout << simh_one_loc[ts] << \" \";"
"for(unsigned ts = start; ts < end; ts++) std::cout << v_simh_one_loc[ts] << \" \";"
]
},
{
Expand All @@ -297,7 +295,7 @@
}
],
"source": [
"std::cout << MyMath::mean(simh_one_loc, ds_simh.n_time);"
"std::cout << MyMath::mean(v_simh_one_loc);"
]
},
{
Expand All @@ -323,7 +321,7 @@
}
],
"source": [
"for(unsigned ts = start; ts < end; ts++) std::cout << simp_one_loc[ts] << \" \";"
"for(unsigned ts = start; ts < end; ts++) std::cout << v_simp_one_loc[ts] << \" \";"
]
},
{
Expand All @@ -341,7 +339,7 @@
}
],
"source": [
"std::cout << MyMath::mean(simp_one_loc, ds_simp.n_time);"
"std::cout << MyMath::mean(v_simp_one_loc);"
]
},
{
Expand All @@ -368,7 +366,7 @@
}
],
"source": [
"for(unsigned ts = 0; ts < 10; ts++) std::cout << ls_result[ts] << \" \";"
"for(unsigned ts = 0; ts < 10; ts++) std::cout << v_ls_result[ts] << \" \";"
]
},
{
Expand All @@ -386,7 +384,7 @@
}
],
"source": [
"std::cout << MyMath::mean(ls_result, ds_simp.n_time);"
"std::cout << MyMath::mean(v_ls_result);"
]
},
{
Expand All @@ -412,7 +410,7 @@
}
],
"source": [
"for(unsigned ts = 0; ts < 10; ts++) std::cout << qdm_result[ts] << \" \";"
"for(unsigned ts = 0; ts < 10; ts++) std::cout << v_qdm_result[ts] << \" \";"
]
},
{
Expand All @@ -430,7 +428,7 @@
}
],
"source": [
"std::cout << MyMath::mean(qdm_result, ds_simp.n_time);"
"std::cout << MyMath::mean(v_qdm_result);"
]
},
{
Expand All @@ -454,31 +452,32 @@
"* All time attributes are missing here, so one have to set them using some other variable.\n",
"*/\n",
"NcFileHandler ncSaver;\n",
"ncSaver.to_netcdf(\"qdm_result.nc\", variable_name, qdm_result, ds_simp.n_time) // n_time should be length of qdm_result"
"ncSaver.to_netcdf(\"qdm_result.nc\", variable_name, v_qdm_result) // n_time should be length of qdm_result"
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 25,
"id": "101f92fa-29a6-42fc-86f6-a4614c73e780",
"metadata": {},
"outputs": [],
"source": [
"// Saving can also be done by using the ds_simp instance to copy all time attributes and values to the full adjusted time series \n",
"ds_simp.to_netcdf(\"qdm_result.nc\", variable_name, qdm_result)"
"ds_simp.to_netcdf(\"qdm_result.nc\", variable_name, v_qdm_result)"
]
},
{
"cell_type": "markdown",
"id": "9e53e2b8-d7fc-4cc8-b52b-14ca4e5b7cda",
"metadata": {},
"source": [
"# 4. More usage examples"
"# 4. More usage examples based on Unidata Program Center's NetCDF data strucutres \n",
"References: http://doi.org/10.5065/D6H70CW6"
]
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 26,
"id": "c6563442-e2be-4f64-bc0e-494e79eb4646",
"metadata": {},
"outputs": [
Expand All @@ -496,7 +495,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 27,
"id": "73227a54-95c6-40d5-bc22-5537e0cfe60b",
"metadata": {},
"outputs": [
Expand All @@ -514,7 +513,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 28,
"id": "7f449f18-9bbb-4e0e-8e8f-fb78e6fc9bb8",
"metadata": {},
"outputs": [
Expand All @@ -532,7 +531,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 29,
"id": "d1bdcab5-edb7-41d7-b038-b14b260d7c2b",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -568,17 +567,17 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 30,
"id": "67e00d69-81a9-4a1f-8001-4ec1ddd43ee0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{ \"calendar\" => @0x6000020ec9d8, \"units\" => @0x6000020ec7a8 }"
"{ \"calendar\" => @0x60000089f1a8, \"units\" => @0x60000089f138 }"
]
},
"execution_count": 29,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -589,7 +588,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 31,
"id": "093c24e1-3e99-4c56-a450-207ffa00e4b7",
"metadata": {},
"outputs": [
Expand Down
20 changes: 10 additions & 10 deletions include/CMethods.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@

#include <iostream>

typedef void (*CM_Func_ptr_simple)(float* output, float* ref, float* contr, float* scen, unsigned n_time, std::string kind);
typedef void (*CM_Func_ptr_quantile)(float* output, float* ref, float* contr, float* scen, unsigned n_time, std::string kind, unsigned n_quantiles);
typedef void (*CM_Func_ptr_scaling)(std::vector<float>& v_output, std::vector<float>& v_reference, std::vector<float>& v_control, std::vector<float>& v_scenario, std::string kind);
typedef void (*CM_Func_ptr_quantile)(std::vector<float>& v_output, std::vector<float>& v_reference, std::vector<float>& v_control, std::vector<float>& v_scenario, std::string kind, unsigned n_quantiles);

class CMethods {
public:
CMethods();
~CMethods();

static CM_Func_ptr_simple get_cmethod_simple(std::string method_name);
static CM_Func_ptr_scaling get_cmethod_scaling(std::string method_name);
static CM_Func_ptr_quantile get_cmethod_quantile(std::string method_name);

static std::vector<std::string> simple_method_names;
static std::vector<std::string> scaling_method_names;
static std::vector<std::string> quantile_method_names;
static std::vector<std::string> all_method_names;

static void Linear_Scaling(float* output, float* reference, float* control, float* scenario, unsigned, std::string n_time);
static void Variance_Scaling(float* output, float* reference, float* control, float* scenario, unsigned, std::string n_time);
static void Delta_Method(float* output, float* reference, float* control, float* scenario, unsigned, std::string n_time);
static void Linear_Scaling(std::vector<float>& v_output, std::vector<float>& v_reference, std::vector<float>& v_control, std::vector<float>& v_scenario, std::string kind);
static void Variance_Scaling(std::vector<float>& v_output, std::vector<float>& v_reference, std::vector<float>& v_control, std::vector<float>& v_scenario, std::string kind);
static void Delta_Method(std::vector<float>& v_output, std::vector<float>& v_reference, std::vector<float>& v_control, std::vector<float>& v_scenario, std::string kind);

static std::vector<double> get_xbins(float* a, float* b, unsigned n_quantiles, unsigned length, std::string kind);
static void Quantile_Mapping(float* output, float* reference, float* control, float* scenario, unsigned n_time, std::string kind, unsigned n_quantiles);
static void Quantile_Delta_Mapping(float* output, float* reference, float* control, float* scenario, unsigned n_time, std::string kind, unsigned n_quantiles);
static std::vector<double> get_xbins(std::vector<float>& a, std::vector<float>& b, unsigned n_quantiles, std::string kind);
static void Quantile_Mapping(std::vector<float>& v_output, std::vector<float>& v_reference, std::vector<float>& v_control, std::vector<float>& v_scenario, std::string kind, unsigned n_quantiles);
static void Quantile_Delta_Mapping(std::vector<float>& v_output, std::vector<float>& v_reference, std::vector<float>& v_control, std::vector<float>& v_scenario, std::string kind, unsigned n_quantiles);
};

#endif
Loading

0 comments on commit 2041890

Please sign in to comment.