def setUp(self): self.file = FILE_NAME file = NetCDFFile(self.file, 'w') file.createDimension('n', None) # use unlimited dim. foo = file.createVariable('maskeddata', 'f8', ('n', )) foo.missing_value = missing_value bar = file.createVariable('packeddata', 'i2', ('n', )) bar.scale_factor = scale_factor bar.add_offset = add_offset foo[0:ndim] = maskedarr bar[0:ndim] = packeddata file.close()
def setUp(self): self.file = FILE_NAME file = NetCDFFile(self.file,'w') file.createDimension('n', None) # use unlimited dim. foo = file.createVariable('maskeddata', 'f8', ('n',)) foo.missing_value = missing_value bar = file.createVariable('packeddata', 'i2', ('n',)) bar.scale_factor = scale_factor bar.add_offset = add_offset foo[0:ndim] = maskedarr bar[0:ndim] = packeddata file.close()
def get_landmask(geogr_file,polarstereogr_file): path='/scratch/clisap/seaice/OWN_PRODUCTS/MELT_PONDS/tmp/' if not os.path.exists(path+polarstereogr_file): os.system('grdlandmask '+opts(G,['Rlatlon','I'])+' -Df -N1/NaN/1/NaN/NaN -G'+path+geogr_file+' -V') #-Nocean/land/lake/island/pond. #os.system('grdcut '+path+geogr_file+xy_region+'-G'+path+polarstereogr_file+' -V ') os.system('grdproject '+path+geogr_file+' '+opts(G,['Rll'])+' -A -C -G'+path+polarstereogr_file+' -V') data=NetCDFFile(path+polarstereogr_file) #print data,data.dimensions,data.variables.keys(): x=array(data.variables['x'][:]) y=array(data.variables['y'][:]) z=array(data.variables['z'][:,:]) #z[z<=0.5]=0 #z[z>0.5]=1 #zz=array(z,dtype=int) return x,y,z
def runTest(self): """testing auto-conversion of masked arrays and packed integers""" # no auto-conversion. file = NetCDFFile(self.file, maskandscale=False) datamasked = file.variables['maskeddata'] datapacked = file.variables['packeddata'] # check missing_value, scale_factor and add_offset attributes. assert datamasked.missing_value == missing_value assert datapacked.scale_factor == scale_factor assert datapacked.add_offset == add_offset assert_array_equal(datapacked[:], packeddata2) assert_array_almost_equal(datamasked[:], ranarr) file.close() # auto-conversion file = NetCDFFile(self.file) datamasked = file.variables['maskeddata'] datapacked = file.variables['packeddata'] assert_array_almost_equal(datamasked[:].filled(), ranarr) assert_array_almost_equal(datapacked[:], packeddata, decimal=4) file.close()
def runTest(self): """testing auto-conversion of masked arrays and packed integers""" # no auto-conversion. file = NetCDFFile(self.file,maskandscale=False) datamasked = file.variables['maskeddata'] datapacked = file.variables['packeddata'] # check missing_value, scale_factor and add_offset attributes. assert datamasked.missing_value == missing_value assert datapacked.scale_factor == scale_factor assert datapacked.add_offset == add_offset assert_array_equal(datapacked[:],packeddata2) assert_array_almost_equal(datamasked[:],ranarr) file.close() # auto-conversion file = NetCDFFile(self.file) datamasked = file.variables['maskeddata'] datapacked = file.variables['packeddata'] assert_array_almost_equal(datamasked[:].filled(),ranarr) assert_array_almost_equal(datapacked[:],packeddata,decimal=4) file.close()
print(metalist) for folder in metalist: print folder jahr = folder[-16:-12] julday = folder[-11:-8] date = jahr + '_' + julday workfile = folder + date + '_mosaic' print('work on ' + workfile) if not os.path.exists( '/scratch/clisap/seaice/OWN_PRODUCTS/MELT_PONDS/PRODUCTS/daily/' + date + '/' + date + '_ow.data'): print date + ' does not exist' file1, file2, file4 = workfile + '1.nc', workfile + '2.nc', workfile + '4.nc' #read Netcdfs and extract Bands mb1, mb2, mb4 = NetCDFFile(file1), NetCDFFile(file2), NetCDFFile(file4) print('read nc') #MX=array(mb1.variables['x'][:]) #MY=array(mb1.variables['y'][:]) MB1 = array(mb1.variables['z'][:, :]) MB2 = array(mb2.variables['z'][:, :]) MB4 = array(mb4.variables['z'][:, :]) titlestr = 'MODIS mosaic- ' + date MB1[MB1 == 0.] = NaN MB2[MB2 == 0.] = NaN MB4[MB4 == 0.] = NaN BAND1 = MB1.flatten() BAND2 = MB2.flatten() BAND4 = MB4.flatten() R = array([BAND1, BAND2, BAND4]) print('...done')
os.system('mkdir ' + workfolder + year + '_' + str(d) + '_mosaic/') workfolder_mosaic = workfolder + year + '_' + str(d) + '_mosaic/' workfile = workfolder_mosaic + year + '_' + str(d) + '_mosaic' blendfile = workfile + str(band) + '.job' blendoutfile = workfile + str(band) + '.nc' ctabfile = workfile + str(band) + '.ctab' mapfile = workfile + str(band) + '.ps' piclist = glob.glob(workfolder + year + '_' + str(d) + '*/*' + str(band) + '.nc') print(piclist) mosaic = file(blendfile, 'w') for fn in piclist: print('now working on ' + fn) file1 = fn #read Netcdfs and extract Bands mb1 = NetCDFFile(file1) print('read nc') MX = array(mb1.variables['x'][:]) MY = array(mb1.variables['y'][:]) xmin = min(MX) xmax = max(MX) ymin = min(MY) ymax = max(MY) mosaic.write(fn + ' -R' + str(xmin) + '/' + str(xmax) + '/' + str(ymin) + '/' + str(ymax) + ' 1' + '\n') mosaic.close() #print('######') print(file(blendfile).read()) #print('######') G = {} cs = 0.5 #500m
def plot_etopo(file, m, ax): from copy import deepcopy try: from mpl_toolkits.basemap import NetCDFFile except: from netCDF4 import Dataset as NetCDFFile latll = m.llcrnrlat latur = m.urcrnrlat lonll = m.llcrnrlon lonur = m.urcrnrlon # Read NetCDF ETOPO file try: data = NetCDFFile(file) except: print('plot_etopo: Incorrect ETOPO NetCDF file') raise 'WARNING: error encountered while plotting ETOPO' lats = data.variables['lat'][:] lons = deepcopy(data.variables['lon'][:]) wraplons(lons) ila = [] ilo = [] for i in range(len(lats)): if lats[i] >= latll and lats[i] <= latur: ila.append(i) for i in range(len(lons)): if lons[i] >= lonll and lons[i] <= lonur: ilo.append(i) ila = np.array(ila) ilo = np.array(ilo) inds = np.argsort(lons[ilo]) ilo = ilo[inds] la = lats[ila] lo = lons[ilo] z = data.variables['z'][ila[0]:ila[-1] + 1, ilo] lon, lat = np.meshgrid(lo, la) x, y = m(lon, lat) # Colormaps Lref = 0.35 # Colorbar half length minz = z.min() maxz = z.max() Laxo, Laxc, ticko, tickc = prepColorbar(minz, maxz, Lref) H = float(Laxo + Laxc) / 2.0 oceancmap = plt.cm.Blues_r # Ocean depth colormap if Laxc > 0.: # Elevation colormap cmaps = [plt.cm.YlGn, plt.cm.BrBG] offs = [0, 0] cuts = [0, 5] prop = [0.5, 0.5] elevcmap = concatCmap(cmaps, offs, cuts, prop) else: elevcmap = plt.cm.YlGn # Ocean contour if minz < 0.: plt.axes(ax) zo = np.ma.masked_where(z >= 0., z) co = m.contourf(x, y, zo, 30, cmap=oceancmap) if Laxo > 0.: # Ocean depth colorbar caxo = plt.axes([0.45 - H, 0.04, Laxo, 0.02]) plt.title('Ocean Depth, m', fontsize='medium') cbo = plt.colorbar(mappable=co, cax=caxo, ticks=ticko, format='%.0f', orientation='horizontal') # Land contour if maxz >= 0.: plt.axes(ax) zc = np.ma.masked_where(z < 0., z) cc = m.contourf(x, y, zc, 30, cmap=elevcmap) if Laxc > 0.: # Elevation colorbar caxc = plt.axes([0.45 - H + Laxo, 0.04, Laxc, 0.02]) plt.title('Elevation, m', fontsize='medium') cbc = plt.colorbar(mappable=cc, cax=caxc, ticks=tickc, format='%.0f', orientation='horizontal') plt.axes(ax)
from mpl_toolkits.basemap import Basemap, NetCDFFile import matplotlib.pyplot as plt import numpy as np # read in netCDF sea-surface temperature data # can be a local file, a URL for a remote opendap dataset, # or (if PyNIO is installed) a GRIB or HDF file. ncfile = NetCDFFile('data/sst.nc') sst = ncfile.variables['sst'][:] lats = ncfile.variables['lat'][:] lons = ncfile.variables['lon'][:] # create Basemap instance for mollweide projection. # coastlines not used, so resolution set to None to skip # continent processing (this speeds things up a bit) m = Basemap(projection='moll', lon_0=0, lat_0=0, resolution=None) # compute map projection coordinates of grid. x, y = m(*np.meshgrid(lons, lats)) # plot with pcolor im = m.pcolormesh(x, y, sst, shading='flat', cmap=plt.cm.gist_ncar) # draw parallels and meridians, but don't bother labelling them. m.drawparallels(np.arange(-90., 120., 30.)) m.drawmeridians(np.arange(0., 420., 60.)) # draw line around map projection limb. # color map region background black (missing values will be this color) m.drawmapboundary(fill_color='k') # draw horizontal colorbar. plt.colorbar(orientation='horizontal') plt.show()
#choose test_scene metalist = glob.glob(workfolder + '2008*/') print(metalist) #nn_file='net5neu.data' mosaic = '/scratch/clisap/seaice/OWN_PRODUCTS/MELT_PONDS/GRID_MOSAIC/2008_169_mosaic/' #only for no mp test #mosaic='/scratch/clisap/seaice/OWN_PRODUCTS/MELT_PONDS/GRID_MOSAIC/2000_129_mosaic/' print('now work on day ' + mosaic) jahr = mosaic[-16:-12] #2008 tag = mosaic[-11:-8] #'06' workfile = mosaic + jahr + '_' + tag + '_mosaic' file1, file2, file3, file4 = workfile + '1.nc', workfile + '2.nc', workfile + '3.nc', workfile + '4.nc' titlestr = 'MOD09_mosaic_' + str(tag) + '_' + str(jahr) print(titlestr) #read Netcdfs and extract Bands mb1, mb2, mb3, mb4 = NetCDFFile(file1), NetCDFFile(file2), NetCDFFile( file3), NetCDFFile(file4) print('read nc') ########################find subset coordinates possitions """#Franklin bay and surrounding lon_y=-400. #center coordinates of the plot in polar stereo lat_x=-2000. f=500. #canadian archipelaga fy ice plains lon_y=-850. #center coordinates of the plot in polar stereo lat_x=-1350. f=100. """ #AEREAS={'canadian':[-850,-1350,100.],'fram':[-1200,700,200.],'multiyear':[-100,-1400,200.],'franklin':[-400,-2000,500.]}