1062
1063 class(TimeSeriesFileType), target, intent(inout) :: this
1064 character(len=*), intent(in) :: filename
1065 integer(I4B), intent(in) :: iout
1066 logical, optional, intent(in) :: autoDeallocate
1067
1068 integer(I4B) :: iMethod, istatus, j, nwords
1069 integer(I4B) :: ierr, inunit
1070 logical :: autoDeallocateLocal = .true.
1071 logical :: continueread, found, endOfBlock
1072 logical :: methodWasSet
1073 real(DP) :: sfaclocal
1074 character(len=40) :: keyword, keyvalue
1075 character(len=:), allocatable :: line
1076 character(len=LENTIMESERIESNAME), allocatable, dimension(:) :: words
1077
1078
1079 if (present(autodeallocate)) autodeallocatelocal = autodeallocate
1080 imethod = undefined
1081 methodwasset = .false.
1082
1083
1084 this%iout = iout
1085 this%datafile = filename
1086
1087
1088 this%inunit = getunit()
1089 inunit = this%inunit
1090 call openfile(inunit, 0, filename, 'TS6')
1091
1092
1093 call this%parser%Initialize(this%inunit, this%iout)
1094
1095
1096 continueread = .false.
1097 ierr = 0
1098
1099
1100 call this%parser%GetBlock('ATTRIBUTES', found, ierr, &
1101 supportopenclose=.true.)
1102 if (ierr /= 0) then
1103
1104 errmsg = 'End-of-file encountered while searching for'// &
1105 ' ATTRIBUTES in time-series '// &
1106 'input file "'//trim(this%datafile)//'"'
1107 call store_error(errmsg)
1108 call this%parser%StoreErrorUnit()
1109 elseif (.not. found) then
1110 errmsg = 'ATTRIBUTES block not found in time-series '// &
1111 'tsfile input file "'//trim(this%datafile)//'"'
1112 call store_error(errmsg)
1113 call this%parser%StoreErrorUnit()
1114 end if
1115
1116
1117 do
1118
1119 call this%parser%GetNextLine(endofblock)
1120 if (endofblock) exit
1121
1122
1123 call this%parser%GetStringCaps(keyword)
1124
1125
1126 if (keyword == 'NAMES') keyword = 'NAME'
1127
1128 if (keyword /= 'NAME' .and. keyword /= 'METHODS' .and. &
1129 keyword /= 'SFACS') then
1130
1131 call this%parser%GetStringCaps(keyvalue)
1132 end if
1133
1134 select case (keyword)
1135 case ('NAME')
1136
1137 call this%parser%GetRemainingLine(line)
1138 call parseline(line, nwords, words, this%parser%iuactive)
1139 this%nTimeSeries = nwords
1140
1141
1142 allocate (this%timeSeries(this%nTimeSeries))
1143 do j = 1, this%nTimeSeries
1144 call this%timeSeries(j)%initialize_time_series(this, words(j), &
1145 autodeallocatelocal)
1146 end do
1147 case ('METHOD')
1148 methodwasset = .true.
1149 if (this%nTimeSeries == 0) then
1150 errmsg = 'Error: NAME attribute not provided before METHOD in file: ' &
1151 //trim(filename)
1152 call store_error(errmsg)
1153 call this%parser%StoreErrorUnit()
1154 end if
1155 select case (keyvalue)
1156 case ('STEPWISE')
1157 imethod = stepwise
1158 case ('LINEAR')
1159 imethod = linear
1160 case ('LINEAREND')
1161 imethod = linearend
1162 case default
1163 errmsg = 'Unknown interpolation method: "'//trim(keyvalue)//'"'
1164 call store_error(errmsg)
1165 end select
1166 do j = 1, this%nTimeSeries
1167 this%timeSeries(j)%iMethod = imethod
1168 end do
1169 case ('METHODS')
1170 methodwasset = .true.
1171 if (this%nTimeSeries == 0) then
1172 errmsg = 'Error: NAME attribute not provided before METHODS in file: ' &
1173 //trim(filename)
1174 call store_error(errmsg)
1175 call this%parser%StoreErrorUnit()
1176 end if
1177 call this%parser%GetRemainingLine(line)
1178 call parseline(line, nwords, words, this%parser%iuactive)
1179 if (nwords < this%nTimeSeries) then
1180 errmsg = 'METHODS attribute does not list a method for'// &
1181 ' all time series.'
1182 call store_error(errmsg)
1183 call this%parser%StoreErrorUnit()
1184 end if
1185 do j = 1, this%nTimeSeries
1186 call upcase(words(j))
1187 select case (words(j))
1188 case ('STEPWISE')
1189 imethod = stepwise
1190 case ('LINEAR')
1191 imethod = linear
1192 case ('LINEAREND')
1193 imethod = linearend
1194 case default
1195 errmsg = 'Unknown interpolation method: "'//trim(words(j))//'"'
1196 call store_error(errmsg)
1197 end select
1198 this%timeSeries(j)%iMethod = imethod
1199 end do
1200 case ('SFAC')
1201 if (this%nTimeSeries == 0) then
1202 errmsg = 'NAME attribute not provided before SFAC in file: ' &
1203 //trim(filename)
1204 call store_error(errmsg)
1205 call this%parser%StoreErrorUnit()
1206 end if
1207 read (keyvalue, *, iostat=istatus) sfaclocal
1208 if (istatus /= 0) then
1209 errmsg = 'Error reading numeric value from: "'//trim(keyvalue)//'"'
1210 call store_error(errmsg)
1211 end if
1212 do j = 1, this%nTimeSeries
1213 this%timeSeries(j)%sfac = sfaclocal
1214 end do
1215 case ('SFACS')
1216 if (this%nTimeSeries == 0) then
1217 errmsg = 'NAME attribute not provided before SFACS in file: ' &
1218 //trim(filename)
1219 call store_error(errmsg)
1220 call this%parser%StoreErrorUnit()
1221 end if
1222 do j = 1, this%nTimeSeries
1223 sfaclocal = this%parser%GetDouble()
1224 this%timeSeries(j)%sfac = sfaclocal
1225 end do
1226 case ('AUTODEALLOCATE')
1227 do j = 1, this%nTimeSeries
1228 this%timeSeries(j)%autoDeallocate = (keyvalue == 'TRUE')
1229 end do
1230 case default
1231 errmsg = 'Unknown option found in ATTRIBUTES block: "'// &
1232 trim(keyword)//'"'
1233 call store_error(errmsg)
1234 call this%parser%StoreErrorUnit()
1235 end select
1236 end do
1237
1238
1239 call this%parser%GetBlock('TIMESERIES', found, ierr, &
1240 supportopenclose=.true.)
1241
1242
1243 if (.not. this%read_tsfile_line()) then
1244 errmsg = 'Error: No time-series data contained in file: '// &
1245 trim(this%datafile)
1246 call store_error(errmsg)
1247 end if
1248
1249
1250 if (.not. methodwasset) then
1251 errmsg = 'Interpolation method was not set. METHOD or METHODS &
1252 &must be specified in the ATTRIBUTES block for this time series file.'
1253 call store_error(errmsg)
1254 end if
1255
1256
1257 if (allocated(words)) deallocate (words)
1258
1259 if (count_errors() > 0) then
1260 call this%parser%StoreErrorUnit()
1261 end if