/[MITgcm]/MITgcm_contrib/gael/profilesMatlabProcessing/profiles_IO_v2/MITprof_write.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/profilesMatlabProcessing/profiles_IO_v2/MITprof_write.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (hide annotations) (download)
Fri Nov 5 21:41:00 2010 UTC (14 years, 8 months ago) by gforget
Branch: MAIN
Changes since 1.2: +26 -7 lines
- make this work both with the legacy mexnc stuff (as before)
  and with the modern matlab native netcdf implementation.

1 gforget 1.1 %function: MITprof_write
2 gforget 1.3 %object: write netcdf data files in the "MIT format"
3 gforget 1.1 %author: Gael Forget (gforget@mit.edu)
4 gforget 1.3 %date: Nov 5th, 2010
5 gforget 1.1 %
6 gforget 1.2 %usage: []=MITprof_write(fileOut,MITprof);
7     % ---> writes MITprof to the fileOut file
8 gforget 1.1 % that has previously been created using MITprof_create
9     %
10 gforget 1.2 %inputs: fileOut data file name
11     % list_vars variable list (optional)
12 gforget 1.1 %
13 gforget 1.2 %outputs: MITprof structure containing the various arrays
14 gforget 1.1
15 gforget 1.2 function []=MITprof_write(fileOut,MITprof);
16    
17 gforget 1.3 global useNativeMatlabNetcdf; if isempty(useNativeMatlabNetcdf); useNativeMatlabNetcdf = ~isempty(which('netcdf.open')); end;
18    
19 gforget 1.2 %get directory name:
20     tmp1=strfind(fileOut,'/');
21     if ~isempty(tmp1); dirOut=fileOut(1:tmp1(end)); else; dirOut='./'; end;
22     %check that file exists:
23     tmp1=dir(fileOut);
24     if isempty(tmp1); tmp1=dir([fileOut '.nc']); end;
25     if isempty(tmp1); error([fileOut ' file not found']); end;
26 gforget 1.3
27 gforget 1.2 %open file:
28 gforget 1.3 if (useNativeMatlabNetcdf);
29     nc=netcdf.open([dirOut tmp1.name],'write');
30     else;%try to use old mex stuff
31     nc=netcdf([dirOut tmp1.name], 'write');
32     end;
33 gforget 1.1
34 gforget 1.3 %write to file:
35 gforget 1.1 list_vars=fieldnames(MITprof);
36     for ii=1:length(list_vars);
37 gforget 1.3 eval(['tmp1=MITprof.' list_vars{ii} ';']);
38     ncputvar(nc,list_vars{ii},tmp1);
39 gforget 1.1 end;
40    
41 gforget 1.3 %close file:
42     if (useNativeMatlabNetcdf);
43     netcdf.close(nc);
44     else;%try to use old mex stuff
45     close(nc);
46     end;
47    
48    
49     % an alternative way of doing this, without opening the file:
50     % for ii=1:length(list_vars);
51     % eval([list_vars{ii} '=MITprof.' list_vars{ii} ';']);
52     % eval(['ncsave(''' [dirOut tmp1.name] ''',' list_vars{ii} ');']);
53     % end;
54 gforget 1.1

  ViewVC Help
Powered by ViewVC 1.1.22