#!/usr/local/bin/perl 
#
#  指定範囲の一元化初動メカニズムの分布図を作成するGMTshellを生成する。
#
#  2002/12/04 Koji Nakamura (JMA)
#
#  Ver.1.0

#
# help
#
	if ($ARGV[0] =~ /-h/) {
		print "使い方:make_mecmap.pl -start:YYYYMMDDHHMM -end:YYYYMMDDHHMM -n:??? -s:??? -w:??? -e:??? -m:??? -M:??? -d:??? -D:??? -path:??? -o:OUTFILE -x -L:dep0/dep1/dep2/dep3/dep4/dep5 \n";
		print "引数  -start:YYYYMMDDHHMM   期間の始めの年月日時分 \n";
		print "      -end:YYYYMMDDHHMM     期間の終りの年月日時分 \n";
		print "      -n:???                地図の範囲の北の端の緯度 \n";
		print "      -s:???                地図の範囲の南の端の緯度 \n";
		print "      -e:???                地図の範囲の東の端の経度 \n";
		print "      -w:???                地図の範囲の西の端の経度 \n";
		print "      -d:???                深さの最小値 \n";
		print "      -D:???                深さの最大値 \n";
		print "      -m:???                マグニチュードの最小値 \n";
		print "      -M:???                マグニチュードの最大値 \n";
		print "      -path:???             一元化メカニズムのファイルのあるパス名 \n";
		print "      -o:???                最終的に作成するpsファイルの名前 \n";
		print "      -x                    震源球に軸の場所を示す\n";
		print "      -L:dep0/dep1/dep2/dep3/dep4/dep5  深さの範囲の区切りを指定する.(-d -Dより優先)\n";
		die;
	}
#
#       日数計算の定数セット
#
	&ymd1920_dataset;
#
#  引数解析
#
        ($e_side,$w_side,$s_side,$n_side,$mag_min,$mag_max,$dep_min,$dep_max,
	$out_file,$stdate,$endate,$ax_mode,$path) = &get_argv;

#
#       期間の設定
#
	$year = substr($stdate,0,4);
	$mon  = substr($stdate,4,2);
	$day  = substr($stdate,6,2);
	$ji   = substr($stdate,8,2);
	$hun  = substr($stdate,10,2);
	$start_date = &day1920($year,$mon,$day)+$ji/24 + $hun/24/60;
	#期間始めの文字列(凡例用)	
	$ymd1 = sprintf("%04d/%02d/%02d %02d:%02d",$year,$mon,$day,$ji,$hun);

	$year = substr($endate,0,4);
	$mon  = substr($endate,4,2);
	$day  = substr($endate,6,2);
	$ji   = substr($endate,8,2);
	$hun  = substr($endate,10,2);
	$end_date = &day1920($year,$mon,$day)+$ji/24 + $hun/24/60;
	#期間終りの文字列
	$ymd2 = sprintf("%04d/%02d/%02d %02d:%02d",$year,$mon,$day,$ji,$hun);

#
#  地図パラメータの設定
#
	#
	#  地図の大きさによって解像度を変える 
	#
	$delta_lat = abs($n_side - $s_side);
	$delta_lon = abs($e_side - $w_side);
	if (($delta_lat < 2) || ($delta_lon < 2)) {
		$map_fine = "f";
	} elsif  (($delta_lat < 5) || ($delta_lon < 5)) {
		$map_fine = "h";
	} elsif  (($delta_lat < 10) || ($delta_lon < 10)) {
		$map_fine = "i";
	} else  {
		$map_fine = "l";
	}

	#  地図の中心の位置等	
	$ct_lon = ($e_side + $w_side)*0.5;
	$ct_lat = ($n_side + $s_side)*0.5;
	$ln_side = $ct_lat + ($n_side - $ct_lat)*0.5 ;
	$ls_side = $ct_lat;

	#  地図上と実際の座標との簡単な換算係数
	$lat_len = ($n_side - $s_side)*111000;  
	$lon_len = ($e_side - $w_side)*111000*cos($ct_lat/180*3.1416);
	
	#枠のマーク設定
	if (($n_side - $s_side) > 25) {
		$ano_pich = 10;
		$tick_pich = 5;
	} elsif (($n_side - $s_side) > 10) {
		$ano_pich = 5;
		$tick_pich = 2.5;
	} elsif (($n_side - $s_side) > 10) {
		$ano_pich = 2;
		$tick_pich = 1;
	} elsif (($n_side - $s_side) > 5) {
		$ano_pich = 1;
		$tick_pich = 0.5;
	} elsif (($n_side - $s_side) > 1) {
		$ano_pich = 1;
		$tick_pich = 0.5;
	} else  {
		$ano_pich = 0.5;
		$tick_pich = 0.16667;
	} 

	# 地図領域の幅
	$map_width = 16;                
	# 左のマージン
	$x_margin = int(21 - $map_width)/2 - 0.5; 
	# 上のマージン
	$y_margin = int(30 - ($map_width*$lat_len/$lon_len))*0.85; 
	# 紙の上の長さと緯度経度の対応関係
	$paper2map_lon = ($e_side - $w_side)/16;  # cm ---> degree ただし緯度によってずれる。
	$length_al_lat = 16*($n_side - $s_side)/(($e_side - $w_side)*cos($ct_lat/180*3.1416));
	$paper2map_lat = ($n_side - $s_side)/$length_al_lat;  # cm ---> degree
	# 震源球の基本的な大きさ
	$mag_sc_onmap = 0.75;

	#
	#  深さ別の色設定
	#
	$tmpcpt = "tmp.cpt";

	# 深さ区分
	if ($depth_line) { # 
		# 深さ区分の引数指定があった時
		@dep_div =split("/",$depth_line);
		$dep_min = $dep_div[0];
		$dep_max = $dep_div[$#dep_div];
	} else {  # 
		if (!($dep_max)) {
			@dep_div =(0,10,30,50,100,200);
                } elsif ($dep_max <= 100) { # 100kmより浅い地震
                        @dep_div =(0,10,30,50,$dep_max);
                } elsif ($dep_max <= 200) { # 200kmより浅い地震
                        @dep_div =(0,10,30,50,100,$dep_max);
                } elsif ($dep_max <= 400) { # 400kmより浅い地震
                        @dep_div =(0,50,100,150,200,$dep_max);
                } else { # 400kmより深い地震
                        @dep_div =(0,100,200,300,400,$dep_max);
                }
	}

	# 深さの色	
	@dep_col =("255/0/0","255/0/255","0/255/255","0/255/0","0/0/255"); 

	#
	#  一時的な色指定ファイルの作成
	#
	open(CPTFILE,">".$tmpcpt);
	for ($i=0;$i<(@dep_div-1);$i++) {
		$cptdep1 = $dep_div[$i];
		$cptdep2 = $dep_div[$i+1];
		($cpt_r,$cpt_g,$cpt_b) = split("/",$dep_col[$i]);
		printf CPTFILE ("%d %d %d %d %d %d %d %d \n",$cptdep1,$cpt_r,$cpt_g,$cpt_b,$cptdep2,$cpt_r,$cpt_g,$cpt_b);
	}
	printf CPTFILE ("%s %d %d %d \n","B",0,0,0);
	printf CPTFILE ("%s %d %d %d \n","F",255,255,255);
	printf CPTFILE ("%s %d %d %d \n","N",128,128,128);
	close(CPTFILE);

	if (!($dep_min)) {
		$dep_min = $dep_div[0];
	}
	if (!($dep_max)) {
		$dep_max = $dep_div[$#dep_div];
	}

#
#  データ検索
# 

	# ファイルリストの取得
	if (!($path =~ /\/$/)) { $path = $path."/";}
	opendir(DIR,$path) || die "指定パスからファイルが読みこめません. \n";
	@mchfiles1 = grep { /MCH[0-9]{6}$/ } readdir(DIR); #月報ファイル
	closedir DIR;
	opendir(DIR,$path) || die "指定パスからファイルが読みこめません. \n";
	@mchfiles2 = grep { /mch[0-9]{6}_[ABC]/ } readdir(DIR); #速報ファイル
	closedir DIR;

	@mchfiles = (@mchfiles1,@mchfiles2);

	# ファイルからデータの取得
	foreach $file_val (sort @mchfiles) { 
		$mecfile = $path.$file_val;
		if ((-r $mecfile) && (-f $mecfile)) {
			open(REF_FILE,$mecfile) ;
			while(<REF_FILE>) {
				chomp;
				if(/^J/) {  #震源行の処理
					$hypoline = $_;
			        	($year_ref,$month_ref,$day_ref,$hour_ref,$min_ref,$sec1_ref,$sec2_ref,
       	         			$lat_ref,$lon_ref,$dep_ref,$mag_ref,$area)=get_eqinfo($hypoline);
		
				} elsif (/^M/) {	
					$mecline  = $_;

					# 必要な地震かどうかの判別
					$eqdate = &day1920($year_ref,$month_ref,$day_ref)+$hour_ref/24 + $min_ref/24/60;
					if (($eqdate >= $start_date) && ($eqdate <= $end_date)) {
						if (($mag_ref >= $mag_min) && ($mag_ref <= $mag_max)) {
							if (($lat_ref >= $s_side) && ($lat_ref <= $n_side) 
						   	&& ($lon_ref >= $w_side) && ($lon_ref <= $e_side)) {
								if (($dep_ref >= $dep_min) && ($dep_ref <= $dep_max)) {
									#メカニズム行の処理
       	                						 ($p1,$p2,$t1,$t2,$n1,$n2,$strike1_a,$dip1_a,$rake1_a,
       		                 					    $strike1_b,$dip1_b,$rake1_b) = get_mgyo_info($mecline);
								
							 		push(@ref_lat,$lat_ref);
									push(@ref_lon,$lon_ref);
									push(@ref_dep,$dep_ref);
									push(@ref_mag,$mag_ref);
									push(@ref_strike,$strike1_a);
									push(@ref_dip,$dip1_a);
									push(@ref_slip,$rake1_a);
								}
							}
						}
					}
				}
			}		
			close(REF_FILE);
		}
	}

#  
#   GMT スクリプトの出力 
#	
	printf("#!/bin/sh\n");
	printf("gmtdefaults -D > .gmtdefaults\n");
	printf("gmtset DOTS_PR_INCH 1200\n");

	#地形	
	printf("#--- topography ---\n");
	printf("pscoast -JL%-5.1f/%-4.1f/%-4.1f/%-4.1f/%-2d -R%-5.1f/%-5.1f/%-4.1f/%-4.1f -Ba%-3.1ff%-3.1fWeSn -Na -D%s -G255/235/205 -S224/255/255 -W1 -P -X%-4.1f -Y%-4.1f -K -U/-1.0/-1.5/:\"REDC\" > %s\n",$ct_lon,$ct_lat,$ls_side,$ln_side,$map_width,$w_side,$e_side,$s_side,$n_side,$ano_pich,$tick_pich,$map_fine,$x_margin,$y_margin,$out_file);
	printf("psxy /jma/data/map/trench_data_jma -JL -R -M -W1 -P -O -K -: >> %s\n",$out_file);
	printf("psxy /jma/data/map/border_data -: -JL -R -M -W1 -P -O -K >> %s\n",$out_file);

	#メカニズム
	printf("#--- mechanism distribution ---\n");
	for($i=0;$i<@ref_lat;$i++){
		$dep_color = &get_depcolor($ref_dep[$i],@dep_div);
		if ($dep_color < 0) {
		} else {
			printf("psmeca -R -Sa%f -JL -W1 -P -G%s %s -O -K << END >> %s\n",$mag_sc_onmap,$dep_col[$dep_color],$ax_mode,$out_file);
			printf("%f %f %f %d %d %d %3.1f %f %f\n",$ref_lon[$i],$ref_lat[$i],$ref_dep[$i],$ref_strike[$i],$ref_dip[$i],$ref_slip[$i],($ref_mag[$i]/10),$ref_lon[$i],$ref_lat[$i]);
			printf("END\n");
		}
	}

#
#  図の凡例など
#
	printf("#--- explanatory notes ---\n");

	# 期間	
	$spc_peri = 0.5 + 0.5*($n_side - $s_side)/25;
	$period ="Period:".$ymd1."--".$ymd2;
	$period_size = 15;
	$period_font = 5; # 5 Times-Bold	
	printf("pstext -R -JL -B -P -N -O -K <<END>> %s\n",$out_file);
	printf("%f %f %d 0 %d CB %s \n",$ct_lon,($n_side + $spc_peri*$paper2map_lat),$period_size,$period_font,$period);
        printf("END\n");
 
	# magnitude
	$mag_font_size = 14;
	$mag_font = 5;
	$cap_mag_min = int($mag_min/10);
	$cap_mag_max = int($mag_max/10);
	$spc_keisu = 1.1;
	if ($cap_mag_max < $mag_max/10) { $cap_mag_max++; }	


	#$cap_mag_lon = $e_side + $paper2map_lon*($mag_sc_onmap+0.5);
	#$cap_mag_lat = $ct_lat + 0.25*$paper2map_lat;
	$cap_mag_lon = 17.0; 
	$cap_mag_lat = ($ct_lat - $s_side)/$paper2map_lat;
	for($i=$cap_mag_min;$i<= $cap_mag_max;$i++) {
		if ($i < 5) {
			#$cap_mag_lat = $cap_mag_lat + $mag_sc_onmap*$paper2map_lat;
			$cap_mag_lat = $cap_mag_lat + $mag_sc_onmap*$spc_keisu;
		} else {
			#$cap_mag_lat = $cap_mag_lat + $mag_sc_onmap*$paper2map_lat + ($i - 5.0)*$mag_sc_onmap*0.2*$paper2map_lat;
			$cap_mag_lat = $cap_mag_lat + $mag_sc_onmap*$spc_keisu + ($i - 5.0)*$mag_sc_onmap*0.2;
		}

                printf("psmeca -R0/21/0/30 -Sa%f -JX21/30 -W1 -P -G%s %s -N -O -K << END >> %s\n",$mag_sc_onmap,"255/0/0",$ax_mode,$out_file);
                printf("%f %f %f %d %d %d %3.1f %f %f\n",$cap_mag_lon,$cap_mag_lat,0,327,50,10,$i,$cap_mag_lon,$cap_mag_lat);
                printf("END\n");

		printf("pstext -R -JX  -P -N -O -K <<END>> %s\n",$out_file);
		printf("%f %f %d 0 %d 5 %s \n",($cap_mag_lon + $mag_sc_onmap*1.2),$cap_mag_lat,$mag_font_size,$mag_font,$i);
       		printf("END\n");
	}

	$cap_mag_lat = $cap_mag_lat + $mag_sc_onmap + ($i - 5.0)*$mag_sc_onmap*0.1;
	printf("pstext -R -JX  -P -N -O -K <<END>> %s\n",$out_file);
	printf("%f %f %d 0 %d CM %s \n",$cap_mag_lon,$cap_mag_lat,($mag_font_size+2),$mag_font,"M");
       	printf("END\n");

	#
	# 深さのスケール
	#
	$sc_len = ($ct_lat- $s_side)/$paper2map_lat*0.8;
	$sc_x = ($e_side - $w_side)/$paper2map_lon + 1.0;
	$sc_y = $sc_len*0.5 + 0.8;
	$sc_width = 0.2;	
	printf("psscale -C%s -D%f/%f/%f/%f -O -K >> %s\n",$tmpcpt,$sc_x,$sc_y,$sc_len,$sc_width,$out_file);
	printf("pstext -R0/21/0/30 -JX21/30 -P -N -O <<END>> %s\n",$out_file);
	printf("%f %f %d 0 %d CT %s \n",17.5,0,($mag_font_size),$mag_font,"Depth(km)");
       	printf("END\n");
       	printf("# remove cptfile\n");
       	printf("rm tmp.cpt\n");

sub get_depcolor {
# 深さの区分の決定
	my($depth,@divs) = @_;
	my($modori);

	if ($depth < $divs[0]) {
		$modori = -1;
	} elsif ($depth < $divs[1]) {
		$modori = 0;
	} elsif ($depth < $divs[2]) {
		$modori = 1;
	} elsif ($depth < $divs[3]) {
		$modori = 2;
	} elsif ($depth < $divs[4]) {
		$modori = 3;
	} elsif ($depth < $divs[5]) {
		$modori = 4;
	} else {
		$modori = -1;
	}

	$modori;
}
	
sub ymd1920_dataset
# this is for calculate Julian date
# data of date
{
     $imd1{"1, 1"} = 0;
     $imd1{"2, 1"} = 31;
     $imd1{"3, 1"} = 60;
     $imd1{"4, 1"} = 91;
     $imd1{"5, 1"} = 121;
     $imd1{"6, 1"} = 152;
     $imd1{"7, 1"} = 182;
     $imd1{"8, 1"} = 213;
     $imd1{"9, 1"} = 244;
     $imd1{"10, 1"} = 274;
     $imd1{"11, 1"} = 305;
     $imd1{"12, 1"} = 335;
     $imd1{"13, 1"} = 366;
     $imd1{"1, 2"} = 0;
     $imd1{"2, 2"} = 31;
     $imd1{"3, 2"} = 59;
     $imd1{"4, 2"} = 90;
     $imd1{"5, 2"} = 120;
     $imd1{"6, 2"} = 151;
     $imd1{"7, 2"} = 181;
     $imd1{"8, 2"} = 212;
     $imd1{"9, 2"} = 243;
     $imd1{"10, 2"} = 273;
     $imd1{"11, 2"} = 304;
     $imd1{"12, 2"} = 334;
     $imd1{"13, 2"} = 365;
     $imd2{"1, 1"} = 31;
     $imd2{"2, 1"} = 29;
     $imd2{"3, 1"} = 31;
     $imd2{"4, 1"} = 30;
     $imd2{"5, 1"} = 31;
     $imd2{"6, 1"} = 30;
     $imd2{"7, 1"} = 31;
     $imd2{"8, 1"} = 31;
     $imd2{"9, 1"} = 30;
     $imd2{"10, 1"} = 31;
     $imd2{"11, 1"} = 30;
     $imd2{"12, 1"} = 31;
     $imd2{"1, 2"} = 31;
     $imd2{"2, 2"} = 28;
     $imd2{"3, 2"} = 31;
     $imd2{"4, 2"} = 30;
     $imd2{"5, 2"} = 31;
     $imd2{"6, 2"} = 30;
     $imd2{"7, 2"} = 31;
     $imd2{"8, 2"} = 31;
     $imd2{"9, 2"} = 30;
     $imd2{"10, 2"} = 31;
     $imd2{"11, 2"} = 30;
     $imd2{"12, 2"} = 31;

}

sub day1920 {
# JULIAN IY,IM,ID --> Julian DAY (-4712/ 1/1 =0)--> (1920/ 1/1 =0)

   my($iy,$im,$id,$iday,$tp1);
   my($jd,$ny0,$ngy0);

   $iy = $_[0]+0;
   $im = $_[1]+0;
   $id = $_[2]+0;
   if ($iy <= 0) {
      $iday = 365*$iy + &syou($iy,4) + 1721057;
      $tp1 = ((-1 * $iy) % 4) + 7;
      $jd = &syou($tp1,4);
      $iday = $iday + $imd1{"$im".", $jd"} + $id;
      $iday = $iday - 2422325;
   }
   else {
      $ny0 = $iy - 1;
      $iday = 365 * $ny0 + &syou($ny0,4) +1721423;
      $tp1 = ($iy % 4) + 7;
      $jd = &syou($tp1,4);

      if ($iy > 1582) {
         $ngy0 = $iy -1601;
         $iday = $iday - &syou($ngy0,100) + &syou($ngy0,400) - 10;
         if (($iy % 100) != 0) {
             $iday = $iday + $imd1{"$im".", $jd"} + $id;
             $iday = $iday - 2422325;
         }
        else {
            if (($iy % 400) == 0) {
             $jd = 1;
            } else {
             $jd = 2;
            }
             $iday = $iday + $imd1{"$im".", $jd"} + $id;
             $iday = $iday - 2422325;

         }
      }
      elsif ($iy < 1582) {
         $iday = $iday + $imd1{"$im".", $jd"} + $id;
         $iday = $iday - 2422325;
      }
      elsif ($im > 10) {
         $ngy0 = $iy -1601;
         $iday = $iday - &syou($ngy0,100) + &syou($ngy0,400) - 10;
         if (($iy % 100) != 0) {
             $iday = $iday + $imd1{"$im".", $jd"} + $id;
             $iday = $iday - 2422325;
         }
         else {
            if (($iy % 400) == 0) {
             $jd = 1;
            } else {
             $jd = 2;
            }
             $iday = $iday + $imd1{"$im".", $jd"} + $id;
             $iday = $iday - 2422325;
         }
      }
      elsif ($im < 10) {
         $iday = $iday + $imd1{"$im".", $jd"} + $id;
         $iday = $iday - 2422325;
      }
      elsif ($id < 5) {
         $iday = $iday + $imd1{"$im".", $jd"} + $id;
         $iday = $iday - 2422325;
      }
      else {
         if ($id > 15) {
             $iday = $iday - 10;
             $iday = $iday + $imd1{"$im".", $jd"} + $id;
             $iday = $iday - 2422325;
         }
         else {
             $iday = 2299161;
             $iday = $iday - 2422325;
         }
      }
   }

   $iday;

}

sub syou {
# 割り算
  my($x1,$x2,$x3);
  $x1 = $_[0];
  $x2 = $_[1];
  if ($x1*$x2 != 0) {
    $x3 = int($x1/$x2);
  }
  elsif ($x2 == 0) {
     $x3 = undef;
   }
  elsif ($x1 == 0) {
     $x3 = 0;
  }
}

sub get_eqinfo {
# 気象庁書式の震源行の解析 
#
   my($dat) = @_;
   my($year,$month,$day,$hour,$min,$sec1,$sec2,$area);
   my($elat,$elon,$edep,$mag);
   my($mag_type);

   $year = substr($dat,1,4);
   $month = substr($dat,5,2);
   $day = substr($dat,7,2);
   $hour = substr($dat,9,2);
   $min = substr($dat,11,2);
   $sec1 = substr($dat,13,2);
   $sec2 = substr($dat,15,1);
   $area = substr($dat,68,24);
   $elat = substr($dat,21,3) + (substr($dat,24,1)*1000 + substr($dat,25,1)*100 +
                                  substr($dat,26,1)*10 + substr($dat,27,1))/6000;
   $elat = $elat + 0.00005;
   $elat =~ /(.*\.....).*/;
   $elat = $1;

   $elon = substr($dat,32,4) + (substr($dat,36,1)*1000 + substr($dat,37,1)*100 +
                                  substr($dat,38,1)*10 + substr($dat,39,1))/6000;

   $elon = $elon + 0.00005;
   $elon =~ /(.*\.....).*/;
   $elon = $1;

   $edep = substr($dat,44,3) + (substr($dat,47,1)*10 +
					substr($dat,48,1)*1)/100;
   $edep = $edep + 0.005;
   $edep =~ /(.*\...).*/;
   $edep = $1;

   $mag_type = substr($dat,54,1);
   if (($mag_type eq "J") || ($mag_type eq "D") || ($mag_type eq "V") || 
       ($mag_type eq "B") || ($mag_type eq "S")) {   
        $mag = substr($dat,52,2);
	$mag = &jmamag($mag);	
   } else {
       $mag = "NG";
   }

   ($year,$month,$day,$hour,$min,$sec1,$sec2,$elat,$elon,$edep,$mag,$area);
}

sub get_mgyo_info {
#M行の情報
   my($line) = @_;
   my($strike_a,$dip_a,$rake_a);
   my($strike_b,$dip_b,$rake_b);
   my($p_azi,$p_dip,$t_azi,$t_dip,$n_azi,$n_dip);
   my($toroku);

   $p_azi    = substr($line,19,4);
   $p_dip    = substr($line,23,3);
   $t_azi    = substr($line,26,4);
   $t_dip    = substr($line,30,3);
   $n_azi    = substr($line,33,4);
   $n_dip    = substr($line,37,3);

   $strike_a = substr($line,40,4);
   $dip_a    = substr($line,44,3);
   $rake_a   = substr($line,47,5);
   $strike_b = substr($line,52,4);
   $dip_b    = substr($line,56,3);
   $rake_b   = substr($line,59,5);

       if ($line =~ /LOWS/) {
            $toroku   = "S";
       } elsif ($line =~ /LOW /) {
            $toroku   = "K";
       } else {
            $toroku   = "?";
       }

   ($p_azi,$p_dip,$t_azi,$t_dip,$n_azi,$n_dip,
    $strike_a,$dip_a,$rake_a,
    $strike_b,$dip_b,$rake_b, $toroku);

}

sub jmamag {
#  マグニチュードの変換
#
	my($mag) = @_;
	my($modori);

	if ($mag =~ /^[A-Z]/) {
		$modori = (ord("A") - ord(substr($mag,0,1)) - 1)*10 - substr($mag,1,1); 
	} else {
		$modori = $mag;
	}
	$modori;		
}

sub get_argv {
#
#  引数解析
#
        my($e_side,$w_side,$s_side,$n_side);
	my($mag_min,$mag_max,$dep_min,$dep_max);
	my($out_file,$stdate,$endate,$ax_mode,$path);
        my($i);
 
	$ax_mode = " ";

        for($i=0;$i<@ARGV;$i++) {
                if ($ARGV[$i] =~ /^-e:/) {
                        $ARGV[$i] =~ s/^-e://;
                        $e_side = $ARGV[$i];
                } elsif ($ARGV[$i] =~ /^-w:/) {
                        $ARGV[$i] =~ s/^-w://;
                        $w_side = $ARGV[$i];
                } elsif ($ARGV[$i] =~ /^-n:/) {
			$ARGV[$i] =~ s/^-n://;
                        $n_side = $ARGV[$i];
                } elsif ($ARGV[$i] =~ /^-s:/) {
                        $ARGV[$i] =~ s/^-s://;
                        $s_side = $ARGV[$i];
                } elsif ($ARGV[$i] =~ /^-m:/) {
                        $ARGV[$i] =~ s/^-m://;
                        $mag_min = $ARGV[$i]*10;
                } elsif ($ARGV[$i] =~ /^-M:/) {
                        $ARGV[$i] =~ s/^-M://;
                        $mag_max = $ARGV[$i]*10;
                } elsif ($ARGV[$i] =~ /^-d:/) {
                        $ARGV[$i] =~ s/^-d://;
                        $dep_min = $ARGV[$i];
                } elsif ($ARGV[$i] =~ /^-D:/) {
                        $ARGV[$i] =~ s/^-D://;
                        $dep_max = $ARGV[$i];
                } elsif ($ARGV[$i] =~ /^-o:/) {
                        $ARGV[$i] =~ s/^-o://;
                        $out_file = $ARGV[$i];
                } elsif ($ARGV[$i] =~ /^-start:/) {
                        $ARGV[$i] =~ s/^-start://;
                        $stdate = $ARGV[$i];
                } elsif ($ARGV[$i] =~ /^-end:/) {
                        $ARGV[$i] =~ s/^-end://;
                        $endate = $ARGV[$i];
                } elsif ($ARGV[$i] =~ /^-x/) { # 軸表示 
                        $ax_mode = "-a0.1c/c";
                } elsif ($ARGV[$i] =~ /^-path:/) { # データファイルのパス
                        $ARGV[$i] =~ s/^-path://;
                        $path = $ARGV[$i];
                } elsif ($ARGV[$i] =~ /^-L:/) { # 深さ区分の指定 
                        $ARGV[$i] =~ s/^-L://;
                        $depth_line = $ARGV[$i];
                }
        }

	# 引数指定が省略された時の暫定初期値
        if (!($e_side)) {
                $e_side = 137;
        }
        if (!($w_side)) {
                $w_side = 134;
        }
        if (!($n_side)) {
                $n_side = 36;
        }
        if (!($s_side)) {
                $s_side = 34;
        }
        if (!($mag_min)) {
                $mag_min = 00;
        }
        if (!($mag_max)) {
                $mag_max = 90;
        }
        if (!($out_file)) {
                $out_file = "mec.ps";
        }
        if (!($stdate)) {
                $stdate = "200001010000";
        }
        if (!($endate)) {
                $endate = "200001312359";
        }
        if (!($path)) {
                $path = "./";
        }

        return ($e_side,$w_side,$s_side,$n_side,$mag_min,$mag_max,$dep_min,$dep_max,
	$out_file,$stdate,$endate,$ax_mode,$path);
} 
