-
Notifications
You must be signed in to change notification settings - Fork 2
Add PICASO CK table #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add PICASO CK table #130
Changes from all 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| #include "picaso_ck.hpp" | ||
|
|
||
| #include <harp/math/interpolation.hpp> | ||
| #include <harp/utils/netcdf_opacity_utils.hpp> | ||
|
|
||
| namespace harp { | ||
|
|
||
| PicasoCKImpl::PicasoCKImpl(OpacityOptions const& options_) : options(options_) { | ||
| TORCH_CHECK(options->opacity_files().size() == 1, | ||
| "Only one opacity file is allowed"); | ||
| TORCH_CHECK(options->type().empty() || options->type() == "picaso-ck", | ||
| "Mismatch opacity type: ", options->type(), | ||
| " expecting 'picaso-ck'"); | ||
| reset(); | ||
| } | ||
|
|
||
| void PicasoCKImpl::reset() { | ||
| #ifdef NETCDFOUTPUT | ||
| int fileid = open_file(options->opacity_files()[0]); | ||
|
|
||
| auto qwave = | ||
| read_tensor_permuted(fileid, "quadrature_wavenumber", {"band", "gpoint"}); | ||
| auto qweight = | ||
| read_tensor_permuted(fileid, "quadrature_weight", {"band", "gpoint"}); | ||
| auto lower = read_1d_variable(fileid, "wavenumber_lower"); | ||
| auto upper = read_1d_variable(fileid, "wavenumber_upper"); | ||
| auto ng = qwave.size(1); | ||
|
|
||
| wavenumber = qwave.flatten(); | ||
| weights = qweight.flatten(); | ||
| wave_lower = lower.unsqueeze(1).expand({lower.size(0), ng}).flatten(); | ||
| wave_upper = upper.unsqueeze(1).expand({upper.size(0), ng}).flatten(); | ||
| ln_pressure = read_1d_variable(fileid, "pressure").log(); | ||
| temperature_anomaly = read_1d_variable(fileid, "temperature_offset"); | ||
| ln_temperature_base = | ||
| read_1d_variable(fileid, "nominal_temperature").log().unsqueeze(-1); | ||
| ln_sigma_cross = | ||
| read_tensor_permuted(fileid, "kappa", | ||
| {"band", "gpoint", "pressure", "temperature_offset"}) | ||
| .reshape({-1, ln_pressure.size(0), temperature_anomaly.size(0), 1}) | ||
| .clamp_min(1.e-300) | ||
| .log(); | ||
|
|
||
| check_nc(nc_close(fileid), "Failed to close NetCDF file"); | ||
| #else | ||
| TORCH_CHECK(false, "NetCDF support is not enabled"); | ||
| #endif | ||
|
|
||
| register_buffer("wavenumber", wavenumber); | ||
| register_buffer("weights", weights); | ||
| register_buffer("wave_lower", wave_lower); | ||
| register_buffer("wave_upper", wave_upper); | ||
| register_buffer("ln_pressure", ln_pressure); | ||
| register_buffer("temperature_anomaly", temperature_anomaly); | ||
| register_buffer("ln_temperature_base", ln_temperature_base); | ||
| register_buffer("ln_sigma_cross", ln_sigma_cross); | ||
| } | ||
|
|
||
| torch::Tensor PicasoCKImpl::forward( | ||
| torch::Tensor conc, std::map<std::string, torch::Tensor> const& kwargs) { | ||
| int ncol = conc.size(0); | ||
| int nlyr = conc.size(1); | ||
|
|
||
| TORCH_CHECK(kwargs.count("pres") > 0, "pres is required in kwargs"); | ||
| TORCH_CHECK(kwargs.count("temp") > 0, "temp is required in kwargs"); | ||
| auto const& pres = kwargs.at("pres"); | ||
| auto const& temp = kwargs.at("temp"); | ||
|
|
||
| TORCH_CHECK(pres.size(0) == ncol && pres.size(1) == nlyr, | ||
| "Invalid pres shape: ", pres.sizes(), | ||
| "; needs to be (ncol, nlyr)"); | ||
| TORCH_CHECK(temp.size(0) == ncol && temp.size(1) == nlyr, | ||
| "Invalid temp shape: ", temp.sizes(), | ||
| "; needs to be (ncol, nlyr)"); | ||
|
|
||
| auto wave_query = | ||
| kwargs.count("wavenumber") > 0 ? kwargs.at("wavenumber") : wavenumber; | ||
| TORCH_CHECK(wave_query.dim() == 1, "PicasoCK expects a 1D wavenumber grid"); | ||
| auto lnp = pres.log(); | ||
| auto temperature_base = | ||
| interpn({lnp}, {ln_pressure}, ln_temperature_base).squeeze(-1).exp(); | ||
| auto tempa = temp - temperature_base; | ||
| int const nwave = wave_query.size(0); | ||
| auto wave = wave_query.unsqueeze(-1).unsqueeze(-1).expand( | ||
| {nwave, conc.size(0), conc.size(1)}); | ||
| lnp = lnp.unsqueeze(0).expand_as(wave); | ||
| tempa = tempa.unsqueeze(0).expand_as(wave); | ||
| auto sigma = | ||
| interpn({wave, lnp, tempa}, | ||
| {wavenumber, ln_pressure, temperature_anomaly}, ln_sigma_cross) | ||
| .exp(); | ||
|
|
||
| // PICASO pre-mixed molecular opacity [cm^2/mol] times total concentration. | ||
| return 1.e-4 * sigma * conc.sum(-1).unsqueeze(0).unsqueeze(-1); | ||
| } | ||
|
|
||
| } // namespace harp |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #pragma once | ||
|
|
||
| #include <torch/nn/cloneable.h> | ||
| #include <torch/nn/module.h> | ||
| #include <torch/torch.h> | ||
|
|
||
| #include "opacity_options.hpp" | ||
|
|
||
| namespace harp { | ||
|
|
||
| class PicasoCKImpl : public torch::nn::Cloneable<PicasoCKImpl> { | ||
| public: | ||
| torch::Tensor wavenumber, weights, wave_lower, wave_upper; | ||
| torch::Tensor ln_pressure, temperature_anomaly, ln_temperature_base; | ||
| torch::Tensor ln_sigma_cross; | ||
| OpacityOptions options; | ||
|
|
||
| PicasoCKImpl() : options(OpacityOptionsImpl::create()) {} | ||
| explicit PicasoCKImpl(OpacityOptions const& options_); | ||
| void reset() override; | ||
|
|
||
| torch::Tensor forward(torch::Tensor conc, | ||
| std::map<std::string, torch::Tensor> const& kwargs); | ||
| }; | ||
| TORCH_MODULE(PicasoCK); | ||
|
|
||
| } // namespace harp |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.