Skip to content
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

Add python wrapper for juno_mwr #136

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -705,5 +705,5 @@ jobs:
run: python3 combine.py -o test

- name: Test
working-directory: ${{github.workspace}}/examples/2023-jupiter-mwr-eq
working-directory: ${{github.workspace}}/examples/2024-CLi-juno-mwr
run: echo "cheers"
31 changes: 16 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ else()
message(STATUS "")
endif()

# load all modules
LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/)

# load all macros
FILE(GLOB _macro_files "${CMAKE_SOURCE_DIR}/cmake/macros/*.cmake")
FOREACH(_file ${_macro_files})
MESSAGE(STATUS "Include ${_file}")
INCLUDE(${_file})
ENDFOREACH()

## 1. set up compiler flags ##
message(STATUS "1. Set up project compiler flags ...")
message(STATUS "Include ${CMAKE_SOURCE_DIR}/cmake/compilers.cmake")
Expand All @@ -35,10 +43,10 @@ message(STATUS "Load custom task = ${TASK}")
if (NOT ${TASK} STREQUAL "")
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/cmake/examples/${TASK}.cmake)
message(STATUS "Load custom setting - ${TASK} -")
message(STATUS "Include ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${TASK}.cmake")
message(STATUS "Include ${CMAKE_CURRENT_SOURCE_DIR}/cmake/examples/${TASK}.cmake")
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/examples/${TASK}.cmake)
else()
message(FATAL_ERROR "Cannot find custom file cmake/${TASK}.cmake")
message(FATAL_ERROR "Cannot find custom file cmake/examples/${TASK}.cmake")
endif()
set(TASK ${TASK} CACHE STRING "Custom task to be performed")
else()
Expand Down Expand Up @@ -70,24 +78,17 @@ add_subdirectory(src)
add_subdirectory(tools)
add_subdirectory(data)

## 5. set up python binding ##
if (PYTHON_BINDINGS)
add_subdirectory(python)
endif()

## 6. set up unit tests and examples
## 5. set up examples and tests
message(STATUS "6. Set up unit tests")

# load all macros
FILE(GLOB _macro_files "${CMAKE_SOURCE_DIR}/cmake/macros/*.cmake")
FOREACH(_file ${_macro_files})
MESSAGE(STATUS "Include ${_file}")
INCLUDE(${_file})
ENDFOREACH()

add_subdirectory(examples)
add_subdirectory(tests)

## 6. set up python binding ##
if (PYTHON_BINDINGS)
add_subdirectory(python)
endif()

## 7. add uninstall targets ##
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
Expand Down
3 changes: 2 additions & 1 deletion cmake/examples/juno.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ set_if_empty(NTRACER 2)
# canoe configure
set(HYDROSTATIC ON)
set(NETCDF ON)
set(FITS ON)
# set(FITS ON)
set(RadianceSolver lambert)
set(PYTHON_BINDINGS ON)
9 changes: 9 additions & 0 deletions cmake/macros/macro_set_if_empty.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# A small macro used for set a variable if it is empty
#
# Usage: set_if_empty(name)

macro(set_if_empty _variable)
if("${${_variable}}" STREQUAL "")
set(${_variable} ${ARGN})
endif()
endmacro()
6 changes: 0 additions & 6 deletions cmake/parameters.cmake
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
# define default parameters

macro(SET_IF_EMPTY _variable)
if("${${_variable}}" STREQUAL "")
set(${_variable} ${ARGN})
endif()
endmacro()

list(APPEND CMAKE_PREFIX_PATH "/usr/include/mpich-x84_64" "/usr/lib64/mpich/")

# populate the default values
Expand Down
6 changes: 3 additions & 3 deletions examples/2024-CLi-earth-rt/run_ktable_earth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
sys.path.append("../python")
sys.path.append(".")

from pyharp import radiation_band
from utilities import load_configure, find_resource
from canoe.harp import radiation_band
from canoe import load_configure, find_resource
from numpy import *
from rfmlib import *

Expand Down Expand Up @@ -44,7 +44,7 @@ def create_rfm_atmosphere(nlyr: int) -> dict:

band = radiation_band(band_name, info)

nspec = band.get_num_spec_grids()
nspec = band.get_num_specgrids()
wmin, wmax = band.get_range()
wres = (wmax - wmin) / (nspec - 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
# 1. Compile juno_mwr.cpp
setup_problem(juno_mwr)

# PGEN variable will be used in python bindings
set(PGEN ${CMAKE_CURRENT_SOURCE_DIR}/juno_mwr
CACHE INTERNAL "Path to the source file for problem generator")

# 4. Copy input file to run directory
file(GLOB inputs *.inp *.yaml)
file(GLOB inputs *.inp *.yaml *.py)
foreach(input ${inputs})
file(COPY ${input} DESTINATION ${CMAKE_BINARY_DIR}/bin)
# softlink inp files
execute_process(COMMAND ln -sf ${input} ${CMAKE_BINARY_DIR}/bin/${inp})
endforeach()
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#include <opacity/Giants/microwave/mwr_absorbers.hpp>

// inversion
#include <inversion/profile_inversion.hpp>
// #include <inversion/profile_inversion.hpp>

// special includes
#include "juno_mwr_specs.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Ttriple2 = 195.4
Ptriple2 = 6060.

<radiation>
radiation_config = mwr_channels.yaml
radiation_config = juno_mwr.yaml
outdir = (0,) (15,) (30,) (45,)

#<inversion>
Expand Down
64 changes: 64 additions & 0 deletions examples/2024-CLi-juno-mwr/run_juno_mwr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#! /usr/bin/env python3
import sys, os

sys.path.append("../python")
sys.path.append(".")

from canoe import def_species, load_configure
from canoe.snap import def_thermo
from canoe.athena import Mesh, ParameterInput
from typing import Tuple

# from canoe.snap import air_parcel


def construct_atmosphere(
nlyr: int = 100,
comp: dict = {},
plim: Tuple[float, float] = [0.1e5, 100.0e5],
zlim: Tuple[float, float] = [0.0, 10.0e3],
T0: float = 169.0,
P0: float = 1.0e5,
Tmin: float = 100.0,
) -> Mesh:
mb = Mesh(nlyr)

air = air_parcel(comp)
dlnp = (log(plim[1]) - log(plim[0])) / nlyr

mb.set_layer(0, air)
for i in range(1, nlyr):
atm_extrapolate(air, -dlnp / 2.0, method="dry_adiabat")
mb.set_layer(i, air)

return mb


def modify_atmosphere(
mb: Mesh,
comp: dict = {},
plim: Tuple[float, float] = [0.1e5, 100.0e5],
zlim: Tuple[float, float] = [0.0, 10.0e3],
T0: float = 169.0,
P0: float = 1.0e5,
) -> Mesh:
pass


if __name__ == "__main__":
pin = ParameterInput()
pin.load_from_file("juno_mwr.inp")

vapors = pin.get_string("species", "vapor").split(", ")
clouds = pin.get_string("species", "cloud").split(", ")
tracers = pin.get_string("species", "tracer").split(", ")

def_species(vapors=vapors, clouds=clouds, tracers=tracers)
def_thermo(pin)

config = load_configure("juno_mwr.yaml")

print(pin.get_real("problem", "qH2O.ppmv"))

mesh = Mesh(pin)
mesh.initialize(pin)
10 changes: 3 additions & 7 deletions examples/2024-FDing-jupiter-rt/run_disort_jup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
sys.path.append("../python")
sys.path.append(".")

from pyharp import radiation_band, subscribe_species
from utilities import load_configure
from canoe import def_species, load_configure
from canoe.harp import radiation_band
from numpy import linspace, ones, exp
from netCDF4 import Dataset
from pylab import *
Expand Down Expand Up @@ -35,11 +35,7 @@ def create_atmosphere(nlyr: int) -> dict:


if __name__ == "__main__":
subscribe_species(
{
"vapor": ["H2O", "NH3"],
}
)
def_species(vapors=["H2O", "NH3"])

config = load_configure("jupiter_rt.yaml")
band = radiation_band("B1", config, load_opacity=True)
Expand Down
12 changes: 4 additions & 8 deletions examples/2024-FDing-jupiter-rt/run_ktable_jup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
sys.path.append("../python")
sys.path.append(".")

from pyharp import radiation_band, subscribe_species
from utilities import load_configure, find_resource
from canoe import def_species, load_configure, find_resource
from canoe.harp import radiation_band
from netCDF4 import Dataset
from numpy import *
from rfmlib import *
Expand Down Expand Up @@ -40,16 +40,12 @@ def create_rfm_atmosphere(nlyr: int) -> dict:

if __name__ == "__main__":
hitran_file = find_resource("HITRAN2020.par")
subscribe_species(
{
"vapor": ["H2O", "NH3"],
}
)
def_species(vapors=["H2O", "NH3"])

config = load_configure("jupiter_rt.yaml")
band = radiation_band("B1", config)

nspec = band.get_num_spec_grids()
nspec = band.get_num_specgrids()
wmin, wmax = band.get_range()
wres = (wmax - wmin) / (nspec - 1)

Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ else()
endif()

if (${TASK} STREQUAL "juno")
add_subdirectory(2023-jupiter-mwr-eq)
add_subdirectory(2024-CLi-juno-mwr)
endif()

if (${TASK} STREQUAL "saturn_radio")
Expand Down
74 changes: 38 additions & 36 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,41 @@ execute_process(
OUTPUT_STRIP_TRAILING_WHITESPACE
)

macro(add_python_module namel)
# set module targets
pybind11_add_module(${namel} MODULE ${namel}.cpp)

target_include_directories(${namel}
PRIVATE
${CMAKE_BINARY_DIR}
${CPPDISORT_INCLUDE_DIR}
${CANOE_INCLUDE_DIR}
${PYTHON_INCLUDE_DIR}
${PYBIND11_INCLUDE_DIR}
${MPI_CXX_INCLUDE_PATH}
)

target_link_libraries(${namel} PRIVATE
search_path
${CANOE_LIBRARY_${buildu}}
)

set_target_properties(${namel}
PROPERTIES
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}")

# softlink library
execute_process(
COMMAND ln -sf ${CMAKE_BINARY_DIR}/python/${namel}${PYTHON_MODULE_EXTENSION}
${CMAKE_BINARY_DIR}/lib/${namel}${PYTHON_MODULE_EXTENSION}
)
endmacro()

# add python modules
add_python_module(pyathena)
add_python_module(pycanoe)
add_python_module(pyharp)
add_python_module(utilities)
set(namel canoe)
set_if_empty(PGEN empty)

# set module targets
pybind11_add_module(${namel} MODULE
pyathena.cpp
pycanoe.cpp
pyharp.cpp
pysnap.cpp
pyutils.cpp
${PGEN}.cpp
)

target_include_directories(${namel}
PRIVATE
${CMAKE_BINARY_DIR}
${CPPDISORT_INCLUDE_DIR}
${CANOE_INCLUDE_DIR}
${PYTHON_INCLUDE_DIR}
${PYBIND11_INCLUDE_DIR}
${MPI_CXX_INCLUDE_PATH}
)

target_link_libraries(${namel} PRIVATE
search_path
${CANOE_LIBRARY_${buildu}}
)

set_target_properties(${namel}
PROPERTIES
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}")

# softlink library
execute_process(
COMMAND ln -sf ${CMAKE_BINARY_DIR}/python/${namel}${PYTHON_MODULE_EXTENSION}
${CMAKE_BINARY_DIR}/lib/${namel}${PYTHON_MODULE_EXTENSION}
)
1 change: 1 addition & 0 deletions python/empty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! This is a placeholder file for the default pgen file.
Loading
Loading