function [good,bad]=do_sta2ncml(server, prefix) %do_sta2ncml- generates CF compliant NcML from WHSC Time Series Data % requires one argument that should be in the form 'MBAY_LT/' % ** 12/09 upgrade to hyrax has the url path looking like this % http://stellwagen.er.usgs.gov/opendap/HUDSON_SVALLEY06/ instead of % http://stellwagen.er.usgs.gov/cgi-bin/nph_dods/DATAFILES/HUDSON_SVALLEY06 % code and doc modified to reflect this change % right now works directory by directory % usage: do_sta2ncml('http://stellwagen.er.usgs.gov:8082/thredds/dodsC/TSdata/','ARGO_MERCHANT/') % You MUST use the slash at the end of the input string!! % outputs: ARGO_MERCHANT.ncml % new Apr/2010 % good: list of "good" urls (urls that worked) % bad: list of "bad" urls. (urls that did not open with mDataset) % only the "good ones will be included in the ncml. The others have to % be fixed before they can be included % use the thredds server opendap, not the hyrax/opendap if ~exist('server','var') server='http://stellwagen.er.usgs.gov/thredds/dodsC/TSdata/'; end % use the stellwage Hyrax opendap, not the thredds/opendap % server='http://stellwagen.er.usgs.gov/opendap/'; if ~exist('prefix','var') prefix=input('Enter a directory name containing data files on stellwagen') end url_base=[server prefix]; % grab all the files in this directory... % depending on where you're getting the data, have to use different parsers % because the html generated by urlread of a hyrax/opendap page is different % than the html generated by urlread of a thredds/opendap page if isempty(strfind(server,'thredds')) files=get_files_from_url(url_base); else files=get_files_from_TDS(url_base); end %... or use specific files %files={'3392-a1h.nc','3412-a1h.nc','3482-a1h.nc'}; %make a root name % don't always need to pull out the slashes since prefix should be just one level sep=filesep; slsh=findstr(sep, prefix); if length(slsh)==1 rname=prefix(1:slsh(1)-1); else rname=prefix(slsh(1)+1:slsh(2)-1) end fid=fopen([rname '.ncml'],'wt'); igood=0; ibad=0; for i=1:length(files) % process all files %for i=1:5 % process only the 1st file stafile=char(files(i)); %% %if ~isempty(strfind(lower(stafile),'a1h')); %process only a1h files stafile if (strfind(stafile,'bst')) % this is for Wrightsville, that has burst data disp ('burst file, skipping') % the bursts generate java heap errors else url=[url_base stafile]; try % replaces get_whfc_meta 4/19/10 % get_whfc_time has correct cast to double required usind mDataset [mvals]=get_whfc_time(url); if mvals.timebase_uniform %start=mvals.mjd_start; % matlab datenum %dt=mvals.dtms; % convert dt (seconds => days) site=['USGS/CMGP-' stafile(1:3)]; %sta2ncml(fid,server,prefix,stafile,start,dt,site); sta2ncml(fid,server,prefix,stafile,mvals,site); end igood=igood+1; good{igood}=url; catch ME ibad=ibad+1; bad.addr{ibad}=url; % when opendap error checker fails it's retrievable with lasterror prob=lasterror; msg_str=getfield(prob,'message'); % now save the message c=textscan(msg_str,'%s','HeaderLines',1, 'EndOfLine','\n'); if strmatch(char(c{1}(9)),'at') bad.cause{ibad}=msg_str(1:100); else bad.cause{ibad}=char(c{1}(9)); %it's usually the 9th line end end end %end %% end fclose(fid); % to output a sensible list of contents of bad, enter: if(ibad > 0) % only do if there's bad data rsons=char(bad.cause); addrs=char(bad.addr); qbad=strcat(addrs, '- => ', rsons) oname=[prefix(1:end-1) '_ng.txt'] % now save it for posterity fid=fopen(oname,'w') [p,q]=size(qbad); fmt=['%' num2str(q) 's\n']; for ik=1:p fprintf(fid,fmt,qbad(ik,:)) end fclose(fid) else bad=0; %must return something in bad end end