From f58be82f678c68f539ddfb88b9643e6d7aabb50f Mon Sep 17 00:00:00 2001 From: Olivier Sauter <Olivier.Sauter@epfl.ch> Date: Thu, 14 Nov 2019 21:31:25 +0100 Subject: [PATCH] add netcdf hdf5 related get files --- matlab/CHDF/cdf2mat.m | 105 ++++++++++++++++++++ matlab/CHDF/transp_netcdf2mat.m | 160 ++++++++++++++++++++++++++++++ matlab/TCV/gdat_tcv.m | 32 +++++- matlab/TCV/tcv_help_parameters.m | 1 + matlab/TCV/tcv_requests_mapping.m | 2 +- matlab/gdatpaths.m | 2 +- 6 files changed, 299 insertions(+), 3 deletions(-) create mode 100644 matlab/CHDF/cdf2mat.m create mode 100644 matlab/CHDF/transp_netcdf2mat.m diff --git a/matlab/CHDF/cdf2mat.m b/matlab/CHDF/cdf2mat.m new file mode 100644 index 00000000..22eaf3b7 --- /dev/null +++ b/matlab/CHDF/cdf2mat.m @@ -0,0 +1,105 @@ +function cdf2mat_out = cdf2mat(pfname) +% +% reads all variables and coordinates from netcdf +% some trials with netcdf, using 50725c01.cdf as test +% +% Based on TANSP netcdf file, but should work with other if simple group structure probably +% +% Uses matlab netcdf.open, ncinfo, netcdf.inqVarIDs, netcd.getVar, etc +% + +% +if ~exist(pfname,'file') + error([pfname ' is not a valid filename for a netcdf file']); +end + +try + funnetcdf=netcdf.open(pfname,'nowrite'); +catch ME + warning(['problems with netcdf.open(' pfname ',''nowrite''); May be not a netcdf file?']); + rethrow(ME) +end + +% all data of all vars + +allinfo=ncinfo(pfname); + +allvarids=netcdf.inqVarIDs(funnetcdf); +allvarnames={allinfo.Variables(:).Name}; +if length(allvarnames) ~= length(allvarids) + allinfo; + error('problem with Variables, may be several groups') + return +end + +[varnames_sorted,~]=sort(allvarnames); + +% to find a variable: +% strmatch('GFUN',allvarnames,'exact') + +% construct relevant data strtucture + +coordnames=strtrim({allinfo.Dimensions(:).Name}); +[coordnames_sorted,~]=sort(coordnames); +fields_variables_to_copy = {'Name', 'Dimensions', 'Size', 'Datatype'}; +for i=1:length(coordnames_sorted) + matcdf.coords(i).name = coordnames_sorted{i}; + matcdf.coords(i).index_allvarnames = strmatch(matcdf.coords(i).name,allvarnames,'exact'); + matcdf.coords(i).index_varnames_sorted = strmatch(matcdf.coords(i).name,varnames_sorted,'exact'); + matcdf.coords(i).varid = allvarids(matcdf.coords(i).index_allvarnames); + if strcmp(allinfo.Variables(matcdf.coords(i).index_allvarnames).Datatype,'single') + matcdf.coords(i).data = netcdf.getVar(funnetcdf,matcdf.coords(i).varid,'double'); + else + matcdf.coords(i).data = netcdf.getVar(funnetcdf,matcdf.coords(i).varid); + end + for ij=1:length(fields_variables_to_copy) + matcdf.coords(i).(fields_variables_to_copy{ij}) = allinfo.Variables(matcdf.coords(i).index_allvarnames).(fields_variables_to_copy{ij}); + end + matcdf.coords(i).units = strtrim(allinfo.Variables(matcdf.coords(i).index_allvarnames).Attributes(1).Value); + matcdf.coords(i).long_name = strtrim(allinfo.Variables(matcdf.coords(i).index_allvarnames).Attributes(2).Value); + matcdf.coords(i).label = [matcdf.coords(i).name ' ' num2str(matcdf.coords(i).Size) ': ' matcdf.coords(i).long_name]; + cdf2mat_out.coords.(matcdf.coords(i).name) = matcdf.coords(i); +end + +for i=1:length(varnames_sorted) + matcdf.vars(i).name = varnames_sorted{i}; + matcdf.vars(i).index_allvarnames = strmatch(matcdf.vars(i).name,allvarnames,'exact'); + matcdf.vars(i).varid = allvarids(matcdf.vars(i).index_allvarnames); + if strcmp(allinfo.Variables(matcdf.vars(i).index_allvarnames).Datatype,'single') + matcdf.vars(i).data = netcdf.getVar(funnetcdf,matcdf.vars(i).varid,'double'); + else + matcdf.vars(i).data = netcdf.getVar(funnetcdf,matcdf.vars(i).varid); + end + for ij=1:length(fields_variables_to_copy) + matcdf.vars(i).(fields_variables_to_copy{ij}) = allinfo.Variables(matcdf.vars(i).index_allvarnames).(fields_variables_to_copy{ij}); + end + matcdf.vars(i).units = strtrim(allinfo.Variables(matcdf.vars(i).index_allvarnames).Attributes(1).Value); + matcdf.vars(i).long_name = strtrim(allinfo.Variables(matcdf.vars(i).index_allvarnames).Attributes(2).Value); + matcdf.vars(i).label = matcdf.vars(i).name; + for j=1:length(matcdf.vars(i).Dimensions) + ij = strmatch(matcdf.vars(i).Dimensions(j).Name,coordnames_sorted,'exact'); + matcdf.vars(i).dim{j} = matcdf.coords(ij).data; + matcdf.vars(i).dimunits{j} = matcdf.coords(ij).units; + matcdf.vars(i).dimname{j} = matcdf.coords(ij).name; + if j==1 + matcdf.vars(i).label = [matcdf.vars(i).label '(' matcdf.coords(ij).name]; + else + matcdf.vars(i).label = [matcdf.vars(i).label ',' matcdf.coords(ij).name]; + end + if j==length(matcdf.vars(i).Dimensions) + matcdf.vars(i).label = [matcdf.vars(i).label ')']; + end + end + matcdf.vars(i).label = [matcdf.vars(i).label ': ' matcdf.vars(i).long_name ' [' matcdf.vars(i).units ']']; + cdf2mat_out.allvars.(matcdf.vars(i).name) = matcdf.vars(i); +end +top_attr_names = {allinfo.Attributes(:).Name}; +ij = strmatch('shot',top_attr_names,'exact'); +cdf2mat_out.shot = allinfo.Attributes(ij).Value; +cdf2mat_out.fname = pfname; +cdf2mat_out.id = pfname(length(pfname)-11:length(pfname)-4); + +netcdf.close(funnetcdf); + +clear matcdf +return diff --git a/matlab/CHDF/transp_netcdf2mat.m b/matlab/CHDF/transp_netcdf2mat.m new file mode 100644 index 00000000..575bd91c --- /dev/null +++ b/matlab/CHDF/transp_netcdf2mat.m @@ -0,0 +1,160 @@ +% +% some trials with netcdf, using 50725c01.cdf as test +% +% Installed matlab netcdf toolbox from web (http://crusty.er.usgs.gov/~cdenham/MexCDF/nc4ml5.html#GUIDE) +% +% then + +[fname,pname]=uigetfile('*.cdf','Open NetCDF File'); +funnetcdf=netcdf.open([pname fname],'nowrite'); + +% all data of all vars +allinfo=ncinfo([pname fname]); +allvarids=netcdf.inqVarIDs(funnetcdf); +allvarnames={allinfo.Variables(:).Name}; +if length(allvarnames) ~= length(allvarids) + allinfo + error('problem with Variables, may be several groups') + return +end + +[varnames_sorted,ind_sort_varnames]=sort(allvarnames); + +% to find a variable: +% strmatch('GFUN',allvarnames,'exact') + +% then data of var{1} of name allvarnames{1} (='TIME') can be obtained from: +% time_data=funnetcdf{'TIME'}(:); +% or +% time_data=funnetcdf{allvarnames{1}}(:); + +% coordinates variables: +% >>coord(funnetcdf) +% TIME +% TIME3 +% X +% XB +% THETA +% EION +% KSID +% RMJSYM +% RMAJM +% ILDEN +% IVISB +% INTNC + +% construct relevant data strtucture + +coordnames=strtrim({allinfo.Dimensions(:).Name}); +[coordnames_sorted,ind_sort_coordnames]=sort(coordnames); +fields_variables_to_copy = {'Name', 'Dimensions', 'Size', 'Datatype'}; +for i=1:length(coordnames_sorted) + matcdf.coords(i).name = coordnames_sorted{i}; + matcdf.coords(i).index_allvarnames = strmatch(matcdf.coords(i).name,allvarnames,'exact'); + matcdf.coords(i).index_varnames_sorted = strmatch(matcdf.coords(i).name,varnames_sorted,'exact'); + matcdf.coords(i).varid = allvarids(matcdf.coords(i).index_allvarnames); + if strcmp(allinfo.Variables(matcdf.coords(i).index_allvarnames).Datatype,'single') + matcdf.coords(i).data = netcdf.getVar(funnetcdf,matcdf.coords(i).varid,'double'); + else + matcdf.coords(i).data = netcdf.getVar(funnetcdf,matcdf.coords(i).varid); + end + for ij=1:length(fields_variables_to_copy) + matcdf.coords(i).(fields_variables_to_copy{ij}) = allinfo.Variables(matcdf.coords(i).index_allvarnames).(fields_variables_to_copy{ij}); + end + matcdf.coords(i).units = strtrim(allinfo.Variables(matcdf.coords(i).index_allvarnames).Attributes(1).Value); + matcdf.coords(i).long_name = strtrim(allinfo.Variables(matcdf.coords(i).index_allvarnames).Attributes(2).Value); + matcdf.coords(i).label = [matcdf.coords(i).name ' ' num2str(matcdf.coords(i).Size) ': ' matcdf.coords(i).long_name]; + matcdf_per_name.coords.(matcdf.coords(i).name) = matcdf.coords(i); +end + +for i=1:length(varnames_sorted) + matcdf.vars(i).name = varnames_sorted{i}; + matcdf.vars(i).index_allvarnames = strmatch(matcdf.vars(i).name,allvarnames,'exact'); + matcdf.vars(i).varid = allvarids(matcdf.vars(i).index_allvarnames); + if strcmp(allinfo.Variables(matcdf.vars(i).index_allvarnames).Datatype,'single') + matcdf.vars(i).data = netcdf.getVar(funnetcdf,matcdf.vars(i).varid,'double'); + else + matcdf.vars(i).data = netcdf.getVar(funnetcdf,matcdf.vars(i).varid); + end + for ij=1:length(fields_variables_to_copy) + matcdf.vars(i).(fields_variables_to_copy{ij}) = allinfo.Variables(matcdf.vars(i).index_allvarnames).(fields_variables_to_copy{ij}); + end + matcdf.vars(i).units = strtrim(allinfo.Variables(matcdf.vars(i).index_allvarnames).Attributes(1).Value); + matcdf.vars(i).long_name = strtrim(allinfo.Variables(matcdf.vars(i).index_allvarnames).Attributes(2).Value); + matcdf.vars(i).label = matcdf.vars(i).name; + for j=1:length(matcdf.vars(i).Dimensions) + ij = strmatch(matcdf.vars(i).Dimensions(j).Name,coordnames_sorted,'exact'); + matcdf.vars(i).dim{j} = matcdf.coords(ij).data; + matcdf.vars(i).dimunits{j} = matcdf.coords(ij).units; + matcdf.vars(i).dimname{j} = matcdf.coords(ij).name; + if j==1 + matcdf.vars(i).label = [matcdf.vars(i).label '(' matcdf.coords(ij).name]; + else + matcdf.vars(i).label = [matcdf.vars(i).label ',' matcdf.coords(ij).name]; + end + if j==length(matcdf.vars(i).Dimensions) + matcdf.vars(i).label = [matcdf.vars(i).label ')']; + end + end + matcdf.vars(i).label = [matcdf.vars(i).label ': ' matcdf.vars(i).long_name ' [' matcdf.vars(i).units ']']; + matcdf_per_name.allvars.(matcdf.vars(i).name) = matcdf.vars(i); +end +top_attr_names = {allinfo.Attributes(:).Name}; +ij = strmatch('shot',top_attr_names,'exact'); +matcdf_per_name.shot = allinfo.Attributes(ij).Value; + +% +% save 63704V32_matcdf.mat matcdf_per_name +clear matcdf +netcdf.close(funnetcdf); + +% break +% to see the list: +%% + +% load 63704V32_matcdf.mat +%% + +varnames = fieldnames(matcdf_per_name.allvars); +for i=1:length(varnames) + varnames_labels{i} = matcdf_per_name.allvars.(varnames{i}).label; +end +coordnames = fieldnames(matcdf_per_name.coords); +for i=1:length(coordnames) + coordnames_labels{i} = matcdf_per_name.coords.(coordnames{i}).label; +end +%% +matcdf.hfig_main=figure; +set(matcdf.hfig_main,'position',[50 80 900 750]) +matcdf.hvarnames=uicontrol('style','listbox','string',varnames_labels,'position',[20 205 800 510]); +matcdf.hvarnames_text=uicontrol('style','text','string','variables (dims): long name [units]','position',[20 720 300 15]); + +matcdf.hcoord=uicontrol('style','listbox','string',coordnames_labels,'position',[20 20 600 160]); +matcdf.hcoord_text=uicontrol('style','text','string','coordinates (size)','position',[20 185 100 15]); + +matcdf.h(1)=uicontrol('style','pushbutton','String','Display variable','position',[650 20 200 20], ... + 'Callback','aa=matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')})'); +matcdf.h(2)=uicontrol('style','pushbutton','String','Plot variable','position',[650 50 200 20], ... + 'Callback','figure(matcdf.hfig_plot);plot(matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).data)'); + +matcdf.h(3)=uicontrol('style','pushbutton','String','Plot transposed var.','position',[650 80 200 20], ... + 'Callback','figure(matcdf.hfig_plot);plot(matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).data'')'); +cback = ['figure(matcdf.hfig_plot);plot(matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).dim{1},' ... + 'matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).data); ', ... + 'xlabel([matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).dimname{1} '' '' ', ... + 'matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).dimunits{1}]);', ... + 'ylabel([matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).name '' '' ', ... + 'matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).units]);']; +matcdf.h(4)=uicontrol('style','pushbutton','String','Plot vs dim 1','position',[650 110 200 20], ... + 'Callback',cback); +cback = ['figure(matcdf.hfig_plot);plot(matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).dim{2},' ... + 'matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).data); ', ... + 'xlabel([matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).dimname{2} '' '' ', ... + 'matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).dimunits{2}]);', ... + 'ylabel([matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).name '' '' ', ... + 'matcdf_per_name.allvars.(varnames{get(matcdf.hvarnames,''Value'')}).units]);']; +matcdf.h(5)=uicontrol('style','pushbutton','String','Plot vs dim 2','position',[650 140 200 20], ... + 'Callback',cback); + +matcdf.hfig_plot=figure; +%% diff --git a/matlab/TCV/gdat_tcv.m b/matlab/TCV/gdat_tcv.m index 65c4e01c..6d6c6986 100644 --- a/matlab/TCV/gdat_tcv.m +++ b/matlab/TCV/gdat_tcv.m @@ -222,7 +222,7 @@ if (nargin>=ivarargin_first_char) end % enforce lower case for any character driven input if ischar(varargin_eff{i+1}) && ~strcmp('path',varargin_eff{i}) - gdat_params.(lower(varargin_eff{i})) = lower(varargin_eff{i+1}); + gdat_params.(lower(varargin_eff{i})) = varargin_eff{i+1}; % cannot enforce lower(params value) if filename or else are case sensitive else gdat_params.(lower(varargin_eff{i})) = varargin_eff{i+1}; end @@ -3142,6 +3142,36 @@ keyboard end + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + case {'transp'} + % read transp netcdf output file provided in source parameter and generate substructure with all variables + if ~isfield(gdat_data.gdat_params,'source') || isempty(gdat_data.gdat_params.source) || ~exist(gdat_data.gdat_params.source,'file') + warning([char(10) 'requires a netcdf file in ''source'' parameter' char(10)]) + return + end + + gdat_data.transp = cdf2mat(gdat_data.gdat_params.source); + + % deal with errors and exceptions + % XB and X are rhotor_norm and not r/a radii + vars2change = {'X', 'XB'}; + main_substruct = {'coords','allvars'}; + subfields = {'long_name', 'label'}; + for i=1:length(vars2change) + for j=1:length(main_substruct) + for k=1:length(subfields) + try + gdat_data.transp.(main_substruct{j}).(vars2change{i}).(subfields{k}) = strrep(gdat_data.transp.coords.(vars2change{i}).(subfields{k}),'x"r/a" bdy','sqrt(Phi/Philim)'); + gdat_data.transp.(main_substruct{j}).(vars2change{i}).(subfields{k}) = strrep(gdat_data.transp.coords.(vars2change{i}).(subfields{k}),'x"r/a" ctr', ... + 'sqrt(Phi/Philim)'); + catch + end + end + end + end + gdat_data.data_fullpath = ['.transp structure obtained from cdf2mat(gdat_data.gdat_params.source)']; + gdat_data.help='Can use transp_gui(gdat_data.transp) to browse and plot'; + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% case {'ttprime', 'ttprime_rho'} if liuqe_matlab==0 diff --git a/matlab/TCV/tcv_help_parameters.m b/matlab/TCV/tcv_help_parameters.m index 289e15be..2d232d30 100644 --- a/matlab/TCV/tcv_help_parameters.m +++ b/matlab/TCV/tcv_help_parameters.m @@ -45,6 +45,7 @@ help_struct_all.source = sprintf('%s\n','cxrs: [1 2 3] (default systems);', ... 'mhd request: ''23'':23 LFS/HFS (default), ''23full'': 23cm sector 3 and 11, ''0'':z=0 LFS/HFS, ''0full'': 0cm sector 3 and 11', ... 'powers: ohmic in any case + ''ec'', ''nbi'', ''rad'' ', ... 'rtc: defined, all, adcs', ... + 'transp: source provides the netcdf file', ... 'icds: ''ec'', ''nbi'' ', ... 'ids_names for request ''ids'' like magnetics, equilibrium, etc'); help_struct_all.source_ec = sprintf('%s\n','toray (for toray nodes), no other source for eccd yet implemented'); diff --git a/matlab/TCV/tcv_requests_mapping.m b/matlab/TCV/tcv_requests_mapping.m index b503f245..499bbd90 100644 --- a/matlab/TCV/tcv_requests_mapping.m +++ b/matlab/TCV/tcv_requests_mapping.m @@ -423,7 +423,7 @@ switch lower(data_request) mapping.label = 'Ti'; mapping.method = 'switchcase'; case 'transp' - mapping.label = 'transp output'; + mapping.label = 'transp output from netcdf file'; mapping.method = 'switchcase'; case {'ttprime', 'ttprime_rho'} mapping.timedim = 2; diff --git a/matlab/gdatpaths.m b/matlab/gdatpaths.m index d7dcdd43..108aff2e 100644 --- a/matlab/gdatpaths.m +++ b/matlab/gdatpaths.m @@ -19,7 +19,7 @@ a = fileparts(a); % machines=[{'JET'} {'TCV'} {'AUG'} {'D3D'}]; % machines=[{'JET'} {'TCV'} {'AUG'} {'KSTAR'}]; -machines=[{'JET'} {'TCV'} {'AUG'} {'D3D'} {'KSTAR'} {'TCV_IMAS'} {'AUG_IMAS'}]; +machines=[{'JET'} {'TCV'} {'AUG'} {'D3D'} {'KSTAR'} {'TCV_IMAS'} {'AUG_IMAS'} {'CHDF'}]; add_paths = cell(1,length(machines)); for i=1:length(machines) -- GitLab