summaryrefslogtreecommitdiff
path: root/lib/77bit/packjt77_schema.f90
blob: c61ce8f9879a8d445fd74b5f8d7b1b232ff87095 (plain)
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
module packjt77_schema

  use, intrinsic :: iso_fortran_env, only: int64
  implicit none

  integer, parameter :: PACK77_SCHEMA_INT_KIND=int64
  integer, parameter :: PACK77_MAX_SCHEMA_FIELDS=8
  integer, parameter :: PACK77_FIELD_NAME_LEN=16
  integer, parameter :: PACK77_SCHEMA_NAME_LEN=32

  private :: NO_FIELD,put_int,get_int,begin_message,put_int_i4,put_int_i8, &
       put_bits,get_int_i4,get_int_i8,get_bits,valid_field_bounds, &
       encode_pack77_field_day,encode_pack77_type12,decode_pack77_field_day, &
       decode_pack77_type12,require_int,pack77_binary_precondition, &
       bits_are_binary

  ! Schema bit ranges use Fortran string coordinates in the 77-character c77
  ! payload: start_bit is 1-based and integer fields are encoded MSB-first.
  type bit_field
     character(len=PACK77_FIELD_NAME_LEN) :: name=''
     integer :: start_bit=0
     integer :: width=0
     integer(kind=int64) :: min_value=0_int64
     integer(kind=int64) :: max_value=-1_int64
     logical :: bitstring=.false.
  end type bit_field

  type message_schema
     integer :: i3=-1
     integer :: n3=-1
     character(len=PACK77_SCHEMA_NAME_LEN) :: name=''
     integer :: nfields=0
     type(bit_field) :: fields(PACK77_MAX_SCHEMA_FIELDS)
  end type message_schema

  type(bit_field), parameter :: NO_FIELD=bit_field('',0,0,0_int64,-1_int64,.false.)

  interface put_int
     module procedure put_int_i4
     module procedure put_int_i8
  end interface put_int

  interface get_int
     module procedure get_int_i4
     module procedure get_int_i8
  end interface get_int

  type(message_schema), parameter :: PACK77_SCHEMA_FREE_TEXT=message_schema( &
       0,0,'free text',3,(/ &
       bit_field('text_bits',1,71,0_int64,0_int64,.true.), &
       bit_field('n3',72,3,0_int64,0_int64,.false.), &
       bit_field('i3',75,3,0_int64,0_int64,.false.), &
       NO_FIELD,NO_FIELD,NO_FIELD,NO_FIELD,NO_FIELD /))

  type(message_schema), parameter :: PACK77_SCHEMA_TELEMETRY=message_schema( &
       0,5,'telemetry',5,(/ &
       bit_field('ntel1',1,23,0_int64,8388607_int64,.false.), &
       bit_field('ntel2',24,24,0_int64,16777215_int64,.false.), &
       bit_field('ntel3',48,24,0_int64,16777215_int64,.false.), &
       bit_field('n3',72,3,5_int64,5_int64,.false.), &
       bit_field('i3',75,3,0_int64,0_int64,.false.), &
       NO_FIELD,NO_FIELD,NO_FIELD /))

  type(message_schema), parameter :: PACK77_SCHEMA_DXPEDITION=message_schema( &
       0,1,'dxpedition',6,(/ &
       bit_field('n28a',1,28,0_int64,268435455_int64,.false.), &
       bit_field('n28b',29,28,0_int64,268435455_int64,.false.), &
       bit_field('n10',57,10,0_int64,1023_int64,.false.), &
       bit_field('n5',67,5,0_int64,31_int64,.false.), &
       bit_field('n3',72,3,1_int64,1_int64,.false.), &
       bit_field('i3',75,3,0_int64,0_int64,.false.), &
       NO_FIELD,NO_FIELD /))

  type(message_schema), parameter :: PACK77_SCHEMA_FIELD_DAY_LOW=message_schema( &
       0,3,'field day low',8,(/ &
       bit_field('n28a',1,28,0_int64,268435455_int64,.false.), &
       bit_field('n28b',29,28,0_int64,268435455_int64,.false.), &
       bit_field('ir',57,1,0_int64,1_int64,.false.), &
       bit_field('intx',58,4,0_int64,15_int64,.false.), &
       bit_field('nclass',62,3,0_int64,7_int64,.false.), &
       bit_field('isec',65,7,0_int64,127_int64,.false.), &
       bit_field('n3',72,3,3_int64,3_int64,.false.), &
       bit_field('i3',75,3,0_int64,0_int64,.false.) /))

  type(message_schema), parameter :: PACK77_SCHEMA_FIELD_DAY_HIGH=message_schema( &
       0,4,'field day high',8,(/ &
       bit_field('n28a',1,28,0_int64,268435455_int64,.false.), &
       bit_field('n28b',29,28,0_int64,268435455_int64,.false.), &
       bit_field('ir',57,1,0_int64,1_int64,.false.), &
       bit_field('intx',58,4,0_int64,15_int64,.false.), &
       bit_field('nclass',62,3,0_int64,7_int64,.false.), &
       bit_field('isec',65,7,0_int64,127_int64,.false.), &
       bit_field('n3',72,3,4_int64,4_int64,.false.), &
       bit_field('i3',75,3,0_int64,0_int64,.false.) /))

  type(message_schema), parameter :: PACK77_SCHEMA_WSPR_TYPE1=message_schema( &
       0,6,'wspr type 1',8,(/ &
       bit_field('n28',1,28,0_int64,268435455_int64,.false.), &
       bit_field('igrid4',29,15,0_int64,32767_int64,.false.), &
       ! idbm max 18 = nint(0.3*dBm) for dBm 0..60; range enforced here
       bit_field('idbm',44,5,0_int64,18_int64,.false.), &
       bit_field('j49',49,1,0_int64,0_int64,.false.), &
       bit_field('j50',50,1,0_int64,0_int64,.false.), &
       bit_field('pad',51,21,0_int64,0_int64,.false.), &
       bit_field('n3',72,3,6_int64,6_int64,.false.), &
       bit_field('i3',75,3,0_int64,0_int64,.false.) /))

  type(message_schema), parameter :: PACK77_SCHEMA_WSPR_TYPE2=message_schema( &
       0,6,'wspr type 2',7,(/ &
       bit_field('n28',1,28,0_int64,268435455_int64,.false.), &
       bit_field('npfx',29,16,0_int64,65535_int64,.false.), &
       ! idbm max 18 = nint(0.3*dBm) for dBm 0..60; range enforced here
       bit_field('idbm',45,5,0_int64,18_int64,.false.), &
       bit_field('j50',50,1,1_int64,1_int64,.false.), &
       bit_field('pad',51,21,0_int64,0_int64,.false.), &
       bit_field('n3',72,3,6_int64,6_int64,.false.), &
       bit_field('i3',75,3,0_int64,0_int64,.false.), &
       NO_FIELD /))

  type(message_schema), parameter :: PACK77_SCHEMA_WSPR_TYPE3=message_schema( &
       0,6,'wspr type 3',6,(/ &
       bit_field('n22',1,22,0_int64,4194303_int64,.false.), &
       bit_field('igrid6',23,25,0_int64,33554431_int64,.false.), &
       bit_field('selector',48,3,2_int64,2_int64,.false.), &
       bit_field('pad',51,21,0_int64,0_int64,.false.), &
       bit_field('n3',72,3,6_int64,6_int64,.false.), &
       bit_field('i3',75,3,0_int64,0_int64,.false.), &
       NO_FIELD,NO_FIELD /))

  type(message_schema), parameter :: PACK77_SCHEMA_TYPE1=message_schema( &
       1,0,'type 1 standard',7,(/ &
       bit_field('n28a',1,28,0_int64,268435455_int64,.false.), &
       bit_field('ipa',29,1,0_int64,1_int64,.false.), &
       bit_field('n28b',30,28,0_int64,268435455_int64,.false.), &
       bit_field('ipb',58,1,0_int64,1_int64,.false.), &
       bit_field('ir',59,1,0_int64,1_int64,.false.), &
       bit_field('igrid4',60,15,0_int64,32767_int64,.false.), &
       bit_field('i3',75,3,1_int64,1_int64,.false.), &
       NO_FIELD /))

  type(message_schema), parameter :: PACK77_SCHEMA_TYPE2=message_schema( &
       2,0,'type 2 portable',7,(/ &
       bit_field('n28a',1,28,0_int64,268435455_int64,.false.), &
       bit_field('ipa',29,1,0_int64,1_int64,.false.), &
       bit_field('n28b',30,28,0_int64,268435455_int64,.false.), &
       bit_field('ipb',58,1,0_int64,1_int64,.false.), &
       bit_field('ir',59,1,0_int64,1_int64,.false.), &
       bit_field('igrid4',60,15,0_int64,32767_int64,.false.), &
       bit_field('i3',75,3,2_int64,2_int64,.false.), &
       NO_FIELD /))

  type(message_schema), parameter :: PACK77_SCHEMA_TYPE3=message_schema( &
       3,0,'type 3 rtty',7,(/ &
       bit_field('itu',1,1,0_int64,1_int64,.false.), &
       bit_field('n28a',2,28,0_int64,268435455_int64,.false.), &
       bit_field('n28b',30,28,0_int64,268435455_int64,.false.), &
       bit_field('ir',58,1,0_int64,1_int64,.false.), &
       bit_field('irpt',59,3,0_int64,7_int64,.false.), &
       bit_field('nexch',62,13,0_int64,8191_int64,.false.), &
       bit_field('i3',75,3,3_int64,3_int64,.false.), &
       NO_FIELD /))

  type(message_schema), parameter :: PACK77_SCHEMA_TYPE4=message_schema( &
       4,0,'type 4 nonstandard',6,(/ &
       bit_field('n12',1,12,0_int64,4095_int64,.false.), &
       bit_field('n58',13,58,0_int64,288230376151711743_int64,.false.), &
       bit_field('iflip',71,1,0_int64,1_int64,.false.), &
       bit_field('nrpt',72,2,0_int64,3_int64,.false.), &
       bit_field('icq',74,1,0_int64,1_int64,.false.), &
       bit_field('i3',75,3,4_int64,4_int64,.false.), &
       NO_FIELD,NO_FIELD /))

  type(message_schema), parameter :: PACK77_SCHEMA_TYPE5=message_schema( &
       5,0,'type 5 eu vhf',7,(/ &
       bit_field('n12',1,12,0_int64,4095_int64,.false.), &
       bit_field('n22',13,22,0_int64,4194303_int64,.false.), &
       bit_field('ir',35,1,0_int64,1_int64,.false.), &
       bit_field('irpt',36,3,0_int64,7_int64,.false.), &
       bit_field('iserial',39,11,1_int64,2047_int64,.false.), &
       ! igrid6 max 18662399 = EU-VHF grid6 range, enforced here
       ! (WSPR type3 igrid6 keeps the full 2^25-1 range)
       bit_field('igrid6',50,25,0_int64,18662399_int64,.false.), &
       bit_field('i3',75,3,5_int64,5_int64,.false.), &
       NO_FIELD /))

  type pack77_free_text_fields
     character(len=71) :: text_bits=''
  end type pack77_free_text_fields

  type pack77_telemetry_fields
     integer :: ntel1=0
     integer :: ntel2=0
     integer :: ntel3=0
  end type pack77_telemetry_fields

  type pack77_dxpedition_fields
     integer :: n28a=0
     integer :: n28b=0
     integer :: n10=0
     integer :: n5=0
  end type pack77_dxpedition_fields

  type pack77_field_day_fields
     integer :: n28a=0
     integer :: n28b=0
     integer :: ir=0
     integer :: intx=0
     integer :: nclass=0
     integer :: isec=0
  end type pack77_field_day_fields

  type pack77_wspr_type1_fields
     integer :: n28=0
     integer :: igrid4=0
     integer :: idbm=0
  end type pack77_wspr_type1_fields

  type pack77_wspr_type2_fields
     integer :: n28=0
     integer :: npfx=0
     integer :: idbm=0
  end type pack77_wspr_type2_fields

  type pack77_wspr_type3_fields
     integer :: n22=0
     integer :: igrid6=0
  end type pack77_wspr_type3_fields

  type pack77_type12_fields
     integer :: n28a=0
     integer :: ipa=0
     integer :: n28b=0
     integer :: ipb=0
     integer :: ir=0
     integer :: igrid4=0
  end type pack77_type12_fields

  type pack77_type3_fields
     integer :: itu=0
     integer :: n28a=0
     integer :: n28b=0
     integer :: ir=0
     integer :: irpt=0
     integer :: nexch=0
  end type pack77_type3_fields

  type pack77_type4_fields
     integer :: n12=0
     integer(kind=int64) :: n58=0_int64
     integer :: iflip=0
     integer :: nrpt=0
     integer :: icq=0
  end type pack77_type4_fields

  type pack77_type5_fields
     integer :: n12=0
     integer :: n22=0
     integer :: ir=0
     integer :: irpt=0
     integer :: iserial=0
     integer :: igrid6=0
  end type pack77_type5_fields
contains


  subroutine begin_message(c77,ok)
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    c77=repeat('0',len(c77))
    ok=.true.
  end subroutine begin_message

  subroutine put_int_i4(ok,c77,field,value)
    logical, intent(inout) :: ok
    character(len=77), intent(inout) :: c77
    type(bit_field), intent(in) :: field
    integer, intent(in) :: value

    call put_int_i8(ok,c77,field,int(value,kind=int64))
  end subroutine put_int_i4

  subroutine put_int_i8(ok,c77,field,value)
    logical, intent(inout) :: ok
    character(len=77), intent(inout) :: c77
    type(bit_field), intent(in) :: field
    integer(kind=int64), intent(in) :: value
    integer :: i
    integer(kind=int64) :: place

    if(.not.ok) return
    ok=.false.
    if(.not.valid_field_bounds(field,len(c77))) return
    if(field%bitstring) return
    if(value.lt.field%min_value .or. value.gt.field%max_value) return
    do i=field%width,1,-1
       place=2_int64**(field%width-i)
       if(mod(value/place,2_int64).eq.1_int64) then
          c77(field%start_bit+i-1:field%start_bit+i-1)='1'
       else
          c77(field%start_bit+i-1:field%start_bit+i-1)='0'
       endif
    enddo
    ok=.true.
  end subroutine put_int_i8

  subroutine put_bits(ok,c77,field,bits)
    logical, intent(inout) :: ok
    character(len=77), intent(inout) :: c77
    type(bit_field), intent(in) :: field
    character(len=*), intent(in) :: bits
    integer :: i

    if(.not.ok) return
    ok=.false.
    if(.not.valid_field_bounds(field,len(c77))) return
    if(.not.field%bitstring) return
    if(len_trim(bits).ne.field%width) return
    do i=1,field%width
       if(bits(i:i).ne.'0' .and. bits(i:i).ne.'1') return
    enddo
    c77(field%start_bit:field%start_bit+field%width-1)=bits(1:field%width)
    ok=.true.
  end subroutine put_bits

  subroutine get_int_i4(ok,c77,field,value)
    logical, intent(inout) :: ok
    character(len=77), intent(in) :: c77
    type(bit_field), intent(in) :: field
    integer, intent(out) :: value
    integer(kind=int64) :: int64_value

    value=0
    call get_int_i8(ok,c77,field,int64_value)
    if(ok) value=int(int64_value)
  end subroutine get_int_i4

  subroutine get_int_i8(ok,c77,field,value)
    logical, intent(inout) :: ok
    character(len=77), intent(in) :: c77
    type(bit_field), intent(in) :: field
    integer(kind=int64), intent(out) :: value
    integer :: i

    value=0_int64
    if(.not.ok) return
    ok=.false.
    if(.not.valid_field_bounds(field,len(c77))) return
    if(field%bitstring) return
    do i=0,field%width-1
       value=value*2_int64
       if(c77(field%start_bit+i:field%start_bit+i).eq.'1') then
          value=value+1_int64
       else if(c77(field%start_bit+i:field%start_bit+i).ne.'0') then
          return
       endif
    enddo
    if(value.lt.field%min_value .or. value.gt.field%max_value) return
    ok=.true.
  end subroutine get_int_i8

  subroutine get_bits(ok,c77,field,bits)
    logical, intent(inout) :: ok
    character(len=77), intent(in) :: c77
    type(bit_field), intent(in) :: field
    character(len=*), intent(out) :: bits

    bits=''
    if(.not.ok) return
    ok=.false.
    if(.not.valid_field_bounds(field,len(c77))) return
    if(.not.field%bitstring) return
    if(field%width.gt.len(bits)) return
    if(.not.bits_are_binary(c77(field%start_bit:field%start_bit+field%width-1))) return
    bits(1:field%width)=c77(field%start_bit:field%start_bit+field%width-1)
    ok=.true.
  end subroutine get_bits

  logical function valid_field_bounds(field,nbits) result(ok)
    type(bit_field), intent(in) :: field
    integer, intent(in) :: nbits

    ok=.false.
    if(field%start_bit.lt.1 .or. field%width.lt.1) return
    if(field%start_bit+field%width-1.gt.nbits) return
    ok=.true.
  end function valid_field_bounds

  subroutine encode_pack77_free_text(text_bits,c77,ok)
    character(len=*), intent(in) :: text_bits
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok
    character(len=71) :: packed_text

    call begin_message(c77,ok)
    if(len(text_bits).ne.71) then
       ok=.false.
       return
    endif
    packed_text=text_bits
    call put_bits(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(1),packed_text)
    call put_int(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(2),0)
    call put_int(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(3),0)
  end subroutine encode_pack77_free_text

  subroutine encode_pack77_telemetry(ntel1,ntel2,ntel3,c77,ok)
    integer, intent(in) :: ntel1,ntel2,ntel3
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call begin_message(c77,ok)
    call put_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(1),ntel1)
    call put_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(2),ntel2)
    call put_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(3),ntel3)
    call put_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(4),5)
    call put_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(5),0)
  end subroutine encode_pack77_telemetry

  subroutine encode_pack77_dxpedition(n28a,n28b,n10,n5,c77,ok)
    integer, intent(in) :: n28a,n28b,n10,n5
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call begin_message(c77,ok)
    call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(1),n28a)
    call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(2),n28b)
    call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(3),n10)
    call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(4),n5)
    call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(5),1)
    call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(6),0)
  end subroutine encode_pack77_dxpedition

  subroutine encode_pack77_field_day(schema,n28a,n28b,ir,intx,nclass,isec,c77,ok)
    type(message_schema), intent(in) :: schema
    integer, intent(in) :: n28a,n28b,ir,intx,nclass,isec
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call begin_message(c77,ok)
    call put_int(ok,c77,schema%fields(1),n28a)
    call put_int(ok,c77,schema%fields(2),n28b)
    call put_int(ok,c77,schema%fields(3),ir)
    call put_int(ok,c77,schema%fields(4),intx)
    call put_int(ok,c77,schema%fields(5),nclass)
    call put_int(ok,c77,schema%fields(6),isec)
    call put_int(ok,c77,schema%fields(7),schema%n3)
    call put_int(ok,c77,schema%fields(8),0)
  end subroutine encode_pack77_field_day

  subroutine encode_pack77_field_day_low(n28a,n28b,ir,intx,nclass,isec,c77,ok)
    integer, intent(in) :: n28a,n28b,ir,intx,nclass,isec
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call encode_pack77_field_day(PACK77_SCHEMA_FIELD_DAY_LOW, &
         n28a,n28b,ir,intx,nclass,isec,c77,ok)
  end subroutine encode_pack77_field_day_low

  subroutine encode_pack77_field_day_high(n28a,n28b,ir,intx,nclass,isec,c77,ok)
    integer, intent(in) :: n28a,n28b,ir,intx,nclass,isec
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call encode_pack77_field_day(PACK77_SCHEMA_FIELD_DAY_HIGH, &
         n28a,n28b,ir,intx,nclass,isec,c77,ok)
  end subroutine encode_pack77_field_day_high

  subroutine encode_pack77_wspr_type1(n28,igrid4,idbm,c77,ok)
    integer, intent(in) :: n28,igrid4,idbm
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call begin_message(c77,ok)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(1),n28)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(2),igrid4)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(3),idbm)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(4),0)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(5),0)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(6),0)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(7),6)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(8),0)
  end subroutine encode_pack77_wspr_type1

  subroutine encode_pack77_wspr_type2(n28,npfx,idbm,c77,ok)
    integer, intent(in) :: n28,npfx,idbm
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call begin_message(c77,ok)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(1),n28)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(2),npfx)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(3),idbm)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(4),1)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(5),0)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(6),6)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(7),0)
  end subroutine encode_pack77_wspr_type2

  subroutine encode_pack77_wspr_type3(n22,igrid6,c77,ok)
    integer, intent(in) :: n22,igrid6
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call begin_message(c77,ok)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(1),n22)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(2),igrid6)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(3),2)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(4),0)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(5),6)
    call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(6),0)
  end subroutine encode_pack77_wspr_type3

  subroutine encode_pack77_type12(schema,n28a,ipa,n28b,ipb,ir,igrid4,c77,ok)
    type(message_schema), intent(in) :: schema
    integer, intent(in) :: n28a,ipa,n28b,ipb,ir,igrid4
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call begin_message(c77,ok)
    call put_int(ok,c77,schema%fields(1),n28a)
    call put_int(ok,c77,schema%fields(2),ipa)
    call put_int(ok,c77,schema%fields(3),n28b)
    call put_int(ok,c77,schema%fields(4),ipb)
    call put_int(ok,c77,schema%fields(5),ir)
    call put_int(ok,c77,schema%fields(6),igrid4)
    call put_int(ok,c77,schema%fields(7),schema%i3)
  end subroutine encode_pack77_type12

  subroutine encode_pack77_type1(n28a,ipa,n28b,ipb,ir,igrid4,c77,ok)
    integer, intent(in) :: n28a,ipa,n28b,ipb,ir,igrid4
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call encode_pack77_type12(PACK77_SCHEMA_TYPE1, &
         n28a,ipa,n28b,ipb,ir,igrid4,c77,ok)
  end subroutine encode_pack77_type1

  subroutine encode_pack77_type2(n28a,ipa,n28b,ipb,ir,igrid4,c77,ok)
    integer, intent(in) :: n28a,ipa,n28b,ipb,ir,igrid4
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call encode_pack77_type12(PACK77_SCHEMA_TYPE2, &
         n28a,ipa,n28b,ipb,ir,igrid4,c77,ok)
  end subroutine encode_pack77_type2

  subroutine encode_pack77_type3(itu,n28a,n28b,ir,irpt,nexch,c77,ok)
    integer, intent(in) :: itu,n28a,n28b,ir,irpt,nexch
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call begin_message(c77,ok)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(1),itu)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(2),n28a)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(3),n28b)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(4),ir)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(5),irpt)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(6),nexch)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(7),3)
  end subroutine encode_pack77_type3

  subroutine encode_pack77_type4(n12,n58,iflip,nrpt,icq,c77,ok)
    integer, intent(in) :: n12,iflip,nrpt,icq
    integer(kind=int64), intent(in) :: n58
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call begin_message(c77,ok)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(1),n12)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(2),n58)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(3),iflip)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(4),nrpt)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(5),icq)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(6),4)
  end subroutine encode_pack77_type4

  subroutine encode_pack77_type5(n12,n22,ir,irpt,iserial,igrid6,c77,ok)
    integer, intent(in) :: n12,n22,ir,irpt,iserial,igrid6
    character(len=77), intent(out) :: c77
    logical, intent(out) :: ok

    call begin_message(c77,ok)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(1),n12)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(2),n22)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(3),ir)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(4),irpt)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(5),iserial)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(6),igrid6)
    call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(7),5)
  end subroutine encode_pack77_type5

  subroutine decode_pack77_tag(c77,i3,n3,ok,binary_checked)
    character(len=77), intent(in) :: c77
    integer, intent(out) :: i3,n3
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked
    integer(kind=int64) :: value

    i3=-1
    n3=-1
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,bit_field('i3',75,3,0_int64,7_int64,.false.),value)
    if(.not.ok) return
    i3=int(value)
    if(i3.eq.0) then
       call get_int(ok,c77,bit_field('n3',72,3,0_int64,7_int64,.false.),value)
       if(.not.ok) return
       n3=int(value)
    else
       n3=0
    endif
  end subroutine decode_pack77_tag

  integer function pack77_wspr_payload_type(c77,binary_checked) result(wspr_type)
    character(len=77), intent(in) :: c77
    logical, intent(in), optional :: binary_checked
    type(bit_field) :: selector
    integer :: value
    logical :: ok

    wspr_type=0
    ok=pack77_binary_precondition(c77,binary_checked)
    selector=PACK77_SCHEMA_WSPR_TYPE2%fields(4)
    selector%min_value=0_int64
    call get_int(ok,c77,selector,value)
    if(.not.ok) return
    if(value.eq.1) then
       wspr_type=2
       return
    endif

    selector=PACK77_SCHEMA_WSPR_TYPE1%fields(4)
    selector%max_value=1_int64
    call get_int(ok,c77,selector,value)
    if(.not.ok) return
    if(value.eq.0) then
       wspr_type=1
    else
       ! Selector 110 belongs to this dispatch path but fails Type 3 decoding.
       wspr_type=3
    endif
  end function pack77_wspr_payload_type

  subroutine decode_pack77_free_text(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_free_text_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_free_text_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_bits(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(1),fields%text_bits)
    call require_int(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(2),0)
    call require_int(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(3),0)
  end subroutine decode_pack77_free_text

  subroutine decode_pack77_telemetry(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_telemetry_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_telemetry_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(1),fields%ntel1)
    call get_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(2),fields%ntel2)
    call get_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(3),fields%ntel3)
    call require_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(4),5)
    call require_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(5),0)
  end subroutine decode_pack77_telemetry

  subroutine decode_pack77_dxpedition(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_dxpedition_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_dxpedition_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(1),fields%n28a)
    call get_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(2),fields%n28b)
    call get_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(3),fields%n10)
    call get_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(4),fields%n5)
    call require_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(5),1)
    call require_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(6),0)
  end subroutine decode_pack77_dxpedition

  subroutine decode_pack77_field_day_low(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_field_day_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    call decode_pack77_field_day(PACK77_SCHEMA_FIELD_DAY_LOW,c77,fields, &
         ok,binary_checked)
  end subroutine decode_pack77_field_day_low

  subroutine decode_pack77_field_day_high(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_field_day_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    call decode_pack77_field_day(PACK77_SCHEMA_FIELD_DAY_HIGH,c77,fields, &
         ok,binary_checked)
  end subroutine decode_pack77_field_day_high

  subroutine decode_pack77_wspr_type1(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_wspr_type1_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_wspr_type1_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(1),fields%n28)
    call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(2),fields%igrid4)
    call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(3),fields%idbm)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(4),0)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(5),0)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(6),0)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(7),6)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(8),0)
  end subroutine decode_pack77_wspr_type1

  subroutine decode_pack77_wspr_type2(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_wspr_type2_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_wspr_type2_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(1),fields%n28)
    call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(2),fields%npfx)
    call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(3),fields%idbm)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(4),1)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(5),0)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(6),6)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(7),0)
  end subroutine decode_pack77_wspr_type2

  subroutine decode_pack77_wspr_type3(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_wspr_type3_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_wspr_type3_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(1),fields%n22)
    call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(2),fields%igrid6)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(3),2)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(4),0)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(5),6)
    call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(6),0)
  end subroutine decode_pack77_wspr_type3

  subroutine decode_pack77_type1(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_type12_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    call decode_pack77_type12(PACK77_SCHEMA_TYPE1,c77,fields,ok,binary_checked)
  end subroutine decode_pack77_type1

  subroutine decode_pack77_type2(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_type12_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    call decode_pack77_type12(PACK77_SCHEMA_TYPE2,c77,fields,ok,binary_checked)
  end subroutine decode_pack77_type2

  subroutine decode_pack77_type3(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_type3_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_type3_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(1),fields%itu)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(2),fields%n28a)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(3),fields%n28b)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(4),fields%ir)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(5),fields%irpt)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(6),fields%nexch)
    call require_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(7),3)
  end subroutine decode_pack77_type3

  subroutine decode_pack77_type4(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_type4_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_type4_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(1),fields%n12)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(2),fields%n58)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(3),fields%iflip)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(4),fields%nrpt)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(5),fields%icq)
    call require_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(6),4)
  end subroutine decode_pack77_type4

  subroutine decode_pack77_type5(c77,fields,ok,binary_checked)
    character(len=77), intent(in) :: c77
    type(pack77_type5_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_type5_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(1),fields%n12)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(2),fields%n22)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(3),fields%ir)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(4),fields%irpt)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(5),fields%iserial)
    call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(6),fields%igrid6)
    call require_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(7),5)
  end subroutine decode_pack77_type5

  subroutine decode_pack77_field_day(schema,c77,fields,ok,binary_checked)
    type(message_schema), intent(in) :: schema
    character(len=77), intent(in) :: c77
    type(pack77_field_day_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_field_day_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,schema%fields(1),fields%n28a)
    call get_int(ok,c77,schema%fields(2),fields%n28b)
    call get_int(ok,c77,schema%fields(3),fields%ir)
    call get_int(ok,c77,schema%fields(4),fields%intx)
    call get_int(ok,c77,schema%fields(5),fields%nclass)
    call get_int(ok,c77,schema%fields(6),fields%isec)
    call require_int(ok,c77,schema%fields(7),schema%n3)
    call require_int(ok,c77,schema%fields(8),0)
  end subroutine decode_pack77_field_day

  subroutine decode_pack77_type12(schema,c77,fields,ok,binary_checked)
    type(message_schema), intent(in) :: schema
    character(len=77), intent(in) :: c77
    type(pack77_type12_fields), intent(out) :: fields
    logical, intent(out) :: ok
    logical, intent(in), optional :: binary_checked

    fields=pack77_type12_fields()
    ok=pack77_binary_precondition(c77,binary_checked)
    call get_int(ok,c77,schema%fields(1),fields%n28a)
    call get_int(ok,c77,schema%fields(2),fields%ipa)
    call get_int(ok,c77,schema%fields(3),fields%n28b)
    call get_int(ok,c77,schema%fields(4),fields%ipb)
    call get_int(ok,c77,schema%fields(5),fields%ir)
    call get_int(ok,c77,schema%fields(6),fields%igrid4)
    call require_int(ok,c77,schema%fields(7),schema%i3)
  end subroutine decode_pack77_type12

  subroutine require_int(ok,c77,field,expected)
    logical, intent(inout) :: ok
    character(len=77), intent(in) :: c77
    type(bit_field), intent(in) :: field
    integer, intent(in) :: expected
    integer(kind=int64) :: value

    call get_int(ok,c77,field,value)
    if(ok) ok=value.eq.int(expected,kind=int64)
  end subroutine require_int

  logical function pack77_binary_precondition(c77,binary_checked) result(ok)
    character(len=*), intent(in) :: c77
    logical, intent(in), optional :: binary_checked

    ok=.false.
    if(present(binary_checked)) then
       if(binary_checked) then
          ! Caller asserts decode_pack77_tag has accepted this same c77.
          ok=.true.
          return
       endif
    endif
    ok=bits_are_binary(c77)
  end function pack77_binary_precondition

  logical function bits_are_binary(c77) result(ok)
    character(len=*), intent(in) :: c77
    integer :: i

    ok=.false.
    do i=1,len(c77)
       if(c77(i:i).ne.'0' .and. c77(i:i).ne.'1') return
    enddo
    ok=.true.
  end function bits_are_binary

end module packjt77_schema