You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: obs_write.m
    path of file: ./repos/tracacode/1911-COAWST/script/mfiles/rutgers/4dvar
    
    the code of the file until where you have to start completion: function [status]=obs_write(ncfile,S);

%
% OBS_WRITE:  Writes 4D-Var observation data into existing NetCDF file
%
% [status]=obs_write(ncfile,S)
%
% This function writes all observation data in structure array S into
% a existing ROMS 4D-Var observation NetCDF file.
%
% On Input:
%
%    ncfile  4D-Var Observations NetCDF file name (string)
%
%    S       Observations data (structure array):
%
%              S.spherical      spherical grid switch
%              S.Nobs           number of observations per survey
%              S.survey_time    time for each survey time
%              S.variance       global variance per state variable
%              S.type           state variable associated with observation
%              S.time           time for each observation
%              S.depth          depth of observation
%              S.Xgrid          observation fractional x-grid location
%              S.Ygrid          observation fractional y-grid location
%              S.Zgrid          observation fractional z-grid location
%              S.error          observation error
%              S.value          observation value
%
%            The following optional variables will be written if available:
%
%              S.provenance     observation origin
%              S.lon            observation longitude
%              S.lat            observation latitude
% 
% On Output:
%
%    status  Error flag
%

% svn $Id: obs_write.m 938 2019-01-28 06:35:10Z arango $
%===========================================================================%
%  Copyright (c) 2002-2019 The ROMS/TOMS Group                              %
%    Licensed under a MIT/X style license                                   %
%    See License_ROMS.txt                           Hernan G. Arango        %
%===========================================================================%

    
function [status]=obs_write(ncfile,S);

%
% OBS_WRITE:  Writes 4D-Var observation data into existing NetCDF file
%
% [status]=obs_write(ncfile,S)
%
% This function writes all observation data in structure array S into
% a existing ROMS 4D-Var observation NetCDF file.
%
% On Input:
%
%    ncfile  4D-Var Observations NetCDF file name (string)
%
%    S       Observations data (structure array):
%
%              S.spherical      spherical grid switch
%              S.Nobs           number of observations per survey
%              S.survey_time    time for each survey time
%              S.variance       global variance per state variable
%              S.type           state variable associated with observation
%              S.time           time for each observation
%              S.depth          depth of observation
%              S.Xgrid          observation fractional x-grid location
%              S.Ygrid          observation fractional y-grid location
%              S.Zgrid          observation fractional z-grid location
%              S.error          observation error
%              S.value          observation value
%
%            The following optional variables will be written if available:
%
%              S.provenance     observation origin
%              S.lon            observation longitude
%              S.lat            observation latitude
% 
% On Output:
%
%    status  Error flag
%

% svn $Id: obs_write.m 938 2019-01-28 06:35:10Z arango $
%===========================================================================%
%  Copyright (c) 2002-2019 The ROMS/TOMS Group                              %
%    Licensed under a MIT/X style license                                   %
%    See License_ROMS.txt                           Hernan G. Arango        %
%===========================================================================%

status=[];

%----------------------------------------------------------------------------
%  Check dimensions of input NetCDF file.
%----------------------------------------------------------------------------

D=nc_dinfo(ncfile);

for n=1:length(D),
  name=char(D(n).Name);
  switch name
    case 'survey'
      Nsurvey=D(n).Length;
    case 'state_variable'
      Nstate=D(n).Length;
    case 'datum'
      Ndatum=D(n).Length;
      unlimited=D(n).Unlimited;
  end