Skip to content

Instantly share code, notes, and snippets.

@lgloege
Created April 16, 2018 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgloege/a1d4c490465a0507a1121bddbac3698f to your computer and use it in GitHub Desktop.
Save lgloege/a1d4c490465a0507a1121bddbac3698f to your computer and use it in GitHub Desktop.
reads CbPM from web
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get CbPM carbon mg / m**3:
% This script reads CbPM data from web and saves to file.
%
% Source: http://www.science.oregonstate.edu/ocean.productivity/custom.php
%
% L. Gloege 2016 (Augmented a script from A.Fay)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear; clc
% Which satellite do you want to read?
% s = SEAWIFS
% m = MODIS
satellite = 'm';
% defines year range for seawifs and modis satellite
if strcmp(satellite,'s')==1
yearRange = [1997 2010];
elseif strcmp(satellite,'m')==1
yearRange = [2010 2011];
else
error([satellite ' is not an option. choose s for seawif or m for modis']);
end
% day of year for nonleap year (mo) and leapyears (mol)
mo = ['001';'032';'060';'091';'121';'152';'182';'213';'244';'274';'305';'335'];
mol = ['001';'032';'061';'092';'122';'153';'183';'214';'245';'275';'306';'336'];
% initialize time vector
time_CbPM_carbon = [];
iter = 1;
for yr = yearRange(1):yearRange(2)
disp(yr)
for m = 1:12
% Controls Leap Years
if yr == 2000 || yr == 2004 || yr == 2008 || yr == 2012 %leap years
mon = mol;
else
mon = mo;
end
% Defines data URL
fl1 = 'http://orca.science.oregonstate.edu/data/1x2/monthly/';
fl2 = ['carbon.' satellite '.r2014.gsm.v8/hdf/'];
filename = ['carbon.' num2str(yr) num2str(mon(m,:)) '.hdf'];
url=[fl1 fl2 filename '.gz'];
try
outfilename = websave([filename '.gz'],url);
system(['gunzip ' outfilename])
%load in hdf file
file_info = hdfinfo(filename);
sds_info = file_info.SDS;
x = hdfread(sds_info);
[nrow,ncol]=size(x);
x(x==-9999.)=NaN;
x = double(x); % Loaded as single, needs be double
x2 = flipud(x); % orients globe to correct direction
if exist('lat_Cbpm_carbon','var')==0
lat_Cbpm_carbon=linspace(-90,90,nrow);
lon_Cbpm_carbon=linspace(-180,180,ncol);
[X_Cbpm_carbon,Y_Cbpm_carbon]=meshgrid(lon_Cbpm_carbon,lat_Cbpm_carbon);
end
% Allocate space for this
CbPM_carbon(iter,:,:) = x2;
% update time vector in [year month day] format
time_CbPM_carbon = [time_CbPM_carbon; yr m 1];
% clear variables you do not need anymore
clear x x2 filename file* sds_info
%update Iteration
iter = iter+1;
catch
continue
end
end
end
% put data into structure
disp('Storing data as structure...')
out.time = time_CbPM_carbon;
out.biomass = CbPM_carbon;
out.lat = lat_Cbpm_carbon;
out.lon = lon_Cbpm_carbon;
out.X = X_Cbpm_carbon;
out.Y = Y_Cbpm_carbon;
out.UNITS = {'biomass units are mg m**3'; 'time units YYYY MM DD'};
out.SOURCE = {'http://www.science.oregonstate.edu/ocean.productivity/custom.php'};
out.ACCESSDATE = [date];
% save data
disp('Saving data...')
if strcmp(satellite,'s')==1
outFile = 'cbpm_seawifs.mat';
cbpm_seawifs=out; clear out;
save(outFile,'cbpm_seawifs','-v7.3');
elseif strcmp(satellite,'m')==1
outFile = 'cbpm_modis.mat';
cbpm_modis=out; clear out;
save(outFile,'cbpm_modis','-v7.3');
else
error('Satellite needs to be s or m')
end
% completetion statement
disp('Complete!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment