PageRenderTime 77ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/other_authors/netcdf/snctools/nc_addvar.m

http://m--pack.googlecode.com/
Objective C | 219 lines | 186 code | 33 blank | 0 comment | 25 complexity | 6f1c69957397bd3e4f599f5494382f85 MD5 | raw file
  1. function return_status = nc_addvar ( ncfile, varstruct )
  2. % NC_ADDVAR: adds a variable to a NetCDF file
  3. %
  4. % USAGE: status = nc_addvar ( ncfile, varstruct );
  5. %
  6. % PARAMETERS:
  7. % Input
  8. % ncfile:
  9. % varstruct:
  10. % This is a structure with four fields:
  11. %
  12. % Name
  13. % Nctype
  14. % Dimension
  15. % Attribute
  16. %
  17. % "Name" is just that, the name of the variable to be defined.
  18. %
  19. % "Nctype" should be
  20. % 'double', 'float', 'int', 'short', or 'byte', or 'char'
  21. % 'NC_DOUBLE', 'NC_FLOAT', 'NC_INT', 'NC_SHORT', 'NC_BYTE', 'NC_CHAR'
  22. %
  23. % "Dimension" is a cell array of dimension names.
  24. %
  25. % "Attribute" is also a structure array. Each element has two
  26. % fields, "Name", and "Value".
  27. %
  28. % Output:
  29. % status:
  30. % Optional. If not requested, an exception is thrown if an
  31. % error condition arises. Otherwise, -1 is returned if the
  32. % routine fails, or 0 is the routine succeeds
  33. %
  34. % AUTHOR:
  35. % johnevans@acm.org
  36. %
  37. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  38. %
  39. % $Name: snctools-2_0_21 $
  40. % $Id: nc_addvar.m,v 1.17 2006/04/25 18:47:14 johnevans007 Exp $
  41. % AUTHOR: johnevans@acm.org
  42. %
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44. if nargout < 1
  45. throw_exception = 1;
  46. else
  47. throw_exception = 0;
  48. end
  49. return_status = -1;
  50. if nargin < 2
  51. msg = sprintf ( '%s: must have two input arguments.\n', mfilename );
  52. handle_error ( msg, throw_exception );
  53. return
  54. end
  55. if ( ~strcmp(class(ncfile),'char') )
  56. msg = sprintf ( '%s: 1st input argument must be char.\n', mfilename );
  57. handle_error ( msg, throw_exception );
  58. return
  59. end
  60. if ( ~isstruct(varstruct) )
  61. msg = sprintf ( '%s: 2nd input argument must be a structure.\n', mfilename );
  62. handle_error ( msg, throw_exception );
  63. return
  64. end
  65. %
  66. % Check that required fields are there.
  67. % Must at least have a name.
  68. if ~isfield ( varstruct, 'Name' )
  69. msg = sprintf ( '%s: variable structure must at least have ''Name'' field.\n', mfilename );
  70. handle_error ( msg, throw_exception );
  71. return
  72. end
  73. %
  74. % Check that required fields are there.
  75. % Default Nctype is double.
  76. if ~isfield ( varstruct, 'Nctype' )
  77. varstruct.Nctype = 'double';
  78. end
  79. % If the datatype is not a string.
  80. % Change suggested by Brian Powell
  81. if ( isa(varstruct.Nctype, 'double') && varstruct.Nctype < 7 )
  82. types={ 'byte' 'char' 'short' 'int' 'float' 'double'};
  83. varstruct.Nctype = char(types(varstruct.Nctype));
  84. end
  85. %
  86. % Check that the datatype is known.
  87. switch ( varstruct.Nctype )
  88. case { 'NC_DOUBLE', 'double', ...
  89. 'NC_FLOAT', 'float', ...
  90. 'NC_INT', 'int', ...
  91. 'NC_SHORT', 'short', ...
  92. 'NC_BYTE', 'byte', ...
  93. 'NC_CHAR', 'char' }
  94. %
  95. % Do nothing
  96. otherwise
  97. msg = sprintf ( '%s: unknown Nctype ''%s''\n', mfilename, varstruct.Nctype );
  98. handle_error ( msg, throw_exception );
  99. return
  100. end
  101. %
  102. % Check that required fields are there.
  103. % Default Dimension is none. Singleton scalar.
  104. if ~isfield ( varstruct, 'Dimension' )
  105. varstruct.Dimension = [];
  106. end
  107. %
  108. % Check that required fields are there.
  109. % Default Attributes are none
  110. if ~isfield ( varstruct, 'Attribute' )
  111. varstruct.Attribute = [];
  112. end
  113. [ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
  114. if ( status < 0 )
  115. ncerr = mexnc ( 'strerror', status );
  116. msg = sprintf ( '%s: mexnc:open failed on ''%s'', error message ''%s''.\n', mfilename, ncfile, ncerr );
  117. handle_error ( msg, throw_exception );
  118. return
  119. end
  120. %
  121. % determine the dimids of the named dimensions
  122. num_dims = length(varstruct.Dimension);
  123. dimids = [];
  124. for j = 1:num_dims
  125. [dimids(j), status] = mexnc ( 'dimid', ncid, varstruct.Dimension{j} );
  126. if ( status < 0 )
  127. ncerr = mexnc ( 'strerror', status );
  128. mexnc ( 'close', ncid );
  129. strformat = '%s: mexnc:dimid failed to return a dimid for %s, file %s, error ''%s''.\n';
  130. msg = sprintf ( strformat, mfilename, varstruct.Dimension{j}, ncfile, ncerr );
  131. handle_error ( msg, throw_exception );
  132. return
  133. end
  134. end
  135. %
  136. % go into define mode
  137. status = mexnc ( 'redef', ncid );
  138. if ( status < 0 )
  139. ncerr = mexnc ( 'strerror', status );
  140. mexnc ( 'close', ncid );
  141. msg = sprintf ( '%s: mexnc:redef failed on %s, ''%s''.\n', mfilename, ncfile, ncerr );
  142. handle_error ( msg, throw_exception );
  143. return
  144. end
  145. status = mexnc ( 'def_var', ncid, varstruct.Name, varstruct.Nctype, num_dims, dimids );
  146. if ( status < 0 )
  147. ncerr = mexnc ( 'strerror', status );
  148. mexnc ( 'endef', ncid );
  149. mexnc ( 'close', ncid );
  150. msg = sprintf ( '%s: mexnc:def_var failed on %s, ''%s''.\n', mfilename, ncfile, ncerr );
  151. handle_error ( msg, throw_exception );
  152. return
  153. end
  154. status = mexnc ( 'endef', ncid );
  155. if ( status < 0 )
  156. mexnc ( 'close', ncid );
  157. msg = sprintf ('%s: mexnc:endef failed on %s.\n', mfilename, ncfile );
  158. handle_error ( msg, throw_exception );
  159. return
  160. end
  161. status = mexnc ( 'close', ncid );
  162. if ( status < 0 )
  163. msg = sprintf ( '%s: mexnc:close failed on %s.\n', mfilename, ncfile );
  164. handle_error ( msg, throw_exception );
  165. return;
  166. end
  167. %
  168. % Now just use nc_attput to put in the attributes
  169. for j = 1:length(varstruct.Attribute)
  170. attname = varstruct.Attribute(j).Name;
  171. attval = varstruct.Attribute(j).Value;
  172. status = nc_attput ( ncfile, varstruct.Name, attname, attval );
  173. if ( status == -1 )
  174. msg = sprintf ( '%s: nc_attput failed to write %s to variable %s, file %s.\n', mfilename, attname, varstruct.Name, ncfile );
  175. handle_error ( msg, throw_exception );
  176. return
  177. end
  178. end
  179. return_status = 0;
  180. function handle_error ( msg, throw_exception )
  181. if throw_exception
  182. error ( msg );
  183. else
  184. fprintf ( 2, msg );
  185. end
  186. return