【问题标题】:Python remove coordinates with all especific variables - netcdfPython 删除所有特定变量的坐标 - netcdf
【发布时间】:2019-09-28 01:05:00
【问题描述】:

我有几个 netcdf 文件,想删除具有特定特征的数据并保存到新的 netcdf 文件。

该文件具有纬度、经度和时间坐标,具有可变的温度和风。这个想法是,如果风变量小于 6 m / s,则风和温度变量的所有信息(对于这个纬度、经度和时间)都将被删除。我如何在 python 中做到这一点?

目前为止

编辑:一个文件.nc

from netCDF4 import Dataset
from datetime import * 
import os

#-----------------------------------------------------------------------------------------------------------------------------------
path = '/home/Downloads/Arquivos_GOES_2018_OSISAF_NC/'

data_ini = datetime(2018,1,1,1,0,0) 
data_end = datetime(2018,7,1,1,0,0) 

IDval = 6 # minimo quality_level aceito

#-----------------------------------------------------------------------------------------------------------------------------------
DateTimeNow = data_ini 

while DateTimeNow<=data_end:


   namefile_net = DateTimeNow.strftime('%Y%m%d%H%M')+'00-OSISAF-L3C_GHRSST-SSTsubskin-GOES16-ssteqc_goes16_'+DateTimeNow.strftime('%Y%m%d_%H%M')+'00-v02.0-fv01.0.nc'


   if os.path.isfile(path+namefile_net) == True:


      data_net = Dataset(path+namefile_net)
      tsm = data_net.variables['sea_surface_temperature'][0,:]-273.15 
      lat = data_net.variables['lat'][:]
      lon = data_net.variables['lon'][:]
      wind = data_net.variables['wind_speed'][0,:]

      wind = np.where(qlv.mask==True,-1.0, qlv) 

      lon, lat = np.meshgrid(lon, lat) # criar uma matriz com lat e lon


      tsm_area = tsm[yid,xid] 
      lon_area = lon[yid,xid] 
      lat_area = lat[yid,xid] 
      wind_area = wind[yid,xid] 

      if np.sum(wind>=IDval) > 0:

         lon_area = np.where(qlv_area<IDval,-999.9,lon_area)
         lat_area = np.where(qlv_area<IDval,-999.9,lat_area)

         yid2, xid2 = seach_point(lon_area, lat_area, lon_point, lat_point)
         ```


【问题讨论】:

    标签: python netcdf


    【解决方案1】:

    您的数据似乎已经在 numpy.这是一个想法(没有你的 cdf,所以让我们假设第 3 列是风):

    >>> y = np.zeros((4,4))
    >>> data = np.zeros((4,4))
    >>> data[:,3] = [1,12, 4, 9]
    >>> data
    array([[  0.,   0.,   0.,   1.],
           [  0.,   0.,   0.,  12.],
           [  0.,   0.,   0.,   4.],
           [  0.,   0.,   0.,   9.]])
    >>> data[ data[:,3] > 6]
    array([[  0.,   0.,   0.,  12.],
           [  0.,   0.,   0.,   9.]])
    

    【讨论】:

      【解决方案2】:

      所以,我将使用以下策略来完成任务:

      • 复制原始文件

      • 替换新文件中的数据

      这是第一部分中包含一些数据生成器的示例:

      #!/usr/bin/env ipython
      # ----------------------------------------
      import numpy as np
      from netCDF4 import Dataset,num2date,date2num
      import os, datetime, subprocess
      # ---------------------------------------
      # Some input:
      londata = np.arange(9.,31.,0.5);nx=np.size(londata);
      latdata = np.arange(54.,66.,0.25);ny=np.size(latdata);
      ntime = 4000;
      timevals = np.linspace(0.,ntime*3600.0,ntime);
      dataout = 0.+np.random.random((ntime,ny,nx))*25.0
      # =========================================
      # GENERATE SOME RANDOM FILE WITH ORIGINAL DATA:
      genfile = 'data.nc'
      ncout=Dataset(genfile,'w');
      ncout.createDimension('lon',nx)
      ncout.createDimension('lat',ny)
      ncout.createDimension('time')
      # ------------------------------------------
      xvar = ncout.createVariable('lon','float32',('lon'));xvar[:]=londata
      yvar = ncout.createVariable('lat','float32',('lat'));yvar[:]=latdata
      tvar = ncout.createVariable('time','float64',('time'));tvar[:] = timevals;tvar.setncattr('units','seconds since 2019-01-01 00:00:00');
      dvar = ncout.createVariable('wspeed','float32',('time','lat','lon'));
      dvar[:] = dataout;
      ncout.close();
      # =========================================
      # REMOVEDATA:
      filein = genfile;
      fileout = filein.replace('.nc','.modified.nc')
      cstr = 'cp '+filein+' '+fileout;
      subprocess.call(cstr,shell=True); # Let us make the copy of the file, so that we do not have to copy the variables on our own
      # ----------------------------------------
      ncout = Dataset(fileout,'a');
      datain = ncout.variables['wspeed'][:];
      dataout = np.array(datain,copy=True);# I am making a real copy of the data, just in case I want to compare afterwards
      dataout[dataout<6.0] = np.nan; # So here I replace the values
      ncout.variables['wspeed'][:] = dataout; # I replace the values in the file
      ncout.close();
      

      【讨论】:

        猜你喜欢
        • 2019-03-04
        • 2018-06-06
        • 1970-01-01
        • 1970-01-01
        • 2020-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-09
        相关资源
        最近更新 更多