function define_4dvar_obs_file ( output_ncfile, num_surveys )
% DEFINE_4DVAR_OBS_FILE:  define the obs netCDF file from the xbt data
%
% USAGE:  define_4dvar_obs_file ( output_ncfile, num_surveys );
%
% PARAMETERS:
% Output:
%     output_ncfile:  
%         This netCDF file will be created.
%     num_surveys:
%         This number defines the length of the 'survey' dimension.


%
% DEfine the netCDF file.
[ncid, status] = mexnc ( 'create', output_ncfile, nc_write_mode );
if status, error ( mexnc('strerror',status) ), end

%
% Define the surveys as the sum of the two 
[survey_dimid, status] = mexnc ( 'DEF_DIM', ncid, 'survey', num_surveys );
if status, error ( mexnc('strerror',status) ), end



% The 'datum' is unlimited.  
[datum_dimid, status] = mexnc ( 'DEF_DIM', ncid, 'datum', 0 );
if status, error ( mexnc('strerror',status) ), end


%
% Define the weight dimension
[weight_dimid, status] = mexnc ( 'DEF_DIM', ncid, 'weight', 8 );
if status, error ( mexnc('strerror',status) ), end



%
% Define the record dimension
[record_dimid, status] = mexnc ( 'DEF_DIM', ncid, 'record', 2 );
if status, error ( mexnc('strerror',status) ), end



%
% Define the state variable dimension
[state_variable_dimid, status] = mexnc ( 'DEF_DIM', ncid, 'state_variable', 7 );
if status, error ( mexnc('strerror',status) ), end



%
% Define the variables.
%
% Stupid spherical switch.
[varid, status] = mexnc ( 'DEF_VAR', ncid, 'spherical', nc_char, 0, [] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'grid type logical switch';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
if status, error ( mexnc('strerror',status) ), end

attname = 'option_T';
attvalue = 'spherical';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
if status, error ( mexnc('strerror',status) ), end

attname = 'option_F';
attvalue = 'Cartesian';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
if status, error ( mexnc('strerror',status) ), end


%
% OBS_VARIANCE
[varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_variance', nc_double, 1, [state_variable_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'global time and space observation variance';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
if status, error ( mexnc('strerror',status) ), end

attname = 'units';
attvalue = 'squared state variable units';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
if status, error ( mexnc('strerror',status) ), end



% Nobs seems to be the number of observations per survey 
% (or xbt transect).
[Nobs_varid, status] = mexnc ( 'DEF_VAR', ncid, 'Nobs', nc_int, 1, [survey_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'number of observations with the same survey time';
status = mexnc ( 'PUT_ATT_TEXT', ncid, Nobs_varid, 'long_name', nc_char, length(attvalue), attvalue );


%
% Define the survey time
[survey_time_varid, status] = mexnc ( 'DEF_VAR', ncid, 'survey_time', nc_double, 1, [survey_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'survey time';
status = mexnc ( 'PUT_ATT_TEXT', ncid, survey_time_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'days since 2001-01-01 00:00:00';
status = mexnc ( 'PUT_ATT_TEXT', ncid, survey_time_varid, attname, nc_char, length(attvalue), attvalue );


%
% Define the obs_type
[obs_type_varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_type', nc_int, 1, [datum_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'model state variable associated with observation';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_type_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'nondimensional';
status = mexnc ( 'PUT_ATT_TEXT', ncid, survey_time_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'option_01';
attvalue = 'free-surface';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_type_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'option_02';
attvalue = 'vertically integrated u-momentum component';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_type_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'option_03';
attvalue = 'vertically integrated v-momentum component';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_type_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'option_04';
attvalue = 'u-momentum component';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_type_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'option_05';
attvalue = 'v-momentum component';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_type_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'option_06';
attvalue = 'potential temperature';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_type_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'option_07';
attvalue = 'salinity';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_type_varid, attname, nc_char, length(attvalue), attvalue );


%
% Define the "obs_time"
[obs_time_varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_time', nc_double, 1, [datum_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'time of observation';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_time_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'days since 2001-01-01 00:00:00';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_time_varid, attname, nc_char, length(attvalue), attvalue );





%
% Define the "obs_lon"
[obs_lon_varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_lon', nc_double, 1, [datum_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'longitude of observation';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_lon_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'degrees_east';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_lon_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'valid_min';
attvalue = -180;
status = mexnc ( 'PUT_ATT_DOUBLE', ncid, obs_lon_varid, attname, nc_float, length(attvalue), attvalue );

attname = 'valid_max';
attvalue = 180;
status = mexnc ( 'PUT_ATT_DOUBLE', ncid, obs_lon_varid, attname, nc_float, length(attvalue), attvalue );




%
% Define the "obs_lat"
[obs_lat_varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_lat', nc_double, 1, [datum_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'latitude of observation';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_lat_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'degrees_north';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_lat_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'valid_min';
attvalue = -90;
status = mexnc ( 'PUT_ATT_DOUBLE', ncid, obs_lat_varid, attname, nc_float, length(attvalue), attvalue );

attname = 'valid_max';
attvalue = 90;
status = mexnc ( 'PUT_ATT_DOUBLE', ncid, obs_lat_varid, attname, nc_float, length(attvalue), attvalue );




%
% Define the "obs_depth"
[obs_depth_varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_depth', nc_double, 1, [datum_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'depth of observation';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_depth_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'meter';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_depth_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'negative';
attvalue = 'downwards';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_depth_varid, attname, nc_char, length(attvalue), attvalue );


%
% observation error.
[varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_error', nc_double, 1, [datum_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'observation error, assigned weight';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'magnitude in terms of state variable type';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );


%
% observation values.
[obs_value_varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_value', nc_double, 1, [datum_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'observation value';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_value_varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'magnitude in terms of state variable type';
status = mexnc ( 'PUT_ATT_TEXT', ncid, obs_value_varid, attname, nc_char, length(attvalue), attvalue );


%%
%% NLmodel_value
%[varid, status] = mexnc ( 'DEF_VAR', ncid, 'NLmodel_value', nc_double, 2, [datum_dimid record_dimid] );
%if status, error ( mexnc('strerror',status) ), end
%
%attname = 'long_name';
%attvalue = 'nonlinear interpolated model value';
%status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
%
%attname = 'units';
%attvalue = 'magnitude in terms of state variable type';
%status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
%
%
%%
%% TLmodel_value
%[varid, status] = mexnc ( 'DEF_VAR', ncid, 'TLmodel_value', nc_double, 2, [datum_dimid record_dimid] );
%if status, error ( mexnc('strerror',status) ), end
%
%attname = 'long_name';
%attvalue = 'tangent linear interpolated model value';
%status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
%
%attname = 'units';
%attvalue = 'magnitude in terms of state variable type';
%status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
%
%%
%% Hmat
%[varid, status] = mexnc ( 'DEF_VAR', ncid, 'Hmat', nc_double, 2, [datum_dimid weight_dimid] );
%if status, error ( mexnc('strerror',status) ), end
%
%attname = 'long_name';
%attvalue = 'interpolation weights';
%status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
%
%attname = 'units';
%attvalue = 'nondimensional';
%status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );




%
%
%
% obs_Xgrid
[varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_Xgrid', nc_double, 1, [datum_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'x-grid observation location';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'nondimensional';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = 'left';
attvalue = 'INT(obs_Xgrid(datum))';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = 'right';
attvalue = 'INT(obs_Xgrid(datum))+1';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
%
%attname = '_FillValue';
%attvalue = -99999;
%status = mexnc ( 'PUT_ATT_DOUBLE', ncid, varid, attname, nc_double, length(attvalue), attvalue );

%
% obs_Ygrid
[varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_Ygrid', nc_double, 1, [datum_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'y-grid observation location';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'nondimensional';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = 'top';
attvalue = 'INT(obs_Ygrid(datum))+1';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = 'bottom';
attvalue = 'INT(obs_Ygrid(datum))';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );
%
%attname = '_FillValue';
%attvalue = -99999;
%status = mexnc ( 'PUT_ATT_DOUBLE', ncid, varid, attname, nc_double, length(attvalue), attvalue );




%
% obs_Zgrid
[varid, status] = mexnc ( 'DEF_VAR', ncid, 'obs_Zgrid', nc_double, 1, [datum_dimid] );
if status, error ( mexnc('strerror',status) ), end

attname = 'long_name';
attvalue = 'z-grid observation location';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = 'units';
attvalue = 'nondimensional';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = 'up';
attvalue = 'INT(obs_Zgrid(datum))+1';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = 'down';
attvalue = 'INT(obs_Zgrid(datum))';
status = mexnc ( 'PUT_ATT_TEXT', ncid, varid, attname, nc_char, length(attvalue), attvalue );

attname = '_FillValue';
attvalue = -1e308;
status = mexnc ( 'PUT_ATT_DOUBLE', ncid, varid, attname, nc_double, length(attvalue), attvalue );



%
% Global attributes

attname = 'type';
attvalue = 'Roms observation file';
status = mexnc ( 'PUT_ATT_TEXT', ncid, nc_global, attname, nc_float, length(attvalue), attvalue );
if status, error ( mexnc('strerror',status) ), end

 
attname = 'state_units';
 
attvalue = 'Free-sruface (m), 2D momentum (m/s), 3D momentum (m/s), potential temperature (Celsius), salinity (PSU)';
status = mexnc ( 'PUT_ATT_TEXT', ncid, nc_global, attname, nc_char, length(attvalue), attvalue );
if status, error ( mexnc('strerror',status) ), end


[status] = mexnc ( 'close', ncid );
if status, error ( mexnc('strerror',status) ), end


return
