mpt.viterbi:

Usage Arguments Examples

Usage

1
mpt.viterbi(allpost2, L, fmat, sst, D = c(100, 500), D2s = 0.09)

Arguments

allpost2
L
fmat
sst
D
D2s

Examples

  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
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (allpost2, L, fmat, sst, D = c(100, 500), D2s = 0.09) 
{
    land = make.landmask(sst)$mask
    print(sprintf("Number of days: %i\n", dim(L[[3]][3])))
    s = D * D2s
    rrow = dim(L[[3]])[1]
    ccol = dim(L[[3]])[2]
    icalc = dim(L[[3]])[3]
    numnames = 1
    unc = sqrt(2 * s[1])
    ks = ceiling(unc * 10 + 1)
    ks = ks + mod(ks, 2) + 1
    ks1 = max(15, ks)
    kern1 = gausskern(ks1, unc)
    unc = sqrt(2 * s[2])
    ks = ceiling(unc * 10 + 1)
    ks = ks + mod(ks, 2) + 1
    ks2 = max(15, ks)
    kern2 = gausskern(ks2, unc)
    mpt.maplong = matrix(L$lon, ccol, rrow, byrow = T)
    mpt.maplat = matrix(L$lat, ccol, rrow, byrow = F)
    dlong = diff(L$lon)[1]
    dlat = diff(L$lat)[1]
    R = mapmatrix(L$lat[1], L$lon[1], dlat, dlong)
    smatrix = allpost2
    smatrix[is.nan(smatrix)] = 0
    M = smatrix[, , 1]
    M = log(M)
    subject = (M != -Inf) * 1
    zro = matrix(0, rrow, ccol)
    theend = icalc
    Tprevx = numeric(rrow * ccol * theend)
    dim(Tprevx) = c(rrow, ccol, theend)
    Tprevy = Tprevx
    xidx = which.min((fmat[1, 8] - (L$lon))^2)
    yidx = which.min((fmat[1, 9] - L$lat)^2)
    Tprevx[xidx, yidx, 1] = xidx
    Tprevy[xidx, yidx, 1] = yidx
    Ltotal = smatrix * 0 + 1
    for (j in 1:numnames) {
        Ltotal = Ltotal * smatrix
    }
    Ltotal[is.nan(Ltotal)] = 0
    Ltotal[, , 1] = smatrix[, , 1]
    print("Starting iterations...")
    print(sprintf("Day   1 -  9..."))
    Tx = numeric(rrow * ccol * theend)
    dim(Tx) = c(rrow, ccol, theend)
    Ty = Tx
    for (j in 2:(theend - 1)) {
        if (!mod(j, 10)) {
            print(sprintf("\n done! time = %4.3f", Sys.time()))
            print(sprintf("Day %3.0i -%3.0i...", j, j + 9))
            time1
        }
        Mtemp = log(zro)
        Ttempx = -1 + zro
        Ttempy = Ttempx
        time1 = Sys.time()
        if (fmat$behav[j - 1] == 1) {
            ks = ks1
            kern = kern1
        }
        else {
            ks = ks2
            kern = kern2
        }
        for (xx in 1:ccol) {
            for (yy in 1:rrow) {
                if (as.logical(subject[yy, xx])) {
                  kminlat = 1 + max(ceiling(ks/2) - yy, 0)
                  kmaxlat = min(ks - (yy + floor(ks/2) - rrow), 
                    ks)
                  kminlong = 1 + max(ceiling(ks/2) - xx, 0)
                  kmaxlong = min(ks - (xx + floor(ks/2) - ccol), 
                    ks)
                  klat = kminlat:kmaxlat
                  klong = kminlong:kmaxlong
                  mminlat = max(yy - floor(ks/2), 1)
                  mmaxlat = min(yy + floor(ks/2), rrow)
                  mminlong = max(xx - floor(ks/2), 1)
                  mmaxlong = min(xx + floor(ks/2), ccol)
                  mlat = mminlat:mmaxlat
                  mlong = mminlong:mmaxlong
                  B = log(Ltotal[mlat, mlong, j - 1] * kern[klat, 
                    klong])
                  Msub = B + M[yy, xx]
                  Mupdate = Mtemp[mlat, mlong]
                  Txupdate = Ttempx[mlat, mlong]
                  Tyupdate = Ttempy[mlat, mlong]
                  update = (Mupdate < Msub)
                  update[is.na(update)] = F
                  Mupdate[update] = Msub[update]
                  Txupdate[update] = xx
                  Tyupdate[update] = yy
                  Mtemp[mlat, mlong] = Mupdate
                  Ttempx[mlat, mlong] = Txupdate
                  Ttempy[mlat, mlong] = Tyupdate
                }
            }
        }
        Mtemp[land == 1] = -Inf
        subject = (Mtemp != -Inf) * 1
        for (xx in 1:ccol) {
            for (yy in 1:rrow) {
                if (as.logical(subject[yy, xx])) {
                  Tx[yy, xx, 1:(j - 1)] = Tprevx[Ttempy[yy, xx], 
                    Ttempx[yy, xx], 1:(j - 1)]
                  Ty[yy, xx, 1:(j - 1)] = Tprevy[Ttempy[yy, xx], 
                    Ttempx[yy, xx], 1:(j - 1)]
                  Tx[yy, xx, j] = xx
                  Ty[yy, xx, j] = yy
                }
            }
        }
        print(Sys.time() - time1)
        M = Mtemp
        Tprevx = Tx
        Tprevy = Ty
    }
    Sys.time() - time1
    M[land == 1] = Inf * -1
    val = max(M)
    ind = which.max(M)
    txy = ind2sub(c(rrow, ccol), ind)
    xm = txy[2]
    ym = txy[1]
    mpt.long = Tx[ym, xm, ]
    mpt.long_clean = mpt.long
    mpt.lat = Ty[ym, xm, ]
    mpt.lat_clean = mpt.lat
    mpt.lat = mpt.lat_clean = jitter(mpt.lat)
    mpt.long = mpt.long_clean = jitter(mpt.long)
    mpt = pixtomap(R, mpt.long_clean, mpt.lat_clean)
    mpt[, 1] = mpt[, 1]
    mpt[1, ] = as.numeric(fmat[1, 8:9])
    mpt[nrow(mpt), ] = as.numeric(fmat[nrow(fmat), 8:9])
    mpt
  }

galuardi/hmmwoa documentation built on May 16, 2019, 5:37 p.m.