質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

968閲覧

pythonでall the input arrays must have same number of dimensionsエラーが発生します。

Daimian

総合スコア53

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/02/06 04:08

編集2019/02/06 04:14

前提・実現したいこと

気象情報をNASAのHPから取得し、地図を描画、csv出力するコードです。
拡張子nsのファイルをダウンロード後、下記のpythonのプログラムを通してmapを描画、csvファイル出力を試みていますが、下記エラーが発生します。

発生している問題・エラーメッセージ

Traceback (most recent call last): File "ISS_LIS_FlashLoc_Quickview2.py", line 66, in <module> flash_event_time = np.concatenate([flash_event_time,time_result]) ValueError: all the input arrays must have same number of dimensions

どうやら同じ次元の配列にしないとconcatenate出来ないということみたいです。。どう解決すれば良いでしょうか?
問題の

flash_event_time = np.concatenate([flash_event_time,time_result])

という部分ですが、time_resultは7487284.33333のようなfloat型であるはずで、flash_event_timeは空配列のはずなんですが。。。

ソースコード

ISS_LIS_FlashLoc_Quickview2.py

python3

1# -*- coding: utf-8 -*- 2 3#### Import Python packages #### 4import numpy as np 5import glob 6from netCDF4 import Dataset 7import matplotlib.pyplot as plt 8from mpl_toolkits.basemap import Basemap 9import csv 10try: 11 # Python 2 12 from itertools import izip 13except ImportError: 14 # Python 3 15 izip = zip 16import datetime 17 18default_time = datetime.datetime(1993, 1, 1, 00, 00, 00) 19 20#Define the file directories 21dataDir = '/Users/hoge/projects/sxsw2019/NASA_challenge/nc/20180104/' #File path where ISS LIS data are stored 22csvfile = "/Users/hoge/projects/sxsw2019/NASA_challenge/csv/isslis_flashloc_test.csv" #File path and name of CSV file 23 24#Identify all NetCDF files in the directory 25files = glob.glob(dataDir+'*.nc') 26 27#Create empty arrays to populate lightning flash location coordinates 28flash_lat = np.array([]) 29flash_lon = np.array([]) 30flash_event_time = np.array([]) 31 32 33#Loop through list of NetCDF files, for each file, extract the lightning flash latidude 34#and longitude, and add to the respective empty array (flash_lat and flash_lon) 35for i in files: 36 datafile = Dataset(i) 37 38 flash_lat = np.concatenate([flash_lat,datafile.variables['lightning_flash_lat'][:]]) #add to array 39 flash_lon = np.concatenate([flash_lon,datafile.variables['lightning_flash_lon'][:]]) #add to array 40 41 input_time = datafile.variables['lightning_event_TAI93_time'] 42 time = input_time[0] 43 time_result = default_time + datetime.timedelta(seconds = float(time)) 44 45 flash_event_time = np.concatenate([flash_event_time,time_result]) 46 47#Create CSV files of values from the populated flash_lat/lon arrays 48with open(csvfile, 'w') as myfile: 49 writer = csv.writer(myfile) 50 writer.writerows(izip(["flash_lat"], ["flash_lon"], ["flash_event_time"])) #Define headers in row (izip creates columns) 51 writer.writerows(izip(flash_lat,flash_lon,flash_event_time)) #Define data rows (izip creates columns) 52 53 54#Create plot of lightning flash location heat map 55plt.figure(figsize=((20,20))) #Set plot dimensions 56map = Basemap(projection='cyl', lon_0 = 0, resolution='c') 57lightning = map.hexbin(flash_lon, flash_lat, gridsize=300,bins='log',cmap='jet',mincnt=1,zorder=10) #Bin flash counts into hexbins using a gridsize of your choice 58 59#Draw geographic boundaries and meridians/parallels 60map.drawmapboundary(fill_color='k') 61map.fillcontinents(color='grey',lake_color='grey') 62map.drawcoastlines(color='white') 63map.drawcountries(color='white') 64map.drawmeridians(np.arange(0,390,30), labels=[0,0,0,1],fontsize=10, color="lightgray") 65map.drawparallels(np.arange(-90,120,30), labels=[1,0,0,0],fontsize=10, color="lightgray") 66 67cbar = map.colorbar(lightning,location='bottom',pad="5%") 68cbar.set_label('Flash Count') #Remember to change label 69 70plt.title('ISS LIS Detected Lightning Flash Locations January 4, 2018', fontsize = 18) #Rember to change title 71 72 73plt.show() 74

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tiitoi

2019/02/06 04:39

flash_event_time と time_result の形状はどうなっていますか。 print(flash_event_time.shape) などで見れるので
Daimian

2019/02/06 04:49

回答ありがとうございます!!!! flash_event_time.shape 出力結果 → (0,) time_result 出力結果 → print(time_result.shape)    AttributeError: 'datetime.datetime' object has no attribute 'shape'
tiitoi

2019/02/06 04:53

flash_event_time はスカラーの値で、time_result は datetime.datetime オブジェクトなのですね。 numpy.concatenate は同じ型同士でないと結合できないので使えないと思います。 この2つの変数をどのようにしたいのでしょうか?
Daimian

2019/02/06 05:04

またまた迅速なご回答ありがとうございます。 最終的に、 writer.writerows(izip(flash_lat,flash_lon,flash_event_time)) という項目でcsvファイルに記述しますので、flash_event_time配列にtime_resultを追加したいです!
Daimian

2019/02/06 07:54

色々と迅速なご対応誠にありがとうございます!確かにおっしゃる通りnumpy配列にこだわり必要はございませんでした。通常の配列に格納し、実装を行いました。誠にありがとうございます!
guest

回答1

0

ベストアンサー

flash_event_time配列に時間情報time_resultを追加するのであれば、

flash_event_time.append(time_result)

でよろしいかと。

https://qiita.com/AfricaUmare/items/22de1ca413176e4a19a6

投稿2019/02/06 04:52

t_obara

総合スコア5488

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Daimian

2019/02/06 07:53

ご指摘の通り、numpyを使用せずに通常の配列で実装しました。ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問