制作nc文件流程——以站点降水数据为例

2023-10-13 20:10

本文主要是介绍制作nc文件流程——以站点降水数据为例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

netcdf文件制作流程

      • 1. define dimensions
      • 2. define variables
      • 3. put variables into nc
      • 4. put attribute into nc
      • 5. define global attribution
      • 6.附上代码全文

【提示】 事先想好需要存储变量的结构及维度,之后需要注意的是创建 nc 文件的 类 型 \color{#FF0000}{类型} ,以下供参考:

Value of cmodeDescription
‘NOCLOBBER’Prevent overwriting of existing file with the same name.(不覆盖已经有的同名文件)
‘CLOBBER’Overwrite any existing file with the same name.(覆盖任何已有的同名文件)
‘SHARE’Allow synchronous file updates.(允许同步文件更新)
‘64BIT_OFFSET’Allow easier creation of files and variables which are larger than two gigabytes.(可创建>2G的文件)
‘NETCDF4’Create a NetCDF-4/HDF5 file(普通)
‘CLASSIC_MODEL’Enforce the classic model; has no effect unless used in a bitwise-or with NETCDF4
outDir     = 'H:\TempData\SationPrecp';
outFile    = 'Precip_Daily_2481stations_China.nc';
outDirFile = [outDir, '\', outFile];
outid      = netcdf.create(outDirFile, '64BIT_OFFSET');

1. define dimensions

numStatdimID = netcdf.defDim(outid,'numStat', 2481);
yeardimID     = netcdf.defDim(outid,'year', 68);
mondimID     = netcdf.defDim(outid,'mon', 12);
daydimID    = netcdf.defDim(outid,'day', 31);

2. define variables

lon_id  = netcdf.defVar(outid,'lon','NC_FLOAT', numStatdimID);
lat_id  = netcdf.defVar(outid,'lat','NC_FLOAT', numStatdimID);
alt_id  = netcdf.defVar(outid,'alt','NC_FLOAT', numStatdimID);
Prec_id = netcdf.defVar(outid,'Prec','NC_FLOAT',[daydimID, mondimID, yeardimID, numStatdimID]);netcdf.endDef(outid);

3. put variables into nc

【提醒】 注意放置的数据时,变量一定要和自己在 2.定义维度 时的矩阵形状完全一致才行!这个形状也就是matllab 中用 n c d i s p ( n c n a m e ) \color{FF0000}{ncdisp(ncname)} ncdisp(ncname) 函数显示出来的矩阵形状。

netcdf.putVar(outid, lon_id, UnqLon/100);
netcdf.putVar(outid, lat_id, UnqLat/100);
netcdf.putVar(outid, alt_id, UnqAlt/100);
netcdf.putVar(outid, Prec_id, Precmmd);netcdf.reDef(outid);

4. put attribute into nc

netcdf.putAtt(outid, lon_id, 'longname', 'longitude');
netcdf.putAtt(outid, lon_id, 'units', 'degrees_east');netcdf.putAtt(outid, lat_id, 'longname', 'latitude');
netcdf.putAtt(outid, lat_id, 'bounds', 'degrees_north');netcdf.putAtt(outid, alt_id, 'longname', 'height above sea level');
netcdf.putAtt(outid, alt_id, 'units', 'm');netcdf.putAtt(outid, Prec_id, 'longname', 'daily rainfall');
netcdf.putAtt(outid, Prec_id, 'units', 'mm/day');
netcdf.putAtt(outid, Prec_id, '_FillValue', single(-999));
netcdf.putAtt(outid, Prec_id, 'missing_value', single(-999));

5. define global attribution

netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'title', 'station daily rainfall data');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Data_Period', '1951-2018');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Author', 'Dayang Wang, SYSU, Email : wangdy58@mail2.sysu.edu.cn');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'History', ['created on: ',datestr(now)]);netcdf.close(outid);

6.附上代码全文

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% Aim: this script aim at transfering Precp data of Station to nc type file
% Date: 2021-03-03
% Author: Dayang Wang, Sun Yat-sen University
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % tic
clc; clear;
% read data from station 
inDir = ('H:\TempData\SationPrecp');
inFile = ('PREC_Stn2481.mat');
inDirFile = [inDir, '\', inFile];
load (inDirFile);stationId = PREC(:, 1);
N = length(stationId);
Lat  = PREC(:, 2);
Lon  = PREC(:, 3);
Alt  = PREC(:, 4);
Year = PREC(:, 5);
Month = PREC(:, 6);
Day   = PREC(:, 7);
ValPrecp = PREC(:, 8);[Unqid, ind, ~] = unique(stationId, 'stable');
UnqLat = Lat(ind);
UnqLon = Lon(ind);
UnqAlt = Alt(ind);numStat = length(Unqid);
for i = 1 : numStat stationId(stationId == Unqid(i)) = i;
endYRbeg = min(Year);
YRend = max(Year);
numYR = YRend - YRbeg + 1;
for j = 1 : numYRYear(Year == YRbeg + j - 1 ) = j;
endPrecmmd = -999 * ones(31, 12, numYR, numStat);for k = 1 : NiDay  = Day(k);iMon  = Month(k);iYear = Year(k);iStat = stationId(k);Precmmd(iDay, iMon, iYear, iStat) = ValPrecp(k);% check scheduleif mod(k, 10^6) == 0disp(['----- ',num2str(k),' -----'])end
endPrecmmd = single(Precmmd);% PREC = [Day, Month, Year, stationId];%----------------------------------------------------------------------
% make nc
%----------------------------------------------------------------------
outDir     = 'H:\TempData\SationPrecp';
outFile    = 'Precip_Daily_2481stations_China.nc';
outDirFile = [outDir, '\', outFile];
outid      = netcdf.create(outDirFile, '64BIT_OFFSET');% define dimensions
numStatdimID = netcdf.defDim(outid,'numStat', 2481);
yeardimID     = netcdf.defDim(outid,'year', 68);
mondimID     = netcdf.defDim(outid,'mon', 12);
daydimID    = netcdf.defDim(outid,'day', 31);% define variables 
station_id  = netcdf.defVar(outid,'stid','NC_FLOAT', numStatdimID);
lon_id  = netcdf.defVar(outid,'lon','NC_FLOAT', numStatdimID);
lat_id  = netcdf.defVar(outid,'lat','NC_FLOAT', numStatdimID);
alt_id  = netcdf.defVar(outid,'alt','NC_FLOAT', numStatdimID);
Prec_id = netcdf.defVar(outid,'Prec','NC_FLOAT',[daydimID, mondimID, yeardimID, numStatdimID]);netcdf.endDef(outid);% put variables into nc
netcdf.putVar(outid, station_id, Unqid);
netcdf.putVar(outid, lon_id, UnqLon/100);
netcdf.putVar(outid, lat_id, UnqLat/100);
netcdf.putVar(outid, alt_id, UnqAlt/100);
netcdf.putVar(outid, Prec_id, Precmmd);netcdf.reDef(outid);% put attribute into nc
netcdf.putAtt(outid, lon_id, 'longname', 'original station id');netcdf.putAtt(outid, lon_id, 'longname', 'longitude');
netcdf.putAtt(outid, lon_id, 'units', 'degrees_east');netcdf.putAtt(outid, lat_id, 'longname', 'latitude');
netcdf.putAtt(outid, lat_id, 'bounds', 'degrees_north');netcdf.putAtt(outid, alt_id, 'longname', 'height above sea level');
netcdf.putAtt(outid, alt_id, 'units', 'm');netcdf.putAtt(outid, Prec_id, 'longname', 'daily rainfall');
netcdf.putAtt(outid, Prec_id, 'units', 'mm/day');
netcdf.putAtt(outid, Prec_id, '_FillValue', single(-999));
netcdf.putAtt(outid, Prec_id, 'missing_value', single(-999));% define global attribution
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Title', 'Station daily rainfall data');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Data Period', '1951-2018');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'Author', 'Dayang Wang, SYSU, Email : wangdy58@mail2.sysu.edu.cn');
netcdf.putAtt(outid, netcdf.getConstant('NC_GLOBAL'), 'History', ['created on: ',datestr(now)]);netcdf.close(outid);toc

文章同步推送 水文取经人 公众号,欢迎对水文、气象、陆面过程模拟研究方向感兴趣的大佬一起交流讨论!更多干货,敬请期待!
在这里插入图片描述

这篇关于制作nc文件流程——以站点降水数据为例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/205574

相关文章

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

Linux流媒体服务器部署流程

《Linux流媒体服务器部署流程》文章详细介绍了流媒体服务器的部署步骤,包括更新系统、安装依赖组件、编译安装Nginx和RTMP模块、配置Nginx和FFmpeg,以及测试流媒体服务器的搭建... 目录流媒体服务器部署部署安装1.更新系统2.安装依赖组件3.解压4.编译安装(添加RTMP和openssl模块

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

springboot启动流程过程

《springboot启动流程过程》SpringBoot简化了Spring框架的使用,通过创建`SpringApplication`对象,判断应用类型并设置初始化器和监听器,在`run`方法中,读取配... 目录springboot启动流程springboot程序启动入口1.创建SpringApplicat

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑