1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
module jtty_source_codec
use iso_c_binding, only: c_char,c_int,c_int8_t,c_int32_t
use packjt77, only: pack28,unpack28
use packjt77_grammar, only: PACK77_NSEC,PACK77_ARRL_SECTIONS
implicit none
! i2.n2 Example Bits Total Purpose
! 0.0 CQ K1ABC CQ 28+2+2 32 CQ call action
! 0.1 K1ABC 28+2+2 32 bare call action
! 0.2 TU K1ABC CQ 28+2+2 32 TU/CQ call action
! 0.3 K1ABC TU 28+2+2 32 call/TU action
! 1.0 K1ABC AGN? 28+2+2 32 call/AGN action
! 1.1 TU NOW K1ABC 28+2+2 32 TU NOW/call action
! 1.2-3 reserved 28+2+2 32 invalid in version 1
! 2.x STRUCT30 27+3+2 32 typed structured source atom
! 3.x TEXT5 30+2 32 five six-bit JTTY characters
!
! family Name Body fields, from bit 1 toward bit 27
! 000 EXCH_NUM role1 + number_kind4 + value17 + zero5
! 001 EXCH_LOC role1 + location_kind4 + length1 + token16 + zero5
! 010 EXCH_PAIR pair_schema3 + pair_data23 + zero1
! 011 EXCH_NUM_TIME role1 + serial14 + minute_of_day11 + zero1
! 100 MISC subtype4 + subtype_data23
! 101-110 reserved invalid in version 1
! 111 guard space always invalid
integer, parameter :: I8=selected_int_kind(18)
character(len=*), parameter :: ALPHABET = &
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ +-./?!"#$%,&*()_''=[]{}<>|:;'
integer, parameter :: JTTY_ATOM_CALL=0,JTTY_ATOM_EXCH_NUM=1
integer, parameter :: JTTY_ATOM_EXCH_LOC=2,JTTY_ATOM_EXCH_PAIR=3
integer, parameter :: JTTY_ATOM_EXCH_NUM_TIME=4,JTTY_ATOM_CONTROL=5
integer, parameter :: JTTY_ATOM_GRID4=6,JTTY_ATOM_TEXT5=7
integer, parameter :: JTTY_CALL_CQ=0,JTTY_CALL_CALL=1,JTTY_CALL_TU_CQ=2
integer, parameter :: JTTY_CALL_CALL_TU=3,JTTY_CALL_CALL_AGN=4,JTTY_CALL_TU_NOW=5
integer, parameter :: JTTY_ROLE_FIELD_ONLY=0,JTTY_ROLE_FULL=1
integer, parameter :: JTTY_NUM_SERIAL=0,JTTY_NUM_CQ_ZONE=1,JTTY_NUM_ITU_ZONE=2
integer, parameter :: JTTY_NUM_AGE=3,JTTY_NUM_POWER=4,JTTY_NUM_CHECK=5
integer, parameter :: JTTY_NUM_LICENSE_YEAR=6,JTTY_NUM_GENERIC=7
integer, parameter :: JTTY_LOC_STATE_PROVINCE=0,JTTY_LOC_SECTION=1
integer, parameter :: JTTY_LOC_COUNTRY_PREFIX=2,JTTY_LOC_QTH=3,JTTY_LOC_ADMIN_CODE=4
integer, parameter :: JTTY_PAIR_ZONE_LOC3=0,JTTY_PAIR_CLASS_SECTION=1
integer, parameter :: JTTY_MISC_CONTROL=0,JTTY_MISC_GRID4=1
integer(c_int), parameter :: JTTY_ENCODE_OK=0,JTTY_ENCODE_INVALID_DESCRIPTOR=1
integer(c_int), parameter :: JTTY_ENCODE_UNKNOWN_SECTION=2
integer, parameter :: JTTY_CONTROL_AGN=0,JTTY_CONTROL_CALL=1,JTTY_CONTROL_AGN_CALL=2
integer, parameter :: JTTY_CONTROL_NR=3,JTTY_CONTROL_AGN_NR=4,JTTY_CONTROL_EXCH=5
integer, parameter :: JTTY_CONTROL_STATE=6,JTTY_CONTROL_SECTION=7,JTTY_CONTROL_ZONE=8
integer, parameter :: JTTY_CONTROL_GRID=9,JTTY_CONTROL_RPRT=10,JTTY_CONTROL_QSL_TU=11
integer, parameter :: JTTY_CONTROL_TU=12,JTTY_CONTROL_QRZ=13,JTTY_CONTROL_QSO_B4=14
integer, parameter :: JTTY_CONTROL_WAIT=15,JTTY_CONTROL_NIL=16,JTTY_CONTROL_OK=17
character(len=8), parameter :: CONTROL_TEXT(0:17)=[character(len=8) :: &
'AGN?','CALL?','AGN CALL','NR?','AGN NR','EXCH?','STATE?','SECTION?', &
'ZONE?','GRID?','RPRT?','QSL TU','TU','QRZ?','QSO B4','WAIT','NIL?','OK?']
type :: jtty_source_atom
integer :: kind=-1,subtype=0,role=JTTY_ROLE_FIELD_ONLY,value=0,value2=0
character(len=13) :: text=''
end type jtty_source_atom
type, bind(C) :: jtty_source_atom_c
integer(c_int8_t) :: kind,subtype,role,reserved
integer(c_int32_t) :: value
character(kind=c_char) :: text(9)
end type jtty_source_atom_c
contains
subroutine pack_jtty_atom(atom,frame,eom,valid)
type(jtty_source_atom), intent(in) :: atom
character(len=34), intent(out) :: frame
logical, intent(in) :: eom
logical, intent(out) :: valid
integer(I8) :: body,top30,n32,token_value,pair_data,data
integer :: i,n28,i2,n2,n,grid_index,class_index
character(len=13) :: call_text,roundtrip
character(len=5) :: text5
logical :: success
frame=''; valid=.false.; n32=0
select case(atom%kind)
case(JTTY_ATOM_CALL)
if(atom%subtype.lt.0 .or. atom%subtype.gt.5) return
call_text=atom%text
if(.not.standard_call(call_text)) return
call pack28(call_text,n28); call unpack28(n28,roundtrip,success)
if(.not.success .or. trim(roundtrip).ne.trim(call_text)) return
if(atom%subtype.le.3) then; i2=0; n2=atom%subtype
else; i2=1; n2=atom%subtype-4; endif
n32=ishft(int(n28,I8),4)+4*n2+i2
case(JTTY_ATOM_EXCH_NUM)
if(.not.valid_role(atom%role) .or. .not.valid_number(atom%subtype,atom%value)) return
body=ishft(int(atom%role,I8),26)+ishft(int(atom%subtype,I8),22)+ishft(int(atom%value,I8),5)
call finish_struct(body,0,n32)
case(JTTY_ATOM_EXCH_LOC)
if(.not.valid_role(atom%role) .or. atom%subtype.lt.0 .or. atom%subtype.gt.4) return
n=len_trim(atom%text); if(n.ne.2 .and. n.ne.3) return
call pack_base36(atom%text(1:n),token_value,success); if(.not.success) return
body=ishft(int(atom%role,I8),26)+ishft(int(atom%subtype,I8),22)+ &
ishft(int(n-2,I8),21)+ishft(token_value,5)
call finish_struct(body,1,n32)
case(JTTY_ATOM_EXCH_PAIR)
select case(atom%subtype)
case(JTTY_PAIR_ZONE_LOC3)
if(atom%value.lt.1 .or. atom%value.gt.40) return
n=len_trim(atom%text); if(n.ne.2 .and. n.ne.3) return
call pack_base36(atom%text(1:n),token_value,success); if(.not.success) return
pair_data=ishft(int(atom%value,I8),17)+ishft(int(n-2,I8),16)+token_value
case(JTTY_PAIR_CLASS_SECTION)
if(atom%value.lt.1 .or. atom%value.gt.32) return
if(atom%value2.lt.1 .or. atom%value2.gt.PACK77_NSEC .or. len_trim(atom%text).ne.1) return
class_index=ichar(atom%text(1:1))-ichar('A'); if(class_index.lt.0 .or. class_index.gt.5) return
pair_data=ishft(int(atom%value,I8),17)+ishft(int(class_index,I8),14)+ &
ishft(int(atom%value2,I8),7)
case default; return
end select
body=ishft(int(atom%subtype,I8),24)+ishft(pair_data,1); call finish_struct(body,2,n32)
case(JTTY_ATOM_EXCH_NUM_TIME)
if(.not.valid_role(atom%role) .or. atom%value.lt.0 .or. atom%value.gt.16383) return
if(atom%value2.lt.0 .or. atom%value2.gt.1439) return
body=ishft(int(atom%role,I8),26)+ishft(int(atom%value,I8),12)+ishft(int(atom%value2,I8),1)
call finish_struct(body,3,n32)
case(JTTY_ATOM_CONTROL)
if(atom%subtype.lt.0 .or. atom%subtype.gt.17) return
body=ishft(int(JTTY_MISC_CONTROL,I8),23)+ishft(int(atom%subtype,I8),16)
call finish_struct(body,4,n32)
case(JTTY_ATOM_GRID4)
if(.not.valid_role(atom%role)) return
call grid4_to_index(atom%text,grid_index,success); if(.not.success) return
data=ishft(int(atom%role,I8),22)+ishft(int(grid_index,I8),7)
body=ishft(int(JTTY_MISC_GRID4,I8),23)+data; call finish_struct(body,4,n32)
case(JTTY_ATOM_TEXT5)
text5=atom%text(1:5); top30=0
do i=1,5
if(source_index(text5(i:i)).lt.0) return
top30=64*top30+source_index(text5(i:i))
enddo
n32=4*top30+3
case default; return
end select
write(frame(1:32),'(b32.32)') n32
frame(33:33)='0'
frame(34:34)=merge('1','0',eom)
valid=.true.
end subroutine pack_jtty_atom
subroutine finish_struct(body,family,n32)
integer(I8), intent(in) :: body
integer, intent(in) :: family
integer(I8), intent(out) :: n32
n32=4*(8*body+family)+2
end subroutine finish_struct
subroutine unpack_jtty_atom(frame,atom,valid,eom)
character(len=34), intent(in) :: frame
type(jtty_source_atom), intent(out) :: atom
logical, intent(out) :: valid,eom
integer(I8) :: top30,body,pair_data,data,token_value
integer :: i,i2,family,n28,n2,zero,n,grid_index,class_index
character(len=13) :: call_text
logical :: success
atom=jtty_source_atom(); valid=.false.; eom=.false.
if(verify(frame,'01').ne.0 .or. frame(33:33).ne.'0') return
if(frame(1:32).eq.repeat('0',32)) return
read(frame,'(b30.30,b2.2)') top30,i2
select case(i2)
case(0,1)
read(frame,'(b28.28,b2.2)') n28,n2
if(i2.eq.1 .and. n2.gt.1) return
call unpack28(n28,call_text,success)
if(.not.success .or. .not.standard_call(call_text)) return
atom=jtty_call_atom(merge(n2,n2+4,i2.eq.0),call_text)
case(2)
family=int(iand(top30,7_I8)); body=ishft(top30,-3)
select case(family)
case(0)
atom%kind=JTTY_ATOM_EXCH_NUM; atom%role=int(ibits(body,26,1))
atom%subtype=int(ibits(body,22,4)); atom%value=int(ibits(body,5,17))
zero=int(ibits(body,0,5))
if(zero.ne.0 .or. .not.valid_number(atom%subtype,atom%value)) return
case(1)
atom%kind=JTTY_ATOM_EXCH_LOC; atom%role=int(ibits(body,26,1))
atom%subtype=int(ibits(body,22,4)); n=int(ibits(body,21,1))+2
token_value=ibits(body,5,16); zero=int(ibits(body,0,5))
if(zero.ne.0 .or. atom%subtype.gt.4 .or. token_value.ge.36_I8**n) return
if(n.eq.3 .and. token_value.lt.36_I8**2) return
call unpack_base36(token_value,n,atom%text)
case(2)
atom%kind=JTTY_ATOM_EXCH_PAIR; atom%subtype=int(ibits(body,24,3))
pair_data=ibits(body,1,23); zero=int(ibits(body,0,1)); if(zero.ne.0) return
select case(atom%subtype)
case(JTTY_PAIR_ZONE_LOC3)
atom%value=int(ibits(pair_data,17,6)); n=int(ibits(pair_data,16,1))+2
token_value=ibits(pair_data,0,16)
if(atom%value.lt.1 .or. atom%value.gt.40 .or. token_value.ge.36_I8**n) return
if(n.eq.3 .and. token_value.lt.36_I8**2) return
call unpack_base36(token_value,n,atom%text)
case(JTTY_PAIR_CLASS_SECTION)
atom%value=int(ibits(pair_data,17,6)); class_index=int(ibits(pair_data,14,3))
atom%value2=int(ibits(pair_data,7,7)); zero=int(ibits(pair_data,0,7))
if(atom%value.lt.1 .or. atom%value.gt.32 .or. class_index.gt.5) return
if(atom%value2.lt.1 .or. atom%value2.gt.PACK77_NSEC .or. zero.ne.0) return
atom%text(1:1)=char(ichar('A')+class_index)
case default; return
end select
case(3)
atom%kind=JTTY_ATOM_EXCH_NUM_TIME; atom%role=int(ibits(body,26,1))
atom%value=int(ibits(body,12,14)); atom%value2=int(ibits(body,1,11))
zero=int(ibits(body,0,1)); if(zero.ne.0 .or. atom%value2.gt.1439) return
case(4)
i=int(ibits(body,23,4)); data=ibits(body,0,23)
select case(i)
case(JTTY_MISC_CONTROL)
atom%kind=JTTY_ATOM_CONTROL; atom%subtype=int(ibits(data,16,7))
zero=int(ibits(data,0,16)); if(zero.ne.0 .or. atom%subtype.gt.17) return
case(JTTY_MISC_GRID4)
atom%kind=JTTY_ATOM_GRID4; atom%role=int(ibits(data,22,1))
grid_index=int(ibits(data,7,15)); zero=int(ibits(data,0,7))
if(zero.ne.0 .or. grid_index.ge.32400) return
call index_to_grid4(grid_index,atom%text)
case default; return
end select
case default; return
end select
case(3)
atom%kind=JTTY_ATOM_TEXT5
do i=1,5
atom%text(i:i)=source_char(int(iand(ishft(top30,-6*(5-i)),63_I8)))
enddo
end select
eom=frame(34:34).eq.'1'
valid=.true.
end subroutine unpack_jtty_atom
subroutine render_jtty_atom(atom,text,valid)
type(jtty_source_atom), intent(in) :: atom
character(len=80), intent(out) :: text
logical, intent(out) :: valid
character(len=16) :: first,second
character(len=3) :: section
integer :: hours,minutes
text=''; valid=.true.
select case(atom%kind)
case(JTTY_ATOM_CALL)
select case(atom%subtype)
case(JTTY_CALL_CQ); text='CQ '//trim(atom%text)//' CQ'
case(JTTY_CALL_CALL); text=trim(atom%text)
case(JTTY_CALL_TU_CQ); text='TU '//trim(atom%text)//' CQ'
case(JTTY_CALL_CALL_TU); text=trim(atom%text)//' TU'
case(JTTY_CALL_CALL_AGN); text=trim(atom%text)//' AGN?'
case(JTTY_CALL_TU_NOW); text='TU NOW '//trim(atom%text)
case default; valid=.false.
end select
case(JTTY_ATOM_EXCH_NUM)
if(.not.valid_role(atom%role) .or. .not.valid_number(atom%subtype,atom%value)) then
valid=.false.; return
endif
call render_number(atom%subtype,atom%value,first); call render_role(atom%role,trim(first),text)
case(JTTY_ATOM_EXCH_LOC)
if(atom%subtype.lt.0 .or. atom%subtype.gt.4) then; valid=.false.; return; endif
call render_role(atom%role,trim(atom%text),text)
case(JTTY_ATOM_EXCH_PAIR)
select case(atom%subtype)
case(JTTY_PAIR_ZONE_LOC3)
write(first,'(i2.2)') atom%value; text='599 '//trim(first)//' '//trim(atom%text)
case(JTTY_PAIR_CLASS_SECTION)
if(atom%value2.lt.1 .or. atom%value2.gt.PACK77_NSEC) then; valid=.false.; return; endif
section=PACK77_ARRL_SECTIONS(atom%value2)
write(first,'(i0,a1)') atom%value,atom%text(1:1); text=trim(first)//' '//trim(section)
case default; valid=.false.
end select
case(JTTY_ATOM_EXCH_NUM_TIME)
if(.not.valid_role(atom%role) .or. atom%value.lt.0 .or. atom%value.gt.16383 .or. &
atom%value2.lt.0 .or. atom%value2.gt.1439) then; valid=.false.; return; endif
call render_number(JTTY_NUM_SERIAL,atom%value,first)
hours=atom%value2/60; minutes=mod(atom%value2,60)
write(second,'(i2.2,i2.2)') hours,minutes
call render_role(atom%role,trim(first)//' '//trim(second),text)
case(JTTY_ATOM_CONTROL)
if(atom%subtype.lt.0 .or. atom%subtype.gt.17) then; valid=.false.; return; endif
text=trim(CONTROL_TEXT(atom%subtype))
case(JTTY_ATOM_GRID4)
call render_role(atom%role,atom%text(1:4),text)
case(JTTY_ATOM_TEXT5); text(1:5)=atom%text(1:5)
case default; valid=.false.
end select
end subroutine render_jtty_atom
subroutine render_role(role,field,text)
integer, intent(in) :: role
character(len=*), intent(in) :: field
character(len=80), intent(out) :: text
if(role.eq.JTTY_ROLE_FULL) then; text='599 '//field; else; text=field; endif
end subroutine render_role
subroutine render_number(kind,value,text)
integer, intent(in) :: kind,value
character(len=16), intent(out) :: text
select case(kind)
case(JTTY_NUM_SERIAL)
if(value.lt.1000) then; write(text,'(i3.3)') value; else; write(text,'(i0)') value; endif
case(JTTY_NUM_CQ_ZONE,JTTY_NUM_ITU_ZONE,JTTY_NUM_CHECK)
if(value.lt.100) then; write(text,'(i2.2)') value; else; write(text,'(i0)') value; endif
case(JTTY_NUM_LICENSE_YEAR); write(text,'(i4.4)') value
case default; write(text,'(i0)') value
end select
end subroutine render_number
logical function valid_number(kind,value)
integer, intent(in) :: kind,value
valid_number=kind.ge.0 .and. kind.le.7 .and. value.ge.0 .and. value.le.131071
if(.not.valid_number) return
if(kind.eq.JTTY_NUM_CQ_ZONE) valid_number=value.ge.1 .and. value.le.40
if(kind.eq.JTTY_NUM_ITU_ZONE) valid_number=value.ge.1 .and. value.le.90
if(kind.eq.JTTY_NUM_LICENSE_YEAR) valid_number=value.le.9999
end function valid_number
logical function valid_role(role)
integer, intent(in) :: role
valid_role=role.eq.JTTY_ROLE_FIELD_ONLY .or. role.eq.JTTY_ROLE_FULL
end function valid_role
logical function standard_call(call_text)
character(len=13), intent(in) :: call_text
character(len=13) :: work,unpacked
character(len=6) :: base_call
logical :: ok,success
integer :: n28
standard_call=.false.; work=call_text
call chkcall(work,base_call,ok)
if(.not.ok .or. index(work,'/').gt.0 .or. work(1:1).eq.'Q') return
if(verify(trim(work),'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789').ne.0) return
call pack28(work,n28); call unpack28(n28,unpacked,success)
standard_call=success .and. trim(unpacked).eq.trim(work)
end function standard_call
type(jtty_source_atom) function jtty_call_atom(action,call) result(atom)
integer, intent(in) :: action
character(len=*), intent(in) :: call
atom%kind=JTTY_ATOM_CALL; atom%subtype=action; atom%text=call
end function jtty_call_atom
type(jtty_source_atom) function jtty_exch_num_atom(role,kind,value) result(atom)
integer, intent(in) :: role,kind,value
atom%kind=JTTY_ATOM_EXCH_NUM; atom%role=role; atom%subtype=kind; atom%value=value
end function jtty_exch_num_atom
type(jtty_source_atom) function jtty_exch_loc_atom(role,kind,token) result(atom)
integer, intent(in) :: role,kind
character(len=*), intent(in) :: token
atom%kind=JTTY_ATOM_EXCH_LOC; atom%role=role; atom%subtype=kind; atom%text=token
end function jtty_exch_loc_atom
type(jtty_source_atom) function jtty_zone_loc_atom(zone,token) result(atom)
integer, intent(in) :: zone
character(len=*), intent(in) :: token
atom%kind=JTTY_ATOM_EXCH_PAIR; atom%subtype=JTTY_PAIR_ZONE_LOC3
atom%value=zone; atom%text=token
end function jtty_zone_loc_atom
type(jtty_source_atom) function jtty_class_section_atom(count,class,section_index) result(atom)
integer, intent(in) :: count,section_index
character(len=*), intent(in) :: class
atom%kind=JTTY_ATOM_EXCH_PAIR; atom%subtype=JTTY_PAIR_CLASS_SECTION
atom%value=count; atom%value2=section_index; atom%text=class
end function jtty_class_section_atom
type(jtty_source_atom) function jtty_exch_num_time_atom(role,serial,minute_of_day) result(atom)
integer, intent(in) :: role,serial,minute_of_day
atom%kind=JTTY_ATOM_EXCH_NUM_TIME; atom%role=role
atom%value=serial; atom%value2=minute_of_day
end function jtty_exch_num_time_atom
type(jtty_source_atom) function jtty_control_atom(phrase_id) result(atom)
integer, intent(in) :: phrase_id
atom%kind=JTTY_ATOM_CONTROL; atom%subtype=phrase_id
end function jtty_control_atom
type(jtty_source_atom) function jtty_grid4_atom(role,grid) result(atom)
integer, intent(in) :: role
character(len=*), intent(in) :: grid
atom%kind=JTTY_ATOM_GRID4; atom%role=role; atom%text=grid
end function jtty_grid4_atom
type(jtty_source_atom) function jtty_text5_atom(text) result(atom)
character(len=*), intent(in) :: text
integer :: n
atom%kind=JTTY_ATOM_TEXT5; atom%text=''; n=min(5,len(text))
if(n.gt.0) atom%text(1:n)=text(1:n)
end function jtty_text5_atom
subroutine pack_jtty_atoms(atoms,natoms,frames,nframes,valid,is_final)
type(jtty_source_atom), intent(in) :: atoms(:)
integer, intent(in) :: natoms
character(len=34), intent(out) :: frames(:)
integer, intent(out) :: nframes
logical, intent(out) :: valid
integer, intent(in), optional :: is_final !0/1; absent means final (existing behavior)
integer :: i
logical :: final_ok
frames=''; nframes=0
valid=natoms.ge.1 .and. natoms.le.16 .and. natoms.le.size(atoms) .and. natoms.le.size(frames)
if(.not.valid) return
final_ok=.true.
if(present(is_final)) final_ok=is_final.ne.0
do i=1,natoms
call pack_jtty_atom(atoms(i),frames(i),i.eq.natoms .and. final_ok,valid)
if(.not.valid) then; frames=''; return; endif
enddo
nframes=natoms
end subroutine pack_jtty_atoms
subroutine pack_base36(token,value,valid)
character(len=*), intent(in) :: token
integer(I8), intent(out) :: value
logical, intent(out) :: valid
integer :: i,digit
value=0; valid=len(token).eq.2 .or. len(token).eq.3
if(.not.valid) return
do i=1,len(token)
digit=index('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',token(i:i))-1
if(digit.lt.0) then; valid=.false.; return; endif
value=36*value+digit
enddo
if(len(token).eq.3 .and. value.lt.36_I8**2) valid=.false.
end subroutine pack_base36
subroutine unpack_base36(value,n,token)
integer(I8), intent(in) :: value
integer, intent(in) :: n
character(len=13), intent(out) :: token
integer(I8) :: work
integer :: i,digit
token=''; work=value
do i=n,1,-1
digit=int(mod(work,36_I8)); work=work/36
token(i:i)='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'(digit+1:digit+1)
enddo
end subroutine unpack_base36
subroutine grid4_to_index(grid,index_value,valid)
character(len=*), intent(in) :: grid
integer, intent(out) :: index_value
logical, intent(out) :: valid
integer :: a,b,c,d
index_value=0; valid=len_trim(grid).eq.4
if(.not.valid) return
a=ichar(grid(1:1))-ichar('A'); b=ichar(grid(2:2))-ichar('A')
c=ichar(grid(3:3))-ichar('0'); d=ichar(grid(4:4))-ichar('0')
valid=a.ge.0 .and. a.lt.18 .and. b.ge.0 .and. b.lt.18 .and. &
c.ge.0 .and. c.lt.10 .and. d.ge.0 .and. d.lt.10
if(valid) index_value=((a*18+b)*10+c)*10+d
end subroutine grid4_to_index
subroutine index_to_grid4(index_value,grid)
integer, intent(in) :: index_value
character(len=13), intent(out) :: grid
integer :: work,a,b,c,d
grid=''; work=index_value
d=mod(work,10); work=work/10; c=mod(work,10); work=work/10
b=mod(work,18); a=work/18
grid(1:4)=char(ichar('A')+a)//char(ichar('A')+b)//char(ichar('0')+c)//char(ichar('0')+d)
end subroutine index_to_grid4
integer function source_index(c)
character(len=1), intent(in) :: c
source_index=index(ALPHABET,c)-1
end function source_index
character(len=1) function source_char(i)
integer, intent(in) :: i
source_char=ALPHABET(i+1:i+1)
end function source_char
end module jtty_source_codec
|