From 723f788b75308bd9832799f15a01c493abc540b3 Mon Sep 17 00:00:00 2001
From: Jacques Grelet <jacques.grelet@ird.fr>
Date: Tue, 15 Jan 2008 22:19:32 +0000
Subject: [PATCH] =?UTF-8?q?les=20variables=20etaient=20cr=C3=A9es=20par=20?=
 =?UTF-8?q?defaut=20de=20type=20double=20Utilise=20le=20champs=20nctype=20?=
 =?UTF-8?q?de=20tsg=5Fncvvar.csv=20La=20dimension=20des=20variables=20est?=
 =?UTF-8?q?=20faite=20sous=20forme=20de=20cell=20(voir=20exemple=20ncexamp?=
 =?UTF-8?q?le.m?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 tsg_io/readTsgDataNetCDF.m  |  2 +-
 tsg_io/writeTSGDataNetCDF.m | 48 ++++++++++++++++++++++++-------------
 2 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/tsg_io/readTsgDataNetCDF.m b/tsg_io/readTsgDataNetCDF.m
index 4df8f6c..4aaf3e6 100644
--- a/tsg_io/readTsgDataNetCDF.m
+++ b/tsg_io/readTsgDataNetCDF.m
@@ -109,7 +109,7 @@ close(nc);
 
 % Display time to read file on console
 % ------------------------------------
-t = toc; fprintf('...done (%6.2f sec).\n\n',t);
+t = toc; fprintf('...done (%5.2f sec).\n\n',t);
 
 % Everything OK
 % -------------
diff --git a/tsg_io/writeTSGDataNetCDF.m b/tsg_io/writeTSGDataNetCDF.m
index 65607ff..4d048e4 100644
--- a/tsg_io/writeTSGDataNetCDF.m
+++ b/tsg_io/writeTSGDataNetCDF.m
@@ -54,7 +54,7 @@ nca_keys        = keys(nca);
 
 % Get GF3 variables list codes from class tsg_nc with file 'tsg_ncvar.csv'
 % -----------------------------------------------------------------------
-ncv             = tsg_nc('tsg_test.csv');
+ncv             = tsg_nc('tsg_ncvar.csv');
 ncv_keys        = keys(ncv);
 ncv_fieldNames  = get_fieldnames(ncv);
 
@@ -95,20 +95,36 @@ end
 % Create NetCDF variables and attributes
 % --------------------------------------
 for i=1:numel(ncv_keys)
-  for j=1:numel(ncv_fieldNames)
-    variable  = ncv_keys{i};
+  variable  = ncv_keys{i};
+  dimension = get(ncv, variable, ncv_fieldNames{1});
+  nctype    = get(ncv, variable, ncv_fieldNames{2});
+  
+  % define dimension, value must be a cell
+  % --------------------------------------
+  switch nctype
+    case 'double'
+      nc{variable} = ncdouble(dimension);
+    case 'float'
+      nc{variable} = ncfloat(dimension);
+    case 'int'
+      nc{variable} = ncint(dimension);
+    case 'short'
+      nc{variable} = ncshort(dimension);  
+    case 'byte'
+      nc{variable} = ncbyte(dimension);
+    case 'char'
+      nc{variable} = ncchar(dimension);  
+    otherwise
+      nc{variable} = ncfloat(dimension); 
+  end
+  
+  % jump dimension and nctype field
+  % -------------------------------
+  for j=3:numel(ncv_fieldNames)
     fieldName = ncv_fieldNames{j};
     value = get(ncv, variable, fieldName);
     
-    % define dimension
-    % ----------------
-    if strcmp(fieldName,'dimension')
-      s = ['nc{''' variable '''} = {''' value '''}'];
-      eval(s);
-      
-    % define variable attributes
-    % --------------------------
-    elseif ~isempty(value)
+    if ~isempty(value)
       if ischar(value)
         nc{variable}.(fieldName) = ncchar(value);
       else  
@@ -154,9 +170,9 @@ for i=1:numel(ncv_keys)
   % DATE as two dimension
   % don't work under windows
   % nc{variable}(:) = tsg.(variable);
-  if strmatch('DATE', variable)
-    nc{variable}(:,:) = tsg.(variable);  
-  else
+  %if strmatch('DATE', variable)
+  %  nc{variable}(:,:) = tsg.(variable);  
+  %else
     % other one dimension
     % under Linux, when you write an with empty matrix to an unlimited
     % variable dimension, nc was corrupted to double object
@@ -165,7 +181,7 @@ for i=1:numel(ncv_keys)
       %nc{variable}(1:length(tsg.(variable))) = tsg.(variable); 
       nc{variable}(:) = tsg.(variable); 
     end
-  end
+  %end
 end
 
 % Close waitbar
-- 
GitLab