{"action":"created","comment":{"url":"https://api.github.com/repos/NCAR/summa/pulls/comments/61743653","id":61743653,"diff_hunk":"@@ -24,350 +24,314 @@ module modelwrite_module\n USE netcdf_util_module,only:netcdf_err                    ! netcdf error handling function\n implicit none\n private\n-public::writeForce\n-public::writeAttrb\n-public::writeParam\n-public::writeModel\n+public::writeParm\n+public::writeData\n public::writeBasin\n+public::writeTime\n ! define dimension lengths\n integer(i4b),parameter      :: maxSpectral=2              ! maximum number of spectral bands\n contains\n \n-\n  ! **********************************************************************************************************\n- ! public subroutine writeAttrb: write local attributes\n+ ! public subroutine writeParm: write model parameters\n  ! **********************************************************************************************************\n- subroutine writeAttrb(fileout,iHRU,attr_data,type_data,err,message)\n- USE globalData,only:attr_meta                   ! metadata for local attributes\n- USE globalData,only:type_meta                   ! metadata for local classification of veg, soil, etc.\n+ subroutine writeParm(iHRU,dat,meta,err,message)\n+ USE data_types,only:var_info,var_d,var_i        ! metadata structure type\n+ USE var_lookup,only:iLookStat                   ! to index into write flag\n+ USE multiconst,only:integerMissing\n+ USE globalData,only:ncid                        ! netcdf file ids\n  implicit none\n+\n  ! declare input variables\n- character(*), intent(in)    :: fileout          ! output file\n- integer(i4b), intent(in)    :: iHRU             ! hydrologic response unit\n- real(dp),     intent(in)    :: attr_data(:)     ! local attributes\n- integer(i4b), intent(in)    :: type_data(:)     ! local classification of veg, soil, etc.\n- ! declare output variables\n- integer(i4b),intent(out)    :: err              ! error code\n- character(*),intent(out)    :: message          ! error message\n+ integer(i4b)  ,intent(in)   :: iHRU             ! hydrologic response unit\n+ class(*)      ,intent(in)   :: dat(:)           ! local attributes\n+ type(var_info),intent(in)   :: meta(:)\n+ integer(i4b)  ,intent(out)  :: err              ! error code\n+ character(*)  ,intent(out)  :: message          ! error message\n  ! local variables\n- integer(i4b)                :: ncid             ! NetCDF file ID\n  integer(i4b)                :: iVar             ! loop through variables\n- integer(i4b)                :: iVarId           ! variable ID\n+ integer(i4b)  ,parameter    :: modelTime=1      ! these particular data are only output in the timestep file\n+\n  ! initialize error control\n- err=0;message=\"f-writeAttrb/\"\n+ err=0;message=\"f-writeParm/\"\n \n- ! open NetCDF file\n- err = nf90_open(trim(fileout),nf90_write,ncid)\n- call netcdf_err(err,message); if (err/=0) return\n+ ! loop through local column model parameters\n+ do iVar = 1,size(meta)\n \n- ! loop through local attributes\n- do iVar=1,size(attr_meta)\n   ! check that the variable is desired\n-  if (.not.attr_meta(iVar)%v_write) cycle\n-  ! initialize message\n-  message=trim(message)//trim(attr_meta(iVar)%varname)//'/'\n-  ! get variable ID\n-  err = nf90_inq_varid(ncid,trim(attr_meta(iVar)%varname),iVarId)\n-  call netcdf_err(err,message); if (err/=0) return\n-  ! write data\n-  err = nf90_put_var(ncid,iVarId,(/attr_data(iVar)/),start=(/iHRU/),count=(/1/))\n-  call netcdf_err(err,message); if (err/=0) return\n-  ! re-initialize message\n-  message=\"f-writeAttrb/\"\n- end do  ! looping through local attributes\n+  if (.not.meta(iVar)%statFlag(iLookStat%inst)) cycle\n \n- ! loop through local classification of veg, soil, etc.\n- do iVar=1,size(type_meta)\n-  ! check that the variable is desired\n-  if (.not.type_meta(iVar)%v_write) cycle\n   ! initialize message\n-  message=trim(message)//trim(type_meta(iVar)%varname)//'/'\n-  ! get variable ID\n-  err = nf90_inq_varid(ncid,trim(type_meta(iVar)%varname),iVarId)\n-  call netcdf_err(err,message); if (err/=0) return\n-  ! write data\n-  err = nf90_put_var(ncid,iVarId,(/type_data(iVar)/),start=(/iHRU/),count=(/1/))\n-  call netcdf_err(err,message); if (err/=0) return\n-  ! re-initialize message\n-  message=\"f-writeAttrb/\"\n- end do  ! looping through local classification of veg, soil, etc.\n-\n- ! close output file\n- err = nf90_close(ncid); call netcdf_err(err,message); if (err/=0) return\n- end subroutine writeAttrb\n+  message=trim(message)//trim(meta(iVar)%varName)//'/'\n \n-\n- ! **********************************************************************************************************\n- ! public subroutine writeParam: write model parameters\n- ! **********************************************************************************************************\n- subroutine writeParam(fileout,iHRU,mpar_data,bpar_data,err,message)\n- USE globalData,only:mpar_meta                   ! metadata for local-column model parameter structures\n- USE globalData,only:bpar_meta                   ! metadata for basin-average model parameter structures\n- implicit none\n- ! declare input variables\n- character(*), intent(in)    :: fileout          ! output file\n- integer(i4b), intent(in)    :: iHRU             ! hydrologic response unit\n- real(dp),     intent(in)    :: mpar_data(:)     ! vector of local-column model parameters\n- real(dp),     intent(in)    :: bpar_data(:)     ! vector of basin-aaverage model parameters\n- ! declare output variables\n- integer(i4b),intent(out)    :: err              ! error code\n- character(*),intent(out)    :: message          ! error message\n- ! local variables\n- integer(i4b)                :: ncid             ! NetCDF file ID\n- integer(i4b)                :: ipar             ! loop through model parameters\n- integer(i4b)                :: iVarId           ! variable ID\n- ! initialize error control\n- err=0;message=\"f-writeParam/\"\n-\n- ! open NetCDF file\n- err = nf90_open(trim(fileout),nf90_write,ncid)\n- call netcdf_err(err,message); if (err/=0) return\n-\n- ! loop through local column model parameters\n- do ipar=1,size(mpar_meta)\n-  ! check that the variable is desired\n-  if (.not.mpar_meta(ipar)%v_write) cycle\n-  ! initialize message\n-  message=trim(message)//trim(mpar_meta(ipar)%varname)//'/'\n-  ! get variable ID\n-  err = nf90_inq_varid(ncid,trim(mpar_meta(ipar)%varname),iVarId)\n-  call netcdf_err(err,message); if (err/=0) return\n   ! write data\n-  err = nf90_put_var(ncid,iVarId,(/mpar_data(ipar)/),start=(/iHRU/),count=(/1/))\n+  if (iHRU.ne.integerMissing) then\n+   selecttype (dat)\n+    typeis (integer)\n+     err = nf90_put_var(ncid(modelTime),meta(iVar)%ncVarID(iLookStat%inst),(/dat(iVar)/),start=(/iHRU/),count=(/1/))\n+    typeis (real(dp))\n+     err = nf90_put_var(ncid(modelTime),meta(iVar)%ncVarID(iLookStat%inst),(/dat(iVar)/),start=(/iHRU/),count=(/1/))\n+    class default; err=20; message=trim(message)//'unkonwn dat type (with HRU)'; return\n+   endselect\n+  else\n+   selecttype (dat)\n+    typeis (real(dp))\n+     err = nf90_put_var(ncid(modelTime),meta(iVar)%ncVarID(iLookStat%inst),(/dat(iVar)/),start=(/1/),count=(/1/))\n+    class default; err=20; message=trim(message)//'unkonwn dat type (no HRU)'; return\n+   endselect\n+  endif\n   call netcdf_err(err,message); if (err/=0) return\n-  ! re-initialize message\n-  message=\"f-writeParam/\"\n- end do  ! looping through local column model parameters\n \n- ! loop through basin-average model parameters\n- do ipar=1,size(bpar_meta)\n-  ! check that the variable is desired\n-  if (.not.bpar_meta(ipar)%v_write) cycle\n-  ! initialize message\n-  message=trim(message)//trim(bpar_meta(ipar)%varname)//'/'\n-  ! get variable ID\n-  err = nf90_inq_varid(ncid,trim(bpar_meta(ipar)%varname),iVarId)\n-  call netcdf_err(err,message); if (err/=0) return\n-  ! write data\n-  err = nf90_put_var(ncid,iVarId,(/bpar_data(ipar)/),start=(/1/),count=(/1/))\n-  call netcdf_err(err,message); if (err/=0) return\n   ! re-initialize message\n-  message=\"f-writeParam/\"\n- end do  ! looping through basin-average model parameters\n-\n- ! close output file\n- err = nf90_close(ncid); call netcdf_err(err,message); if (err/=0) return\n- end subroutine writeParam\n-\n+  message=\"f-writeParm/\"\n+ end do  ! looping through local column model parameters\n \n- ! **********************************************************************************************************\n- ! public subroutine writeParam: write model forcing data\n- ! **********************************************************************************************************\n- subroutine writeForce(fileout,forc_data,iHRU,istep,err,message)\n- USE data_types,only:var_d                       ! data structure: x%var(:)    (dp)\n- USE globalData,only:forc_meta                   ! forcing metadata\n- USE var_lookup,only:iLookFORCE                  ! identifies element of the forcing structure\n+ end subroutine writeParm\n+\n+ ! **************************************************************************************\n+ ! public subroutine writeData: write model time-dependent data\n+ ! **************************************************************************************\n+ subroutine writeData(mstep,ostep,meta,stat,dat,indx,iHRU,err,message)\n+ USE data_types,only:var_info,dlength,ilength       ! type structures for passing\n+ USE var_lookup,only:maxVarStat                     ! index into stats structure\n+ USE var_lookup,only:iLookVarType                   ! index into type structure\n+ USE var_lookup,only:iLookIndex                     ! index into index structure\n+ USE var_lookup,only:iLookStat                      ! index into stat structure\n+ USE globalData,only:outFreq,nFreq,ncid             ! output file information\n+ USE get_ixName_module,only:get_varTypeName         ! to access type strings for error messages\n+ USE get_ixName_module,only:get_statName            ! to access type strings for error messages\n  implicit none\n+\n  ! declare dummy variables\n- character(*), intent(in)    :: fileout          ! output file\n- type(var_d),  intent(in)    :: forc_data        ! forcing data structure\n- integer(i4b), intent(in)    :: iHRU             ! hydrologic response unit\n- integer(i4b), intent(in)    :: istep            ! model time step\n- integer(i4b),intent(out)    :: err              ! error code\n- character(*),intent(out)    :: message          ! error message\n+ type(var_info),intent(in)     :: meta(:)           ! meta data\n+ class(*)      ,intent(in)     :: stat(:)           ! stats data\n+ class(*)      ,intent(in)     :: dat(:)            ! timestep data\n+ type(ilength) ,intent(in)     :: indx(:)           ! index data\n+ integer(i4b)  ,intent(in)     :: iHRU              ! hydrologic response unit\n+ integer(i4b)  ,intent(in)     :: mStep             ! model time step\n+ integer(i4b)  ,intent(in)     :: oStep(:)          ! output time step\n+ integer(i4b)  ,intent(out)    :: err               ! error code\n+ character(*)  ,intent(out)    :: message           ! error message\n  ! local variables\n- integer(i4b)                :: ncid             ! NetCDF file ID\n- integer(i4b)                :: iforce           ! loop through model forcing variables\n- integer(i4b)                :: iVarId           ! variable ID\n- ! initialize error control\n- err=0;message=\"f-writeForce/\"\n-\n- ! open NetCDF file\n- err = nf90_open(trim(fileout),nf90_write,ncid)\n- call netcdf_err(err,message); if (err/=0) return\n-\n- ! write the time coordinate variable\n- if(iHRU == 1)then\n-  message=trim(message)//'writeTime/'\n-  !print*, 'iHRU, istep, forc_data%var(iLookFORCE%time) = ', iHRU, istep, forc_data%var(iLookFORCE%time)\n-  err = nf90_inq_varid(ncid,'time',iVarId); call netcdf_err(err,message); if (err/=0) return\n-  err = nf90_put_var(ncid,iVarId,(/forc_data%var(iLookFORCE%time)/),start=(/istep/),count=(/1/))\n-  call netcdf_err(err,message); if (err/=0) return\n-  message=\"f-writeForce/\"\n- endif\n+ integer(i4b)                  :: iVar              ! variable index\n+ integer(i4b)                  :: iStat             ! statistics index\n+ integer(i4b)                  :: iFreq             ! frequency index\n+ integer(i4b)                  :: ncVarID           ! used only for time\n+ integer(i4b)                  :: nSnow             ! number of snow layers\n+ integer(i4b)                  :: nSoil             ! number of soil layers\n+ integer(i4b)                  :: nLayers           ! total number of layers\n+ integer(i4b)                  :: midSnowStartIndex ! start index of the midSnow vector for a given timestep\n+ integer(i4b)                  :: midSoilStartIndex ! start index of the midSoil vector for a given timestep\n+ integer(i4b)                  :: midTotoStartIndex ! start index of the midToto vector for a given timestep\n+ integer(i4b)                  :: ifcSnowStartIndex ! start index of the ifcSnow vector for a given timestep\n+ integer(i4b)                  :: ifcSoilStartIndex ! start index of the ifcSoil vector for a given timestep\n+ integer(i4b)                  :: ifcTotoStartIndex ! start index of the ifcToto vector for a given timestep\n \n- ! loop through model forcing variables\n- do iforce=1,size(forc_meta)\n-  ! ignore the time variable (used as a coordinate variable above)\n-  if(forc_meta(iforce)%varname == 'time') cycle\n-  ! check that the variable is desired\n-  if (.not.forc_meta(iforce)%v_write) cycle\n-  ! initialize message\n-  message=trim(message)//trim(forc_meta(iforce)%varname)//'/'\n-  ! get variable ID\n-  err = nf90_inq_varid(ncid,trim(forc_meta(iforce)%varname),iVarId)\n-  call netcdf_err(err,message); if (err/=0) return\n-  ! write data\n-  err = nf90_put_var(ncid,iVarId,(/forc_data%var(iforce)/),start=(/iHRU,istep/),count=(/1,1/))\n-  call netcdf_err(err,message); if (err/=0) return\n- end do  ! looping through forcing data variables\n+ ! initialize error control\n+ err=0;message=\"writeData/\"\n \n- ! close output file\n- message=\"f-writeForce/\"\n- err = nf90_close(ncid); call netcdf_err(err,message); if (err/=0) return\n- end subroutine writeForce\n+ ! model layers\n+ nSoil             = indx(iLookIndex%nSoil)%dat(1)\n+ nSnow             = indx(iLookIndex%nSnow)%dat(1)\n+ nLayers           = indx(iLookIndex%nLayers)%dat(1)\n+ ! model indices\n+ midSnowStartIndex = indx(iLookIndex%midSnowStartIndex)%dat(1)\n+ midSoilStartIndex = indx(iLookIndex%midSoilStartIndex)%dat(1)\n+ midTotoStartIndex = indx(iLookIndex%midTotoStartIndex)%dat(1)\n+ ifcSnowStartIndex = indx(iLookIndex%ifcSnowStartIndex)%dat(1)\n+ ifcSoilStartIndex = indx(iLookIndex%ifcSoilStartIndex)%dat(1)\n+ ifcTotoStartIndex = indx(iLookIndex%ifcTotoStartIndex)%dat(1)\n+\n+ do iFreq = 1,nFreq\n+  ! check that the timestep is desired\n+  if (mod(mStep,outFreq(iFreq)).ne.0) cycle\n+\n+   ! loop through model variables\n+   do iVar = 1,size(meta)\n+\n+    ! handle time first\n+    if (meta(iVar)%varName=='time') then    \n+     selecttype(stat)\n+      typeis (dlength)\n+       err = nf90_inq_varid(ncid(iFreq),trim(meta(iVar)%varName),ncVarID) \n+       call netcdf_err(err,message); if (err/=0) return\n+       err = nf90_put_var(ncid(iFreq),ncVarID,(/stat(iVar)%dat(iLookStat%inst)/),start=(/oStep(iFreq)/),count=(/1,1/))\n+       call netcdf_err(err,message); if (err/=0) return\n+       cycle\n+     class default; err=20; message=trim(message)//'time variable must be of type dlength'; return; \n+     endselect\n+    endif\n \n- ! **********************************************************************************************************\n- ! public subroutine writeModel: write local column model variables\n- ! **********************************************************************************************************\n- subroutine writeModel(fileout,indx_data,metaStruct,dataStruct,iHRU,istep,err,message)\n- USE var_lookup,only:iLookINDEX                  ! identifies element of the index structure\n- USE data_types,only:var_ilength                 ! data structure: x%var(:)%dat (i4b)\n- USE data_types,only:var_dlength                 ! data structure: x%var(:)%dat (dp)\n- USE data_types,only:var_info                    ! metadata structure\n+    ! check that the variable is desired\n+    if (meta(iVar)%outFreq.ne.iFreq) cycle\n+\n+    ! loop through output stats\n+    do iStat = 1,maxVarStat\n+     ! check that the variable is desired\n+     if ((.not.meta(iVar)%statFlag(iStat)).or.(trim(meta(iVar)%varName)=='unknown')) cycle\n+\n+     ! stats/dats output - select data type\n+     if (meta(iVar)%varType==iLookVarType%scalarv) then\n+       selecttype(stat)\n+        typeis (ilength)\n+         err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/stat(iVar)%dat(iStat)/),start=(/iHRU,oStep(iFreq)/),count=(/1,1/))\n+        typeis (dlength)\n+         err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/stat(iVar)%dat(iStat)/),start=(/iHRU,oStep(iFreq)/),count=(/1,1/))\n+        class default; err=20; message=trim(message)//'stats must be scalarv and either ilength of dlength'; return\n+       endselect  ! stat \n+     else\n+      selecttype (dat)\n+       typeis (dlength)\n+        selectcase (meta(iVar)%varType)\n+         case(iLookVarType%wLength); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,1,oStep(iFreq)/),count=(/1,maxSpectral,1/))\n+         case(iLookVarType%midToto); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,midTotoStartIndex/),count=(/1,nLayers/))\n+         case(iLookVarType%midSnow); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,midSnowStartIndex/),count=(/1,nSnow/))\n+         case(iLookVarType%midSoil); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,midSoilStartIndex/),count=(/1,nSoil/))\n+         case(iLookVarType%ifcToto); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,ifcTotoStartIndex/),count=(/1,nLayers/))\n+         case(iLookVarType%ifcSnow); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,ifcSnowStartIndex/),count=(/1,nSnow/))\n+         case(iLookVarType%ifcSoil); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,ifcSoilStartIndex/),count=(/1,nSoil/))\n+        endselect ! vartype\n+       typeis (ilength)\n+        selectcase (meta(iVar)%varType)\n+         case(iLookVarType%wLength); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,1,oStep(iFreq)/),count=(/1,maxSpectral,1/))\n+         case(iLookVarType%midToto); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,midTotoStartIndex/),count=(/1,nLayers/))\n+         case(iLookVarType%midSnow); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,midSnowStartIndex/),count=(/1,nSnow/))\n+         case(iLookVarType%midSoil); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,midSoilStartIndex/),count=(/1,nSoil/))\n+         case(iLookVarType%ifcToto); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,ifcTotoStartIndex/),count=(/1,nLayers/))\n+         case(iLookVarType%ifcSnow); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,ifcSnowStartIndex/),count=(/1,nSnow/))\n+         case(iLookVarType%ifcSoil); err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/iHRU,ifcSoilStartIndex/),count=(/1,nSoil/))\n+        endselect ! vartype\n+      endselect ! dat\n+     endif ! sacalarv\n+\n+     ! process error code\n+     if (err.ne.0) print*, meta(iVar)%ncVarID(iStat)\n+     if (err.ne.0) message=trim(message)//trim(meta(iVar)%varName)//'_'//trim(get_statName(iStat))\n+     call netcdf_err(err,message); if (err/=0) return\n+\n+    enddo ! iStat\n+   enddo ! iVar\n+  enddo ! iFreq\n+\n+ end subroutine writeData\n+\n+ ! **************************************************************************************\n+ ! public subroutine writeBasin: write basin-average variables\n+ ! **************************************************************************************\n+ subroutine writeBasin(mstep,ostep,meta,stat,dat,indx,err,message)\n+ USE data_types,only:var_info,dlength,ilength       ! type structures for passing\n+ USE var_lookup,only:maxVarStat                     ! index into stats structure\n+ USE var_lookup,only:iLookVarType                   ! index into type structure\n+ USE var_lookup,only:iLookIndex                     ! index into index structure\n+ USE var_lookup,only:iLookStat                      ! index into stat structure\n+ USE globalData,only:outFreq,nFreq,ncid             ! output file information\n+ USE get_ixName_module,only:get_varTypeName         ! to access type strings for error messages\n+ USE get_ixName_module,only:get_statName            ! to access type strings for error messages\n  implicit none\n- ! input variables\n- character(*), intent(in)      :: fileout        ! output file\n- type(var_ilength),intent(in)  :: indx_data      ! model indices\n- type(var_info),intent(in)     :: metaStruct(:)  ! metadata structure\n- class(*),intent(in)           :: dataStruct     ! data structure\n- integer(i4b), intent(in)      :: iHRU           ! hydrologic response unit\n- integer(i4b), intent(in)      :: istep          ! model time step\n- ! output variables\n- integer(i4b),intent(out)      :: err            ! error code\n- character(*),intent(out)      :: message        ! error message\n+\n+ ! declare dummy variables\n+ type(var_info),intent(in)     :: meta(:)           ! meta data\n+ type(dlength) ,intent(in)     :: stat(:)           ! stats data\n+ type(dlength) ,intent(in)     :: dat(:)            ! timestep data\n+ type(ilength) ,intent(in)     :: indx(:)           ! index data\n+ integer(i4b)  ,intent(in)     :: mStep             ! model time step\n+ integer(i4b)  ,intent(in)     :: oStep(:)          ! output time step\n+ integer(i4b)  ,intent(out)    :: err               ! error code\n+ character(*)  ,intent(out)    :: message           ! error message\n  ! local variables\n- integer(i4b)                  :: ncid           ! NetCDF file ID\n- integer(i4b)                  :: ivar           ! variable index\n- integer(i4b)                  :: iVarId         ! variable ID\n+ integer(i4b)                  :: iVar              ! variable index\n+ integer(i4b)                  :: iStat             ! statistics index\n+ integer(i4b)                  :: iFreq             ! frequency index\n  ! initialize error control\n- err=0;message=\"writeModel/\"\n+ err=0;message=\"f-writeBasin/\"\n \n- ! associate local variables with information in the data structures\n- associate(&\n- ! model layers\n- nSoil             => indx_data%var(iLookINDEX%nSoil)%dat(1)             ,&          \n- nSnow             => indx_data%var(iLookINDEX%nSnow)%dat(1)             ,&  \n- nLayers           => indx_data%var(iLookINDEX%nLayers)%dat(1)           ,&   \n- ! model indices\n- midSnowStartIndex => indx_data%var(iLookINDEX%midSnowStartIndex)%dat(1) ,&             \n- midSoilStartIndex => indx_data%var(iLookINDEX%midSoilStartIndex)%dat(1) ,&             \n- midTotoStartIndex => indx_data%var(iLookINDEX%midTotoStartIndex)%dat(1) ,&             \n- ifcSnowStartIndex => indx_data%var(iLookINDEX%ifcSnowStartIndex)%dat(1) ,&             \n- ifcSoilStartIndex => indx_data%var(iLookINDEX%ifcSoilStartIndex)%dat(1) ,&             \n- ifcTotoStartIndex => indx_data%var(iLookINDEX%ifcTotoStartIndex)%dat(1)  &\n- )  ! (associating local variables with information in the data structures)\n+ do iFreq = 1,nFreq\n+  ! check that the timestep is desired\n+  if (mod(mStep,outFreq(iFreq)).ne.0) cycle\n \n- ! open NetCDF file\n- err = nf90_open(trim(fileout),nf90_write,ncid)\n- call netcdf_err(err,message); if (err/=0) return\n+   ! loop through model variables\n+   do iVar = 1,size(meta)\n \n- ! loop through model variables\n- do ivar=1,size(metaStruct)\n+    ! check that the variable is desired\n+    if (meta(iVar)%outFreq.ne.iFreq) cycle\n \n-  ! check that the variable is desired\n-  if (.not.metaStruct(ivar)%v_write .or. trim(metaStruct(ivar)%varname)=='unknown') cycle\n+    ! loop through output stats\n+    do iStat = 1,maxVarStat\n+     ! check that the variable is desired\n+     if ((.not.meta(iVar)%statFlag(iStat)).or.(trim(meta(iVar)%varName)=='unknown')) cycle\n \n-  ! get variable ID\n-  err = nf90_inq_varid(ncid,trim(metaStruct(ivar)%varname),iVarId)\n-  call netcdf_err(err,message); if (err/=0) return\n+     ! stats/dats output - select data type\n+     selectcase (meta(iVar)%varType)\n \n-  ! select data type\n-  select type(dataStruct)\n-\n-   ! write integer data\n-   type is (var_ilength)\n-    err = nf90_put_var(ncid,iVarId,dataStruct%var(ivar)%dat,start=(/iHRU,istep/),count=(/1,1/))\n-\n-   ! write double precision data\n-   type is (var_dlength)\n-\n-    ! write model data \n-    select case(trim(metaStruct(ivar)%vartype))\n-     case('scalarv'); err = nf90_put_var(ncid,iVarId,dataStruct%var(ivar)%dat,start=(/iHRU,istep/),count=(/1,1/))\n-     case('wLength'); err = nf90_put_var(ncid,iVarId,dataStruct%var(ivar)%dat,start=(/iHRU,1,istep/),count=(/1,maxSpectral,1/))\n-     case('midSnow'); err = nf90_put_var(ncid,iVarId,dataStruct%var(ivar)%dat,start=(/iHRU,midSnowStartIndex/),count=(/1,nSnow/))\n-     case('midSoil'); err = nf90_put_var(ncid,iVarId,dataStruct%var(ivar)%dat,start=(/iHRU,midSoilStartIndex/),count=(/1,nSoil/))\n-     case('midToto'); err = nf90_put_var(ncid,iVarId,dataStruct%var(ivar)%dat,start=(/iHRU,midTotoStartIndex/),count=(/1,nLayers/))\n-     case('ifcSnow'); err = nf90_put_var(ncid,iVarId,dataStruct%var(ivar)%dat,start=(/iHRU,ifcSnowStartIndex/),count=(/1,nSnow+1/))\n-     case('ifcSoil'); err = nf90_put_var(ncid,iVarId,dataStruct%var(ivar)%dat,start=(/iHRU,ifcSoilStartIndex/),count=(/1,nSoil+1/))\n-     case('ifcToto'); err = nf90_put_var(ncid,iVarId,dataStruct%var(ivar)%dat,start=(/iHRU,ifcTotoStartIndex/),count=(/1,nLayers+1/))\n-     case default\n-      err=40; message=trim(message)//\"unknownVariableType[name='\"//trim(metaStruct(ivar)%varname)//\"'; &\n-                                     &type='\"//trim(metaStruct(ivar)%vartype)//\"']\"; return\n-    endselect ! selecting the variable type\n-\n-   ! check that we found the data type\n-   class default; err=20; message=trim(message)//'unable to identify the data structure'; return\n-\n-  end select  ! selecting the data structure\n-\n-  ! process error code\n-  call netcdf_err(err,message); if (err/=0) return\n+      case (iLookVarType%scalarv)\n+       err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/stat(iVar)%dat(iStat)/),start=(/oStep(iFreq)/),count=(/1/))\n \n- end do  ! looping through model variables\n+      case (iLookVarType%routing)\n+       if (mstep==1) then\n+        err = nf90_put_var(ncid(iFreq),meta(iVar)%ncVarID(iStat),(/dat(iVar)%dat/),start=(/1/),count=(/1000/))\n+       endif\n \n- ! close output file\n- err = nf90_close(ncid); call netcdf_err(err,message); if (err/=0) return\n+      case default\n+       err=40; message=trim(message)//\"unknownVariableType[name='\"//trim(meta(iVar)%varName)//\"';type='\"//trim(get_varTypeName(meta(iVar)%varType))//    \"']\"; return\n+      endselect ! variable type\n \n- ! end associating local variables with information in the data structures\n- end associate\n+     ! process error code\n+     if (err.ne.0) print*, meta(iVar)%ncVarID(iStat)","path":"build/source/netcdf/modelwrite.f90","position":503,"original_position":503,"commit_id":"a9b86f8d1cdc554512574ebc18087a57f36eb9c5","original_commit_id":"a9b86f8d1cdc554512574ebc18087a57f36eb9c5","user":{"login":"greyNearing","id":17014654,"avatar_url":"https://avatars.githubusercontent.com/u/17014654?v=3","gravatar_id":"","url":"https://api.github.com/users/greyNearing","html_url":"https://github.com/greyNearing","followers_url":"https://api.github.com/users/greyNearing/followers","following_url":"https://api.github.com/users/greyNearing/following{/other_user}","gists_url":"https://api.github.com/users/greyNearing/gists{/gist_id}","starred_url":"https://api.github.com/users/greyNearing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/greyNearing/subscriptions","organizations_url":"https://api.github.com/users/greyNearing/orgs","repos_url":"https://api.github.com/users/greyNearing/repos","events_url":"https://api.github.com/users/greyNearing/events{/privacy}","received_events_url":"https://api.github.com/users/greyNearing/received_events","type":"User","site_admin":false},"body":"Fixed (deleted line).","created_at":"2016-05-02T14:16:43Z","updated_at":"2016-05-02T14:16:43Z","html_url":"https://github.com/NCAR/summa/pull/139#discussion_r61743653","pull_request_url":"https://api.github.com/repos/NCAR/summa/pulls/139","_links":{"self":{"href":"https://api.github.com/repos/NCAR/summa/pulls/comments/61743653"},"html":{"href":"https://github.com/NCAR/summa/pull/139#discussion_r61743653"},"pull_request":{"href":"https://api.github.com/repos/NCAR/summa/pulls/139"}}},"pull_request":{"url":"https://api.github.com/repos/NCAR/summa/pulls/139","id":65897165,"html_url":"https://github.com/NCAR/summa/pull/139","diff_url":"https://github.com/NCAR/summa/pull/139.diff","patch_url":"https://github.com/NCAR/summa/pull/139.patch","issue_url":"https://api.github.com/repos/NCAR/summa/issues/139","number":139,"state":"open","locked":false,"title":"Feature/merge refactor","user":{"login":"greyNearing","id":17014654,"avatar_url":"https://avatars.githubusercontent.com/u/17014654?v=3","gravatar_id":"","url":"https://api.github.com/users/greyNearing","html_url":"https://github.com/greyNearing","followers_url":"https://api.github.com/users/greyNearing/followers","following_url":"https://api.github.com/users/greyNearing/following{/other_user}","gists_url":"https://api.github.com/users/greyNearing/gists{/gist_id}","starred_url":"https://api.github.com/users/greyNearing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/greyNearing/subscriptions","organizations_url":"https://api.github.com/users/greyNearing/orgs","repos_url":"https://api.github.com/users/greyNearing/repos","events_url":"https://api.github.com/users/greyNearing/events{/privacy}","received_events_url":"https://api.github.com/users/greyNearing/received_events","type":"User","site_admin":false},"body":"This PR follows martynpclark's # 137. It has been merged, resolved, and tested with that branch.\r\n\r\nChanges allow for multiple output streams at different frequencies.\r\nEach variable may be output at exactly one frequency (specified in terms of the number of model time steps), but the user may request any number of several output statistics over that time period.\r\n\r\nFor example, if we want soil moisture output daily from a model run at an hourly time step, this has an output frequency of 24, and we may request the daily:\r\n1. average\r\n2. max/min\r\n3. variance (maximum likelihood estimator, not unbiased estimator).\r\n4. total (for things like fluxes that can be summed)\r\n5. or the instantaneous value at the particular output time step.\r\n\r\nThere may be a maximum of 10 different output streams, however this maximum number can be changed in line 30 of source/dshare/var_lookup.f90\r\n\r\nThis all requires a new input file that allows the user to request different variables in different output streams. This new file is pointed to in the file manager file - it replaces the old META_LOCALMVAR file, which was not used any longer. An example model output control file is located in the build/ directory in this PR.\r\n\r\nThis branch has been tested on all of the testcases that are currently running with netcdf input (all except figures 5 and 9 from the wrr testcase directory).  It has been checked with output timings at 1 (every model timestep) and 24 (once per day) on all outputs from the testcases - this includes outputs of every time. It provides answers within machine precision.","created_at":"2016-04-10T00:43:45Z","updated_at":"2016-05-02T14:16:43Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0a54c7be9d03f239a848e7b082e6fc6ced3091ba","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/NCAR/summa/pulls/139/commits","review_comments_url":"https://api.github.com/repos/NCAR/summa/pulls/139/comments","review_comment_url":"https://api.github.com/repos/NCAR/summa/pulls/comments{/number}","comments_url":"https://api.github.com/repos/NCAR/summa/issues/139/comments","statuses_url":"https://api.github.com/repos/NCAR/summa/statuses/a9b86f8d1cdc554512574ebc18087a57f36eb9c5","head":{"label":"greyNearing:feature/mergeRefactor","ref":"feature/mergeRefactor","sha":"a9b86f8d1cdc554512574ebc18087a57f36eb9c5","user":{"login":"greyNearing","id":17014654,"avatar_url":"https://avatars.githubusercontent.com/u/17014654?v=3","gravatar_id":"","url":"https://api.github.com/users/greyNearing","html_url":"https://github.com/greyNearing","followers_url":"https://api.github.com/users/greyNearing/followers","following_url":"https://api.github.com/users/greyNearing/following{/other_user}","gists_url":"https://api.github.com/users/greyNearing/gists{/gist_id}","starred_url":"https://api.github.com/users/greyNearing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/greyNearing/subscriptions","organizations_url":"https://api.github.com/users/greyNearing/orgs","repos_url":"https://api.github.com/users/greyNearing/repos","events_url":"https://api.github.com/users/greyNearing/events{/privacy}","received_events_url":"https://api.github.com/users/greyNearing/received_events","type":"User","site_admin":false},"repo":{"id":50875620,"name":"summa","full_name":"greyNearing/summa","owner":{"login":"greyNearing","id":17014654,"avatar_url":"https://avatars.githubusercontent.com/u/17014654?v=3","gravatar_id":"","url":"https://api.github.com/users/greyNearing","html_url":"https://github.com/greyNearing","followers_url":"https://api.github.com/users/greyNearing/followers","following_url":"https://api.github.com/users/greyNearing/following{/other_user}","gists_url":"https://api.github.com/users/greyNearing/gists{/gist_id}","starred_url":"https://api.github.com/users/greyNearing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/greyNearing/subscriptions","organizations_url":"https://api.github.com/users/greyNearing/orgs","repos_url":"https://api.github.com/users/greyNearing/repos","events_url":"https://api.github.com/users/greyNearing/events{/privacy}","received_events_url":"https://api.github.com/users/greyNearing/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/greyNearing/summa","description":"Structure for Unifying Multiple Modeling Alternatives:","fork":true,"url":"https://api.github.com/repos/greyNearing/summa","forks_url":"https://api.github.com/repos/greyNearing/summa/forks","keys_url":"https://api.github.com/repos/greyNearing/summa/keys{/key_id}","collaborators_url":"https://api.github.com/repos/greyNearing/summa/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/greyNearing/summa/teams","hooks_url":"https://api.github.com/repos/greyNearing/summa/hooks","issue_events_url":"https://api.github.com/repos/greyNearing/summa/issues/events{/number}","events_url":"https://api.github.com/repos/greyNearing/summa/events","assignees_url":"https://api.github.com/repos/greyNearing/summa/assignees{/user}","branches_url":"https://api.github.com/repos/greyNearing/summa/branches{/branch}","tags_url":"https://api.github.com/repos/greyNearing/summa/tags","blobs_url":"https://api.github.com/repos/greyNearing/summa/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/greyNearing/summa/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/greyNearing/summa/git/refs{/sha}","trees_url":"https://api.github.com/repos/greyNearing/summa/git/trees{/sha}","statuses_url":"https://api.github.com/repos/greyNearing/summa/statuses/{sha}","languages_url":"https://api.github.com/repos/greyNearing/summa/languages","stargazers_url":"https://api.github.com/repos/greyNearing/summa/stargazers","contributors_url":"https://api.github.com/repos/greyNearing/summa/contributors","subscribers_url":"https://api.github.com/repos/greyNearing/summa/subscribers","subscription_url":"https://api.github.com/repos/greyNearing/summa/subscription","commits_url":"https://api.github.com/repos/greyNearing/summa/commits{/sha}","git_commits_url":"https://api.github.com/repos/greyNearing/summa/git/commits{/sha}","comments_url":"https://api.github.com/repos/greyNearing/summa/comments{/number}","issue_comment_url":"https://api.github.com/repos/greyNearing/summa/issues/comments{/number}","contents_url":"https://api.github.com/repos/greyNearing/summa/contents/{+path}","compare_url":"https://api.github.com/repos/greyNearing/summa/compare/{base}...{head}","merges_url":"https://api.github.com/repos/greyNearing/summa/merges","archive_url":"https://api.github.com/repos/greyNearing/summa/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/greyNearing/summa/downloads","issues_url":"https://api.github.com/repos/greyNearing/summa/issues{/number}","pulls_url":"https://api.github.com/repos/greyNearing/summa/pulls{/number}","milestones_url":"https://api.github.com/repos/greyNearing/summa/milestones{/number}","notifications_url":"https://api.github.com/repos/greyNearing/summa/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/greyNearing/summa/labels{/name}","releases_url":"https://api.github.com/repos/greyNearing/summa/releases{/id}","deployments_url":"https://api.github.com/repos/greyNearing/summa/deployments","created_at":"2016-02-01T22:18:51Z","updated_at":"2016-02-01T22:18:52Z","pushed_at":"2016-04-21T16:03:43Z","git_url":"git://github.com/greyNearing/summa.git","ssh_url":"git@github.com:greyNearing/summa.git","clone_url":"https://github.com/greyNearing/summa.git","svn_url":"https://github.com/greyNearing/summa","homepage":"http://www.ral.ucar.edu/projects/summa","size":23928,"stargazers_count":0,"watchers_count":0,"language":"FORTRAN","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"NCAR:develop","ref":"develop","sha":"ce46b0acd7b64b173344c2c88c62ce8a9a89e4ce","user":{"login":"NCAR","id":2007542,"avatar_url":"https://avatars.githubusercontent.com/u/2007542?v=3","gravatar_id":"","url":"https://api.github.com/users/NCAR","html_url":"https://github.com/NCAR","followers_url":"https://api.github.com/users/NCAR/followers","following_url":"https://api.github.com/users/NCAR/following{/other_user}","gists_url":"https://api.github.com/users/NCAR/gists{/gist_id}","starred_url":"https://api.github.com/users/NCAR/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NCAR/subscriptions","organizations_url":"https://api.github.com/users/NCAR/orgs","repos_url":"https://api.github.com/users/NCAR/repos","events_url":"https://api.github.com/users/NCAR/events{/privacy}","received_events_url":"https://api.github.com/users/NCAR/received_events","type":"Organization","site_admin":false},"repo":{"id":21404710,"name":"summa","full_name":"NCAR/summa","owner":{"login":"NCAR","id":2007542,"avatar_url":"https://avatars.githubusercontent.com/u/2007542?v=3","gravatar_id":"","url":"https://api.github.com/users/NCAR","html_url":"https://github.com/NCAR","followers_url":"https://api.github.com/users/NCAR/followers","following_url":"https://api.github.com/users/NCAR/following{/other_user}","gists_url":"https://api.github.com/users/NCAR/gists{/gist_id}","starred_url":"https://api.github.com/users/NCAR/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NCAR/subscriptions","organizations_url":"https://api.github.com/users/NCAR/orgs","repos_url":"https://api.github.com/users/NCAR/repos","events_url":"https://api.github.com/users/NCAR/events{/privacy}","received_events_url":"https://api.github.com/users/NCAR/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/NCAR/summa","description":"Structure for Unifying Multiple Modeling Alternatives:","fork":false,"url":"https://api.github.com/repos/NCAR/summa","forks_url":"https://api.github.com/repos/NCAR/summa/forks","keys_url":"https://api.github.com/repos/NCAR/summa/keys{/key_id}","collaborators_url":"https://api.github.com/repos/NCAR/summa/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/NCAR/summa/teams","hooks_url":"https://api.github.com/repos/NCAR/summa/hooks","issue_events_url":"https://api.github.com/repos/NCAR/summa/issues/events{/number}","events_url":"https://api.github.com/repos/NCAR/summa/events","assignees_url":"https://api.github.com/repos/NCAR/summa/assignees{/user}","branches_url":"https://api.github.com/repos/NCAR/summa/branches{/branch}","tags_url":"https://api.github.com/repos/NCAR/summa/tags","blobs_url":"https://api.github.com/repos/NCAR/summa/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/NCAR/summa/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/NCAR/summa/git/refs{/sha}","trees_url":"https://api.github.com/repos/NCAR/summa/git/trees{/sha}","statuses_url":"https://api.github.com/repos/NCAR/summa/statuses/{sha}","languages_url":"https://api.github.com/repos/NCAR/summa/languages","stargazers_url":"https://api.github.com/repos/NCAR/summa/stargazers","contributors_url":"https://api.github.com/repos/NCAR/summa/contributors","subscribers_url":"https://api.github.com/repos/NCAR/summa/subscribers","subscription_url":"https://api.github.com/repos/NCAR/summa/subscription","commits_url":"https://api.github.com/repos/NCAR/summa/commits{/sha}","git_commits_url":"https://api.github.com/repos/NCAR/summa/git/commits{/sha}","comments_url":"https://api.github.com/repos/NCAR/summa/comments{/number}","issue_comment_url":"https://api.github.com/repos/NCAR/summa/issues/comments{/number}","contents_url":"https://api.github.com/repos/NCAR/summa/contents/{+path}","compare_url":"https://api.github.com/repos/NCAR/summa/compare/{base}...{head}","merges_url":"https://api.github.com/repos/NCAR/summa/merges","archive_url":"https://api.github.com/repos/NCAR/summa/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/NCAR/summa/downloads","issues_url":"https://api.github.com/repos/NCAR/summa/issues{/number}","pulls_url":"https://api.github.com/repos/NCAR/summa/pulls{/number}","milestones_url":"https://api.github.com/repos/NCAR/summa/milestones{/number}","notifications_url":"https://api.github.com/repos/NCAR/summa/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/NCAR/summa/labels{/name}","releases_url":"https://api.github.com/repos/NCAR/summa/releases{/id}","deployments_url":"https://api.github.com/repos/NCAR/summa/deployments","created_at":"2014-07-01T22:58:27Z","updated_at":"2016-02-21T22:05:15Z","pushed_at":"2016-05-01T05:54:09Z","git_url":"git://github.com/NCAR/summa.git","ssh_url":"git@github.com:NCAR/summa.git","clone_url":"https://github.com/NCAR/summa.git","svn_url":"https://github.com/NCAR/summa","homepage":"http://www.ral.ucar.edu/projects/summa","size":24247,"stargazers_count":10,"watchers_count":10,"language":"FORTRAN","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":17,"mirror_url":null,"open_issues_count":46,"forks":17,"open_issues":46,"watchers":10,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/NCAR/summa/pulls/139"},"html":{"href":"https://github.com/NCAR/summa/pull/139"},"issue":{"href":"https://api.github.com/repos/NCAR/summa/issues/139"},"comments":{"href":"https://api.github.com/repos/NCAR/summa/issues/139/comments"},"review_comments":{"href":"https://api.github.com/repos/NCAR/summa/pulls/139/comments"},"review_comment":{"href":"https://api.github.com/repos/NCAR/summa/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/NCAR/summa/pulls/139/commits"},"statuses":{"href":"https://api.github.com/repos/NCAR/summa/statuses/a9b86f8d1cdc554512574ebc18087a57f36eb9c5"}}}}