############################################################################### # Time-stamp synchronizing program timerge # John Nimmo # started 11/9/17 # # This R program generates interpolations of data sets for use with the EMR/MRC # family of programs. # # The masterfile has data to be retained and supplemented. Column 1 has date # and time in Excel format (based on the number of days since the # midnight starting January 1, 1900). Column 2 codes the time of # measurement as a single number in the user's chosen time units on a # scale with 0 at since a user-designated starting time, for example # the time in days since the start of measurements. Column 3 has the # measured quantity, e.g. precipitation or streamflow. # # The interpfile has data to be paired with the measured data in the # masterfile. These data may have gaps or time mismatches with respect # to the data in the masterfile. Its three columns are in the same # format as the three columns of the masterfile. # # The outputfile has the data of the masterfile combined with interpolations # of the interpfile data at the times corresponding to those of the # masterfile. The first and second columns are the time since the # designated start and the measured data taken from the master file. The # third column is interpolated data from the interpfile corresponding to # the times in column 1. These three columns have the form required for # use as input files in the EMR/MRC programs. The fourth column, which is # not used by EMR/MRC programs, is the timestamp from the first column # of the masterfile. # ############################################################################### #### User inputs ############################################################## # Folders for input data and output: inputlocation = "P:/Research_Tools/EMR-MRC/sampledata/" outputlocation = inputlocation # Can use different folder if desired # File names for input data and for output: masterfile = "SHczo.Q.2009.noprecip.csv" interpfile = "SHczo.precip53.2009.raw.csv" outputfile = "SHczo.Q&precip.2009.csv" #### End user inputs ########################################################## masterpath = paste0(inputlocation, masterfile) interppath = paste0(inputlocation, interpfile) outputpath = paste0(outputlocation, outputfile) # Read data datamaster = read.csv(masterpath, header=TRUE) datainterp = read.csv(interppath, header=TRUE) timemstr = datamaster[,2] ymstr = datamaster[,3] timeintp = datainterp[,2] yintp = datainterp[,3] xyinterp = approx(timeintp, yintp, xout=timemstr, method="linear") datamerged = data.frame(timemstr, ymstr, xyinterp[2], datamaster[,1]) colnames(datamerged)[1:2] = colnames(datamaster)[2:3] colnames(datamerged)[3] = colnames(datainterp)[3] colnames(datamerged)[4] = colnames(datamaster)[1] write.csv(datamerged, file=outputpath, row.names=FALSE) ###############################################################################