/[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.6 - (hide annotations) (download)
Thu Apr 14 20:12:07 2011 UTC (14 years, 3 months ago) by roquet
Branch: MAIN
Changes since 1.5: +79 -18 lines
- Simplification of IO interface to MITcdf netcdf format.
  MITprof_create and MITprof_read are now low-level functions that should not be used by user.
  Instead, use MITprof_write to create/update a file, and MITprof_load to load data.
- Depth levels must now be provided as an argument of MITprof_create and MITprof_struct,
  and cannot be changed once the MITprof ncfile or struct have been created.
- profiles_prep_main and profiles_prep_write_nc have been changed accordingly.

1 roquet 1.6 function []=MITprof_write(fileOut,MITprof,varargin);
2 gforget 1.1 %function: MITprof_write
3 roquet 1.6 %object: write data in a MITprof netcdf file
4 gforget 1.1 %author: Gael Forget (gforget@mit.edu)
5 gforget 1.3 %date: Nov 5th, 2010
6 gforget 1.1 %
7 roquet 1.6 %usage: []=MITprof_write(fileOut,MITprof, [list_vars] );
8     %
9     %inputs: fileOut data file name (absolute/relative path)
10     % .nc suffix is added if not already present
11     % MITprof struct containing profile data
12     % list_vars variable list (optional)
13     %
14     % Format description of MITprof struct input variable:
15     % list of fields always available:
16     % prof_depth [nDepth x 1]
17     % prof_descr {nProf x 1} --> will be converted into a char array
18     % prof_date [nProf x 1]
19     % prof_YYYYMMDD [nProf x 1]
20     % prof_HHMMSS [nProf x 1]
21     % prof_lon [nProf x 1]
22     % prof_lat [nProf x 1]
23     % prof_basin [nProf x 1]
24     % prof_point [nProf x 1]
25     %
26     % other fields generally available of size [nProf x nLev] :
27     % prof_T, prof_Tflag, prof_Terr, prof_Tweight, prof_Testim
28     % prof_S, prof_Sflag, prof_Serr, prof_Sweight, prof_Sestim
29     %
30     % if list_vars is not specified, every variables with a name starting
31     % with 'prof_', except prof_depth, will be saved in the MITprof
32     % netcdf file. An error will occur if the program try to save a
33     % variable that is not initialized in the MITprof netcdf file.
34     %
35     % if list_vars is specified, only variables present in the list will be
36     % saved.
37     %
38     % NaN are auomatically replaced by missing_value attribute (or _FillValue).
39     % the file fileOut will be created using MITprof_create if needed.
40 gforget 1.1 %
41    
42 gforget 1.2
43 gforget 1.3
44 roquet 1.6 % check that file exists and add prefix and suffix if necessary
45 roquet 1.5 [pathstr, name, ext] = fileparts(fileOut);
46     if isempty(pathstr) | strcmp(pathstr,'.'), pathstr=pwd; end
47     if isempty(ext) | ~strcmp(ext,'.nc'), ext='.nc'; end
48     fileOut=[pathstr '/' name ext];
49 gforget 1.3
50 roquet 1.6 % build the list of variables that must be loaded
51     if nargin>2,
52     list_vars=varargin{1};
53     else
54     list1=fieldnames(MITprof); list_vars={};
55     I=strfind(list1,'prof_');
56     for kk=1:length(I),
57     if I{kk}==1 & ~strcmp(list1{kk},'prof_depth'),
58     list_vars{end+1}=list1{kk};
59     end
60     end
61     end
62    
63     % create the file if needed
64     if ~exist(fileOut,'file'),
65     prof_depth=MITprof.prof_depth;
66     nProf=length(MITprof.prof_lon);
67     MITprof_create(fileOut,nProf,prof_depth,list_vars);
68     end
69    
70     % prof_descr: convert back from cell 2 array format
71     if isfield(MITprof,'prof_descr')
72     ncload(fileOut,'prof_descr'); nb_char=size(prof_descr,2);
73     prof_descr=char(MITprof.prof_descr);
74     if size(prof_descr,2)<nb_char,
75     prof_descr(:,size(prof_descr,2)+1:nb_char)=' ';
76     elseif size(prof_descr,2)>nb_char
77     disp('some profile descriptive strings have been shortened');
78     prof_descr=prof_descr(:,1:nb_char);
79     end
80     MITprof.prof_descr=prof_descr;
81     end
82    
83     % write to file:
84 roquet 1.4 nc=ncopen(fileOut,'write');
85 gforget 1.1 for ii=1:length(list_vars);
86 roquet 1.6 varname=list_vars{ii};
87     data=getfield(MITprof,varname);
88     spval=ncgetFillVal(nc,varname);
89     if isnumeric(data)
90     data(isnan(data))=spval;
91     MITprof=setfield(MITprof,varname,data);
92     end;
93     ncputvar(nc,varname,data);
94 gforget 1.1 end;
95 roquet 1.4 ncclose(nc);
96 roquet 1.6
97 gforget 1.1

  ViewVC Help
Powered by ViewVC 1.1.22