Python使用所有especific变量删除坐标-netcdf

时间:2019-09-26 18:29:23

标签: python netcdf

我有几个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)
         ```


2 个答案:

答案 0 :(得分: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.]])

答案 1 :(得分:0)

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

  • 复制原始文件

  • 将数据替换到新文件中

下面是一个带有一些数据生成器的示例:

#!/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();