制作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

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

SpringBoot集成XXL-JOB实现任务管理全流程

《SpringBoot集成XXL-JOB实现任务管理全流程》XXL-JOB是一款轻量级分布式任务调度平台,功能丰富、界面简洁、易于扩展,本文介绍如何通过SpringBoot项目,使用RestTempl... 目录一、前言二、项目结构简述三、Maven 依赖四、Controller 代码详解五、Service