Skip to content
Snippets Groups Projects
Commit 426399f9 authored by pierre.rousselot_ird.fr's avatar pierre.rousselot_ird.fr
Browse files

New netcdf convention (OceanSites)

parent 0f2f1b94
No related branches found
No related tags found
No related merge requests found
function f_w_ADCP_ncOS(tc_filenamin,cell_Attributes,struct_ADCP,d_fillval)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Function to write ADCP mooring current post-processed fields into NetCDF file format following OceanSites requirements.
%
% INPUTS :
% tc_filenamin : output NETCDF file name including path
% cell_Attributes : cell array containing global attribute informations needed to create NETCDF file
% (attributes read previously from xls file)
% struct_ADCP : structure array with ADCP data (U,V), dimensions (TIME,DEPTH) and mooring name
% and positions (Name of the fields are: mooringName,mooringLat,mooringLon,time,depth,u,v)
% d_fillval : numeric value to specify fill value.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Authors : Jerome LLIDO IRD/LEGOS
% Pierre Rousselot IRD/US-IMAGO
%
% Date : 26/11/2020
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TEST INPUT ARGUMENTS %%
if nargin < 4
error('MATLAB:notEnoughInputs',...
'\nNot enough input arguments.');
end
if ~ischar(tc_filenamin)
error('MyComponent:incorrectType',...
'\nFirst input argument must be a char, not a %s.',class(tc_filenamin));
end
if ~iscell(cell_Attributes)
error('MyComponent:incorrectType',...
'\nSecond input argument must be a cell, not a %s.',class(cell_Attributes));
end
if ~isstruct(struct_ADCP)
error('MyComponent:incorrectType',...
'\nThird input argument must be a structure, not a %s.',class(struct_ADCP));
elseif ~all(isfield(struct_ADCP,{'mooringName','mooringLat','mooringLon','time','depth','u','v'}))
error('\nCheck the field names of the structure array %s. \n Fields : time, depth, mooringLat, mooringLon, u and v required.',class(struct_ADCP));
end
if ~isnumeric(d_fillval)
error('MyComponent:incorrectType',...
'\nLast input argument must be a numeric value, not a %s.',class(d_fillval));
end
%% Get start and final dates of mooring data %%
ti_startDate=julian(struct_ADCP.time(1,1));
ti_finalDate=julian(struct_ADCP.time(length(struct_ADCP.time)));
% Convert initial time from Julian Days (JD) beginning at 0000 hours, May 23, 1968 (as initialy created) to days numbers (JD) to be used as started at 0000 hours on January 01, 1950 %%
ti_timeJulD=struct_ADCP.time;
ti_timeJ=ones(1,numel(ti_timeJulD)) * NaN;
for it=1:numel(ti_timeJulD)
ti_timeJ(it)=days(datetime(ti_timeJulD(it)-0.5,'ConvertFrom','juliandate') - datetime(1950,1,1,0,0,0)); % -0.5 is necessary to remove 12h00 introduced by datetime function starting at noon instead of midnight as performed by julian function previously used
end
struct_ADCP.time=ti_timeJ;
%% Find and replace NaN by FillValue in all variables %%
% Test and replace NaN (if needed) in variable dimensions (TIME, DEPTH, LATITUDE, LONGITUDE) %
if ~isempty(find(isnan(struct_ADCP.time)))
struct_ADCP.time(find(isnan(struct_ADCP.time)))=d_fillval;
end
if ~isempty(find(isnan(struct_ADCP.depth)))
struct_ADCP.time(find(isnan(struct_ADCP.depth)))=d_fillval;
end
if ~isempty(find(isnan(struct_ADCP.mooringLat)))
struct_ADCP.time(find(isnan(struct_ADCP.mooringLat)))=d_fillval;
end
if ~isempty(find(isnan(struct_ADCP.mooringLon)))
struct_ADCP.time(find(isnan(struct_ADCP.mooringLon)))=d_fillval;
end
% Find and replace NaN by FillValue in U,V ADCP fields %
struct_ADCP.u(find(isnan(struct_ADCP.u)))=d_fillval;
struct_ADCP.v(find(isnan(struct_ADCP.v)))=d_fillval;
%% Create NetCDF file %%
ncid_ADCP = netcdf.create(tc_filenamin,'CLOBBER'); % NOCLOBBER must be used to prevent overwritting of existing file
%% Define Dimensions %%
i_dimTime = netcdf.defDim(ncid_ADCP,'TIME',length(struct_ADCP.time));
i_dimDepth = netcdf.defDim(ncid_ADCP,'DEPTH',length(struct_ADCP.depth));
i_dimLat = netcdf.defDim(ncid_ADCP,'LATITUDE',length(struct_ADCP.mooringLat));
i_dimLon = netcdf.defDim(ncid_ADCP,'LONGITUDE',length(struct_ADCP.mooringLon));
%% Create Netcdf Global Attributes %%
for j=1:length(cell_Attributes)
% Test attribute's name to fill in time_coverage_start, time_coverage_end and geospatial lat_min, lat_max, lon_min and lon_max %
if strcmp(cell_Attributes{j,1},'time_coverage_start')
netcdf.putAtt(ncid_ADCP, netcdf.getConstant('NC_GLOBAL'), 'time_coverage_start', [num2str(ti_startDate(1)) '-' num2str(ti_startDate(2),'%02d')...
'-' num2str(ti_startDate(3),'%02d') 'T' num2str(ti_startDate(4),'%02d') ':' num2str(ti_startDate(5),'%02d') ':' num2str(ti_startDate(6),'%02d') 'Z']);
elseif strcmp(cell_Attributes{j,1},'time_coverage_end')
netcdf.putAtt(ncid_ADCP, netcdf.getConstant('NC_GLOBAL'), 'time_coverage_end', [num2str(ti_finalDate(1)) '-' num2str(ti_finalDate(2),'%02d')...
'-' num2str(ti_finalDate(3),'%02d') 'T' num2str(ti_finalDate(4),'%02d') ':' num2str(ti_finalDate(5),'%02d') ':' num2str(ti_finalDate(6),'%02d') 'Z']);
elseif strcmp(cell_Attributes{j,1},'geospatial_lat_min')
netcdf.putAtt(ncid_ADCP, netcdf.getConstant('NC_GLOBAL'),'geospatial_lat_min',struct_ADCP.mooringLat);
elseif strcmp(cell_Attributes{j,1},'geospatial_lat_max')
netcdf.putAtt(ncid_ADCP, netcdf.getConstant('NC_GLOBAL'),'geospatial_lat_max',struct_ADCP.mooringLat);
elseif strcmp(cell_Attributes{j,1},'geospatial_lon_min')
netcdf.putAtt(ncid_ADCP, netcdf.getConstant('NC_GLOBAL'),'geospatial_lon_min',struct_ADCP.mooringLon);
elseif strcmp(cell_Attributes{j,1},'geospatial_lon_max')
netcdf.putAtt(ncid_ADCP, netcdf.getConstant('NC_GLOBAL'),'geospatial_lon_max',struct_ADCP.mooringLon);
else
netcdf.putAtt(ncid_ADCP, netcdf.getConstant('NC_GLOBAL'),cell_Attributes{j,1},cell_Attributes{j,2});
end
end % end loop j
% Add creation date
netcdf.putAtt(ncid_ADCP, netcdf.getConstant('NC_GLOBAL'), 'date_created', datestr(now, 'yyyy-mm-ddTHH:MM:SSZ'));
%% Create Netcdf Variables & Attributes %%
% Coordinate Variables %
f_creer_newvar2(ncid_ADCP, 'TIME', 'NC_DOUBLE', i_dimTime,...
'standard_name', 'time',...
'units', 'days since 1950-01-01 00:00:00 UTC',...
'axis', 'T',...
'long_name','time of measurement',...
'valid_min', 0.0, 'valid_max', 90000.0,...
'_FillValue', double(d_fillval),...
'QC_indicator','good data',...
'Processing_level','Data interpolated'); %% JLL 16/12/2020 : 'accuracy': ATTRIBUTE TO BE ADDED IF NECESSARY
f_creer_newvar2(ncid_ADCP, 'DEPTH', 'NC_FLOAT', i_dimDepth,...
'standard_name', 'depth',...
'units', 'meters',...
'positive', 'down',...
'axis', 'Z',...
'reference', 'sea_level',...
'coordinate_reference_frame', 'urn:ogc:def:crs:EPSG::5831',...
'_FillValue', single(d_fillval),...
'long_name', 'depth of measurement',...
'valid_min', 0, 'valid_max', 12000,...
'QC_indicator','good data',...
'Processing_level','Data interpolated'); %% JLL 16/12/2020 : 'accuracy': ATTRIBUTE TO BE ADDED IF NECESSARY
f_creer_newvar2(ncid_ADCP, 'LATITUDE', 'NC_FLOAT', i_dimLat,...
'standard_name', 'latitude',...
'units', 'degrees_north',...
'axis', 'Y',...
'long_name', 'latitude of measurement',...
'reference', 'WGS84',...
'coordinate_reference_frame', 'urn:ogc:def:crs:EPSG::4326',...
'valid_min', -90.0, 'valid_max', 90.0,...
'_FillValue', single(d_fillval),...
'QC_indicator','good data',...
'Processing_level','Data interpolated'); %% JLL 16/12/2020 : 'accuracy': ATTRIBUTE TO BE ADDED IF NECESSARY
f_creer_newvar2(ncid_ADCP, 'LONGITUDE', 'NC_FLOAT', i_dimLon,...
'standard_name', 'longitude',...
'units', 'degrees_east',...
'axis', 'X',...
'long_name', 'longitude of measurement',...
'reference', 'WGS84',...
'coordinate_reference_frame', 'urn:ogc:def:crs:EPSG::4326',...
'valid_min', -180.0, 'valid_max', 180.0,...
'_FillValue', single(d_fillval),...
'QC_indicator','good data',...
'Processing_level','Data interpolated'); %% JLL 16/12/2020 : 'accuracy': ATTRIBUTE TO BE ADDED IF NECESSARY
% Data Variables %
f_creer_newvar2(ncid_ADCP, 'UCUR', 'NC_FLOAT', [i_dimTime, i_dimDepth, i_dimLat, i_dimLon],...
'standard_name', 'eastward velocity',...
'units', 'm/s',...
'_FillValue', single(d_fillval),...
'coordinates','TIME DEPTH LATITUDE LONGITUDE',...
'long_name', 'eastward sea water velocity',...
'QC_indicator','good data',...
'processing_level','Data interpolated',...
'valid_min' ,-12, 'valid_max', 12); %% JLL 16/12/2020 : 'accuracy': ATTRIBUTE TO BE ADDED IF NECESSARY
f_creer_newvar2(ncid_ADCP, 'VCUR', 'NC_FLOAT', [i_dimTime, i_dimDepth, i_dimLat, i_dimLon],...
'standard_name', 'northward velocity',...
'units', 'm/s',...
'_FillValue', single(d_fillval),...
'coordinates','TIME DEPTH LATITUDE LONGITUDE',...
'long_name', 'northward sea water velocity',...
'QC_indicator','good data',...
'processing_level','Data interpolated',...
'valid_min' ,-12, 'valid_max', 12); %% JLL 16/12/2020 : 'accuracy': ATTRIBUTE TO BE ADDED IF NECESSARY
netcdf.endDef(ncid_ADCP); % End Netcdf variables definition
%% Write/Store Dimensions and Variables in Netcdf file
netcdf.putVar(ncid_ADCP, netcdf.inqVarID(ncid_ADCP,'TIME'),struct_ADCP.time);
netcdf.putVar(ncid_ADCP, netcdf.inqVarID(ncid_ADCP,'DEPTH'),struct_ADCP.depth);
netcdf.putVar(ncid_ADCP, netcdf.inqVarID(ncid_ADCP,'LATITUDE'),struct_ADCP.mooringLat);
netcdf.putVar(ncid_ADCP, netcdf.inqVarID(ncid_ADCP,'LONGITUDE'),struct_ADCP.mooringLon);
netcdf.putVar(ncid_ADCP, netcdf.inqVarID(ncid_ADCP,'UCUR'),struct_ADCP.u);
netcdf.putVar(ncid_ADCP, netcdf.inqVarID(ncid_ADCP,'VCUR'),struct_ADCP.v);
%% Close NetCDF file %%
netcdf.close(ncid_ADCP);
end
...@@ -5,14 +5,14 @@ ...@@ -5,14 +5,14 @@
clear all; close all; clear all; close all;
%% Variables %% Variables
FileToMerge1 = 'F:\Encours\PIRATA_ADCP-MOORINGS\Convert_ADCP/ADCP_23W0N_2001_2015.nc'; %.nc file to merge with FileToMerge1 = 'M:\PIRATA-FR31\data-processing\MOUILLAGE_ADCP\10W\ADCP_10W0N_2001_2019_1d.nc'; %.nc file to merge with
FileToMerge2 = 'F:\Encours\PIRATA_ADCP-MOORINGS\23W\0_Final_Files/ADCP_0N23W_2015_2016_1d_up_down.nc'; %.nc file to merge FileToMerge2 = 'M:\PIRATA-FR31\data-processing\MOUILLAGE_ADCP\10W\ADCP_10W0N_2019_2019_1d.nc'; %.nc file to merge
% FileToMerge3 = '/media/irdcampagnes/PIRATA/PIRATA_ADCP-MOORINGS/10W/0_Final_Files/ADCP_10W0N_2004_2005_1d.nc'; %.nc file to merge % FileToMerge3 = '/media/irdcampagnes/PIRATA/PIRATA_ADCP-MOORINGS/10W/0_Final_Files/ADCP_10W0N_2004_2005_1d.nc'; %.nc file to merge
% FileToMerge1 = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/IDMX/ADCP_0N130E_2013_2013_1d.nc'; %.nc file to merge with % FileToMerge1 = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/IDMX/ADCP_0N130E_2013_2013_1d.nc'; %.nc file to merge with
% FileToMerge2 = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/IDMX/ADCP_0N130E_2013_2016_1d.nc'; %.nc file to merge % FileToMerge2 = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/IDMX/ADCP_0N130E_2013_2016_1d.nc'; %.nc file to merge
step_subsampling = 1; % 1=daily step_subsampling = 1; % 1=daily
plot_data = 1; plot_data = 1;
mooring.name = '23W0N'; mooring.name = '10W0N';
freq = 'Variable'; freq = 'Variable';
% mooring.name = '0N130E'; % mooring.name = '0N130E';
% freq = '75'; % freq = '75';
...@@ -20,8 +20,8 @@ freq = 'Variable'; ...@@ -20,8 +20,8 @@ freq = 'Variable';
%% Read first .nc file %% Read first .nc file
ncfile1.time = ncread(FileToMerge1,'TIME'); ncfile1.time = ncread(FileToMerge1,'TIME');
ncfile1.depth = ncread(FileToMerge1,'DEPTH'); ncfile1.depth = ncread(FileToMerge1,'DEPTH');
ncfile1.u = ncread(FileToMerge1,'U')'; ncfile1.u = ncread(FileToMerge1,'UCUR')';
ncfile1.v = ncread(FileToMerge1,'V')'; ncfile1.v = ncread(FileToMerge1,'VCUR')';
%% Read second .nc file %% Read second .nc file
ncfile2.time = ncread(FileToMerge2,'TIME'); ncfile2.time = ncread(FileToMerge2,'TIME');
...@@ -29,18 +29,13 @@ ncfile2.depth = ncread(FileToMerge2,'DEPTH'); ...@@ -29,18 +29,13 @@ ncfile2.depth = ncread(FileToMerge2,'DEPTH');
ncfile2.u = ncread(FileToMerge2,'UCUR'); ncfile2.u = ncread(FileToMerge2,'UCUR');
ncfile2.v = ncread(FileToMerge2,'VCUR'); ncfile2.v = ncread(FileToMerge2,'VCUR');
% %% Read third .nc file
% ncfile3.time = ncread(FileToMerge3,'TIME');
% ncfile3.depth = ncread(FileToMerge3,'DEPTH');
% ncfile3.u = ncread(FileToMerge3,'UCUR');
% ncfile3.v = ncread(FileToMerge3,'VCUR');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Create depth matrix %% Create depth matrix
depth = max(vertcat(ncfile1.depth,ncfile2.depth)):ncfile1.depth(2)-ncfile1.depth(1):min(vertcat(ncfile1.depth,ncfile2.depth)); depth = max(vertcat(ncfile1.depth,ncfile2.depth)):ncfile1.depth(2)-ncfile1.depth(1):min(vertcat(ncfile1.depth,ncfile2.depth));
depth = fliplr(depth); depth = fliplr(depth);
max(vertcat(ncfile1.depth,ncfile2.depth)) max(vertcat(ncfile1.depth,ncfile2.depth))
% min(vertcat(ncfile1.depth,ncfile2.depth)) min(vertcat(ncfile1.depth,ncfile2.depth))
depth = 0:5:1000; depth = 0:5:350;
%% Create time matrix %% Create time matrix
time = vertcat(ncfile1.time,ncfile2.time); time = vertcat(ncfile1.time,ncfile2.time);
...@@ -51,17 +46,14 @@ time = time*ones(1,length(depth)); ...@@ -51,17 +46,14 @@ time = time*ones(1,length(depth));
%% Create u/v matrix %% Create u/v matrix
for ii = 1:length(ncfile1.time) for ii = 1:length(ncfile1.time)
u1(ii,:) = interp1(ncfile1.depth,ncfile1.u(ii,:),depth); u1(ii,:) = interp1(ncfile1.depth,ncfile1.u(:,ii),depth);
v1(ii,:) = interp1(ncfile1.depth,ncfile1.v(ii,:),depth); v1(ii,:) = interp1(ncfile1.depth,ncfile1.v(:,ii),depth);
end end
for ii = 1:length(ncfile2.time) for ii = 1:length(ncfile2.time)
u2(ii,:) = interp1(ncfile2.depth,ncfile2.u(ii,:),depth); u2(ii,:) = interp1(ncfile2.depth,ncfile2.u(ii,:),depth);
v2(ii,:) = interp1(ncfile2.depth,ncfile2.v(ii,:),depth); v2(ii,:) = interp1(ncfile2.depth,ncfile2.v(ii,:),depth);
end end
% for ii = 1:length(ncfile3.time)
% u3(ii,:) = interp1(ncfile3.depth,ncfile3.u(ii,:),depth);
% v3(ii,:) = interp1(ncfile3.depth,ncfile3.v(ii,:),depth);
% end
u = vertcat(u1,u2); u = vertcat(u1,u2);
v = vertcat(v1,v2); v = vertcat(v1,v2);
...@@ -128,7 +120,7 @@ if plot_data ...@@ -128,7 +120,7 @@ if plot_data
gregtick; gregtick;
title({[mooring.name, ' - ' num2str(date1) ' to ' num2str(date2) ' - MERIDIONAL VELOCITY - RDI ',num2str(freq),' kHz (filtered from tide)']}); title({[mooring.name, ' - ' num2str(date1) ' to ' num2str(date2) ' - MERIDIONAL VELOCITY - RDI ',num2str(freq),' kHz (filtered from tide)']});
graph_name = ['ADCP_' mooring.name '_' num2str(date1) '_' num2str(date2) '_U_V_daily']; graph_name = ['/ADCP_' mooring.name '_' num2str(date1) '_' num2str(date2) '_U_V_daily'];
set(hf,'Units','Inches'); set(hf,'Units','Inches');
pos = get(hf,'Position'); pos = get(hf,'Position');
set(hf,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)]) set(hf,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
...@@ -142,15 +134,17 @@ timed(:,5) = 0; ...@@ -142,15 +134,17 @@ timed(:,5) = 0;
timed(:,6) = 0; timed(:,6) = 0;
time = datenum(timed)-datenum(1950,01,01); time = datenum(timed)-datenum(1950,01,01);
depth = depth(:,1);
disp('****') disp('****')
disp('Creating .nc file') disp('Creating .nc file')
yr_start = datestr(time(1)+datenum(1950,01,01), 'yyyy'); yr_start = datestr(time(1)+datenum(1950,01,01), 'yyyy');
yr_end = datestr(time(end)+datenum(1950,01,01), 'yyyy'); yr_end = datestr(time(end)+datenum(1950,01,01), 'yyyy');
ncid = netcdf.create(['ADCP_',mooring.name,'_',num2str(yr_start),'_',num2str(yr_end),'.nc'],'NC_CLOBBER'); ncid = netcdf.create(['/ADCP_',mooring.name,'_',num2str(yr_start),'_',num2str(yr_end),'.nc'],'NC_CLOBBER');
%create dimension %create dimension
dimidz = netcdf.defDim(ncid, 'DEPTH', size(depth,2)); dimidz = netcdf.defDim(ncid, 'DEPTH', size(depth,1));
dimidt = netcdf.defDim(ncid, 'TIME', netcdf.getConstant('NC_UNLIMITED')); dimidt = netcdf.defDim(ncid, 'TIME', netcdf.getConstant('NC_UNLIMITED'));
%Define IDs for the dimension variables (pressure,time,latitude,...) %Define IDs for the dimension variables (pressure,time,latitude,...)
time_ID = netcdf.defVar(ncid,'TIME','double',dimidt); time_ID = netcdf.defVar(ncid,'TIME','double',dimidt);
......
...@@ -21,18 +21,21 @@ fpath_output = './0-0/'; ...@@ -21,18 +21,21 @@ fpath_output = './0-0/';
% Cruise/mooring info % Cruise/mooring info
cruise.name = 'PIRATA'; % cruise name cruise.name = 'PIRATA'; % cruise name
mooring.name = '0W0N'; % '0N10W' mooring.name = '10W0N'; % '0N10W'
mooring.lat = '0000.000'; % latitude [°'] mooring.lat = '0000.000'; % latitude [°']
mooring.lon = '0000.000'; % longitude [°'] mooring.lon = '-1000.000'; % longitude [°']
clock_drift = 146; % [seconds] clock_drift = 418; % [seconds]
% ADCP info % ADCP info
adcp.sn = 509; % ADCP serial number adcp.sn = 24629; % ADCP serial number
adcp.type = '150 khz Quartermaster'; % Type : Quartermaster, longranger adcp.type = '150 khz Quartermaster'; % Type : Quartermaster, longranger
adcp.direction = 'up'; % upward-looking 'up', downward-looking 'dn' adcp.direction = 'up'; % upward-looking 'up', downward-looking 'dn'
adcp.instr_depth = 300; % nominal instrument depth adcp.instr_depth = 300; % nominal instrument depth
instr = 1; % this is just for name convention and sorting of all mooring instruments instr = 1; % this is just for name convention and sorting of all mooring instruments
% NCFILE info
d_fillvalue = -9999;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
...@@ -682,35 +685,36 @@ end ...@@ -682,35 +685,36 @@ end
%% Write netcdf file %% Write netcdf file
disp('****') disp('****')
disp('Creating .nc file') disp('Creating .nc file')
[yr_start , ~, ~] = gregorian(inttim(1));
[yr_end, ~, ~] = gregorian(inttim(length(inttim))); % Input parameters for NETCDF Global Attributes
tc_globAttFilename = fullfile('tools/input_GlobalAttrParameters.xls'); % JLL 2020/12 Il serait judicieux de remonter cette valeur en début du script template_get_adcp_data.m
ncid = netcdf.create([fpath_output,'ADCP_',mooring.name,'_',num2str(yr_start),'_',num2str(yr_end),'_1d.nc'],'NC_WRITE');
%% Prepare informations and variables required to create NETCDF file %%
%create dimension [yr_start , ~, ~] = gregorian(data.inttim(1));
dimidt = netcdf.defDim(ncid,'time',length(inttim)); [yr_end, ~, ~] = gregorian(data.inttim(length(data.inttim)));
dimidz = netcdf.defDim(ncid,'depth',length(data.Z));
%Define IDs for the dimension variables (pressure,time,latitude,...) % Read inputs metadata required for NETCDF Global Attributes
time_ID = netcdf.defVar(ncid,'time','double',dimidt); [~,~,cell_ncAttributes] = xlsread(tc_globAttFilename);
depth_ID = netcdf.defVar(ncid,'depth','double',dimidz);
%Define the main variable () % Complete output path and filename
u_ID = netcdf.defVar(ncid,'u','double',[dimidt dimidz]); tc_ncFilenam_out = fullfile(fpath_output,['ADCP_',mooring.name,'_',num2str(yr_start),'_',num2str(yr_end),'_1d.nc']);
v_ID = netcdf.defVar(ncid,'v','double',[dimidt dimidz]);
if reply_ts == 1 % Assign a "4D-size" (TIME,DEPTH,LATITUDE,LONGITUDE) to the ADCP current variables : UINTFILT, VINTFILT
ts_ID = netcdf.defVar(ncid,'ts','double',[dimidt dimidz]); td_uADCP = ones(numel(data.inttim),numel(data.Z),numel(data.lat),numel(data.lon)) * d_fillvalue;
end td_uADCP(:,:,1,1) = data.uintfilt';
%We are done defining the NetCdf td_vADCP = ones(numel(data.inttim),numel(data.Z),numel(data.lat),numel(data.lon)) * d_fillvalue;
netcdf.endDef(ncid); td_vADCP(:,:,1,1) = data.vintfilt';
%Then store the dimension variables in
netcdf.putVar(ncid,time_ID,inttim); % Flip for convention
netcdf.putVar(ncid,depth_ID,data.Z); data.Z = fliplr(data.Z);
%Then store my main variable td_uADCP = fliplr(td_uADCP);
netcdf.putVar(ncid,u_ID,data.uintfilt'); td_vADCP = fliplr(td_vADCP);
netcdf.putVar(ncid,v_ID,data.vintfilt');
if reply_ts == 1 % Group general ADCP mooring informations and ADCP data to be written in NETCDF file format
netcdf.putVar(ncid,ts_ID,data.tsintfilt'); struct_dataADCP = struct('mooringName', mooring.name, 'mooringLat', mooring.lat,...
end 'mooringLon', mooring.lon, 'time', data.inttim, 'depth', data.Z,...
%We're done, close the netcdf 'u', td_uADCP, 'v', td_vADCP);
netcdf.close(ncid);
disp('****') %% Write netcdf file %%
% ------------------------------------------------------------------------------------------- f_w_ADCP_ncOS(tc_ncFilenam_out,cell_ncAttributes,struct_dataADCP,d_fillvalue);
disp('****')
\ No newline at end of file
function f_creer_newvar2(nc,namevar,type,dimid,varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Cascade Exploitation :
% -----------------------
%
% Fonction permettant de creer une nouvelle
% Variable dans un fichier netcdf deja ouvert.
% Fonction a utiliser pour creer plusieurs variables dans un meme fichier NetCDF existant.
%
% f_creer_newvar2(filenc,namevar,type,dimvar,varargin)
%
% En entree :
% -------------
% nc : identifiant du fichier NetCDF ou la variable doit etre creee.
% namevar : nom de la variable a creer
% type : Type de la variable
% dimid : Liste des identifiants des dimensions de la variable.
% varargin : liste(nom,valeur) des attributs de la variable a creer. ex: 'long_name','Vitessse en ms'
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% C. Kermabon - P Le Bot Avril 2009
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definition/Creation de la variable.
id_var=netcdf.defVar(nc,namevar,type,dimid);
% Et de ses attributs.
for iatt=1:2:size(varargin,2)
netcdf.putAtt(nc,id_var,varargin{iatt},varargin{iatt+1});
end
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment