简体   繁体   中英

Error using .assign_latitude_longitude: AttributeError: crs attribute is not available

I am working with the NDFD forecast datasets and would like to assign lat and long within the array to give me the ability to plot the data outside of matplotlib. My versions are:

python 3.8,metpy 1.0.0,cartopy 0.18.0, xarray 0.16.2, siphon 0.8.0

import os.path
import sys
import xarray
from xarray.backends import NetCDF4DataStore
import xarray as xr
import pyproj

from datetime import *

import cartopy.feature as cfeature
#import cartopy.io.shapereader as shpreader
import metpy
import siphon
import math

from siphon.catalog import TDSCatalog
from siphon.radarserver import RadarServer
from siphon.cdmr import Dataset

import cartopy.crs as ccrs
import numpy as np
import pandas as pd
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
#from awips.dataaccess import DataAccessLayer
%matplotlib inline

gfs_catalog = ('https://thredds.ucar.edu/thredds/catalog/grib/NCEP/NDFD/NWS/CONUS/CONDUIT/catalog.xml?dataset=grib/NCEP/NDFD/NWS/CONUS/CONDUIT/Best')
cat = TDSCatalog(gfs_catalog)
ncss = cat.datasets[0].subset()
query = ncss.query()
query.time_range(datetime.utcnow(),datetime.utcnow()+timedelta(days=7))
query.accept('netcdf') 
query.variables('Total_precipitation_surface_6_Hour_Accumulation','Total_precipitation_surface_12_Hour_Ac
cumulation_probability_above_0p254')

query.lonlat_box(west=-86.175, east=-86.0, north=39.85, south=39.75)
data = ncss.get_data(query)
ds = xr.open_dataset(NetCDF4DataStore(data))

#This works to return x and y in metpy's custom projection
var = ds.metpy.parse_cf('Total_precipitation_surface_6_Hour_Accumulation')

#This doesn't
var2 = ds.metpy.assign_latitude_longitude(force='False')

AttributeError: crs attribute is not available.

assign_latitude_longitude requires that information about the CRS is available, so that it can properly calculation longitude and latitude from your projected coordinates. This means that you first need to have called parse_cf() in some way. In your code above, you save the results of calling parse_cf('Total_precipitation_surface_6_Hour_Accumulation') to var , but this does not modify the original Dataset stored in ds .

Instead you need to either call it on the variable that has had the information parsed as:

var = var.metpy.assign_latitude_longitude(force=False)

(note False rather than 'False' ). You can also use parse_cf() and assign_latitude_longitude() on the full dataset:

ds = ds.metpy.parse_cf()
ds = ds.metpy.assign_latitude_longitude(force=False)

Note that this will download the metadata for all variables in the latter case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM