function [status]=c_observation(fname,is3d,survey,Nstate,title);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2006 Rutgers University.                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                      %
% function [status]=c_observation(fname,is3d,survey,Nstate)            %
%                                                                      %
% This function creates 4DVAR observation NetCDF file.                 %
%                                                                      %
% On Input:                                                            %
%                                                                      %
%    fname       4DVAR observation NetCDF file name (string).          %
%    is3d        Processing 3D observations (integer).                 %
%    survey      Size of survey dimension (integer).                   %
%    Nstate      Number of state variables (integer).                  % 
%    title       Application title (string).                           %
%                                                                      %
% On Output:                                                           %
%                                                                      %
%    status      NetCDF error flag.                                    %
%                                                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%-----------------------------------------------------------------------
%  Get some NetCDF parameters.
%-----------------------------------------------------------------------

[ncglobal]=mexcdf('parameter','nc_global');
[ncchar]=mexcdf('parameter','nc_char');
[ncdouble]=mexcdf('parameter','nc_double');
[ncfloat]=mexcdf('parameter','nc_float');
[ncint]=mexcdf('parameter','nc_int');
[ncunlim]=mexcdf('parameter','nc_unlimited');

% Set dimension names.

Dnam.one='one';
Dnam.survey='survey';
Dnam.state='state_variable';
Dnam.datum='datum';

% Set variable names.

Vname.spherical='spherical';
Vname.Nobs     ='Nobs';
Vname.survey   ='survey_time';
Vname.variance ='obs_variance';
Vname.type     ='obs_type';
Vname.time     ='obs_time';
Vname.depth    ='obs_depth';
Vname.Xgrid    ='obs_Xgrid';
Vname.Ygrid    ='obs_Ygrid';
Vname.Zgrid    ='obs_Zgrid';
Vname.error    ='obs_error';
Vname.value    ='obs_value';

% Set floating-point variables type.

vartyp=ncfloat;

%-----------------------------------------------------------------------
%  Create NetCDF file.
%-----------------------------------------------------------------------

[ncid,status]=mexcdf('nccreate',fname,'nc_write');
if (ncid == -1),
  error(['C_OBSERVATION: ncopen - unable to create file: ', fname]);
  return
end,

%-----------------------------------------------------------------------
%  Define dimensions.
%-----------------------------------------------------------------------

[Did.one]=mexcdf('ncdimdef',ncid,Dnam.one,1);
if (Did.one == -1),
  error(['C_OBSERVATION: ncdimdef - unable to define dimension: ', ...
         Dnam.one]);
end,
[Did.survey]=mexcdf('ncdimdef',ncid,Dnam.survey,survey);
if (Did.survey == -1),
  error(['C_OBSERVATION: ncdimdef - unable to define dimension: ', ...
         Dnam.survey]);
end,

[Did.state]=mexcdf('ncdimdef',ncid,Dnam.state,Nstate);
if (Did.state == -1),
  error(['C_OBSERVATION: ncdimdef - unable to define dimension: ', ...
         Dnam.state]);
end,

[Did.datum]=mexcdf('ncdimdef',ncid,Dnam.datum,ncunlim);
if (Did.datum == -1),
  error(['C_OBSERVATION: ncdimdef - unable to define dimension: ', ...
         Dnam.datum]);
end,

%-----------------------------------------------------------------------
%  Create global attribute(s).
%-----------------------------------------------------------------------

type='ROMS Observation file';
lstr=max(size(type));
[status]=mexcdf('ncattput',ncid,ncglobal,'type',ncchar,lstr,type);
if (status == -1),
  error(['C_OBSERVATION: ncattput - unable to global attribure: ', ...
         'type.']);
  return
end,

lstr=max(size(title));
if (lstr > 0),
  [status]=mexcdf('ncattput',ncid,ncglobal,'title',ncchar,lstr,title);
  if (status == -1),
    error(['C_OBSERVATION: ncattput - unable to global attribure: ', ...
           'title.']);
    return
  end,
end,

units=(['Free-sruface (m), 2D momentum (m/s), 3D momentum (m/s), ', ...
        'potential temperature (Celsius), salinity (PSU)']);
lstr=max(size(units));
[status]=mexcdf('ncattput',ncid,ncglobal,'state_units',ncchar,lstr,units);
if (status == -1),
  error(['C_OBSERVATION: ncattput - unable to global attribure: ', ...
         'state_units.']);
  return
end,

history=['4DVAR observations, ', date_stamp];
lstr=max(size(history));

[status]=mexcdf('ncattput',ncid,ncglobal,'history',ncchar,lstr,history);
if (status == -1),
  error(['C_FORCING: ncattput - unable to global attribure: history.']);
  return
end,

%-----------------------------------------------------------------------
%  Define variables.
%-----------------------------------------------------------------------

%  Spherical switch.

Var.name  =Vname.spherical;
Var.type  =ncchar;
Var.dimid =Did.one;
Var.long  ='grid type logical switch';
Var.opt_T ='spherical';
Var.opt_F ='Cartesian';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Number of observations for each survey.

Var.name  =Vname.Nobs;
Var.type  =ncint;
Var.dimid =Did.survey;
Var.long  ='number of observations with the same survey time';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Survey time.

Var.name  =Vname.survey;
Var.type  =ncdouble;
Var.dimid =Did.survey;
Var.long  ='survey time';
Var.units ='day';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Global variance for each state variable.

Var.name  =Vname.variance;
Var.type  =ncdouble;
Var.dimid =Did.state;
Var.long  ='global time and space observation variance';
Var.units ='squared state variable units';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Observation type.

Var.name  =Vname.type;
Var.type  =ncint;
Var.dimid =Did.datum;
Var.long  ='model state variable associated with observation';
Var.opt_1 ='free-surface';
Var.opt_2 ='vertically integrated u-momentum component';
Var.opt_3 ='vertically integrated v-momentum component';
Var.opt_4 ='u-momentum component';
Var.opt_5 ='v-momentum component';
Var.opt_6 ='potential temperature';
Var.opt_7 ='salinity';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Observation time.

Var.name  =Vname.time;
Var.type  =ncdouble;
Var.dimid =Did.datum;
Var.long  ='time of observation';
Var.units ='day';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Observation depth.

if (is3d),
  Var.name  =Vname.depth;
  Var.type  =ncdouble;
  Var.dimid =Did.datum;
  Var.long  ='depth of observation';
  Var.units ='meter';
  Var.minus ='downwards';
  [varid,status]=nc_vdef(ncid,Var);
  clear Var
end,

%  Observation X-grid locations.

Var.name  =Vname.Xgrid;
Var.type  =ncdouble;
Var.dimid =Did.datum;
Var.long  ='x-grid observation location';
Var.units ='nondimensional';
Var.left  ='INT(obs_Xgrid(datum))';
Var.right ='INT(obs_Xgrid(datum))+1';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Observation Y-grid locations.

Var.name  =Vname.Ygrid;
Var.type  =ncdouble;
Var.dimid =Did.datum;
Var.long  ='y-grid observation location';
Var.units ='nondimensional';
Var.top   ='INT(obs_Ygrid(datum))+1';
Var.bottom='INT(obs_Ygrid(datum))';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Observation z-grid locations.

if (is3d),
  Var.name  =Vname.Zgrid;
  Var.type  =ncdouble;
  Var.dimid =Did.datum;
  Var.long  ='z-grid observation location';
  Var.units ='nondimensional';
  Var.up    ='INT(obs_Zgrid(datum))+1';
  Var.down  ='INT(obs_Zgrid(datum))';
  [varid,status]=nc_vdef(ncid,Var);
  clear Var
end,

%  Observation error.

Var.name  =Vname.error;
Var.type  =ncdouble;
Var.dimid =Did.datum;
Var.long  ='observation error covariance';
Var.units ='squared state variable units';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%  Observation value.

Var.name  =Vname.value;
Var.type  =ncdouble;
Var.dimid =Did.datum;
Var.long  ='observation value';
Var.units ='state variable units';
[varid,status]=nc_vdef(ncid,Var);
clear Var

%-----------------------------------------------------------------------
%  Leave definition mode.
%-----------------------------------------------------------------------

[status]=mexcdf('ncendef',ncid);
if (status == -1),
  error(['C_OBSERVATION: ncendef - unable to leave definition mode.'])
end,

%-----------------------------------------------------------------------
%  Close NetCDF file.
%-----------------------------------------------------------------------

[status]=mexcdf('ncclose',ncid);
if (status == -1),
  error(['C_OBSERVATION: ncclose - unable to close NetCDF file: ', fname])
end,

return
