Skip to content

Commit

Permalink
Refactoring. Minor updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
interkosmos committed Mar 7, 2024
1 parent 3b4d98d commit ba0d509
Show file tree
Hide file tree
Showing 20 changed files with 422 additions and 357 deletions.
2 changes: 1 addition & 1 deletion adoc/dmexport.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ an empty file will be created.
Node id (required).

*--output*, *-o* _file_::
Path of output file.
Path of output file. Empty or `-` for standard output.

*--response*, *-R* _name_::
Response name (required for type `dp`).
Expand Down
103 changes: 54 additions & 49 deletions app/dmexport.f90
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ program dmexport
character(len=*), parameter :: APP_NAME = 'dmexport'
integer, parameter :: APP_MAJOR = 0
integer, parameter :: APP_MINOR = 9
integer, parameter :: APP_PATCH = 0
integer, parameter :: APP_PATCH = 1

type :: app_type
!! Command-line arguments.
character(len=FILE_PATH_LEN) :: database = ' ' !! Path to database.
character(len=FILE_PATH_LEN) :: output = ' ' !! Output file path.
character(len=FILE_PATH_LEN) :: output = ' ' !! Output file path, empty or '-' for stdout.
character(len=NODE_ID_LEN) :: node = ' ' !! Node id.
character(len=SENSOR_ID_LEN) :: sensor = ' ' !! Sensor id.
character(len=TARGET_ID_LEN) :: target = ' ' !! Target id.
Expand All @@ -30,7 +30,7 @@ program dmexport
end type app_type

integer :: rc ! Return code.
type(app_type) :: app ! App type.
type(app_type) :: app ! App settings.

! Initialise DMPACK.
call dm_init()
Expand All @@ -46,7 +46,7 @@ program dmexport
integer function export(app) result(rc)
type(app_type), intent(inout) :: app

integer :: fu, stat
integer :: stat, unit
logical :: is_file
type (db_type) :: db

Expand All @@ -60,7 +60,7 @@ integer function export(app) result(rc)
type(target_type), allocatable :: targets(:)

is_file = .false.
if (len_trim(app%output) > 0) is_file = .true.
if (len_trim(app%output) > 0 .and. app%output /= '-') is_file = .true.

rc = dm_db_open(db, app%database, read_only=.true., validate=.true.)

Expand Down Expand Up @@ -90,7 +90,7 @@ integer function export(app) result(rc)
target_id=app%target, from=app%from, to=app%to)
case (TYPE_BEAT)
rc = dm_db_select(db, beats)
case (TYPE_DATA_POINT)
case (TYPE_DP)
rc = dm_db_select(db, data_points, node_id=app%node, sensor_id=app%sensor, &
target_id=app%target, response_name=app%response, &
from=app%from, to=app%to)
Expand All @@ -100,77 +100,83 @@ integer function export(app) result(rc)
if (dm_is_error(dm_db_close(db))) rc = E_DB
if (dm_is_error(rc)) return

fu = stdout
unit = stdout

! Open file.
if (is_file) then
rc = E_IO
open (action='write', file=trim(app%output), iostat=stat, newunit=fu, &
status='replace')
open (action = 'write', &
file = trim(app%output), &
iostat = stat, &
newunit = unit, &
status = 'replace')
if (stat /= 0) return
end if

! Output records in selected format.
select case (app%format)
case (FORMAT_BLOCK)
rc = E_INVALID
if (app%type == TYPE_DATA_POINT) then
rc = dm_block_write(data_points, fu)
if (app%type == TYPE_DP) then
rc = dm_block_write(data_points, unit)
end if

case (FORMAT_CSV)
select case (app%type)
case (TYPE_NODE)
rc = dm_csv_write(nodes, fu, app%header, app%separator)
rc = dm_csv_write(nodes, unit, app%header, app%separator)
case (TYPE_SENSOR)
rc = dm_csv_write(sensors, fu, app%header, app%separator)
rc = dm_csv_write(sensors, unit, app%header, app%separator)
case (TYPE_TARGET)
rc = dm_csv_write(targets, fu, app%header, app%separator)
rc = dm_csv_write(targets, unit, app%header, app%separator)
case (TYPE_OBSERV)
rc = dm_csv_write(observs, fu, app%header, app%separator)
rc = dm_csv_write(observs, unit, app%header, app%separator)
case (TYPE_LOG)
rc = dm_csv_write(logs, fu, app%header, app%separator)
rc = dm_csv_write(logs, unit, app%header, app%separator)
case (TYPE_BEAT)
rc = dm_csv_write(beats, fu, app%header, app%separator)
case (TYPE_DATA_POINT)
rc = dm_csv_write(data_points, fu, app%header, app%separator)
rc = dm_csv_write(beats, unit, app%header, app%separator)
case (TYPE_DP)
rc = dm_csv_write(data_points, unit, app%header, app%separator)
end select

case (FORMAT_JSON)
select case (app%type)
case (TYPE_NODE)
rc = dm_json_write(nodes, fu)
rc = dm_json_write(nodes, unit)
case (TYPE_SENSOR)
rc = dm_json_write(sensors, fu)
rc = dm_json_write(sensors, unit)
case (TYPE_TARGET)
rc = dm_json_write(targets, fu)
rc = dm_json_write(targets, unit)
case (TYPE_OBSERV)
rc = dm_json_write(observs, fu)
rc = dm_json_write(observs, unit)
case (TYPE_LOG)
rc = dm_json_write(logs, fu)
rc = dm_json_write(logs, unit)
case (TYPE_BEAT)
rc = dm_json_write(beats, fu)
case (TYPE_DATA_POINT)
rc = dm_json_write(data_points, fu)
rc = dm_json_write(beats, unit)
case (TYPE_DP)
rc = dm_json_write(data_points, unit)
end select

case (FORMAT_JSONL)
select case (app%type)
case (TYPE_NODE)
rc = dm_jsonl_write(nodes, fu)
rc = dm_jsonl_write(nodes, unit)
case (TYPE_SENSOR)
rc = dm_jsonl_write(sensors, fu)
rc = dm_jsonl_write(sensors, unit)
case (TYPE_TARGET)
rc = dm_jsonl_write(targets, fu)
rc = dm_jsonl_write(targets, unit)
case (TYPE_OBSERV)
rc = dm_jsonl_write(observs, fu)
rc = dm_jsonl_write(observs, unit)
case (TYPE_LOG)
rc = dm_jsonl_write(logs, fu)
rc = dm_jsonl_write(logs, unit)
case (TYPE_BEAT)
rc = dm_jsonl_write(beats, fu)
case (TYPE_DATA_POINT)
rc = dm_jsonl_write(data_points, fu)
rc = dm_jsonl_write(beats, unit)
case (TYPE_DP)
rc = dm_jsonl_write(data_points, unit)
end select
end select

if (is_file) close (fu)
if (is_file) close (unit)

if (dm_is_error(rc)) then
call dm_error_out(rc, 'failed to write data')
Expand All @@ -195,9 +201,9 @@ integer function read_args(app) result(rc)
arg_type('target', short='T', type=ARG_TYPE_ID), & ! -T, --target <id>
arg_type('from', short='B', type=ARG_TYPE_TIME), & ! -F, --from <timestamp>
arg_type('to', short='E', type=ARG_TYPE_TIME), & ! -T, --to <timestamp>
arg_type('response', short='R', type=ARG_TYPE_ID, max_len=RESPONSE_NAME_LEN), & ! -R, --response <name>
arg_type('response', short='R', type=ARG_TYPE_ID, max_len=RESPONSE_NAME_LEN), & ! -R, --response <name>
arg_type('format', short='f', type=ARG_TYPE_CHAR, max_len=FORMAT_NAME_LEN, required=.true.), & ! -f, --format <string>
arg_type('type', short='t', type=ARG_TYPE_CHAR, max_len=TYPE_NAME_LEN, required=.true.), & ! -t, --type <string>
arg_type('type', short='t', type=ARG_TYPE_CHAR, max_len=TYPE_NAME_LEN, required=.true.), & ! -t, --type <string>
arg_type('header', short='H', type=ARG_TYPE_BOOL), & ! -H, --header
arg_type('separator', short='s', type=ARG_TYPE_CHAR, max_len=1) & ! -a, --separator <char>
]
Expand Down Expand Up @@ -235,49 +241,48 @@ integer function read_args(app) result(rc)

! Data type.
select case (app%type)
case (TYPE_NODE, TYPE_SENSOR, TYPE_TARGET, TYPE_OBSERV, TYPE_LOG, TYPE_BEAT, TYPE_DATA_POINT)
case (TYPE_NODE, TYPE_SENSOR, TYPE_TARGET, TYPE_OBSERV, TYPE_LOG, TYPE_BEAT, TYPE_DP)
continue
case default
call dm_error_out(rc, 'invalid type')
return
end select

! Log, observation, and data point.
if (app%type == TYPE_LOG .or. app%type == TYPE_OBSERV .or. &
app%type == TYPE_DATA_POINT) then
if (app%type == TYPE_LOG .or. app%type == TYPE_OBSERV .or. app%type == TYPE_DP) then
if (.not. dm_time_valid(app%from)) then
call dm_error_out(rc, 'missing argument --from')
call dm_error_out(rc, 'invalid or missing argument --from')
return
end if

if (.not. dm_time_valid(app%to)) then
call dm_error_out(rc, 'missing argument --to')
call dm_error_out(rc, 'invalid or missing argument --to')
return
end if
end if

! Observation and data point.
if (app%type == TYPE_OBSERV .or. app%type == TYPE_DATA_POINT) then
if (app%type == TYPE_OBSERV .or. app%type == TYPE_DP) then
if (.not. dm_id_valid(app%node)) then
call dm_error_out(rc, 'missing argument --node')
call dm_error_out(rc, 'invalid or missing argument --node')
return
end if

if (.not. dm_id_valid(app%sensor)) then
call dm_error_out(rc, 'missing argument --sensor')
call dm_error_out(rc, 'invalid or missing argument --sensor')
return
end if

if (.not. dm_id_valid(app%target)) then
call dm_error_out(rc, 'missing argument --target')
call dm_error_out(rc, 'invalid or missing argument --target')
return
end if
end if

! Data point.
if (app%type == TYPE_DATA_POINT) then
if (app%type == TYPE_DP) then
if (.not. dm_id_valid(app%response)) then
call dm_error_out(rc, 'missing argument --response')
call dm_error_out(rc, 'invalid or missing argument --response')
return
end if
else
Expand Down
16 changes: 8 additions & 8 deletions app/dmimport.f90
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ integer function import(app) result(rc)

type(app_type), intent(inout) :: app

integer :: er, fu, stat
integer :: er, stat, unit
integer(kind=i8) :: nrecs, nrows
logical :: exists, valid
real(kind=r8) :: dt
Expand All @@ -58,7 +58,7 @@ integer function import(app) result(rc)

! Try to open input file.
rc = E_IO
open (action='read', file=trim(app%input), iostat=stat, newunit=fu)
open (action='read', file=trim(app%input), iostat=stat, newunit=unit)

if (stat /= 0) then
call dm_error_out(rc, 'failed to open file ' // trim(app%input))
Expand Down Expand Up @@ -132,15 +132,15 @@ integer function import(app) result(rc)
! Read record from file.
select case (app%type)
case (TYPE_NODE)
rc = dm_csv_read(node, fu, app%separator, app%quote)
rc = dm_csv_read(node, unit, app%separator, app%quote)
case (TYPE_SENSOR)
rc = dm_csv_read(sensor, fu, app%separator, app%quote)
rc = dm_csv_read(sensor, unit, app%separator, app%quote)
case (TYPE_TARGET)
rc = dm_csv_read(target, fu, app%separator, app%quote)
rc = dm_csv_read(target, unit, app%separator, app%quote)
case (TYPE_OBSERV)
rc = dm_csv_read(observ, fu, app%separator, app%quote)
rc = dm_csv_read(observ, unit, app%separator, app%quote)
case (TYPE_LOG)
rc = dm_csv_read(log, fu, app%separator, app%quote)
rc = dm_csv_read(log, unit, app%separator, app%quote)
end select

! Ignore comments and empty rows.
Expand Down Expand Up @@ -266,7 +266,7 @@ integer function import(app) result(rc)
end if

! Close file.
close (fu)
close (unit)
if (app%verbose) print '("Closed file ", a)', trim(app%input)

if (dm_is_error(rc)) return
Expand Down
2 changes: 1 addition & 1 deletion app/dmpipe.f90
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ program dmpipe
integer, parameter :: APP_MINOR = 9
integer, parameter :: APP_PATCH = 0

character, parameter :: APP_CSV_SEPARATOR = ',' !! CSV seperator character.
character, parameter :: APP_CSV_SEPARATOR = ',' !! CSV field separator.
logical, parameter :: APP_MQ_BLOCKING = .true. !! Observation forwarding is blocking.

integer, parameter :: OUTPUT_NONE = 0 !! No output.
Expand Down
37 changes: 25 additions & 12 deletions app/dmserial.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ program dmserial
integer, parameter :: APP_MINOR = 9
integer, parameter :: APP_PATCH = 1

character, parameter :: APP_CSV_SEPARATOR = ',' !! CSV seperator character.
character, parameter :: APP_CSV_SEPARATOR = ',' !! CSV field separator.
logical, parameter :: APP_MQ_BLOCKING = .true. !! Observation forwarding is blocking.

integer, parameter :: OUTPUT_NONE = 0 !! No output.
Expand Down Expand Up @@ -55,7 +55,14 @@ program dmserial
if (dm_is_error(rc)) call dm_stop(1)

! Create TTY type.
rc = create_tty(tty, app)
rc = create_tty(tty = tty, &
path = app%tty, &
baud_rate = app%baud_rate, &
byte_size = app%byte_size, &
parity = app%parity, &
stop_bits = app%stop_bits, &
dtr = app%dtr, &
rts = app%rts)
if (dm_is_error(rc)) call dm_stop(1)

! Initialise logger.
Expand All @@ -75,28 +82,34 @@ program dmserial

call dm_stop(0)
contains
integer function create_tty(tty, app) result(rc)
integer function create_tty(tty, path, baud_rate, byte_size, parity, stop_bits, dtr, rts) result(rc)
!! Creates TTY type from application settings.
type(tty_type), intent(inout) :: tty !! TTY type.
type(app_type), intent(inout) :: app !! App type.
type(tty_type), intent(out) :: tty !! TTY type.
character(len=*), intent(in) :: path !! Device path.
integer, intent(in) :: baud_rate !! Numeric baud rate.
integer, intent(in) :: byte_size !! Numeric byte size.
character(len=*), intent(in) :: parity !! Parity string.
integer, intent(in) :: stop_bits !! Numeric stop bits.
logical, intent(in) :: dtr !! DTR enabled.
logical, intent(in) :: rts !! RTS enabled.

tty_block: block
tty%path = app%tty
tty%path = path

tty%baud_rate = dm_tty_baud_rate_from_value(app%baud_rate, error=rc)
tty%baud_rate = dm_tty_baud_rate_from_value(baud_rate, error=rc)
if (dm_is_error(rc)) exit tty_block

tty%byte_size = dm_tty_byte_size_from_value(app%byte_size, error=rc)
tty%byte_size = dm_tty_byte_size_from_value(byte_size, error=rc)
if (dm_is_error(rc)) exit tty_block

tty%parity = dm_tty_parity_from_name(app%parity, error=rc)
tty%parity = dm_tty_parity_from_name(parity, error=rc)
if (dm_is_error(rc)) exit tty_block

tty%stop_bits = dm_tty_stop_bits_from_value(app%stop_bits, error=rc)
tty%stop_bits = dm_tty_stop_bits_from_value(stop_bits, error=rc)
if (dm_is_error(rc)) exit tty_block

tty%dtr = app%dtr
tty%rts = app%rts
tty%dtr = dtr
tty%rts = rts
end block tty_block

if (dm_is_error(rc)) call dm_error_out(rc, 'invalid TTY parameters')
Expand Down
4 changes: 2 additions & 2 deletions app/dmsync.f90
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ program dmsync
integer, parameter :: PASSWORD_LEN = 256 !! Max. length of password.

type :: app_type
!! Global application settings.
!! Application settings.
character(len=ID_LEN) :: name = APP_NAME !! Name of instance/configuration.
character(len=FILE_PATH_LEN) :: config = ' ' !! Path to configuration file.
character(len=LOGGER_NAME_LEN) :: logger = ' ' !! Name of logger.
Expand All @@ -44,7 +44,7 @@ program dmsync
end type app_type

integer :: rc ! Return code.
type(app_type) :: app ! App configuration.
type(app_type) :: app ! App settings.
type(db_type) :: db ! Database type.
type(sem_type) :: sem ! POSIX semaphore type.

Expand Down
2 changes: 1 addition & 1 deletion app/dmweb.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,7 @@ function html_form_observs(nodes, sensors, targets, max_results, node_id, sensor
end function html_form_observs

function html_form_plots(nodes, sensors, targets, max_results, node_id, sensor_id, &
target_id, response_name, from, to, nresults) result(html)
target_id, response_name, from, to, nresults) result(html)
!! Returns HTML form for plot selection.
type(node_type), intent(inout) :: nodes(:) !! Node types.
type(sensor_type), intent(inout) :: sensors(:) !! Sensor types.
Expand Down
Loading

0 comments on commit ba0d509

Please sign in to comment.