Skip to content

Demo Download a subset of curvilinear coordinates model (eg., ROMS) output via OpenDAP

HappySpring edited this page Sep 20, 2021 · 1 revision
%% 
% =========================================================================
% Parameters
% =========================================================================

% URL of data in the rmote server
     filename0 = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.nc';
 

% ## Define the a subset of the domain ------------------------------------

    % ### months
     month_list = datenum(2017,1:24, 1);

    % ### define spatial limit 

    % + select vertical layers
     s_limit   = [-0.02 0];  % it is s-coordinate used in ROMS. So you need to specify the range for the variable "s_rho".
    
    % + select subdomain horizontally
     is_horizon_limit = true;
     
    if is_horizon_limit == true
        
        % define lon/lat range
        lon_limit = [-82   -70];
        lat_limit = [26    55 ];
        
        % convert lonlimit/latlimit to eta_rho, xi_rho
        %    this is not a big problem for Ruying's output since they do not
        %    rotate or stretch the grid. 

        dim_eta_name = 'eta_rho'; % it need to be edited to eta_u or eta_v for currents
        dim_xi_name  = 'xi_rho';  % it need to be edited to xi_u or xi_v for currents
        lon_varname  = 'lon_rho';
        lat_varname  = 'lat_rho';
        
        N_eta_rho = FUN_nc_get_dim_length( filename0, dim_eta_name );
        N_xi_rho  = FUN_nc_get_dim_length( filename0, dim_xi_name );
        i_eta_rho   = 1 : N_eta_rho;
        i_xi_rho    = 1 : N_xi_rho;

        lon_rho = FUN_nc_varget_enhanced( filename0, lon_varname ); %it should be lon_u, or lon_v for currents.
        lat_rho = FUN_nc_varget_enhanced( filename0, lat_varname );

        [ xi_selected, eta_selected ] = find([ lon_rho >= lon_limit(1) &  lon_rho <= lon_limit(end) &  lat_rho  >= lat_limit(1) &  lat_rho  <= lat_limit(end)] );

        eta_limit = [min(eta_selected), max(eta_selected)];
        xi_limit  = [min(xi_selected), max(xi_selected)];
    end

% ## Specify the variables to be downloaded -------------------------------

    % variable to be downloaded
     var_download = {'lon_rho','lat_rho','ocean_time','Cs_r','h','hc','s_rho','temp'}; % empty indicates downloading all variables

    % Variables that should be downloaded piece by piece
    % 
     var_divided  = {'temp'};

    % which dim you'd like to download piece by piece (e.g., 'time', or 'depth')
     divided_dim_str = 'ocean_time';
     
     % max size of each "piece"
     Max_Count_per_group = 1; 

% output -----------------------------------------------------------------
     output_folder = 'temp';
     mkdir(output_folder);

     output_filename_prefix = 'model_output_temp';
%% 
% =========================================================================
% Download
% =========================================================================

 % Loop for each month
 for im = 1:length( month_list )
 
     [year_now, month_now, ~] = datevec( month_list(im) );
     timestr_now = datestr( month_list(im), 'yyyy-mm' );
     
     timelimit_now = [ datenum( year_now, month_now, 1 ), datenum( year_now, month_now+1, 0, 23, 59, 59 ) ];
     
    % output filename
     filename1 = fullfile( output_folder, [output_filename_prefix, '_', timestr_now '.nc'] );

    % calculate time limits
     timelimit  = timelimit_now;
    
     if is_horizon_limit == true
         dim_limit_var    = {'ocean_time', 's_rho',   dim_eta_name, dim_xi_name };
         dim_limit_val    = {timelimit,     s_limit,  eta_limit,    xi_limit   };
         dim_varname_list = {'ocean_time', 's_rho',   i_eta_rho,    i_xi_rho};
         
         FUN_nc_OpenDAP_with_limit( filename0, filename1, dim_limit_var, dim_limit_val, var_download, var_divided, divided_dim_str, Max_Count_per_group, ...
             'is_auto_chunksize', true, 'time_var_name', 'ocean_time', 'dim_varname', dim_varname_list );
                            
     else
         dim_limit_var = {'ocean_time', 's_rho'};
         dim_limit_val = {timelimit,    s_limit};
         
         FUN_nc_OpenDAP_with_limit( filename0, filename1, dim_limit_var, dim_limit_val, var_download, var_divided, divided_dim_str, Max_Count_per_group, ...
                                'is_auto_chunksize', true, 'time_var_name', 'ocean_time' );
     end



end
 
Clone this wiki locally