如何从 tif 使用 python netCDF4 创建 netCDF 文件?

问题描述

我想将带有 tif 图像的文件夹转换为 netcdf

import numpy as np
import datetime as dt
import os
import gdal
import netCDF4
import re
import xarray as xr

#PRIMERA IMAGEN 
ds = gdal.Open("first_image")

a = ds.ReadAsArray()
nlat,nlon = np.shape(a)

b = ds.GetGeoTransform()  # bBox,interval
lon = np.arange(nlon)*b[1]+b[0]
lat = np.arange(nlat)*b[5]+b[3]

#FECHA DE LA PRIMERA IMAGEN
basedate = dt.datetime(2020,10,16,9,0)

# Create NetCDF file
nco = netCDF4.Dataset("File.nc",'w',format='NETCDF4')

nco.description = 'Example simulation data'

# Create dimensions,variables and attributes:
nco.createDimension('lon',nlon)
nco.createDimension('lat',nlat)
nco.createDimension('time',None)

timeo = nco.createVariable('time','f4',('time',))
timeo.units = 'hours since 2012-09-28 00:00:00'
timeo.standard_name = 'time'

lono = nco.createVariable('lon',('lon',))
lono.standard_name = 'longitude'

lato = nco.createVariable('lat',('lat',))
lato.standard_name = 'latitude'

# Create short integer variable for temperature data,with chunking
tmno = nco.createVariable('tmn','lat','lon'),zlib=True,fill_value=-9999)
tmno.units = 'degC'
tmno.scale_factor = 1#0.01
tmno.add_offset = 0.00
tmno.long_name = 'minimum monthly temperature'
tmno.standard_name = 'air_temperature'
tmno.set_auto_maskandscale(False)

nco.Conventions = 'CF-1.6'

# Write lon,lat
lono[:] = lon
lato[:] = lat


itime = 0

# Step through data,writing time and data to NetCDF
for root,dirs,files in os.walk(r'folder_images'):
    dirs.sort()
    files.sort()
    for f in files:
        if f[-3:]=='tif':
            print(f)
            # read the time values by parsing the filename
            year = 2012
            mon = 9
            day=28
            hora=int(f[0:2])
            date = dt.datetime(year,mon,day,hora,0)
            print(date)
            dtime = (date-basedate).total_seconds()/3600
            timeo[itime] = dtime
            # min temp
            tmn_path = os.path.join(root,f)
            print(tmn_path)
            tmn = gdal.Open(tmn_path)
            a = tmn.ReadAsArray()  # data
            tmno[itime,:,:] = a
            itime = itime+1
            tmn=None
nco.close()
ds =None

当我将 netcdf 文件添加到 Qgis 时,坐标是错误的:

范围 0.5000000000000000,0.5000000000000000 : 804.5000000000000000,559.5000000000000000

栅格范围是: 范围 576172.0000000000000000,4143175.8648832864128053 : 616453.8192302760435268,4171198.00000000000000000000

我做错了什么?

图片https://drive.google.com/file/d/13TkWx3UgSyBt48nm0UMC6GJRO_Nwoch6/view?usp=sharing

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...