def save_tracks_to_file(nc_outfile,iyear,MPA_SOURCE):

    nc_ofid = Dataset(nc_outfile, 'w')
    
# write run info to track file
    
    nc_ofid.mpa_source = MPA_SOURCE
    nc_ofid.iyear = iyear
    nc_ofid.NUM_LARVAE = NUM_LARVAE
    nc_ofid.RELEASE_WINDOW = RELEASE_WINDOW
    nc_ofid.STARTDAY = STARTDAY
    nc_ofid.DT = DT
    nc_ofid.KMX = KMX
    nc_ofid.KMY = KMY
    nc_ofid.KMZ = KMZ
    
    nc_ofid.VERTICAL_INTERP = str(VERTICAL_INTERP)
    nc_ofid.ANIMATE = str(ANIMATE)
    nc_ofid.DEATH = str(DEATH)

    nc_ofid.BEHAVIOUR = BEHAVIOUR    # behaviour index
#    # 1 is standard Larrson et al behaviour
#    # 2 is modified Larsson et al with small swimming speeds
    nc_ofid.TARGETDEPTH = TARGETDEPTH    # target depth
    nc_ofid.SWIMSLOW = SWIMSLOW          #initial swimming speed
    nc_ofid.SWIMFAST = SWIMFAST     # max swimming speed
    nc_ofid.SWIMSTART = SWIMSTART       #age in days at which start swimming
    nc_ofid.SWIMMAX = SWIMMAX          #average age in days at which max swimming speed is reached
    nc_ofid.DESCENDAGE = DESCENDAGE       # average age at which probability of heading down starts
                            # to increase
    nc_ofid.DESCENDAGERANGE = DESCENDAGERANGE    # now fully heading down
    nc_ofid.MINSETTLEAGE = MINSETTLEAGE     # minimum age at which can settle given suitable 
                            # habitat
    nc_ofid.DEADAGE = DEADAGE          # Average age at which dead
    
    
    nl = len(larvae_dead) + len(larvae_outofarea) + len(larvae_group)
    
    if DEATH:
        maxt = int((DEADAGE + 1) * SECONDS_IN_DAY / DT)
    else:
        maxt = int((NRUNDAYS + 1) * SECONDS_IN_DAY / DT)
        
    time = nc_ofid.createDimension('time', maxt)
    nlarvae = nc_ofid.createDimension('nlarvae', nl)
    lon = nc_ofid.createVariable('longitude','f8',('nlarvae','time',))
    lat = nc_ofid.createVariable('latitude','f8',('nlarvae','time',))
    dep = nc_ofid.createVariable('depth','f8',('nlarvae','time',))
    bed = nc_ofid.createVariable('at bed','i',('nlarvae','time',))
    temp = nc_ofid.createVariable('temperature','f8',('nlarvae','time',))
    sal = nc_ofid.createVariable('salinity','f8',('nlarvae','time',))
    rt = nc_ofid.createVariable('release day','i',('nlarvae',))
    fate = nc_ofid.createVariable('fate','S1',('nlarvae',))    
    
    i = 0
           
    for larva in larvae_dead:
        x, y = larva.get_track()
        z, b = larva.get_depth_history()
        t = larva.get_temperature_history()
        s = larva.get_salinity_history()
        release_time = larva.get_release_day()
        state = 'D'
           
        lon[i,0:len(x)] = x[0:len(x)]
        lat[i,0:len(y)] = y[0:len(y)]
        dep[i,0:len(z)] = z[0:len(z)]
        bed[i,0:len(z)] = b[0:len(z)]
        temp[i,0:len(t)] = t[0:len(t)]
        sal[i,0:len(t)] = s[0:len(t)]
        
        rt[i] = release_time
        fate[i] = state
        
        i = i + 1
    
    for larva in larvae_outofarea:
        x, y = larva.get_track()
        z, b = larva.get_depth_history()
        t = larva.get_temperature_history()
        release_time = larva.get_release_day()
        state = 'L'
           
        lon[i,0:len(x)] = x[0:len(x)]
        lat[i,0:len(y)] = y[0:len(y)]
        dep[i,0:len(z)] = z[0:len(z)]
        bed[i,0:len(z)] = b[0:len(z)]
        temp[i,0:len(t)] = t[0:len(t)]
        sal[i,0:len(t)] = s[0:len(t)]
        rt[i] = release_time
        fate[i] = state
        
        i = i + 1
        
    for larva in larvae_group:
        x, y = larva.get_track()
        z, b = larva.get_depth_history()
        t = larva.get_temperature_history()
        release_time = larva.get_release_day()
        state = 'A'
           
        lon[i,0:len(x)] = x[0:len(x)]
        lat[i,0:len(y)] = y[0:len(y)]
        dep[i,0:len(z)] = z[0:len(z)]
        bed[i,0:len(z)] = b[0:len(z)]
        temp[i,0:len(t)] = t[0:len(t)]
        sal[i,0:len(t)] = s[0:len(t)]
        rt[i] = release_time
        fate[i] = state
        
        i = i + 1
        
    nc_ofid.close()