-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatamod.f90
1149 lines (813 loc) · 34.3 KB
/
statamod.f90
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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! This file is part of STATAMOD. Copyright (c) 2006-2016 Andrew Shephard
!
! STATAMOD is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! STATAMOD is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with STATAMOD. If not, see <http://www.gnu.org/licenses/>.
module stataMod
implicit none
private
! public access
public :: openStata, saveStata, closeOpenStata, closeSaveStata, readStata
public :: writeStata, descStata, nobsStata, nvarStata, existStata
public :: sampleStata
! data types
integer, parameter :: sp = selected_real_kind(6, 30)
integer, parameter :: dp = selected_real_kind(15, 100)
integer, parameter :: i4 = selected_int_kind(5)
integer, parameter :: i2 = selected_int_kind(3)
integer, parameter :: i1 = selected_int_kind(1)
integer, parameter, public :: maxST_byte = 100
integer, parameter, public :: maxST_int = 32740
integer, parameter, public :: maxST_long = 2147483620
real(sp), parameter, public :: maxST_float = real(Z'7effffff', sp) ! approx +1.701e+38
real(dp), parameter, public :: maxST_double = real(Z'7fdfffffffffffff', dp) ! approx +8.988e+307
integer, parameter, public :: minST_byte = -127
integer, parameter, public :: minST_int = -32767
integer, parameter, public :: minST_long = -2147483647
real(sp), parameter, public :: minST_float = real(Z'feffffff', sp) ! approx -1.701e+38
real(dp), parameter, public :: minST_double = real(Z'ffefffffffffffff', dp) ! approx -1.798e+308
type :: position
integer :: start
integer, dimension(:), allocatable :: offset
integer, dimension(:), allocatable :: varpos
integer :: nvaroffset
end type position
type stcell !use linked list when saving
real(dp), dimension(:), allocatable :: valReal8
real(sp), dimension(:), allocatable :: valReal4
integer(i4), dimension(:), allocatable :: valInt4
integer(i2), dimension(:), allocatable :: valInt2
integer(i1), dimension(:), allocatable :: valInt1
integer(i2) :: valType
character(33) :: name
character(81) :: label
type(stcell), pointer :: next
end type stcell
type :: stata
! file header
integer(i1) :: ds_format
integer(i1) :: byteorder
integer(i1) :: filetype
integer(i2) :: nvar
integer(i4) :: nobs
character(81) :: data_label
character(18) :: time_stamp
! descriptors
integer(i2), dimension(:), allocatable :: typlist
character(33), dimension(:), allocatable :: varlist
integer(i2), dimension(:), allocatable :: srtlist
character(49), dimension(:), allocatable :: fmtlist
character(33), dimension(:), allocatable :: lbllist
!variable labels
character(81), dimension(:), allocatable :: varlabl
character(1), dimension(:,:), allocatable :: thesedata !enitre data if cached
integer :: unit !unit number
logical :: cache !cache data?
logical :: init = .false. !initialised?
logical :: doSample = .false.
integer, dimension(:), allocatable :: sampleWeight
type(position) :: stataPosition !file position
! the remaining information is for when Stata files are written
integer(i4) :: saveDim !array dim size
integer(i4) :: saveUnit !unit number
integer(i4) :: saveNObs !save observations
integer(i2) :: saveNVar !save variables
logical :: saveInit = .false. !initialised?
logical :: saveOnce !has a variable been written?
logical :: saveCache !save cache?
logical :: saveCompress !compress dataset
character(81) :: saveLabel !dataset label
character(18) :: saveTime !time stamp
type(stcell), pointer :: saveCurr, saveHead
end type stata
type(stata), save :: statafile
interface writestata
module procedure writeStataInt1
module procedure writeStataInt2
module procedure writeStataInt4
module procedure writeStataReal4
module procedure writeStataReal8
end interface writestata
interface readStata
module procedure readStataInt1
module procedure readStataInt2
module procedure readStataInt4
module procedure readStataReal4
module procedure readStataReal8
module procedure readStataLogical
end interface readStata
contains
subroutine sampleStata(sampleWeight)
implicit none
integer(i4), intent(in), optional :: sampleWeight(:)
if (.not. stataFile%init) then
call statamodError('STATA file is not open')
end if
if (present(sampleWeight)) then
if (size(sampleWeight) .ne. stataFile%nobs) then
call statamodError('sampleWeight dimension does not equal number of observations')
end if
if ( (minval(sampleWeight)<1) .or. (maxval(sampleWeight)>stataFile%nobs) ) then
call statamodError('sampleWeight is out of range')
end if
stataFile%doSample = .true.
if (allocated(stataFile%sampleWeight)) then
deallocate(stataFile%sampleWeight)
end if
allocate(stataFile%sampleWeight(stataFile%nobs))
stataFile%sampleWeight = sampleWeight
else
stataFile%doSample = .false.
end if
end subroutine sampleStata
subroutine saveStata(fileName, obs, label)
implicit none
character(*), intent(in) :: fileName
integer(i4), intent(in) :: obs
character(*), intent(in), optional :: label
integer(i1) :: tempInt1
integer(i4) :: ios
if (stataFile%saveInit) then
call statamodError('A file has already been specified for saving')
end if
if (obs < 1) then
call statamodError('Expecting at least one observation to be declared')
end if
stataFile%saveNObs = obs
stataFile%saveNVar = 0
stataFile%saveOnce = .false.
stataFile%saveTime = dateStata()
if (present(label)) then
tempInt1 = len(label) + 1
stataFile%saveLabel = label
stataFile%saveLabel(tempInt1:81) = char(0)
else
stataFile%saveLabel = char(0)
end if
open(NEWUNIT = stataFile%saveUnit, FILE = fileName, ACTION = 'write', STATUS = 'replace', ACCESS = 'stream', IOSTAT = ios)
if (ios /= 0) then
call statamodError('error saving file ' // fileName)
end if
!save as stata 7 s/e
write(stataFile%saveUnit, IOSTAT = ios) 111_i1 ! Stata 7 S/E
write(stataFile%saveUnit, IOSTAT = ios) 2_i1 ! byte order
write(stataFile%saveUnit, IOSTAT = ios) 1_i1 ! filetype
write(stataFile%saveUnit, IOSTAT = ios) 0_i1 ! junk
if (ios /= 0) then
call statamodError('error writing file header')
end if
stataFile%saveInit = .true.
stataFile%saveDim = 0
end subroutine saveStata
subroutine writeStataReal4(thisVar, thisName, thisLabel)
implicit none
real(sp), dimension(:), intent(in) :: thisVar
include 'writestata1.inc'
! var type
stataFile%saveCurr%valType = 254
stataFile%saveDim = stataFile%saveDim + 4
! copy contents
allocate(stataFile%saveCurr%valReal4(stataFile%saveNObs), STAT = ios)
if (ios /= 0) then
call statamodError('error allocating memory')
end if
stataFile%saveCurr%valReal4 = thisVar
include 'writestata2.inc'
end subroutine writeStataReal4
subroutine writeStataReal8(thisVar, thisName, thisLabel)
implicit none
real(dp), dimension(:), intent(in) :: thisVar
include 'writestata1.inc'
! var type
stataFile%saveCurr%valType = 255
stataFile%saveDim = stataFile%saveDim + 8
! copy contents
allocate(stataFile%saveCurr%valReal8(stataFile%saveNObs), STAT = ios)
if (ios /= 0) then
call statamodError('error allocating memory')
end if
stataFile%saveCurr%valReal8 = thisVar
include 'writestata2.inc'
end subroutine writeStataReal8
subroutine writeStataInt1(thisVar, thisName, thisLabel)
implicit none
integer(i1), dimension(:), intent(in) :: thisVar
include 'writestata1.inc'
! var type
stataFile%saveCurr%valType = 251
stataFile%saveDim = stataFile%saveDim + 1
! copy contents
allocate(stataFile%saveCurr%valInt1(stataFile%saveNObs), STAT = ios)
if (ios /= 0) then
call statamodError('error allocating memory')
end if
stataFile%saveCurr%valInt1 = thisVar
include 'writestata2.inc'
end subroutine writeStataInt1
subroutine writeStataInt2(thisVar, thisName, thisLabel)
implicit none
integer(i2), dimension(:), intent(in) :: thisVar
include 'writestata1.inc'
! var type
stataFile%saveCurr%valType = 252
stataFile%saveDim = stataFile%saveDim + 2
! copy contents
allocate(stataFile%saveCurr%valInt2(stataFile%saveNObs), STAT = ios)
if (ios /= 0) then
call statamodError('error allocating memory')
end if
stataFile%saveCurr%valInt2 = thisVar
include 'writestata2.inc'
end subroutine writeStataInt2
subroutine writeStataInt4(thisVar, thisName, thisLabel)
implicit none
integer(i4), dimension(:), intent(in) :: thisVar
include 'writestata1.inc'
! var type
stataFile%saveCurr%valType = 253
stataFile%saveDim = stataFile%saveDim + 4
! copy contents
allocate(stataFile%saveCurr%valInt4(stataFile%saveNObs), STAT = ios)
if (ios /= 0) then
call statamodError('error allocating memory')
end if
stataFile%saveCurr%valInt4 = thisVar
include 'writestata2.inc'
end subroutine writeStataInt4
subroutine closeSaveStata(cache)
implicit none
logical, intent(in), optional :: cache
integer(i4) :: ios, tempInt4, i, j
character(1) :: unsignedByte !unsigned integers are not supported. use this as work around
integer(i1), dimension(2*(stataFile%saveNVar + 1)) :: srtlist
character(12), dimension(stataFile%saveNVar) :: fmtlist
character(33), dimension(stataFile%saveNVar) :: lbllist
integer(i1), dimension(5) :: expfield
character(stataFile%saveDim), allocatable, dimension(:) :: dataset
if (.not. stataFile%saveInit) then
call statamodError('no file have been specified for saving')
end if
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveNVar !Variables
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveNObs !Observations
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveLabel !Label
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveTime !time stamp
if (ios /= 0) then
call statamodError('error writing file specific header information')
end if
! write descriptors
! typlist
stataFile%saveCurr => stataFile%saveHead
tempInt4 = 0
do i = 1, stataFile%saveNVar
unsignedByte = char(stataFile%saveCurr%valType) !work around
write(stataFile%saveUnit, IOSTAT = ios) unsignedByte
stataFile%saveCurr => stataFile%saveCurr%next
end do !i
! varlist
stataFile%saveCurr => stataFile%saveHead
do i = 1, stataFile%saveNVar
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveCurr%name
stataFile%saveCurr => stataFile%saveCurr%next
end do !i
! srtlist (unsorted)
srtlist = 0
write(stataFile%saveUnit, IOSTAT = ios) srtlist
! fmtlist (use default stata format)
fmtlist = '%8.0g'//char(0)
write(stataFile%saveUnit, IOSTAT = ios) fmtlist
! lbllist
lbllist = char(0)
write(stataFile%saveUnit, IOSTAT = ios) lbllist
if (ios /= 0) then
call statamodError('error writing descriptors')
end if
! write variable labels
stataFile%saveCurr => stataFile%saveHead
do i = 1, stataFile%saveNVar
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveCurr%label
stataFile%saveCurr => stataFile%saveCurr%next
end do !i
if (ios /= 0) then
call statamodError('error writing variable labels')
end if
! write expansion fields
expfield = 0
write(stataFile%saveUnit, IOSTAT = ios) expfield
if (ios /= 0) then
call statamodError('error writing expansion fields')
end if
! write actual variables
if (present(cache)) then
stataFile%saveCache = cache
else
stataFile%saveCache = .true.
end if
if (stataFile%saveCache) then
! because the data is of potentially different types, we must construct
! an appropriately sized character array and use the transfer function
! to copy exact bit pattern
allocate(dataset(stataFile%saveNObs), STAT = ios)
if (ios /= 0) then
call statamodError('error allocating memory for variable writing. Use closeSaveStata(.false.).')
end if
stataFile%saveCurr => stataFile%saveHead
tempInt4 = 1
do i = 1, stataFile%saveNVar
select case(stataFile%saveCurr%valType)
case(251_i2) !ST_byte
dataset(:)(tempInt4:tempInt4) = transfer(stataFile%saveCurr%valInt1, (/1_'0'/))
tempInt4 = tempInt4 + 1
case(252_i2) !ST_int
dataset(:)(tempInt4:tempInt4+1) = transfer(stataFile%saveCurr%valInt2, (/1_'00'/))
tempInt4 = tempInt4 + 2
case(253_i2) !ST_long
dataset(:)(tempInt4:tempInt4+3) = transfer(stataFile%saveCurr%valInt4, (/1_'0000'/))
tempInt4 = tempInt4 + 4
case(254_i2) !ST_float
dataset(:)(tempInt4:tempInt4+3) = transfer(stataFile%saveCurr%valReal4, (/1_'0000'/))
tempInt4 = tempInt4 + 4
case(255_i2) !ST_double
dataset(:)(tempInt4:tempInt4+7) = transfer(stataFile%saveCurr%valReal8, (/1_'00000000'/))
tempInt4 = tempInt4 + 8
case default
call statamodError('unknown data type')
end select
stataFile%saveCurr => stataFile%saveCurr%next
end do !i
write(stataFile%saveUnit, IOSTAT = ios) dataset
else
do i = 1, stataFile%saveNObs
stataFile%saveCurr => stataFile%saveHead
do j = 1, stataFile%saveNVar
select case(stataFile%saveCurr%valType)
case(251_i2) !ST_byte
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveCurr%valInt1(i:i)
case(252_i2) !ST_int
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveCurr%valInt2(i:i)
case(253_i2) !ST_long
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveCurr%valInt4(i:i)
case(254_i2) !ST_float
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveCurr%valReal4(i:i)
case(255_i2) !ST_double
write(stataFile%saveUnit, IOSTAT = ios) stataFile%saveCurr%valReal8(i:i)
case default
call statamodError('unknown data type')
end select
stataFile%saveCurr => stataFile%saveCurr%next
end do !j
end do !i
end if
if (ios /= 0) then
call statamodError('error writing actual data contents')
end if
! deallocate memory and close file
stataFile%saveCurr => stataFile%saveHead
do while(associated(stataFile%saveCurr))
stataFile%saveHead => stataFile%saveCurr%next
deallocate(stataFile%saveCurr, STAT = ios)
if (ios /= 0) call statamodWarn('error deallocating memory')
stataFile%saveCurr => stataFile%saveHead
end do
stataFile%saveInit = .false.
!if (stataFile%saveCache) then
! deallocate(dataset)
!end if
!if (allocated(dataset)) deallocate(dataset)
close(stataFile%saveUnit)
call statamodMsg('STATA file successfully saved')
end subroutine closeSaveStata
subroutine openStata(fileName, cache)
implicit none
character(*), intent(in) :: fileName
logical, intent(in), optional :: cache
logical :: ex
integer(i4) :: ios
integer(i4) :: i, j
integer(i1) :: tempInt1 !temporary integers
integer(i4) :: tempInt4
character(9) :: str9 !temporary strings
character(32) :: str32
character(33) :: str33
character(81) :: str81
logical :: cacheData = .false.
character(1) :: unsignedByte !unsigned integers are not supported. use this as work around
integer :: filePos ! file position
if (stataFile%init) then
call statamodError('STATA file already open')
end if
stataFile%init = .false. !change to true if successful
! open file
inquire(FILE = fileName, EXIST = ex)
if (.not. ex) then
call statamodError('file ' // fileName // ' does not exist')
end if
open(NEWUNIT = stataFile%unit, FILE = fileName, action = 'read', status = 'old', access = 'stream', iostat = ios)
! error check
if (ios /= 0) then
call statamodError('error opening file ' // filename)
end if
! read header
read(stataFile%unit, IOSTAT = ios) stataFile%ds_format
read(stataFile%unit, IOSTAT = ios) stataFile%byteorder
read(stataFile%unit, IOSTAT = ios) stataFile%filetype
read(stataFile%unit, IOSTAT = ios) tempInt1 !junk
read(stataFile%unit, IOSTAT = ios) stataFile%nvar
read(stataFile%unit, IOSTAT = ios) stataFile%nobs
if (versionStata(stataFile%ds_format) == '5') then
read(stataFile%unit, IOSTAT = ios) str32
stataFile%data_label = str32
else
read(stataFile%unit, IOSTAT = ios) str81
stataFile%data_label = str81
end if
read(stataFile%unit, IOSTAT = ios) stataFile%time_stamp
if (ios == 1) then
call statamodError('error reading from file '//fileName)
end if
!check header
if (versionStata(stataFile%ds_format) == '?') ios = 1
if (stataFile%byteorder /=1 .and. stataFile%byteorder /= 2) ios = 1
if (stataFile%filetype /=1) ios = 1
if (stataFile%nvar < 0) ios = 1
if (stataFile%nobs < 0) ios = 1
if (ios == 1) then
call statamodError('not a valid STATA file')
end if
if (stataFile%nvar == 0) then
call statamodError('file does not contain any variables')
end if
if (stataFile%nobs == 0) then
call statamodError('file does not contain any observations')
end if
!read descriptors
allocate(stataFile%typlist(stataFile%nvar), STAT = ios)
allocate(stataFile%varlist(stataFile%nvar), STAT = ios)
allocate(stataFile%srtlist(stataFile%nvar + 1), STAT = ios)
allocate(stataFile%fmtlist(stataFile%nvar), STAT = ios)
allocate(stataFile%lbllist(stataFile%nvar), STAT = ios)
if (ios /= 0) then
call statamodError('error allocating memory')
end if
do i = 1, stataFile%nvar
read(stataFile%unit) unsignedByte
stataFile%typlist(I) = ichar(unsignedByte)
end do
do i = 1, stataFile%nvar
if (versionStata(stataFile%ds_format) == '5' .or. versionStata(stataFile%ds_format) == '6') then
read(stataFile%unit) str9
stataFile%varlist(i) = str9
else
read(stataFile%unit) str33
stataFile%varlist(i) = str33
end if
do j = 1, 33
if (stataFile%varlist(i)(j:j) == char(0)) then
stataFile%varlist(i)(j:33) = ' '
exit
end if
end do
end do
do i = 1, (stataFile%nvar + 1)
read(stataFile%unit) stataFile%srtlist(i)
end do
if (stataFile%ds_format >= 114) then
do i = 1, stataFile%nvar
read(stataFile%unit) stataFile%fmtlist(i)
end do
else
do i = 1, stataFile%nvar
read(stataFile%unit) stataFile%fmtlist(i)(1:12)
end do
end if
do i = 1, stataFile%nvar
read(stataFile%unit) stataFile%lbllist(i)
end do
!read variable labels
allocate(stataFile%varlabl(stataFile%nvar), STAT = ios)
if (ios /= 0) then
call statamodError('error allocating memory')
end if
do i = 1, stataFile%nvar
read(stataFile%unit) stataFile%varlabl(i)
end do
!skip expansion fields
tempInt1 = 1
tempInt4 = 1
inquire(unit = stataFile%unit, POS = filePos)
do while ( tempInt1 /= 0 .and. tempInt4 /=0 )
read(stataFile%unit, POS = filePos) tempInt1
read(stataFile%unit) tempInt4
inquire(unit = stataFile%unit, POS = filePos)
filePos = filePos + tempInt4
end do
!position of start of data
inquire(unit = stataFile%unit, POS = stataFile%stataPosition%start)
allocate(stataFile%stataPosition%offset(stataFile%nvar))
allocate(stataFile%stataPosition%varpos(stataFile%nvar))
stataFile%stataPosition%offset(1) = 0
stataFile%stataPosition%varpos(1) = stataFile%stataPosition%start
if (stataFile%nvar > 1) then
do i = 2, stataFile%nvar
stataFile%stataPosition%offset(i) = stataFile%stataPosition%offset(i - 1) + sizeStata(stataFile%typlist(i - 1))
stataFile%stataPosition%varpos(i) = stataFile%stataPosition%varpos(i - 1) + sizeStata(stataFile%typlist(i - 1))
end do
end if
stataFile%stataPosition%nvarOffset = stataFile%stataPosition%offset(stataFile%nvar) + &
& sizeStata(stataFile%typlist(stataFile%nvar))
!load entire dataset into memory
if (present(cache)) then
if (cache) cacheData = .true.
else
cacheData = .true.
end if
if (cacheData) then
allocate(stataFile%theseData(stataFile%stataPosition%nvarOffset, stataFile%nobs), STAT = ios)
if (ios /= 0) then
call statamodError('error allocating memory')
end if
read(stataFile%unit, IOSTAT = ios) stataFile%theseData
if (ios /=0) then
call statamodError('error reading data into memory')
end if
stataFile%cache = .true.
close(stataFile%unit)
end if
!if we reach here, data has opened okay
stataFile%init = .true.
end subroutine openStata
subroutine readStataReal4(readStataVar, varname)
implicit none
character(*), intent(in) :: varname
real(sp), intent(out), dimension(:) :: readStataVar
real(sp), dimension(size(readStataVar)) :: readStataVar2
include 'readstata.inc'
end subroutine readStataReal4
subroutine readStataReal8(readStataVar, varname)
implicit none
character(*), intent(in) :: varname
real(dp), intent(out), dimension(:) :: readStataVar
real(dp), dimension(size(readStataVar)) :: readStataVar2
include 'readstata.inc'
end subroutine readStataReal8
subroutine readStataInt1(readStataVar, varname)
implicit none
character(*), intent(in) :: varname
integer(i1), intent(out), dimension(:) :: readStataVar
integer(i1), dimension(size(readStataVar)) :: readStataVar2
include 'readstata.inc'
end subroutine readStataInt1
subroutine readStataInt2(readStataVar, varname)
implicit none
character(*), intent(in) :: varname
integer(i2), intent(out), dimension(:) :: readStataVar
integer(i2), dimension(size(readStataVar)) :: readStataVar2
include 'readstata.inc'
end subroutine readStataInt2
subroutine readStataInt4(readStataVar, varname)
implicit none
character(*), intent(in) :: varname
integer(i4), intent(out), dimension(:) :: readStataVar
integer(i4), dimension(size(readStataVar)) :: readStataVar2
include 'readstata.inc'
end subroutine readStataInt4
subroutine readStataLogical(readStataVarLogical, varname)
implicit none
character(*), intent(in) :: varname
logical, intent(out), dimension(:) :: readStataVarLogical
real(dp), dimension(size(readStataVarLogical)) :: readStataVar
real(dp), dimension(size(readStataVarLogical)) :: readStataVar2
include 'readstata.inc'
where (readStataVar > 0.0_dp)
readStataVarLogical = .true.
else where
readStataVarLogical = .false.
end where
end subroutine readStataLogical
subroutine closeOpenStata()
implicit none
integer(i4) :: err
err = 0
if (.not. stataFile%init) then
call statamodWarn('STATA file is not open')
return
end if
if (allocated(stataFile%theseData)) deallocate(stataFile%theseData, STAT = err)
if (allocated(stataFile%typlist)) deallocate(stataFile%typlist, STAT = err)
if (allocated(stataFile%varlist)) deallocate(stataFile%varlist, STAT = err)
if (allocated(stataFile%srtlist)) deallocate(stataFile%srtlist, STAT = err)
if (allocated(stataFile%fmtlist)) deallocate(stataFile%fmtlist, STAT = err)
if (allocated(stataFile%lbllist)) deallocate(stataFile%lbllist, STAT = err)
if (allocated(stataFile%varlabl)) deallocate(stataFile%varlabl, STAT = err)
if (allocated(stataFile%stataPosition%offset)) deallocate(stataFile%stataPosition%offset, STAT = err)
if (err /= 0) call statamodWarn('error deallocating memory')
if (.not. stataFile%cache) close(stataFile%unit) !already closed if cache is true
stataFile%init = .false.
end subroutine closeOpenStata
function existStata(varname) !check whether var exists in dataset
implicit none
character(*), intent(in) :: varname
logical :: existStata
integer(i4) :: thisVarno
if (.not. stataFile%init) then
call statamodWarn('STATA file is not open')
existStata = .false.
return
end if
thisVarno = varno(varname)
if (thisVarno == 0) then
existStata = .false.
else
existStata = .true.
end if
end function existStata
integer(i4) function nobsStata() !return number of observations
implicit none
if (.not. stataFile%init) then
call statamodWarn('STATA file is not open')
nobsStata = 0
return
end if
nobsStata = stataFile%nobs
end function nobsStata
integer(i2) function nvarStata() !return number of variables
implicit none
if (.not. stataFile%init) then
call statamodWarn('STATA file is not open')
nvarStata = 0
return
end if
nvarStata = stataFile%nvar
end function nvarStata
subroutine descStata() !print descriptive statistics to console
implicit none
character(24) :: intToStr
integer(i4) :: i
if (.not. stataFile%init) then
call statamodWarn('STATA file is not open')
return
end if
write (*, *) 'ST_version: ' // versionStata(stataFile%ds_format)
write (*, *) 'ST_date: ' // stataFile%time_stamp
write (intToStr, *) stataFile%nvar
write (*, *) 'ST_nvars: ' // adjustl(intToStr)
write (intToStr,*) stataFile%nobs
write (*, *) 'ST_nobs: ' // adjustl(intToStr)
write (*, *)
do i = 1, stataFile%nvar
write (*, *) typeStata(i) // stataFile%varlist(i)
end do
end subroutine descStata
character(12) function typeStata(varno) !return string with typlist description
implicit none
integer(i4), intent(in) :: varno
select case(stataFile%typlist(varno))
case(1_i2:244_i2) !str
typeStata = 'ST_str'
case(251_i2) !ST_byte
typeStata = 'ST_byte'
case(252_i2) !ST_int
typeStata = 'ST_int'
case(253_i2) !ST_long
typeStata = 'ST_long'
case(254_i2) !ST_float
typeStata = 'ST_float'
case(255_i2) !ST_double
typeStata = 'ST_double'
case default
typeStata = 'unknown'
end select
end function typeStata
integer(i2) function sizeStata(typlist) !return size of typlist
implicit none
integer(i2), intent(in) :: typlist
select case(typlist)
case(1_i2:244_i2) !str
sizeStata = typlist
case(251_i2) !ST_byte
sizeStata = 1_i2
case(252_i2) !ST_int
sizeStata = 2_i2
case(253_i2) !ST_long
sizeStata = 4_i2
case(254_i2) !ST_float