Skip to content

Commit

Permalink
Refactoring of plot module.
Browse files Browse the repository at this point in the history
  • Loading branch information
interkosmos committed Aug 25, 2024
1 parent abcbc01 commit 7b11c26
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 200 deletions.
81 changes: 43 additions & 38 deletions app/dmplot.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ program dmplot
character(len=*), parameter :: APP_NAME = 'dmplot'
integer, parameter :: APP_MAJOR = 0
integer, parameter :: APP_MINOR = 9
integer, parameter :: APP_PATCH = 2
integer, parameter :: APP_PATCH = 3

type :: app_type
!! Application settings.
character(len=ID_LEN) :: name = APP_NAME !! Name of instance and POSIX semaphore.
character(len=FILE_PATH_LEN) :: config = ' ' !! Path to configuration file.
character(len=FILE_PATH_LEN) :: database = ' ' !! Path to observation database.
character(len=NODE_ID_LEN) :: node = ' ' !! Node id.
character(len=SENSOR_ID_LEN) :: sensor = ' ' !! Sensor id.
character(len=TARGET_ID_LEN) :: target = ' ' !! Target id.
character(len=RESPONSE_NAME_LEN) :: response = ' ' !! Response name.
character(len=TIME_LEN) :: from = ' ' !! Start of time range (ISO 8601).
character(len=TIME_LEN) :: to = ' ' !! End of time range (ISO 8601).
integer :: terminal = PLOT_TERM_NONE !! Plot terminal (Gnuplot terminal).
character(len=FILE_PATH_LEN) :: output = ' ' !! Path of plot file.
character(len=8) :: background = ' ' !! Background colour.
character(len=8) :: foreground = ' ' !! Foreground colour (graph).
character(len=32) :: font = ' ' !! Font name.
character(len=80) :: title = ' ' !! Plot title.
integer :: width = 1000 !! Plot width.
integer :: height = 400 !! Plot height.
character(len=ID_LEN) :: name = APP_NAME !! Name of instance and POSIX semaphore.
character(len=FILE_PATH_LEN) :: config = ' ' !! Path to configuration file.
character(len=FILE_PATH_LEN) :: database = ' ' !! Path to observation database.
character(len=NODE_ID_LEN) :: node = ' ' !! Node id.
character(len=SENSOR_ID_LEN) :: sensor = ' ' !! Sensor id.
character(len=TARGET_ID_LEN) :: target = ' ' !! Target id.
character(len=RESPONSE_NAME_LEN) :: response = ' ' !! Response name.
character(len=TIME_LEN) :: from = ' ' !! Start of time range (ISO 8601).
character(len=TIME_LEN) :: to = ' ' !! End of time range (ISO 8601).
integer :: terminal = PLOT_TERMINAL_NONE !! Plot terminal backend.
character(len=FILE_PATH_LEN) :: output = ' ' !! Path of plot file.
character(len=8) :: background = ' ' !! Background colour.
character(len=8) :: foreground = ' ' !! Foreground colour (graph).
character(len=32) :: font = ' ' !! Font name.
character(len=80) :: title = ' ' !! Plot title.
integer :: width = 1000 !! Plot width.
integer :: height = 400 !! Plot height.
end type app_type

integer :: rc ! Return code.
Expand Down Expand Up @@ -88,16 +88,16 @@ integer function create_graph(dps, terminal, output, background, foreground, &

type(plot_type) :: plot

plot%term = terminal
plot%terminal = terminal

if (plot%term == PLOT_TERM_X11) plot%persist = .true.
if (dm_string_is_present(output)) plot%output = output
if (dm_string_is_present(background)) plot%background = background
if (dm_string_is_present(foreground)) plot%foreground = foreground
if (dm_string_is_present(font)) plot%font = font
if (dm_string_is_present(title)) plot%title = title
if (dm_string_is_present(xlabel)) plot%xlabel = xlabel
if (dm_string_is_present(ylabel)) plot%ylabel = ylabel
if (plot%terminal == PLOT_TERMINAL_X11) plot%persist = .true.
if (dm_string_is_present(output)) plot%output = output
if (dm_string_is_present(background)) plot%background = background
if (dm_string_is_present(foreground)) plot%foreground = foreground
if (dm_string_is_present(font)) plot%font = font
if (dm_string_is_present(title)) plot%title = title
if (dm_string_is_present(xlabel)) plot%xlabel = xlabel
if (dm_string_is_present(ylabel)) plot%ylabel = ylabel

if (present(width)) then
if (width > 0) plot%width = width
Expand All @@ -114,9 +114,9 @@ integer function read_args(app) result(rc)
!! Reads command-line arguments and settings from file.
type(app_type), intent(out) :: app !! App type.

character(len=PLOT_TERM_NAME_LEN) :: terminal
character(len=:), allocatable :: version
type(arg_type) :: args(17)
character(len=PLOT_TERMINAL_NAME_LEN) :: terminal
character(len=:), allocatable :: version
type(arg_type) :: args(17)

rc = E_NONE

Expand All @@ -130,7 +130,7 @@ integer function read_args(app) result(rc)
arg_type('response', short='R', type=ARG_TYPE_ID, max_len=RESPONSE_NAME_LEN), & ! -R, --response <name>
arg_type('from', short='B', type=ARG_TYPE_TIME), & ! -B, --from <timestamp>
arg_type('to', short='E', type=ARG_TYPE_TIME), & ! -E, --to <timestamp>
arg_type('terminal', short='m', type=ARG_TYPE_STRING, max_len=PLOT_TERM_NAME_LEN), & ! -m, --terminal <name>
arg_type('terminal', short='m', type=ARG_TYPE_STRING, max_len=PLOT_TERMINAL_NAME_LEN), & ! -m, --terminal <name>
arg_type('output', short='o', type=ARG_TYPE_STRING), & ! -o, --output <file>
arg_type('background', short='G', type=ARG_TYPE_STRING), & ! -G, --background <color>
arg_type('foreground', short='P', type=ARG_TYPE_STRING), & ! -P, --foreground <color>
Expand Down Expand Up @@ -169,7 +169,7 @@ integer function read_args(app) result(rc)
rc = dm_arg_get(args(16), app%width)
rc = dm_arg_get(args(17), app%height)

app%terminal = dm_plot_term_from_name(terminal)
app%terminal = dm_plot_terminal_from_name(terminal)

! Validate settings.
rc = E_INVALID
Expand Down Expand Up @@ -209,7 +209,7 @@ integer function read_args(app) result(rc)
return
end if

if (.not. dm_plot_term_valid(app%terminal)) then
if (.not. dm_plot_terminal_valid(app%terminal)) then
call dm_error_out(rc, 'invalid plot terminal')
return
end if
Expand All @@ -225,12 +225,17 @@ integer function read_args(app) result(rc)
end if

select case (app%terminal)
case (PLOT_TERM_GIF, PLOT_TERM_PNG, PLOT_TERM_PNG_CAIRO, PLOT_TERM_SVG)
case (PLOT_TERMINAL_GIF, &
PLOT_TERMINAL_PNG, &
PLOT_TERMINAL_PNG_CAIRO, &
PLOT_TERMINAL_SVG)
if (len_trim(app%output) == 0) then
call dm_error_out(rc, 'missing output path')
return
end if
case (PLOT_TERM_ANSI, PLOT_TERM_SIXEL, PLOT_TERM_X11)
case (PLOT_TERMINAL_ANSI, &
PLOT_TERMINAL_SIXEL, &
PLOT_TERMINAL_X11)
! Ignore output file path.
app%output = ' '
end select
Expand All @@ -242,8 +247,8 @@ integer function read_config(app) result(rc)
!! Reads app configuration from (Lua) file.
type(app_type), intent(inout) :: app !! App type.

character(len=PLOT_TERM_NAME_LEN) :: terminal
type(config_type) :: config
character(len=PLOT_TERMINAL_NAME_LEN) :: terminal
type(config_type) :: config

rc = E_NONE
if (len_trim(app%config) == 0) return
Expand All @@ -267,7 +272,7 @@ integer function read_config(app) result(rc)
rc = dm_config_get(config, 'to', app%to)
rc = dm_config_get(config, 'width', app%width)

app%terminal = dm_plot_term_from_name(terminal)
app%terminal = dm_plot_terminal_from_name(terminal)
end if

call dm_config_close(config)
Expand Down
53 changes: 25 additions & 28 deletions app/dmreport.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ program dmreport
character(len=*), parameter :: APP_NAME = 'dmreport'
integer, parameter :: APP_MAJOR = 0
integer, parameter :: APP_MINOR = 9
integer, parameter :: APP_PATCH = 2
integer, parameter :: APP_PATCH = 3

character(len=*), parameter :: APP_FONT = 'Open Sans' !! Default font name.
integer, parameter :: APP_PLOT_WIDTH = 1000 !! Default plot width.
Expand Down Expand Up @@ -68,7 +68,7 @@ function html_plot(data_points, response, unit, format, title, meta, color, widt

! Plot settings.
plot%bidirect = .true. ! Bi-directional pipe to Gnuplot.
plot%term = format ! Gnuplot terminal.
plot%terminal = format ! Gnuplot terminal.
plot%font = APP_FONT ! Font name.
plot%width = APP_PLOT_WIDTH ! Plot width.
plot%height = APP_PLOT_HEIGHT ! Plot height.
Expand All @@ -80,14 +80,9 @@ function html_plot(data_points, response, unit, format, title, meta, color, widt
plot%ylabel = trim(plot%ylabel) // ' [' // trim(unit) // ']'
end if

! Set title, meta, colour, width, and height.
if (present(title)) then
if (len_trim(title) > 0) plot%title = title
end if

if (present(color)) then
if (len_trim(color) > 0) plot%foreground = color
end if
! Set title, colour, width, and height.
if (dm_string_is_present(title)) plot%title = title
if (dm_string_is_present(color)) plot%foreground = color

if (present(width)) then
if (width > 0) plot%width = width
Expand All @@ -98,12 +93,12 @@ function html_plot(data_points, response, unit, format, title, meta, color, widt
end if

! Select MIME type according to format.
select case (plot%term)
case (PLOT_TERM_GIF)
select case (plot%terminal)
case (PLOT_TERMINAL_GIF)
mime = MIME_GIF
case (PLOT_TERM_PNG, PLOT_TERM_PNG_CAIRO)
case (PLOT_TERMINAL_PNG, PLOT_TERMINAL_PNG_CAIRO)
mime = MIME_PNG
case (PLOT_TERM_SVG)
case (PLOT_TERMINAL_SVG)
mime = MIME_SVG
case default
! Fail-safe: should never occur.
Expand Down Expand Up @@ -143,14 +138,16 @@ function html_report_table(node, from, to) result(html)
character(len=:), allocatable :: html !! Generated HTML.

html = H_NAV // H_TABLE // H_TBODY // &
H_TR // H_TH // 'From:' // H_TH_END // H_TD // dm_html_time(from) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'To:' // H_TH_END // H_TD // dm_html_time(to) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'Node ID:' // H_TH_END // H_TD // dm_html_encode(node%id) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'Node Name:' // H_TH_END // H_TD // dm_html_encode(node%name) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'Node Meta:' // H_TH_END // H_TD // dm_html_encode(node%meta) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'Node X:' // H_TH_END // H_TD // dm_ftoa(node%x) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'Node Y:' // H_TH_END // H_TD // dm_ftoa(node%y) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'Node Z:' // H_TH_END // H_TD // dm_ftoa(node%z) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'From:' // H_TH_END // H_TD // dm_html_time(from) // H_TD_END // &
H_TH // 'To:' // H_TH_END // H_TD // dm_html_time(to) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'Node ID:' // H_TH_END // H_TD // dm_html_encode(node%id) // H_TD_END // &
H_TH // 'Node Name:' // H_TH_END // H_TD // dm_html_encode(node%name) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'Node X:' // H_TH_END // H_TD // dm_ftoa(node%x) // H_TD_END // &
H_TH // 'Longitude:' // H_TH_END // H_TD // dm_ftoa(node%lon) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'Node Y:' // H_TH_END // H_TD // dm_ftoa(node%y) // H_TD_END // &
H_TH // 'Latitude:' // H_TH_END // H_TD // dm_ftoa(node%lat) // H_TD_END // H_TR_END // &
H_TR // H_TH // 'Node Z:' // H_TH_END // H_TD // dm_ftoa(node%z) // H_TD_END // &
H_TH // 'Altitude:' // H_TH_END // H_TD // dm_ftoa(node%alt) // H_TD_END // H_TR_END // &
H_TBODY_END // H_TABLE_END // H_NAV_END
end function html_report_table

Expand Down Expand Up @@ -228,10 +225,10 @@ integer function read_args(app) result(rc)
if (allocated(plot%observs)) n = size(plot%observs)

do i = 1, n
format = dm_plot_term_from_name(plot%observs(i)%format)
format = dm_plot_terminal_from_name(plot%observs(i)%format)

if (format /= PLOT_TERM_GIF .and. format /= PLOT_TERM_PNG .and. &
format /= PLOT_TERM_PNG_CAIRO .and. format /= PLOT_TERM_SVG) then
if (format /= PLOT_TERMINAL_GIF .and. format /= PLOT_TERMINAL_PNG .and. &
format /= PLOT_TERMINAL_PNG_CAIRO .and. format /= PLOT_TERMINAL_SVG) then
call dm_error_out(rc, 'invalid plot format ' // plot%observs(i)%format)
return
end if
Expand Down Expand Up @@ -467,10 +464,10 @@ subroutine create_report(report, error)
end if

! Get Gnuplot terminal name.
format = dm_plot_term_from_name(report%plot%observs(i)%format)
format = dm_plot_terminal_from_name(report%plot%observs(i)%format)

if (format /= PLOT_TERM_GIF .and. format /= PLOT_TERM_PNG .and. &
format /= PLOT_TERM_PNG_CAIRO .and. format /= PLOT_TERM_SVG) then
if (format /= PLOT_TERMINAL_GIF .and. format /= PLOT_TERMINAL_PNG .and. &
format /= PLOT_TERMINAL_PNG_CAIRO .and. format /= PLOT_TERMINAL_SVG) then
! Fail safe: should never occur.
write (unit, '(a)') dm_html_error(E_INVALID, 'invalid plot format')
exit plot_block
Expand Down
22 changes: 11 additions & 11 deletions app/dmweb.f90
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ program dmweb
! Program version number and patch level.
integer, parameter :: APP_MAJOR = 0
integer, parameter :: APP_MINOR = 9
integer, parameter :: APP_PATCH = 5
integer, parameter :: APP_PATCH = 6

! Program parameters.
character(len=*), parameter :: APP_BASE_PATH = '/dmpack' !! URI base path.
character(len=*), parameter :: APP_CSS_PATH = '/dmweb' !! Path to CSS directory.
character(len=*), parameter :: APP_JS_PATH = APP_CSS_PATH !! Path to JavaScript directory.
character(len=*), parameter :: APP_TITLE = 'DMPACK' !! HTML title and heading.
integer, parameter :: APP_DB_TIMEOUT = DB_TIMEOUT_DEFAULT !! SQLite 3 busy timeout in mseconds.
integer, parameter :: APP_PLOT_TERM = PLOT_TERM_SVG !! Plotting backend.
logical, parameter :: APP_READ_ONLY = .false. !! Default database access mode.
real(kind=r8), parameter :: APP_MAP_LON = 10.4541194_r8 !! Default map view longitude.
real(kind=r8), parameter :: APP_MAP_LAT = 51.1642292_r8 !! Default map view latitude.
character(len=*), parameter :: APP_BASE_PATH = '/dmpack' !! URI base path.
character(len=*), parameter :: APP_CSS_PATH = '/dmweb' !! Path to CSS directory.
character(len=*), parameter :: APP_JS_PATH = APP_CSS_PATH !! Path to JavaScript directory.
character(len=*), parameter :: APP_TITLE = 'DMPACK' !! HTML title and heading.
integer, parameter :: APP_DB_TIMEOUT = DB_TIMEOUT_DEFAULT !! SQLite 3 busy timeout in mseconds.
integer, parameter :: APP_PLOT_TERMINAL = PLOT_TERMINAL_SVG !! Plotting backend.
logical, parameter :: APP_READ_ONLY = .false. !! Default database access mode.
real(kind=r8), parameter :: APP_MAP_LAT = 51.1642292_r8 !! Default map view latitude.
real(kind=r8), parameter :: APP_MAP_LON = 10.4541194_r8 !! Default map view longitude.

! Global settings.
character(len=FILE_PATH_LEN) :: db_beat = ' ' ! Path to beat database.
Expand Down Expand Up @@ -1342,7 +1342,7 @@ subroutine route_plots(env)
end if

! Plotting via Gnuplot.
plot%term = APP_PLOT_TERM
plot%terminal = APP_PLOT_TERMINAL
plot%font = 'sans'
plot%graph = '#ffffff'
plot%bidirect = .true.
Expand Down
Loading

0 comments on commit 7b11c26

Please sign in to comment.