function [avail_diag]=read_avail_diag_v2(filAvailDiag,nameDiag);
%This routine is copied/adapted from gcmfaces_IO/process2nctiles.m
%
%Read some relevant information form available_diagnostics.log about diag nameDiag
%
%Input:
%       filAvailDiag    full path of available_diagnostics.log
%       nameDiag        name of diagnostic being processed
%
%Output:
%       avail_diag      matlab structure with info about the diagnostic
%e.g.:
%>> avail_diag
%avail_diag = 
%  struct with fields:
%
%           units: 'degC'
%    longNameDiag: 'Potential Temperature'
%           loc_h: 'C'
%           loc_z: 'M'
%              nr: 50
%
% created by Victor Ocana 2019
% atn: mod on 15may2022 to accommodate 9char instead of 8char str

Lchar=9;%8
avail_diag=[];
fid=fopen(filAvailDiag,'rt');
while ~feof(fid);
    tline = fgetl(fid);
    tmp1=Lchar-length(nameDiag); tmp1=repmat(' ',[1 tmp1]);
    tname = ['|' sprintf('%s',nameDiag) tmp1 '|'];
    if ~isempty(strfind(tline,tname));
        %e.g. tline='   235 |SIatmQnt|  1 |       |SM      U1|W/m^2           |Net atmospheric heat flux, >0 decreases theta';
        %
        tmp1=strfind(tline,'|'); tmp1=tmp1(end-1:end);
        avail_diag.units=strtrim(tline(tmp1(1)+1:tmp1(2)-1));
        avail_diag.longNameDiag=tline(tmp1(2)+1:end);
        %
        tmp1=strfind(tline,'|'); tmp1=tmp1(4:5);
        pars=tline(tmp1(1)+1:tmp1(2)-1);
        %
        if strcmp(pars(2),'M'); avail_diag.loc_h='C';
        elseif strcmp(pars(2),'U'); avail_diag.loc_h='W';
        elseif strcmp(pars(2),'V'); avail_diag.loc_h='S';
        end;
        %
        avail_diag.loc_z=pars(9);
        %
        if strcmp(pars(10),'1'); avail_diag.nr=1;
        else; tmp1=strfind(tline,'|'); tmp1=tmp1(2:3); avail_diag.nr=str2num(tline(tmp1(1)+2:tmp1(2)-2));% avail_diag.nr=length(mygrid.RC);
        end;
    end;
end;
fclose(fid);

